KickJava   Java API By Example, From Geeks To Geeks.

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


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  * WebModuleDeployer.java
26  *
27  * Created on January 11, 2002, 12:11 AM
28  */

29
30 package com.sun.enterprise.deployment.backend;
31
32 import com.sun.ejb.codegen.IASEJBCTimes;
33 import com.sun.enterprise.config.ConfigException;
34 import com.sun.enterprise.config.serverbeans.WebModule;
35 import com.sun.enterprise.deployment.Application;
36 import com.sun.enterprise.deployment.archivist.Archivist;
37 import com.sun.enterprise.deployment.autodeploy.AutoDeployConstants;
38 import com.sun.enterprise.deployment.BundleDescriptor;
39 import com.sun.enterprise.deployment.deploy.shared.AbstractArchive;
40 import com.sun.enterprise.deployment.interfaces.SecurityRoleMapper;
41 import com.sun.enterprise.deployment.interfaces.SecurityRoleMapperFactory;
42 import com.sun.enterprise.deployment.phasing.DeploymentServiceUtils;
43 import com.sun.enterprise.deployment.RootDeploymentDescriptor;
44 import com.sun.enterprise.deployment.WebBundleDescriptor;
45 import com.sun.enterprise.deployment.WebService;
46 import com.sun.enterprise.deployment.WebServicesDescriptor;
47 import com.sun.enterprise.instance.BaseManager;
48 import com.sun.enterprise.instance.InstanceEnvironment;
49 import com.sun.enterprise.instance.ModuleEnvironment;
50 import com.sun.enterprise.instance.WebModulesManager;
51 import com.sun.enterprise.loader.EJBClassPathUtils;
52 import com.sun.enterprise.security.SecurityUtil;
53 import com.sun.enterprise.security.util.IASSecurityException;
54 import com.sun.enterprise.util.i18n.StringManager;
55 import com.sun.enterprise.util.io.FileUtils;
56 import com.sun.enterprise.util.StringUtils;
57 import com.sun.enterprise.util.zip.ZipFileException;
58 import com.sun.enterprise.util.zip.ZipItem;
59 import com.sun.web.security.*;
60 import java.io.File JavaDoc;
61 import java.io.IOException JavaDoc;
62 import java.util.*;
63 import java.util.logging.Level JavaDoc;
64
65 /**
66  * WebModuleDeployer is a class for deploying Web Modules
67  * Much of the code is in the super-class.
68  *
69  * @author bnevins
70  * @version
71  */

