1 25 26 package org.objectweb.jonas.discovery; 27 28 import java.util.ArrayList ; 29 30 import javax.management.InstanceNotFoundException ; 31 import javax.management.JMException ; 32 import javax.management.MBeanRegistrationException ; 33 import javax.management.MBeanServer ; 34 import javax.management.MalformedObjectNameException ; 35 import javax.management.remote.JMXServiceURL ; 36 import javax.naming.Context ; 37 import javax.naming.NamingException ; 38 39 import org.objectweb.jonas.common.Log; 40 import org.objectweb.jonas.jmx.JmxService; 41 import org.objectweb.jonas.jmx.JonasObjectName; 42 import org.objectweb.jonas.service.AbsServiceImpl; 43 import org.objectweb.jonas.service.ServiceException; 44 import org.objectweb.jonas.service.ServiceManager; 45 import org.objectweb.util.monolog.api.BasicLevel; 46 import org.objectweb.util.monolog.api.Logger; 47 48 66 public class DiscoveryServiceImpl extends AbsServiceImpl implements DiscoveryService , DiscoveryServiceImplMBean { 67 68 71 private static final String DISCOVERY_SOURCE_PORT_DEFAULT = "9888"; 72 75 private static final String DISCOVERY_GREETING_PORT_DEFAULT = "9899"; 76 80 private static final String DISCOVERY_GREETING_TIMEOUT_DEFAULT = "1000"; 81 84 private String listeningIp = null; 85 88 private int listeningPort; 89 92 private int greetingListeningPort; 93 96 private int greetingAckTimeOut; 97 100 private int sourcePort; 101 104 private boolean isDiscoveryMaster = false; 105 108 private MBeanServer mbeanServer = null; 109 110 113 private JmxService jmxService = null; 114 117 private static Logger logger = null; 118 119 122 public String getMulticastAddress() { 123 return listeningIp; 124 } 125 126 129 public String getMulticastPort() { 130 return String.valueOf(listeningPort); 131 } 132 133 136 public Boolean getIsDiscoveryMaster() { 137 return new Boolean (isDiscoveryMaster); 138 } 139 144 public void startDiscoveryMaster() throws JMException { 145 if (!isDiscoveryMaster) { 146 createEnroller(); 147 createDiscClient(); 148 isDiscoveryMaster = true; 149 } 150 } 151 152 155 protected void doInit(Context ctx) throws ServiceException { 156 logger = Log.getLogger(Log.JONAS_DISCOVERY_PREFIX); 158 159 try { 161 listeningIp = (String ) ctx.lookup("jonas.service.discovery.multicast.address"); 162 String sListeningPort = (String ) ctx.lookup("jonas.service.discovery.multicast.port"); 163 listeningPort = (Integer.valueOf(sListeningPort)).intValue(); 164 165 } catch (NamingException ne) { 166 String err = "Cannot read initializations arguments in service context"; 167 logger.log(BasicLevel.ERROR, err); 168 throw new ServiceException(err, ne); 169 } 170 try { 172 String sMaster = (String ) ctx.lookup("jonas.service.discovery.master"); 173 if (sMaster != null && sMaster.equals("true")) { 174 isDiscoveryMaster = true; 175 } 176 } catch (NamingException ne) { 177 } 179 if (!isDiscoveryMaster) { 184 if (getDomainName().equals(getJonasServerName())) { 185 isDiscoveryMaster = true; 186 } 187 } 188 String sSourcePort = null; 190 String greetListeningPort = null; 191 String greetAckTimeOut = null; 192 193 try { 195 sSourcePort = (String ) ctx.lookup("jonas.service.discovery.source.port"); 196 } catch (NamingException ne) { 197 sSourcePort = DISCOVERY_SOURCE_PORT_DEFAULT; 198 } 199 sourcePort = (Integer.valueOf(sSourcePort)).intValue(); 200 201 try { 203 greetListeningPort = (String ) ctx.lookup("jonas.service.discovery.greeting.port"); 204 } catch (NamingException e1) { 205 greetListeningPort = DISCOVERY_GREETING_PORT_DEFAULT; 206 } 207 greetingListeningPort = (Integer.valueOf(greetListeningPort)).intValue(); 208 209 try { 211 greetAckTimeOut = (String ) ctx.lookup("jonas.service.discovery.greeting.timeout"); 212 } catch (NamingException e1) { 213 greetAckTimeOut = DISCOVERY_GREETING_TIMEOUT_DEFAULT; 214 } 215 greetingAckTimeOut = (Integer.valueOf(greetAckTimeOut)).intValue(); 216 217 ServiceManager sm = null; 219 try { 220 sm = ServiceManager.getInstance(); 221 } catch (Exception e) { 222 String err = "Cannot get ServiceManager instance"; 223 logger.log(BasicLevel.ERROR, err); 224 throw new ServiceException(err, e); 225 } 226 jmxService = ((JmxService) sm.getJmxService()); 227 try { 228 mbeanServer = jmxService.getJmxServer(); 229 } catch (ServiceException e) { 230 mbeanServer = null; 232 String err = "Cannot get MBeanServer reference"; 233 logger.log(BasicLevel.ERROR, err); 234 throw new ServiceException(err, e); 235 } 236 } 237 238 244 protected void doStart() throws ServiceException { 245 DiscoveryManager dm = new DiscoveryManager(this.getJonasServerName(), listeningPort, 247 listeningIp, greetingListeningPort, greetingAckTimeOut); 248 dm.setDomainName(jmxService.getDomainName()); 249 dm.setJonasName(jmxService.getJonasServerName()); 250 JMXServiceURL [] connectorServerURLs = jmxService 251 .getConnectorServerURLs(); 252 ArrayList urlsList = new ArrayList (); 253 for (int i = 0; i < connectorServerURLs.length; i++) { 254 if (connectorServerURLs[i] != null) { 258 urlsList.add(connectorServerURLs[i].toString()); 259 } 260 } 261 String [] urls = new String [urlsList.size()]; 262 for (int i = 0; i < urls.length; i++) { 263 urls[i] = (String ) urlsList.get(i); 264 } 265 dm.setUrls(urls); 266 try { 267 if (mbeanServer != null) { 269 mbeanServer.registerMBean(dm, JonasObjectName 270 .discoveryManager()); 271 } 272 } catch (JMException e) { 273 throw new ServiceException( 274 "Problem when starting the Discovery Service: ", e); 275 } 276 try { 278 dm.start(); 279 } catch (DuplicateServerNameException e) { 280 logger.log(BasicLevel.ERROR, 282 "Discovery manager failed to start due to a pre-existing" 283 + " server in the domain with the same name.", e); 284 try { 285 mbeanServer.unregisterMBean(JonasObjectName.discoveryManager()); 287 } catch (JMException e1) { 288 logger.log(BasicLevel.ERROR, "Problem trying to unregister " 289 + "DiscoveryManager: ", e); 290 } 291 throw new ServiceException( 293 "Problem when starting the Discovery Service:", e); 294 } 295 296 if (isDiscoveryMaster) { 297 try { 299 createEnroller(); 300 } catch (JMException e) { 301 throw new ServiceException( 302 "Problem when starting the Discovery Service: ", e); 303 } 304 305 try { 307 createDiscClient(); 308 } catch (JMException e) { 309 throw new ServiceException( 310 "Problem when starting the Discovery Service: ", e); 311 } 312 } 313 314 try { 316 if (mbeanServer != null) { 317 mbeanServer.registerMBean(this, JonasObjectName 318 .discoveryService()); 319 } 320 } catch (JMException e) { 321 throw new ServiceException( 322 "Problem when starting the Discovery Service: ", e); 323 } 324 } 325 326 private void createEnroller() throws JMException { 327 Enroller enroller = new Enroller(listeningPort, listeningIp); 328 if (mbeanServer != null) { 329 mbeanServer.registerMBean(enroller, JonasObjectName.discoveryEnroller()); 330 } 331 } 332 333 private void createDiscClient() throws JMException { 334 DiscoveryClient dc = new DiscoveryClient(listeningPort, listeningIp, sourcePort); 335 if (mbeanServer != null) { 337 mbeanServer.registerMBean(dc, JonasObjectName.discoveryClient()); 338 } 339 } 340 341 344 protected void doStop() throws ServiceException { 345 348 try { 350 if (mbeanServer != null) { 351 mbeanServer.unregisterMBean(JonasObjectName.discoveryService()); 352 } 353 } catch (JMException e) { 354 throw new ServiceException("Problem when stoping the Discovery Service: ", e); 355 } 356 } 357 } | Popular Tags |