KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > easybeans > deployer > Deployer


1 /**
2  * EasyBeans
3  * Copyright (C) 2006 Bull S.A.S.
4  * Contact: easybeans@objectweb.org
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19  * USA
20  *
21  * --------------------------------------------------------------------------
22  * $Id: Deployer.java 1026 2006-08-04 14:54:10Z benoitf $
23  * --------------------------------------------------------------------------
24  */

25
26 package org.objectweb.easybeans.deployer;
27
28 import static org.objectweb.easybeans.util.url.URLUtils.fileToURL2;
29
30 import java.io.File JavaDoc;
31 import java.io.FileNotFoundException JavaDoc;
32 import java.io.FileOutputStream JavaDoc;
33 import java.io.IOException JavaDoc;
34 import java.net.URL JavaDoc;
35 import java.util.ArrayList JavaDoc;
36 import java.util.List JavaDoc;
37 import java.util.jar.JarFile JavaDoc;
38
39 import javax.enterprise.deploy.shared.ModuleType JavaDoc;
40
41 import org.objectweb.easybeans.api.EZBArchive;
42 import org.objectweb.easybeans.api.EZBContainer;
43 import org.objectweb.easybeans.api.EZBContainerException;
44 import org.objectweb.easybeans.container.archive.ArchiveManager;
45 import org.objectweb.easybeans.loader.EasyBeansClassLoader;
46 import org.objectweb.easybeans.log.JLog;
47 import org.objectweb.easybeans.log.JLogFactory;
48 import org.objectweb.easybeans.persistence.PersistenceUnitManager;
49 import org.objectweb.easybeans.persistence.xml.PersistenceXmlFileAnalyzer;
50 import org.objectweb.easybeans.persistence.xml.PersistenceXmlFileAnalyzerException;
51 import org.objectweb.easybeans.server.Embedded;
52 import org.objectweb.easybeans.util.files.FileUtils;
53 import org.objectweb.easybeans.util.files.FileUtilsException;
54 import org.objectweb.easybeans.util.url.URLUtilsException;
55
56 /**
57  * Deployer allowing deploy/undeploy functions on this container.
58  * @author Florent Benoit
59  */

