KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > deployment > io > DeploymentDescriptorFileFactory


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
25 package com.sun.enterprise.deployment.io;
26
27 import java.io.File JavaDoc;
28 import javax.xml.parsers.*;
29 import org.w3c.dom.*;
30
31 import com.sun.enterprise.deployment.*;
32 import com.sun.enterprise.deployment.xml.*;
33 import com.sun.enterprise.deployment.node.SaxParserHandler;
34 import com.sun.enterprise.deployment.node.SaxParserHandlerFactory;
35 import javax.enterprise.deploy.shared.ModuleType JavaDoc;
36
37 /**
38  * Factory for DeploymentDescriptorFile implementations
39  *
40  * @author Jerome Dochez
41  */

42 public class DeploymentDescriptorFileFactory {
43     
44     /** no instance necessary for this factory **/
45     private DeploymentDescriptorFileFactory() {
46     }
47     
48     /**
49      * Creates and return an appropriate DeploymentDescriptorFile
50      * capable of handling the passed descriptor
51      *
52      * @param descriptor used to identify the associated DeploymentDescriptorFile
53      * @return the created DeploymentDescriptorFile
54      */

55     public static DeploymentDescriptorFile getDDFileFor(RootDeploymentDescriptor descriptor) {
56         if (descriptor instanceof Application) {
57             return new ApplicationDeploymentDescriptorFile();
58         }
59         if (descriptor instanceof EjbBundleDescriptor) {
60             return new EjbDeploymentDescriptorFile();
61         }
62         if (descriptor instanceof WebBundleDescriptor) {
63             return new WebDeploymentDescriptorFile();
64         }
65         if (descriptor instanceof ConnectorDescriptor) {
66             return new ConnectorDeploymentDescriptorFile();
67         }
68         if (descriptor instanceof ApplicationClientDescriptor) {
69             return new AppClientDeploymentDescriptorFile();
70         }
71         return null;
72     }
73     
74     /**
75      * Creates and return an appropriate DeploymentDescriptorFile
76      * capable of handling the passed descriptor
77      *
78      * @param descriptor used to identify the associated DeploymentDescriptorFile
79      * @return the created DeploymentDescriptorFile
80      */

81     public static DeploymentDescriptorFile getDDFileFor(ModuleType JavaDoc type) {
82         if (type==null) {
83             return null;
84         }
85         if (type.equals(ModuleType.EAR)) {
86             return new ApplicationDeploymentDescriptorFile();
87         }
88         if (type.equals(ModuleType.EJB)) {
89             return new EjbDeploymentDescriptorFile();
90         }
91         if (type.equals(ModuleType.WAR)) {
92             return new WebDeploymentDescriptorFile();
93         }
94         if (type.equals(ModuleType.RAR)) {
95             return new ConnectorDeploymentDescriptorFile();
96         }
97         if (type.equals(ModuleType.CAR)) {
98             return new AppClientDeploymentDescriptorFile();
99         }
100         return null;
101     }
102     
103     /**
104      * Creates and return the appropriate DeploymentDescriptorFile
105      * depending on the XML file passed.
106      *
107      * @param input xml file
108      * @return the DeploymentDescriptorFile responsible for handling
109      * the xml file
110      */

111     public static DeploymentDescriptorFile getDDFileFor(File JavaDoc xmlFile)
112         throws Exception JavaDoc {
113             
114         // this is higly unefficient but we read the xml file as a DOM
115
// tree, figure out the top xml element name and return the
116
// appropriate DeploymentDescriptorFile
117

118         DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
119         factory.setValidating(false);
120         DocumentBuilder docBuilder = factory.newDocumentBuilder();
121         docBuilder.setEntityResolver(SaxParserHandlerFactory.newInstance());
122         Document document = docBuilder.parse(xmlFile);
123         Element element = document.getDocumentElement();
124         if (element.getTagName().equals(ApplicationTagNames.APPLICATION)) {
125             return new ApplicationDeploymentDescriptorFile();
126         }
127         if (element.getTagName().equals(EjbTagNames.EJB_BUNDLE_TAG)) {
128             return new EjbDeploymentDescriptorFile();
129         }
130         if (element.getTagName().equals(WebTagNames.WEB_BUNDLE)) {
131             return new WebDeploymentDescriptorFile();
132         }
133         if (element.getTagName().equals(ConnectorTagNames.CONNECTOR)) {
134             return new ConnectorDeploymentDescriptorFile();
135         }
136         if (element.getTagName().equals(ApplicationClientTagNames.APPLICATION_CLIENT_TAG)) {
137             return new AppClientDeploymentDescriptorFile();
138         }
139         if (element.getTagName().equals(PersistenceTagNames.PERSISTENCE)) {
140             return new PersistenceDeploymentDescriptorFile();
141         }
142         return null;
143     }
144 }
145
Popular Tags