KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > cache > loader > RmiDelegatingCacheLoader


1 /*
2  * JBoss, the OpenSource J2EE webOS
3  *
4  * Distributable under LGPL license.
5  * See terms of license at gnu.org.
6  */

7 package org.jboss.cache.loader;
8
9 import org.jboss.cache.CacheImpl;
10 import org.jboss.cache.CacheSPI;
11 import org.jboss.cache.Fqn;
12 import org.jboss.cache.NodeSPI;
13 import org.jboss.cache.config.CacheLoaderConfig.IndividualCacheLoaderConfig;
14 import org.jboss.cache.loader.rmi.RemoteTreeCache;
15
16 import java.io.ObjectInputStream JavaDoc;
17 import java.io.ObjectOutputStream JavaDoc;
18 import java.rmi.Naming JavaDoc;
19 import java.util.Map JavaDoc;
20 import java.util.Set JavaDoc;
21
22 /**
23  * DelegatingCacheLoader implementation which delegates to a remote (not in the same VM)
24  * CacheImpl using the Java RMI mechanism. If configured via an XML configuration file,
25  * the remote CacheImpl delegated to is this cacheloader's cache's coordinator. If
26  * configured programmatically, this cacheloader may delegate to any remote cache that
27  * has been appropriately bound.
28  * <p/>
29  * This CacheLoader expects three configuration properties: <tt>host</tt>, <tt>port</tt>
30  * and <tt>bindName</tt>. If the <tt>host</tt> propety is not specified, it defaults to
31  * <tt>localhost</tt>; if the <tt>port</tt> property is not specified, it defaults to
32  * <tt>1098</tt>; if the <tt>bindName</tt> property is not specified, it defaults to the
33  * cacheloader's cache's cluster name.
34  *
35  * @author Daniel Gredler
36  * @version $Id: RmiDelegatingCacheLoader.java,v 1.17 2007/01/10 03:55:41 msurtani Exp $
37  */

