|
Added
Link Here
|
| 1 |
package org.eclipse.tptp.platform.agentcontroller.config; |
| 2 |
/********************************************************************** |
| 3 |
Copyright (c) 2008 IBM Corporation and others. |
| 4 |
All rights reserved. This program and the accompanying materials |
| 5 |
are made available under the terms of the Eclipse Public License v1.0 |
| 6 |
which accompanies this distribution, and is available at |
| 7 |
http://www.eclipse.org/legal/epl-v10.html |
| 8 |
$Id: $ |
| 9 |
|
| 10 |
Contributors: |
| 11 |
IBM Rational - initial implementation |
| 12 |
**********************************************************************/ |
| 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.Logger; |
| 25 |
import org.eclipse.tptp.platform.agentcontroller.config.SecurityEnabled; |
| 26 |
import org.eclipse.tptp.platform.agentcontroller.config.UserDefinition; |
| 27 |
import org.w3c.dom.Document; |
| 28 |
import org.w3c.dom.Node; |
| 29 |
import org.w3c.dom.NodeList; |
| 30 |
import org.xml.sax.SAXException; |
| 31 |
|
| 32 |
/* |
| 33 |
* Sample usage: |
| 34 |
* ConfigUpgrade upgrade = new ConfigUpgrade("<AC HOME>\\config\\serviceconfig.xml"); |
| 35 |
* upgrade.parseDocument(); |
| 36 |
*/ |
| 37 |
public class ConfigUpgrade { |
| 38 |
|
| 39 |
protected ConfigFile configFile; |
| 40 |
protected Document doc; |
| 41 |
protected static String JAVA_PATH = new String("JAVA_PATH"); //$NON-NLS-1$ |
| 42 |
protected Properties hash; |
| 43 |
|
| 44 |
/** |
| 45 |
* Provides for upgrading an existing Agent Controller installation. |
| 46 |
* @param filename The path to the serviceconfig.xml to retrieve an existing configuration from. |
| 47 |
*/ |
| 48 |
public ConfigUpgrade(String filename) { |
| 49 |
try { |
| 50 |
doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(new File(filename)); |
| 51 |
configFile = new ConfigFile(filename); |
| 52 |
hash = new Properties(); |
| 53 |
} catch (SAXException e) { |
| 54 |
Logger.err(e.getMessage()); |
| 55 |
} catch (IOException e) { |
| 56 |
Logger.err(e.getMessage()); |
| 57 |
} catch (ParserConfigurationException e) { |
| 58 |
Logger.err(e.getMessage()); |
| 59 |
} |
| 60 |
} |
| 61 |
|
| 62 |
/** |
| 63 |
* Parses the serviceconfig.xml and obtains the user provided values. |
| 64 |
*/ |
| 65 |
public void parseDocument() { |
| 66 |
hash.put(JAVA_PATH, configFile.getValue(JAVA_PATH)); |
| 67 |
if (doc.hasChildNodes()) { |
| 68 |
traverseNode(doc.getChildNodes()); |
| 69 |
} |
| 70 |
} |
| 71 |
|
| 72 |
/** |
| 73 |
* Traverses a set of nodes to determine their values. |
| 74 |
* @param nl A list of nodes in the serviceconfig.xml. |
| 75 |
*/ |
| 76 |
protected void traverseNode(NodeList nl) { |
| 77 |
for (int i = 0; i < nl.getLength(); i++) { |
| 78 |
Node n = nl.item(i); |
| 79 |
/* Handled nodes contain the desired information, so there is no need to check its child nodes */ |
| 80 |
if(!handleNode(n)) { |
| 81 |
traverseNode(n.getChildNodes()); |
| 82 |
} |
| 83 |
} |
| 84 |
} |
| 85 |
|
| 86 |
/** |
| 87 |
* Determines which nodes are of value from the serviceconfig.xml for storing. |
| 88 |
* @param n The node to check. |
| 89 |
* @return true if the node was handled, false otherwise. |
| 90 |
*/ |
| 91 |
protected boolean handleNode(Node n) { |
| 92 |
/* The security enabled tag */ |
| 93 |
if (n.getNodeName().equals(SecurityEnabled.TAG)) { |
| 94 |
hash.put(SecurityEnabled.TAG, n.getTextContent()); |
| 95 |
return true; |
| 96 |
} |
| 97 |
/* The users allowed to access the Agent Controller tag */ |
| 98 |
else if (n.getNodeName().equals(UserDefinition.TAG)) { |
| 99 |
hash.put(UserDefinition.TAG, n.getTextContent()); |
| 100 |
return true; |
| 101 |
} |
| 102 |
/* The hosts tag containing hosts indicating machines allowed network access */ |
| 103 |
else if (n.getNodeName().equals(Allow.TAG)) { |
| 104 |
String host = (String) hash.get(Allow.TAG); |
| 105 |
/* No hosts exist in the hash table */ |
| 106 |
if (host == null) { |
| 107 |
hash.put(Allow.TAG, n.getAttributes().getNamedItem("host").getTextContent()); //$NON-NLS-1$ |
| 108 |
} |
| 109 |
/* Add to the existing list of hosts */ |
| 110 |
else { |
| 111 |
host = host + "," + n.getAttributes().getNamedItem("host").getTextContent(); //$NON-NLS-1$ |
| 112 |
hash.put(Allow.TAG, host); |
| 113 |
} |
| 114 |
return true; |
| 115 |
} |
| 116 |
else { |
| 117 |
/* The tag was not handled */ |
| 118 |
return false; |
| 119 |
} |
| 120 |
} |
| 121 |
|
| 122 |
/** |
| 123 |
* |
| 124 |
* @return An instance of the serviceconfig.xml Document. |
| 125 |
*/ |
| 126 |
public Document getDoc() { |
| 127 |
return doc; |
| 128 |
} |
| 129 |
|
| 130 |
/** |
| 131 |
* Provides a hash table containing the user entered values. |
| 132 |
* @return A hash with the user entered values. |
| 133 |
*/ |
| 134 |
public Hashtable getHash() { |
| 135 |
return hash; |
| 136 |
} |
| 137 |
|
| 138 |
/** |
| 139 |
* Outputs the user entered values to a property file. |
| 140 |
* @param filename The name of the property file to store the user entered values to. |
| 141 |
*/ |
| 142 |
public void getPropertyFile(File filename) { |
| 143 |
try { |
| 144 |
hash.store(new FileOutputStream(filename), "Agent Controller Configuration"); |
| 145 |
} catch (IOException e) { |
| 146 |
Logger.err(e.getMessage()); |
| 147 |
} |
| 148 |
|
| 149 |
} |
| 150 |
} |