KickJava   Java API By Example, From Geeks To Geeks.

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


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 package com.sun.enterprise.deployment.backend;
24
25 import com.sun.enterprise.deployment.Application;
26 import com.sun.enterprise.deployment.ApplicationClientDescriptor;
27 import com.sun.enterprise.deployment.deploy.shared.AbstractArchive;
28 import com.sun.enterprise.deployment.deploy.shared.FileArchive;
29 import com.sun.enterprise.deployment.deploy.shared.OutputJarArchive;
30 import com.sun.enterprise.deployment.interfaces.ClientJarMaker;
31 import com.sun.enterprise.deployment.interfaces.DeploymentImplConstants;
32 import com.sun.enterprise.deployment.RootDeploymentDescriptor;
33 import com.sun.enterprise.deployment.util.ModuleDescriptor;
34 import com.sun.enterprise.util.i18n.StringManager;
35 import com.sun.enterprise.util.io.FileUtils;
36 import com.sun.enterprise.util.zip.ZipItem;
37
38 import java.io.File JavaDoc;
39 import java.util.Collection JavaDoc;
40 import java.util.Iterator JavaDoc;
41 import java.util.logging.Level JavaDoc;
42 import java.util.Properties JavaDoc;
43 import java.util.Set JavaDoc;
44
45 /**
46  * This thread subclass is responsible for creating the client jar
47  * file.
48  *
49  * @author Jerome Dochez
50  */

