KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > resource > ResourcesXMLParser


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23
24 package com.sun.enterprise.resource;
25
26 import javax.xml.parsers.DocumentBuilder JavaDoc;
27 import javax.xml.parsers.DocumentBuilderFactory JavaDoc;
28 import javax.xml.parsers.ParserConfigurationException JavaDoc;
29  
30 import org.xml.sax.SAXException JavaDoc;
31 import org.xml.sax.SAXParseException JavaDoc;
32 import org.xml.sax.InputSource JavaDoc;
33 import org.xml.sax.ErrorHandler JavaDoc;
34 import org.xml.sax.EntityResolver JavaDoc;
35
36 import java.io.File JavaDoc;
37 import java.io.IOException JavaDoc;
38 import java.util.ArrayList JavaDoc;
39 import java.util.List JavaDoc;
40 import java.util.Vector JavaDoc;
41 import java.util.Iterator JavaDoc;
42 import java.util.logging.Level JavaDoc;
43 import java.util.logging.Logger JavaDoc;
44 import java.util.StringTokenizer JavaDoc;
45
46 import org.w3c.dom.Document JavaDoc;
47 import org.w3c.dom.DOMException JavaDoc;
48 import org.w3c.dom.Node JavaDoc;
49 import org.w3c.dom.NodeList JavaDoc;
50 import org.w3c.dom.NamedNodeMap JavaDoc;
51
52 import com.sun.enterprise.util.SystemPropertyConstants;
53 import com.sun.enterprise.admin.common.constant.AdminConstants;
54
55 import javax.management.Attribute JavaDoc;
56 import javax.management.AttributeList JavaDoc;
57
58 //i18n import
59
import com.sun.enterprise.util.i18n.StringManager;
60
61 import static com.sun.enterprise.resource.ResourceConstants.*;
62
63 /**
64  * This Class reads the Properties (resources) from the xml file supplied
65  * to constructor
66  */

