Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.
View | Details | Raw Unified | Return to bug 202626 | Differences between
and this patch

Collapse All | Expand All

(-)src-config/org/eclipse/tptp/platform/agentcontroller/config/ConfigUpgrade.java (+147 lines)
Added Link Here
1
/**********************************************************************
2
 Copyright (c) 2008 IBM Corporation and others.
3
 All rights reserved.   This program and the accompanying materials
4
 are made available under the terms of the Eclipse Public License v1.0
5
 which accompanies this distribution, and is available at
6
 http://www.eclipse.org/legal/epl-v10.html
7
 $Id$
8
  
9
 Contributors:
10
     IBM Rational - initial implementation
11
 **********************************************************************/
12
package org.eclipse.tptp.platform.agentcontroller.config;
13
14
import java.io.File;
15
import java.io.FileOutputStream;
16
import java.io.IOException;
17
import java.util.Properties;
18
import javax.xml.parsers.DocumentBuilderFactory;
19
import javax.xml.parsers.ParserConfigurationException;
20
21
import org.eclipse.tptp.platform.agentcontroller.config.Allow;
22
import org.eclipse.tptp.platform.agentcontroller.config.ConfigFile;
23
import org.eclipse.tptp.platform.agentcontroller.config.ConfigUtility;
24
import org.eclipse.tptp.platform.agentcontroller.config.Constants;
25
import org.eclipse.tptp.platform.agentcontroller.config.Logger;
26
import org.eclipse.tptp.platform.agentcontroller.config.SecurityEnabled;
27
import org.eclipse.tptp.platform.agentcontroller.config.UserDefinition;
28
import org.w3c.dom.Document;
29
import org.w3c.dom.Node;
30
import org.w3c.dom.NodeList;
31
import org.xml.sax.SAXException;
32
33
/**
34
 * Sample usage: <br>
35
 * ConfigUpgrade upgrade = ConfigUpgrade.getInstance("<AC HOME>\\config\\serviceconfig.xml"); <br>
36
 * upgrade.parseDocument();
37
 * 
38
 * @author Joel Cayne
39
 * @version February 29, 2008
40
 * @since February 29, 2008
41
 * 
42
 */
43
public class ConfigUpgrade {
44
45
	protected ConfigFile configFile;
46
	protected Document doc;
47
	protected Properties properties;
48
	
49
	private static ConfigUpgrade instance = null;
50
	
51
	/**
52
	 * The method called to acquire a reference of the singleton ConfigUpgrade.
53
	 * @param filename The path to the serviceconfig.xml to retrieve an existing configuration from.
54
	 * @return An instance of ConfigUpgrade.
55
	 */
56
	public static ConfigUpgrade getInstance(String filename) {
57
		if(instance == null) {
58
			instance = new ConfigUpgrade(filename); 
59
		}
60
		return instance;
61
	}
62
63
	/**
64
	 * Provides for upgrading an existing Agent Controller installation.
65
	 * @param filename The path to the serviceconfig.xml to retrieve an existing configuration from.
66
	 */
67
	private ConfigUpgrade(String filename) {
68
		try {
69
			doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(new File(filename));
70
			configFile = new ConfigFile(filename);
71
			properties = new Properties();
72
		} catch (SAXException e) {
73
			Logger.err(e.getMessage());
74
		} catch (IOException e) {
75
			Logger.err(e.getMessage());
76
		} catch (ParserConfigurationException e) {
77
			Logger.err(e.getMessage());
78
		}
79
	}
80
81
	/**
82
	 * Parses the serviceconfig.xml and obtains the user provided values.
83
	 * @return The properties hash with the user entered values.
84
	 */
85
	public Properties parseDocument() {
86
		properties.put(Constants.JAVA_PATH, configFile.getValue(Constants.JAVA_PATH));
87
		addElement(SecurityEnabled.TAG);
88
		addElement(UserDefinition.TAG);
89
		addElement(Allow.TAG, ConfigUtility.getString("Config.Allow.Host.Tag"));
90
		return properties;
91
	}
92
	
93
	/**
94
	 * Searches for the tag in the serviceconfig.xml and adds it to the properties hash.
95
	 * @param tag The tag to search for.
96
	 */
97
	protected void addElement(String tag) {
98
		addElement(tag, null);
99
	}
100
	
101
	/**
102
	 * Searches for the tag containing the id attribute in serviceconfig.xml and adds it to the properties hash.
103
	 * @param tag The tag to search for.
104
	 * @param id The id attribute to search for in the tag.
105
	 */
106
	protected void addElement(String tag, String id) {
107
		NodeList nl = doc.getElementsByTagName(tag);
108
		if(nl != null) {
109
			/* Traverse the nodes with the tag name adding them to the properties hash */
110
			for(int i = 0; i < nl.getLength(); i++) {
111
				Node n = nl.item(i);
112
				String text = null;
113
				/* Obtain the attribute value and add it to the properties hash */
114
				if(id != null) {
115
					n = n.getAttributes().getNamedItem(id);
116
					if(n != null) {
117
						text = n.getTextContent();
118
						/* The tag exists in the properties hash */ 
119
						if (properties.containsKey(tag)) {
120
							text = (String) properties.get(tag) + "," + text;
121
						}
122
					}
123
				} else {
124
					/* Obtain a unique tag value and add it to the properties hash */
125
					text = n.getTextContent();
126
				}
127
				if(text != null) {
128
					properties.put(tag, text);
129
				}
130
			}
131
		}	
132
	}
133
134
	/**
135
	 * Outputs the user entered values to a property file and provides a properties hash table containing the user entered values.
136
	 * @param filename The name of the property file to store the user entered values to.
137
	 */
138
	public void getPropertyFile(File filename) {
139
		try {
140
			if(filename != null) {
141
				properties.store(new FileOutputStream(filename), null);
142
			}
143
		} catch (IOException e) {
144
			Logger.err(e.getMessage());
145
		}
146
	}
147
}

Return to bug 202626