1 17 package org.apache.geronimo.plugin.packaging; 18 19 import java.io.File ; 20 import java.net.URI ; 21 import java.util.Iterator ; 22 import java.util.List ; 23 import javax.management.MalformedObjectNameException ; 24 import javax.management.ObjectName ; 25 26 import org.apache.geronimo.gbean.GBeanData; 27 import org.apache.geronimo.kernel.KernelRegistry; 28 import org.apache.geronimo.kernel.KernelFactory; 29 import org.apache.geronimo.kernel.Kernel; 30 import org.apache.geronimo.kernel.config.ConfigurationManager; 31 import org.apache.geronimo.kernel.config.ConfigurationUtil; 32 33 39 public class PackageBuilder { 40 private static final String KERNEL_NAME = "geronimo.maven"; 41 44 private static final ObjectName REPOSITORY_NAME; 45 46 49 private static final ObjectName CONFIGSTORE_NAME; 50 51 private static final String [] ARG_TYPES = { 52 File .class.getName(), 53 File .class.getName(), 54 File .class.getName(), 55 Boolean.TYPE.getName(), 56 String .class.getName(), 57 String .class.getName(), 58 String .class.getName(), 59 }; 60 61 static { 62 try { 63 REPOSITORY_NAME = new ObjectName (KERNEL_NAME + ":name=Repository"); 64 CONFIGSTORE_NAME = new ObjectName (KERNEL_NAME + ":name=MavenConfigStore,j2eeType=ConfigurationStore"); 65 } catch (MalformedObjectNameException e) { 66 throw new ExceptionInInitializerError (e.getMessage()); 67 } 68 } 69 70 private File repository; 71 private URI deploymentConfig; 72 private ObjectName deployerName; 73 74 private File planFile; 75 private File moduleFile; 76 private File packageFile; 77 private String mainClass; 78 private String classPath; 79 private String endorsedDirs; 80 81 public File getRepository() { 82 return repository; 83 } 84 85 90 public void setRepository(File repository) { 91 this.repository = repository; 92 } 93 94 public String getDeploymentConfig() { 95 return deploymentConfig.toString(); 96 } 97 98 103 public void setDeploymentConfig(String deploymentConfig) { 104 this.deploymentConfig = URI.create(deploymentConfig); 105 } 106 107 public String getDeployerName() { 108 return deployerName.toString(); 109 } 110 111 116 public void setDeployerName(String deployerName) { 117 try { 118 this.deployerName = new ObjectName (deployerName); 119 } catch (MalformedObjectNameException e) { 120 throw new IllegalArgumentException ("deployerName is not a valid ObjectName: " + deployerName); 121 } 122 } 123 124 public File getPlanFile() { 125 return planFile; 126 } 127 128 133 public void setPlanFile(File planFile) { 134 this.planFile = planFile; 135 } 136 137 public File getModuleFile() { 138 return moduleFile; 139 } 140 141 146 public void setModuleFile(File moduleFile) { 147 this.moduleFile = moduleFile; 148 } 149 150 public File getPackageFile() { 151 return packageFile; 152 } 153 154 159 public void setPackageFile(File packageFile) { 160 this.packageFile = packageFile; 161 } 162 163 public String getMainClass() { 164 return mainClass; 165 } 166 167 172 public void setMainClass(String mainClass) { 173 this.mainClass = mainClass; 174 } 175 176 public String getClassPath() { 177 return classPath; 178 } 179 180 public void setClassPath(String classPath) { 181 this.classPath = classPath; 182 } 183 184 public String getEndorsedDirs() { 185 return endorsedDirs; 186 } 187 188 public void setEndorsedDirs(String endorsedDirs) { 189 this.endorsedDirs = endorsedDirs; 190 } 191 192 public void execute() throws Exception { 193 Kernel kernel = createKernel(); 194 195 ConfigurationManager configMgr = ConfigurationUtil.getConfigurationManager(kernel); 197 if (!configMgr.isLoaded(deploymentConfig)) { 198 List configs = configMgr.loadRecursive(deploymentConfig); 199 for (int i = 0; i < configs.size(); i++) { 200 ObjectName configName = (ObjectName ) configs.get(i); 201 kernel.startRecursiveGBean(configName); 202 } 203 } 204 205 ObjectName deployer = locateDeployer(kernel); 206 invokeDeployer(kernel, deployer); 207 System.out.println("Generated package " + packageFile); 208 } 209 210 213 private Kernel createKernel() throws Exception { 214 Kernel kernel = KernelRegistry.getKernel(KERNEL_NAME); 215 if (kernel != null) { 216 return kernel; 217 } 218 219 kernel = KernelFactory.newInstance().createKernel(KERNEL_NAME); 220 kernel.boot(); 221 222 bootDeployerSystem(kernel); 223 224 return kernel; 225 } 226 227 232 private void bootDeployerSystem(Kernel kernel) throws Exception { 233 ClassLoader cl = PackageBuilder.class.getClassLoader(); 234 GBeanData repoGBean = new GBeanData(REPOSITORY_NAME, MavenRepository.GBEAN_INFO); 235 repoGBean.setAttribute("root", repository); 236 237 GBeanData storeGBean = new GBeanData(CONFIGSTORE_NAME, MavenConfigStore.GBEAN_INFO); 238 storeGBean.setReferencePattern("Repository", REPOSITORY_NAME); 239 240 kernel.loadGBean(repoGBean, cl); 241 kernel.startGBean(REPOSITORY_NAME); 242 kernel.loadGBean(storeGBean, cl); 243 kernel.startGBean(CONFIGSTORE_NAME); 244 } 245 246 253 private ObjectName locateDeployer(Kernel kernel) { 254 Iterator i = kernel.listGBeans(deployerName).iterator(); 255 if (!i.hasNext()) { 256 throw new IllegalStateException ("No deployer found matching deployerName: " + deployerName); 257 } 258 ObjectName deployer = (ObjectName ) i.next(); 259 if (i.hasNext()) { 260 throw new IllegalStateException ("Multiple deployers found matching deployerName: " + deployerName); 261 } 262 return deployer; 263 } 264 265 private List invokeDeployer(Kernel kernel, ObjectName deployer) throws Exception { 266 Object [] args = {planFile, moduleFile, packageFile, Boolean.FALSE, mainClass, classPath, endorsedDirs}; 267 return (List ) kernel.invoke(deployer, "deploy", args, ARG_TYPES); 268 } 269 } 270 | Popular Tags |