unification with the actual initial project. Some things are new now, but there has...
[RRRRHHHH_Code] / ruralHouses / src / configuration / ConfigXML.java
1 package configuration;
2
3 import java.io.File;
4
5 import javax.xml.parsers.DocumentBuilder;
6 import javax.xml.parsers.DocumentBuilderFactory;
7
8 import org.w3c.dom.Document;
9 import org.w3c.dom.Element;
10 import org.w3c.dom.Node;
11 import org.w3c.dom.NodeList;
12
13 public class ConfigXML {
14         private String businessLogicNode;
15
16         private String portRMI;
17
18         private String serviceRMI;
19
20         private String javaPolicyPath;
21
22         private static String db4oFilename;
23
24         //Two possible values: "open" or "initialize"
25         private String dataBaseOpenMode;
26
27         //Two possible values: true (no instance of RemoteServer needs to be launched) or false (RemoteServer needs to be run first)
28         private boolean businessLogicLocal;
29
30         //Two possible values: true (if the database is in same node as business logic ) or false (in other case)
31         private boolean databaseLocal;
32         
33         private String databaseNode;
34
35         private int activationDepth;
36         
37         private int updateDepth;
38         
39         private int databasePort;
40         
41         private String user;
42         
43         private String password;
44         
45         public int getDatabasePort() {
46                 return databasePort;
47         }
48
49         public String getUser() {
50                 return user;
51         }
52
53         public String getPassword() {
54                 return password;
55         }
56
57         public int getActivationDepth() {
58                 return activationDepth;
59         }
60
61         public int getUpdateDepth() {
62                 return updateDepth;
63         }
64         
65         public boolean isDatabaseLocal() {
66                 return databaseLocal;
67         }
68
69         public boolean isBusinessLogicLocal() {
70                 return businessLogicLocal;
71         }
72         private static ConfigXML theInstance = new ConfigXML();
73
74         private ConfigXML(){
75                 
76                   try {
77                           DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
78                           DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
79                           Document doc = dBuilder.parse(new File("config.xml"));
80                           doc.getDocumentElement().normalize();
81
82                           NodeList list = doc.getElementsByTagName("config");
83                           Element config = (Element) list.item(0); // list.item(0) is a Node that is an Element
84
85                           
86                                 //Two possible values: true (no instance of RemoteServer needs to be launched) or false (RemoteServer needs to be run first)
87                           String value= ((Element)config.getElementsByTagName("businessLogic").item(0)).getAttribute("local");
88                           businessLogicLocal=value.equals("true");
89
90                           businessLogicNode = getTagValue("businessLogicNode", config);
91
92                           portRMI = getTagValue("portRMI", config);
93
94                           serviceRMI = getTagValue("serviceRMI", config);
95
96                           javaPolicyPath= getTagValue("javaPolicyPath", config);
97
98                           db4oFilename = getTagValue("db4oFilename", config);
99
100                                 //Two possible values: true (no instance of RemoteServer needs to be launched) or false (RemoteServer needs to be run first)
101                           value= ((Element)config.getElementsByTagName("database").item(0)).getAttribute("local");
102                           databaseLocal=value.equals("true");
103                           
104                           
105                           //Two possible values: "open" or "initialize"
106                           dataBaseOpenMode= getTagValue("dataBaseOpenMode", config);
107
108         
109                           databaseNode = getTagValue("databaseNode", config);
110
111                           activationDepth=Integer.parseInt(getTagValue("activationDepth", config));
112
113                           updateDepth=Integer.parseInt(getTagValue("updateDepth", config));
114                           
115                           
116                           databasePort=Integer.parseInt(getTagValue("databasePort", config));
117                                 
118                           user=getTagValue("user", config);
119                                 
120                           password=getTagValue("password", config);
121
122                           System.out.print("Read from config.xml: ");
123                           System.out.print("\t businessLogicLocal="+businessLogicLocal);
124                           System.out.print("\t databaseLocal="+databaseLocal);
125                           System.out.println("\t dataBaseOpenMode="+dataBaseOpenMode); 
126                                           
127                   } catch (Exception e) {
128                         System.out.println("Error in ConfigXML.java: problems with config.xml");
129                     e.printStackTrace();
130                   }             
131                 
132                 
133                 
134                 
135         }
136
137         private static String getTagValue(String sTag, Element eElement)
138          {
139                   NodeList nlList= eElement.getElementsByTagName(sTag).item(0).getChildNodes();
140                   Node nValue = (Node) nlList.item(0);
141
142                   return nValue.getNodeValue();
143
144          }
145         
146         public static ConfigXML getInstance() {
147                 return theInstance;
148         }
149
150         public String getBusinessLogicNode() {
151                 return businessLogicNode;
152         }
153
154         public String getPortRMI() {
155                 return portRMI;
156         }
157
158         public String getServiceRMI() {
159                 return serviceRMI;
160         }
161         public String getDb4oFilename(){
162                 return db4oFilename;
163         }
164         public String getJavaPolicyPath(){
165                 return javaPolicyPath;
166         }
167         public String getDataBaseOpenMode(){
168                 return dataBaseOpenMode;
169         }
170
171         public String getDatabaseNode() {
172                 return databaseNode;
173         }
174
175 }