1 22 package org.jboss.deployers.plugins.deployer; 23 24 import java.io.Serializable ; 25 import java.util.Collections ; 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 org.jboss.deployers.plugins.attachments.AbstractAttachments; 33 import org.jboss.deployers.plugins.structure.ComponentDeploymentContext; 34 import org.jboss.deployers.spi.DeploymentException; 35 import org.jboss.deployers.spi.attachments.Attachments; 36 import org.jboss.deployers.spi.classloader.ClassLoaderFactory; 37 import org.jboss.deployers.spi.deployer.DeploymentUnit; 38 import org.jboss.deployers.spi.structure.DeploymentContext; 39 import org.jboss.logging.Logger; 40 import org.jboss.virtual.VirtualFile; 41 42 52 public class AbstractDeploymentUnit extends AbstractAttachments 53 implements DeploymentUnit, Serializable 54 { 55 private static final Logger log = Logger.getLogger(AbstractDeploymentUnit.class); 56 private static final long serialVersionUID = 1; 57 58 59 private DeploymentContext deploymentContext; 60 61 66 public AbstractDeploymentUnit(DeploymentContext deploymentContext) 67 { 68 if (deploymentContext == null) 69 throw new IllegalArgumentException ("Null deployment context"); 70 this.deploymentContext = deploymentContext; 71 } 72 73 public String getName() 74 { 75 return deploymentContext.getName(); 76 } 77 78 public String getSimpleName() 79 { 80 return deploymentContext.getSimpleName(); 81 } 82 83 89 public String getRelativePath() 90 { 91 return deploymentContext.getRelativePath(); 92 } 93 94 99 public VirtualFile getFile(String name) 100 { 101 VirtualFile root = deploymentContext.getRoot(); 102 VirtualFile file = null; 103 try 104 { 105 file = root.findChild(name); 106 } 107 catch(Exception e) 108 { 109 if( log.isTraceEnabled() ) 110 log.trace("Failed to find: "+name, e); 111 } 112 return file; 113 } 114 115 public ClassLoader getClassLoader() 116 { 117 ClassLoader cl = deploymentContext.getClassLoader(); 118 if (cl == null) 119 throw new IllegalStateException ("ClassLoader has not been set"); 120 deploymentContext.deployed(); 121 return cl; 122 } 123 124 public boolean createClassLoader(ClassLoaderFactory factory) throws DeploymentException 125 { 126 return deploymentContext.createClassLoader(factory); 127 } 128 129 public VirtualFile getMetaDataFile(String name) 130 { 131 return deploymentContext.getMetaDataFile(name); 132 } 133 134 public List <VirtualFile> getMetaDataFiles(String name, String suffix) 135 { 136 return deploymentContext.getMetaDataFiles(name, suffix); 137 } 138 139 public DeploymentUnit addComponent(String name) 140 { 141 ComponentDeploymentContext component = new ComponentDeploymentContext(name, deploymentContext); 142 AbstractDeploymentUnit unit = new AbstractDeploymentUnit(component); 143 component.setDeploymentUnit(unit); 144 deploymentContext.addComponent(component); 145 return unit; 146 } 147 148 public boolean removeComponent(String name) 149 { 150 if (name == null) 151 throw new IllegalArgumentException ("Null name"); 152 153 for (DeploymentContext component : deploymentContext.getComponents()) 154 { 155 if (name.equals(component.getName())) 156 return deploymentContext.removeComponent(component); 157 } 158 return false; 159 } 160 161 public Map <String , Object > getAttachments() 162 { 163 DeploymentContext parent = deploymentContext.getParent(); 164 if (deploymentContext.isComponent() == false) 165 parent = null; 166 HashMap <String , Object > result = new HashMap <String , Object >(); 167 if (parent != null) 168 result.putAll(parent.getTransientAttachments().getAttachments()); 169 result.putAll(deploymentContext.getTransientAttachments().getAttachments()); 170 if (parent != null) 171 result.putAll(parent.getTransientManagedObjects().getAttachments()); 172 result.putAll(deploymentContext.getTransientManagedObjects().getAttachments()); 173 if (parent != null) 174 result.putAll(parent.getPredeterminedManagedObjects().getAttachments()); 175 result.putAll(deploymentContext.getPredeterminedManagedObjects().getAttachments()); 176 if (result.isEmpty() == false) 177 deploymentContext.deployed(); 178 return Collections.unmodifiableMap(result); 179 } 180 181 public Object addAttachment(String name, Object attachment) 182 { 183 deploymentContext.deployed(); 184 return deploymentContext.getTransientAttachments().addAttachment(name, attachment); 185 } 186 187 public Object getAttachment(String name) 188 { 189 DeploymentContext parent = deploymentContext.getParent(); 190 if (deploymentContext.isComponent() == false) 191 parent = null; 192 Object result = deploymentContext.getPredeterminedManagedObjects().getAttachment(name); 193 if (result != null) 194 { 195 deploymentContext.deployed(); 196 return result; 197 } 198 if (parent != null) 199 { 200 result = parent.getPredeterminedManagedObjects().getAttachment(name); 201 if (result != null) 202 { 203 deploymentContext.deployed(); 204 return result; 205 } 206 } 207 result = deploymentContext.getTransientManagedObjects().getAttachment(name); 208 if (result != null) 209 { 210 deploymentContext.deployed(); 211 return result; 212 } 213 if (parent != null) 214 { 215 result = parent.getTransientManagedObjects().getAttachment(name); 216 if (result != null) 217 { 218 deploymentContext.deployed(); 219 return result; 220 } 221 } 222 result = deploymentContext.getTransientAttachments().getAttachment(name); 223 if (result != null) 224 { 225 deploymentContext.deployed(); 226 return result; 227 } 228 if (parent != null) 229 { 230 result = parent.getTransientAttachments().getAttachment(name); 231 if (result != null) 232 { 233 deploymentContext.deployed(); 234 return result; 235 } 236 } 237 return null; 238 } 239 240 public boolean isAttachmentPresent(String name) 241 { 242 DeploymentContext parent = deploymentContext.getParent(); 243 if (deploymentContext.isComponent() == false) 244 parent = null; 245 if (deploymentContext.getPredeterminedManagedObjects().isAttachmentPresent(name)) 246 { 247 deploymentContext.deployed(); 248 return true; 249 } 250 if (parent != null && parent.getPredeterminedManagedObjects().isAttachmentPresent(name)) 251 { 252 deploymentContext.deployed(); 253 return true; 254 } 255 if (deploymentContext.getTransientManagedObjects().isAttachmentPresent(name)) 256 { 257 deploymentContext.deployed(); 258 return true; 259 } 260 if (parent != null && parent.getTransientAttachments().isAttachmentPresent(name)) 261 { 262 deploymentContext.deployed(); 263 return true; 264 } 265 if (deploymentContext.getTransientAttachments().isAttachmentPresent(name)) 266 { 267 deploymentContext.deployed(); 268 return true; 269 } 270 if (parent != null && parent.getTransientAttachments().isAttachmentPresent(name)) 271 { 272 deploymentContext.deployed(); 273 return true; 274 } 275 return false; 276 } 277 278 public Object removeAttachment(String name) 279 { 280 return deploymentContext.getTransientAttachments().removeAttachment(name); 281 } 282 283 public Attachments getTransientManagedObjects() 284 { 285 return deploymentContext.getTransientManagedObjects(); 286 } 287 288 @SuppressWarnings ("unchecked") 289 public <T> Set <? extends T> getAllMetaData(Class <T> type) 291 { 292 if (type == null) 293 throw new IllegalArgumentException ("Null type"); 294 295 Set <T> result = new HashSet <T>(); 296 Map <String , Object > attachments = getAttachments(); 297 for (Object object : attachments.values()) 298 { 299 if (type.isInstance(object)) 300 result.add((T) object); 301 } 302 if (result.isEmpty() == false) 303 deploymentContext.deployed(); 304 return result; 305 } 306 307 @Deprecated 308 public DeploymentContext getDeploymentContext() 309 { 310 deploymentContext.deployed(); 311 return deploymentContext; 312 } 313 } 314 | Popular Tags |