KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > deployment > cache > DeploymentCache


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

9
10 package org.jboss.deployment.cache;
11
12 import java.net.URL JavaDoc;
13
14 import javax.management.ObjectName JavaDoc;
15 import javax.management.MBeanServer JavaDoc;
16
17 import org.jboss.system.ServiceMBeanSupport;
18 import org.jboss.system.MissingAttributeException;
19
20 import org.jboss.deployment.Deployer;
21 import org.jboss.deployment.DeploymentException;
22
23 import org.jboss.util.NullArgumentException;
24 import org.jboss.mx.util.MBeanProxyExt;
25 import org.jboss.mx.util.MBeanProxyInstance;
26
27 /**
28  * A Deployer-like service which intercepts deploy/undeploy calls
29  * to MainDeployer and provides local caching of target URLs using
30  * local disk.
31  *
32  * @jmx:mbean extends="org.jboss.system.ServiceMBean, org.jboss.deployment.DeployerMBean"
33  *
34  * @todo clean up stale cache members
35  *
36  * @version <tt>$Revision: 1.8 $</tt>
37  * @author <a HREF="mailto:jason@planet57.com">Jason Dillon</a>
38  */

39 public class DeploymentCache
40    extends ServiceMBeanSupport
41    implements Deployer, DeploymentCacheMBean
42 {
43    /** A proxy to the deployer we are using. */
44    protected Deployer deployer;
45
46    /** A proxy to the deployment store we are using. */
47    protected DeploymentStore store;
48
49
50    /////////////////////////////////////////////////////////////////////////
51
// Pluggables //
52
/////////////////////////////////////////////////////////////////////////
53

54    /**
55     * @jmx:managed-attribute
56     */

57    public void setDeployer(final ObjectName JavaDoc deployerName)
58    {
59       if (deployerName == null)
60          throw new NullArgumentException("deployerName");
61
62       deployer = (Deployer)
63          MBeanProxyExt.create(Deployer.class, deployerName, server);
64    }
65
66    /**
67     * @jmx:managed-attribute
68     */

69    public ObjectName JavaDoc getDeployer()
70    {
71       return ((MBeanProxyInstance)deployer).getMBeanProxyObjectName();
72    }
73
74    /**
75     * @jmx:managed-attribute
76     */

77    public void setStore(final ObjectName JavaDoc storeName)
78    {
79       if (storeName == null)
80          throw new NullArgumentException("storeName");
81
82       store = (DeploymentStore)
83          MBeanProxyExt.create(DeploymentStore.class, storeName, server);
84    }
85
86    /**
87     * @jmx:managed-attribute
88     */

89    public ObjectName JavaDoc getStore()
90    {
91       return ((MBeanProxyInstance)store).getMBeanProxyObjectName();
92    }
93
94
95    /////////////////////////////////////////////////////////////////////////
96
// Deployer //
97
/////////////////////////////////////////////////////////////////////////
98

99    protected boolean isInvalid(final URL JavaDoc orig, final URL JavaDoc stored)
100       throws Exception JavaDoc
101    {
102       boolean trace = log.isTraceEnabled();
103
104       long omod = orig.openConnection().getLastModified();
105       long smod = stored.openConnection().getLastModified();
106
107       if (trace) {
108          log.trace("Modfication times (orig, stored): " + omod + ", " + smod);
109       }
110       
111       return omod > smod;
112    }
113
114    public void deploy(final URL JavaDoc url) throws DeploymentException
115    {
116       boolean debug = log.isDebugEnabled();
117
118       try {
119          URL JavaDoc storedURL = store.get(url);
120          if (storedURL != null) {
121             // it's in the cache, is it still valid ?
122

123             if (isInvalid(url, storedURL)) {
124                // the stored version is old, get the new version
125
log.info("Cached deployment is invalid; refreshing store for URL: " + url);
126                storedURL = store.put(url);
127             }
128             else {
129                if (debug) {
130                   log.debug("Using cached deployment URL: " + storedURL);
131                }
132             }
133          }
134          else {
135             // not in the cache, put it there
136
log.info("Deployment not in cache; adding URL to store: " + url);
137             storedURL = store.put(url);
138          }
139
140          // invoke the chained deployer with the stored URL
141
deployer.deploy(storedURL);
142       }
143       catch (Exception JavaDoc e) {
144          throw new DeploymentException(e);
145       }
146    }
147
148    public void undeploy(final URL JavaDoc url) throws DeploymentException
149    {
150       boolean debug = log.isDebugEnabled();
151
152       try {
153          URL JavaDoc storedURL = store.get(url);
154          if (storedURL != null) {
155             // invoke undeploy on target deployer using local cache url
156
deployer.undeploy(storedURL);
157          }
158          else {
159             if (debug) {
160                log.debug("Not found in store; ignoring URL: " + url);
161             }
162          }
163       }
164       catch (Exception JavaDoc e) {
165          throw new DeploymentException(e);
166       }
167    }
168
169    public boolean isDeployed(final URL JavaDoc url)
170    {
171       try {
172          URL JavaDoc storedURL = store.get(url);
173
174          // if the stored url is not null then ask the target deployer
175
// else it is not deployed
176
return storedURL != null && deployer.isDeployed(url);
177       }
178       catch (Exception JavaDoc e) {
179          return false;
180       }
181    }
182
183
184    /////////////////////////////////////////////////////////////////////////
185
// Service/ServiceMBeanSupport //
186
/////////////////////////////////////////////////////////////////////////
187

188    protected void createService() throws Exception JavaDoc
189    {
190       // create stale deployemnt timer/scanner/whatever
191
}
192    
193    protected void startService() throws Exception JavaDoc
194    {
195       if (deployer == null)
196          throw new MissingAttributeException("Deployer");
197       if (store == null)
198          throw new MissingAttributeException("Store");
199
200       // start stale deployemnt timer/scanner/whatever
201
}
202    
203    protected void stopService() throws Exception JavaDoc
204    {
205       // stop stale deployemnt timer/scanner/whatever
206
}
207    
208    protected void destroyService() throws Exception JavaDoc
209    {
210       deployer = null;
211       store = null;
212    }
213 }
214
Popular Tags