KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openejb > alt > config > EjbJarUtils


1 /**
2  * Redistribution and use of this software and associated documentation
3  * ("Software"), with or without modification, are permitted provided
4  * that the following conditions are met:
5  *
6  * 1. Redistributions of source code must retain copyright
7  * statements and notices. Redistributions must also contain a
8  * copy of this document.
9  *
10  * 2. Redistributions in binary form must reproduce the
11  * above copyright notice, this list of conditions and the
12  * following disclaimer in the documentation and/or other
13  * materials provided with the distribution.
14  *
15  * 3. The name "OpenEJB" must not be used to endorse or promote
16  * products derived from this Software without prior written
17  * permission of The OpenEJB Group. For written permission,
18  * please contact dev@openejb.org.
19  *
20  * 4. Products derived from this Software may not be called "OpenEJB"
21  * nor may "OpenEJB" appear in their names without prior written
22  * permission of The OpenEJB Group. OpenEJB is a registered
23  * trademark of The OpenEJB Group.
24  *
25  * 5. Due credit should be given to the OpenEJB Project
26  * (http://www.openejb.org/).
27  *
28  * THIS SOFTWARE IS PROVIDED BY THE OPENEJB GROUP AND CONTRIBUTORS
29  * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
30  * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
31  * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
32  * THE OPENEJB GROUP OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
33  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
34  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
35  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
36  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
37  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
38  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
39  * OF THE POSSIBILITY OF SUCH DAMAGE.
40  *
41  * Copyright 2001 (C) The OpenEJB Group. All Rights Reserved.
42  *
43  * $Id: EjbJarUtils.java 2164 2005-09-17 05:31:31Z jcscoobyrs $
44  */

45 package org.openejb.alt.config;
46
47 import java.io.File JavaDoc;
48 import java.io.FileInputStream JavaDoc;
49 import java.io.FileOutputStream JavaDoc;
50 import java.io.FileWriter JavaDoc;
51 import java.io.IOException JavaDoc;
52 import java.io.Writer JavaDoc;
53 import java.util.Properties JavaDoc;
54 import java.util.Vector JavaDoc;
55 import java.util.jar.JarEntry JavaDoc;
56 import java.util.jar.JarFile JavaDoc;
57
58 import org.exolab.castor.util.Configuration;
59 import org.exolab.castor.util.LocalConfiguration;
60 import org.exolab.castor.xml.MarshalException;
61 import org.exolab.castor.xml.ValidationException;
62 import org.openejb.OpenEJBException;
63 import org.openejb.alt.config.ejb11.EjbJar;
64 import org.openejb.alt.config.ejb11.EnterpriseBeansItem;
65 import org.openejb.alt.config.ejb11.OpenejbJar;
66 import org.openejb.alt.config.sys.Container;
67 import org.openejb.loader.SystemInstance;
68 import org.openejb.util.JarUtils;
69 import org.openejb.util.Logger;
70 import org.openejb.util.Messages;
71
72 /**
73  *
74  * @author <a HREF="mailto:david.blevins@visi.com">David Blevins</a>
75  */

