KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > smartlib > pool > core > ConfigFileProcessor


1 package org.smartlib.pool.core;
2
3
4 import java.io.File JavaDoc;
5 import java.io.IOException JavaDoc;
6 import java.util.List JavaDoc;
7 import java.util.ArrayList JavaDoc;
8 import java.util.HashMap JavaDoc;
9
10 import org.apache.log4j.Logger;
11 import org.apache.xerces.parsers.DOMParser;
12 import org.xml.sax.*;
13 import org.w3c.dom.Document JavaDoc;
14 import org.w3c.dom.Node JavaDoc;
15 import org.w3c.dom.NodeList JavaDoc;
16 import org.w3c.dom.NamedNodeMap JavaDoc;
17
18
19 /**
20  * Created by IntelliJ IDEA.
21  * User: kerneldebugger
22  * Date: Sep 28, 2005
23  * Time: 8:05:29 PM
24  * To change this template use File | Settings | File Templates.
25  */

26 public class ConfigFileProcessor {
27
28     Logger logger = Logger.getLogger(ConfigFileProcessor.class);
29
30     PoolConfig[] getPoolConfig(String JavaDoc fileName) throws SAXException, SAXNotRecognizedException, IOException JavaDoc, ConnectionPoolException {
31
32         if (fileName == null)
33             throw new IllegalArgumentException JavaDoc("Filename cannot be null");
34
35         File JavaDoc file = new File JavaDoc(fileName);
36         if (!file.exists()) {
37             throw new IllegalArgumentException JavaDoc("File: " + file + " does not exist");
38         }
39
40         return process(file);
41
42
43     }
44
45     PoolConfig[] getPoolConfig() throws SAXException, SAXNotRecognizedException, IOException JavaDoc, ConnectionPoolException {
46
47         String JavaDoc fileName = System.getProperty(PoolConstants.CONFIG_FILE_SYSTEM_PROPERTY);
48         if (logger.isDebugEnabled()) {
49             logger.debug("Config File System Property:" + fileName);
50         }
51         return getPoolConfig(fileName);
52
53     }
54
55
56     private PoolConfig[] process(File JavaDoc file) throws SAXException, SAXNotRecognizedException, IOException JavaDoc, ConnectionPoolException {
57
58         DOMParser xmlParser = new DOMParser();
59         //xmlParser.setFeature("http://xml.org/sax/features/validation", true);
60
xmlParser.setFeature("http://apache.org/xml/features/validation/schema", true);
61         //xmlParser.setProperty( "http://apache.org/xml/properties/schema/external-schemaLocation",
62
// "D:\\sachin\\work\\smartpool\\project\\conf\\pool-config.xsd");
63
//xmlParser.setProperty( "http://apache.org/xml/properties/schema/external-schemaLocation",
64
// "http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd");
65

66
67
68         xmlParser.setErrorHandler(new MyErrorHandler());
69         if (logger.isDebugEnabled()) {
70             logger.debug("Reading file: " + file.getAbsolutePath());
71         }
72         xmlParser.parse(file.getAbsolutePath());
73         Document JavaDoc dom = xmlParser.getDocument();
74
75         NodeList JavaDoc nodeList = dom.getElementsByTagName("pool");
76         PoolConfig[] configs = new PoolConfig[nodeList.getLength()];
77         for (int i=0; i<nodeList.getLength(); i++) {
78             Node JavaDoc node = nodeList.item(i);
79             configs[i] = getPoolConfig(node);
80         }
81         validatePoolConfigs(configs);
82         return configs;
83
84     }
85
86     private PoolConfig getPoolConfig(Node JavaDoc node) throws ConnectionPoolException {
87         PoolConfig config = new PoolConfig();
88         NodeList JavaDoc list = node.getChildNodes();
89         for (int i=0; i<list.getLength(); i++) {
90             Node JavaDoc childNode = list.item(i);
91             String JavaDoc nodeName = childNode.getNodeName();
92             if (nodeName != null && nodeName.equals("basic")) {
93                 populateBasic(config, childNode);
94             }
95             if (nodeName != null && nodeName.equals("connect-strings")) {
96                 populateConnectString(config, childNode);
97             }
98             if (nodeName != null && nodeName.equals("pool-sizing")) {
99                 populatePoolSizing(config, childNode);
100             }
101             if (nodeName != null && nodeName.equals("leak-detection")) {
102                 populateLeakDetection(config, childNode);
103             }
104             if (nodeName != null && nodeName.equals("external-pooling")) {
105                 populateExternalPooling(config, childNode);
106             }
107         }
108         return config;
109
110     }
111
112     private void populateExternalPooling(PoolConfig config, Node JavaDoc node) throws ConnectionPoolException {
113
114         NodeList JavaDoc list = node.getChildNodes();
115         List JavaDoc connList = new ArrayList JavaDoc();
116         for (int i=0; i<list.getLength(); i++) {
117             Node JavaDoc childNode = list.item(i);
118             String JavaDoc nodeName = childNode.getNodeName();
119             if (nodeName != null) {
120                 if (nodeName != null) {
121                     if (nodeName.equals("connection-loader-class")) {
122                         PoolConfig.ConnectionLoaderClass connectLoaderClasses = new PoolConfig.ConnectionLoaderClass();
123                         connectLoaderClasses.setConnectionLoaderClass(getTagValue(childNode));
124                         connectLoaderClasses.setName(config.getMultiPoolName() + "." + getAttributeValue(childNode, "name"));
125                         connList.add(connectLoaderClasses);
126                     }
127                     if (nodeName.equals("thread-stickiness")) {
128                         config.setThreadStickiness(Boolean.valueOf(getTagValue(childNode)));
129                     }
130                 }
131
132             }
133         }
134         config.setConnectionLoaderClass((PoolConfig.ConnectionLoaderClass[])connList.toArray(new PoolConfig.ConnectionLoaderClass[connList.size()]));
135     }
136
137     private void populateConnectString(PoolConfig config, Node JavaDoc node) throws ConnectionPoolException {
138
139         NodeList JavaDoc list = node.getChildNodes();
140         List JavaDoc connList = new ArrayList JavaDoc();
141
142         for (int i=0; i<list.getLength(); i++) {
143             Node JavaDoc childNode = list.item(i);
144             String JavaDoc nodeName = childNode.getNodeName();
145             if (nodeName != null) {
146                 if (nodeName.equals("connect-string")) {
147                     PoolConfig.ConnectionString connectString = new PoolConfig.ConnectionString();
148                     connectString.setConnectString(getTagValue(childNode));
149                     connectString.setName(config.getMultiPoolName() + "." + getAttributeValue(childNode, "name"));
150                     connList.add(connectString);
151                 }
152                 if (nodeName.equals("thread-stickiness")) {
153                     config.setThreadStickiness(Boolean.valueOf(getTagValue(childNode)));
154                 }
155             }
156         }
157         config.setConnectionString((PoolConfig.ConnectionString[])connList.toArray(new PoolConfig.ConnectionString[connList.size()]));
158
159     }
160
161     private void populatePoolSizing(PoolConfig config, Node JavaDoc node) {
162
163         NodeList JavaDoc list = node.getChildNodes();
164         for (int i=0; i<list.getLength(); i++) {
165             Node JavaDoc childNode = list.item(i);
166             String JavaDoc nodeName = childNode.getNodeName();
167             if (nodeName != null) {
168                 if (nodeName.equals("min-connections")) {
169                     config.setMinConnections(Integer.valueOf(getTagValue(childNode)));
170                 }
171                 if (nodeName.equals("max-connections")) {
172                     config.setMaxConnections(Integer.valueOf(getTagValue(childNode)));
173                 }
174                 if (nodeName.equals("increment-by")) {
175                     config.setIncrement(Integer.valueOf(getTagValue(childNode)));
176                 }
177                 if (nodeName.equals("increment-by")) {
178                     config.setIncrement(Integer.valueOf(getTagValue(childNode)));
179                 }
180                 if (nodeName.equals("max-free-connections-for-release")) {
181                     config.setMaxConnectionsForRelease(Integer.valueOf(getTagValue(childNode)));
182                 }
183                 if (nodeName.equals("connection-wait-time-out")) {
184                     config.setConnectionWaitTimeOut(Integer.valueOf(getTagValue(childNode)));
185                 }
186                 if (nodeName.equals("max-connection-idle-time")) {
187                     config.setMaxConnectionIdleTime(Integer.valueOf(getTagValue(childNode)));
188                 }
189             }
190         }
191     }
192
193     private void populateLeakDetection(PoolConfig config, Node JavaDoc node) {
194
195         NodeList JavaDoc list = node.getChildNodes();
196         for (int i=0; i<list.getLength(); i++) {
197             Node JavaDoc childNode = list.item(i);
198             String JavaDoc nodeName = childNode.getNodeName();
199             if (nodeName != null) {
200                 if (nodeName.equals("detect-leaks")) {
201                     config.setDetectLeaks(Boolean.valueOf(getTagValue(childNode)));
202                 }
203                 if (nodeName.equals("leak-time-out")) {
204                     config.setLeakTimeOut(Integer.valueOf(getTagValue(childNode)));
205                 }
206                 if (nodeName.equals("poll-thread-time")) {
207                     config.setPollThreadTime(Integer.valueOf(getTagValue(childNode)));
208                 }
209                 if (nodeName.equals("default-listener")) {
210                     config.setDefaultListener(getTagValue(childNode));
211                 }
212             }
213
214         }
215
216
217     }
218
219     private void populateBasic(PoolConfig config, Node JavaDoc node) {
220
221
222         NodeList JavaDoc list = node.getChildNodes();
223         for (int i=0; i<list.getLength(); i++) {
224             Node JavaDoc childNode = list.item(i);
225             String JavaDoc nodeName = childNode.getNodeName();
226             if (nodeName != null) {
227                 if (nodeName.equals("pool-name")) {
228                     config.setMultiPoolName(getTagValue(childNode));
229                 }
230                 if (nodeName.equals("default-pool")) {
231                     config.setDefaultPool(Boolean.valueOf(getTagValue(childNode)));
232                 }
233                 if (nodeName.equals("connection-driver")) {
234                     config.setDriver(getTagValue(childNode));
235                 }
236                 if (nodeName.equals("username")) {
237                     config.setUserName(getTagValue(childNode));
238                 }
239                 if (nodeName.equals("password")) {
240                     config.setPassword(getTagValue(childNode));
241                 }
242                 if (nodeName.equals("auto-close")) {
243                     config.setAutoClose(Boolean.valueOf(getTagValue(childNode)));
244                 }
245                 if (nodeName.equals("allow-anonymous-connections")) {
246                     config.setAllowAnonymousConnections(Boolean.valueOf(getTagValue(childNode)));
247                 }
248                 if (nodeName.equals("validator-query")) {
249                     config.setValidatorQuery(getTagValue(childNode));
250                 }
251             }
252         }
253
254     }
255
256     public class MyErrorHandler implements ErrorHandler {
257
258
259         public void warning(SAXParseException exception) throws SAXException {
260             logger.fatal("Warning:", exception);
261         }
262
263         public void error(SAXParseException exception) throws SAXException {
264             logger.fatal("Warning:", exception);
265         }
266
267         public void fatalError(SAXParseException exception) throws SAXException {
268             logger.fatal("Warning:", exception);
269         }
270
271     }
272
273
274     private String JavaDoc getTagValue(Node JavaDoc nod) {
275
276         // Some crazy xml hack
277
if (nod.getChildNodes() != null && nod.getChildNodes().getLength() != 0)
278             return nod.getChildNodes().item(0).getNodeValue();
279         else
280             return nod.getNodeValue();
281
282     }
283
284     private String JavaDoc getAttributeValue(Node JavaDoc nod, String JavaDoc attrName) throws ConnectionPoolException {
285
286         // Some crazy xml hack
287
NamedNodeMap JavaDoc map = nod.getAttributes();
288         Node JavaDoc attrNode = map.getNamedItem(attrName);
289         if (attrNode == null)
290             throw new ConnectionPoolException("Attribute '" + attrName + "' of node '" + nod.getNodeName() + "' is null");
291         return attrNode.getNodeValue();
292
293     }
294
295     private void validatePoolConfigs(PoolConfig[] configs) throws ConnectionPoolException {
296         for (int i=0; i<configs.length; i++) {
297             PoolConfig config = configs[i];
298             if (config.getConnectionLoaderClass() != null && config.getConnectionLoaderClass().length == 0)
299                 throw new ConnectionPoolException("Please provide a valid connection provider for pool:" + config.getMultiPoolName());
300             if (config.getConnectionString() !=null && config.getConnectionString().length == 0)
301                 throw new ConnectionPoolException("Please provide a valid connection string for pool:" + config.getMultiPoolName());
302             if (config.getConnectionString() != null && config.getConnectionLoaderClass() != null)
303                 throw new ConnectionPoolException("You can specify either external-pooling or connect-strings but not both.");
304
305             HashMap JavaDoc distinctMap = new HashMap JavaDoc();
306             PoolConfig.ConnectionString[] connectStrings = config.getConnectionString();
307             if (connectStrings != null) {
308                 for (int j=0; j <connectStrings.length; j++ ) {
309                     if (connectStrings[j].getName() == null || connectStrings[j].getName().trim().length() == 0)
310                         throw new ConnectionPoolException("Connect String name attribute cannot be null");
311                     if (connectStrings[j].getConnectString() == null || connectStrings[j].getConnectString().trim().length() == 0)
312                         throw new ConnectionPoolException("Connect String cannot be null");
313                     if (distinctMap.get(connectStrings[j].getName()) != null)
314                         throw new ConnectionPoolException("Connection string name is not unique: " + connectStrings[j].getName());
315                     distinctMap.put(connectStrings[j].getName(), "hello");
316                 }
317             }
318
319             distinctMap.clear();
320             PoolConfig.ConnectionLoaderClass[] connectionLoaderClasses = config.getConnectionLoaderClass();
321             if (connectionLoaderClasses != null) {
322                 for (int j=0; j <connectionLoaderClasses.length; j++ ) {
323                     if (connectionLoaderClasses[j].getName() == null || connectionLoaderClasses[j].getName().trim().length() == 0)
324                         throw new ConnectionPoolException("Connect String name attribute cannot be null");
325                     if (connectionLoaderClasses[j].getConnectionLoaderClass() == null || connectionLoaderClasses[j].getConnectionLoaderClass().trim().length() == 0)
326                         throw new ConnectionPoolException("Connect String cannot be null");
327                     if (distinctMap.get(connectionLoaderClasses[j].getName()) != null)
328                         throw new ConnectionPoolException("Connection provider name is not unique: " + connectionLoaderClasses[j].getName());
329                     distinctMap.put(connectionLoaderClasses[j].getName(), "hello");
330                 }
331             }
332
333
334         }
335     }
336
337
338 }
339
Popular Tags