KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > portal > server > deployment > WebAppIntercepter


1 /*****************************************
2  * *
3  * JBoss Portal: The OpenSource Portal *
4  * *
5  * Distributable under LGPL license. *
6  * See terms of license at gnu.org. *
7  * *
8  *****************************************/

9
10 package org.jboss.portal.server.deployment;
11
12 import java.net.URL JavaDoc;
13 import java.util.ArrayList JavaDoc;
14 import java.util.Collection JavaDoc;
15 import java.util.Collections JavaDoc;
16 import java.util.HashMap JavaDoc;
17 import java.util.Iterator JavaDoc;
18 import java.util.Map JavaDoc;
19
20 import javax.management.Notification JavaDoc;
21 import javax.management.NotificationListener JavaDoc;
22 import javax.management.ObjectName JavaDoc;
23
24 import org.jboss.deployment.DeploymentInfo;
25 import org.jboss.deployment.SubDeployer;
26 import org.jboss.portal.server.util.Service;
27 import org.jboss.web.WebApplication;
28
29 /**
30  * Relay web deployments. When a web deployment occurs, it is abstracted
31  * into a PortalWebApp object that provides a consistent way to getPortalObjectContext informations
32  * and modify the web application.
33  *
34  * When this service stops it does not send undeployment notifications, therefore
35  * it is up to the client of this service to perform any cleanup task associated
36  * to a deployment web application. The purpose of this is that most of the time
37  * clients of this service will be stopped before this one and they would
38  * receive undeployments in a not started state.
39  *
40  * @author <a HREF="mailto:julien@jboss.org">Julien Viet</a>
41  * @version $Revision: 1.2 $
42  */

