1 23 24 package com.sun.enterprise.admin.mbeans.custom.loading; 25 26 import com.sun.enterprise.admin.mbeans.custom.CMBStrings; 27 import com.sun.enterprise.admin.servermgmt.RepositoryConfig; 28 import com.sun.enterprise.admin.servermgmt.pe.PEFileLayout; 29 import com.sun.enterprise.util.SystemPropertyConstants; 30 import java.io.ByteArrayOutputStream ; 31 import java.io.File ; 32 import java.io.FileInputStream ; 33 import java.io.FileNotFoundException ; 34 import java.io.IOException ; 35 import java.net.MalformedURLException ; 36 import java.net.URL ; 37 import java.io.BufferedInputStream ; 38 import com.sun.enterprise.util.OS; 39 40 49 public final class MBeanClassLoader extends ClassLoader { 50 51 private URL url; 52 private ClassLoader parent; 53 54 78 public MBeanClassLoader() throws IllegalArgumentException { 79 super(MBeanClassLoader.class.getClassLoader()); 80 init(); 81 } 82 83 public MBeanClassLoader(final ClassLoader delegatee) throws IllegalArgumentException { 84 super(delegatee); 85 init(); 86 } 87 88 private void init() throws IllegalArgumentException { 89 final File mbf = getDefaultMBeanDirectory(); 90 try { 91 this.url = mbf.toURL(); 92 } catch (final MalformedURLException m) { 93 throw new IllegalArgumentException (m); 94 } 95 } 96 97 private File getDefaultMBeanDirectory() { 98 104 final File appsFolder = new File (System.getProperty(SystemPropertyConstants.INSTANCE_ROOT_PROPERTY), PEFileLayout.APPLICATIONS_DIR); 105 return ( new File (appsFolder, PEFileLayout.MBEAN_FOLDER_NAME) ); 106 } 107 protected Class findClass(final String className) throws ClassNotFoundException { 108 final byte[] cd = getClassData(className); 109 if (cd == null) 110 throw new ClassNotFoundException (CMBStrings.get("findClassFailed", className)); 111 return ( defineClass(className, cd, 0, cd.length) ); 112 } 113 114 private byte[] getClassData(final String cn) { 115 byte[] cd = null; 116 try { 117 if(isURLFile()) { 118 cd = getClassDataFromFile(url.getFile(), cn); 119 } else { 120 cd = getClassDataFromURL(); 121 } 122 } catch(final Exception e) { 123 e.printStackTrace(); 124 } 125 return ( cd ); 126 } 127 private boolean isURLFile() { 128 return ( "file".equals(url.getProtocol()) ); 129 } 130 131 private byte[] getClassDataFromFile(final String base, final String cn) throws IOException , FileNotFoundException { 132 String path = base + '/' + cn.replace('.', '/') + ".class"; 133 path = prunePath(path); 134 final FileInputStream fis = new FileInputStream (path); 135 final BufferedInputStream bis = new BufferedInputStream (fis); 136 final ByteArrayOutputStream bos = new ByteArrayOutputStream (); 137 byte[] cd = null; 138 try { 139 byte[] buf = new byte[1024]; int br = bis.read(buf, 0, buf.length); 141 while (br != -1) { 142 bos.write(buf, 0, br); 143 br = bis.read(buf, 0, buf.length); 144 } 145 cd = bos.toByteArray(); 146 } catch(final FileNotFoundException fe) { 147 throw fe; 148 } catch (final IOException e) { 149 throw e; 150 } finally { 151 try { 152 bis.close(); 154 bos.close(); 155 } catch(Throwable t) {} 156 return ( cd ); 157 } 158 } 159 private String prunePath(final String path) { 160 161 if (OS.isWindows() && path.charAt(0) == '/') { 162 return ( path.substring(1) ); 163 } 164 else 165 return ( path ) ; 166 } 167 private byte[] getClassDataFromURL() { 168 throw new UnsupportedOperationException (CMBStrings.get("InternalError", "getClassDataFromURL() -- not implemented yet")); 169 } 170 } 172 | Popular Tags |