76 public class EjbJarUtils {
77
78     public static final Messages messages = new Messages("org.openejb.util.resources");
79     public static final Logger logger = Logger.getInstance("OpenEJB.startup", "org.openejb.util.resources");
80
81     private final EjbJar ejbJar;
82     private final String JavaDoc jarLocation;
83     private OpenejbJar openejbJar;
84     
85     static {
86         Properties JavaDoc properties = LocalConfiguration.getInstance().getProperties();
87         properties.setProperty( Configuration.Property.Indent, "true");
88     }
89
90     // TODO Make this a plain EjbJar instance with String jarFile as constructor
91
// TODO Add support for unpacked jars (jarFile is a directory)
92
public EjbJarUtils(String JavaDoc jarLocation) throws OpenEJBException {
93         /*[1.1] Get the jar ***************/
94         this.jarLocation = jarLocation;
95         this.ejbJar = readEjbJar(jarLocation);
96         try {
97             this.openejbJar = readOpenEjbJar(jarLocation);
98         } catch (OpenEJBException e) {
99             logger.warning("Reading openejb-jar.xml.",e);
100         }
101     }
102
103     /**
104      * Opens the specified jar file, locates the openejb-jar.xml file,
105      * unmarshals it to a java object and returns it.
106      *
107      * @param jarLocation
108      * @return null if there is no openejb-jar.xml
109      * @throws OpenEJBException
110      */

111     private OpenejbJar readOpenEjbJar(String JavaDoc jarLocation) throws OpenEJBException {
112         return (OpenejbJar) Unmarshaller.unmarshal(OpenejbJar.class, "META-INF/openejb-jar.xml", jarLocation);
113     }
114
115     private EjbJar readEjbJar(String JavaDoc jarLocation) throws OpenEJBException {
116         return (EjbJar)Unmarshaller.unmarshal(EjbJar.class, "META-INF/ejb-jar.xml", jarLocation);
117     }
118
119     public String JavaDoc getJarLocation() {
120         return jarLocation;
121     }
122
123     public EjbJar getEjbJar() {
124         return ejbJar;
125     }
126
127     public OpenejbJar getOpenejbJar() {
128         return openejbJar;
129     }
130
131     public void setOpenejbJar(OpenejbJar openejbJar) {
132         this.openejbJar = openejbJar;
133     }
134
135     public void writeEjbJar(String JavaDoc xmlFile) throws OpenEJBException {
136         /* TODO: Just to be picky, the xml file created by
137         Castor is really hard to read -- it is all on one line.
138         People might want to edit this in the future by hand, so if Castor can
139         make the output look better that would be great! Otherwise we could
140         just spruce the output up by adding a few new lines and tabs.
141         */

142         Writer JavaDoc writer = null;
143         try {
144             File JavaDoc file = new File JavaDoc(xmlFile);
145             writer = new FileWriter JavaDoc(file);
146             ejbJar.marshal(writer);
147         } catch (IOException JavaDoc e) {
148             throw new OpenEJBException(messages.format("conf.3040", xmlFile, e.getLocalizedMessage()));
149         } catch (MarshalException e) {
150             if (e.getException() instanceof IOException JavaDoc) {
151                 throw new OpenEJBException(messages.format("conf.3040", xmlFile, e.getLocalizedMessage()));
152             } else {
153                 throw new OpenEJBException(messages.format("conf.3050", xmlFile, e.getLocalizedMessage()));
154             }
155         } catch (ValidationException e) {
156             /* TODO: Implement informative error handling here.
157                The exception will say "X doesn't match the regular
158                expression Y"
159                This should be checked and more relevant information
160                should be given -- not everyone understands regular
161                expressions.
162              */

163             /* NOTE: This doesn't seem to ever happen. When the object graph
164              * is invalid, the MarshalException is thrown, not this one as you
165              * would think.
166              */

167             throw new OpenEJBException(messages.format("conf.3060", xmlFile, e.getLocalizedMessage()));
168         }
169         try {
170             writer.close();
171         } catch (Exception JavaDoc e) {
172             throw new OpenEJBException(messages.format("file.0020", xmlFile, e.getLocalizedMessage()));
173         }
174     }
175
176     public static String JavaDoc moveJar(String JavaDoc jar, boolean overwrite) throws OpenEJBException {
177         File JavaDoc origFile = new File JavaDoc(jar);
178
179         // Safety checks
180
if (!origFile.exists()) {
181             handleException("deploy.m.010", origFile.getAbsolutePath());
182         }
183
184         if (origFile.isDirectory()) {
185             handleException("deploy.m.020", origFile.getAbsolutePath());
186         }
187
188         if (!origFile.isFile()) {
189             handleException("deploy.m.030", origFile.getAbsolutePath());
190         }
191
192         // Move file
193
String JavaDoc jarName = origFile.getName();
194         File JavaDoc beansDir = null;
195         try {
196             beansDir = SystemInstance.get().getBase().getDirectory("beans");
197         } catch (java.io.IOException JavaDoc ioe) {
198             throw new OpenEJBException(messages.format("deploy.m.040", origFile.getAbsolutePath(), ioe.getMessage()));
199         }
200
201         File JavaDoc newFile = new File JavaDoc(beansDir, jarName);
202         boolean moved = false;
203
204         try {
205             if (newFile.exists()) {
206                 if (overwrite) {
207                     newFile.delete();
208                 } else {
209                     throw new OpenEJBException(messages.format("deploy.m.061", origFile.getAbsolutePath(), beansDir.getAbsolutePath()));
210                 }
211             }
212             moved = origFile.renameTo(newFile);
213         } catch (SecurityException JavaDoc se) {
214             throw new OpenEJBException(messages.format("deploy.m.050", origFile.getAbsolutePath(), se.getMessage()));
215         }
216
217         if (!moved) {
218             throw new OpenEJBException(messages.format("deploy.m.060", origFile.getAbsolutePath(), newFile.getAbsoluteFile()));
219         }
220         return newFile.getAbsolutePath();
221     }
222
223     public static String JavaDoc copyJar(String JavaDoc jar, boolean overwrite) throws OpenEJBException {
224         File JavaDoc origFile = new File JavaDoc(jar);
225
226         // Safety checks
227
if (!origFile.exists()) {
228             handleException("deploy.c.010", origFile.getAbsolutePath());
229             return jar;
230         }
231
232         if (origFile.isDirectory()) {
233             handleException("deploy.c.020", origFile.getAbsolutePath());
234             return jar;
235         }
236
237         if (!origFile.isFile()) {
238             handleException("deploy.c.030", origFile.getAbsolutePath());
239             return jar;
240         }
241
242         // Move file
243
String JavaDoc jarName = origFile.getName();
244         File JavaDoc beansDir = null;
245         try {
246             beansDir = SystemInstance.get().getBase().getDirectory("beans");
247         } catch (java.io.IOException JavaDoc ioe) {
248             throw new OpenEJBException(messages.format("deploy.c.040", origFile.getAbsolutePath(), ioe.getMessage()));
249         }
250
251         File JavaDoc newFile = new File JavaDoc(beansDir, jarName);
252
253         try {
254             if (newFile.exists()) {
255                 if (overwrite) {
256                     newFile.delete();
257                 } else {
258                     throw new OpenEJBException(messages.format("deploy.c.061", origFile.getAbsolutePath(), beansDir.getAbsolutePath()));
259                 }
260             }
261
262             FileInputStream JavaDoc in = new FileInputStream JavaDoc(origFile);
263             FileOutputStream JavaDoc out = new FileOutputStream JavaDoc(newFile);
264
265             int b = in.read();
266             while (b != -1) {
267                 out.write(b);
268                 b = in.read();
269             }
270
271             in.close();
272             out.close();
273
274         } catch (SecurityException JavaDoc e) {
275             throw new OpenEJBException(messages.format("deploy.c.050", origFile.getAbsolutePath(), beansDir.getAbsolutePath(), e.getMessage()));
276         } catch (IOException JavaDoc e) {
277             handleException("deploy.c.060", origFile.getAbsolutePath(), newFile.getAbsolutePath(), e.getClass().getName(), e.getMessage());
278         }
279
280         return newFile.getAbsolutePath();
281     }
282
283     public static Container[] getUsableContainers(Container[] containers, Bean bean) {
284         Vector JavaDoc c = new Vector JavaDoc();
285
286         for (int i = 0; i < containers.length; i++) {
287             if (containers[i].getCtype().equals(bean.getType())) {
288                 c.add(containers[i]);
289             }
290         }
291
292         Container[] useableContainers = new Container[c.size()];
293         c.copyInto(useableContainers);
294
295         return useableContainers;
296     }
297
298     public Bean[] getBeans() {
299         EnterpriseBeansItem[] items = ejbJar.getEnterpriseBeans().getEnterpriseBeansItem();
300         Bean[] beans = new Bean[items.length];
301         for (int i = 0; i < items.length; i++) {
302             if (items[i].getEntity() == null) {
303                 beans[i] = new SessionBean(items[i].getSession());
304             } else {
305                 beans[i] = new EntityBean(items[i].getEntity());
306             }
307         }
308         return beans;
309     }
310
311     /*------------------------------------------------------*/
312     /* Methods for easy exception handling */
313     /*------------------------------------------------------*/
314     public static void handleException(String JavaDoc errorCode, Object JavaDoc arg0, Object JavaDoc arg1, Object JavaDoc arg2, Object JavaDoc arg3) throws OpenEJBException {
315         throw new OpenEJBException(messages.format(errorCode, arg0, arg1, arg2, arg3));
316     }
317
318     public static void handleException(String JavaDoc errorCode, Object JavaDoc arg0) throws OpenEJBException {
319         throw new OpenEJBException(messages.format(errorCode, arg0));
320     }
321
322     public static void handleException(String JavaDoc errorCode) throws OpenEJBException {
323         throw new OpenEJBException(messages.message(errorCode));
324     }
325
326     public static boolean checkForOpenejbJar(String JavaDoc jarFile) throws OpenEJBException {
327         /*[1.1] Get the jar ***************/
328         JarFile JavaDoc jar = JarUtils.getJarFile(jarFile);
329
330         /*[1.2] Find the openejb-jar.xml from the jar ***************/
331         JarEntry JavaDoc entry = jar.getJarEntry("META-INF/openejb-jar.xml");
332         if (entry == null) entry = jar.getJarEntry("openejb-jar.xml");
333         if (entry == null) return false;
334
335         return true;
336     }
337
338     public static void writeOpenejbJar(String JavaDoc xmlFile, OpenejbJar openejbJarObject) throws OpenEJBException {
339         /* TODO: Just to be picky, the xml file created by
340         Castor is really hard to read -- it is all on one line.
341         People might want to edit this in the future by hand, so if Castor can
342         make the output look better that would be great! Otherwise we could
343         just spruce the output up by adding a few new lines and tabs.
344         */

345         Writer JavaDoc writer = null;
346         try {
347             File JavaDoc file = new File JavaDoc(xmlFile);
348             File JavaDoc dirs = file.getParentFile();
349             if (dirs != null) dirs.mkdirs();
350             writer = new FileWriter JavaDoc(file);
351             openejbJarObject.marshal(writer);
352         } catch (SecurityException JavaDoc e) {
353             throw new OpenEJBException(messages.format("conf.2040", xmlFile, e.getLocalizedMessage()));
354         } catch (IOException JavaDoc e) {
355             throw new OpenEJBException(messages.format("conf.2040", xmlFile, e.getLocalizedMessage()));
356         } catch (MarshalException e) {
357             if (e.getException() instanceof IOException JavaDoc) {
358                 throw new OpenEJBException(messages.format("conf.2040", xmlFile, e.getLocalizedMessage()));
359             } else {
360                 throw new OpenEJBException(messages.format("conf.2050", xmlFile, e.getLocalizedMessage()));
361             }
362         } catch (ValidationException e) {
363             /* TODO: Implement informative error handling here.
364                The exception will say "X doesn't match the regular
365                expression Y"
366                This should be checked and more relevant information
367                should be given -- not everyone understands regular
368                expressions.
369              */

370             /* NOTE: This doesn't seem to ever happen. When the object graph
371              * is invalid, the MarshalException is thrown, not this one as you
372              * would think.
373              */

374             throw new OpenEJBException(messages.format("conf.2060", xmlFile, e.getLocalizedMessage()));
375         }
376         try {
377             writer.close();
378         } catch (Exception JavaDoc e) {
379             throw new OpenEJBException(messages.format("file.0020", xmlFile, e.getLocalizedMessage()));
380         }
381     }
382 }
Popular Tags