67 public class ResourcesXMLParser implements EntityResolver JavaDoc
68 {
69
70     private File JavaDoc resourceFile = null;
71     private Document JavaDoc document;
72     private List JavaDoc<Resource> vResources;
73     /* list of resources that needs to be created prior to module deployment. This
74      * includes all non-Connector resources and resource-adapter-config
75      */

76 // private List<Resource> connectorResources;
77

78     /* Includes all connector resources in the order in which the resources needs
79       to be created */

80 // private List<Resource> nonConnectorResources;
81

82     // i18n StringManager
83
private static StringManager localStrings =
84         StringManager.getManager( ResourcesXMLParser.class );
85     
86     private static final int NONCONNECTOR = 2;
87     private static final int CONNECTOR = 1;
88
89     /** Creates new ResourcesXMLParser */
90     public ResourcesXMLParser(String JavaDoc resourceFileName) throws Exception JavaDoc
91     {
92         resourceFile = new File JavaDoc(resourceFileName);
93         initProperties();
94         vResources = new ArrayList JavaDoc<Resource>();
95         generateResourceObjects();
96     }
97
98     /**
99      *Parse the XML Properties file and populate it into document object
100      */

101     public void initProperties() throws Exception JavaDoc
102     {
103         DocumentBuilderFactory JavaDoc factory = DocumentBuilderFactory.newInstance();
104         try {
105             AddResourcesErrorHandler errorHandler = new AddResourcesErrorHandler();
106             factory.setValidating(true);
107             DocumentBuilder JavaDoc builder = factory.newDocumentBuilder();
108             builder.setEntityResolver((EntityResolver JavaDoc)this);
109             builder.setErrorHandler(errorHandler);
110             if (resourceFile == null) {
111         String JavaDoc msg = localStrings.getString( "admin.server.core.mbean.config.no_resource_file" );
112                 throw new Exception JavaDoc( msg );
113             }
114             InputSource JavaDoc is = new InputSource JavaDoc(resourceFile.toString());
115             document = builder.parse(is);
116         }/*catch(SAXParseException saxpe){
117             throw new Exception(saxpe.getLocalizedMessage());
118         }*/
catch (SAXException JavaDoc sxe) {
119             Exception JavaDoc x = sxe;
120             if (sxe.getException() != null)
121                x = sxe.getException();
122             throw new Exception JavaDoc(x.getLocalizedMessage());
123         }
124         catch (ParserConfigurationException JavaDoc pce) {
125             // Parser with specified options can't be built
126
throw new Exception JavaDoc(pce.getLocalizedMessage());
127         }
128         catch (IOException JavaDoc ioe) {
129             throw new Exception JavaDoc(ioe.getLocalizedMessage());
130         }
131     }
132     
133     /**
134      * Get All the resources from the document object.
135      *
136      */

137     private void generateResourceObjects() throws Exception JavaDoc
138     {
139         if (document != null) {
140             for (Node JavaDoc nextKid = document.getDocumentElement().getFirstChild();
141                     nextKid != null; nextKid = nextKid.getNextSibling())
142             {
143                 String JavaDoc nodeName = nextKid.getNodeName();
144                 if (nodeName.equalsIgnoreCase(Resource.CUSTOM_RESOURCE)) {
145                     generateCustomResource(nextKid);
146                 }
147                 else if (nodeName.equalsIgnoreCase(Resource.EXTERNAL_JNDI_RESOURCE))
148                 {
149                     generateJNDIResource(nextKid);
150                 }
151                 else if (nodeName.equalsIgnoreCase(Resource.JDBC_RESOURCE))
152                 {
153                     generateJDBCResource(nextKid);
154                 }
155                 else if (nodeName.equalsIgnoreCase(Resource.JDBC_CONNECTION_POOL))
156                 {
157                     generateJDBCConnectionPoolResource(nextKid);
158                 }
159                 else if (nodeName.equalsIgnoreCase(Resource.MAIL_RESOURCE))
160                 {
161                     generateMailResource(nextKid);
162                 }
163                 else if (nodeName.equalsIgnoreCase(Resource.PERSISTENCE_MANAGER_FACTORY_RESOURCE))
164                 {
165                     generatePersistenceResource(nextKid);
166                 }
167                 else if (nodeName.equalsIgnoreCase(Resource.ADMIN_OBJECT_RESOURCE))
168                 {
169                     generateAdminObjectResource(nextKid);
170                 }
171                 else if (nodeName.equalsIgnoreCase(Resource.CONNECTOR_RESOURCE))
172                 {
173                     generateConnectorResource(nextKid);
174                 }
175                 else if (nodeName.equalsIgnoreCase(Resource.CONNECTOR_CONNECTION_POOL))
176                 {
177                     generateConnectorConnectionPoolResource(nextKid);
178                 }
179                 else if (nodeName.equalsIgnoreCase(Resource.RESOURCE_ADAPTER_CONFIG))
180                 {
181                     generateResourceAdapterConfig(nextKid);
182                 }
183             }
184         }
185     }
186     
187     /**
188      * Sorts the resources defined in the resources configuration xml file into
189      * two buckets
190      * a) list of resources that needs to be created prior to
191      * module deployment. This includes all non-Connector resources
192      * and resource-adapter-config
193      * b) includes all connector resources in the order in which the resources needs
194      * to be created
195      * and returns the requested resources bucker to the caller.
196      *
197      * @param resources Resources list as defined in sun-resources.xml
198      * @param type Specified either ResourcesXMLParser.CONNECTOR or
199      * ResourcesXMLParser.NONCONNECTOR to indicate the type of
200      * resources are needed by the client of the ResourcesXMLParser
201      * @param isResourceCreation During the resource Creation Phase, RA configs are
202      * added to the nonConnector resources list so that they can be created in the
203      * <code>PreResCreationPhase</code>. When the isResourceCreation is false, the
204      * RA config resuorces are added to the Connector Resources list, so that they
205      * can be deleted as all other connector resources in the
206      * <code>PreResDeletionPhase</code>
207      */

208     private static List JavaDoc<Resource> getResourcesOfType(List JavaDoc<Resource> resources,
209                                             int type, boolean isResourceCreation) {
210         List JavaDoc<Resource> nonConnectorResources = new ArrayList JavaDoc<Resource>();
211         List JavaDoc<Resource> connectorResources = new ArrayList JavaDoc<Resource>();
212         
213         for (Resource res : resources) {
214             if (isConnectorResource(res)) {
215                 if (res.getType().equals(Resource.RESOURCE_ADAPTER_CONFIG)) {
216                     if(isResourceCreation) {
217                         //RA config is considered as a nonConnector Resource,
218
//during sun-resources.xml resource-creation phase, so that
219
//it could be created before the RAR is deployed.
220
nonConnectorResources.add(res);
221                     } else {
222                         connectorResources.add(res);
223                     }
224                 } else {
225                     connectorResources.add(res);
226                 }
227             } else {
228                 nonConnectorResources.add(res);
229             }
230         }
231         
232         List JavaDoc<Resource> finalSortedConnectorList = sortConnectorResources(connectorResources);
233         if (type == CONNECTOR) {
234             return finalSortedConnectorList;
235         } else {
236             return nonConnectorResources;
237         }
238     }
239
240     /**
241      * Sort connector resources
242      * Resource Adater configs are added first.
243      * Pools are then added to the list, so that the conncetion
244      * pools can be created prior to any other connector resource defined
245      * in the resources configuration xml file.
246      * @param connectorResources List of Resources to be sorted.
247      * @return The sorted list.
248      */

249     private static List JavaDoc<Resource> sortConnectorResources(List JavaDoc<Resource> connectorResources) {
250         List JavaDoc<Resource> raconfigs = new ArrayList JavaDoc<Resource>();
251         List JavaDoc<Resource> ccps = new ArrayList JavaDoc<Resource>();
252         List JavaDoc<Resource> others = new ArrayList JavaDoc<Resource>();
253         
254         List JavaDoc<Resource> finalSortedConnectorList = new ArrayList JavaDoc<Resource>();
255         
256         for (Resource resource : connectorResources) {
257             if (resource.getType().equals(Resource.RESOURCE_ADAPTER_CONFIG)){
258                 raconfigs.add(resource);
259             } else if (resource.getType().equals(Resource.CONNECTOR_CONNECTION_POOL)) {
260                 ccps.add(resource);
261             } else {
262                 others.add(resource);
263             }
264         }
265         
266         finalSortedConnectorList.addAll(raconfigs);
267         finalSortedConnectorList.addAll(ccps);
268         finalSortedConnectorList.addAll(others);
269         return finalSortedConnectorList;
270     }
271     
272     /**
273      * Determines if the passed in <code>Resource</code> is a connector
274      * resource. A connector resource is either a connector connection pool or a
275      * connector resource, security map, ra config or an admin object
276      *
277      * @param res Resource that needs to be tested
278      * @return
279      */

280     private static boolean isConnectorResource(Resource res) {
281         String JavaDoc type = res.getType();
282         return (
283                     (type.equals(Resource.ADMIN_OBJECT_RESOURCE)) ||
284                     (type.equals(Resource.CONNECTOR_CONNECTION_POOL)) ||
285                     (type.equals(Resource.CONNECTOR_RESOURCE)) ||
286                     (type.equals(Resource.CONNECTOR_SECURITY_MAP)) ||
287                     (type.equals(Resource.RESOURCE_ADAPTER_CONFIG))
288             );
289     }
290
291     /*
292      * Generate the Custom resource
293      */

294     private void generateCustomResource(Node JavaDoc nextKid) throws Exception JavaDoc
295     {
296         NamedNodeMap JavaDoc attributes = nextKid.getAttributes();
297         
298         if (attributes == null)
299             return;
300         
301         Node JavaDoc jndiNameNode = attributes.getNamedItem(JNDI_NAME);
302         String JavaDoc jndiName = jndiNameNode.getNodeValue();
303         
304         Node JavaDoc resTypeNode = attributes.getNamedItem(RES_TYPE);
305         String JavaDoc resType = resTypeNode.getNodeValue();
306         
307         Node JavaDoc factoryClassNode = attributes.getNamedItem(FACTORY_CLASS);
308         String JavaDoc factoryClass = factoryClassNode.getNodeValue();
309         
310         Node JavaDoc enabledNode = attributes.getNamedItem(ENABLED);
311         
312         Resource customResource = new Resource(Resource.CUSTOM_RESOURCE);
313         customResource.setAttribute(JNDI_NAME, jndiName);
314         customResource.setAttribute(RES_TYPE, resType);
315         customResource.setAttribute(FACTORY_CLASS, factoryClass);
316         if (enabledNode != null) {
317            String JavaDoc sEnabled = enabledNode.getNodeValue();
318            customResource.setAttribute(ENABLED, sEnabled);
319         }
320         
321         NodeList JavaDoc children = nextKid.getChildNodes();
322         generatePropertyElement(customResource, children);
323         vResources.add(customResource);
324         
325         //debug strings
326
printResourceElements(customResource);
327     }
328     
329     /*
330      * Generate the JNDI resource
331      */

332     private void generateJNDIResource(Node JavaDoc nextKid) throws Exception JavaDoc
333     {
334         NamedNodeMap JavaDoc attributes = nextKid.getAttributes();
335         if (attributes == null)
336             return;
337         
338         Node JavaDoc jndiNameNode = attributes.getNamedItem(JNDI_NAME);
339         String JavaDoc jndiName = jndiNameNode.getNodeValue();
340         Node JavaDoc jndiLookupNode = attributes.getNamedItem(JNDI_LOOKUP);
341         String JavaDoc jndiLookup = jndiLookupNode.getNodeValue();
342         Node JavaDoc resTypeNode = attributes.getNamedItem(RES_TYPE);
343         String JavaDoc resType = resTypeNode.getNodeValue();
344         Node JavaDoc factoryClassNode = attributes.getNamedItem(FACTORY_CLASS);
345         String JavaDoc factoryClass = factoryClassNode.getNodeValue();
346         Node JavaDoc enabledNode = attributes.getNamedItem(ENABLED);
347         
348         Resource jndiResource = new Resource(Resource.EXTERNAL_JNDI_RESOURCE);
349         jndiResource.setAttribute(JNDI_NAME, jndiName);
350         jndiResource.setAttribute(JNDI_LOOKUP, jndiLookup);
351         jndiResource.setAttribute(RES_TYPE, resType);
352         jndiResource.setAttribute(FACTORY_CLASS, factoryClass);
353         if (enabledNode != null) {
354            String JavaDoc sEnabled = enabledNode.getNodeValue();
355            jndiResource.setAttribute(ENABLED, sEnabled);
356         }
357         
358         NodeList JavaDoc children = nextKid.getChildNodes();
359         generatePropertyElement(jndiResource, children);
360         vResources.add(jndiResource);
361         
362         //debug strings
363
printResourceElements(jndiResource);
364     }
365     
366     /*
367      * Generate the JDBC resource
368      */

369     private void generateJDBCResource(Node JavaDoc nextKid) throws Exception JavaDoc
370     {
371         NamedNodeMap JavaDoc attributes = nextKid.getAttributes();
372         if (attributes == null)
373             return;
374         
375         Node JavaDoc jndiNameNode = attributes.getNamedItem(JNDI_NAME);
376         String JavaDoc jndiName = jndiNameNode.getNodeValue();
377         Node JavaDoc poolNameNode = attributes.getNamedItem(POOL_NAME);
378         String JavaDoc poolName = poolNameNode.getNodeValue();
379         Node JavaDoc enabledNode = attributes.getNamedItem(ENABLED);
380
381         Resource jdbcResource = new Resource(Resource.JDBC_RESOURCE);
382         jdbcResource.setAttribute(JNDI_NAME, jndiName);
383         jdbcResource.setAttribute(POOL_NAME, poolName);
384         if (enabledNode != null) {
385            String JavaDoc enabledName = enabledNode.getNodeValue();
386            jdbcResource.setAttribute(ENABLED, enabledName);
387         }
388         
389         NodeList JavaDoc children = nextKid.getChildNodes();
390         //get description
391
if (children != null)
392         {
393             for (int ii=0; ii<children.getLength(); ii++)
394             {
395                 if (children.item(ii).getNodeName().equals("description")) {
396                     if (children.item(ii).getFirstChild() != null) {
397                         jdbcResource.setDescription(
398                             children.item(ii).getFirstChild().getNodeValue());
399                     }
400                 }
401             }
402         }
403
404         vResources.add(jdbcResource);
405         
406         //debug strings
407
printResourceElements(jdbcResource);
408     }
409     
410     /*
411      * Generate the JDBC Connection pool Resource
412      */

413     private void generateJDBCConnectionPoolResource(Node JavaDoc nextKid) throws Exception JavaDoc
414     {
415         NamedNodeMap JavaDoc attributes = nextKid.getAttributes();
416         if (attributes == null)
417             return;
418         
419         Node JavaDoc nameNode = attributes.getNamedItem(CONNECTION_POOL_NAME);
420         String JavaDoc name = nameNode.getNodeValue();
421         Node JavaDoc nSteadyPoolSizeNode = attributes.getNamedItem(STEADY_POOL_SIZE);
422         Node JavaDoc nMaxPoolSizeNode = attributes.getNamedItem(MAX_POOL_SIZE);
423         Node JavaDoc nMaxWaitTimeInMillisNode =
424              attributes.getNamedItem(MAX_WAIT_TIME_IN_MILLIS);
425         Node JavaDoc nPoolSizeQuantityNode =
426              attributes.getNamedItem(POOL_SIZE_QUANTITY);
427         Node JavaDoc nIdleTimeoutInSecNode =
428              attributes.getNamedItem(IDLE_TIME_OUT_IN_SECONDS);
429         Node JavaDoc nIsConnectionValidationRequiredNode =
430              attributes.getNamedItem(IS_CONNECTION_VALIDATION_REQUIRED);
431         Node JavaDoc nConnectionValidationMethodNode =
432              attributes.getNamedItem(CONNECTION_VALIDATION_METHOD);
433         Node JavaDoc nFailAllConnectionsNode =
434              attributes.getNamedItem(FAIL_ALL_CONNECTIONS);
435         Node JavaDoc nValidationTableNameNode =
436              attributes.getNamedItem(VALIDATION_TABLE_NAME);
437         Node JavaDoc nResType = attributes.getNamedItem(RES_TYPE);
438         Node JavaDoc nTransIsolationLevel =
439              attributes.getNamedItem(TRANS_ISOLATION_LEVEL);
440         Node JavaDoc nIsIsolationLevelQuaranteed =
441              attributes.getNamedItem(IS_ISOLATION_LEVEL_GUARANTEED);
442         Node JavaDoc datasourceNode = attributes.getNamedItem(DATASOURCE_CLASS);
443         String JavaDoc datasource = datasourceNode.getNodeValue();
444         
445         Resource jdbcResource = new Resource(Resource.JDBC_CONNECTION_POOL);
446         jdbcResource.setAttribute(CONNECTION_POOL_NAME, name);
447         jdbcResource.setAttribute(DATASOURCE_CLASS, datasource);
448         if (nSteadyPoolSizeNode != null) {
449            String JavaDoc sSteadyPoolSize = nSteadyPoolSizeNode.getNodeValue();
450            jdbcResource.setAttribute(STEADY_POOL_SIZE, sSteadyPoolSize);
451         }
452         if (nMaxPoolSizeNode != null) {
453            String JavaDoc sMaxPoolSize = nMaxPoolSizeNode.getNodeValue();
454            jdbcResource.setAttribute(MAX_POOL_SIZE, sMaxPoolSize);
455         }
456         if (nMaxWaitTimeInMillisNode != null) {
457            String JavaDoc sMaxWaitTimeInMillis = nMaxWaitTimeInMillisNode.getNodeValue();
458            jdbcResource.setAttribute(MAX_WAIT_TIME_IN_MILLIS, sMaxWaitTimeInMillis);
459         }
460         if (nPoolSizeQuantityNode != null) {
461            String JavaDoc sPoolSizeQuantity = nPoolSizeQuantityNode.getNodeValue();
462            jdbcResource.setAttribute(POOL_SIZE_QUANTITY, sPoolSizeQuantity);
463         }
464         if (nIdleTimeoutInSecNode != null) {
465            String JavaDoc sIdleTimeoutInSec = nIdleTimeoutInSecNode.getNodeValue();
466            jdbcResource.setAttribute(IDLE_TIME_OUT_IN_SECONDS, sIdleTimeoutInSec);
467         }
468         if (nIsConnectionValidationRequiredNode != null) {
469            String JavaDoc sIsConnectionValidationRequired = nIsConnectionValidationRequiredNode.getNodeValue();
470            jdbcResource.setAttribute(IS_CONNECTION_VALIDATION_REQUIRED, sIsConnectionValidationRequired);
471         }
472         if (nConnectionValidationMethodNode != null) {
473            String JavaDoc sConnectionValidationMethod = nConnectionValidationMethodNode.getNodeValue();
474            jdbcResource.setAttribute(CONNECTION_VALIDATION_METHOD, sConnectionValidationMethod);
475         }
476         if (nFailAllConnectionsNode != null) {
477            String JavaDoc sFailAllConnection = nFailAllConnectionsNode.getNodeValue();
478            jdbcResource.setAttribute(FAIL_ALL_CONNECTIONS, sFailAllConnection);
479         }
480         if (nValidationTableNameNode != null) {
481            String JavaDoc sValidationTableName = nValidationTableNameNode.getNodeValue();
482            jdbcResource.setAttribute(VALIDATION_TABLE_NAME, sValidationTableName);
483         }
484         if (nResType != null) {
485            String JavaDoc sResType = nResType.getNodeValue();
486            jdbcResource.setAttribute(RES_TYPE, sResType);
487         }
488         if (nTransIsolationLevel != null) {
489            String JavaDoc sTransIsolationLevel = nTransIsolationLevel.getNodeValue();
490            jdbcResource.setAttribute(TRANS_ISOLATION_LEVEL, sTransIsolationLevel);
491         }
492         if (nIsIsolationLevelQuaranteed != null) {
493            String JavaDoc sIsIsolationLevelQuaranteed =
494                   nIsIsolationLevelQuaranteed.getNodeValue();
495            jdbcResource.setAttribute(IS_ISOLATION_LEVEL_GUARANTEED,
496                                      sIsIsolationLevelQuaranteed);
497         }
498         
499         NodeList JavaDoc children = nextKid.getChildNodes();
500         generatePropertyElement(jdbcResource, children);
501         vResources.add(jdbcResource);
502         
503         //debug strings
504
printResourceElements(jdbcResource);
505     }
506     
507     /*
508      * Generate the Mail resource
509      */

510     private void generateMailResource(Node JavaDoc nextKid) throws Exception JavaDoc
511     {
512         NamedNodeMap JavaDoc attributes = nextKid.getAttributes();
513         if (attributes == null)
514             return;
515
516         Node JavaDoc jndiNameNode = attributes.getNamedItem(JNDI_NAME);
517         Node JavaDoc hostNode = attributes.getNamedItem(MAIL_HOST);
518         Node JavaDoc userNode = attributes.getNamedItem(MAIL_USER);
519         Node JavaDoc fromAddressNode = attributes.getNamedItem(MAIL_FROM_ADDRESS);
520         Node JavaDoc storeProtoNode = attributes.getNamedItem(MAIL_STORE_PROTO);
521         Node JavaDoc storeProtoClassNode = attributes.getNamedItem(MAIL_STORE_PROTO_CLASS);
522         Node JavaDoc transProtoNode = attributes.getNamedItem(MAIL_TRANS_PROTO);
523         Node JavaDoc transProtoClassNode = attributes.getNamedItem(MAIL_TRANS_PROTO_CLASS);
524         Node JavaDoc debugNode = attributes.getNamedItem(MAIL_DEBUG);
525         Node JavaDoc enabledNode = attributes.getNamedItem(ENABLED);
526
527         String JavaDoc jndiName = jndiNameNode.getNodeValue();
528         String JavaDoc host = hostNode.getNodeValue();
529         String JavaDoc user = userNode.getNodeValue();
530         String JavaDoc fromAddress = fromAddressNode.getNodeValue();
531         
532         Resource mailResource = new Resource(Resource.MAIL_RESOURCE);
533
534         mailResource.setAttribute(JNDI_NAME, jndiName);
535         mailResource.setAttribute(MAIL_HOST, host);
536         mailResource.setAttribute(MAIL_USER, user);
537         mailResource.setAttribute(MAIL_FROM_ADDRESS, fromAddress);
538         if (storeProtoNode != null) {
539            String JavaDoc sStoreProto = storeProtoNode.getNodeValue();
540            mailResource.setAttribute(MAIL_STORE_PROTO, sStoreProto);
541         }
542         if (storeProtoClassNode != null) {
543            String JavaDoc sStoreProtoClass = storeProtoClassNode.getNodeValue();
544            mailResource.setAttribute(MAIL_STORE_PROTO_CLASS, sStoreProtoClass);
545         }
546         if (transProtoNode != null) {
547            String JavaDoc sTransProto = transProtoNode.getNodeValue();
548            mailResource.setAttribute(MAIL_TRANS_PROTO, sTransProto);
549         }
550         if (transProtoClassNode != null) {
551            String JavaDoc sTransProtoClass = transProtoClassNode.getNodeValue();
552            mailResource.setAttribute(MAIL_TRANS_PROTO_CLASS, sTransProtoClass);
553         }
554         if (debugNode != null) {
555            String JavaDoc sDebug = debugNode.getNodeValue();
556            mailResource.setAttribute(MAIL_DEBUG, sDebug);
557         }
558         if (enabledNode != null) {
559            String JavaDoc sEnabled = enabledNode.getNodeValue();
560            mailResource.setAttribute(ENABLED, sEnabled);
561         }
562
563         NodeList JavaDoc children = nextKid.getChildNodes();
564         generatePropertyElement(mailResource, children);
565         vResources.add(mailResource);
566         
567         //debug strings
568
printResourceElements(mailResource);
569     }
570     
571     /*
572      * Generate the Persistence Factory Manager resource
573      */

574     private void generatePersistenceResource(Node JavaDoc nextKid) throws Exception JavaDoc
575     {
576         NamedNodeMap JavaDoc attributes = nextKid.getAttributes();
577         if (attributes == null)
578             return;
579         
580         Node JavaDoc jndiNameNode = attributes.getNamedItem(JNDI_NAME);
581         String JavaDoc jndiName = jndiNameNode.getNodeValue();
582         Node JavaDoc factoryClassNode = attributes.getNamedItem(FACTORY_CLASS);
583         Node JavaDoc poolNameNode = attributes.getNamedItem(JDBC_RESOURCE_JNDI_NAME);
584         Node JavaDoc enabledNode = attributes.getNamedItem(ENABLED);
585
586         Resource persistenceResource =
587                     new Resource(Resource.PERSISTENCE_MANAGER_FACTORY_RESOURCE);
588         persistenceResource.setAttribute(JNDI_NAME, jndiName);
589         if (factoryClassNode != null) {
590            String JavaDoc factoryClass = factoryClassNode.getNodeValue();
591            persistenceResource.setAttribute(FACTORY_CLASS, factoryClass);
592         }
593         if (poolNameNode != null) {
594            String JavaDoc poolName = poolNameNode.getNodeValue();
595            persistenceResource.setAttribute(JDBC_RESOURCE_JNDI_NAME, poolName);
596         }
597         if (enabledNode != null) {
598            String JavaDoc sEnabled = enabledNode.getNodeValue();
599            persistenceResource.setAttribute(ENABLED, sEnabled);
600         }
601         
602         NodeList JavaDoc children = nextKid.getChildNodes();
603         generatePropertyElement(persistenceResource, children);
604         vResources.add(persistenceResource);
605         
606         //debug strings
607
printResourceElements(persistenceResource);
608     }
609
610     /*
611      * Generate the Admin Object resource
612      */

613     private void generateAdminObjectResource(Node JavaDoc nextKid) throws Exception JavaDoc
614     {
615         NamedNodeMap JavaDoc attributes = nextKid.getAttributes();
616         if (attributes == null)
617             return;
618         
619         Node JavaDoc jndiNameNode = attributes.getNamedItem(JNDI_NAME);
620         String JavaDoc jndiName = jndiNameNode.getNodeValue();
621         Node JavaDoc resTypeNode = attributes.getNamedItem(RES_TYPE);
622         String JavaDoc resType = resTypeNode.getNodeValue();
623         Node JavaDoc resAdapterNode = attributes.getNamedItem(RES_ADAPTER);
624         String JavaDoc resAdapter = resAdapterNode.getNodeValue();
625         Node JavaDoc enabledNode = attributes.getNamedItem(ENABLED);
626
627         Resource adminObjectResource =
628                     new Resource(Resource.ADMIN_OBJECT_RESOURCE);
629         adminObjectResource.setAttribute(JNDI_NAME, jndiName);
630         adminObjectResource.setAttribute(RES_TYPE, resType);
631         adminObjectResource.setAttribute(RES_ADAPTER, resAdapter);
632
633         if (enabledNode != null) {
634            String JavaDoc sEnabled = enabledNode.getNodeValue();
635            adminObjectResource.setAttribute(ENABLED, sEnabled);
636         }
637         
638         NodeList JavaDoc children = nextKid.getChildNodes();
639         generatePropertyElement(adminObjectResource, children);
640         vResources.add(adminObjectResource);
641         
642         //debug strings
643
printResourceElements(adminObjectResource);
644     }
645
646     /*
647      * Generate the Connector resource
648      */

649     private void generateConnectorResource(Node JavaDoc nextKid) throws Exception JavaDoc
650     {
651         NamedNodeMap JavaDoc attributes = nextKid.getAttributes();
652         if (attributes == null)
653             return;
654         
655         Node JavaDoc jndiNameNode = attributes.getNamedItem(JNDI_NAME);
656         String JavaDoc jndiName = jndiNameNode.getNodeValue();
657         Node JavaDoc poolNameNode = attributes.getNamedItem(POOL_NAME);
658         String JavaDoc poolName = poolNameNode.getNodeValue();
659         Node JavaDoc resTypeNode = attributes.getNamedItem(RESOURCE_TYPE);
660         Node JavaDoc enabledNode = attributes.getNamedItem(ENABLED);
661
662         Resource connectorResource =
663                     new Resource(Resource.CONNECTOR_RESOURCE);
664         connectorResource.setAttribute(JNDI_NAME, jndiName);
665         connectorResource.setAttribute(POOL_NAME, poolName);
666         if (resTypeNode != null) {
667            String JavaDoc resType = resTypeNode.getNodeValue();
668            connectorResource.setAttribute(RESOURCE_TYPE, resType);
669         }
670         if (enabledNode != null) {
671            String JavaDoc sEnabled = enabledNode.getNodeValue();
672            connectorResource.setAttribute(ENABLED, sEnabled);
673         }
674         
675         NodeList JavaDoc children = nextKid.getChildNodes();
676         generatePropertyElement(connectorResource, children);
677         vResources.add(connectorResource);
678         
679         //debug strings
680
printResourceElements(connectorResource);
681     }
682
683     private void generatePropertyElement(Resource rs, NodeList JavaDoc children) throws Exception JavaDoc
684     {
685         if (children != null) {
686             for (int i=0; i<children.getLength(); i++) {
687                 if (children.item(i).getNodeName().equals("property")) {
688                     NamedNodeMap JavaDoc attNodeMap = children.item(i).getAttributes();
689                     Node JavaDoc nameNode = attNodeMap.getNamedItem("name");
690                     Node JavaDoc valueNode = attNodeMap.getNamedItem("value");
691                     if (nameNode != null && valueNode != null) {
692                         boolean bDescFound = false;
693                         String JavaDoc sName = nameNode.getNodeValue();
694                         String JavaDoc sValue = valueNode.getNodeValue();
695                         //get property description
696
// FIX ME: Ignoring the value for description for the
697
// time-being as there is no method available in
698
// configMBean to set description for a property
699
Node JavaDoc descNode = children.item(i).getFirstChild();
700                         while (descNode != null && !bDescFound) {
701                             if (descNode.getNodeName().equalsIgnoreCase("description")) {
702                                 try {
703                                     //rs.setElementProperty(sName, sValue, descNode.getFirstChild().getNodeValue());
704
rs.setProperty(sName, sValue);
705                                     bDescFound = true;
706                                 }
707                                 catch (DOMException JavaDoc dome) {
708                                     // DOM Error
709
throw new Exception JavaDoc(dome.getLocalizedMessage());
710                                 }
711                             }
712                             descNode = descNode.getNextSibling();
713                         }
714                         if (!bDescFound) {
715                             rs.setProperty(sName, sValue);
716                         }
717                     }
718                 }
719                 if (children.item(i).getNodeName().equals("description")) {
720                     rs.setDescription(children.item(i).getFirstChild().getNodeValue());
721                 }
722             }
723         }
724     }
725     
726     /*
727      * Generate the Connector Connection pool Resource
728      */

729     private void generateConnectorConnectionPoolResource(Node JavaDoc nextKid) throws Exception JavaDoc
730     {
731         NamedNodeMap JavaDoc attributes = nextKid.getAttributes();
732         if (attributes == null)
733             return ;
734         
735         Node JavaDoc nameNode
736             = attributes.getNamedItem(CONNECTOR_CONNECTION_POOL_NAME);
737         Node JavaDoc raConfigNode
738             = attributes.getNamedItem(RESOURCE_ADAPTER_CONFIG_NAME);
739         Node JavaDoc conDefNode
740             = attributes.getNamedItem(CONN_DEF_NAME);
741         Node JavaDoc steadyPoolSizeNode
742             = attributes.getNamedItem(CONN_STEADY_POOL_SIZE);
743         Node JavaDoc maxPoolSizeNode
744             = attributes.getNamedItem(CONN_MAX_POOL_SIZE);
745         Node JavaDoc poolResizeNode
746             = attributes.getNamedItem(CONN_POOL_RESIZE_QUANTITY);
747         Node JavaDoc idleTimeOutNode
748             = attributes.getNamedItem(CONN_IDLE_TIME_OUT);
749         Node JavaDoc failAllConnNode
750             = attributes.getNamedItem(CONN_FAIL_ALL_CONNECTIONS);
751         
752         String JavaDoc poolName = null;
753         
754         Resource connectorConnPoolResource = new Resource(Resource.CONNECTOR_CONNECTION_POOL);
755         if(nameNode != null){
756             poolName = nameNode.getNodeValue();
757             connectorConnPoolResource.setAttribute(CONNECTION_POOL_NAME, poolName);
758         }
759        if(raConfigNode != null){
760             String JavaDoc raConfig = raConfigNode.getNodeValue();
761             connectorConnPoolResource.setAttribute(RESOURCE_ADAPTER_CONFIG_NAME,raConfig);
762         }
763         if(conDefNode != null){
764             String JavaDoc conDef = conDefNode.getNodeValue();
765             connectorConnPoolResource.setAttribute(CONN_DEF_NAME,conDef);
766         }
767         if(steadyPoolSizeNode != null){
768             String JavaDoc steadyPoolSize = steadyPoolSizeNode.getNodeValue();
769             connectorConnPoolResource.setAttribute(CONN_STEADY_POOL_SIZE,steadyPoolSize);
770         }
771         if(maxPoolSizeNode != null){
772             String JavaDoc maxPoolSize = maxPoolSizeNode.getNodeValue();
773             connectorConnPoolResource.setAttribute(CONN_MAX_POOL_SIZE,maxPoolSize);
774         }
775         if(poolResizeNode != null){
776             String JavaDoc poolResize = poolResizeNode.getNodeValue();
777             connectorConnPoolResource.setAttribute(CONN_POOL_RESIZE_QUANTITY,poolResize);
778         }
779         if(idleTimeOutNode != null){
780             String JavaDoc idleTimeOut = idleTimeOutNode.getNodeValue();
781             connectorConnPoolResource.setAttribute(CONN_IDLE_TIME_OUT,idleTimeOut);
782         }
783         if(failAllConnNode != null){
784             String JavaDoc failAllConn = failAllConnNode.getNodeValue();
785             connectorConnPoolResource.setAttribute(CONN_FAIL_ALL_CONNECTIONS,failAllConn);
786         }
787                      
788         NodeList JavaDoc children = nextKid.getChildNodes();
789         //get description
790
generatePropertyElement(connectorConnPoolResource, children);
791         
792         vResources.add(connectorConnPoolResource);
793         // with the given poolname ..create security-map
794
if (children != null){
795             for (int i=0; i<children.getLength(); i++) {
796                 if((children.item(i).getNodeName().equals(SECURITY_MAP)))
797                     generateSecurityMap(poolName,children.item(i));
798                     
799             }
800         }
801         
802         
803         
804         //debug strings
805
printResourceElements(connectorConnPoolResource);
806     }
807     
808     private void generateSecurityMap(String JavaDoc poolName,Node JavaDoc mapNode) throws Exception JavaDoc{
809         
810         NamedNodeMap JavaDoc attributes = mapNode.getAttributes();
811         if (attributes == null)
812             return ;
813         Node JavaDoc nameNode
814             = attributes.getNamedItem(SECURITY_MAP_NAME);
815                
816               
817         Resource map = new Resource(Resource.CONNECTOR_SECURITY_MAP);
818         if(nameNode != null){
819             String JavaDoc name = nameNode.getNodeValue();
820             map.setAttribute(SECURITY_MAP_NAME, name);
821         }
822         if(poolName != null)
823            map.setAttribute(POOL_NAME,poolName);
824         
825         StringBuffer JavaDoc principal = new StringBuffer JavaDoc();
826         StringBuffer JavaDoc usergroup = new StringBuffer JavaDoc();
827         
828         NodeList JavaDoc children = mapNode.getChildNodes();
829         
830         if(children != null){
831             for (int i=0; i<children.getLength(); i++){
832                 Node JavaDoc gChild = children.item(i);
833                 String JavaDoc strNodeName = gChild.getNodeName();
834                 if(strNodeName.equals(PRINCIPAL)){
835                     String JavaDoc p = (gChild.getFirstChild()).getNodeValue();
836                     principal.append(p+",");
837                 }
838                 if(strNodeName.equals(USERGROUP)){
839                     String JavaDoc u = (gChild.getFirstChild()).getNodeValue();
840                     usergroup.append(u+",");
841                 }
842                 if((strNodeName.equals(BACKEND_PRINCIPAL))){
843                     NamedNodeMap JavaDoc attributes1 = (children.item(i)).getAttributes();
844                     if(attributes1 != null){
845                         Node JavaDoc userNode = attributes1.getNamedItem(USER_NAME);
846                         if(userNode != null){
847                             String JavaDoc userName =userNode.getNodeValue();
848                             map.setAttribute(USER_NAME,userName);
849                         }
850                         Node JavaDoc passwordNode = attributes1.getNamedItem(PASSWORD);
851                         if(passwordNode != null){
852                             String JavaDoc pwd = passwordNode.getNodeValue();
853                             map.setAttribute(PASSWORD,pwd);
854                         }
855                     }
856                 }
857             }
858         }
859         if(principal != null)
860             map.setAttribute(PRINCIPAL,convertToStringArray(principal.toString()));
861         if(usergroup != null)
862             map.setAttribute("user_group",convertToStringArray(usergroup.toString()));
863        vResources.add(map);
864     }//end of generateSecurityMap....
865

866     
867     /*
868      * Generate the Resource Adapter Config
869      *
870      */

871        
872      
873     private void generateResourceAdapterConfig(Node JavaDoc nextKid) throws Exception JavaDoc
874     { NamedNodeMap JavaDoc attributes = nextKid.getAttributes();
875         if (attributes == null)
876             return;
877         
878         Resource resAdapterConfigResource = new Resource(Resource.RESOURCE_ADAPTER_CONFIG);
879         Node JavaDoc resAdapConfigNode = attributes.getNamedItem(RES_ADAPTER_CONFIG);
880         if(resAdapConfigNode != null){
881             String JavaDoc resAdapConfig = resAdapConfigNode.getNodeValue();
882             resAdapterConfigResource.setAttribute(RES_ADAPTER_CONFIG,resAdapConfig);
883         }
884         Node JavaDoc poolIdNode = attributes.getNamedItem(THREAD_POOL_IDS);
885         if(poolIdNode != null){
886             String JavaDoc threadPoolId = poolIdNode.getNodeValue();
887             resAdapterConfigResource.setAttribute(THREAD_POOL_IDS,threadPoolId);
888         }
889         Node JavaDoc resAdapNameNode = attributes.getNamedItem(RES_ADAPTER_NAME);
890         if(resAdapNameNode != null){
891             String JavaDoc resAdapName = resAdapNameNode.getNodeValue();
892             resAdapterConfigResource.setAttribute(RES_ADAPTER_NAME,resAdapName);
893         }
894         
895         NodeList JavaDoc children = nextKid.getChildNodes();
896         generatePropertyElement(resAdapterConfigResource, children);
897         vResources.add(resAdapterConfigResource);
898         
899         //debug strings
900
printResourceElements(resAdapterConfigResource);
901     }
902     
903     /**
904      * Returns an Iterator of <code>Resource</code>objects in the order as defined
905      * in the resources XML configuration file. Maintained for backward compat
906      * purposes only.
907      */

908     public Iterator JavaDoc<Resource> getResources()
909     {
910         return vResources.iterator();
911     }
912     
913     public List JavaDoc getResourcesList() {
914         return vResources;
915     }
916     
917     /**
918      * Returns an List of <code>Resource</code>objects that needs to be
919      * created prior to module deployment. This includes all non-Connector
920      * resources and resource-adapter-config
921      * @param resources List of resources, from which the non connector
922      * resources need to be obtained.
923      * @param isResourceCreation indicates if this determination needs to be
924      * done during the <code>PreResCreationPhase</code>. In the
925      * <code>PreResCreationPhase</code>, RA config is added to the
926      * non connector list, so that the RA config is created prior to the
927      * RA deployment. For all other purpose, this flag needs to be set to false.
928      */

929     public static List JavaDoc getNonConnectorResourcesList(List JavaDoc<Resource> resources,
930                                                  boolean isResourceCreation) {
931         return getResourcesOfType(resources, NONCONNECTOR, isResourceCreation);
932     }
933
934     /**
935      * Returns an Iterator of <code>Resource</code> objects that correspond to
936      * connector resources that needs to be created post module deployment. They
937      * are arranged in the order in which the resources needs to be created
938      * @param resources List of resources, from which the non connector
939      * resources need to be obtained.
940      * @param isResourceCreation indicates if this determination needs to be
941      * done during the <code>PreResCreationPhase</code>. In the
942      * <code>PreResCreationPhase</code>, RA config is added to the
943      * non connector list, so that the RA config is created prior to the
944      * RA deployment. For all other purpose, this flag needs to be set to false.
945      */

946     
947     public static List JavaDoc getConnectorResourcesList(List JavaDoc<Resource> resources, boolean isResourceCreation) {
948         return getResourcesOfType(resources, CONNECTOR, isResourceCreation);
949     }
950     
951     /*
952      * Print(Debug) the resource
953      */

954     private void printResourceElements(Resource resource)
955     {
956         AttributeList JavaDoc attrList = resource.getAttributes();
957         
958         for (int i=0; i<attrList.size(); i++)
959         {
960             Attribute JavaDoc attr = (Attribute JavaDoc)attrList.get(i);
961             Logger JavaDoc logger = Logger.getLogger(AdminConstants.kLoggerName);
962             logger.log(Level.FINE, "general.print_attr_name", attr.getName());
963         }
964     }
965     
966     // Helper Method to convert a String type to a String[]
967
private String JavaDoc[] convertToStringArray(Object JavaDoc sOptions){
968         StringTokenizer JavaDoc optionTokenizer = new StringTokenizer JavaDoc((String JavaDoc)sOptions,",");
969         int size = optionTokenizer.countTokens();
970         String JavaDoc [] sOptionsList = new String JavaDoc[size];
971         for (int ii=0; ii<size; ii++){
972             sOptionsList[ii] = optionTokenizer.nextToken();
973         }
974         return sOptionsList;
975      }
976     
977     
978       class AddResourcesErrorHandler implements ErrorHandler JavaDoc {
979           public void error(SAXParseException JavaDoc e) throws org.xml.sax.SAXException JavaDoc{
980            throw e ;
981         }
982           public void fatalError(SAXParseException JavaDoc e) throws org.xml.sax.SAXException JavaDoc{
983           throw e ;
984         }
985           public void warning(SAXParseException JavaDoc e) throws org.xml.sax.SAXException JavaDoc{
986           throw e ;
987         }
988     }
989       
990       
991     public InputSource JavaDoc resolveEntity(String JavaDoc publicId,String JavaDoc systemId)
992         throws SAXException JavaDoc {
993         InputSource JavaDoc is = null;
994         try {
995             String JavaDoc dtd = System.getProperty(SystemPropertyConstants.INSTALL_ROOT_PROPERTY) +
996                          File.separator + "lib" + File.separator + "dtds" + File.separator +
997                          "sun-resources_1_2.dtd";
998             is = new InputSource JavaDoc(new java.io.FileInputStream JavaDoc(dtd));
999         } catch(Exception JavaDoc e) {
1000            throw new SAXException JavaDoc("cannot resolve dtd", e);
1001        }
1002        return is;
1003    }
1004}
1005
1006
Popular Tags