51 public class ClientJarMakerThread extends Thread JavaDoc {
52
53     private DeploymentRequest request;
54     private File JavaDoc clientJar;
55     private ZipItem[] clientStubs;
56     private String JavaDoc clientJarChoice = null;
57     
58     private static StringManager localStrings = StringManager.getManager( Deployer.class );
59     
60     /** Creates a new instance of ClientJarMakerThread */
61     public ClientJarMakerThread(DeploymentRequest request, File JavaDoc clientJar,
62                                 ZipItem[] clientStubs,
63                                 String JavaDoc clientJarChoice) {
64         this.request = request;
65         this.clientJar = clientJar;
66         this.clientStubs = clientStubs;
67         this.clientJarChoice = clientJarChoice;
68     }
69     
70     public void run() {
71         // first thing to do is to register ourselves in the
72
// client jar maker registry
73
ClientJarMakerRegistry registry = ClientJarMakerRegistry.getInstance();
74         
75         String JavaDoc moduleID = request.getName();
76         registry.register(moduleID, this);
77         
78         // now we build the client jar file
79
try {
80             createClientJar(request, clientJar, clientStubs, clientJarChoice);
81         } catch(IASDeploymentException e) {
82             // unfortunetely, we cannot provide failures feedback to the client
83
// at this point, but we certainly need to log it.
84
DeploymentLogger.get().log(Level.SEVERE,
85                 localStrings.getString("enterprise.deployment.error_creating_client_jar",
86                     e.getLocalizedMessage()) ,e);
87         
88         } finally {
89             
90             // we are done, unregister ouselves from the registry
91
registry.unregister(moduleID);
92         }
93         
94         // friendly log
95
DeploymentLogger.get().fine("Created client jar file for " + moduleID + " at " + clientJar.getAbsolutePath());
96         
97     }
98    
99     /**
100      * this method is called from the thread to create the client jar or
101      * synchronously from the Deployer
102      */

103     public static final void createClientJar(
104         DeploymentRequest request, File JavaDoc clientJar, ZipItem[] clientStubs,
105         String JavaDoc clientJarChoice) throws IASDeploymentException {
106         try {
107             // client jar naming convension is <app-name>Client.jar
108
OutputJarArchive target = new OutputJarArchive();
109             target.create(clientJar.getAbsolutePath());
110             
111             RootDeploymentDescriptor descriptor;
112             if (request.getDescriptor().isVirtual()) {
113                 descriptor = request.getDescriptor().getStandaloneBundleDescriptor();
114             } else {
115                 descriptor = request.getDescriptor();
116             }
117             
118             AbstractArchive source = new FileArchive();
119             ((FileArchive) source).open(request.getDeployedDirectory().getAbsolutePath());
120             PEDeploymentFactoryImpl pe = new PEDeploymentFactoryImpl();
121             Properties JavaDoc props = getPropertiesForClientJarMaker(
122                 CLIENT_JAR_CHOICES.getClientJarChoice(clientJarChoice),
123                 request, descriptor);
124             ClientJarMaker jarMaker = pe.getClientJarMaker(props);
125
126             // copy xml files from generated directory archive to original
127
// directory archive so the created client jar contain
128
// processed xml files.
129
if (FileUtils.safeIsDirectory(
130                 request.getGeneratedXMLDirectory())) {
131                 AbstractArchive source2 = new FileArchive();
132                 ((FileArchive) source2).open(
133                     request.getGeneratedXMLDirectory().getAbsolutePath());
134                 jarMaker.create(descriptor, source, source2, target, clientStubs,
135                     null);
136                 source2.close();
137             } else {
138                 jarMaker.create(descriptor, source, target, clientStubs,null);
139             }
140             source.close();
141             target.close();
142         } catch(Exception JavaDoc e) {
143             IASDeploymentException newE = new IASDeploymentException();
144             newE.initCause(e);
145             throw newE;
146         }
147     }
148
149     private static Properties JavaDoc getPropertiesForClientJarMaker(
150         CLIENT_JAR_CHOICES choice,
151         DeploymentRequest request, RootDeploymentDescriptor descriptor) {
152       
153         boolean qualify = qualifyModuleClientFormat(request,descriptor);
154
155         Properties JavaDoc props = null;
156         Boolean JavaDoc propertySetting = choice.useModuleClientJarMaker(qualify);
157
158         if (propertySetting != null) {
159             props = new Properties JavaDoc();
160             props.setProperty(
161                 DeploymentImplConstants.USE_MODULE_CLIENT_JAR_MAKER,
162                 propertySetting.toString());
163         }
164         return props;
165     }
166
167     /**
168      * This method determines if the generated appclient jar should be in
169      * the standalone appclient module format. There are 3 cases to create
170      * the simpler version of the generated appclient:
171      * 1) deployed module is a standalone appclient/ejb
172      * 2) deployed module is an ear containing 0 or 1 appclient jar
173      * 3) deployed module does not contain appclient that uses persistence unit
174      */

175     private static boolean qualifyModuleClientFormat
176         (DeploymentRequest request, RootDeploymentDescriptor descriptor) {
177
178         // create appclient format for standalone appclient module
179
if (request.getDescriptor().isVirtual()) {
180             return true;
181         }
182
183         Application app = Application.class.cast(descriptor);
184         Set JavaDoc appClients = app.getApplicationClientDescriptors();
185         if (appClients != null) {
186     
187             // create ear format of appclient if there are more than
188
// one appclients in the ear file
189
if (appClients.size() > 1) {
190                 return false;
191             }
192
193             if (!appClients.isEmpty()) {
194                 ApplicationClientDescriptor ac =
195                 ApplicationClientDescriptor.class.cast(appClients.iterator().next());
196
197                 // checks to see if this appclient has entries for
198
// message-destination-ref. if so, use ear format
199
Set JavaDoc msgDestRefs = ac.getMessageDestinationReferenceDescriptors();
200                 if (msgDestRefs != null && !msgDestRefs.isEmpty()) {
201                     return false;
202                 }
203
204                 // checks to see if this appclient depends on a PU.
205
// if so, use ear format
206
Set JavaDoc entityMgrFacRefs = ac.getEntityManagerFactoryReferenceDescriptors();
207                 if (entityMgrFacRefs != null && !entityMgrFacRefs.isEmpty()) {
208                     return false;
209                 }
210             }
211         }
212
213         for (Iterator JavaDoc modules = app.getModules(); modules.hasNext();) {
214             ModuleDescriptor md = ModuleDescriptor.class.cast(modules.next());
215
216             // checks to see if any of the sub modules uses altDD
217
// if so, use the application package format. we could
218
// also choose to override the original dd with the altDD,
219
// but that might get very confusing when someone is trying
220
// to debug.
221
if (md.getAlternateDescriptor() != null) {
222                 return false;
223             }
224         }
225
226         return true;
227     }
228
229     private static enum CLIENT_JAR_CHOICES {
230
231         //original implementation, for comparing behavior regression
232
USE_ORIGINAL_MAKER {
233         public Boolean JavaDoc useModuleClientJarMaker(boolean qualify) {
234             return null;}},
235
236         //transitional default option. only turn on the generation of the
237
//appclient module format
238
USE_TRANSITION_MAKER {
239         public Boolean JavaDoc useModuleClientJarMaker(boolean qualify) {
240             return qualify == true ? Boolean.TRUE : null;}},
241
242         //alternative default option. generate appclient module format
243
//or the ear module format accordingly
244
USE_COMBO_MAKER {
245         public Boolean JavaDoc useModuleClientJarMaker(boolean qualify) {
246             return new Boolean JavaDoc(qualify);}},
247
248         //current default. generate only the ear module format, i.e. no
249
//optimization on certain deployed ear
250
USE_EAR_MAKER {
251         public Boolean JavaDoc useModuleClientJarMaker(boolean qualify) {
252             return Boolean.FALSE;}};
253        
254         public abstract Boolean JavaDoc useModuleClientJarMaker(boolean qualify);
255        
256         public static CLIENT_JAR_CHOICES DEFAULT_CHOICE = USE_EAR_MAKER;
257        
258         public static CLIENT_JAR_CHOICES getClientJarChoice(String JavaDoc choice) {
259             try {
260                 if (choice == null) {
261                     return DEFAULT_CHOICE;
262                 }
263                 return valueOf(choice);
264             } catch (IllegalArgumentException JavaDoc iae) {
265                 return DEFAULT_CHOICE;
266             }
267         }
268     }
269 }
270
Popular Tags