KickJava   Java API By Example, From Geeks To Geeks.

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


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

24
25 package com.sun.enterprise.deployment.archivist;
26
27 import java.io.InputStream JavaDoc;
28 import java.io.IOException JavaDoc;
29 import java.util.jar.Attributes JavaDoc;
30 import java.util.jar.Attributes.Name;
31 import java.util.jar.Manifest JavaDoc;
32 import java.util.logging.Level JavaDoc;
33 import java.util.Vector JavaDoc;
34 import java.util.Iterator JavaDoc;
35 import javax.enterprise.deploy.shared.ModuleType JavaDoc;
36 import org.xml.sax.SAXParseException JavaDoc;
37
38 import com.sun.enterprise.deployment.ApplicationClientDescriptor;
39 import com.sun.enterprise.deployment.deploy.shared.AbstractArchive;
40 import com.sun.enterprise.deployment.Descriptor;
41 import com.sun.enterprise.deployment.Application;
42 import com.sun.enterprise.deployment.io.AppClientDeploymentDescriptorFile;
43 import com.sun.enterprise.deployment.io.DeploymentDescriptorFile;
44 import com.sun.enterprise.deployment.io.runtime.AppClientRuntimeDDFile;
45 import com.sun.enterprise.deployment.node.appclient.AppClientNode;
46 import com.sun.enterprise.deployment.RootDeploymentDescriptor;
47 import com.sun.enterprise.deployment.util.AppClientVisitor;
48 import com.sun.enterprise.deployment.util.ApplicationValidator;
49 import com.sun.enterprise.deployment.util.DOLUtils;
50 import com.sun.enterprise.deployment.util.ModuleContentValidator;
51 import com.sun.enterprise.deployment.util.ModuleDescriptor;
52
53 /**
54  * This class is responsible for handling J2EE app client files.
55  *
56  * @author Sheetal Vartak
57  * @version
58  */

