KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > deployment > archivist > WebArchivist


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.deployment.archivist;
25
26 import java.io.File JavaDoc;
27 import java.io.IOException JavaDoc;
28 import java.util.logging.Level JavaDoc;
29 import java.util.*;
30 import javax.enterprise.deploy.shared.ModuleType JavaDoc;
31 import org.xml.sax.SAXParseException JavaDoc;
32
33 import com.sun.enterprise.deployment.Application;
34 import com.sun.enterprise.deployment.deploy.shared.AbstractArchive;
35 import com.sun.enterprise.deployment.deploy.shared.Archive;
36 import com.sun.enterprise.deployment.Descriptor;
37 import com.sun.enterprise.deployment.io.DeploymentDescriptorFile;
38 import com.sun.enterprise.deployment.io.DescriptorConstants;
39 import com.sun.enterprise.deployment.io.runtime.WebRuntimeDDFile;
40 import com.sun.enterprise.deployment.io.WebDeploymentDescriptorFile;
41 import com.sun.enterprise.deployment.RootDeploymentDescriptor;
42 import com.sun.enterprise.deployment.util.ApplicationValidator;
43 import com.sun.enterprise.deployment.util.DOLUtils;
44 import com.sun.enterprise.deployment.util.DOLLoadingContextFactory;
45 import com.sun.enterprise.deployment.util.ModuleContentValidator;
46 import com.sun.enterprise.deployment.util.WebBundleVisitor;
47 import com.sun.enterprise.deployment.WebBundleDescriptor;
48
49
50 /**
51  * This module is responsible for reading and write web applications
52  * archive files (war).
53  *
54  * @author Jerome Dochez
55  * @version
56  */

