KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > geronimo > deployment > mavenplugin > DeploymentClient


1 package org.apache.geronimo.deployment.mavenplugin;
2
3 import java.io.IOException JavaDoc;
4 import java.net.JarURLConnection JavaDoc;
5 import java.net.URL JavaDoc;
6 import java.net.URLClassLoader JavaDoc;
7 import java.util.StringTokenizer JavaDoc;
8 import java.util.jar.Attributes JavaDoc;
9 import javax.enterprise.deploy.shared.factories.DeploymentFactoryManager JavaDoc;
10 import javax.enterprise.deploy.spi.factories.DeploymentFactory JavaDoc;
11 import javax.enterprise.deploy.spi.status.ProgressObject JavaDoc;
12 import javax.enterprise.deploy.spi.status.ProgressListener JavaDoc;
13 import javax.enterprise.deploy.spi.status.ProgressEvent JavaDoc;
14 import javax.enterprise.deploy.spi.status.DeploymentStatus JavaDoc;
15
16 public class DeploymentClient {
17     private static final DeploymentFactoryManager JavaDoc FACTORY_MANAGER = DeploymentFactoryManager.getInstance();
18
19     private URL JavaDoc provider;
20
21     public URL JavaDoc getProvider() {
22         return provider;
23     }
24
25     public void setProvider(URL JavaDoc provider) {
26         this.provider = provider;
27     }
28
29     public void doIt() throws IOException JavaDoc {
30         registerProvider(provider, null);
31     }
32
33     public static void registerProvider(URL JavaDoc provider, ClassLoader JavaDoc parent) throws IOException JavaDoc {
34         if (parent == null) {
35             parent = Thread.currentThread().getContextClassLoader();
36         }
37         if (parent == null) {
38             parent = DeploymentClient.class.getClassLoader();
39         }
40
41         // read manifest entry from provider
42
URL JavaDoc url = new URL JavaDoc("jar:" + provider.toString() + "!/");
43         JarURLConnection JavaDoc jarConnection = (JarURLConnection JavaDoc) url.openConnection();
44         Attributes JavaDoc attrs = jarConnection.getMainAttributes();
45         String JavaDoc factoryNames = attrs.getValue("J2EE-DeploymentFactory-Implementation-Class");
46         if (factoryNames == null) {
47             throw new IOException JavaDoc("No DeploymentFactory found in jar");
48         }
49
50         // register listed DeploymentFactories
51
ClassLoader JavaDoc cl = new URLClassLoader JavaDoc(new URL JavaDoc[]{provider}, parent);
52         for (StringTokenizer JavaDoc tokenizer = new StringTokenizer JavaDoc(factoryNames); tokenizer.hasMoreTokens();) {
53             String JavaDoc className = tokenizer.nextToken();
54             try {
55                 DeploymentFactory JavaDoc factory = (DeploymentFactory JavaDoc) cl.loadClass(className).newInstance();
56                 FACTORY_MANAGER.registerDeploymentFactory(factory);
57             } catch (Exception JavaDoc e) {
58                 throw (IOException JavaDoc) new IOException JavaDoc("Unable to instantiate DeploymentFactory: " + className).initCause(e);
59             }
60         }
61     }
62
63     public static void waitFor(final ProgressObject JavaDoc progress) throws InterruptedException JavaDoc {
64         ProgressListener JavaDoc listener = new ProgressListener JavaDoc() {
65             public void handleProgressEvent(ProgressEvent JavaDoc event) {
66                 DeploymentStatus JavaDoc 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