KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > deployapi > DeploymentFactoryInstaller


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.deployapi;
25
26 import java.io.*;
27 import java.util.jar.*;
28 import java.net.URL JavaDoc;
29 import java.net.URLClassLoader JavaDoc;
30 import java.util.logging.Logger JavaDoc;
31 import java.util.logging.Level JavaDoc;
32
33 import javax.enterprise.deploy.shared.factories.DeploymentFactoryManager JavaDoc;
34 import javax.enterprise.deploy.spi.factories.DeploymentFactory JavaDoc;
35
36 import com.sun.enterprise.deployment.util.DOLUtils;
37 import com.sun.enterprise.util.shared.ArchivistUtils;
38
39 /**
40  * This singleton object is responsible to resolving all the
41  * DeploymentManagerFactory installed in the RI and register
42  * them to the DeploymentManagerFactory
43  *
44  * @author Jerome Dochez
45  */

46 public class DeploymentFactoryInstaller {
47     
48     private static DeploymentFactoryInstaller dfInstaller = null;
49     
50     private final String JavaDoc J2EE_DEPLOYMENT_MANAGER_REPOSITORY = "lib" + File.separator + "deployment";
51     private final String JavaDoc J2EE_DEPLOYMENT_MANAGER = "J2EE-DeploymentFactory-Implementation-Class";
52     private final String JavaDoc J2EE_HOME = "com.sun.enterprise.home";
53     
54     /** Creates a single instance of DeploymentManagerFactoryResolver */
55     private DeploymentFactoryInstaller() {
56     }
57     
58     public static DeploymentFactoryInstaller getInstaller() {
59     
60         if (dfInstaller==null) {
61             dfInstaller = new DeploymentFactoryInstaller();
62             dfInstaller.initialize();
63         }
64         return dfInstaller;
65     }
66     
67     /**
68      * @return a list of installed deployment manager
69      * implementation archives
70      */

71     public File[] getListOfDeploymentFactoryFiles() {
72         
73         File repository = new File(System.getProperty("com.sun.aas.installRoot")+File.separator+
74             J2EE_DEPLOYMENT_MANAGER_REPOSITORY);
75         
76         if (DOLUtils.getDefaultLogger().isLoggable(Level.FINE)) {
77             DOLUtils.getDefaultLogger().fine("J2EE Deployment factory repository = "
78                     + repository.getAbsolutePath());
79         }
80         if (!repository.exists()) {
81             DOLUtils.getDefaultLogger().log(Level.SEVERE, "enterprise.deployment.backend.deplyomentManagerLoadFailure",
82                 new Object JavaDoc[] {"Cannot find any deployment manager"});
83             return null;
84         }
85         
86         return repository.listFiles();
87     }
88     
89     /**
90      * Add a new deployment manager to our respository
91      */

92     public void addDeploymentFactory(File newDM) throws IOException {
93         
94         int number=1;
95         // copy to the right location...
96
File repository = new File(System.getProperty(J2EE_HOME)+File.separator+
97             J2EE_DEPLOYMENT_MANAGER_REPOSITORY);
98         File to = new File(repository, newDM.getName());
99         while (to.exists()) {
100             to = new File(repository, newDM.getName()+number);
101             number++;
102         }
103         ArchivistUtils.copy(
104             new BufferedInputStream(new FileInputStream(newDM)),
105             new BufferedOutputStream(new FileOutputStream(to)));
106         
107         installDeploymentFactory(to);
108     
109     }
110     
111     
112     protected void installDeploymentFactory(File installedDM) throws IOException {
113         
114         if (DOLUtils.getDefaultLogger().isLoggable(Level.FINE)) {
115             DOLUtils.getDefaultLogger().fine("Installing Deployment factory = "
116                     + installedDM.getAbsolutePath());
117         }
118         
119         // let's check first that we indeed have a valid
120
// deployment manager implementation
121

122         /*
123          *Declare the JarFile and Manifest but populate them inside the first try block. This way the
124          *jar file can be closed right away to conserve resources.
125          */

126         JarFile jarFile = null;
127         Manifest m = null;
128         try {
129             jarFile = new JarFile(installedDM);
130             m = jarFile.getManifest();
131         } finally {
132             /*
133              *The jarFile.close can throw IOException, but this method also throws that exception so there is no
134              *need to catch it and wrap it here in the finally clause.
135              */

136             jarFile.close();
137             jarFile = null;
138         }
139         String JavaDoc className = m.getMainAttributes().getValue(J2EE_DEPLOYMENT_MANAGER);
140         URL JavaDoc[] urls = new URL JavaDoc[]{installedDM.toURI().toURL()};
141         URLClassLoader JavaDoc urlClassLoader = new java.net.URLClassLoader JavaDoc(urls, getClass().getClassLoader());
142         Class JavaDoc factory = null;
143         try {
144             factory=urlClassLoader.loadClass(className);
145         } catch (ClassNotFoundException JavaDoc cnfe) {
146             DOLUtils.getDefaultLogger().log(Level.SEVERE, "enterprise.deployment.backend.deplyomentManagerLoadFailure",
147                 new Object JavaDoc[] {"Unable to load declared DeploymentManagerFactory"});
148             throw new IllegalArgumentException JavaDoc(className + " is not present in the " + installedDM.getName());
149         }
150         
151         // Ok we have the class, let's instanciate it, check it and
152
// if everything is fine, register it to the DeploymentFactoryManager
153
Object JavaDoc df = null;
154         try {
155             df = factory.newInstance();
156         } catch (Exception JavaDoc ie) {
157             DOLUtils.getDefaultLogger().log(Level.SEVERE, "enterprise.deployment.backend.deplyomentManagerLoadFailure",
158                     new Object JavaDoc[]{className});
159             ie.printStackTrace();
160             throw new IllegalArgumentException JavaDoc("Cannot install " + installedDM.getName());
161         }
162         if (df instanceof DeploymentFactory JavaDoc) {
163             DeploymentFactoryManager.getInstance().registerDeploymentFactory((DeploymentFactory JavaDoc) df);
164         } else {
165             throw new IllegalArgumentException JavaDoc("The " + className +
166                 " declared as a DeploymentFactory does implement the DeploymentFactory interface");
167         }
168     }
169     
170     protected void initialize() {
171                 
172         File[] elligibleFiles = getListOfDeploymentFactoryFiles();
173         if (elligibleFiles==null) {
174             return;
175         }
176            
177         for (int i=0;i<elligibleFiles.length;i++) {
178             try {
179                 installDeploymentFactory(elligibleFiles[i]);
180             } catch(Exception JavaDoc ioe) {
181                 ioe.printStackTrace();
182                 DOLUtils.getDefaultLogger().log(Level.SEVERE, "enterprise.deployment.backend.deplyomentManagerLoadFailure",
183                     new Object JavaDoc[] {elligibleFiles[i].getName()});
184             }
185         }
186     }
187 }
188
Popular Tags