1 45 package org.openejb.util; 46 47 import java.io.ByteArrayOutputStream ; 48 import java.io.File ; 49 import java.io.FileNotFoundException ; 50 import java.io.IOException ; 51 import java.io.PrintStream ; 52 import java.net.URL ; 53 import java.security.AccessController ; 54 import java.security.PrivilegedAction ; 55 import java.util.Hashtable ; 56 import java.util.jar.JarFile ; 57 58 import org.openejb.OpenEJBException; 59 60 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 81 91 Hashtable urlHandlers = (Hashtable )AccessController.doPrivileged( 92 new PrivilegedAction (){ 93 public Object run() { 94 java.lang.reflect.Field handlers = null; 95 try{ 96 handlers = URL .class.getDeclaredField("handlers"); 97 handlers.setAccessible(true); 98 return handlers.get(null); 99 } catch (Exception 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 getJarContaining(String path) throws OpenEJBException{ 112 File jarFile = null; 113 try { 114 URL url = new URL ("resource:/"+path); 115 116 121 String jarPath = null; 122 if ( url.getProtocol().compareTo("resource") == 0 ) { 123 String resource = url.getFile().substring( 1 ); 124 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 (jarPath); 138 jarFile = jarFile.getAbsoluteFile(); 139 } catch (Exception 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 jarFile, String file ) throws OpenEJBException{ 146 ByteArrayOutputStream errorBytes = new ByteArrayOutputStream (); 147 148 155 PrintStream newErr = new PrintStream (errorBytes); 156 PrintStream 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 [] args = new String []{"uf",jarFile,file}; 162 jarTool.run(args); 163 164 System.setErr(oldErr); 165 166 try{ 167 errorBytes.close(); 168 newErr.close(); 169 } catch (Exception e){ 170 throw new OpenEJBException( messages.format("file.0020",jarFile, e.getLocalizedMessage())); 171 } 172 173 String error = new String (errorBytes.toByteArray()); 174 if (error.indexOf("java.io.IOException") != -1) { 175 int begin = error.indexOf(':')+1; 178 int end = error.indexOf('\n'); 179 String message = error.substring(begin, end); 180 throw new OpenEJBException( messages.format("file.0003", file, jarFile, message) ); 181 } 182 183 } 184 185 public static JarFile getJarFile(String jarFile) throws OpenEJBException{ 186 187 JarFile jar = null; 188 try { 189 File file = new File (jarFile); 190 jar = new JarFile (file); 191 } catch ( FileNotFoundException e ) { 192 throw new OpenEJBException( messages.format("file.0001", jarFile, e.getLocalizedMessage())); 193 } catch ( IOException e ) { 194 throw new OpenEJBException( messages.format("file.0002", jarFile, e.getLocalizedMessage())); 195 } 196 return jar; 197 } 198 199 public static ClassLoader getContextClassLoader() { 200 return (ClassLoader ) java.security.AccessController.doPrivileged( 201 new java.security.PrivilegedAction () { 202 public Object run() { 203 return Thread.currentThread().getContextClassLoader(); 204 } 205 } 206 ); 207 } 208 209 } 210 | Popular Tags |