KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openejb > util > JarUtils


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: JarUtils.java 1912 2005-06-16 22:29:56Z jlaskowski $
44  */

45 package org.openejb.util;
46
47 import java.io.ByteArrayOutputStream JavaDoc;
48 import java.io.File JavaDoc;
49 import java.io.FileNotFoundException JavaDoc;
50 import java.io.IOException JavaDoc;
51 import java.io.PrintStream JavaDoc;
52 import java.net.URL JavaDoc;
53 import java.security.AccessController JavaDoc;
54 import java.security.PrivilegedAction JavaDoc;
55 import java.util.Hashtable JavaDoc;
56 import java.util.jar.JarFile JavaDoc;
57
58 import org.openejb.OpenEJBException;
59
60 /**
61  * @author <a HREF="mailto:adc@toolazydogs.com">Alan Cabrera</a>
62  * @author <a HREF="mailto:david.blevins@visi.com">David Blevins</a>
63  */

64 public class JarUtils{
65     
66     private static Messages messages = new Messages( "org.openejb.util.resources" );
67     
68     static {
69         setHandlerSystemProperty();
70     }
71     
72     private static boolean alreadySet = false;
73
74     public static void setHandlerSystemProperty(){
75         if (!alreadySet) {
76             /*
77              * Setup the java protocol handler path to include org.openejb.util.urlhandler
78              * so that org.openejb.util.urlhandler.resource.Handler will be used for URLs
79              * of the form "resource:/path".
80              */

81             /*try {
82                 String oldPkgs = System.getProperty( "java.protocol.handler.pkgs" );
83
84                 if ( oldPkgs == null )
85                     System.setProperty( "java.protocol.handler.pkgs", "org.openejb.util.urlhandler" );
86                 else if ( oldPkgs.indexOf( "org.openejb.util.urlhandler" ) < 0 )
87                     System.setProperty( "java.protocol.handler.pkgs", oldPkgs + "|" + "org.openejb.util.urlhandler" );
88
89             } catch ( SecurityException ex ) {
90             }*/

91             Hashtable JavaDoc urlHandlers = (Hashtable JavaDoc)AccessController.doPrivileged(
92                 new PrivilegedAction JavaDoc(){
93                     public Object JavaDoc run() {
94                         java.lang.reflect.Field JavaDoc handlers = null;
95                         try{
96                         handlers = URL JavaDoc.class.getDeclaredField("handlers");
97                         handlers.setAccessible(true);
98                         return handlers.get(null);
99                         } catch (Exception JavaDoc e2){
100                             e2.printStackTrace();
101                         }
102                         return null;
103                     }
104                 }
105             );
106             urlHandlers.put("resource", new org.openejb.util.urlhandler.resource.Handler());
107             alreadySet = true;
108         }
109     }
110
111     public static File JavaDoc getJarContaining(String JavaDoc path) throws OpenEJBException{
112         File JavaDoc jarFile = null;
113         try {
114             URL JavaDoc url = new URL JavaDoc("resource:/"+path);
115         
116             /*
117              * If we loaded the configuration from a jar, either from a jar:
118              * URL or a resource: URL, we must strip off the config file location
119              * from the URL.
120              */

121             String JavaDoc jarPath = null;
122             if ( url.getProtocol().compareTo("resource") == 0 ) {
123                 String JavaDoc resource = url.getFile().substring( 1 );
124                 //url = ClassLoader.getSystemResource( resource );
125
url = getContextClassLoader().getResource( resource );
126                 if (url == null) {
127                     throw new OpenEJBException("Could not locate a jar containing the path "+path);
128                 }
129             }
130             
131             if ( url != null ) {
132                 jarPath = url.getFile();
133                 jarPath = jarPath.substring( 0, jarPath.indexOf('!') );
134                 jarPath = jarPath.substring( "file:".length() );
135             }
136
137             jarFile = new File JavaDoc(jarPath);
138             jarFile = jarFile.getAbsoluteFile();
139         } catch (Exception JavaDoc e){
140             throw new OpenEJBException("Could not locate a jar containing the path "+path, e);
141         }
142         return jarFile;
143     }
144
145     public static void addFileToJar(String JavaDoc jarFile, String JavaDoc file ) throws OpenEJBException{
146         ByteArrayOutputStream JavaDoc errorBytes = new ByteArrayOutputStream JavaDoc();
147     
148         /* NOTE: Sadly, we have to play this little game
149          * with temporarily switching the standard error
150          * stream to capture the errors.
151          * Although you can pass in an error stream in
152          * the constructor of the jar tool, they are not
153          * used when an error occurs.
154          */

155         PrintStream JavaDoc newErr = new PrintStream JavaDoc(errorBytes);
156         PrintStream JavaDoc oldErr = System.err;
157         System.setErr(newErr);
158     
159         sun.tools.jar.Main jarTool = new sun.tools.jar.Main(newErr, newErr, "config_utils");
160     
161         String JavaDoc[] args = new String JavaDoc[]{"uf",jarFile,file};
162         jarTool.run(args);
163     
164         System.setErr(oldErr);
165     
166         try{
167         errorBytes.close();
168         newErr.close();
169         } catch (Exception JavaDoc e){
170             throw new OpenEJBException( messages.format("file.0020",jarFile, e.getLocalizedMessage()));
171         }
172     
173         String JavaDoc error = new String JavaDoc(errorBytes.toByteArray());
174         if (error.indexOf("java.io.IOException") != -1) {
175             // an IOException was thrown!
176
// clean the error message
177
int begin = error.indexOf(':')+1;
178             int end = error.indexOf('\n');
179             String JavaDoc message = error.substring(begin, end);
180             throw new OpenEJBException( messages.format("file.0003", file, jarFile, message) );
181         }
182     
183     }
184
185     public static JarFile JavaDoc getJarFile(String JavaDoc jarFile) throws OpenEJBException{
186         /*[1.1] Get the jar ***************/
187         JarFile JavaDoc jar = null;
188         try {
189             File JavaDoc file = new File JavaDoc(jarFile);
190             jar = new JarFile JavaDoc(file);
191         } catch ( FileNotFoundException JavaDoc e ) {
192             throw new OpenEJBException( messages.format("file.0001", jarFile, e.getLocalizedMessage()));
193         } catch ( IOException JavaDoc e ) {
194             throw new OpenEJBException( messages.format("file.0002", jarFile, e.getLocalizedMessage()));
195         }
196         return jar;
197     }
198
199     public static ClassLoader JavaDoc getContextClassLoader() {
200         return (ClassLoader JavaDoc) java.security.AccessController.doPrivileged(
201             new java.security.PrivilegedAction JavaDoc() {
202                 public Object JavaDoc run() {
203                     return Thread.currentThread().getContextClassLoader();
204                 }
205             }
206         );
207     }
208
209 }
210
Popular Tags