1 package org.apache.geronimo.deployment.mavenplugin; 2 3 import java.io.IOException ; 4 import java.net.JarURLConnection ; 5 import java.net.URL ; 6 import java.net.URLClassLoader ; 7 import java.util.StringTokenizer ; 8 import java.util.jar.Attributes ; 9 import javax.enterprise.deploy.shared.factories.DeploymentFactoryManager ; 10 import javax.enterprise.deploy.spi.factories.DeploymentFactory ; 11 import javax.enterprise.deploy.spi.status.ProgressObject ; 12 import javax.enterprise.deploy.spi.status.ProgressListener ; 13 import javax.enterprise.deploy.spi.status.ProgressEvent ; 14 import javax.enterprise.deploy.spi.status.DeploymentStatus ; 15 16 public class DeploymentClient { 17 private static final DeploymentFactoryManager FACTORY_MANAGER = DeploymentFactoryManager.getInstance(); 18 19 private URL provider; 20 21 public URL getProvider() { 22 return provider; 23 } 24 25 public void setProvider(URL provider) { 26 this.provider = provider; 27 } 28 29 public void doIt() throws IOException { 30 registerProvider(provider, null); 31 } 32 33 public static void registerProvider(URL provider, ClassLoader parent) throws IOException { 34 if (parent == null) { 35 parent = Thread.currentThread().getContextClassLoader(); 36 } 37 if (parent == null) { 38 parent = DeploymentClient.class.getClassLoader(); 39 } 40 41 URL url = new URL ("jar:" + provider.toString() + "!/"); 43 JarURLConnection jarConnection = (JarURLConnection ) url.openConnection(); 44 Attributes attrs = jarConnection.getMainAttributes(); 45 String factoryNames = attrs.getValue("J2EE-DeploymentFactory-Implementation-Class"); 46 if (factoryNames == null) { 47 throw new IOException ("No DeploymentFactory found in jar"); 48 } 49 50 ClassLoader cl = new URLClassLoader (new URL []{provider}, parent); 52 for (StringTokenizer tokenizer = new StringTokenizer (factoryNames); tokenizer.hasMoreTokens();) { 53 String className = tokenizer.nextToken(); 54 try { 55 DeploymentFactory factory = (DeploymentFactory ) cl.loadClass(className).newInstance(); 56 FACTORY_MANAGER.registerDeploymentFactory(factory); 57 } catch (Exception e) { 58 throw (IOException ) new IOException ("Unable to instantiate DeploymentFactory: " + className).initCause(e); 59 } 60 } 61 } 62 63 public static void waitFor(final ProgressObject progress) throws InterruptedException { 64 ProgressListener listener = new ProgressListener () { 65 public void handleProgressEvent(ProgressEvent event) { 66 DeploymentStatus status = event.getDeploymentStatus(); 67 if (status.getMessage() != null) { 68 System.out.println(status.getMessage()); 69 } 70 if (!status.isRunning()) { 71 synchronized (progress) { 72 progress.notify(); 73 } 74 } 75 } 76 }; 77 progress.addProgressListener(listener); 78 synchronized (progress) { 79 while (progress.getDeploymentStatus().isRunning()) { 80 progress.wait(); 81 } 82 } 83 } 84 } 85 | Popular Tags |