00001
00026 package org.objectweb.jonas.discovery;
00027
00028 import javax.management.JMException;
00029 import javax.management.MBeanServer;
00030 import javax.management.remote.JMXServiceURL;
00031 import javax.naming.Context;
00032 import javax.naming.NamingException;
00033
00034 import org.objectweb.jonas.common.Log;
00035 import org.objectweb.jonas.jmx.JmxService;
00036 import org.objectweb.jonas.jmx.JonasObjectName;
00037 import org.objectweb.jonas.service.AbsServiceImpl;
00038 import org.objectweb.jonas.service.ServiceException;
00039 import org.objectweb.jonas.service.ServiceManager;
00040 import org.objectweb.util.monolog.api.BasicLevel;
00041 import org.objectweb.util.monolog.api.Logger;
00042
00061 public class DiscoveryServiceImpl extends AbsServiceImpl implements DiscoveryService , DiscoveryServiceImplMBean {
00062
00063 private final String DISCOVERY_SOURCE_PORT_DEFAULT = "9888";
00064 private String listeningIp = null;
00065 private int listeningPort;
00066 private int sourcePort;
00070 private boolean isDiscoveryMaster = false;
00074 private MBeanServer mbeanServer = null;
00075
00079 private JmxService jmxService = null;
00083 private static Logger logger = null;
00084
00088 public String getMulticastAddress() {
00089 return listeningIp;
00090 }
00091
00095 public String getMulticastPort() {
00096 return String.valueOf(listeningPort);
00097 }
00098
00102 public Boolean getIsDiscoveryMaster() {
00103 return new Boolean(isDiscoveryMaster);
00104 }
00110 public void startDiscoveryMaster() throws JMException {
00111 if (!isDiscoveryMaster) {
00112 createEnroller();
00113 createDiscClient();
00114 isDiscoveryMaster = true;
00115 }
00116 }
00117
00118
00119
00120
00121 protected void doInit(Context ctx) throws ServiceException {
00122
00123 logger = Log.getLogger(Log.JONAS_DISCOVERY_PREFIX);
00124
00125
00126 try {
00127 listeningIp = (String) ctx.lookup("jonas.service.discovery.multicast.address");
00128 String sListeningPort = (String) ctx.lookup("jonas.service.discovery.multicast.port");
00129 listeningPort = (Integer.valueOf(sListeningPort)).intValue();
00130 } catch (NamingException ne) {
00131 String err = "Cannot read initializations arguments in service context";
00132 logger.log(BasicLevel.ERROR, err);
00133 throw new ServiceException(err, ne);
00134 }
00135
00136 try {
00137 String sMaster = (String) ctx.lookup("jonas.service.discovery.master");
00138 if (sMaster != null && sMaster.equals("true")) {
00139 isDiscoveryMaster = true;
00140 }
00141 } catch (NamingException ne) {
00142
00143 }
00144
00145
00146
00147
00148 if (!isDiscoveryMaster) {
00149 if (getDomainName().equals(getJonasServerName())) {
00150 isDiscoveryMaster = true;
00151 }
00152 }
00153
00154 String sSourcePort = null;
00155 try {
00156 sSourcePort = (String) ctx.lookup("jonas.service.discovery.source.port");
00157 } catch (NamingException ne) {
00158 sSourcePort = DISCOVERY_SOURCE_PORT_DEFAULT;
00159 }
00160 sourcePort = (Integer.valueOf(sSourcePort)).intValue();
00161
00162
00163 ServiceManager sm = null;
00164 try {
00165 sm = ServiceManager.getInstance();
00166 } catch (Exception e) {
00167 String err = "Cannot get ServiceManager instance";
00168 logger.log(BasicLevel.ERROR, err);
00169 throw new ServiceException(err, e);
00170 }
00171 jmxService = ((JmxService) sm.getJmxService());
00172 try {
00173 mbeanServer = jmxService.getJmxServer();
00174 } catch (ServiceException e) {
00175
00176 mbeanServer = null;
00177 String err = "Cannot get MBeanServer reference";
00178 logger.log(BasicLevel.ERROR, err);
00179 throw new ServiceException(err, e);
00180 }
00181 }
00182
00187 protected void doStart() throws ServiceException {
00188
00189 DiscoveryManager dm = new DiscoveryManager(listeningPort, listeningIp);
00190 dm.setDomainName(jmxService.getDomainName());
00191 dm.setJonasName(jmxService.getJonasServerName());
00192 JMXServiceURL[] connectorServerURLs = jmxService.getConnectorServerURLs();
00193 String[] urls = new String[connectorServerURLs.length];
00194 for (int i = 0; i < urls.length; i++) {
00195 urls[i] = connectorServerURLs[i].toString();
00196 }
00197 dm.setUrls(urls);
00198 try {
00199
00200 if (mbeanServer != null) {
00201 mbeanServer.registerMBean(dm, JonasObjectName.discoveryManager());
00202 }
00203 } catch (JMException e) {
00204 throw new ServiceException("Problem when starting the Discovery Service: ", e);
00205 }
00206
00207 dm.start();
00208
00209 if (isDiscoveryMaster) {
00210
00211 try {
00212 createEnroller();
00213 } catch (JMException e) {
00214 throw new ServiceException("Problem when starting the Discovery Service: ", e);
00215 }
00216
00217
00218 try {
00219 createDiscClient();
00220 } catch (JMException e) {
00221 throw new ServiceException("Problem when starting the Discovery Service: ", e);
00222 }
00223 }
00224
00225
00226 try {
00227 if (mbeanServer != null) {
00228 mbeanServer.registerMBean(this, JonasObjectName.discoveryService());
00229 }
00230 } catch (JMException e) {
00231 throw new ServiceException("Problem when starting the Discovery Service: ", e);
00232 }
00233 }
00234
00235 private void createEnroller() throws JMException {
00236 Enroller enroller = new Enroller(listeningPort, listeningIp);
00237 if (mbeanServer != null) {
00238 mbeanServer.registerMBean(enroller, JonasObjectName.discoveryEnroller());
00239 }
00240 }
00241
00242 private void createDiscClient() throws JMException {
00243 DiscoveryClient dc = new DiscoveryClient(listeningPort, listeningIp, sourcePort);
00244
00245 if (mbeanServer != null) {
00246 mbeanServer.registerMBean(dc, JonasObjectName.discoveryClient());
00247 }
00248 }
00249
00250
00251
00252
00253 protected void doStop() throws ServiceException {
00254
00255
00256
00257
00258 try {
00259 if (mbeanServer != null) {
00260 mbeanServer.unregisterMBean(JonasObjectName.discoveryService());
00261 }
00262 } catch (JMException e) {
00263 throw new ServiceException("Problem when stoping the Discovery Service: ", e);
00264 }
00265 }
00266 }