KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > cache > loader > rmi > RmiCacheServer


1 package org.jboss.cache.loader.rmi;
2
3 import org.jboss.cache.CacheImpl;
4 import org.jboss.cache.factories.XmlConfigurationParser;
5 import org.jboss.cache.jmx.JmxUtil;
6
7 import javax.management.MBeanServer JavaDoc;
8 import javax.management.MBeanServerInvocationHandler JavaDoc;
9 import javax.management.MalformedObjectNameException JavaDoc;
10 import javax.management.ObjectName JavaDoc;
11 import java.rmi.Naming JavaDoc;
12
13 /**
14  * Server which exports an RMI stub to the CacheImpl. Clients can use the RmiDelegatingCacheLoader to remotely
15  * delegate to this CacheImpl
16  *
17  * @author Bela Ban
18  * @version $Id: RmiCacheServer.java,v 1.9 2006/12/30 17:50:00 msurtani Exp $
19  */

20 public class RmiCacheServer implements RmiCacheServerMBean
21 {
22    //TreeCacheMBean cache;
23
CacheImpl cache;
24    RemoteTreeCacheImpl remoteObj;
25    String JavaDoc bindAddress;
26    String JavaDoc mbeanServerName;
27    int port;
28    String JavaDoc configFile;
29    String JavaDoc bindName;
30    ObjectName JavaDoc cacheName;
31
32    public String JavaDoc getBindAddress()
33    {
34       return bindAddress;
35    }
36
37    public void setBindAddress(String JavaDoc bindAddress)
38    {
39       this.bindAddress = bindAddress;
40    }
41
42    public int getPort()
43    {
44       return port;
45    }
46
47    public void setPort(int port)
48    {
49       this.port = port;
50    }
51
52    public String JavaDoc getMBeanServerName()
53    {
54       return mbeanServerName;
55    }
56
57    public void setMBeanServerName(String JavaDoc name)
58    {
59       mbeanServerName = name;
60    }
61
62    public String JavaDoc getConfig()
63    {
64       return configFile;
65    }
66
67    public void setConfig(String JavaDoc config)
68    {
69       configFile = config;
70    }
71
72    //public TreeCacheMBean getCache()
73
public CacheImpl getCache()
74    {
75       return cache;
76    }
77
78    //public void setCache(TreeCacheMBean cache)
79
public void setCache(CacheImpl cache)
80    {
81       this.cache = cache;
82    }
83
84    public String JavaDoc getCacheName()
85    {
86       return cacheName != null ? cacheName.toString() : "n/a";
87    }
88
89    public void setCacheName(String JavaDoc cacheName) throws MalformedObjectNameException JavaDoc
90    {
91       this.cacheName = new ObjectName JavaDoc(cacheName);
92    }
93
94    public String JavaDoc getBindName()
95    {
96       return bindName;
97    }
98
99    public void setBindName(String JavaDoc bindName)
100    {
101       this.bindName = bindName;
102    }
103
104    public RmiCacheServer(String JavaDoc host, int port, String JavaDoc bindName, String JavaDoc config)
105    {
106       this.bindAddress = host;
107       this.port = port;
108       this.configFile = config;
109       this.bindName = bindName;
110    }
111
112    public void create()
113    {
114    }
115
116    public void start() throws Exception JavaDoc
117    {
118       if (cache == null)
119       {
120          MBeanServer JavaDoc server = JmxUtil.getMBeanServer();
121          // 1. check whether we have an object name, pointing to the cache MBean
122
if (cacheName != null && server != null)
123          {
124             //cache = (TreeCacheMBean) MBeanProxyExt.create(TreeCacheMBean.class, cacheName, server);
125
cache = (CacheImpl) MBeanServerInvocationHandler.newProxyInstance(server, cacheName, CacheImpl.class, false);
126             //cache = (CacheImpl) MBeanProxyExt.create(CacheImpl.class, cacheName, server);
127
}
128       }
129
130       if (cache == null)
131       {
132          cache = new CacheImpl();
133          cache.setConfiguration(new XmlConfigurationParser().parseFile(configFile));
134          cache.start(); // kick start tree cache
135
}
136
137       remoteObj = new RemoteTreeCacheImpl(cache);
138       Naming.rebind("//" + bindAddress + ":" + port + "/" + bindName, remoteObj);
139    }
140
141    public void stop()
142    {
143       if (cache != null)
144       {
145          cache.stop();
146          cache.destroy();
147          cache = null;
148       }
149       if (remoteObj != null)
150       {
151          try
152          {
153             Naming.unbind("//" + bindAddress + ":" + port + "/" + bindName);
154          }
155          catch (Exception JavaDoc e)
156          {
157             e.printStackTrace();
158          }
159       }
160    }
161
162    public void destroy()
163    {
164    }
165
166
167    public static void main(String JavaDoc[] args)
168    {
169       String JavaDoc bindAddress = "localhost", configFile = "cache-service.xml", bindName = "rmiCacheLoader";
170       int port = 1098;
171       RmiCacheServer server;
172
173       for (int i = 0; i < args.length; i++)
174       {
175          if (args[i].equals("-bindAddress"))
176          {
177             bindAddress = args[++i];
178             continue;
179          }
180          if (args[i].equals("-port"))
181          {
182             port = Integer.parseInt(args[+i]);
183             continue;
184          }
185          if (args[i].equals("-config"))
186          {
187             configFile = args[++i];
188             continue;
189          }
190          if (args[i].equals("-bindName"))
191          {
192             bindName = args[++i];
193             continue;
194          }
195          help();
196          return;
197       }
198       server = new RmiCacheServer(bindAddress, port, bindName, configFile);
199       try
200       {
201          server.start();
202       }
203       catch (Exception JavaDoc e)
204       {
205          e.printStackTrace();
206       }
207    }
208
209
210    private static void help()
211    {
212       System.out.println("CacheServer [-bindAddress <host>] [-port <port>] [-bindName <RMI bind name>] [-config <cache config>] [-help]");
213    }
214 }
215
Popular Tags