KickJava   Java API By Example, From Geeks To Geeks.

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


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  * AppDeployerBase.java
26  *
27  * Created on April 26, 2002, 5:02 PM
28  *
29  * @author bnevins
30  * <BR> <I>$Source: /cvs/glassfish/appserv-core/src/java/com/sun/enterprise/deployment/backend/AppDeployerBase.java,v $
31  *
32  */

33
34 package com.sun.enterprise.deployment.backend;
35
36 import java.io.*;
37 import java.util.Properties JavaDoc;
38 import java.util.Vector JavaDoc;
39 import java.util.Iterator JavaDoc;
40 import java.util.ArrayList JavaDoc;
41 import java.util.List JavaDoc;
42 import java.util.Set JavaDoc;
43 import javax.enterprise.deploy.shared.ModuleType JavaDoc;
44 import java.util.logging.Level JavaDoc;
45
46 import com.sun.enterprise.instance.ApplicationEnvironment;
47 import com.sun.enterprise.instance.AppsManager;
48 import com.sun.enterprise.instance.BaseManager;
49 import com.sun.enterprise.util.StringUtils;
50 import com.sun.enterprise.util.io.FileUtils;
51 import com.sun.enterprise.config.ConfigException;
52 import com.sun.enterprise.util.i18n.StringManager;
53 import com.sun.enterprise.security.SecurityUtil;
54 import com.sun.enterprise.deployment.Application;
55 import com.sun.enterprise.deployment.EjbBundleDescriptor;
56 import com.sun.enterprise.deployment.archivist.Archivist;
57 import com.sun.enterprise.deployment.deploy.shared.AbstractArchive;
58 import com.sun.enterprise.deployment.util.ModuleDescriptor;
59 import com.sun.enterprise.deployment.phasing.DeploymentServiceUtils;
60 import com.sun.enterprise.server.Constants;
61 import com.sun.enterprise.security.util.IASSecurityException;
62 import com.sun.enterprise.security.application.EJBSecurityManager;
63 import com.sun.enterprise.security.factory.EJBSecurityManagerFactory;
64 import com.sun.enterprise.loader.EJBClassPathUtils;
65 import com.sun.web.security.WebSecurityManager;
66 import com.sun.enterprise.deployment.WebBundleDescriptor;
67 import com.sun.enterprise.deployment.BundleDescriptor;
68 import com.sun.enterprise.deployment.WebServiceEndpoint;
69 import com.sun.enterprise.deployment.WebServicesDescriptor;
70 import com.sun.enterprise.deployment.WebService;
71 import javax.security.jacc.PolicyConfiguration JavaDoc;
72 import javax.security.jacc.PolicyConfigurationFactory JavaDoc;
73 import javax.security.jacc.PolicyContextException JavaDoc;
74 import com.sun.web.security.WebSecurityManagerFactory;
75 /** Abstract base class for AppDeployer, AppRedeployer, AppUndeployer.
76  */