60 public final class Deployer implements IDeployer {
61
62     /**
63      * Embedded server linked to this deployer.
64      */

65     private Embedded embedded = null;
66
67     /**
68      * Logger.
69      */

70     private static JLog logger = JLogFactory.getLog(Deployer.class);
71
72     /**
73      * Folder to create in tmp folder.
74      */

75     private static final String JavaDoc DEFAULT_FOLDER = "EasyBeans-Deployer";
76
77     /**
78      * Build a Deployer for the given Embedded instance.
79      * @param embedded the Server instance.
80      */

81     public Deployer(final Embedded embedded) {
82         this.embedded = embedded;
83     }
84
85     /**
86      * Deploy a file on the EJB3 container.
87      * @param typeparam type of the file (EJB, WAR, EAR, RAR, CAR) (ModuleType
88      * of JSR88)
89      * @param bfile bytes array of the file
90      * @param filename basename of the file to be deployed
91      * @return the path of the deployed file
92      */

93     public String JavaDoc deployFile(final Integer JavaDoc typeparam, final java.lang.Byte JavaDoc[] bfile, final String JavaDoc filename) {
94         logger.info("Trying to deploy file with param {0}, filename = {1}", typeparam, filename);
95
96         int type = typeparam.intValue();
97
98         String JavaDoc returnedPath = null;
99
100         File JavaDoc rootFolder = new File JavaDoc(System.getProperty("java.io.tmpdir") + File.separator + DEFAULT_FOLDER);
101         rootFolder.mkdirs();
102
103         // Create file in tmp folder
104
File JavaDoc tmpFolder = new File JavaDoc(rootFolder, "TMP");
105         tmpFolder.mkdirs();
106         File JavaDoc file = new File JavaDoc(tmpFolder, filename);
107
108         FileOutputStream JavaDoc out = null;
109         try {
110             out = new FileOutputStream JavaDoc(file);
111         } catch (FileNotFoundException JavaDoc e) {
112             throw new IllegalStateException JavaDoc("Cannot build an outputstream on file '" + file + "'., e");
113         }
114         byte[] bfileused = new byte[bfile.length];
115         for (int i = 0; i < bfile.length; i++) {
116             bfileused[i] = bfile[i].byteValue();
117         }
118         try {
119             out.write(bfileused);
120         } catch (IOException JavaDoc e) {
121             throw new IllegalStateException JavaDoc("Cannot write byte in outputstream", e);
122         }
123         try {
124             out.close();
125         } catch (IOException JavaDoc e) {
126             throw new IllegalStateException JavaDoc("Cannot close outpustream", e);
127         }
128
129         if (type == ModuleType.EAR.getValue()) {
130             JarFile JavaDoc jarFile = null;
131             try {
132                 jarFile = new JarFile JavaDoc(file);
133             } catch (IOException JavaDoc e) {
134                 throw new IllegalStateException JavaDoc("Cannot build a Jar file on file '" + file + "'.", e);
135             }
136             File JavaDoc appsFolder = new File JavaDoc(rootFolder, "APPS");
137             appsFolder.mkdirs();
138             File JavaDoc dest = new File JavaDoc(appsFolder, filename);
139             try {
140                 FileUtils.unpack(jarFile, dest);
141             } catch (FileUtilsException e) {
142                 throw new IllegalStateException JavaDoc("Cannot unpack file '" + jarFile + "'.", e);
143             }
144             logger.info("Unpack of ear {0} in folder {1}", file, dest);
145             try {
146                 jarFile.close();
147             } catch (IOException JavaDoc e) {
148                 throw new IllegalStateException JavaDoc("Cannot close jarFile '" + jarFile + "'.", e);
149             }
150             returnedPath = dest.getAbsolutePath();
151         }
152
153         if (returnedPath == null) {
154             throw new IllegalStateException JavaDoc("Returned path should not be null");
155         }
156         logger.info("Returning path = {0}", returnedPath);
157         return returnedPath;
158     }
159
160     /**
161      * Deploy an application packaged in a EAR file.
162      * @param fileName the name of the file to deploy
163      */

164     public void deployEar(final String JavaDoc fileName) {
165         logger.info("Deploying ear {0}", fileName);
166
167         // Try to find ejb-jar.
168
File JavaDoc earFile = new File JavaDoc(fileName);
169         if (!earFile.isDirectory()) {
170             throw new IllegalArgumentException JavaDoc("Handle only EAR directories");
171         }
172
173         // Init libraries list.
174
List JavaDoc<File JavaDoc> libraries = new ArrayList JavaDoc<File JavaDoc>();
175
176         // Search files of the directory (to find an ejb-jar)
177
List JavaDoc<File JavaDoc> ejbFiles = new ArrayList JavaDoc<File JavaDoc>();
178         File JavaDoc[] files = earFile.listFiles();
179         if (files != null) {
180             for (File JavaDoc f : files) {
181                 if (f.getName().toLowerCase().endsWith("_ejb.jar")) {
182                     ejbFiles.add(f);
183                 }
184
185                 // lib folder in the ear ?
186
if (f.isDirectory() && f.getName().equals("lib")) {
187                     // Add all jars found in this directory.
188
File JavaDoc[] libs = f.listFiles();
189                     if (libs != null) {
190                         for (File JavaDoc lib : libs) {
191                         libraries.add(lib);
192                         }
193                     }
194                 }
195             }
196         }
197
198         // build common classloader with EJB and libraries
199
URL JavaDoc[] urls = new URL JavaDoc[libraries.size() + ejbFiles.size()];
200         int u = 0;
201         for (File JavaDoc library : libraries) {
202             try {
203                 urls[u++] = fileToURL2(library);
204             } catch (URLUtilsException e) {
205                 throw new IllegalStateException JavaDoc("Cannot transform file '" + library + "' into an URL.", e);
206             }
207         }
208         for (File JavaDoc ejb : ejbFiles) {
209             try {
210                 urls[u++] = fileToURL2(ejb);
211             } catch (URLUtilsException e) {
212                 throw new IllegalStateException JavaDoc("Cannot transform file '" + ejb + "' into an URL.", e);
213             }
214         }
215         ClassLoader JavaDoc appClassLoader = new EasyBeansClassLoader(urls, Thread.currentThread().getContextClassLoader());
216
217         // Analyze libraries to detect persistence archive (only once for now and for all libraries)
218
PersistenceUnitManager persistenceUnitManager = null;
219         for (File JavaDoc library : libraries) {
220             PersistenceUnitManager builtPersistenceUnitManager = null;
221             try {
222                 EZBArchive archive = ArchiveManager.getInstance().getArchive(library);
223                 builtPersistenceUnitManager = PersistenceXmlFileAnalyzer.analyzePersistenceXmlFile(archive, appClassLoader);
224             } catch (PersistenceXmlFileAnalyzerException e) {
225                 throw new IllegalStateException JavaDoc("Failure when analyzing the persistence.xml file", e);
226             }
227             if (persistenceUnitManager != null) {
228                 throw new IllegalStateException JavaDoc("There was an existing persistence.xml in another library of this EAR."
229                         + " This is not supported.");
230             }
231             persistenceUnitManager = builtPersistenceUnitManager;
232         }
233
234
235         if (ejbFiles.size() == 0) {
236             throw new IllegalStateException JavaDoc("No EJB-JAR file was found in the ear file.");
237         }
238
239         // Create containers
240
List JavaDoc<EZBContainer> containers = new ArrayList JavaDoc<EZBContainer>();
241         for (File JavaDoc ejbFile : ejbFiles) {
242             containers.add(createContainer(ejbFile.getAbsolutePath()));
243         }
244
245         // Configure containers
246
for (EZBContainer container : containers) {
247             // Set the classloader that needs to be used
248
container.setClassLoader(appClassLoader);
249             // Add persistence context found
250
container.setPersistenceUnitManager(persistenceUnitManager);
251         }
252
253
254
255         // Start containers
256
for (EZBContainer container : containers) {
257             try {
258                 container.start();
259             } catch (EZBContainerException e) {
260                 logger.error("Cannot start container {0}", container.getName(), e);
261             }
262         }
263
264     }
265
266     /**
267      * Deploy an EJB packaged in a JAR file.
268      * @param fileName the name of the file to deploy
269      */

270     public void deployEjbJar(final String JavaDoc fileName) {
271         logger.info("Deploying EJB JAR {0}", fileName);
272         EZBContainer container = createContainer(fileName);
273         try {
274             container.start();
275         } catch (EZBContainerException e) {
276             logger.error("Cannot start container {0}", container.getName(), e);
277         }
278     }
279
280     /**
281      * Create a new container for the given fileName.
282      * @param fileName the name of the file for which a container needs to be created.
283      * @return an instance of a container.
284      */

285     private EZBContainer createContainer(final String JavaDoc fileName) {
286         File JavaDoc f = new File JavaDoc(fileName);
287         EZBArchive archive = ArchiveManager.getInstance().getArchive(f);
288         return embedded.createContainer(archive);
289     }
290
291 }
292
Popular Tags