1 17 package org.apache.servicemix.geronimo; 18 19 import java.io.File ; 20 import java.io.IOException ; 21 import java.net.URI ; 22 import java.net.URL ; 23 import java.util.Collection ; 24 import java.util.Enumeration ; 25 import java.util.Properties ; 26 import java.util.Set ; 27 import java.util.jar.JarFile ; 28 import java.util.zip.ZipEntry ; 29 30 import javax.management.MalformedObjectNameException ; 31 32 import org.apache.commons.logging.Log; 33 import org.apache.commons.logging.LogFactory; 34 import org.apache.geronimo.common.DeploymentException; 35 import org.apache.geronimo.deployment.ConfigurationBuilder; 36 import org.apache.geronimo.deployment.DeploymentContext; 37 import org.apache.geronimo.deployment.ModuleIDBuilder; 38 import org.apache.geronimo.deployment.service.EnvironmentBuilder; 39 import org.apache.geronimo.deployment.util.DeploymentUtil; 40 import org.apache.geronimo.gbean.AbstractName; 41 import org.apache.geronimo.gbean.AbstractNameQuery; 42 import org.apache.geronimo.gbean.GBeanData; 43 import org.apache.geronimo.gbean.GBeanInfo; 44 import org.apache.geronimo.gbean.GBeanInfoBuilder; 45 import org.apache.geronimo.j2ee.j2eeobjectnames.NameFactory; 46 import org.apache.geronimo.kernel.Kernel; 47 import org.apache.geronimo.kernel.config.ConfigurationAlreadyExistsException; 48 import org.apache.geronimo.kernel.config.ConfigurationModuleType; 49 import org.apache.geronimo.kernel.config.ConfigurationStore; 50 import org.apache.geronimo.kernel.config.ConfigurationUtil; 51 import org.apache.geronimo.kernel.repository.Artifact; 52 import org.apache.geronimo.kernel.repository.ArtifactResolver; 53 import org.apache.geronimo.kernel.repository.Environment; 54 import org.apache.geronimo.kernel.repository.ImportType; 55 import org.apache.geronimo.kernel.repository.Repository; 56 import org.apache.geronimo.kernel.repository.Version; 57 import org.apache.servicemix.jbi.deployment.Descriptor; 58 import org.apache.servicemix.jbi.deployment.DescriptorFactory; 59 import org.apache.servicemix.jbi.deployment.ServiceUnit; 60 import org.apache.servicemix.jbi.deployment.SharedLibraryList; 61 62 public class ServiceMixConfigBuilder implements ConfigurationBuilder { 63 64 private static final Log log = LogFactory.getLog(ServiceMixConfigBuilder.class); 65 66 private final Environment defaultEnvironment; 67 68 private final Collection repositories; 69 70 private final Kernel kernel; 71 72 public static final GBeanInfo GBEAN_INFO; 73 74 static { 75 GBeanInfoBuilder infoFactory = new GBeanInfoBuilder(ServiceMixConfigBuilder.class, NameFactory.CONFIG_BUILDER); 76 infoFactory.addInterface(ConfigurationBuilder.class); 77 infoFactory.addAttribute("defaultEnvironment", Environment.class, true, true); 78 infoFactory.addAttribute("kernel", Kernel.class, false); 79 infoFactory.addReference("Repositories", Repository.class, "Repository"); 80 infoFactory.setConstructor(new String [] { "defaultEnvironment", "Repositories", "kernel" }); 81 GBEAN_INFO = infoFactory.getBeanInfo(); 82 } 83 84 public static GBeanInfo getGBeanInfo() { 85 return GBEAN_INFO; 86 } 87 88 public ServiceMixConfigBuilder(Environment defaultEnvironment, Collection repositories, Kernel kernel) { 89 this.defaultEnvironment = defaultEnvironment; 90 this.repositories = repositories; 91 this.kernel = kernel; 92 } 93 94 107 public Object getDeploymentPlan(File planFile, JarFile module, ModuleIDBuilder idBuilder) 108 throws DeploymentException { 109 log.debug("Checking for ServiceMix deployment."); 110 System.err.println("Checking for ServiceMix deployment."); 111 if (module == null) { 112 return null; 113 } 114 115 try { 117 URL url = DeploymentUtil.createJarURL(module, "META-INF/jbi.xml"); 118 Descriptor descriptor = DescriptorFactory.buildDescriptor(url); 119 if (descriptor == null) { 120 return null; 121 } 122 DescriptorFactory.checkDescriptor(descriptor); 123 return descriptor; 124 } catch (Exception e) { 125 log.debug("Not a ServiceMix deployment: no jbi.xml found.", e); 126 return null; 128 } 129 } 130 131 144 public Artifact getConfigurationID(Object plan, JarFile module, ModuleIDBuilder idBuilder) throws IOException , 145 DeploymentException { 146 Descriptor descriptor = (Descriptor) plan; 147 if (descriptor.getComponent() != null) { 148 return new Artifact("servicemix-components", descriptor.getComponent().getIdentification().getName(), 149 (Version) null, "car"); 150 } else if (descriptor.getServiceAssembly() != null) { 151 return new Artifact("servicemix-assemblies", descriptor.getServiceAssembly().getIdentification().getName(), 152 (Version) null, "car"); 153 } else if (descriptor.getSharedLibrary() != null) { 154 return new Artifact("servicemix-libraries", descriptor.getSharedLibrary().getIdentification().getName(), 155 descriptor.getSharedLibrary().getVersion(), "car"); 156 } else { 157 throw new DeploymentException("Unable to construct configuration ID " + module.getName() 158 + ": unrecognized jbi package. Should be a component, assembly or library."); 159 } 160 } 161 162 178 public DeploymentContext buildConfiguration(boolean inPlaceDeployment, Artifact configId, Object plan, 179 JarFile jarFile, Collection configurationStores, ArtifactResolver artifactResolver, 180 ConfigurationStore targetConfigurationStore) throws IOException , DeploymentException { 181 if (plan == null) { 182 log.warn("Expected a Descriptor but received null"); 183 return null; 184 } 185 if (plan instanceof Descriptor == false) { 186 log.warn("Expected a Descriptor but received a " + plan.getClass().getName()); 187 return null; 188 } 189 File configurationDir; 190 try { 191 configurationDir = targetConfigurationStore.createNewConfigurationDir(configId); 192 } catch (ConfigurationAlreadyExistsException e) { 193 throw new DeploymentException(e); 194 } 195 196 Environment environment = new Environment(); 197 environment.setConfigId(configId); 198 EnvironmentBuilder.mergeEnvironments(environment, defaultEnvironment); 199 200 DeploymentContext context = null; 201 try { 202 Descriptor descriptor = (Descriptor) plan; 203 context = new DeploymentContext(configurationDir, 204 inPlaceDeployment ? DeploymentUtil.toFile(jarFile) : null, environment, 205 ConfigurationModuleType.SERVICE, kernel.getNaming(), ConfigurationUtil 206 .getConfigurationManager(kernel), repositories); 207 if (descriptor.getComponent() != null) { 208 buildComponent(descriptor, context, jarFile); 209 } else if (descriptor.getServiceAssembly() != null) { 210 buildServiceAssembly(descriptor, context, jarFile); 211 } else if (descriptor.getSharedLibrary() != null) { 212 buildSharedLibrary(descriptor, context, jarFile); 213 } else { 214 throw new IllegalStateException ("Invalid jbi descriptor"); 215 } 216 } catch (Exception e) { 217 if (context != null) { 218 context.close(); 219 } 220 DeploymentUtil.recursiveDelete(configurationDir); 221 throw new DeploymentException("Unable to deploy", e); 222 } 223 224 return context; 225 } 226 227 protected void buildComponent(Descriptor descriptor, DeploymentContext context, JarFile module) throws Exception { 228 Environment environment = context.getConfiguration().getEnvironment(); 229 File targetDir = new File (context.getBaseDir(), "install"); 231 targetDir.mkdirs(); 232 unzip(context, module, new URI ("install/")); 233 File workDir = new File (context.getBaseDir(), "workspace"); 235 workDir.mkdirs(); 236 if ("self-first".equals(descriptor.getComponent().getComponentClassLoaderDelegation())) { 240 context.getConfiguration().getEnvironment().setInverseClassLoading(true); 241 } 242 SharedLibraryList[] slList = descriptor.getComponent().getSharedLibraries(); 243 if (slList != null) { 244 for (int i = 0; i < slList.length; i++) { 245 Artifact sl = new Artifact("servicemix-libraries", slList[i].getName(), slList[i].getVersion(), "car"); 246 environment.addDependency(sl, ImportType.CLASSES); 247 } 248 } 249 if (descriptor.getComponent().getComponentClassPath() != null) { 250 String [] pathElements = descriptor.getComponent().getComponentClassPath().getPathElements(); 251 if (pathElements != null) { 252 for (int i = 0; i < pathElements.length; i++) { 253 File include = new File (targetDir, pathElements[i]); 256 File temp = new File (workDir, pathElements[i]); 257 if (!include.isFile()) { 258 throw new Exception ("Classpath element '" + pathElements[i] + "' not found"); 259 } 260 temp.getParentFile().mkdirs(); 261 include.renameTo(temp); 262 context.addInclude(new URI ("install/").resolve(pathElements[i]), temp); 263 temp.delete(); 264 } 265 } 266 } 267 Properties props = new Properties (); 269 props.put("jbiType", "JBIComponent"); 270 props.put("name", descriptor.getComponent().getIdentification().getName()); 271 AbstractName name = new AbstractName(environment.getConfigId(), props); 272 GBeanData gbeanData = new GBeanData(name, Component.GBEAN_INFO); 273 gbeanData.setAttribute("name", descriptor.getComponent().getIdentification().getName()); 274 gbeanData.setAttribute("description", descriptor.getComponent().getIdentification().getDescription()); 275 gbeanData.setAttribute("type", descriptor.getComponent().getType()); 276 gbeanData.setAttribute("className", descriptor.getComponent().getComponentClassName()); 277 gbeanData.setReferencePattern("container", getContainerObjectName()); 278 context.addGBean(gbeanData); 279 } 280 281 protected void buildServiceAssembly(Descriptor descriptor, DeploymentContext context, JarFile module) 282 throws Exception { 283 Environment environment = context.getConfiguration().getEnvironment(); 284 File targetDir = new File (context.getBaseDir(), "install"); 286 targetDir.mkdirs(); 287 unzip(context, module, new URI ("install/")); 288 ServiceUnit[] sus = descriptor.getServiceAssembly().getServiceUnits(); 290 for (int i = 0; i < sus.length; i++) { 291 String name = sus[i].getIdentification().getName(); 292 String zip = sus[i].getTarget().getArtifactsZip(); 293 String comp = sus[i].getTarget().getComponentName(); 294 unzip(context, new JarFile (new File (targetDir, zip)), new URI ("sus/" + comp + "/" + name + "/")); 295 Artifact sl = new Artifact("servicemix-components", comp, (Version) null, "car"); 299 environment.addDependency(sl, ImportType.ALL); 300 } 301 Properties props = new Properties (); 303 props.put("jbiType", "JBIServiceAssembly"); 304 props.put("name", descriptor.getServiceAssembly().getIdentification().getName()); 305 AbstractName name = new AbstractName(environment.getConfigId(), props); 306 GBeanData gbeanData = new GBeanData(name, ServiceAssembly.GBEAN_INFO); 307 gbeanData.setAttribute("name", descriptor.getServiceAssembly().getIdentification().getName()); 308 gbeanData.setReferencePattern("container", getContainerObjectName()); 309 for (int i = 0; i < sus.length; i++) { 310 String comp = sus[i].getTarget().getComponentName(); 311 gbeanData.addDependency(getComponentName(comp)); 312 } 313 context.addGBean(gbeanData); 314 } 315 316 protected void buildSharedLibrary(Descriptor descriptor, DeploymentContext context, JarFile module) 317 throws Exception { 318 Environment environment = context.getConfiguration().getEnvironment(); 319 File targetDir = new File (context.getBaseDir(), "install"); 321 targetDir.mkdirs(); 322 unzip(context, module, new URI ("install/")); 323 File workDir = new File (context.getBaseDir(), "workspace"); 325 workDir.mkdirs(); 326 if ("self-first".equals(descriptor.getSharedLibrary().getClassLoaderDelegation())) { 328 context.getConfiguration().getEnvironment().setInverseClassLoading(true); 329 } 330 if (descriptor.getSharedLibrary().getSharedLibraryClassPath() != null) { 331 String [] pathElements = descriptor.getSharedLibrary().getSharedLibraryClassPath().getPathElements(); 332 if (pathElements != null) { 333 for (int i = 0; i < pathElements.length; i++) { 334 log.debug("Processing pathElements[" + i + "]: " + pathElements[i]); 335 File include = new File (targetDir, pathElements[i]); 338 File temp = new File (workDir, pathElements[i]); 339 if (!include.isFile()) { 340 throw new Exception ("Classpath element '" + pathElements[i] + "' not found"); 341 } 342 temp.getParentFile().mkdirs(); 343 include.renameTo(temp); 344 context.addInclude(new URI ("install/").resolve(pathElements[i]), temp); 345 temp.delete(); 346 } 347 } else { 348 log.debug("SharedLibrary().getSharedLibraryClassPath().getPathElements() is null"); 349 } 350 } else { 351 log.debug("SharedLibrary().getSharedLibraryClassPath() is null"); 352 } 353 Properties props = new Properties (); 355 props.put("jbiType", "JBISharedLibrary"); 356 props.put("name", descriptor.getSharedLibrary().getIdentification().getName()); 357 AbstractName name = new AbstractName(environment.getConfigId(), props); 358 GBeanData gbeanData = new GBeanData(name, SharedLibrary.GBEAN_INFO); 359 gbeanData.setAttribute("name", descriptor.getSharedLibrary().getIdentification().getName()); 360 gbeanData.setAttribute("description", descriptor.getSharedLibrary().getIdentification().getDescription()); 361 gbeanData.setReferencePattern("container", getContainerObjectName()); 362 context.addGBean(gbeanData); 363 } 364 365 protected void unzip(DeploymentContext context, JarFile module, URI targetUri) throws IOException { 366 Enumeration entries = module.entries(); 367 while (entries.hasMoreElements()) { 368 ZipEntry entry = (ZipEntry ) entries.nextElement(); 369 URI target = targetUri.resolve(entry.getName()); 370 context.addFile(target, module, entry); 371 } 372 } 373 374 protected AbstractName getContainerObjectName() { 375 AbstractNameQuery query = new AbstractNameQuery(Container.class.getName()); 376 Set names = kernel.listGBeans(query); 377 return (AbstractName) names.iterator().next(); 378 } 379 380 protected AbstractNameQuery getComponentName(String name) throws MalformedObjectNameException { 381 URI uri = URI.create("servicemix-components/" + name + "//car?jbiType=JBIComponent"); 382 return new AbstractNameQuery(uri); 383 } 384 385 } 386 | Popular Tags |