VersiĆ³n del proyecto de Apuestas tras la iteraciĆ³n 1
[ISBets21MAUBRY] / eclipse-workspace / ISBets21BRYMAUJONUNA / src / main / java / 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 /**
14  * It provides the configuration data from the "resources/config.xml" XML file
15  */
16 public class ConfigXML {
17         
18         private String configFile = "src/main/resources/config.xml";
19                 
20         private String businessLogicNode;
21
22         private String businessLogicPort;
23
24         private String businessLogicName;
25         
26         private static String dbFilename;
27
28         //Two possible values: "open" or "initialize"
29         private String dataBaseOpenMode;
30
31         //Two possible values: true (no instance of RemoteServer needs to be launched) or false (RemoteServer needs to be run first)
32         private boolean businessLogicLocal;
33
34         //Two possible values: true (if the database is in same node as business logic ) or false (in other case)
35         private boolean databaseLocal;
36         
37         private String databaseNode;
38         
39         private int databasePort;
40         
41
42         
43         private String user;
44         
45         private String password;
46         
47         private String locale;
48
49         public String getLocale() {
50                 return locale;
51         }
52         
53         public int getDatabasePort() {
54                 return databasePort;
55         }
56
57         public String getUser() {
58                 return user;
59         }
60
61         public String getPassword() {
62                 return password;
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(configFile));
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                           businessLogicPort = getTagValue("businessLogicPort", config);
93
94                           businessLogicName = getTagValue("businessLogicName", config);
95                           
96                           locale = getTagValue("locale", config);
97
98                           
99                           
100                                 
101
102                           dbFilename = getTagValue("dbFilename", config);
103
104                                 //Two possible values: true (no instance of RemoteServer needs to be launched) or false (RemoteServer needs to be run first)
105                           value= ((Element)config.getElementsByTagName("database").item(0)).getAttribute("local");
106                           databaseLocal=value.equals("true");
107                           
108                           
109                           //Two possible values: "open" or "initialize"
110                          // dataBaseOpenMode= getTagValue("dataBaseOpenMode", config);
111                           dataBaseOpenMode = "open";
112         
113                           databaseNode = getTagValue("databaseNode", config);
114                           
115                           databasePort=Integer.parseInt(getTagValue("databasePort", config));
116                                 
117                           user=getTagValue("user", config);
118                                 
119                           password=getTagValue("password", config);
120
121                           System.out.print("Read from config.xml: ");
122                           System.out.print("\t businessLogicLocal="+businessLogicLocal);
123                           System.out.print("\t databaseLocal="+databaseLocal);
124                           System.out.println("\t dataBaseOpenMode="+dataBaseOpenMode); 
125                                           
126                   } catch (Exception e) {
127                         System.out.println("Error in ConfigXML.java: problems with "+ configFile);
128                     e.printStackTrace();
129                   }             
130                 
131         }
132
133         private static String getTagValue(String sTag, Element eElement)
134          {
135                   NodeList nlList= eElement.getElementsByTagName(sTag).item(0).getChildNodes();
136                   Node nValue = (Node) nlList.item(0);
137
138                   return nValue.getNodeValue();
139
140          }
141         
142         public static ConfigXML getInstance() {
143                 return theInstance;
144         }
145
146         public String getBusinessLogicNode() {
147                 return businessLogicNode;
148         }
149
150         public String getBusinessLogicPort() {
151                 return businessLogicPort;
152         }
153
154         public String getBusinessLogicName() {
155                 return businessLogicName;
156         }
157         
158         public String getDbFilename(){
159                 return dbFilename;
160         }
161
162         public String getDataBaseOpenMode(){
163                 return dataBaseOpenMode;
164         }
165
166         public String getDatabaseNode() {
167                 return databaseNode;
168         }
169
170 }