View | Details | Raw Unified | Return to bug 77761 | Differences between
and this patch

Collapse All | Expand All

(-)PlatformConfiguration.java (-31 / +65 lines)
Lines 7-25 Link Here
7
 * 
7
 * 
8
 * Contributors:
8
 * Contributors:
9
 *     IBM Corporation - initial API and implementation
9
 *     IBM Corporation - initial API and implementation
10
 * 	   Phil Loats/Erxiang Liu (IBM Corp.) - fix to only use Foundation APIs
10
 *******************************************************************************/
11
 *******************************************************************************/
11
package org.eclipse.update.internal.configurator;
12
package org.eclipse.update.internal.configurator;
12
13
13
import java.io.*;
14
import java.io.*;
14
import java.lang.reflect.*;
15
import java.lang.reflect.*;
15
import java.net.*;
16
import java.net.*;
16
import java.nio.channels.*;
17
//import java.nio.channels.*;
17
import java.util.*;
18
import java.util.*;
18
19
19
import javax.xml.parsers.*;
20
import javax.xml.parsers.*;
20
import javax.xml.transform.*;
21
//import javax.xml.transform.*;
21
import javax.xml.transform.dom.*;
22
//import javax.xml.transform.dom.*;
22
import javax.xml.transform.stream.*;
23
//import javax.xml.transform.stream.*;
23
24
24
import org.eclipse.core.runtime.*;
25
import org.eclipse.core.runtime.*;
25
import org.eclipse.osgi.service.datalocation.*;
26
import org.eclipse.osgi.service.datalocation.*;
Lines 40-48 Link Here
40
 */
41
 */
41
public class PlatformConfiguration implements IPlatformConfiguration, IConfigurationConstants {
42
public class PlatformConfiguration implements IPlatformConfiguration, IConfigurationConstants {
42
43
44
	private static final String XML_ENCODING = "UTF-8";
43
	private static PlatformConfiguration currentPlatformConfiguration = null;
45
	private static PlatformConfiguration currentPlatformConfiguration = null;
44
	private static final DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
46
	private static final DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
45
	private static final TransformerFactory transformerFactory = TransformerFactory.newInstance();
47
	//private static final TransformerFactory transformerFactory = TransformerFactory.newInstance();
46
48
47
	private Configuration config;
49
	private Configuration config;
48
	private URL configLocation;
50
	private URL configLocation;
Lines 52-58 Link Here
52
	private boolean featuresChangeStampIsValid;
54
	private boolean featuresChangeStampIsValid;
53
	private long pluginsChangeStamp;
55
	private long pluginsChangeStamp;
54
	private boolean pluginsChangeStampIsValid;
56
	private boolean pluginsChangeStampIsValid;
55
	private FileLock lock;
57
	//private FileLock lock;
56
58
57
	private static final String ECLIPSE = "eclipse"; //$NON-NLS-1$
59
	private static final String ECLIPSE = "eclipse"; //$NON-NLS-1$
58
	private static final String CONFIG_HISTORY = "history"; //$NON-NLS-1$
60
	private static final String CONFIG_HISTORY = "history"; //$NON-NLS-1$
Lines 214-224 Link Here
214
216
215
		SiteEntry result = config.getSiteEntry(key);	
217
		SiteEntry result = config.getSiteEntry(key);	
216
		if (result == null) { // retry with decoded URL string
218
		if (result == null) { // retry with decoded URL string
217
			try {
219
//			try {
218
				key = URLDecoder.decode(key, "UTF-8"); //$NON-NLS-1$
220
//				key = URLDecoder.decode(key, "UTF-8"); //$NON-NLS-1$
219
			} catch (UnsupportedEncodingException e) {
221
			key = URLDecoder.decode(key);			
220
				// ignore
222
//			} catch (UnsupportedEncodingException e) {
221
			}
223
//				// ignore
224
//			}
222
			result = config.getSiteEntry(key);
225
			result = config.getSiteEntry(key);
223
		}
226
		}
224
			
227
			
Lines 746-771 Link Here
746
	 * @param url configuration directory
749
	 * @param url configuration directory
747
	 */
750
	 */
748
	private void getConfigurationLock(URL url) {
751
	private void getConfigurationLock(URL url) {
749
		if (!url.getProtocol().equals("file")) //$NON-NLS-1$
752
//		if (!url.getProtocol().equals("file")) //$NON-NLS-1$
750
			return;
753
//		return;
751
754
752
		File lockFile = new File(url.getFile(), ConfigurationActivator.NAME_SPACE+ File.separator+CONFIG_FILE_LOCK_SUFFIX);
755
//	File lockFile = new File(url.getFile(), ConfigurationActivator.NAME_SPACE+ File.separator+CONFIG_FILE_LOCK_SUFFIX);
753
		verifyPath(url);
756
//	verifyPath(url);
754
		try {
757
//	try {
755
			RandomAccessFile raf = new RandomAccessFile(lockFile, "rw"); //$NON-NLS-1$
758
//		RandomAccessFile raf = new RandomAccessFile(lockFile, "rw"); //$NON-NLS-1$
756
			lock = raf.getChannel().lock();
759
//		lock = raf.getChannel().lock();
757
		} catch (IOException ioe) {
760
//	} catch (IOException ioe) {
758
			lock = null;
761
//		lock = null;
759
		}	
762
//	}	
760
	}
763
	}
761
	
764
	
762
	private void clearConfigurationLock() {
765
	private void clearConfigurationLock() {
763
		if (lock != null) {
766
//		if (lock != null) {
764
			try {
767
//		try {
765
				lock.channel().close();
768
//			lock.channel().close();
766
			} catch (IOException ioe) {
769
//		} catch (IOException ioe) {
767
			}
770
//		}
768
		}
771
//	}
769
	}
772
	}
770
	
773
	
771
774
Lines 1073-1084 Link Here
1073
		return installURL;
1076
		return installURL;
1074
	}
1077
	}
