KickJava   Java API By Example, From Geeks To Geeks.

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


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 info@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://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 2005 (C) The OpenEJB Group. All Rights Reserved.
42  *
43  * $Id: TempCodebase.java 2082 2005-08-16 04:18:56Z dblevins $
44  */

45
46 package org.openejb.alt.config;
47
48 import org.openejb.OpenEJBException;
49 import org.openejb.util.SafeToolkit;
50 import org.openejb.util.FileUtils;
51
52 import java.util.HashMap JavaDoc;
53 import java.net.URL JavaDoc;
54 import java.io.File JavaDoc;
55
56 /**
57  * @version $Revision: 2082 $ $Date: 2005-08-15 21:18:56 -0700 (Mon, 15 Aug 2005) $
58  */

59 public class TempCodebase {
60
61     protected static final HashMap JavaDoc tempCodebases = new HashMap JavaDoc();
62
63     private final String JavaDoc codebase;
64     private final ClassLoader JavaDoc classLoader;
65
66     public TempCodebase(String JavaDoc codebase) throws OpenEJBException {
67         this.codebase = codebase;
68         ClassLoader JavaDoc cl = null;
69         try {
70             URL JavaDoc[] urlCodebase = new URL JavaDoc[1];
71             urlCodebase[0] = createTempCopy(codebase).toURL();
72             cl = new java.net.URLClassLoader JavaDoc(urlCodebase, TempCodebase.class.getClassLoader());
73         } catch (java.net.MalformedURLException JavaDoc mue) {
74             throw new OpenEJBException(SafeToolkit.messages.format("cl0001", codebase, mue.getMessage()));
75         } catch (SecurityException JavaDoc se) {
76             throw new OpenEJBException(SafeToolkit.messages.format("cl0002", codebase, se.getMessage()));
77         }
78         this.classLoader = cl;
79     }
80
81     public String JavaDoc getCodebase() {
82         return codebase;
83     }
84
85     public ClassLoader JavaDoc getClassLoader() {
86         return classLoader;
87     }
88
89     public static TempCodebase getTempCodebase(String JavaDoc codebase) throws OpenEJBException {
90         if (codebase == null) {
91             codebase = "CLASSPATH";
92         }
93         TempCodebase tempCodebase = (TempCodebase)tempCodebases.get(codebase);
94         if (tempCodebase == null){
95             tempCodebase = new TempCodebase(codebase);
96             tempCodebases.put(codebase, tempCodebase);
97         }
98         return tempCodebase;
99     }
100
101     public Class JavaDoc loadClass(String JavaDoc className) throws OpenEJBException {
102         ClassLoader JavaDoc cl = getClassLoader();
103         Class JavaDoc clazz = null;
104         try {
105             clazz = cl.loadClass(className);
106         } catch (ClassNotFoundException JavaDoc cnfe) {
107             throw new OpenEJBException(SafeToolkit.messages.format("cl0007", className, codebase));
108         }
109         return clazz;
110     }
111
112     public static void unloadTempCodebase(String JavaDoc codebase) {
113         //TODO Delete temp jar
114
tempCodebases.remove(codebase);
115     }
116
117     /**
118      * Ensures that a class loader for each code base used in the
119      * system is created at most one time. The default bootsrap
120      * classloader is used if codebase is null.
121      *
122      * @param codebase
123      * @return ClassLoader
124      * @throws org.openejb.OpenEJBException
125      */

126     protected static ClassLoader JavaDoc getCodebaseTempClassLoader(String JavaDoc codebase) throws OpenEJBException {
127         if (codebase == null) codebase = "CLASSPATH";
128
129         ClassLoader JavaDoc cl = (ClassLoader JavaDoc) tempCodebases.get(codebase);
130         if (cl == null) {
131             synchronized (SafeToolkit.codebases) {
132                 cl = (ClassLoader JavaDoc) SafeToolkit.codebases.get(codebase);
133                 if (cl == null) {
134                     try {
135                         URL JavaDoc[] urlCodebase = new URL JavaDoc[1];
136                         urlCodebase[0] = createTempCopy(codebase).toURL();
137
138 // make sure everything works if we were not loaded by the system class loader
139
cl = new java.net.URLClassLoader JavaDoc(urlCodebase, SafeToolkit.class.getClassLoader());
140
141                         tempCodebases.put(codebase, cl);
142                     } catch (java.net.MalformedURLException JavaDoc mue) {
143                         throw new OpenEJBException(SafeToolkit.messages.format("cl0001", codebase, mue.getMessage()));
144                     } catch (SecurityException JavaDoc se) {
145                         throw new OpenEJBException(SafeToolkit.messages.format("cl0002", codebase, se.getMessage()));
146                     }
147                 }
148             }
149         }
150         return cl;
151     }
152
153     /**
154      * Ensures that a class loader for each code base used in the
155      * system is created at most one time. The default bootsrap
156      * classloader is used if codebase is null.
157      *
158      * @param codebase
159      * @return ClassLoader
160      * @throws org.openejb.OpenEJBException
161      */

162     protected static ClassLoader JavaDoc getTempClassLoader(String JavaDoc codebase) throws OpenEJBException {
163         ClassLoader JavaDoc cl = null;
164         try {
165             URL JavaDoc[] urlCodebase = new URL JavaDoc[1];
166             urlCodebase[0] = createTempCopy(codebase).toURL();
167
168             // make sure everything works if we were not loaded by the system class loader
169
cl = new java.net.URLClassLoader JavaDoc(urlCodebase, SafeToolkit.class.getClassLoader());
170         } catch (java.net.MalformedURLException JavaDoc mue) {
171             throw new OpenEJBException(SafeToolkit.messages.format("cl0001", codebase, mue.getMessage()));
172         } catch (SecurityException JavaDoc se) {
173             throw new OpenEJBException(SafeToolkit.messages.format("cl0002", codebase, se.getMessage()));
174         }
175         return cl;
176     }
177
178     protected static File JavaDoc createTempCopy(String JavaDoc codebase) throws OpenEJBException {
179         File JavaDoc file = null;
180
181         try {
182             File JavaDoc codebaseFile = new File JavaDoc(codebase);
183 // if (codebaseFile.isDirectory()) return codebaseFile;
184

185             file = File.createTempFile("openejb_validate", ".jar", null);
186             file.deleteOnExit();
187
188             FileUtils.copyFile(file, codebaseFile);
189         } catch (Exception JavaDoc e) {
190             throw new OpenEJBException(SafeToolkit.messages.format("cl0002", codebase, e.getMessage()));
191         }
192         return file;
193     }
194 }
195
Popular Tags