72 public class WebModuleDeployer extends ModuleDeployer
73 {
74     WebModuleDeployer(DeploymentRequest r) throws IASDeploymentException
75     {
76         super(r);
77     }
78     
79     ///////////////////////////////////////////////////////////////////////////
80

81     protected BaseManager createConfigManager(InstanceEnvironment ienv, ModuleEnvironment menv)
82             throws IASDeploymentException, ConfigException
83     {
84         webModulesMgr = new WebModulesManager(ienv);
85         return webModulesMgr;
86     }
87     
88     ///////////////////////////////////////////////////////////////////////////
89

90     protected void preDeploy() throws IASDeploymentException
91     {
92         try
93         {
94             assert moduleDir != null;
95             assert StringUtils.ok(moduleName);
96             
97             if(isRegistered()) {
98                 String JavaDoc msg = localStrings.getString(
99                         "enterprise.deployment.backend.deploy_error_module_exists" );
100                 throw new IASDeploymentException( msg );
101             }
102
103             // if the directories already exist on disk -- wipe them out
104
// We do this because otherwise they'd never get this Module deployed
105
// without wiping the dirs manually
106
liquidateModuleDirAndStubsDirIfTheyHappenToExist();
107
108             if(request.isArchive())
109             {
110                             // check if this is a .war file or a single .class file
111
if (request.getFileSource().getFile().getName().endsWith(".class")) {
112                                 // we just need to copy the file to the WEB-INF/lib/classes
113
File JavaDoc subDir = new File JavaDoc(moduleDir, "WEB-INF");
114                                 subDir = new File JavaDoc(subDir, "classes");
115                                 copyAutodeployedClassFile(request.getFileSource().getFile(), subDir);
116                             } else {
117                                 
118                                 /*
119                                  * Bug 4980750 - Specify the temporary directory into which to explode nested jars.
120                                  * This allows web services compilation to avoid including the nested jar file in the
121                                  * classpath. Their inclusion there was causing jar files to be opened but never
122                                  * closed (as a result of Introspection during web services compilation and, in turn,
123                                  * the use of sun.misc.URLClasspath which is where the unbalanced open actually occurs).
124                                  * With the nested jar's contents unjarred into this temporary directory, the temp.
125                                  * directory can be included in the web services compilation classpath rather than the
126                                  * jar file. The open jar file was causing subsequent redeployments of the same
127                                  * application to fail because on Windows the open jar file could not be overwritten.
128                                  */

129                 J2EEModuleExploder.explodeJar(request.getFileSource().getFile(), moduleDir);
130                             }
131                         }
132
133                     xmlDir.mkdirs();
134                     stubsDir.mkdirs();
135         }
136         catch(Exception JavaDoc e)
137         {
138             throw new IASDeploymentException(e);
139         }
140     }
141     
142     ///////////////////////////////////////////////////////////////////////////
143

144     protected void deploy() throws IASDeploymentException
145     {
146             // module dir where this module is exploded
147
String JavaDoc mLocation;
148             try {
149                 mLocation = moduleDir.getCanonicalPath();
150             } catch(java.io.IOException JavaDoc e) {
151                 throw new IASDeploymentException(e);
152             }
153             
154             // loads the deployment descriptors
155
Application app = loadDescriptors();
156             // Set the generated XML directory in application desc
157
request.getDescriptor().setGeneratedXMLDirectory(xmlDir.getAbsolutePath());
158         
159             WebBundleDescriptor bundleDesc = (WebBundleDescriptor) app.getStandaloneBundleDescriptor();
160
161             // The priority order of how the context root should be used:
162
// 1. Context root specified by user (via --contextRoot option
163
// or via gui), i.e. request.getContextRoot()
164
// 2. Context root specified in sun-web.xml i.e.
165
// bundleDesc.getContextRoot()
166
// 3. The default context root (archive name etc) i.e.
167
// req.getDefaultContextRoot()
168

169             if (request.getContextRoot() == null ||
170                 request.getContextRoot().trim().equals("")) {
171                 if (bundleDesc.getContextRoot() != null &&
172                     !bundleDesc.getContextRoot().trim().equals("")) {
173                     request.setContextRoot(bundleDesc.getContextRoot());
174                 } else {
175                     request.setContextRoot(request.getDefaultContextRoot());
176                 }
177             }
178
179             ZipItem[] clientStubs = runEJBC();
180
181         try
182         {
183             // 4919768 -- please remove this line after October, 2003
184
// bnevins 9-9-2003
185
// For some reason, a year ago the check was added to see if there are any
186
// jsp files by calling: bundleDesc.getJspDescriptors()).isEmpty().
187
// But it is an unneccessary restriction. Many stock web.xml files do not have
188
// the magic <jsp-file> tag -- but the web module contains jsp files. In this case
189
// the isEmpty() will return true and JSP precompilation will not occur until Tomcat
190
// gets to it later at runtime. The jsp compiler
191
// in such a case will find them and generate the servlet java files(s).
192
// So I removed the check. And I tested to make sure everything will work with
193
// (a) normal web modules where the jsp files are tagged in the xml
194
// (b) web modules that don't have the tags in web.xml but the module DOES have jsp files
195
// (c) web modules that have no jsp files
196

197             //old code: if(request.getPrecompileJSP() && !(bundleDesc.getJspDescriptors()).isEmpty())
198
if(request.getPrecompileJSP())
199             {
200                 long time = System.currentTimeMillis();
201                 JSPCompiler.compile(
202                                         moduleDir,
203                                         jspDir,
204                                         bundleDesc,
205                                         request.getCompleteClasspath());
206                 addJSPCTime(System.currentTimeMillis() - time);
207             }
208             // runs verifier if verifier option is ON
209
runVerifier();
210         }
211         catch(IASDeploymentException de)
212         {
213             throw de;
214         }
215         catch(Exception JavaDoc e)
216         {
217             throw new IASDeploymentException(e);
218         }
219     }
220     
221     ///////////////////////////////////////////////////////////////////////////
222

223     protected void preRedeploy() throws IASDeploymentException, ConfigException
224     {
225         assert moduleDir != null;
226         assert StringUtils.ok(moduleName);
227         
228         setOldDirs();
229
230         if(request.isArchive())
231         {
232             try
233             {
234                             // check if this is a .war file or a single .class file
235
if (request.getFileSource().getFile().getName().endsWith(".class")) {
236                                 // we just need to copy the file to the WEB-INF/lib/classes
237
File JavaDoc subDir = new File JavaDoc(moduleDir, "WEB-INF");
238                                 subDir = new File JavaDoc(subDir, "classes");
239                                 copyAutodeployedClassFile(request.getFileSource().getFile(), subDir);
240                                 
241                             } else {
242                                                             
243                             
244                                 /*
245                                  *Please see the discussion in the parallel place in preDeploy regarding bug 4980750.
246                                  */

247                 J2EEModuleExploder.explodeJar(request.getFileSource().getFile(), moduleDir);
248                             }
249                         }
250             catch(IOException JavaDoc e)
251             {
252                 throw new IASDeploymentException(e.toString(), e);
253             }
254         }
255
256             xmlDir.mkdirs();
257             stubsDir.mkdirs();
258     }
259     
260         
261     protected boolean needsStubs()
262     {
263         // override this for webservices stubs/ties
264
return true;
265         }
266
267         ///////////////////////////////////////////////////////////////////////////
268

269     protected boolean needsJSPs()
270     {
271         // override this for any module that works with generated JSPs
272
return true;
273     }
274     
275
276     ///////////////////////////////////////////////////////////////////////////
277

278    protected void generatePolicy() throws IASDeploymentException
279     {
280         // if this is a part of an application then AppDeployerBase will be
281
// called.
282
// The rolemapper is not created for standalone web components
283

284         // Load the WebArchivist, so that the rolemapper is instantiated.
285
try{
286             if(webModulesMgr == null){
287                 webModulesMgr =
288                     (WebModulesManager)createConfigManager(getInstanceEnv(),
289                                                            moduleEnv);
290             }
291         Application app = request.getDescriptor();
292             WebBundleDescriptor wbd = (WebBundleDescriptor) app.getStandaloneBundleDescriptor();
293
294             // other things like creating the WebSecurityManager
295
WebSecurityManagerFactory wsmf = null;
296             wsmf = WebSecurityManagerFactory.getInstance();
297             // this should create all permissions
298
wsmf.newWebSecurityManager(wbd);
299             // for an application the securityRoleMapper should already be created.
300
// I am just creating the web permissions and handing it to the security
301
// component.
302
if(request.isApplication()){
303                 return;
304             }
305             String JavaDoc name = WebSecurityManager.getContextID(wbd) ;
306             SecurityUtil.generatePolicyFile(name);
307         }catch(IASDeploymentException iasde){ // what exception to propagate ahead
308
// there will be problems in accessing the application
309
String JavaDoc msg =
310                 localStrings.getString("enterprise.deployment.backend.generate_policy_error" );
311             logger.log(Level.WARNING, msg, iasde);
312             throw iasde;
313         }catch(ConfigException ce){
314             // there will be problems in accessing the application
315
String JavaDoc msg =
316                 localStrings.getString("enterprise.deployment.backend.generate_policy_error" );
317             logger.log(Level.WARNING, msg, ce);
318             throw new IASDeploymentException(ce.toString());
319         } catch(IASSecurityException iassec){
320             // log this
321
String JavaDoc msg =
322                 localStrings.getString("enterprise.deployment.backend.generate_policy_error" );
323             logger.log(Level.WARNING, msg, iassec);
324             throw new IASDeploymentException(msg, iassec);
325         }
326
327     }
328
329     ///////////////////////////////////////////////////////////////////////////
330
public void removePolicy() throws IASDeploymentException
331     {
332         if (request.isApplication()) {
333             return;
334         }
335         WebSecurityManagerFactory wsmf = WebSecurityManagerFactory.getInstance();
336         String JavaDoc requestName = request.getName();
337         String JavaDoc[] name
338          = wsmf.getAndRemoveContextIdForWebAppName(requestName);
339         try {
340             if(name != null){
341                 if(name[0] != null)
342                     SecurityUtil.removePolicy(name[0]);
343             }
344         } catch(IASSecurityException ex) {
345             String JavaDoc msg = localStrings.getString(
346             "enterprise.deployment.backend.remove_policy_error",name);
347             logger.log(Level.WARNING, msg, ex);
348             throw new IASDeploymentException(msg, ex);
349         }
350     }
351
352     ///////////////////////////////////////////////////////////////////////////
353

354
355     private WebModulesManager webModulesMgr = null;
356     private static StringManager localStrings =
357         StringManager.getManager( WebModuleDeployer.class );
358 }
359
360
Popular Tags