38 public class RmiDelegatingCacheLoader extends DelegatingCacheLoader
39 {
40    private RmiDelegatingCacheLoaderConfig config = new RmiDelegatingCacheLoaderConfig();
41    private CacheImpl cache;
42    private RemoteTreeCache remoteCache;
43    private boolean programmaticInit;
44
45    /**
46     * Default constructor.
47     */

48    public RmiDelegatingCacheLoader()
49    {
50       // Empty.
51
}
52
53    /**
54     * Allows programmatic configuration.
55     *
56     * @param cache The cache that the cacheloader will acting on behalf of.
57     * @param host The host on which to look up the remote object.
58     * @param port The port on which to look up the remote object.
59     * @param bindName The name to which the remote object is bound.
60     */

61    public RmiDelegatingCacheLoader(CacheImpl cache, String JavaDoc host, int port, String JavaDoc bindName)
62    {
63       this.cache = cache;
64       config.setHost(host);
65       config.setPort(String.valueOf(port));
66       config.setBindName(bindName);
67       this.programmaticInit = true;
68       this.tryToInitRemoteCache();
69    }
70
71    /**
72     * Allows configuration via XML config file.
73     */

74    public void setConfig(IndividualCacheLoaderConfig base)
75    {
76       if (base instanceof RmiDelegatingCacheLoaderConfig)
77       {
78          this.config = (RmiDelegatingCacheLoaderConfig) base;
79       }
80       else
81       {
82          config = new RmiDelegatingCacheLoaderConfig(base);
83       }
84       this.tryToInitRemoteCache();
85    }
86
87    public IndividualCacheLoaderConfig getConfig()
88    {
89       return config;
90    }
91
92    /**
93     * Allows configuration via XML config file.
94     *
95     * @see DelegatingCacheLoader#setCache(org.jboss.cache.CacheSPI)
96     */

97    public void setCache(CacheSPI cache)
98    {
99       super.setCache(cache);
100       this.tryToInitRemoteCache();
101    }
102
103    /**
104     * @see org.jboss.cache.loader.DelegatingCacheLoader#delegateGetChildrenNames(org.jboss.cache.Fqn)
105     */

106    protected Set JavaDoc delegateGetChildrenNames(Fqn fqn) throws Exception JavaDoc
107    {
108       return (this.remoteCache != null ? this.remoteCache.getChildrenNames(fqn) : null);
109    }
110
111    // See http://jira.jboss.com/jira/browse/JBCACHE-118 for why this is commented out.
112
/**
113     * @see org.jboss.cache.loader.DelegatingCacheLoader#delegateGet(org.jboss.cache.Fqn,java.lang.Object)
114     */

115    // protected Object delegateGet(Fqn name, Object key) throws Exception {
116
// return ( this.remoteCache != null ? this.remoteCache.get(name, key) : null );
117
// }
118

119    /**
120     * @see org.jboss.cache.loader.DelegatingCacheLoader#delegateGet(org.jboss.cache.Fqn)
121     */

122    protected Map JavaDoc delegateGet(Fqn name) throws Exception JavaDoc
123    {
124       NodeSPI n = (NodeSPI) (this.remoteCache != null ? this.remoteCache.get(name) : null);
125       if (n == null)
126       {
127          return null;
128       }
129       return n.getDataDirect();
130    }
131
132    /**
133     * @see org.jboss.cache.loader.DelegatingCacheLoader#delegateExists(org.jboss.cache.Fqn)
134     */

135    protected boolean delegateExists(Fqn name) throws Exception JavaDoc
136    {
137       return (this.remoteCache != null && this.remoteCache.exists(name));
138    }
139
140    /**
141     * @see org.jboss.cache.loader.DelegatingCacheLoader#delegatePut(org.jboss.cache.Fqn,java.lang.Object,java.lang.Object)
142     */

143    protected Object JavaDoc delegatePut(Fqn name, Object JavaDoc key, Object JavaDoc value) throws Exception JavaDoc
144    {
145       return (this.remoteCache != null ? this.remoteCache.put(name, key, value) : null);
146    }
147
148    /**
149     * @see org.jboss.cache.loader.DelegatingCacheLoader#delegatePut(org.jboss.cache.Fqn,java.util.Map)
150     */

151    protected void delegatePut(Fqn name, Map JavaDoc attributes) throws Exception JavaDoc
152    {
153       if (this.remoteCache != null) this.remoteCache.put(name, attributes);
154    }
155
156    /**
157     * @see org.jboss.cache.loader.DelegatingCacheLoader#delegateRemove(org.jboss.cache.Fqn,java.lang.Object)
158     */

159    protected Object JavaDoc delegateRemove(Fqn name, Object JavaDoc key) throws Exception JavaDoc
160    {
161       return (this.remoteCache != null ? this.remoteCache.remove(name, key) : null);
162    }
163
164    /**
165     * @see org.jboss.cache.loader.DelegatingCacheLoader#delegateRemove(org.jboss.cache.Fqn)
166     */

167    protected void delegateRemove(Fqn name) throws Exception JavaDoc
168    {
169       if (this.remoteCache != null) this.remoteCache.remove(name);
170    }
171
172    /**
173     * @see org.jboss.cache.loader.DelegatingCacheLoader#delegateRemoveData(org.jboss.cache.Fqn)
174     */

175    protected void delegateRemoveData(Fqn name) throws Exception JavaDoc
176    {
177       if (this.remoteCache != null) this.remoteCache.removeData(name);
178    }
179
180    @Override JavaDoc
181    protected void delegateLoadEntireState(ObjectOutputStream JavaDoc os) throws Exception JavaDoc
182    {
183       throw new UnsupportedOperationException JavaDoc("setting and loading state for specific Fqns not supported");
184    }
185
186    @Override JavaDoc
187    protected void delegateLoadState(Fqn subtree, ObjectOutputStream JavaDoc os) throws Exception JavaDoc
188    {
189       throw new UnsupportedOperationException JavaDoc("setting and loading state for specific Fqns not supported");
190    }
191
192    @Override JavaDoc
193    protected void delegateStoreEntireState(ObjectInputStream JavaDoc is) throws Exception JavaDoc
194    {
195       throw new UnsupportedOperationException JavaDoc("setting and loading state for specific Fqns not supported");
196    }
197
198    @Override JavaDoc
199    protected void delegateStoreState(Fqn subtree, ObjectInputStream JavaDoc is) throws Exception JavaDoc
200    {
201       throw new UnsupportedOperationException JavaDoc("setting and loading state for specific Fqns not supported");
202    }
203
204    /**
205     * Tries to initialize the remote cache object. If this cacheloader has been
206     * cofigured using an XML file
207     */

208    private void tryToInitRemoteCache()
209    {
210       if (config.getHost() == null || config.getPort() == null || this.cache == null)
211       {
212          return;
213       }
214       if (config.getBindName() == null)
215       {
216          config.setBindName(this.cache.getConfiguration().getClusterName());
217       }
218       if (!this.programmaticInit && this.cache.isCoordinator())
219       {
220          // CacheLoader specified via XML, but this cache is the coordinator!
221
this.remoteCache = null;
222          return;
223       }
224       String JavaDoc name = "//" + config.getHost() + ":" + config.getPort() + "/" + config.getBindName();
225       try
226       {
227          this.remoteCache = (RemoteTreeCache) Naming.lookup(name);
228       }
229       catch (Throwable JavaDoc t)
230       {
231          log.error("Unable to lookup remote cache at '" + name + "'.", t);
232       }
233    }
234 }
235
Popular Tags