77
78 abstract class AppDeployerBase extends Deployer
79 {
80     AppDeployerBase(DeploymentRequest r) throws IASDeploymentException
81     {
82         super(r);
83     }
84     ///////////////////////////////////////////////////////////////////////////
85
////// Abstract Methods
86
///////////////////////////////////////////////////////////////////////////
87
abstract protected File setAppDir() throws IASDeploymentException;
88
89     ///////////////////////////////////////////////////////////////////////////
90
////// Overridable Protected Methods
91
///////////////////////////////////////////////////////////////////////////
92

93     /** Before even attempting the deployment operation -- check and verify and set lots of
94      * useful variables and references.
95      */

96     protected void begin() throws IASDeploymentException
97     {
98         super.begin();
99         
100         try
101         {
102             appEnv = request.getAppEnv();
103
104             if(appEnv == null) {
105                 String JavaDoc msg = localStrings.getString(
106                     "enterprise.deployment.backend.null_applicationenvironment_object");
107                 throw new IASDeploymentException( msg );
108             }
109
110             appMgr = new AppsManager(getInstanceEnv());
111             appName = request.getName();
112             
113             if(!StringUtils.ok(appName)) {
114                 String JavaDoc msg = localStrings.getString(
115                         "enterprise.deployment.backend.null_appname" );
116
117                 throw new IASDeploymentException( msg );
118             }
119             
120             isReg = DeploymentServiceUtils.isRegistered(
121                             getAppName(), request.getType());
122             if(isReg) {
123                 isSystem = isSystem();
124             }
125             verify();
126         }
127         catch(IASDeploymentException e)
128         {
129             throw e;
130         }
131         catch(Exception JavaDoc e)
132         {
133             throw new IASDeploymentException(e);
134         }
135         
136     }
137     
138     ///////////////////////////////////////////////////////////////////////////
139

140     /** Before even attempting the deployment operation -- check and verify and set lots of
141      * useful variables and references.
142      */

143     protected void predeploy() throws IASDeploymentException
144     {
145         try
146         {
147             appDir = setAppDir();
148             request.setDeployedDirectory(appDir);
149             setGeneratedDirs();
150         }
151         catch(IASDeploymentException e)
152         {
153             throw e;
154         }
155         catch(Exception JavaDoc e)
156         {
157             throw new IASDeploymentException(e);
158         }
159     }
160     
161     ///////////////////////////////////////////////////////////////////////////
162

163     protected final void verify() throws IASDeploymentException
164     {
165         if(!request.isApplication()) {
166             String JavaDoc msg = localStrings.getString(
167             "enterprise.deployment.backend.attempt_to_deploy_non_application");
168             throw new IASDeploymentException( msg );
169         }
170             
171         if(request.isUnDeploy())
172         {
173             if(!isReg) {
174                 String JavaDoc msg = localStrings.getString(
175                 "enterprise.deployment.backend.undeploy_error_application_not_registered");
176                 throw new IASDeploymentException( msg );
177             }else if(isSystem)
178             {
179                 String JavaDoc msg = localStrings.getString(
180                 "enterprise.deployment.backend.undeploy_error_application_is_a_system_resource");
181                 throw new IASDeploymentException( msg );
182             }
183         }
184
185         else if(request.isDeploy())
186         {
187             if(isReg)
188             {
189                 String JavaDoc msg = localStrings.getString(
190                 "enterprise.deployment.backend.deploy_error_application_exists");
191                 throw new IASDeploymentException( msg );
192             }
193
194             // isReg is false. This means that it isn't registered as an App.
195
// But we might be clashing with a registered module of a different flavor.
196
// E.g. there may be a web module already deployed with the same name.
197
// this will throw an IASDeploymentException if it is registered to another type...
198

199             checkRegisteredAnywhereElse(appName);
200         }
201         else if(request.isReDeploy())
202         {
203             if(!isReg)
204             {
205                 String JavaDoc msg = localStrings.getString(
206                 "enterprise.deployment.backend.redeploy_error_application_does_not_exist");
207                 throw new IASDeploymentException( msg );
208             }else if(isSystem)
209             {
210                 String JavaDoc msg = localStrings.getString(
211                 "enterprise.deployment.backend.redeploy_error_application_is_a_system_resource");
212                 throw new IASDeploymentException( msg );
213             }
214         }
215     }
216
217     ///////////////////////////////////////////////////////////////////////////
218
////// Private Methods
219
///////////////////////////////////////////////////////////////////////////
220
private final void setGeneratedDirs() throws IASDeploymentException
221     {
222         try
223         {
224             stubsDir = new File(getAppEnv().getAppStubPath());
225             jspDir = new File(getAppEnv().getAppJSPPath());
226             xmlDir = new File(getAppEnv().getAppGeneratedXMLPath());
227             jwsDir = new File(getAppEnv().getJavaWebStartPath());
228             request.setJSPDirectory(jspDir);
229             request.setStubsDirectory(stubsDir);
230             request.setGeneratedXMLDirectory(xmlDir);
231         }
232         catch(Exception JavaDoc e)
233         {
234             String JavaDoc msg = localStrings.getString(
235                 "enterprise.deployment.backend.error_getting_generated_dirs",
236                 e );
237             throw new IASDeploymentException( msg );
238         }
239     }
240     ///////////////////////////////////////////////////////////////////////////
241
////// Access Methods -- note that the compiler should inline them ///
242
///////////////////////////////////////////////////////////////////////////
243
protected final ApplicationEnvironment getAppEnv()
244     {
245         return appEnv;
246     }
247     
248     ///////////////////////////////////////////////////////////////////////////
249

250     protected BaseManager getManager()
251     {
252         return appMgr;
253     }
254     
255     ///////////////////////////////////////////////////////////////////////////
256

257     protected final String JavaDoc getAppName()
258     {
259         return appName;
260     }
261     
262     ///////////////////////////////////////////////////////////////////////////
263

264     protected final File getStubsDir()
265     {
266         return stubsDir;
267     }
268     
269     ///////////////////////////////////////////////////////////////////////////
270

271     protected final File getJSPDir()
272     {
273         return jspDir;
274     }
275
276        ///////////////////////////////////////////////////////////////////////////
277

278         protected final File getXMLDir()
279         {
280                 return xmlDir;
281         }
282
283        ///////////////////////////////////////////////////////////////////////////
284

285         protected final File getJWSDir()
286         {
287                 return jwsDir;
288         }
289
290         
291     ///////////////////////////////////////////////////////////////////////////
292

293     protected final File getAppDir()
294     {
295         return appDir;
296     }
297     
298     
299     protected final File getModuleDir() {
300         return appDir;
301     }
302     
303         /**
304      * @return the module classpath
305      */

306     protected List JavaDoc getModuleClasspath(Archivist archivist,
307                 AbstractArchive archive) throws IASDeploymentException
308     {
309         try {
310                 Application application = request.getDescriptor();
311                 if (application==null) {
312                     application = (Application)archivist.readStandardDeploymentDescriptor(archive);
313                     application.setRegistrationName(request.getName());
314                 }
315             return EJBClassPathUtils.getAppClassPath(
316                     application, request.getDeployedDirectory().getAbsolutePath(), getManager());
317         } catch(Exception JavaDoc e) {
318         throw new IASDeploymentException(e);
319         }
320     }
321
322     /**
323      * @return a fully initialized and validated deployment descriptors for this
324      * deployment request.
325      */

326     protected Application loadDescriptors() throws IASDeploymentException {
327             Application app = super.loadDescriptors();
328             (new com.sun.enterprise.webservice.WsUtil()).genWSInfo(app, request);
329             return app;
330         }
331         
332     ///////////////////////////////////////////////////////////////////////////
333

334     protected final boolean isSystem() throws IASDeploymentException
335     {
336         try{
337             boolean isSystem = DeploymentServiceUtils.isSystem(getAppName(), request.getType());
338             boolean guardSystemApp = !(Boolean.valueOf(System.getProperty(Constants.ALLOW_SYSAPP_DEPLOYMENT, "false")).booleanValue());
339             if(isSystem && guardSystemApp)
340                 return true;
341             else
342                 return false;
343         }catch(Exception JavaDoc e) {
344             throw new IASDeploymentException(e);
345         }
346     }
347         
348
349     ///////////////////////////////////////////////////////////////////////////
350

351        protected void generatePolicy() throws IASDeploymentException {
352        try{
353                // generate policy for all web modules with
354
// moduleName + contextRoot and then generate for ejbs
355
// with Appname
356
Application applicationDD = request.getDescriptor();
357                                                        
358                 // link with the ejb name
359
String JavaDoc linkName = null;
360                 boolean lastInService = false;
361                 for (Iterator JavaDoc iter = applicationDD.getWebBundleDescriptors().iterator();
362                         iter.hasNext();){
363                     String JavaDoc name
364                         = WebSecurityManager.getContextID((WebBundleDescriptor)iter.next());
365                     lastInService = SecurityUtil.linkPolicyFile(name, linkName, lastInService);
366                     linkName = name;
367         }
368                 for (Iterator JavaDoc iter = applicationDD.getEjbBundleDescriptors().iterator(); iter.hasNext();) {
369                     String JavaDoc name =
370                         EJBSecurityManager.getContextID((EjbBundleDescriptor)iter.next());
371                     lastInService = SecurityUtil.linkPolicyFile(name, linkName, lastInService);
372                     linkName = name;
373                 }
374                 // generate policies
375
for (Iterator JavaDoc iter = applicationDD.getWebBundleDescriptors().iterator();
376                         iter.hasNext();){
377                     String JavaDoc name
378                         = WebSecurityManager.getContextID((WebBundleDescriptor)iter.next());
379                     SecurityUtil.generatePolicyFile(name);
380         }
381                 for (Iterator JavaDoc iter = applicationDD.getEjbBundleDescriptors().iterator(); iter.hasNext();) {
382                     String JavaDoc name =
383                         EJBSecurityManager.getContextID((EjbBundleDescriptor)iter.next());
384                     SecurityUtil.generatePolicyFile(name);
385                 }
386          } catch(IASSecurityException se){
387          // log this
388
String JavaDoc msg =
389          localStrings.getString("enterprise.deployment.backend.generate_policy_error", request.getName());
390          throw new IASDeploymentException(msg, se);
391      }
392        }
393
394     public void removePolicy() throws IASDeploymentException
395     {
396             String JavaDoc name = request.getName();
397             
398             try {
399                 String JavaDoc[] webcontexts
400                  = WebSecurityManagerFactory.getInstance().getAndRemoveContextIdForWebAppName(name);
401         if(webcontexts !=null){
402                     for(int i=0; i<webcontexts.length; i++){
403                if(webcontexts[i] != null){
404                             SecurityUtil.removePolicy(webcontexts[i]);
405                }
406                     }
407         }
408
409                 // removing ejb policy
410
String JavaDoc[] ejbContextIds
411                     = ((EJBSecurityManagerFactory)EJBSecurityManagerFactory.getInstance()).getAndRemoveContextIdForEjbAppName(name);
412                 if (ejbContextIds != null) {
413                     for (String JavaDoc ejbContextId : ejbContextIds) {
414                         if (ejbContextId != null) {
415                             SecurityUtil.removePolicy(ejbContextId);
416                         }
417                     }
418                 }
419
420                 //remove any remaining policy
421
//This is to address the bug where the CONTEXT_ID in
422
//WebSecurityManagerFactory is not properly populated.
423
//We force the sub-modules to be removed in this case.
424
//This should not impact undeploy performance on DAS.
425
//This needs to be fixed better later.
426
String JavaDoc policyRootDir = System.getProperty(
427                     "com.sun.enterprise.jaccprovider.property.repository");
428                 if (policyRootDir != null) {
429                     List JavaDoc<String JavaDoc> contextIds = new ArrayList JavaDoc<String JavaDoc>();
430                     File policyDir = new File(
431                         policyRootDir + File.separator + name);
432                     if (policyDir.exists()) {
433                         File[] policies = policyDir.listFiles();
434                         for (int i = 0; i < policies.length; i++) {
435                             if (policies[i].isDirectory()) {
436                                 contextIds.add(name + '/' + policies[i].getName());
437                             }
438                         }
439                     } else {
440                         //we tried. give up now.
441
}
442
443                     if (contextIds.size() > 0) {
444                         for (String JavaDoc cId : contextIds) {
445                             SecurityUtil.removePolicy(cId);
446                         }
447                     }
448                 }
449
450             } catch(IASSecurityException ex) {
451                 String JavaDoc msg = localStrings.getString(
452                 "enterprise.deployment.backend.remove_policy_error", name);
453                 logger.log(Level.WARNING, msg, ex);
454                 throw new IASDeploymentException(msg, ex);
455             }
456     }
457
458
459     private ApplicationEnvironment appEnv;
460     private AppsManager appMgr;
461     private String JavaDoc appName;
462     private boolean isReg;
463     private boolean isSystem = false;
464     private File stubsDir;
465     private File jspDir;
466     private File xmlDir;
467     private File jwsDir;
468     private File appDir;
469     private static StringManager localStrings =
470         StringManager.getManager( AppDeployerBase.class );
471 }
472
473
474
475
Popular Tags