KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > deployment > backend > AppDD


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  * AppDD.java
26  *
27  * Created on December 7, 2001, 1:48 PM
28  */

29
30 package com.sun.enterprise.deployment.backend;
31
32 import java.io.*;
33 import java.util.*;
34 import java.util.logging.*;
35
36 import javax.enterprise.deploy.shared.ModuleType JavaDoc;
37
38 import com.sun.enterprise.deployment.Application;
39 import com.sun.enterprise.deployment.io.ApplicationDeploymentDescriptorFile;
40 import com.sun.enterprise.deployment.io.runtime.ApplicationRuntimeDDFile;
41 import com.sun.enterprise.deployment.util.ModuleDescriptor;
42
43 import org.xml.sax.SAXParseException JavaDoc;
44 import com.sun.enterprise.util.i18n.StringManager;
45 import com.sun.enterprise.deployment.util.LogDomains;
46
47
48 /** Code for parsing out the names of all modules in Application.xml
49  *
50  * @author bnevins
51  * @version
52  */

53
54 public class AppDD
55 {
56     public AppDD(File f) throws IASDeploymentException, IOException, SAXParseException JavaDoc
57     {
58         this(f, false);
59     }
60
61     ///////////////////////////////////////////////////////////////////////////
62

63     public AppDD(File f, boolean validateXML) throws IASDeploymentException, IOException, SAXParseException JavaDoc
64     {
65         if(f == null || !f.exists()) {
66             String JavaDoc msg = localStrings.getString(
67                     "enterprise.deployment.backend.bad_file_parameter", f );
68             throw new IllegalArgumentException JavaDoc( msg );
69         }
70         
71                 File f2 = null;
72         if(f.isDirectory())
73         {
74             File f1 = new File(f, "META-INF");
75             f = new File(f1, "application.xml");//noi18n
76
f2 = new File(f1, "sun-application.xml");//noi18n
77
}
78
79         //4669843 - 5/2/02 WBN: changed message and type of Exception -- it used to be IllegalArgumentException
80
if(!f.exists() || f.isDirectory()) {
81             String JavaDoc msg = localStrings.getString(
82                     "enterprise.deployment.backend.no_application_xml",
83                     f.getPath() );
84             throw new IASDeploymentException( msg );
85         }
86         
87         file = f;
88
89                 if(f2 != null && f2.exists() && f2.isFile()) {
90                     file2 = f2;
91                 }
92
93         try
94         {
95             parse(validateXML);
96         }
97         catch(Throwable JavaDoc t)
98         {
99                     
100                     t.printStackTrace();
101                     if(t instanceof IASDeploymentException)
102                         throw (IASDeploymentException) t;
103                     else {
104                         String JavaDoc msg = localStrings.getString(
105                         "enterprise.deployment.backend.error_parsing_application_xml",
106                         file.getPath(), t );
107                         throw new IASDeploymentException( msg, t );
108                     }
109         }
110     }
111
112     ///////////////////////////////////////////////////////////////////////////
113

114         public Application getApplication()
115         {
116          
117                 return app;
118         }
119
120     public String JavaDoc[] getEjbModules()
121     {
122         String JavaDoc[] ss = new String JavaDoc[ejbModules.size()];
123         
124         return (String JavaDoc[])ejbModules.toArray(ss);
125     }
126
127     ///////////////////////////////////////////////////////////////////////////
128

129     public String JavaDoc[] getWarModules()
130     {
131         String JavaDoc[] ss = new String JavaDoc[warModules.size()];
132         
133         return (String JavaDoc[])warModules.toArray(ss);
134     }
135
136     ///////////////////////////////////////////////////////////////////////////
137

138     public String JavaDoc[] getRarModules()
139     {
140         String JavaDoc[] ss = new String JavaDoc[rarModules.size()];
141         
142         return (String JavaDoc[])rarModules.toArray(ss);
143     }
144
145     ///////////////////////////////////////////////////////////////////////////
146

147     public String JavaDoc[] getClientModules()
148     {
149         String JavaDoc[] ss = new String JavaDoc[clientModules.size()];
150         
151         return (String JavaDoc[])clientModules.toArray(ss);
152     }
153
154     ///////////////////////////////////////////////////////////////////////////
155

156     public String JavaDoc[] getContextRoots()
157     {
158         String JavaDoc[] ss = new String JavaDoc[contextRoots.size()];
159         
160         return (String JavaDoc[])contextRoots.toArray(ss);
161     }
162
163     ///////////////////////////////////////////////////////////////////////////
164

165     public File getFile()
166     {
167         return file;
168     }
169     
170     ///////////////////////////////////////////////////////////////////////////
171

172         private void parse(boolean validateXML) throws IASDeploymentException, IOException, SAXParseException JavaDoc
173     {
174             FileInputStream fis = null;
175             try {
176         fis = new FileInputStream(file);
177         ApplicationDeploymentDescriptorFile addf = new ApplicationDeploymentDescriptorFile();
178                 addf.setXMLValidation(validateXML);
179                 if (validateXML) {
180                     addf.setXMLValidationLevel(addf.PARSING_VALIDATION);
181                 }
182
183                 app = (Application) addf.read(fis);
184
185                 // read runtime deployment descriptor file if it exists
186
if (file2 != null) {
187                     FileInputStream fis2 = new FileInputStream(file2);
188             ApplicationRuntimeDDFile arddf =
189                         new ApplicationRuntimeDDFile();
190                     arddf.setXMLValidation(validateXML);
191                     if (validateXML) {
192                         arddf.setXMLValidationLevel(arddf.PARSING_VALIDATION);
193                     }
194                     app = (Application)arddf.read(app, fis2);
195                     if (fis2 != null) {
196                         fis2.close();
197                     }
198
199                 }
200
201         // WBN 4-19-2002 -- If we are using this class ONLY for getting
202
// context-roots -- please note that node.getApplication(null) has
203
// already done all the time-consuming work. The getXXXX methods
204
// simply return a reference. So performance isn't an issue.
205

206                 for (Iterator modules = app.getModules(); modules.hasNext();) {
207                     ModuleDescriptor md = (ModuleDescriptor) modules.next();
208                     if (md.getModuleType().equals(ModuleType.EJB)) {
209                         ejbModules.add(md.getArchiveUri());
210                     } else
211                     if (md.getModuleType().equals(ModuleType.WAR)) {
212                         warModules.add(md.getArchiveUri());
213                     } else
214                     if (md.getModuleType().equals(ModuleType.CAR)) {
215                         clientModules.add(md.getArchiveUri());
216                     } else
217                     if (md.getModuleType().equals(ModuleType.RAR)) {
218                         rarModules.add(md.getArchiveUri());
219                     }
220                 }
221
222         setContextRoots(app);
223             } finally {
224                 if (fis != null) {
225                     fis.close();
226                 }
227             }
228     }
229
230     ///////////////////////////////////////////////////////////////////////////
231

232     private void setContextRoots(Application app) throws IASDeploymentException
233     {
234             assert app != null;
235             assert warModules != null;
236             assert contextRoots == null; // error to call this method more than once
237

238             contextRoots = new HashSet();
239             
240             for(Iterator it = warModules.iterator(); it.hasNext(); ) {
241                 
242                 ModuleDescriptor md = app.getModuleDescriptorByUri((String JavaDoc) it.next());
243                 String JavaDoc cr = md.getContextRoot();
244                 
245                 if(contextRoots.add(cr) == false) {
246                     // this means that there is more than one web-module
247
// in the Application with the same context-root
248

249                     String JavaDoc msg = localStrings.getString(
250                     "enterprise.deployment.backend.duplicate_context_root",
251                     cr );
252                     throw new IASDeploymentException( msg );
253                 }
254             }
255     }
256         
257     ///////////////////////////////////////////////////////////////////////////
258

259     private String JavaDoc trim(String JavaDoc s)
260     {
261         // change <ejb>xxx</ejb> to xxx
262

263         int index = s.indexOf(">");
264         String JavaDoc ret = s.substring(index + 1);
265         ret = ret.substring(0, ret.indexOf("<"));
266         
267         return ret;
268     }
269     
270     ///////////////////////////////////////////////////////////////////////////
271

272     private File file = null;
273     private File file2 = null;
274     private Set ejbModules = new HashSet();
275     private Set warModules = new HashSet();
276     private Set rarModules = new HashSet();
277     private Set clientModules = new HashSet();
278     private Application app = null;
279     private Set contextRoots = null;
280     private final Logger logger = LogDomains.getLogger(LogDomains.DPL_LOGGER);
281     private static StringManager localStrings =
282         StringManager.getManager( AppDD.class );
283 }
284
Popular Tags