57 public class WebArchivist extends Archivist {
58
59     private WebBundleDescriptor descriptor;
60     
61     /**
62      * The DeploymentDescriptorFile handlers we are delegating for XML i/o
63      */

64     DeploymentDescriptorFile standardDD = new WebDeploymentDescriptorFile();
65
66     /** Creates new WebArchivist */
67     public WebArchivist() {
68     }
69         
70     /**
71      * @return the module type handled by this archivist
72      * as defined in the application DTD
73      *
74      */

75     public ModuleType JavaDoc getModuleType() {
76         return ModuleType.WAR;
77     }
78     
79     /**
80      * Archivist read XML deployment descriptors and keep the
81      * parsed result in the DOL descriptor instances. Sets the descriptor
82      * for a particular Archivst type
83      */

84     public void setDescriptor(Descriptor descriptor) {
85         if (descriptor instanceof WebBundleDescriptor) {
86             this.descriptor = (WebBundleDescriptor) descriptor;
87         } else {
88             if (descriptor instanceof Application) {
89                 // this is acceptable if the application actually represents
90
// a standalone module
91
java.util.Set JavaDoc webBundles = ((Application) descriptor).getWebBundleDescriptors();
92                 if (webBundles.size()>0) {
93                     this.descriptor = (WebBundleDescriptor) webBundles.iterator().next();
94                     if (this.descriptor.getModuleDescriptor().isStandalone())
95                         return;
96                     else
97                         this.descriptor=null;
98                 }
99             }
100             DOLUtils.getDefaultLogger().log(Level.SEVERE, "enterprise.deployment.backend.descriptorFailure", new Object JavaDoc[] {this});
101             throw new RuntimeException JavaDoc("Error setting descriptor " + descriptor + " in " + this);
102         }
103     }
104     
105     /**
106      * @return the location of the web services related deployment
107      * descriptor file inside this archive or null if this archive
108      * does not support webservices implementation.
109      */

110     public String JavaDoc getWebServicesDeploymentDescriptorPath() {
111         return DescriptorConstants.WEB_WEBSERVICES_JAR_ENTRY;
112     }
113     
114     /**
115      * @return the DeploymentDescriptorFile responsible for handling
116      * standard deployment descriptor
117      */

118     public DeploymentDescriptorFile getStandardDDFile() {
119         return standardDD;
120     }
121     
122     /**
123      * @return if exists the DeploymentDescriptorFile responsible for
124      * handling the configuration deployment descriptors
125      */

126     public DeploymentDescriptorFile getConfigurationDDFile() {
127         return new WebRuntimeDDFile();
128     }
129     
130     /**
131      * @return the Descriptor for this archvist
132      */

133     public Descriptor getDescriptor() {
134         return descriptor;
135     }
136
137     /**
138      * @return a default BundleDescriptor for this archivist
139      */

140     public Descriptor getDefaultBundleDescriptor() {
141         WebBundleDescriptor webBundleDesc =
142             DOLLoadingContextFactory.getDefaultWebBundleDescriptor();
143         return webBundleDesc;
144     }
145     
146     /**
147      * perform any post deployment descriptor reading action
148      *
149      * @param descriptor the deployment descriptor for the module
150      * @param archive the module archive
151      */

152     protected void postOpen(RootDeploymentDescriptor descriptor, AbstractArchive archive)
153         throws IOException JavaDoc
154     {
155         super.postOpen(descriptor, archive);
156         WebBundleDescriptor webBundle = (WebBundleDescriptor) descriptor;
157         ModuleContentValidator mdv = new ModuleContentValidator(archive);
158         webBundle.visit(mdv);
159     }
160
161     /**
162      * validates the DOL Objects associated with this archivist, usually
163      * it requires that a class loader being set on this archivist or passed
164      * as a parameter
165      */

166     public void validate(ClassLoader JavaDoc aClassLoader) {
167         ClassLoader JavaDoc cl = aClassLoader;
168         if (cl==null) {
169             cl = classLoader;
170         }
171         if (cl==null) {
172             return;
173         }
174         descriptor.setClassLoader(cl);
175         descriptor.visit((WebBundleVisitor) new ApplicationValidator());
176     }
177
178     /**
179      * In the case of web archive, the super handles() method should be able
180      * to make a unique identification. If not, then the archive is definitely
181      * not a war.
182      */

183     protected boolean postHandles(AbstractArchive abstractArchive)
184             throws IOException JavaDoc {
185         return false;
186     }
187
188     protected String JavaDoc getArchiveExtension() {
189         return WEB_EXTENSION;
190     }
191     
192     /**
193      * @return a list of libraries included in the archivist
194      */

195     public Vector getLibraries(AbstractArchive archive) {
196         
197         Enumeration<String JavaDoc> entries = archive.entries();
198         if (entries==null)
199             return null;
200         
201         Vector libs = new Vector();
202         while (entries.hasMoreElements()) {
203             
204             String JavaDoc entryName = entries.nextElement();
205             if (!entryName.startsWith("WEB-INF/lib")) {
206                 continue; // not in WEB-INF...
207
}
208             if (entryName.endsWith(".jar")) {
209                 libs.add(entryName);
210             }
211         }
212         return libs;
213     }
214
215     @Override JavaDoc public void readPersistenceDeploymentDescriptors(
216             AbstractArchive archive, Descriptor descriptor) throws IOException JavaDoc, SAXParseException JavaDoc {
217         if(logger.isLoggable(Level.FINE)) {
218             logger.logp(Level.FINE, "WebArchivist",
219                     "readPersistenceDeploymentDescriptors", "archive = {0}",
220                     archive.getURI());
221         }
222         Map<String JavaDoc, Archive> subArchives = new HashMap<String JavaDoc, Archive>();
223         Enumeration entries = archive.entries();
224         final String JavaDoc CLASSES_DIR = "WEB-INF/classes/";
225         final String JavaDoc LIB_DIR = "WEB-INF/lib/";
226         final String JavaDoc JAR_EXT = ".jar";
227         try {
228             final String JavaDoc pathOfPersistenceXMLInsideClassesDir =
229                     CLASSES_DIR+DescriptorConstants.PERSISTENCE_DD_ENTRY;
230             while(entries.hasMoreElements()){
231                 final String JavaDoc nextEntry = String JavaDoc.class.cast(entries.nextElement());
232                 if(pathOfPersistenceXMLInsideClassesDir.equals(nextEntry)) {
233                     subArchives.put(CLASSES_DIR, archive.getSubArchive(CLASSES_DIR));
234                 } else if (nextEntry.startsWith(LIB_DIR) && nextEntry.endsWith(JAR_EXT)) {
235                     String JavaDoc jarFile = nextEntry.substring(LIB_DIR.length(), nextEntry.length()-JAR_EXT.length());
236                     if(jarFile.indexOf('/') == -1) { // to avoid WEB-INF/lib/foo/bar.jar
237
// this jarFile is directly inside WEB-INF/lib directory
238
subArchives.put(nextEntry, archive.getSubArchive(nextEntry));
239                     } else {
240                         if(logger.isLoggable(Level.FINE)) {
241                             logger.logp(Level.FINE, "WebArchivist",
242                                     "readPersistenceDeploymentDescriptors",
243                                     "skipping {0} as it exists inside a directory in {1}.",
244                                     new Object JavaDoc[]{nextEntry, LIB_DIR});
245                         }
246                         continue;
247                     }
248                 }
249             }
250             for(Map.Entry<String JavaDoc, Archive> pathToArchiveEntry : subArchives.entrySet()) {
251                 readPersistenceDeploymentDescriptor(pathToArchiveEntry.getValue(), pathToArchiveEntry.getKey(), descriptor);
252             }
253         } finally {
254             for(Archive subArchive : subArchives.values()) {
255                 subArchive.close();
256             }
257         }
258     }
259
260 }
261
Popular Tags