KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > web > tomcat > tc6 > session > JBossCacheCluster


1 /*
2 * JBoss, Home of Professional Open Source
3 * Copyright 2005, JBoss Inc., and individual contributors as indicated
4 * by the @authors tag. See the copyright.txt in the distribution for a
5 * full listing of individual contributors.
6 *
7 * This is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU Lesser General Public License as
9 * published by the Free Software Foundation; either version 2.1 of
10 * the License, or (at your option) any later version.
11 *
12 * This software is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this software; if not, write to the Free
19 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21 */

22 package org.jboss.web.tomcat.tc6.session;
23
24 import java.io.File JavaDoc;
25 import java.io.FileInputStream JavaDoc;
26 import java.io.FileNotFoundException JavaDoc;
27 import java.io.IOException JavaDoc;
28 import java.io.InputStream JavaDoc;
29 import java.net.URL JavaDoc;
30 import java.util.ArrayList JavaDoc;
31
32 import javax.management.MBeanServer JavaDoc;
33 import javax.management.MBeanServerFactory JavaDoc;
34 import javax.management.ObjectName JavaDoc;
35
36 import org.apache.catalina.Container;
37 import org.apache.catalina.Engine;
38 import org.apache.catalina.Host;
39 import org.apache.catalina.Lifecycle;
40 import org.apache.catalina.LifecycleException;
41 import org.apache.catalina.LifecycleListener;
42 import org.apache.catalina.Manager;
43 import org.apache.catalina.core.ContainerBase;
44 import org.apache.catalina.util.LifecycleSupport;
45 import org.apache.commons.logging.Log;
46 import org.apache.commons.logging.LogFactory;
47 import org.jboss.cache.BatchModeTransactionManagerLookup;
48 import org.jboss.cache.config.Configuration;
49 import org.jboss.cache.factories.XmlConfigurationParser;
50 import org.jboss.cache.pojo.PojoCacheFactory;
51 import org.jboss.cache.pojo.jmx.PojoCacheJmxWrapper;
52 import org.jboss.cache.pojo.jmx.PojoCacheJmxWrapperMBean;
53 import org.jboss.mx.util.MBeanProxyExt;
54 import org.jboss.mx.util.MBeanServerLocator;
55
56 /**
57  * A Tomcat <code>Cluster</code> implementation that uses a JBoss
58  * <code>TreeCache</code> to support intra-cluster session replication.
59  * <p>
60  * This class registers a <code>TreeCache</code> in JMX, making it
61  * available to other users who wish to replicate data within the cluster.
62  * </p>
63  *
64  * @author Brian Stansberry
65  * @version $Revision: 58588 $
66  */