1075
	
1078
	
1079
	//Old saveASXML, has to use standard XML parser and use javax.tranform.* package.
1080
	/*
1076
	private void saveAsXML(OutputStream stream) throws CoreException {	
1081
	private void saveAsXML(OutputStream stream) throws CoreException {	
1077
		StreamResult result = null;
1082
		StreamResult result = null;
1078
		try {
1083
		try {
1079
			DocumentBuilder docBuilder = documentBuilderFactory.newDocumentBuilder();
1084
			DocumentBuilder docBuilder = documentBuilderFactory.newDocumentBuilder();
1080
			Document doc = docBuilder.newDocument();
1085
			Document doc = docBuilder.newDocument();
1081
1086
			
1082
			if (config == null)
1087
			if (config == null)
1083
				throw Utils.newCoreException(Messages.getString("PlatformConfiguration.cannotSaveNonExistingConfig"),null); //$NON-NLS-1$
1088
				throw Utils.newCoreException(Messages.getString("PlatformConfiguration.cannotSaveNonExistingConfig"),null); //$NON-NLS-1$
1084
			
1089
			
Lines 1086-1093 Link Here
1086
			doc.appendChild(doc.createComment("Created on " + config.getDate().toString())); //$NON-NLS-1$
1091
			doc.appendChild(doc.createComment("Created on " + config.getDate().toString())); //$NON-NLS-1$
1087
			Element configElement = config.toXML(doc);
1092
			Element configElement = config.toXML(doc);
1088
			doc.appendChild(configElement);
1093
			doc.appendChild(configElement);
1089
1094
		
1090
			// Write out to a file
1095
			//	Write out to a file .
1091
			
1096
			
1092
			Transformer transformer=transformerFactory.newTransformer();
1097
			Transformer transformer=transformerFactory.newTransformer();
1093
			transformer.setOutputProperty(OutputKeys.METHOD, "xml"); //$NON-NLS-1$
1098
			transformer.setOutputProperty(OutputKeys.METHOD, "xml"); //$NON-NLS-1$
Lines 1105-1111 Link Here
1105
			result.setOutputStream(null);
1110
			result.setOutputStream(null);
1106
			result = null;
1111
			result = null;
1107
		}
1112
		}
1108
	}
1113
	} 
1114
	*/
1115
	
1116
	private void saveAsXML(OutputStream stream) throws CoreException,IOException {	
1117
		OutputStreamWriter xmlWriter = new OutputStreamWriter(stream,XML_ENCODING);
1118
		try {
1119
			DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
1120
			factory.setExpandEntityReferences(false);
1121
			factory.setValidating(false);
1122
			factory.setIgnoringComments(true);
1123
			DocumentBuilder docBuilder = factory.newDocumentBuilder();
1124
			Document doc = docBuilder.newDocument();
1125
			
1126
			if (config == null)
1127
				throw Utils.newCoreException(Messages.getString("PlatformConfiguration.cannotSaveNonExistingConfig"),null); //$NON-NLS-1$
1128
			
1129
			config.setDate(new Date());
1130
			Element configElement = config.toXML(doc);
1131
			doc.appendChild(configElement);
1132
			
1133
			XMLPrintHandler.printComment(xmlWriter,"Created on " + config.getDate().toString());
1134
			XMLPrintHandler.printNode(xmlWriter,doc,XML_ENCODING);
1135
			
1136
		} catch (Exception e) {
1137
			throw Utils.newCoreException("", e); //$NON-NLS-1$
1138
		} finally {
1139
			xmlWriter.close();
1140
			xmlWriter = null;
1141
		}
1142
	} 
1109
	
1143
	
