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 ; 8 import javax.management.MBeanServerInvocationHandler ; 9 import javax.management.MalformedObjectNameException ; 10 import javax.management.ObjectName ; 11 import java.rmi.Naming ; 12 13 20 public class RmiCacheServer implements RmiCacheServerMBean 21 { 22 CacheImpl cache; 24 RemoteTreeCacheImpl remoteObj; 25 String bindAddress; 26 String mbeanServerName; 27 int port; 28 String configFile; 29 String bindName; 30 ObjectName cacheName; 31 32 public String getBindAddress() 33 { 34 return bindAddress; 35 } 36 37 public void setBindAddress(String 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 getMBeanServerName() 53 { 54 return mbeanServerName; 55 } 56 57 public void setMBeanServerName(String name) 58 { 59 mbeanServerName = name; 60 } 61 62 public String getConfig() 63 { 64 return configFile; 65 } 66 67 public void setConfig(String config) 68 { 69 configFile = config; 70 } 71 72 public CacheImpl getCache() 74 { 75 return cache; 76 } 77 78 public void setCache(CacheImpl cache) 80 { 81 this.cache = cache; 82 } 83 84 public String getCacheName() 85 { 86 return cacheName != null ? cacheName.toString() : "n/a"; 87 } 88 89 public void setCacheName(String cacheName) throws MalformedObjectNameException 90 { 91 this.cacheName = new ObjectName (cacheName); 92 } 93 94 public String getBindName() 95 { 96 return bindName; 97 } 98 99 public void setBindName(String bindName) 100 { 101 this.bindName = bindName; 102 } 103 104 public RmiCacheServer(String host, int port, String bindName, String 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 117 { 118 if (cache == null) 119 { 120 MBeanServer server = JmxUtil.getMBeanServer(); 121 if (cacheName != null && server != null) 123 { 124 cache = (CacheImpl) MBeanServerInvocationHandler.newProxyInstance(server, cacheName, CacheImpl.class, false); 126 } 128 } 129 130 if (cache == null) 131 { 132 cache = new CacheImpl(); 133 cache.setConfiguration(new XmlConfigurationParser().parseFile(configFile)); 134 cache.start(); } 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 e) 156 { 157 e.printStackTrace(); 158 } 159 } 160 } 161 162 public void destroy() 163 { 164 } 165 166 167 public static void main(String [] args) 168 { 169 String 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 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 |