67 public class JBossCacheCluster
68    implements JBossCacheClusterMBean, Lifecycle
69 {
70    // ------------------------------------------------------- Static Fields
71

72    protected static final String JavaDoc info = "JBossCacheCluster/2.1";
73
74    public static Log log = LogFactory.getLog(JBossCacheCluster.class);
75
76    public static final String JavaDoc DEFAULT_CLUSTER_NAME = "Tomcat-Cluster";
77
78    /** TreeCache's isolation level */
79    public static final String JavaDoc DEFAULT_ISOLATION_LEVEL = "REPEATABLE_READ";
80    
81    /** TreeCache's cache mode */
82    public static final String JavaDoc DEFAULT_CACHE_MODE = "REPL_ASYNC";
83    
84    /** TreeCache's lock aquisition timeout */
85    public static final long DEFAULT_LOCK_TIMEOUT = 15000;
86    
87    /** TransactionManagerLookup implementation that the TreeCache should use. */
88    public static final String JavaDoc DEFAULT_TM_LOOKUP =
89       BatchModeTransactionManagerLookup.class.getName();
90
91    public static final String JavaDoc DEFAULT_CACHE_CONFIG_PATH = "conf/cluster-cache.xml";
92    
93    // ------------------------------------------------------- Instance Fields
94

95    /** Parent container of this cluster. */
96    private Container container = null;
97    
98    /** Our JMX Server. */
99    private MBeanServer JavaDoc mserver = null;
100    
101    /** Name under which we are registered in JMX */
102    private ObjectName JavaDoc objectName = null;
103    
104    /** Are we started? */
105    private boolean started = false;
106
107    /** The lifecycle event support for this component. */
108    private LifecycleSupport lifecycle = new LifecycleSupport(this);
109
110    /** Our tree cache */
111    private PojoCacheJmxWrapperMBean pojoCache = null;
112    
113    /** Name under which our TreeCache is registered in JMX */
114    private String JavaDoc pojoCacheObjectName = "jboss.cache:service=TomcatClusteringCache";
115    
116    /** Did we create the tree cache, or was it already registered in JMX? */
117    private boolean pojoCacheLocal = false;
118    
119    /** Name of the tree cache's JGroups channel */
120    private String JavaDoc clusterName = null;
121    
122    /** File name, URL or String to use to configure JGroups. */
123    private String JavaDoc cacheConfigPath = null;
124    
125    /**
126     * Implementation of Manager to instantiate when
127     * createManager() is called.
128     */

129    private String JavaDoc managerClassName = JBossCacheManager.class.getName();
130
131    /** Does the Engine in which we are running use mod_jk? */
132    private boolean useJK = false;
133    
134    /** Whether our Managers should use a local cache. */
135    private boolean useLocalCache = false;
136
137    /**
138     * Default replication trigger to assign to our
139     * Managers that haven't had this property set.
140     */

141    private String JavaDoc defaultReplicationTrigger = null;
142
143    /**
144     * Default replication granularity to assign to our Managers
145     * that haven't had this property set.
146     */

147    private String JavaDoc defaultReplicationGranularity = null;
148    
149    /**
150     * JBossCacheManager's snapshot mode.
151     */

152    private String JavaDoc snapshotMode = null;
153    
154    /**
155     * JBossCacheManager's snapshot interval.
156     */

157    private int snapshotInterval = 0;
158    
159    /** Whether we use batch mode replication for field level granularity */
160    private boolean replicationFieldBatchMode;
161    
162    // ---------------------------------------------------------- Constructors
163

164    /**
165     * Default constructor.
166     */

167    public JBossCacheCluster()
168    {
169       super();
170    }
171
172    // ------------------------------------------------------------ Properties
173

174    /**
175     * Gets a String representation of the JMX <code>ObjectName</code> under
176     * which our <code>TreeCache</code> is registered.
177     * <p>
178     * If this property is not explicitly set, the <code>TreeCache</code> will
179     * be registered under
180     * @{@link Tomcat6.DEFAULT_CACHE_NAME the default name used in
181     * embedded JBoss/Tomcat}.
182     * </p>
183     *
184     * @jmx.managed-attribute
185     */

186    public String JavaDoc getCacheObjectName()
187    {
188       return pojoCacheObjectName;
189    }
190
191    /**
192     * Sets the JMX <code>ObjectName</code> under which our
193     * <code>TreeCache</code> is registered, if already created, or under
194     * which it should be registered if this object creates it.
195     *
196     * @jmx.managed-attribute
197     */

198    public void setCacheObjectName(String JavaDoc objectName)
199    {
200       this.pojoCacheObjectName = objectName;
201    }
202
203    /**
204     * Sets the name of the <code>TreeCache</code>'s JGroups channel.
205     * <p>
206     * This property is ignored if a <code>TreeCache</code> is already
207     * registered under the provided
208     * {@link #setCacheObjectName cache object name}.
209     * </p>
210     *
211     * @jmx.managed-attribute
212     */

213    public void setClusterName(String JavaDoc clusterName)
214    {
215       this.clusterName = clusterName;
216    }
217
218    /**
219     * Gets the filesystem path, which can either be absolute or a path
220     * relative to <code>$CATALINA_BASE</code>, where a
221     * a JBossCache configuration file can be found.
222     *
223     * @return a path, either absolute or relative to
224     * <code>$CATALINA_BASE</code>. Will return
225     * <code>null</code> if no such path was configured.
226     *
227     * @jmx.managed-attribute
228     */

229    public String JavaDoc getCacheConfigPath()
230    {
231       return cacheConfigPath;
232    }
233
234    /**
235     * Sets the filesystem path, which can either be absolute or a path
236     * relative to <code>$CATALINA_BASE</code>, where a
237     * a JBossCache configuration file can be found.
238     * <p>
239     * This property is ignored if a <code>TreeCache</code> is already
240     * registered under the provided
241     * {@link #setCacheObjectName cache object name}.
242     * </p>
243     *
244     * @param cacheConfigPath a path, absolute or relative to
245     * <code>$CATALINA_BASE</code>,
246     * pointing to a JBossCache configuration file.
247     *
248     * @jmx.managed-attribute
249     */

250    public void setCacheConfigPath(String JavaDoc cacheConfigPath)
251    {
252       this.cacheConfigPath = cacheConfigPath;
253    }
254
255    /**
256     * Gets the name of the implementation of Manager to instantiate when
257     * createManager() is called.
258     *
259     * @jmx.managed-attribute
260     */

261    public String JavaDoc getManagerClassName()
262    {
263       return managerClassName;
264    }
265
266    /**
267     * Sets the name of the implementation of Manager to instantiate when
268     * createManager() is called.
269     * <p>
270     * This should be {@link JBossCacheManager} (the default) or a subclass
271     * of it.
272     * </p>
273     *
274     * @jmx.managed-attribute
275     */

276    public void setManagerClassName(String JavaDoc managerClassName)
277    {
278       this.managerClassName = managerClassName;
279    }
280
281    public void registerManager(Manager arg0)
282    {
283       // TODO tie this into the managerClassName
284
}
285
286    public void removeManager(Manager arg0)
287    {
288       // TODO tie this into the managerClassName
289
}
290
291    /**
292     * Gets whether the <code>Engine</code> in which we are running
293     * uses <code>mod_jk</code>.
294     *
295     * @jmx.managed-attribute
296     */

297    public boolean isUseJK()
298    {
299       return useJK;
300    }
301
302    /**
303     * Sets whether the <code>Engine</code> in which we are running
304     * uses <code>mod_jk</code>.
305     *
306     * @jmx.managed-attribute
307     */

308    public void setUseJK(boolean useJK)
309    {
310       this.useJK = useJK;
311    }
312
313    /**
314     * Gets the <code>JBossCacheManager</code>'s <code>useLocalCache</code>
315     * property.
316     *
317     * @jmx.managed-attribute
318     */

319    public boolean isUseLocalCache()
320    {
321       return useLocalCache;
322    }
323
324    /**
325     * Sets the <code>JBossCacheManager</code>'s <code>useLocalCache</code>
326     * property.
327     *
328     * @jmx.managed-attribute
329     */

330    public void setUseLocalCache(boolean useLocalCache)
331    {
332       this.useLocalCache = useLocalCache;
333    }
334
335    /**
336     * Gets the default granularity of session data replicated across the
337     * cluster; i.e. whether the entire session should be replicated when
338     * replication is triggered, or only modified attributes.
339     * <p>
340     * The value of this property is applied to <code>Manager</code> instances
341     * that did not have an equivalent property explicitly set in
342     * <code>context.xml</code> or <code>server.xml</code>.
343     * </p>
344     *
345     * @jmx.managed-attribute
346     */

347    public String JavaDoc getDefaultReplicationGranularity()
348    {
349       return defaultReplicationGranularity;
350    }
351
352    /**
353     * Sets the granularity of session data replicated across the cluster.
354     * Valid values are:
355     * <ul>
356     * <li>SESSION</li>
357     * <li>ATTRIBUTE</li>
358     * <li>FIELD</li>
359     * </ul>
360     * @jmx.managed-attribute
361     */

362    public void setDefaultReplicationGranularity(
363          String JavaDoc defaultReplicationGranularity)
364    {
365       this.defaultReplicationGranularity = defaultReplicationGranularity;
366    }
367
368    /**
369     * Gets the type of operations on a <code>HttpSession</code> that
370     * trigger replication.
371     * <p>
372     * The value of this property is applied to <code>Manager</code> instances
373     * that did not have an equivalent property explicitly set in
374     * <code>context.xml</code> or <code>server.xml</code>.
375     * </p>
376     *
377     * @jmx.managed-attribute
378     */

379    public String JavaDoc getDefaultReplicationTrigger()
380    {
381       return defaultReplicationTrigger;
382    }
383
384    /**
385     * Sets the type of operations on a <code>HttpSession</code> that
386     * trigger replication. Valid values are:
387     * <ul>
388     * <li>SET_AND_GET</li>
389     * <li>SET_AND_NON_PRIMITIVE_GET</li>
390     * <li>SET</li>
391     * </ul>
392     *
393     * @jmx.managed-attribute
394     */

395    public void setDefaultReplicationTrigger(String JavaDoc defaultTrigger)
396    {
397       this.defaultReplicationTrigger = defaultTrigger;
398    }
399    
400    /**
401     * Gets whether Managers should use batch mode replication.
402     * Only meaningful if replication granularity is set to <code>FIELD</code>.
403     *
404     * @jmx.managed-attribute
405     */

406    public boolean getDefaultReplicationFieldBatchMode()
407    {
408       return replicationFieldBatchMode;
409    }
410    
411    /**
412     * Sets whether Managers should use batch mode replication.
413     * Only meaningful if replication granularity is set to <code>FIELD</code>.
414     *
415     * @jmx.managed-attribute
416     */

417    public void setDefaultReplicationFieldBatchMode(boolean replicationFieldBatchMode)
418    {
419       this.replicationFieldBatchMode = replicationFieldBatchMode;
420    }
421
422    /**
423     * Gets when sessions are replicated to the other nodes.
424     * The default value, "instant", synchronously replicates changes
425     * to the other nodes. In this case, the "SnapshotInterval" attribute
426     * is not used.
427     * The "interval" mode, in association with the "SnapshotInterval"
428     * attribute, indicates that Tomcat will only replicate modified
429     * sessions every "SnapshotInterval" miliseconds at most.
430     *
431     * @see #getSnapshotInterval()
432     *
433     * @jmx.managed-attribute
434     */

435    public String JavaDoc getSnapshotMode()
436    {
437       return snapshotMode;
438    }
439
440    /**
441     * Sets when sessions are replicated to the other nodes. Valid values are:
442     * <ul>
443     * <li>instant</li>
444     * <li>interval</li>
445     * </ul>
446     *
447     * @jmx.managed-attribute
448     */

449    public void setSnapshotMode(String JavaDoc snapshotMode)
450    {
451       this.snapshotMode = snapshotMode;
452    }
453
454    /**
455     * Gets how often session changes should be replicated to other nodes.
456     * Only relevant if property {@link #getSnapshotMode() snapshotMode} is
457     * set to <code>interval</code>.
458     *
459     * @return the number of milliseconds between session replications.
460     *
461     * @jmx.managed-attribute
462     */

463    public int getSnapshotInterval()
464    {
465       return snapshotInterval;
466    }
467
468    /**
469     * Sets how often session changes should be replicated to other nodes.
470     *
471     * @param snapshotInterval the number of milliseconds between
472     * session replications.
473     * @jmx.managed-attribute
474     */

475    public void setSnapshotInterval(int snapshotInterval)
476    {
477       this.snapshotInterval = snapshotInterval;
478    }
479    
480    // ---------------------------------------------------------------- Cluster
481

482    /**
483     * Gets the name of the <code>TreeCache</code>'s JGroups channel.
484     *
485     * @see org.apache.catalina.Cluster#getClusterName()
486     */

487    public String JavaDoc getClusterName()
488    {
489       return clusterName;
490    }
491    
492    /* (non-javadoc)
493     * @see org.apache.catalina.Cluster#getContainer()
494     */

495    public Container getContainer()
496    {
497       return container;
498    }
499    
500    /* (non-javadoc)
501     * @see org.apache.catalina.Cluster#setContainer()
502     */

503    public void setContainer(Container container)
504    {
505       this.container = container;
506    }
507
508    /**
509     * @see org.apache.catalina.Cluster#getInfo()
510     *
511     * @jmx.managed-attribute access="read-only"
512     */

513    public String JavaDoc getInfo()
514    {
515       return info;
516    }
517
518    /**
519     * @see org.apache.catalina.Cluster#createManager(java.lang.String)
520     */

521    public Manager createManager(String JavaDoc name)
522    {
523       if (log.isDebugEnabled())
524          log.debug("Creating ClusterManager for context " + name
525                + " using class " + getManagerClassName());
526       Manager manager = null;
527       try
528       {
529          manager = (Manager) getClass().getClassLoader().loadClass(
530                getManagerClassName()).newInstance();
531       }
532       catch (Exception JavaDoc x)
533       {
534          log.error("Unable to load class for replication manager", x);
535          manager = new JBossCacheManager();
536       }
537       finally
538       {
539          manager.setDistributable(true);
540       }
541       
542       if (manager instanceof JBossCacheManager)
543       {
544          configureManager((JBossCacheManager) manager);
545       }
546       
547       return manager;
548    }
549
550    /**
551     * Does nothing; tracking the status of other members of the cluster is
552     * provided by the JGroups layer.
553     *
554     * @see org.apache.catalina.Cluster#backgroundProcess()
555     */

556    public void backgroundProcess()
557    {
558       ; // no-op
559
}
560
561    // --------------------------------------------- Deprecated Cluster Methods
562

563    /**
564     * Returns <code>null</code>; method is deprecated.
565     *
566     * @return <code>null</code>, always.
567     *
568     * @see org.apache.catalina.Cluster#getProtocol()
569     */

570    public String JavaDoc getProtocol()
571    {
572       return null;
573    }
574    
575    /**
576     * Does nothing; method is deprecated.
577     *
578     * @see org.apache.catalina.Cluster#setProtocol(java.lang.String)
579     */

580    public void setProtocol(String JavaDoc protocol)
581    {
582       ; // no-op
583
}
584    
585    /**
586     * Does nothing; method is deprecated.
587     *
588     * @see org.apache.catalina.Cluster#startContext(java.lang.String)
589     */

590    public void startContext(String JavaDoc contextPath) throws IOException JavaDoc
591    {
592       ; // no-op
593
}
594    
595    /**
596     * Does nothing; method is deprecated.
597     *
598     * @see org.apache.catalina.Cluster#installContext(java.lang.String, java.net.URL)
599     */

600    public void installContext(String JavaDoc contextPath, URL JavaDoc war)
601    {
602       ; // no-op
603
}
604    
605    /**
606     * Does nothing; method is deprecated.
607     *
608     * @see org.apache.catalina.Cluster#stop(java.lang.String)
609     */

610    public void stop(String JavaDoc contextPath) throws IOException JavaDoc
611    {
612       ; // no-op
613
}
614
615    // --------------------------------------------------------- Public Methods
616

617    /**
618     * Sets the cluster-wide properties of a <code>Manager</code> to
619     * match those of this cluster. Does not override
620     * <code>Manager</code>-specific properties with cluster-wide defaults
621     * if the <code>Manager</code>-specfic properties have already been set.
622     */

623    public void configureManager(JBossCacheManager manager)
624    {
625       manager.setSnapshotMode(snapshotMode);
626       manager.setSnapshotInterval(snapshotInterval);
627       manager.setUseJK(useJK);
628       manager.setUseLocalCache(useLocalCache);
629       manager.setCacheObjectNameString(pojoCacheObjectName);
630       
631       // Only set replication attributes if they were not
632
// already set via a <Manager> element in an XML config file
633

634       if (manager.getReplicationGranularityString() == null)
635       {
636          manager.setReplicationGranularityString(defaultReplicationGranularity);
637       }
638       
639       if (manager.getReplicationTriggerString() == null)
640       {
641          manager.setReplicationTriggerString(defaultReplicationTrigger);
642       }
643       
644       if (manager.isReplicationFieldBatchMode() == null)
645       {
646          manager.setReplicationFieldBatchMode(replicationFieldBatchMode);
647       }
648    }
649
650    // --------------------------------------------------------------- Lifecyle
651

652    /**
653     * Finds or creates a {@link TreeCache}; if created, starts the
654     * cache and registers it with our JMX server.
655     *
656     * @see org.apache.catalina.Lifecycle#start()
657     */

658    public void start() throws LifecycleException
659    {
660       if (started)
661       {
662          throw new LifecycleException("Cluster already started");
663       }
664
665       // Notify our interested LifecycleListeners
666
lifecycle.fireLifecycleEvent(BEFORE_START_EVENT, this);
667       
668       try
669       {
670          // Tell the JBoss MBeanServerLocator utility
671
// that Tomcat's MBean server is 'jboss'
672
MBeanServerLocator.setJBoss(getMBeanServer());
673          
674          // Initialize the tree cache
675
PojoCacheJmxWrapperMBean cache = getTreeCache();
676
677          registerMBeans();
678          
679          if (pojoCacheLocal)
680          {
681             cache.create();
682             cache.start();
683          }
684
685          started = true;
686          
687          // Notify our interested LifecycleListeners
688
lifecycle.fireLifecycleEvent(AFTER_START_EVENT, this);
689
690       }
691       catch (LifecycleException e)
692       {
693          throw e;
694       }
695       catch (Exception JavaDoc e)
696       {
697          log.error("Unable to start cluster.", e);
698          throw new LifecycleException(e);
699       }
700    }
701
702    /**
703     * If this object created its own {@link TreeCache}, stops it
704     * and unregisters it with JMX.
705     *
706     * @see org.apache.catalina.Lifecycle#stop()
707     */

708    public void stop() throws LifecycleException
709    {
710       if (!started)
711       {
712          throw new IllegalStateException JavaDoc("Cluster not started");
713       }
714       
715       // Notify our interested LifecycleListeners
716
lifecycle.fireLifecycleEvent(BEFORE_STOP_EVENT, this);
717       
718       if (pojoCacheLocal)
719       {
720          pojoCache.stop();
721          pojoCache.destroy();
722       }
723
724       started = false;
725       // Notify our interested LifecycleListeners
726
lifecycle.fireLifecycleEvent(AFTER_STOP_EVENT, this);
727
728       unregisterMBeans();
729    }
730
731    /* (non-javadoc)
732     * @see org.apache.catalina.Lifecycle#addLifecycleListener()
733     */

734    public void addLifecycleListener(LifecycleListener listener)
735    {
736       lifecycle.addLifecycleListener(listener);
737    }
738
739    /* (non-javadoc)
740     * @see org.apache.catalina.Lifecycle#findLifecycleListeners()
741     */

742    public LifecycleListener[] findLifecycleListeners()
743    {
744       return lifecycle.findLifecycleListeners();
745    }
746
747    /* (non-javadoc)
748     * @see org.apache.catalina.Lifecycle#removeLifecycleListener()
749     */

750    public void removeLifecycleListener(LifecycleListener listener)
751    {
752       lifecycle.removeLifecycleListener(listener);
753    }
754    
755    // -------------------------------------------------------- Private Methods
756

757    /**
758     * Gets our TreeCache, either from a local reference or the JMX
759     * server. If one is not found, creates and configures it.
760     */

761    private PojoCacheJmxWrapperMBean getTreeCache() throws Exception JavaDoc
762    {
763       if (pojoCache == null) {
764          
765          MBeanServer JavaDoc server = getMBeanServer();
766          ObjectName JavaDoc objName = new ObjectName JavaDoc(pojoCacheObjectName);
767          if (server.isRegistered(objName))
768          {
769             // Get a proxy to the existing TreeCache
770
pojoCache = (PojoCacheJmxWrapperMBean)
771                            MBeanProxyExt.create(PojoCacheJmxWrapperMBean.class, objName);
772          }
773          else
774          {
775             // See if there is an XML descriptor file to configure the cache
776
InputStream JavaDoc configIS = getCacheConfigStream();
777             
778             Configuration config = null;
779             
780             if (configIS != null)
781             {
782                XmlConfigurationParser parser = new XmlConfigurationParser();
783                config = parser.parseStream(configIS);
784                try
785                {
786                   configIS.close();
787                }
788                catch (IOException JavaDoc io)
789                {
790                   // ignore
791
}
792                
793                if (clusterName != null)
794                {
795                   // Override the XML config with the name provided in
796
// server.xml. Method setClusterName is specified in the
797
// Cluster interface, otherwise we would not do this
798
config.setClusterName(clusterName);
799                }
800             }
801             else
802             {
803                // User did not try to configure the cache.
804
// Configure it using defaults. Only exception
805
// is the clusterName, which user can specify in server.xml.
806
config = new Configuration();
807                String JavaDoc channelName = (clusterName == null) ? DEFAULT_CLUSTER_NAME
808                                                           : clusterName;
809                config.setClusterName(channelName);
810                config.setIsolationLevel(DEFAULT_ISOLATION_LEVEL);
811                config.setCacheMode(DEFAULT_CACHE_MODE);
812                config.setLockAcquisitionTimeout(DEFAULT_LOCK_TIMEOUT);
813                config.setTransactionManagerLookupClass(DEFAULT_TM_LOOKUP);
814             }
815
816             pojoCache = new PojoCacheJmxWrapper(PojoCacheFactory.createCache(config, false));
817             
818             pojoCacheLocal = true;
819          }
820       }
821       return pojoCache;
822    }
823    
824    
825
826    private InputStream JavaDoc getCacheConfigStream() throws FileNotFoundException JavaDoc
827    {
828       boolean useDefault = (this.cacheConfigPath == null);
829       String JavaDoc path = (useDefault) ? DEFAULT_CACHE_CONFIG_PATH : cacheConfigPath;
830       // See if clusterProperties points to a file relative
831
// to $CATALINA_BASE
832
File JavaDoc file = new File JavaDoc(path);
833       if (!file.isAbsolute())
834       {
835          file = new File JavaDoc(System.getProperty("catalina.base"), path);
836       }
837       
838       try
839       {
840          return new FileInputStream JavaDoc(file);
841       }
842       catch (FileNotFoundException JavaDoc fnf)
843       {
844          if (useDefault)
845          {
846             // Not a problem, just means user did not try to
847
// configure the cache. Return null and let the cache
848
// be configured from defaults.
849
return null;
850          }
851          else
852          {
853             // User provided config was invalid; throw the exception
854
log.error("No tree cache config file found at " +
855                       file.getAbsolutePath());
856             throw fnf;
857          }
858       }
859    }
860
861    /**
862     * Registers this object and the tree cache (if we created it) with JMX.
863     */

864    private void registerMBeans()
865    {
866       try
867       {
868          MBeanServer JavaDoc server = getMBeanServer();
869          
870          String JavaDoc domain;
871          if (container instanceof ContainerBase)
872          {
873             domain = ((ContainerBase) container).getDomain();
874          }
875          else
876          {
877             domain = server.getDefaultDomain();
878          }
879          
880          String JavaDoc name = ":type=Cluster";
881          if (container instanceof Host) {
882             name += ",host=" + container.getName();
883          }
884          else if (container instanceof Engine)
885          {
886             name += ",engine=" + container.getName();
887          }
888          
889          ObjectName JavaDoc clusterName = new ObjectName JavaDoc(domain + name);
890
891          if (server.isRegistered(clusterName))
892          {
893             log.warn("MBean " + clusterName + " already registered");
894          }
895          else
896          {
897             this.objectName = clusterName;
898             server.registerMBean(this, objectName);
899          }
900
901          if (pojoCacheLocal)
902          {
903             // Register the treeCache
904
ObjectName JavaDoc treeCacheName = new ObjectName JavaDoc(pojoCacheObjectName);
905             server.registerMBean(getTreeCache(), treeCacheName);
906          }
907
908       }
909       catch (Exception JavaDoc ex)
910       {
911          log.error(ex.getMessage(), ex);
912       }
913    }
914
915    /**
916     * Unregisters this object and the tree cache (if we created it) with JMX.
917     */

918    private void unregisterMBeans()
919    {
920       if (mserver != null)
921       {
922          try
923          {
924             if (objectName != null) {
925                mserver.unregisterMBean(objectName);
926             }
927             if (pojoCacheLocal)
928             {
929                mserver.unregisterMBean(new ObjectName JavaDoc(pojoCacheObjectName));
930             }
931          }
932          catch (Exception JavaDoc e)
933          {
934             log.error(e);
935          }
936       }
937    }
938
939    /**
940     * Get the current Catalina MBean Server.
941     *
942     * @return
943     * @throws Exception
944     */

945    private MBeanServer JavaDoc getMBeanServer() throws Exception JavaDoc
946    {
947       if (mserver == null)
948       {
949          ArrayList JavaDoc servers = MBeanServerFactory.findMBeanServer(null);
950          if (servers.size() > 0)
951          {
952             mserver = (MBeanServer JavaDoc) servers.get(0);
953          }
954          else
955          {
956             mserver = MBeanServerFactory.createMBeanServer();
957          }
958       }
959       return mserver;
960    }
961
962 }
963
Popular Tags