1 23 24 package com.sun.enterprise.deployapi; 25 26 import java.io.*; 27 import java.util.jar.*; 28 import java.net.URL ; 29 import java.net.URLClassLoader ; 30 import java.util.logging.Logger ; 31 import java.util.logging.Level ; 32 33 import javax.enterprise.deploy.shared.factories.DeploymentFactoryManager ; 34 import javax.enterprise.deploy.spi.factories.DeploymentFactory ; 35 36 import com.sun.enterprise.deployment.util.DOLUtils; 37 import com.sun.enterprise.util.shared.ArchivistUtils; 38 39 46 public class DeploymentFactoryInstaller { 47 48 private static DeploymentFactoryInstaller dfInstaller = null; 49 50 private final String J2EE_DEPLOYMENT_MANAGER_REPOSITORY = "lib" + File.separator + "deployment"; 51 private final String J2EE_DEPLOYMENT_MANAGER = "J2EE-DeploymentFactory-Implementation-Class"; 52 private final String J2EE_HOME = "com.sun.enterprise.home"; 53 54 55 private DeploymentFactoryInstaller() { 56 } 57 58 public static DeploymentFactoryInstaller getInstaller() { 59 60 if (dfInstaller==null) { 61 dfInstaller = new DeploymentFactoryInstaller(); 62 dfInstaller.initialize(); 63 } 64 return dfInstaller; 65 } 66 67 71 public File[] getListOfDeploymentFactoryFiles() { 72 73 File repository = new File(System.getProperty("com.sun.aas.installRoot")+File.separator+ 74 J2EE_DEPLOYMENT_MANAGER_REPOSITORY); 75 76 if (DOLUtils.getDefaultLogger().isLoggable(Level.FINE)) { 77 DOLUtils.getDefaultLogger().fine("J2EE Deployment factory repository = " 78 + repository.getAbsolutePath()); 79 } 80 if (!repository.exists()) { 81 DOLUtils.getDefaultLogger().log(Level.SEVERE, "enterprise.deployment.backend.deplyomentManagerLoadFailure", 82 new Object [] {"Cannot find any deployment manager"}); 83 return null; 84 } 85 86 return repository.listFiles(); 87 } 88 89 92 public void addDeploymentFactory(File newDM) throws IOException { 93 94 int number=1; 95 File repository = new File(System.getProperty(J2EE_HOME)+File.separator+ 97 J2EE_DEPLOYMENT_MANAGER_REPOSITORY); 98 File to = new File(repository, newDM.getName()); 99 while (to.exists()) { 100 to = new File(repository, newDM.getName()+number); 101 number++; 102 } 103 ArchivistUtils.copy( 104 new BufferedInputStream(new FileInputStream(newDM)), 105 new BufferedOutputStream(new FileOutputStream(to))); 106 107 installDeploymentFactory(to); 108 109 } 110 111 112 protected void installDeploymentFactory(File installedDM) throws IOException { 113 114 if (DOLUtils.getDefaultLogger().isLoggable(Level.FINE)) { 115 DOLUtils.getDefaultLogger().fine("Installing Deployment factory = " 116 + installedDM.getAbsolutePath()); 117 } 118 119 122 126 JarFile jarFile = null; 127 Manifest m = null; 128 try { 129 jarFile = new JarFile(installedDM); 130 m = jarFile.getManifest(); 131 } finally { 132 136 jarFile.close(); 137 jarFile = null; 138 } 139 String className = m.getMainAttributes().getValue(J2EE_DEPLOYMENT_MANAGER); 140 URL [] urls = new URL []{installedDM.toURI().toURL()}; 141 URLClassLoader urlClassLoader = new java.net.URLClassLoader (urls, getClass().getClassLoader()); 142 Class factory = null; 143 try { 144 factory=urlClassLoader.loadClass(className); 145 } catch (ClassNotFoundException cnfe) { 146 DOLUtils.getDefaultLogger().log(Level.SEVERE, "enterprise.deployment.backend.deplyomentManagerLoadFailure", 147 new Object [] {"Unable to load declared DeploymentManagerFactory"}); 148 throw new IllegalArgumentException (className + " is not present in the " + installedDM.getName()); 149 } 150 151 Object df = null; 154 try { 155 df = factory.newInstance(); 156 } catch (Exception ie) { 157 DOLUtils.getDefaultLogger().log(Level.SEVERE, "enterprise.deployment.backend.deplyomentManagerLoadFailure", 158 new Object []{className}); 159 ie.printStackTrace(); 160 throw new IllegalArgumentException ("Cannot install " + installedDM.getName()); 161 } 162 if (df instanceof DeploymentFactory ) { 163 DeploymentFactoryManager.getInstance().registerDeploymentFactory((DeploymentFactory ) df); 164 } else { 165 throw new IllegalArgumentException ("The " + className + 166 " declared as a DeploymentFactory does implement the DeploymentFactory interface"); 167 } 168 } 169 170 protected void initialize() { 171 172 File[] elligibleFiles = getListOfDeploymentFactoryFiles(); 173 if (elligibleFiles==null) { 174 return; 175 } 176 177 for (int i=0;i<elligibleFiles.length;i++) { 178 try { 179 installDeploymentFactory(elligibleFiles[i]); 180 } catch(Exception ioe) { 181 ioe.printStackTrace(); 182 DOLUtils.getDefaultLogger().log(Level.SEVERE, "enterprise.deployment.backend.deplyomentManagerLoadFailure", 183 new Object [] {elligibleFiles[i].getName()}); 184 } 185 } 186 } 187 } 188 | Popular Tags |