1 22 package org.jboss.kernel.plugins.dependency; 23 24 import java.security.AccessControlContext ; 25 import java.security.AccessController ; 26 import java.security.PrivilegedAction ; 27 import java.util.Iterator ; 28 import java.util.Set ; 29 import java.util.Stack ; 30 31 import org.jboss.beans.info.spi.BeanInfo; 32 import org.jboss.beans.metadata.spi.BeanMetaData; 33 import org.jboss.beans.metadata.spi.MetaDataVisitor; 34 import org.jboss.beans.metadata.spi.MetaDataVisitorNode; 35 import org.jboss.dependency.plugins.AbstractControllerContext; 36 import org.jboss.dependency.plugins.AbstractDependencyInfo; 37 import org.jboss.dependency.spi.Controller; 38 import org.jboss.dependency.spi.ControllerMode; 39 import org.jboss.dependency.spi.ControllerState; 40 import org.jboss.dependency.spi.DependencyInfo; 41 import org.jboss.dependency.spi.DependencyItem; 42 import org.jboss.kernel.Kernel; 43 import org.jboss.kernel.spi.dependency.KernelController; 44 import org.jboss.kernel.spi.dependency.KernelControllerContext; 45 import org.jboss.kernel.spi.metadata.MutableMetaDataContext; 46 import org.jboss.repository.spi.MetaDataContext; 47 import org.jboss.util.JBossStringBuilder; 48 49 55 public class AbstractKernelControllerContext extends AbstractControllerContext implements KernelControllerContext 56 { 57 60 private static final KernelControllerContextActions actions = KernelControllerContextActions.getInstance(); 61 62 65 private static final KernelControllerContextActions noInstantiate = KernelControllerContextActions.getNoInstantiate(); 66 67 70 protected BeanInfo info; 71 72 75 protected BeanMetaData metaData; 76 77 80 protected AccessControlContext accessContext; 81 82 85 protected boolean isDescribeProcessed; 86 87 94 public AbstractKernelControllerContext(BeanInfo info, BeanMetaData metaData, Object target) 95 { 96 super(metaData.getName(), target == null ? actions : noInstantiate, new AbstractDependencyInfo(), target); 97 this.info = info; 98 this.metaData = metaData; 99 ControllerMode mode = metaData.getMode(); 100 if (mode != null) 101 setMode(mode); 102 if (System.getSecurityManager() != null) 103 accessContext = AccessController.getContext(); 104 } 105 106 public Kernel getKernel() 107 { 108 KernelController controller = (KernelController) getController(); 109 if (controller == null) 110 throw new IllegalStateException ("Context is not installed in controller"); 111 return controller.getKernel(); 112 } 113 114 public BeanInfo getBeanInfo() 115 { 116 return info; 117 } 118 119 124 public void setBeanInfo(BeanInfo info) 125 { 126 this.info = info; 127 infoprocessMetaData(); 128 flushJBossObjectCache(); 129 } 130 131 public BeanMetaData getBeanMetaData() 132 { 133 return metaData; 134 } 135 136 public void toString(JBossStringBuilder buffer) 137 { 138 if (metaData != null) 139 buffer.append(" metadata=").append(metaData); 140 super.toString(buffer); 141 } 142 143 public void setController(Controller controller) 144 { 145 super.setController(controller); 146 preprocessMetaData(); 147 } 148 149 152 protected void preprocessMetaData() 153 { 154 if (metaData == null) 155 return; 156 PreprocessMetaDataVisitor visitor = new PreprocessMetaDataVisitor(metaData); 157 AccessController.doPrivileged(visitor); 158 } 159 160 163 protected void infoprocessMetaData() 164 { 165 if (info == null) 166 { 167 removeClassContextReference(); 168 return; 169 } 170 if (isDescribeProcessed) return; 171 DescribedMetaDataVisitor visitor = new DescribedMetaDataVisitor(metaData); 172 AccessController.doPrivileged(visitor); 173 isDescribeProcessed = true; 174 } 175 176 public MutableMetaDataContext getMetaDataContext() 177 { 178 if (info != null) 179 { 180 try 181 { 182 return (MutableMetaDataContext)info.getMetaDataContext(); 183 } 184 catch (ClassCastException e) 185 { 186 throw new RuntimeException ("MetaDataContext must be an instance of MutableMetaDataContext"); 187 } 188 } 189 return null; 190 } 191 192 public void setMetaDataContext(MetaDataContext mctx) 193 { 194 info = info.getInstanceInfo(); 195 info.setMetaDataContext(mctx); 196 } 197 198 205 protected AccessControlContext getAccessControlContext() 206 { 207 return accessContext; 208 } 209 210 private void removeClassContextReference() 211 { 212 DependencyInfo dependencyInfo = getDependencyInfo(); 213 if (dependencyInfo != null) 214 { 215 Set dependencys = dependencyInfo.getIDependOn(ClassContextDependencyItem.class); 216 for(Iterator it = dependencys.iterator(); it.hasNext();) 217 { 218 DependencyItem di = (DependencyItem) it.next(); 219 di.unresolved(getController()); 220 } 221 } 222 } 223 224 protected abstract class AbstractMetaDataVistor implements MetaDataVisitor, PrivilegedAction <Object > 225 { 226 229 protected ControllerState contextState = ControllerState.INSTANTIATED; 230 231 234 protected BeanMetaData bmd; 235 236 239 protected Stack <MetaDataVisitorNode> visitorNodeStack; 240 241 protected AbstractMetaDataVistor(BeanMetaData bmd) 242 { 243 this.bmd = bmd; 244 this.visitorNodeStack = new Stack <MetaDataVisitorNode>(); 245 } 246 247 public void initialVisit(MetaDataVisitorNode node) 248 { 249 visitorNodeStack.push(node); 250 try 251 { 252 internalInitialVisit(node); 253 } 254 finally 255 { 256 visitorNodeStack.pop(); 257 } 258 } 259 260 public void describeVisit(MetaDataVisitorNode node) 261 { 262 visitorNodeStack.push(node); 263 try 264 { 265 internalDescribeVisit(node); 266 } 267 finally 268 { 269 visitorNodeStack.pop(); 270 } 271 } 272 273 protected void internalInitialVisit(MetaDataVisitorNode node) 274 { 275 } 276 277 protected void internalDescribeVisit(MetaDataVisitorNode node) 278 { 279 } 280 281 public KernelControllerContext getControllerContext() 282 { 283 return AbstractKernelControllerContext.this; 284 } 285 286 public ControllerState getContextState() 287 { 288 return contextState; 289 } 290 291 public void addDependency(DependencyItem dependency) 292 { 293 getDependencyInfo().addIDependOn(dependency); 294 } 295 296 public void setContextState(ControllerState contextState) 297 { 298 this.contextState = contextState; 299 } 300 301 public Stack <MetaDataVisitorNode> visitorNodeStack() 302 { 303 return visitorNodeStack; 304 } 305 306 } 307 308 311 protected class PreprocessMetaDataVisitor extends AbstractMetaDataVistor 312 { 313 318 public PreprocessMetaDataVisitor(BeanMetaData bmd) 319 { 320 super(bmd); 321 } 322 323 326 public Object run() 327 { 328 bmd.initialVisit(this); 329 visitorNodeStack = null; 330 return null; 331 } 332 333 338 protected void internalInitialVisit(MetaDataVisitorNode node) 339 { 340 boolean trace = log.isTraceEnabled(); 341 if (trace) 342 log.trace("Initial visit node " + node); 343 344 Iterator children = node.getChildren(); 346 if (children != null) 347 { 348 ControllerState restoreState = contextState; 349 while (children.hasNext()) 350 { 351 MetaDataVisitorNode child = (MetaDataVisitorNode) children.next(); 352 try 353 { 354 child.initialVisit(this); 355 } 356 finally 357 { 358 contextState = restoreState; 359 } 360 } 361 } 362 } 363 364 } 365 366 369 protected class DescribedMetaDataVisitor extends AbstractMetaDataVistor 370 { 371 376 public DescribedMetaDataVisitor(BeanMetaData bmd) 377 { 378 super(bmd); 379 } 380 381 384 public Object run() 385 { 386 bmd.describeVisit(this); 387 visitorNodeStack = null; 388 return null; 389 } 390 391 396 protected void internalDescribeVisit(MetaDataVisitorNode node) 397 { 398 boolean trace = log.isTraceEnabled(); 399 if (trace) 400 log.trace("Describe visit node " + node); 401 402 Iterator children = node.getChildren(); 404 if (children != null) 405 { 406 ControllerState restoreState = contextState; 407 while (children.hasNext()) 408 { 409 MetaDataVisitorNode child = (MetaDataVisitorNode) children.next(); 410 try 411 { 412 child.describeVisit(this); 413 } 414 finally 415 { 416 contextState = restoreState; 417 } 418 } 419 } 420 } 421 } 422 423 } | Popular Tags |