1 22 package org.jboss.kernel.plugins.deployment; 23 24 import java.security.AccessController ; 25 import java.security.PrivilegedAction ; 26 import java.util.HashSet ; 27 import java.util.Iterator ; 28 import java.util.List ; 29 import java.util.ListIterator ; 30 import java.util.Set ; 31 32 import org.jboss.beans.metadata.spi.BeanMetaData; 33 import org.jboss.beans.metadata.spi.ClassLoaderMetaData; 34 import org.jboss.dependency.spi.ControllerContext; 35 import org.jboss.dependency.spi.ControllerMode; 36 import org.jboss.dependency.spi.ControllerState; 37 import org.jboss.dependency.spi.DependencyInfo; 38 import org.jboss.dependency.spi.DependencyItem; 39 import org.jboss.kernel.Kernel; 40 import org.jboss.kernel.plugins.dependency.AbstractKernelControllerContext; 41 import org.jboss.kernel.spi.dependency.KernelController; 42 import org.jboss.kernel.spi.dependency.KernelControllerContext; 43 import org.jboss.kernel.spi.deployment.KernelDeployment; 44 import org.jboss.logging.Logger; 45 import org.jboss.util.JBossStringBuilder; 46 47 54 public class AbstractKernelDeployer 55 { 56 57 private static final Logger log = Logger.getLogger(AbstractKernelDeployer.class); 58 59 60 protected Kernel kernel; 61 62 63 protected KernelController controller; 64 65 66 protected ControllerState requiredState; 67 68 69 protected ControllerMode mode; 70 71 76 public AbstractKernelDeployer(Kernel kernel) 77 { 78 this(kernel, null, null); 79 } 80 81 88 public AbstractKernelDeployer(final Kernel kernel, ControllerState requiredState, ControllerMode mode) 89 { 90 if (kernel == null) 91 throw new IllegalArgumentException ("Null kernel"); 92 this.kernel = kernel; 93 PrivilegedAction <Object > action = new PrivilegedAction <Object >() 94 { 95 public Object run() 96 { 97 controller = kernel.getController(); 98 return null; 99 } 100 }; 101 AccessController.doPrivileged(action); 102 this.requiredState = requiredState; 103 this.mode = mode; 104 } 105 106 112 public void deploy(final KernelDeployment deployment) throws Throwable 113 { 114 if (deployment.isInstalled()) 115 throw new IllegalArgumentException ("Already installed " + deployment.getName()); 116 117 try 118 { 119 deployBeans(controller, deployment); 120 deployment.setInstalled(true); 121 } 122 catch (Throwable t) 123 { 124 undeploy(deployment); 125 throw t; 126 } 127 } 128 129 136 public void change(KernelDeployment deployment, ControllerState state) throws Throwable 137 { 138 if (deployment.isInstalled() == false) 139 throw new IllegalStateException ("Not installed " + deployment.getName()); 140 141 changeBeans(controller, deployment, state); 142 } 143 144 149 public void undeploy(KernelDeployment deployment) 150 { 151 undeployBeans(controller, deployment); 152 deployment.setInstalled(false); 153 } 154 155 160 public void validate() throws Throwable 161 { 162 Set notInstalled = controller.getNotInstalled(); 163 internalValidate(notInstalled); 164 } 165 166 172 public void validate(KernelDeployment deployment) throws Throwable 173 { 174 Set <KernelControllerContext> notInstalled = new HashSet <KernelControllerContext>(deployment.getInstalledContexts()); 175 internalValidate(notInstalled); 176 } 177 178 184 protected void internalValidate(Set notInstalled) throws Throwable 185 { 186 if (notInstalled.isEmpty() == false) 187 { 188 for (Iterator i = notInstalled.iterator(); i.hasNext();) 189 { 190 KernelControllerContext context = (KernelControllerContext) i.next(); 191 if (context.getState().equals(context.getRequiredState())) 192 i.remove(); 193 } 194 if (notInstalled.isEmpty() == false) 195 { 196 HashSet <KernelControllerContext> errors = new HashSet <KernelControllerContext>(); 197 HashSet <KernelControllerContext> incomplete = new HashSet <KernelControllerContext>(); 198 for (Iterator i = notInstalled.iterator(); i.hasNext();) 199 { 200 KernelControllerContext ctx = (KernelControllerContext) i.next(); 201 if (ctx.getState().equals(ControllerState.ERROR)) 202 errors.add(ctx); 203 else 204 incomplete.add(ctx); 205 } 206 JBossStringBuilder buffer = new JBossStringBuilder(); 207 buffer.append("Incompletely deployed:\n"); 208 if (errors.size() != 0) 209 { 210 buffer.append("\n*** DEPLOYMENTS IN ERROR: Name -> Error\n"); 211 for (Iterator i = errors.iterator(); i.hasNext(); ) 212 { 213 KernelControllerContext ctx = (KernelControllerContext) i.next(); 214 buffer.append(ctx.getName()).append(" -> ").append(ctx.getError().toString()).append('\n'); 215 } 216 } 217 if (incomplete.size() != 0) 218 { 219 buffer.append("\n*** DEPLOYMENTS MISSING DEPENDENCIES: Name -> Dependency{Required State:Actual State}\n"); 220 for (Iterator i = incomplete.iterator(); i.hasNext();) 221 { 222 KernelControllerContext ctx = (KernelControllerContext) i.next(); 223 buffer.append(ctx.getName()).append(" -> "); 224 DependencyInfo dependsInfo = ctx.getDependencyInfo(); 225 Set depends = dependsInfo.getIDependOn(null); 226 for (Iterator j = depends.iterator(); j.hasNext();) 227 { 228 DependencyItem item = (DependencyItem) j.next(); 229 buffer.append(item.getIDependOn()).append('{').append(item.getWhenRequired().getStateString()); 230 buffer.append(':'); 231 Object iDependOn = item.getIDependOn(); 232 if (iDependOn == null) 233 { 234 buffer.append("** UNRESOLVED " + item + " **"); 236 } 237 else 238 { 239 ControllerContext other = controller.getContext(item.getIDependOn(), null); 240 if (other == null) 241 buffer.append("** NOT FOUND **"); 242 else 243 buffer.append(other.getState().getStateString()); 244 } 245 buffer.append('}'); 246 if (j.hasNext()) 247 buffer.append(", "); 248 } 249 buffer.append('\n'); 250 } 251 } 252 throw new IllegalStateException (buffer.toString()); 253 } 254 } 255 } 256 257 264 protected void deployBeans(KernelController controller, KernelDeployment deployment) throws Throwable 265 { 266 List beans = deployment.getBeans(); 267 if (beans != null) 268 { 269 for (int i = 0; i < beans.size(); ++i) 270 { 271 BeanMetaData metaData = (BeanMetaData) beans.get(i); 272 KernelControllerContext context = deployBean(controller, deployment, metaData); 273 deployment.addInstalledContext(context); 274 } 275 } 276 } 277 278 287 protected KernelControllerContext deployBean(KernelController controller, KernelDeployment deployment, BeanMetaData bean) throws Throwable 288 { 289 KernelControllerContext context = new AbstractKernelControllerContext(null, bean, null); 290 if (requiredState != null) 291 context.setRequiredState(requiredState); 292 if (mode != null) 293 context.setMode(mode); 294 ClassLoaderMetaData beanClassLoader = bean.getClassLoader(); 296 if (beanClassLoader == null && deployment != null) 297 { 298 ClassLoaderMetaData deploymentClassLoader = deployment.getClassLoader(); 299 if (deploymentClassLoader != null) 300 bean.setClassLoader(deploymentClassLoader); 301 } 302 controller.install(context); 303 return context; 304 } 305 306 314 protected void changeBeans(KernelController controller, KernelDeployment deployment, ControllerState state) throws Throwable 315 { 316 List contexts = deployment.getInstalledContexts(); 317 if (contexts != null) 318 { 319 for (Iterator i = contexts.iterator(); i.hasNext();) 320 { 321 KernelControllerContext context = (KernelControllerContext) i.next(); 322 changeBean(controller, context, state); 323 } 324 } 325 } 326 327 335 protected void changeBean(KernelController controller, KernelControllerContext context, ControllerState state) throws Throwable 336 { 337 if (ControllerMode.MANUAL.equals(context.getMode()) && ControllerState.ERROR.equals(context.getState()) == false) 338 controller.change(context, state); 339 } 340 341 347 protected void undeployBeans(KernelController controller, KernelDeployment deployment) 348 { 349 List contexts = deployment.getInstalledContexts(); 350 if (contexts.isEmpty() == false) 351 { 352 for (ListIterator i = contexts.listIterator(contexts.size()); i.hasPrevious();) 353 { 354 KernelControllerContext context = (KernelControllerContext) i.previous(); 355 try 356 { 357 undeployBean(controller, context); 358 deployment.removeInstalledContext(context); 359 } 360 catch (Throwable ignored) 361 { 362 log.warn("Ignored error during uninstall of " + context, ignored); 363 } 364 } 365 } 366 } 367 368 375 protected void undeployBean(KernelController controller, KernelControllerContext context) throws Throwable 376 { 377 controller.uninstall(context.getName()); 378 } 379 } 380 | Popular Tags |