|
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.Hashtable; |
| 18 |
import java.util.Properties; |
| 19 |
import javax.xml.parsers.DocumentBuilderFactory; |
| 20 |
import javax.xml.parsers.ParserConfigurationException; |
| 21 |
|
| 22 |
import org.eclipse.tptp.platform.agentcontroller.config.Allow; |
| 23 |
import org.eclipse.tptp.platform.agentcontroller.config.ConfigFile; |
| 24 |
import org.eclipse.tptp.platform.agentcontroller.config.ConfigUtility; |
| 25 |
import org.eclipse.tptp.platform.agentcontroller.config.Constants; |
| 26 |
import org.eclipse.tptp.platform.agentcontroller.config.Logger; |
| 27 |
import org.eclipse.tptp.platform.agentcontroller.config.SecurityEnabled; |
| 28 |
import org.eclipse.tptp.platform.agentcontroller.config.UserDefinition; |
| 29 |
import org.w3c.dom.Document; |
| 30 |
import org.w3c.dom.Node; |
| 31 |
import org.w3c.dom.NodeList; |
| 32 |
import org.xml.sax.SAXException; |
| 33 |
|
| 34 |
/** |
| 35 |
* Sample usage: <br> |
| 36 |
* ConfigUpgrade upgrade = new ConfigUpgrade("<AC HOME>\\config\\serviceconfig.xml"); <br> |
| 37 |
* upgrade.parseDocument(); |
| 38 |
* |
| 39 |
* @author jcayne |
| 40 |
* @version February 26, 2008 |
| 41 |
* @since February 26, 2008 |
| 42 |
* |
| 43 |
*/ |
| 44 |
public class ConfigUpgrade { |
| 45 |
|
| 46 |
protected ConfigFile configFile; |
| 47 |
protected Document doc; |
| 48 |
protected Properties hash; |
| 49 |
|
| 50 |
private static ConfigUpgrade instance = null; |
| 51 |
|
| 52 |
/** |
| 53 |
* The method called to acquire a reference of the singleton ConfigUpgrade. |
| 54 |
* @param filename The path to the serviceconfig.xml to retrieve an existing configuration from. |
| 55 |
* @return An instance of ConfigUpgrade. |
| 56 |
*/ |
| 57 |
public static ConfigUpgrade getInstance(String filename) { |
| 58 |
if(instance == null) { |
| 59 |
instance = new ConfigUpgrade(filename); |
| 60 |
} |
| 61 |
return instance; |
| 62 |
} |
| 63 |
|
| 64 |
/** |
| 65 |
* Provides for upgrading an existing Agent Controller installation. |
| 66 |
* @param filename The path to the serviceconfig.xml to retrieve an existing configuration from. |
| 67 |
*/ |
| 68 |
private ConfigUpgrade(String filename) { |
| 69 |
try { |
| 70 |
doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(new File(filename)); |
| 71 |
configFile = new ConfigFile(filename); |
| 72 |
hash = new Properties(); |
| 73 |
} catch (SAXException e) { |
| 74 |
Logger.err(e.getMessage()); |
| 75 |
} catch (IOException e) { |
| 76 |
Logger.err(e.getMessage()); |
| 77 |
} catch (ParserConfigurationException e) { |
| 78 |
Logger.err(e.getMessage()); |
| 79 |
} |
| 80 |
} |
| 81 |
|
| 82 |
/** |
| 83 |
* Parses the serviceconfig.xml and obtains the user provided values. |
| 84 |
*/ |
| 85 |
public void parseDocument() { |
| 86 |
hash.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 |
} |
| 91 |
|
| 92 |
/** |
| 93 |
* Searches for the tag in the serviceconfig.xml and adds it to the hash. |
| 94 |
* @param tag The tag to search for. |
| 95 |
*/ |
| 96 |
protected void addElement(String tag) { |
| 97 |
addElement(tag, null); |
| 98 |
} |
| 99 |
|
| 100 |
/** |
| 101 |
* Searches for the tag containing the id attribute in serviceconfig.xml and adds it to the hash. |
| 102 |
* @param tag The tag to search for. |
| 103 |
* @param id The id attribute to search for in the tag. |
| 104 |
*/ |
| 105 |
protected void addElement(String tag, String id) { |
| 106 |
NodeList nl = doc.getElementsByTagName(tag); |
| 107 |
if(nl != null) { |
| 108 |
/* Traverse the nodes with the tag name adding them to the hash */ |
| 109 |
for(int i = 0; i < nl.getLength(); i++) { |
| 110 |
Node n = nl.item(i); |
| 111 |
String text = null; |
| 112 |
/* Obtain the attribute value and add it to the hash */ |
| 113 |
if(id != null) { |
| 114 |
n = n.getAttributes().getNamedItem(id); |
| 115 |
if(n != null) { |
| 116 |
text = n.getTextContent(); |
| 117 |
/* The tag exists in the hash table */ |
| 118 |
if (hash.containsKey(tag)) |
| 119 |
text = (String) hash.get(tag) + "," + text; |
| 120 |
} |
| 121 |
} else { |
| 122 |
/* Obtain a unique tag value and add it to the hash */ |
| 123 |
text = n.getTextContent(); |
| 124 |
} |
| 125 |
if(text != null) |
| 126 |
hash.put(tag, text); |
| 127 |
} |
| 128 |
} |
| 129 |
} |
| 130 |
|
| 131 |
/** |
| 132 |
* Outputs the user entered values to a property file and provides a hash table containing the user entered values. |
| 133 |
* @param filename The name of the property file to store the user entered values to. |
| 134 |
* @return The has with the user entered values. |
| 135 |
*/ |
| 136 |
public Hashtable getPropertyFile(File filename) { |
| 137 |
try { |
| 138 |
if(filename != null) |
| 139 |
hash.store(new FileOutputStream(filename), null); |
| 140 |
} catch (IOException e) { |
| 141 |
Logger.err(e.getMessage()); |
| 142 |
} |
| 143 |
return hash; |
| 144 |
} |
| 145 |
} |