1 22 package org.jboss.kernel.plugins.dependency; 23 24 import java.util.Iterator ; 25 import java.util.List ; 26 import java.util.Set ; 27 28 import org.jboss.beans.info.spi.BeanInfo; 29 import org.jboss.beans.metadata.spi.AnnotationMetaData; 30 import org.jboss.beans.metadata.spi.BeanMetaData; 31 import org.jboss.beans.metadata.spi.PropertyMetaData; 32 import org.jboss.dependency.plugins.AbstractDependencyItem; 33 import org.jboss.dependency.spi.ControllerContext; 34 import org.jboss.dependency.spi.ControllerState; 35 import org.jboss.dependency.spi.DependencyInfo; 36 import org.jboss.kernel.Kernel; 37 import org.jboss.kernel.spi.config.KernelConfigurator; 38 import org.jboss.kernel.spi.dependency.KernelController; 39 import org.jboss.kernel.spi.dependency.KernelControllerContext; 40 import org.jboss.kernel.spi.metadata.MutableMetaDataContext; 41 import org.jboss.metadata.spi.repository.MetaDataRepository; 42 import org.jboss.repository.spi.KernelRepository; 43 import org.jboss.repository.spi.MetaDataContext; 44 import org.jboss.repository.spi.MetaDataContextFactory; 45 46 52 public class DescribeAction extends KernelControllerContextAction 53 { 54 public void installAction(KernelControllerContext context) throws Throwable 55 { 56 KernelController controller = (KernelController) context.getController(); 57 Kernel kernel = controller.getKernel(); 58 KernelConfigurator configurator = kernel.getConfigurator(); 59 60 BeanMetaData metaData = context.getBeanMetaData(); 61 if (metaData.getBean() != null) 62 { 63 BeanInfo info = configurator.getBeanInfo(metaData); 64 context.setBeanInfo(info); 65 66 info = addAnnotations(context, metaData, info); 67 68 DependencyInfo depends = context.getDependencyInfo(); 69 List <Object > dependencies = info.getDependencies(); 71 if (dependencies != null) 72 { 73 for (Object dependencyName : dependencies) 74 { 75 AbstractDependencyItem dependency = new AbstractDependencyItem(metaData.getName(), dependencyName, ControllerState.INSTANTIATED, ControllerState.INSTALLED); 76 depends.addIDependOn(dependency); 77 } 78 } 79 } 80 } 81 82 public void uninstallAction(KernelControllerContext context) 83 { 84 context.setMetaDataContext(null); 85 context.setBeanInfo(null); 86 } 87 88 92 private BeanInfo addAnnotations(KernelControllerContext context, BeanMetaData beanMetaData, BeanInfo beanInfo) 93 { 94 MutableMetaDataContext metaCtx = addClassAnnotations(context, beanMetaData, beanInfo); 95 addPropertyAnnotations(metaCtx, context, beanMetaData, beanInfo); 96 return context.getBeanInfo(); 97 } 98 99 private MutableMetaDataContext addClassAnnotations(KernelControllerContext context, BeanMetaData beanMetaData, BeanInfo beanInfo) 100 { 101 Set <AnnotationMetaData> annotations = beanMetaData.getAnnotations(); 102 103 MutableMetaDataContext metaCtx = null; 104 105 if (annotations != null && annotations.size() > 0) 106 { 107 metaCtx = getMetaDataContext(context); 108 if (metaCtx != null) 109 { 110 metaCtx.addAnnotations(annotations); 111 } 112 } 113 114 return metaCtx; 115 } 116 117 private MutableMetaDataContext addPropertyAnnotations(MutableMetaDataContext metaCtx, KernelControllerContext context, BeanMetaData beanMetaData, BeanInfo beanInfo) 118 { 119 Set properties = beanMetaData.getProperties(); 120 121 if (properties != null && properties.size() > 0) 122 { 123 for (Iterator it = properties.iterator() ; it.hasNext() ; ) 124 { 125 PropertyMetaData property = (PropertyMetaData)it.next(); 126 Set propertyAnnotations = property.getAnnotations(); 127 if (propertyAnnotations != null && propertyAnnotations.size() > 0) 128 { 129 if (metaCtx == null) 130 { 131 metaCtx = getMetaDataContext(context); 132 } 133 if (metaCtx != null) 134 { 135 Set propertyInfos = beanInfo.getProperties(); 136 if (propertyInfos != null && propertyInfos.size() > 0) 137 { 138 metaCtx.addPropertyAnnotations(property.getName(), beanInfo.getProperties(), propertyAnnotations); 139 } 140 } 141 } 142 } 143 } 144 145 return metaCtx; 146 } 147 148 private MutableMetaDataContext getMetaDataContext(KernelControllerContext context) 149 { 150 ControllerContext repCtx = context.getController().getContext("Repository", ControllerState.INSTALLED); 152 153 if (repCtx == null) 154 { 155 log.warn("You have defined annotations for bean '" + context.getName() + "', but no MetaDataRepository has been installed under the name 'Repository'"); 156 return null; 157 } 158 159 MetaDataRepository repository = (MetaDataRepository)repCtx.getTarget(); 160 MetaDataContextFactory metaFactory = context.getBeanInfo().getMetaDataContextFactory(); 161 ClassLoader beanLoader = context.getBeanInfo().getClassInfo().getType().getClassLoader(); 162 MetaDataContext metaCtx = metaFactory.getMetaDataContext(beanLoader, repository, (String )context.getName()); 163 164 if (metaCtx instanceof MutableMetaDataContext == false) 165 { 166 throw new RuntimeException ("MetaDataContext must be mutable"); 167 } 168 169 context.setMetaDataContext(metaCtx); 170 171 return (MutableMetaDataContext)metaCtx; 172 } 173 174 } | Popular Tags |