1 23 24 package org.objectweb.jonas.discovery; 25 26 29 import javax.management.JMException ; 30 import javax.management.MBeanRegistration ; 31 import javax.management.MBeanServer ; 32 import javax.management.MBeanServerNotification ; 33 import javax.management.Notification ; 34 import javax.management.NotificationListener ; 35 import javax.management.ObjectName ; 36 37 import org.objectweb.jonas.common.Log; 38 import org.objectweb.jonas.jmx.J2eeObjectName; 39 import org.objectweb.util.monolog.api.BasicLevel; 40 import org.objectweb.util.monolog.api.Logger; 41 42 51 public class DiscoveryManager implements DiscoveryManagerMBean, 52 MBeanRegistration , NotificationListener { 53 54 private static Logger logger = Log.getLogger(Log.JONAS_DISCOVERY_PREFIX); 55 56 60 private int greetingListeningPort; 61 62 65 private int listeningPort; 66 67 70 private String listeningIp; 71 72 76 private DiscoveryComm dc; 77 78 private DiscoveryGreetingResponder dgr; 79 80 private DiscoveryGreetingListener dgl; 81 82 85 private Thread commDaemon; 86 87 92 private Thread discoveryGreetingListenerThread; 93 94 97 private MBeanServer mbeanServer; 98 99 private ObjectName myOn; 100 101 private int ttl = 1; 102 103 private int greetingAckTimeOut; 104 105 private String jonasName = null; 106 107 private String domainName = null; 108 109 private String serverId = null; 110 111 private String [] urls = null; 112 113 127 public DiscoveryManager(String serverId, int listeningPort, 128 String listeningIp, int greetingListeningPort, int greetingAckTimeOut) { 129 this.serverId = serverId; 130 this.listeningIp = listeningIp; 131 this.listeningPort = listeningPort; 132 this.greetingListeningPort = greetingListeningPort; 133 this.greetingAckTimeOut = greetingAckTimeOut; 134 } 135 136 141 public void start() { 142 143 dgl = new DiscoveryGreetingListener(this); 144 dgr = new DiscoveryGreetingResponder(this); 145 146 if (discoveryGreetingListenerThread == null) 148 discoveryGreetingListenerThread = new Thread (dgl, 149 "GreetingListener"); 150 151 discoveryGreetingListenerThread.start(); 152 try { 153 dgr.handleGreeting(); 157 } catch (DuplicateServerNameException e) { 158 logger.log(BasicLevel.ERROR, 160 "Discovery manager failed to start due to a pre-existing" 161 + " server in the domain with the same ID.", e); 162 throw e; 163 } 164 165 dc = new DiscoveryComm(this); 167 if (commDaemon == null) { 168 commDaemon = new Thread (dc, "commDaemon"); 169 } 170 commDaemon.start(); 171 } 172 173 176 public int getGreetingAckTimeOut() { 177 return greetingAckTimeOut; 178 179 } 180 181 186 public void setGreetingAckTimeOut(int greetingAckTimeOut) { 187 this.greetingAckTimeOut = greetingAckTimeOut; 188 189 } 190 191 194 public int getGreetingListeningPort() { 195 return greetingListeningPort; 196 197 } 198 199 204 public void setGreetingListeningPort(int greetingListeningPort) { 205 this.greetingListeningPort = greetingListeningPort; 206 207 } 208 209 212 public int getListeningPort() { 213 return listeningPort; 214 } 215 216 221 public void setListeningPort(int listeningPort) { 222 this.listeningPort = listeningPort; 223 224 } 225 226 229 public String getListeningIp() { 230 return listeningIp; 231 } 232 233 236 public void setListeningIp(String listeningIp) { 237 this.listeningIp = listeningIp; 238 239 } 240 241 244 public void setTimeToLive(int ttl) { 245 this.ttl = ttl; 246 } 247 248 251 public int getTimeToLive() { 252 return this.ttl; 253 } 254 255 259 public ObjectName preRegister(MBeanServer mbeanServer, ObjectName on) 260 throws Exception { 261 this.mbeanServer = mbeanServer; 263 this.myOn = on; 264 265 return myOn; 266 } 267 268 271 public void postRegister(Boolean arg0) { 272 ObjectName delegate; 275 try { 276 delegate = new ObjectName ( 277 "JMImplementation:type=MBeanServerDelegate"); 278 mbeanServer.addNotificationListener(delegate, this, null, null); 279 } catch (JMException e) { 280 e.printStackTrace(); 281 } 282 } 283 284 287 public void preDeregister() throws Exception { 288 stop(); 290 } 291 292 295 public void postDeregister() { 296 297 } 298 299 303 public void handleNotification(Notification notification, Object handback) { 304 if (notification.getType().equals( 305 MBeanServerNotification.UNREGISTRATION_NOTIFICATION)) { 306 ObjectName notificationSender = ((MBeanServerNotification ) notification) 307 .getMBeanName(); 308 ObjectName j2eeServerName = J2eeObjectName.J2EEServer(domainName, 309 jonasName); 310 if (notificationSender.equals(j2eeServerName)) { 311 if(dc != null) { 314 dc.stop(); 315 } 316 317 } 318 } 319 } 320 321 324 public String getJonasName() { 325 return jonasName; 326 } 327 328 332 protected void setJonasName(String jonasName) { 333 this.jonasName = jonasName; 334 } 335 336 339 public String getDomainName() { 340 return domainName; 341 } 342 343 347 protected void setDomainName(String domainNamle) { 348 this.domainName = domainNamle; 349 } 350 351 355 protected String [] getUrls() { 356 return urls; 357 } 358 359 363 protected void setUrls(String [] urls) { 364 this.urls = urls; 365 } 366 367 370 public void stop() { 371 if (dc != null) { 374 dc.stop(); 375 } 376 dgl.stop(); 377 } 378 379 public String getServerId() { 380 return serverId; 381 } 382 383 public void setServerId(String serverId) { 384 this.serverId = serverId; 385 } 386 } | Popular Tags |