59 public class AppClientArchivist extends Archivist {
60
61     ApplicationClientDescriptor descriptor = null;
62     DeploymentDescriptorFile standardDD = new AppClientDeploymentDescriptorFile();
63     
64     /** Creates new ApplicationClientArchvisit */
65     public AppClientArchivist() {
66         handleRuntimeInfo = true;
67     }
68    
69     /**
70      * @return the module type handled by this archivist
71      * as defined in the application DTD
72      *
73      */

74     public ModuleType JavaDoc getModuleType() {
75         return ModuleType.CAR;
76     }
77             
78     /**
79      * Set the DOL descriptor for this Archivist, used by super classes
80      */

81     public void setDescriptor(Descriptor descriptor) {
82         if (descriptor instanceof ApplicationClientDescriptor) {
83             this.descriptor = (ApplicationClientDescriptor) descriptor;
84         } else {
85            if (descriptor instanceof Application) {
86                 // this is acceptable if the application actually represents
87
// a standalone module
88
java.util.Set JavaDoc appClientBundles = ((Application) descriptor).getApplicationClientDescriptors();
89                 if (appClientBundles.size()>0) {
90                     this.descriptor = (ApplicationClientDescriptor) appClientBundles.iterator().next();
91                     if (this.descriptor.getModuleDescriptor().isStandalone())
92                         return;
93                     else
94                         this.descriptor=null;
95                 }
96             }
97             DOLUtils.getDefaultLogger().log(Level.SEVERE, "enterprise.deployment.backend.descriptorFailure", new Object JavaDoc[] {this});
98             throw new RuntimeException JavaDoc("Error setting descriptor " + descriptor + " in " + this);
99         }
100     }
101     
102     /**
103      * @return the DeploymentDescriptorFile responsible for handling
104      * standard deployment descriptor
105      */

106     public DeploymentDescriptorFile getStandardDDFile() {
107         return standardDD;
108     }
109     
110     /**
111      * @return if exists the DeploymentDescriptorFile responsible for
112      * handling the configuration deployment descriptors
113      */

114     public DeploymentDescriptorFile getConfigurationDDFile() {
115         return new AppClientRuntimeDDFile();
116     }
117     
118     /**
119      * @return the Descriptor for this archvist
120      */

121     public Descriptor getDescriptor() {
122         return descriptor;
123     }
124
125     /**
126      * @return a default BundleDescriptor for this archivist
127      */

128     public Descriptor getDefaultBundleDescriptor() {
129         ApplicationClientDescriptor appClientDesc =
130             new ApplicationClientDescriptor();
131         return appClientDesc;
132     }
133
134     /**
135      * validates the DOL Objects associated with this archivist, usually
136      * it requires that a class loader being set on this archivist or passed
137      * as a parameter
138      */

139     public void validate(ClassLoader JavaDoc aClassLoader) {
140         ClassLoader JavaDoc cl = aClassLoader;
141         if (cl==null) {
142             cl = classLoader;
143         }
144         if (cl==null) {
145             return;
146         }
147         descriptor.setClassLoader(cl);
148         descriptor.visit((AppClientVisitor) new ApplicationValidator());
149     }
150         
151     /**
152      * perform any action after all standard DDs is read
153      * @param the deployment descriptor for the module
154      * @param the module archive
155      */

156     protected void postStandardDDsRead(RootDeploymentDescriptor descriptor,
157             AbstractArchive archive) throws IOException JavaDoc {
158         super.postStandardDDsRead(descriptor, archive);
159         // look for MAIN_CLASS
160
ApplicationClientDescriptor appClient = (ApplicationClientDescriptor)descriptor;
161         Manifest JavaDoc m = archive.getManifest();
162         appClient.setMainClassName(getMainClassName(m));
163     }
164     
165     /**
166      * perform any post deployment descriptor reading action
167      *
168      * @param the deployment descriptor for the module
169      * @param the module archive
170      */

171     protected void postOpen(RootDeploymentDescriptor descriptor, AbstractArchive archive)
172         throws IOException JavaDoc
173     {
174         
175         super.postOpen(descriptor, archive);
176         
177         ApplicationClientDescriptor appClient = (ApplicationClientDescriptor) descriptor;
178         ModuleContentValidator mdv = new ModuleContentValidator(archive);
179         appClient.visit(mdv);
180     }
181
182     /**
183      * Add this archive to an application archivist
184      *
185      * @param application archive to add itself to
186      * @param library jars for this archive
187      * @param external deployment descriptor path
188      */

189     protected ModuleDescriptor addToArchive(ApplicationArchivist appArch, String JavaDoc externalDD)
190         throws IOException JavaDoc, SAXParseException JavaDoc {
191             
192         ModuleDescriptor module = super.addToArchive(appArch, externalDD);
193         if (module!=null) {
194             ApplicationClientDescriptor acd = (ApplicationClientDescriptor) module.getDescriptor();
195             AbstractArchive jarFile = abstractArchiveFactory.openArchive(getArchiveUri());
196             Manifest JavaDoc jarManifest = jarFile.getManifest();
197             if (jarManifest!=null) {
198                 String JavaDoc mainClassName = getMainClassName(jarManifest);
199                 if (mainClassName!=null) {
200                     acd.setMainClassName(mainClassName);
201                 }
202             }
203             jarFile.close();
204         }
205         return module;
206     }
207     
208     /**
209      * writes the content of an archive to a JarFile
210      *
211      * @param the input archive
212      * @param the archive output stream to write to
213      * @param the files to not write from the original archive
214      */

215     protected void writeContents(AbstractArchive in, AbstractArchive out, Vector JavaDoc entriesToSkip)
216         throws IOException JavaDoc {
217         
218         // prepare the manifest file to add the main class entry
219
if (manifest==null) {
220             manifest = new Manifest JavaDoc();
221         }
222     manifest.getMainAttributes().put(Attributes.Name.MANIFEST_VERSION, MANIFEST_VERSION_VALUE);
223         manifest.getMainAttributes().put(Attributes.Name.MAIN_CLASS,
224             ((ApplicationClientDescriptor) getDescriptor()).getMainClassName());
225         
226         super.writeContents(in, out, entriesToSkip);
227     }
228      
229     /**
230      * @return the manifest attribute Main-class
231      */

232     public String JavaDoc getMainClassName(Manifest JavaDoc m) {
233         if (m!=null) {
234             return m.getMainAttributes().getValue(Attributes.Name.MAIN_CLASS);
235         }
236         return null;
237     }
238
239     protected boolean postHandles(AbstractArchive abstractArchive)
240             throws IOException JavaDoc {
241         //check the main-class attribute
242
if (getMainClassName(abstractArchive.getManifest()) != null) {
243             return true;
244         }
245
246         return false;
247     }
248
249     protected String JavaDoc getArchiveExtension() {
250         return APPCLIENT_EXTENSION;
251     }
252
253     @Override JavaDoc public void readPersistenceDeploymentDescriptors(
254             AbstractArchive archive,
255             Descriptor descriptor)
256             throws IOException JavaDoc, SAXParseException JavaDoc {
257         if (logger.isLoggable(Level.FINE)) {
258             logger.logp(Level.FINE, "AppClientArchivist",
259                     "readPersistenceDeploymentDescriptors", "archive = {0}",
260                     archive.getURI());
261         }
262         // note we pass "" as the PURootPath because META-INF/persistence.xml
263
// can only be present on the top level in an client-jar, so the root of
264
// persistence unit is always same as the root of an client-jar file.
265
// hence relative distance between them is empty.
266
readPersistenceDeploymentDescriptor(archive, "", descriptor);
267     }
268 }
269
Popular Tags