1 22 package org.jboss.deployers.plugins.structure; 23 24 import java.io.Serializable ; 25 import java.util.Collections ; 26 import java.util.List ; 27 import java.util.Set ; 28 import java.util.concurrent.CopyOnWriteArraySet ; 29 30 import org.jboss.deployers.plugins.attachments.AttachmentsImpl; 31 import org.jboss.deployers.spi.DeploymentException; 32 import org.jboss.deployers.spi.attachments.Attachments; 33 import org.jboss.deployers.spi.classloader.ClassLoaderFactory; 34 import org.jboss.deployers.spi.deployer.DeploymentUnit; 35 import org.jboss.deployers.spi.structure.DeploymentContext; 36 import org.jboss.deployers.spi.structure.DeploymentContextVisitor; 37 import org.jboss.deployers.spi.structure.DeploymentState; 38 import org.jboss.deployers.spi.structure.StructureDetermined; 39 import org.jboss.logging.Logger; 40 import org.jboss.virtual.VirtualFile; 41 42 48 public class ComponentDeploymentContext 49 implements DeploymentContext, Serializable 50 { 51 private static final long serialVersionUID = 1; 52 53 54 protected Logger log = Logger.getLogger(getClass()); 55 56 57 private String name; 58 59 60 private DeploymentUnit unit; 61 62 63 private DeploymentContext parent; 64 65 66 private Set <DeploymentContext> components = new CopyOnWriteArraySet <DeploymentContext>(); 67 68 69 private transient Attachments transientAttachments = new AttachmentsImpl(); 70 71 72 private transient Attachments transientManagedObjects = new AttachmentsImpl(); 73 74 81 public ComponentDeploymentContext(String name, DeploymentContext parent) 82 { 83 if (name == null) 84 throw new IllegalArgumentException ("Null name"); 85 if (parent == null) 86 throw new IllegalArgumentException ("Null parent"); 87 this.name = name; 88 this.parent = parent; 89 } 90 91 public String getName() 92 { 93 return name; 94 } 95 96 public String getSimpleName() 97 { 98 return parent.getSimpleName(); 99 } 100 public String getRelativePath() 101 { 102 return parent.getRelativePath(); 103 } 104 105 public StructureDetermined getStructureDetermined() 106 { 107 return parent.getStructureDetermined(); 108 } 109 110 public void setStructureDetermined(StructureDetermined determined) 111 { 112 throw new UnsupportedOperationException ("Not supported for components"); 113 } 114 115 public boolean isCandidate() 116 { 117 return parent.isCandidate(); 118 } 119 120 public DeploymentState getState() 121 { 122 return parent.getState(); 123 } 124 125 public void setState(DeploymentState state) 126 { 127 parent.setState(state); 128 } 129 130 public DeploymentUnit getDeploymentUnit() 131 { 132 if (unit == null) 133 throw new IllegalStateException ("Deployment unit has not been set"); 134 return unit; 135 } 136 137 public void setDeploymentUnit(DeploymentUnit unit) 138 { 139 this.unit = unit; 140 } 141 142 public VirtualFile getRoot() 143 { 144 return parent.getRoot(); 145 } 146 147 152 public void setRoot(VirtualFile root) 153 { 154 throw new UnsupportedOperationException ("Not supported for components"); 155 } 156 157 public void setMetaDataPath(String path) 158 { 159 throw new UnsupportedOperationException ("Not supported for components"); 160 } 161 162 public VirtualFile getMetaDataLocation() 163 { 164 return parent.getMetaDataLocation(); 165 } 166 167 public void setMetaDataLocation(VirtualFile location) 168 { 169 throw new UnsupportedOperationException ("Not supported for components"); 170 } 171 172 public ClassLoader getClassLoader() 173 { 174 return parent.getClassLoader(); 175 } 176 177 public void setClassLoader(ClassLoader classLoader) 178 { 179 throw new UnsupportedOperationException ("Not supported for components"); 180 } 181 182 public boolean createClassLoader(ClassLoaderFactory factory) throws DeploymentException 183 { 184 return false; 185 } 186 187 public void removeClassLoader() 188 { 189 } 190 191 public List <VirtualFile> getClassPath() 192 { 193 return parent.getClassPath(); 194 } 195 196 public void setClassPath(List <VirtualFile> paths) 197 { 198 throw new UnsupportedOperationException ("Not supported for components"); 199 } 200 201 public boolean isTopLevel() 202 { 203 return false; 204 } 205 206 public DeploymentContext getTopLevel() 207 { 208 return parent.getTopLevel(); 209 } 210 211 public DeploymentContext getParent() 212 { 213 return parent; 214 } 215 216 public void setParent(DeploymentContext parent) 217 { 218 throw new UnsupportedOperationException ("Not supported for components"); 219 } 220 221 public Set <DeploymentContext> getChildren() 222 { 223 return Collections.emptySet(); 224 } 225 226 public void addChild(DeploymentContext child) 227 { 228 throw new UnsupportedOperationException ("Not supported for components"); 229 } 230 231 public boolean removeChild(DeploymentContext child) 232 { 233 throw new UnsupportedOperationException ("Not supported for components"); 234 } 235 236 public boolean isComponent() 237 { 238 return true; 239 } 240 241 public Set <DeploymentContext> getComponents() 242 { 243 return Collections.unmodifiableSet(components); 244 } 245 246 public void addComponent(DeploymentContext component) 247 { 248 if (component == null) 249 throw new IllegalArgumentException ("Null component"); 250 components.add(component); 251 } 252 253 public boolean removeComponent(DeploymentContext component) 254 { 255 if (component == null) 256 throw new IllegalArgumentException ("Null component"); 257 return components.remove(component); 258 } 259 260 public void visit(DeploymentContextVisitor visitor) throws DeploymentException 261 { 262 if (visitor == null) 263 throw new IllegalArgumentException ("Null visitor"); 264 265 visit(this, visitor); 266 } 267 268 275 private void visit(DeploymentContext context, DeploymentContextVisitor visitor) throws DeploymentException 276 { 277 visitor.visit(context); 278 try 279 { 280 Set <DeploymentContext> children = context.getChildren(); 281 if (children.isEmpty()) 282 return; 283 284 DeploymentContext[] childContexts = children.toArray(new DeploymentContext[children.size()]); 285 for (int i = 0; i < childContexts.length; ++i) 286 { 287 if (childContexts[i] == null) 288 throw new IllegalStateException ("Null child context for " + context.getName() + " children=" + children); 289 try 290 { 291 visit(childContexts[i], visitor); 292 } 293 catch (Throwable t) 294 { 295 for (int j = i-1; j >= 0; --j) 296 visitError(childContexts[j], visitor, true); 297 throw DeploymentException.rethrowAsDeploymentException("Error visiting: " + childContexts[i].getName(), t); 298 } 299 } 300 } 301 catch (Throwable t) 302 { 303 visitError(context, visitor, false); 304 throw DeploymentException.rethrowAsDeploymentException("Error visiting: " + context.getName(), t); 305 } 306 } 307 308 316 private void visitError(DeploymentContext context, DeploymentContextVisitor visitor, boolean visitChildren) throws DeploymentException 317 { 318 if (visitChildren) 319 { 320 Set <DeploymentContext> children = context.getChildren(); 321 if (children.isEmpty()) 322 return; 323 324 for (DeploymentContext child : children) 325 { 326 try 327 { 328 visitError(child, visitor, true); 329 } 330 catch (Throwable t) 331 { 332 log.warn("Error during visit error: " + child.getName(), t); 333 } 334 } 335 336 } 337 try 338 { 339 visitor.error(context); 340 } 341 catch (Throwable t) 342 { 343 log.warn("Error during visit error: " + context.getName(), t); 344 } 345 } 346 347 public Attachments getPredeterminedManagedObjects() 348 { 349 return parent.getPredeterminedManagedObjects(); 350 } 351 352 public Attachments getTransientManagedObjects() 353 { 354 return transientManagedObjects; 355 } 356 357 public Attachments getTransientAttachments() 358 { 359 return transientAttachments; 360 } 361 362 public Throwable getProblem() 363 { 364 return parent.getProblem(); 365 } 366 367 public void setProblem(Throwable problem) 368 { 369 parent.setProblem(problem); 370 } 371 372 public VirtualFile getMetaDataFile(String name) 373 { 374 return parent.getMetaDataFile(name); 375 } 376 377 public List <VirtualFile> getMetaDataFiles(String name, String suffix) 378 { 379 return parent.getMetaDataFiles(name, suffix); 380 } 381 382 public void deployed() 383 { 384 parent.deployed(); 385 } 386 387 public boolean isDeployed() 388 { 389 return parent.isDeployed(); 390 } 391 392 public void reset() 393 { 394 components.clear(); 395 396 transientManagedObjects.clear(); 397 transientAttachments.clear(); 398 } 399 400 public String toString() 401 { 402 StringBuilder buffer = new StringBuilder (); 403 buffer.append(getClass().getSimpleName()); 404 buffer.append('@'); 405 buffer.append(System.identityHashCode(this)); 406 buffer.append('{').append(name).append('}'); 407 return buffer.toString(); 408 } 409 } 410 | Popular Tags |