1 22 package org.jboss.beans.metadata.plugins; 23 24 import java.util.Stack ; 25 26 import org.jboss.beans.metadata.spi.BeanMetaData; 27 import org.jboss.beans.metadata.spi.MetaDataVisitor; 28 import org.jboss.beans.metadata.spi.MetaDataVisitorNode; 29 import org.jboss.beans.metadata.spi.PropertyMetaData; 30 import org.jboss.dependency.spi.ControllerState; 31 import org.jboss.joinpoint.spi.TargettedJoinpoint; 32 import org.jboss.kernel.plugins.config.Configurator; 33 import org.jboss.kernel.spi.config.KernelConfigurator; 34 import org.jboss.kernel.spi.dependency.KernelController; 35 import org.jboss.kernel.spi.dependency.KernelControllerContext; 36 import org.jboss.reflect.spi.ClassInfo; 37 import org.jboss.util.JBossStringBuilder; 38 39 45 public abstract class AbstractTypeMetaData extends AbstractValueMetaData 46 { 47 50 protected String type; 51 52 55 protected KernelController controller; 56 57 60 protected KernelConfigurator configurator; 61 62 65 protected String propertyName; 66 67 70 protected String beanName; 71 72 75 public AbstractTypeMetaData() 76 { 77 } 78 79 84 public AbstractTypeMetaData(String value) 85 { 86 super(value); 87 } 88 89 94 public void setType(String type) 95 { 96 this.type = type; 97 } 98 99 public String getType() 100 { 101 return type; 102 } 103 104 public void initialVisit(MetaDataVisitor visitor) 105 { 106 controller = (KernelController) visitor.getControllerContext().getController(); 107 configurator = visitor.getControllerContext().getKernel().getConfigurator(); 108 preparePreinstantiatedLookup(visitor); 109 visitor.initialVisit(this); 110 } 111 112 private void preparePreinstantiatedLookup(MetaDataVisitor visitor) 113 { 114 Stack <MetaDataVisitorNode> visitorNodes = visitor.visitorNodeStack(); 115 MetaDataVisitorNode parent = visitorNodes.pop(); 117 try 118 { 119 if (parent instanceof PropertyMetaData) 120 { 121 PropertyMetaData pmd = (PropertyMetaData) parent; 122 propertyName = pmd.getName(); 123 Object gp = visitorNodes.peek(); 124 if (gp instanceof BeanMetaData) 125 { 126 BeanMetaData bmd = (BeanMetaData) gp; 127 beanName = bmd.getName(); 128 } 129 } 130 } 131 finally 132 { 133 visitorNodes.push(parent); 134 } 135 } 136 137 @SuppressWarnings ("unchecked") 138 protected Object preinstantiatedLookup(ClassLoader cl, Class expected) 139 { 140 Object result = null; 141 if (propertyName != null && beanName != null) 142 { 143 try 144 { 145 KernelControllerContext context = (KernelControllerContext) controller.getContext(beanName, ControllerState.INSTANTIATED); 146 TargettedJoinpoint joinpoint = configurator.getPropertyGetterJoinPoint(context.getBeanInfo(), propertyName); 147 joinpoint.setTarget(context.getTarget()); 148 result = joinpoint.dispatch(); 149 } 150 catch (Throwable t) 151 { 152 log.warn("Exception in preinstantiated lookup: " + t); 153 } 154 if (result != null && expected != null && expected.isAssignableFrom(result.getClass()) == false) 155 throw new ClassCastException (result.getClass() + " is not a " + expected.getName()); 156 } 157 return result; 158 } 159 160 165 public void setConfigurator(KernelConfigurator configurator) 166 { 167 this.configurator = configurator; 168 } 169 170 public void toString(JBossStringBuilder buffer) 171 { 172 super.toString(buffer); 173 if (type != null) 174 buffer.append(" type=").append(type); 175 } 176 177 184 protected ClassInfo getClassInfo(ClassLoader cl) throws Throwable 185 { 186 return getClassInfo(type, cl); 187 } 188 189 protected ClassInfo getClassInfo(String classType, ClassLoader cl) throws Throwable 190 { 191 if (classType == null) 192 return null; 193 194 return configurator.getClassInfo(classType, cl); 195 } 196 197 protected Class getClass(MetaDataVisitor visitor, String classType) throws Throwable 198 { 199 KernelControllerContext context = visitor.getControllerContext(); 200 ClassLoader cl = Configurator.getClassLoader(context.getBeanMetaData()); 201 return getClassInfo(classType, cl).getType(); 202 } 203 204 } 205 | Popular Tags |