1 22 23 package org.objectweb.petals.kernel.admin.service; 24 25 import java.util.Collection ; 26 import java.util.HashMap ; 27 import java.util.HashSet ; 28 import java.util.List ; 29 import java.util.Map ; 30 import java.util.Set ; 31 32 import javax.naming.Binding ; 33 import javax.naming.InitialContext ; 34 import javax.naming.NamingEnumeration ; 35 import javax.naming.NamingException ; 36 37 import org.objectweb.fractal.api.Component; 38 import org.objectweb.fractal.api.control.IllegalLifeCycleException; 39 import org.objectweb.fractal.fraclet.annotation.FractalComponent; 40 import org.objectweb.fractal.fraclet.annotation.Interface; 41 import org.objectweb.fractal.fraclet.annotation.LifeCycle; 42 import org.objectweb.fractal.fraclet.annotation.LifeCycleType; 43 import org.objectweb.fractal.fraclet.annotation.Monolog; 44 import org.objectweb.fractal.fraclet.annotation.Provides; 45 import org.objectweb.fractal.fraclet.annotation.Requires; 46 import org.objectweb.util.monolog.api.Logger; 47 48 import org.objectweb.petals.jbi.management.deployment.DeploymentService; 49 import org.objectweb.petals.jbi.management.service.InstallationService; 50 import org.objectweb.petals.jbi.registry.AbstractEndpoint; 51 import org.objectweb.petals.jbi.registry.Registry; 52 import org.objectweb.petals.jbi.registry.RegistryException; 53 import org.objectweb.petals.jbi.transport.Transporter; 54 import org.objectweb.petals.kernel.admin.ContainerInformation; 55 56 57 import org.objectweb.petals.monitoring.ExchangeStateReport; 58 import org.objectweb.petals.monitoring.RouterMonitor; 59 import org.objectweb.petals.util.JNDIUtil; 60 import org.objectweb.petals.util.LoggingUtil; 61 import org.objectweb.petals.util.PropertyUtil; 62 63 68 @FractalComponent 69 @Provides(interfaces = @Interface(name = "service", signature = org.objectweb.petals.kernel.admin.service.PetalsAdminServiceMBean.class)) 70 public class PetalsAdminService implements PetalsAdminServiceMBean { 71 72 protected static final String CONTAINERS_REF = "containers"; 73 74 protected static final String ENDPOINTS_REF = "endpoints"; 75 76 protected boolean monitoring=true; 77 80 protected LoggingUtil log; 81 82 85 @Monolog(name = "logger") 86 protected Logger logger; 87 88 91 @Requires(name = "deployment", signature = org.objectweb.petals.jbi.management.deployment.DeploymentService.class) 92 protected DeploymentService deployment; 93 94 97 @Requires(name = "installation", signature = org.objectweb.petals.jbi.management.service.InstallationService.class) 98 protected InstallationService installation; 99 100 @Requires(name = "transporter", signature = org.objectweb.petals.jbi.transport.Transporter.class) 101 protected Transporter transporter; 102 103 @Requires(name = "registry", signature = org.objectweb.petals.jbi.registry.Registry.class) 104 protected Registry registry; 105 106 @Requires(name = "router-monitor", signature = org.objectweb.petals.monitoring.RouterMonitor.class) 107 protected RouterMonitor routerMonitor; 108 109 110 protected Component rootComponent; 111 112 118 public void shutdownContainer() throws Exception { 119 log.start(); 120 log 121 .info("Shutdown the server. It will be removed from the Petals network."); 122 try { 123 log.info(" 1/4 Undeploy Service Assemblies."); 124 deployment.shutdown(); 125 } catch (Exception e) { 126 log.error("Can not shutdown the DeploymentService.", e); 127 } 128 try { 129 log.info(" 2/4 Uninstall Shared Libraries and Components."); 130 installation.shutdown(); 131 } catch (Exception e) { 132 log.error("Can not shutdown the InstallationService.", e); 133 } 134 try { 135 log 136 .info(" 3/4 Shutdown the Transporter (remove the server from the global configuration)."); 137 transporter.shutdown(); 138 } catch (Exception e) { 139 log.error("Can not shutdown the Transporter.", e); 140 } 141 142 log.info(" 4/4 Stopping the server..."); 143 System.exit(0); 144 log.end(); 145 } 146 147 public void stopContainer() throws Exception { 148 log.start(); 149 log.info("Stopping the server..."); 150 System.exit(0); 151 log.end(); 152 } 153 154 160 public Collection <Map <String , String >> getAllServersConfiguration() { 161 return retrieveAllServersConfiguration(false); 162 } 163 164 170 public Collection <Map <String , String >> getAllStartedServersConfiguration() { 171 return retrieveAllServersConfiguration(true); 172 } 173 174 182 protected Collection <Map <String , String >> retrieveAllServersConfiguration( 183 boolean startedOnly) { 184 185 log.start(); 186 Set <Map <String , String >> containers = new HashSet <Map <String , String >>(); 187 188 try { 189 InitialContext rootContext = new InitialContext (PropertyUtil 190 .retrieveJNDIProperties()); 191 192 if (JNDIUtil.isBound(rootContext, "/", CONTAINERS_REF)) { 193 194 NamingEnumeration <Binding > enums = rootContext 195 .listBindings(CONTAINERS_REF); 196 197 while (enums.hasMoreElements()) { 198 199 Binding binding = (Binding ) enums.next(); 200 ContainerInformation containerInformation = (ContainerInformation) binding 201 .getObject(); 202 203 if (!startedOnly 204 || (startedOnly && containerInformation.isStarted())) { 205 206 containers 207 .add(createConfigurationMap(containerInformation)); 208 } 209 } 210 } 211 } catch (NamingException e) { 212 log 213 .debug("Problem while trying to retrieve all the containers information : context may not be initialized"); 214 } 215 log.end(); 216 return containers; 217 } 218 219 225 protected Map <String , String > createConfigurationMap( 226 ContainerInformation info) { 227 Map <String , String > map = new HashMap <String , String >(); 228 if (info != null) { 229 map.put(CONF_HOST, info.getHost()); 230 map.put(CONF_HTML_PORT, info.getHtmlPort()); 231 map.put(CONF_JMX_LOGIN, info.getJmxLogin()); 232 map.put(CONF_JMX_PASSWORD, info.getJmxPassword()); 233 map.put(CONF_JMX_PORT, info.getJmxPort()); 234 map.put(CONF_JNDI_FACTORY, info.getJndiFactory()); 235 map.put(CONF_JNDI_PORT, info.getJndiPort()); 236 map.put(CONF_JORAM_DOMAIN, info.getJoramDomain()); 237 map.put(CONF_JORAM_DOMAIN_PORT, info.getJoramDomainPort()); 238 map.put(CONF_JORAM_ID, info.getJoramId()); 239 map.put(CONF_JORAM_LOGIN, info.getJoramLogin()); 240 map.put(CONF_JORAM_PASSWORD, info.getJoramPassword()); 241 map.put(CONF_JORAM_TCP_PORT, info.getJoramTCPPort()); 242 map.put(CONF_NAME, info.getName()); 243 map 244 .put( 245 CONF_STATE, 246 (info.getState() == ContainerInformation.ContainerState.STARTED) ? "started" 247 : "stopped"); 248 map.put(CONF_UID, "" + info.getUid()); 249 } 250 return map; 251 } 252 253 266 public void removeServerFromNetwork(String serverName) throws Exception { 267 268 log.start(); 269 270 InitialContext rootContext; 271 try { 272 rootContext = new InitialContext (PropertyUtil 273 .retrieveJNDIProperties()); 274 } catch (NamingException e) { 275 throw new Exception ("Network configuration can not be accessed.", e); 276 } 277 278 try { 279 Collection <AbstractEndpoint> eps = getServiceEndpointForContainer(serverName); 280 281 rootContext.unbind(CONTAINERS_REF + "/" + serverName); 282 283 for (AbstractEndpoint endpoint : eps) { 284 registry.deregisterInternalEndpoint(endpoint); 285 } 286 287 } catch (NamingException e) { 288 throw new Exception ( 289 "Server not found in the network configuration.", e); 290 } catch (RegistryException e) { 291 throw new Exception ("A ServiceEndpoint failed to be removed.", e); 292 } 293 294 log.end(); 295 } 296 297 305 protected Collection <AbstractEndpoint> getServiceEndpointForContainer( 306 String containerName) throws NamingException { 307 308 log.start(); 309 310 InitialContext rootContext = new InitialContext (PropertyUtil 311 .retrieveJNDIProperties()); 312 313 Collection <AbstractEndpoint> eps = new HashSet <AbstractEndpoint>(); 314 315 if (JNDIUtil.isBound(rootContext, "/", ENDPOINTS_REF)) { 316 317 NamingEnumeration <Binding > enums = rootContext 318 .listBindings(ENDPOINTS_REF); 319 320 while (enums.hasMoreElements()) { 321 322 Binding binding = (Binding ) enums.next(); 323 AbstractEndpoint abstractEndpoint = (AbstractEndpoint) binding 324 .getObject(); 325 326 if (!abstractEndpoint.getContainerName().equals(containerName)) { 327 eps.add(abstractEndpoint); 328 } 329 } 330 } 331 log.end(); 332 333 return eps; 334 } 335 336 337 public boolean getMonitoring(){ 338 return routerMonitor.isMonitoring(); 339 } 340 public void setMonitoring(boolean activate) throws Exception { 341 routerMonitor.setMonitoring(activate); 342 } 343 344 361 public List <List <Map <String ,Object >>> bulkData(long sinceTime){ 362 log.start(); 363 List <ExchangeStateReport> reports = routerMonitor.getReports(sinceTime); 364 List <List <Map <String ,Object >>> bulkDatas = MonitoringService.bulkData(reports); 365 log.end(); 366 return bulkDatas; 367 } 368 369 370 374 377 @LifeCycle(on = LifeCycleType.START) 378 public void start() throws IllegalLifeCycleException { 379 log = new LoggingUtil(logger); 380 } 381 382 } 383 | Popular Tags |