1 23 24 package com.sun.enterprise.addon; 25 26 import java.io.*; 27 import java.lang.reflect.*; 28 import java.net.URL ; 29 import java.net.URLClassLoader ; 30 import java.util.jar.*; 31 import java.util.*; 32 import java.util.logging.Level ; 33 import java.util.logging.Logger ; 34 import com.sun.enterprise.util.OS; 35 36 40 public class Addon { 41 42 private File addonJar; 43 44 45 public Addon(File addonJar) { 46 this.addonJar = addonJar; 47 } 48 49 public boolean install(String installDir, String instanceRoot) throws Exception 50 { 51 JarFile file = new JarFile(addonJar); 52 Manifest mf = file.getManifest(); 53 Attributes attributes = null; 54 if(mf != null) { 55 attributes = mf.getMainAttributes(); 56 if(attributes != null) { 57 String mainClass = attributes.getValue(Attributes.Name.MAIN_CLASS); 58 Logger.getAnonymousLogger().log(Level.FINE, "Addon getting installed: "+addonJar); 59 Logger.getAnonymousLogger().log(Level.FINE, "Main Class "+mainClass); 60 if(mainClass != null) { 61 URL url = addonJar.toURI().toURL(); 62 List<URL > classPath = new ArrayList<URL >(); 63 classPath.add(url); 64 addClasspath(installDir, classPath); 65 URL [] urls = classPath.toArray(new URL [] {}); 67 URLClassLoader loader = new URLClassLoader (urls); 68 Class main = loader.loadClass(mainClass); 69 Object obj = main.newInstance(); 73 String [] argu = new String [] {installDir, instanceRoot}; 74 Class [] types = new Class [] {argu.getClass()}; 75 Method method = main.getMethod(AddonConstants.MAIN, types); 76 Object [] args = new Object [] { argu }; 77 method.invoke(obj,args); 78 return true; 79 80 } else 81 return false; 82 } else 83 return false; 84 } else 85 return false; 86 87 } 88 89 94 private void addClasspath(String installDir, List classPath) throws Exception { 95 BufferedReader bf = null; 96 InputStream in = null; 97 try { 98 String asenv=""; 99 if(OS.isUNIX()) 100 asenv = installDir + File.separator + AddonConstants.CONFIG + File.separator + AddonConstants.ASENVCONF; 101 else 102 asenv = installDir + File.separator + AddonConstants.CONFIG + File.separator + AddonConstants.ASENVBAT; 103 104 in = new FileInputStream(asenv); 105 String antLib = ""; 106 bf = new BufferedReader(new InputStreamReader(in)); 107 String line = bf.readLine(); 108 while(line != null) { 109 if(line.indexOf(AddonConstants.ASANTLIB) != -1) { 110 int pos = line.indexOf("="); 111 if (pos > 0) { 112 String lhs = (line.substring(0, pos)).trim(); 113 String rhs = (line.substring(pos + 1)).trim(); 114 115 if (OS.isWindows()) { lhs = (lhs.substring(3)).trim(); 117 } 118 119 if (OS.isUNIX()) { pos = rhs.indexOf("\""); 121 if(pos != -1) { 122 rhs = (rhs.substring(pos+1)).trim(); 123 pos = rhs.indexOf("\""); 124 if(pos != -1) 125 rhs = (rhs.substring(0, pos)).trim(); 126 } 127 128 } 129 antLib = rhs; 130 break; 131 } 132 } 133 line = bf.readLine(); 134 } 135 136 Logger.getAnonymousLogger().log(Level.FINE,"antLib "+antLib); 137 File antLibDir = new File(antLib); 138 File[] fileArray = antLibDir.listFiles(); 139 140 for(int i = 0;i<fileArray.length;i++) { 141 if(fileArray[i].getName().endsWith(".jar")) { 142 URL url = fileArray[i].toURI().toURL(); 143 classPath.add(url); 144 } 145 } 146 File installLib = new File(installDir + File.separator + AddonConstants.LIB ); 147 File[] installLibArray = installLib.listFiles(); 148 149 Logger.getAnonymousLogger().log(Level.FINE,"installLib "+installLib.getAbsolutePath()); 150 for(int i = 0;i<installLibArray.length;i++) { 151 if(installLibArray[i].getName().endsWith(".jar")) { 152 URL url = installLibArray[i].toURI().toURL(); 153 classPath.add(url); 154 } 155 } 156 }catch(Exception e) { 157 throw e; 158 } finally { 159 if(bf != null) 160 bf.close(); 161 if(in != null) 162 in.close(); 163 } 164 165 } 166 167 } 168 | Popular Tags |