43 public abstract class WebAppIntercepter
44    extends Service
45    implements NotificationListener JavaDoc
46 {
47
48    /** WARDeployer. */
49    private ObjectName JavaDoc interceptedDeployer;
50
51    /** A copy of the WARDeployer used for notification subscription removal. */
52    private ObjectName JavaDoc currentInterceptedDeployer;
53
54    /** The current deployements. */
55    private Map JavaDoc deployments;
56
57    /** The factory creating the portal web app objects. */
58    private PortalWebAppFactory factory;
59
60    /**
61     *
62     */

63    public WebAppIntercepter()
64    {
65       deployments = Collections.synchronizedMap(new HashMap JavaDoc());
66    }
67
68    /**
69     * @jmx.managed-attribute
70     *
71     * Set the deployer on this service.
72     */

73    public void setInterceptedDeployer(ObjectName JavaDoc interceptedDeployer)
74    {
75       this.interceptedDeployer = interceptedDeployer;
76    }
77
78    /**
79     * @jmx.managed-attribute
80     *
81     * Return the intercepted deployer.
82     */

83    public ObjectName JavaDoc getInterceptedDeployer()
84    {
85       return interceptedDeployer;
86    }
87
88    /**
89     * @jmx.managed-attribute
90     * access="read-only"
91     *
92     * Clone and return the deployed URLs.
93     */

94    public Collection JavaDoc getDeployedURLs()
95    {
96       return new ArrayList JavaDoc(deployments.keySet());
97    }
98
99    /**
100     * Only take care of start notifications.
101     */

102    public void handleNotification(Notification JavaDoc notification, Object JavaDoc handback)
103    {
104       String JavaDoc type = notification.getType();
105       boolean start = SubDeployer.START_NOTIFICATION.equals(type);
106       boolean stop = SubDeployer.STOP_NOTIFICATION.equals(type);
107
108       // Do we do something ?
109
if (start || stop)
110       {
111          // The previous loader
112
ClassLoader JavaDoc previousLoader = Thread.currentThread().getContextClassLoader();
113
114          try
115          {
116             // This call is coming with the MainDeployer classloader as context loader
117
// For this call we change the context loader to the container one.
118
ClassLoader JavaDoc nextLoader = server.getClassLoaderFor(getServiceName());
119             Thread.currentThread().setContextClassLoader(nextLoader);
120
121             // Create the portal web app
122
DeploymentInfo info = (DeploymentInfo)notification.getUserData();
123
124             if (start)
125             {
126                // Get all the deployed web applications, our must be among these
127
Iterator JavaDoc iterator = (Iterator JavaDoc)server.getAttribute(interceptedDeployer, "DeployedApplications");
128                while (iterator.hasNext())
129                {
130                   WebApplication webApp = (WebApplication)iterator.next();
131                   if (info == webApp.getDeploymentInfo())
132                   {
133                      PortalWebApp pwa = factory.create(webApp);
134                      URL JavaDoc url = info.url;
135                      deployments.put(url, pwa);
136                      log.debug("Seen URL " + url + " about to deploy");
137                      deploy(pwa);
138                   }
139                }
140             }
141             if (stop)
142             {
143                // Look if we have something for that url
144
PortalWebApp pwa = (PortalWebApp)deployments.remove(info.url);
145
146                // Notify
147
if (pwa != null)
148                {
149                   undeploy(pwa);
150                }
151             }
152          }
153          catch(Exception JavaDoc e)
154          {
155             log.error("Cannot handle the intercepted deployment", e);
156          }
157          finally
158          {
159             // Put previous context loader back
160
Thread.currentThread().setContextClassLoader(previousLoader);
161          }
162       }
163    }
164
165    /**
166     * Start listening to the deployer notifications.
167     */

168    protected void startService() throws Exception JavaDoc
169    {
170       if (interceptedDeployer != null)
171       {
172          // Create factory
173
factory = new PortalWebAppFactory(server);
174
175          // Copy the name for the stop phase
176
currentInterceptedDeployer = interceptedDeployer;
177
178          // Register to notifications
179
log.debug("Start listening notifications from intercepted deployer" + currentInterceptedDeployer);
180          server.addNotificationListener(currentInterceptedDeployer, getServiceName(), null, null);
181
182          // Scans all the previously deployed applications
183
Iterator JavaDoc iterator = (Iterator JavaDoc)server.getAttribute(currentInterceptedDeployer, "DeployedApplications");
184          log.debug("Scan previously deployed web applications");
185          while (iterator.hasNext())
186          {
187             WebApplication webApp = (WebApplication)iterator.next();
188             if (!deployments.containsKey(webApp.getURL()))
189             {
190                PortalWebApp pwa = factory.create(webApp);
191                deployments.put(pwa.getURL(), pwa);
192                log.debug("Seen URL " + pwa.getURL() + " about to deploy");
193                deploy(pwa);
194             }
195          }
196
197       }
198       else
199       {
200          throw new Exception JavaDoc("No intercepted deployer name present");
201       }
202    }
203
204    /**
205     * Stop listening to the deployer notifications.
206     */

207    protected void stopService() throws Exception JavaDoc
208    {
209       if (currentInterceptedDeployer != null)
210       {
211          // Remove all previously deployed applications
212
for (Iterator JavaDoc i = deployments.values().iterator();i.hasNext();)
213          {
214             PortalWebApp pwa = (PortalWebApp)i.next();
215             i.remove();
216             log.debug("Removing URL " + pwa.getURL());
217             undeploy(pwa);
218          }
219
220          // Do not listen notifications anymore
221
log.debug("Stop listening notifications from intercepted deployer" + currentInterceptedDeployer);
222          server.removeNotificationListener(currentInterceptedDeployer, getServiceName());
223
224          // Remove factory
225
factory = null;
226
227          //
228
currentInterceptedDeployer = null;
229       }
230    }
231
232    /**
233     * Perform the deploy notification.
234     */

235    protected abstract void deploy(PortalWebApp pwa);
236
237    /**
238     * Perform the undeploy notification.
239     */

240    protected abstract void undeploy(PortalWebApp pwa);
241 }
242
Popular Tags