1110
	private void reconcile() throws CoreException {
1144
	private void reconcile() throws CoreException {
1111
		long lastChange = config.getDate().getTime();
1145
		long lastChange = config.getDate().getTime();
(-)XMLPrintHandler.java (+122 lines)
Added Link Here
1
/*******************************************************************************
2
 * Copyright (c) 2004 IBM Corporation and others.
3
 * All rights reserved. This program and the accompanying materials 
4
 * are made available under the terms of the Common Public License v1.0
5
 * which accompanies this distribution, and is available at
6
 * http://www.eclipse.org/legal/cpl-v10.html
7
 * 
8
 * Contributors:
9
 *     Erxiang Liu - initial API and implementation
10
 *******************************************************************************/
11
package org.eclipse.update.internal.configurator;
12
13
import java.io.IOException;
14
import java.io.OutputStreamWriter;
15
16
import org.w3c.dom.Document;
17
import org.w3c.dom.NamedNodeMap;
18
import org.w3c.dom.Node;
19
import org.w3c.dom.NodeList;
20
21
22
public class XMLPrintHandler {
23
	//	used to print XML file
24
	public static final String XML_COMMENT_END_TAG = "-->";
25
	public static final String XML_COMMENT_BEGIN_TAG = "<!--";
26
	public static final String XML_HEAD = "<?xml version=\"1.0\" encoding=\"";
27
	public static final String XML_HEAD_END_TAG = "?>";
28
	public static final String XML_COMMA = "\"";
29
	public static final String XML_SPACE = " ";
30
	public static final String XML_BEGIN_TAG = "<";
31
	public static final String XML_END_TAG = ">";
32
	public static final String XML_EQUAL = "=";
33
	public static final String XML_SLASH = "/";
34
35
	public static void printBeginElement(OutputStreamWriter xmlWriter, String elementString) throws IOException{
36
		StringBuffer temp = new StringBuffer(XML_BEGIN_TAG);
37
		temp.append(elementString).append(XML_END_TAG).append("\n");
38
		xmlWriter.write(temp.toString());
39
40
	}
41
42
	public static void printEndElement(OutputStreamWriter xmlWriter, String elementString) throws IOException{
43
		StringBuffer temp = new StringBuffer(XML_BEGIN_TAG);
44
		temp.append(XML_SLASH).append(elementString).append(XML_END_TAG).append("\n");
45
		xmlWriter.write(temp.toString());
46
47
	}
48
49
	
50
	public static void printText(OutputStreamWriter xmlWriter, String text) throws IOException{
51
		xmlWriter.write(text.toString());
52
	}
53
54
	public static void printComment(OutputStreamWriter xmlWriter, String comment)throws IOException {
55
		StringBuffer temp = new StringBuffer(XML_COMMENT_BEGIN_TAG);
56
		temp.append(comment).append(XML_COMMENT_END_TAG).append("\n");
57
		xmlWriter.write(temp.toString());
58
	}
59
60
	public static void printHead(OutputStreamWriter xmlWriter, String encoding) throws IOException {
61
		StringBuffer temp = new StringBuffer(XML_HEAD);
62
		temp.append(encoding).append(XML_COMMA).append(XML_HEAD_END_TAG).append("\n");
63
		xmlWriter.write(temp.toString());
64
	}
65
66
	public static String wrapAttributeForPrint(String attribute, String value) throws IOException {
67
		StringBuffer temp = new StringBuffer(XML_SPACE);
68
		temp.append(attribute).append(XML_EQUAL).append(XML_COMMA)
69
				.append(value).append(XML_COMMA);
70
		return temp.toString();
71
72
	}
73
74
	public static void printNode(OutputStreamWriter xmlWriter, Node node,String encoding)  throws Exception{
75
		if (node == null) {
76
			return;
77
		}
78
79
		switch (node.getNodeType()) {
80
		case Node.DOCUMENT_NODE: {
81
			printHead(xmlWriter,encoding);
82
			printNode(xmlWriter, ((Document) node).getDocumentElement(),encoding);
83
			break;
84
		}
85
		case Node.ELEMENT_NODE: {
86
			//get the attribute list for this node.
87
			StringBuffer tempElementString = new StringBuffer(node.getNodeName());
88
			NamedNodeMap attributeList = node.getAttributes();
89
			if ( attributeList != null ) {
90
				for(int i= 0; i <attributeList.getLength();i++){
91
					Node attribute = attributeList.item(i);
92
					tempElementString.append(wrapAttributeForPrint(attribute.getNodeName(),attribute.getNodeValue()));
93
				}
94
			}
95
			printBeginElement(xmlWriter,tempElementString.toString());
96
			
97
			// do this recusively for the child nodes.
98
			NodeList childNodes = node.getChildNodes();
99
			if (childNodes != null) {
100
				int length = childNodes.getLength();
101
				for (int i = 0; i < length; i++) {
102
					printNode(xmlWriter, childNodes.item(i),encoding);
103
				}
104
			}
105
			
106
			printEndElement(xmlWriter,node.getNodeName());
107
			break;
108
		}
109
		
110
		case Node.TEXT_NODE: {
111
			xmlWriter.write(node.getNodeValue());
112
			break;
113
		}
114
		default: {
115
			throw new UnsupportedOperationException("Unsupported XML Node Type.");
116
			
117
		}
118
		}
119
120
	}
121
122
}

Return to bug 77761