1 22 package org.jboss.system.microcontainer; 23 24 import java.security.AccessController ; 25 import java.security.PrivilegedAction ; 26 import java.util.Iterator ; 27 28 import javax.management.MBeanServer ; 29 import javax.management.ObjectName ; 30 31 import org.jboss.dependency.plugins.AbstractControllerContext; 32 import org.jboss.dependency.spi.Controller; 33 import org.jboss.dependency.spi.ControllerMode; 34 import org.jboss.dependency.spi.ControllerState; 35 import org.jboss.dependency.spi.DependencyItem; 36 import org.jboss.system.Service; 37 import org.jboss.system.ServiceContext; 38 import org.jboss.system.ServiceController; 39 import org.jboss.system.metadata.ServiceMetaData; 40 import org.jboss.system.metadata.ServiceMetaDataVisitor; 41 import org.jboss.system.metadata.ServiceMetaDataVisitorNode; 42 43 49 public class ServiceControllerContext extends AbstractControllerContext 50 { 51 52 private ObjectName objectName; 53 54 55 private ServiceController serviceController; 56 57 58 private ServiceMetaData serviceMetaData; 59 60 61 private ServiceContext serviceContext = new ServiceContext(); 62 63 69 public ServiceControllerContext(ServiceController serviceController, ObjectName name) 70 { 71 super(name.getCanonicalName(), ServiceControllerContextActions.getLifecycleOnly()); 72 this.objectName = name; 73 serviceContext.objectName = objectName; 74 this.serviceController = serviceController; 75 setMode(ControllerMode.MANUAL); 76 } 77 78 84 public ServiceControllerContext(ServiceController serviceController, ServiceMetaData metaData) 85 { 86 super(metaData.getObjectName().getCanonicalName(), ServiceControllerContextActions.getInstance()); 87 this.objectName = metaData.getObjectName(); 88 serviceContext.objectName = objectName; 89 this.serviceController = serviceController; 90 ControllerMode mode = metaData.getMode(); 91 this.serviceMetaData = metaData; 92 if (mode == null) 93 setMode(ControllerMode.MANUAL); 94 else 95 setMode(mode); 96 } 97 98 105 public ServiceControllerContext(ServiceController serviceController, ObjectName name, Object target) 106 { 107 super(name.getCanonicalName(), ServiceControllerContextActions.getInstance()); 108 this.objectName = name; 109 serviceContext.objectName = objectName; 110 this.serviceController = serviceController; 111 setTarget(target); 112 setMode(ControllerMode.MANUAL); 113 } 114 115 120 public ObjectName getObjectName() 121 { 122 return objectName; 123 } 124 125 130 public ServiceMetaData getServiceMetaData() 131 { 132 return serviceMetaData; 133 } 134 135 140 public void setServiceMetaData(ServiceMetaData serviceMetaData) 141 { 142 this.serviceMetaData = serviceMetaData; 143 } 144 145 150 public ServiceController getServiceController() 151 { 152 return serviceController; 153 } 154 155 161 public Service getServiceProxy() throws Exception 162 { 163 if (serviceContext.proxy != null) 164 return serviceContext.proxy; 165 166 MBeanServer server = serviceController.getMBeanServer(); 167 if (server != null) 168 serviceContext.proxy = ServiceProxy.getServiceProxy(objectName, server); 169 170 return serviceContext.proxy; 171 } 172 173 178 public ServiceContext getServiceContext() 179 { 180 try 181 { 182 serviceContext.proxy = getServiceProxy(); 183 } 184 catch (Exception ignored) 185 { 186 } 187 serviceContext.problem = getError(); 188 if (getState() == ControllerState.ERROR) 189 serviceContext.state = ServiceContext.FAILED; 190 return serviceContext; 191 } 192 193 public void install(ControllerState fromState, ControllerState toState) throws Throwable 195 { 196 try 197 { 198 super.install(fromState, toState); 199 } 200 catch (Throwable t) 201 { 202 serviceContext.problem = t; 203 serviceContext.state = ServiceContext.FAILED; 204 throw t; 205 } 206 } 207 208 public void setController(Controller controller) 212 { 213 super.setController(controller); 214 if (controller != null) 215 { 216 preprocessMetaData(); 217 serviceContext.state = ServiceContext.INSTALLED; 218 } 219 else 220 serviceContext.state = ServiceContext.NOTYETINSTALLED; 221 } 222 223 226 protected void preprocessMetaData() 227 { 228 if (serviceMetaData == null) 229 return; 230 PreprocessMetaDataVisitor visitor = new PreprocessMetaDataVisitor(); 231 AccessController.doPrivileged(visitor); 232 } 233 234 237 protected class PreprocessMetaDataVisitor implements ServiceMetaDataVisitor, PrivilegedAction <Object > 238 { 239 240 private ControllerState contextState = ControllerState.INSTANTIATED; 241 242 245 public Object run() 246 { 247 serviceMetaData.visit(this); 248 return null; 249 } 250 251 256 public void visit(ServiceMetaDataVisitorNode node) 257 { 258 boolean trace = log.isTraceEnabled(); 259 if (trace) 260 log.trace("Visit node " + node); 261 262 Iterator <? extends ServiceMetaDataVisitorNode> children = node.getChildren(); 264 if (children != null) 265 { 266 ControllerState restoreState = contextState; 267 while (children.hasNext()) 268 { 269 ServiceMetaDataVisitorNode child = children.next(); 270 try 271 { 272 child.visit(this); 273 } 274 finally 275 { 276 contextState = restoreState; 277 } 278 } 279 } 280 } 281 282 public ServiceControllerContext getControllerContext() 283 { 284 return ServiceControllerContext.this; 285 } 286 287 public ControllerState getContextState() 288 { 289 return contextState; 290 } 291 292 public void addDependency(DependencyItem dependency) 293 { 294 getDependencyInfo().addIDependOn(dependency); 295 } 296 297 public void setContextState(ControllerState contextState) 298 { 299 this.contextState = contextState; 300 } 301 } 302 } 303 | Popular Tags |