1 22 package org.jboss.aop.microcontainer.beans; 23 24 import java.util.ArrayList ; 25 import java.util.Iterator ; 26 import java.util.List ; 27 28 import org.jboss.beans.metadata.plugins.AbstractBeanMetaData; 29 import org.jboss.beans.metadata.plugins.AbstractDependencyValueMetaData; 30 import org.jboss.beans.metadata.plugins.AbstractInstallMetaData; 31 import org.jboss.beans.metadata.plugins.AbstractParameterMetaData; 32 import org.jboss.beans.metadata.plugins.AbstractPropertyMetaData; 33 import org.jboss.beans.metadata.plugins.ThisValueMetaData; 34 import org.jboss.beans.metadata.plugins.factory.GenericBeanFactoryMetaData; 35 import org.jboss.beans.metadata.spi.BeanMetaData; 36 import org.jboss.beans.metadata.spi.BeanMetaDataFactory; 37 import org.jboss.beans.metadata.spi.InstallMetaData; 38 import org.jboss.beans.metadata.spi.MetaDataVisitorNode; 39 import org.jboss.beans.metadata.spi.ParameterMetaData; 40 import org.jboss.beans.metadata.spi.PropertyMetaData; 41 42 48 public class AspectBeanMetaDataFactory extends GenericBeanFactoryMetaData implements BeanMetaDataFactory 49 { 50 51 final static String DEFAULT_ASPECT_MANAGER = "AspectManager"; 52 53 54 private String pointcut; 55 56 57 private String managerBean = DEFAULT_ASPECT_MANAGER; 58 59 60 private String managerProperty; 61 62 67 public String getPointcut() 68 { 69 return pointcut; 70 } 71 72 77 public void setPointcut(String pointcut) 78 { 79 this.pointcut = pointcut; 80 } 81 82 83 public String getManager() 84 { 85 return managerBean; 86 } 87 88 public void setManagerBean(String managerBean) 89 { 90 this.managerBean = managerBean; 91 } 92 93 public String getManagerProperty() 94 { 95 return managerProperty; 96 } 97 98 public void setManagerProperty(String aspectManagerProperty) 99 { 100 this.managerProperty = aspectManagerProperty; 101 } 102 103 public List <BeanMetaData> getBeans() 104 { 105 ArrayList <BeanMetaData> result = new ArrayList <BeanMetaData>(); 106 107 108 result.add(this); 109 110 String aspectName = name + "$Aspect"; 111 AbstractBeanMetaData aspect = new AbstractBeanMetaData(); 112 aspect.setName(aspectName); 113 aspect.setBean("org.jboss.aop.microcontainer.beans.Aspect"); 114 aspect.addProperty(getAspectManagerPropertyMetaData("manager")); 115 result.add(aspect); 116 117 String aspectBindingName = name + "$AspectBinding"; 118 AbstractBeanMetaData aspectBinding = new AbstractBeanMetaData(); 119 aspectBinding.setName(aspectBindingName); 120 aspectBinding.setBean("org.jboss.aop.microcontainer.beans.AspectBinding"); 121 aspectBinding.addProperty(new AbstractPropertyMetaData("pointcut", pointcut)); 122 aspectBinding.addProperty(new AbstractPropertyMetaData("aspect", new AbstractDependencyValueMetaData(aspectName, "definition"))); 123 aspectBinding.addProperty(getAspectManagerPropertyMetaData("manager")); 124 result.add(aspectBinding); 125 126 if (hasInjectedBeans()) 127 { 128 configureWithDependencies(aspect, aspectBinding); 129 } 130 else 131 { 132 configureNoDependencies(aspect, aspectBinding); 133 } 134 135 136 return result; 137 } 138 139 protected PropertyMetaData getAspectManagerPropertyMetaData(String name) 140 { 141 return new AbstractPropertyMetaData(name, new AbstractDependencyValueMetaData(managerBean, managerProperty)); 142 } 143 144 private void configureWithDependencies(AbstractBeanMetaData aspect, AbstractBeanMetaData aspectBinding) 145 { 146 aspect.addProperty(new AbstractPropertyMetaData("adviceBean", name)); 147 148 AbstractInstallMetaData installAspect = new AbstractInstallMetaData(); 149 installAspect.setBean(aspect.getName()); 150 installAspect.setMethodName("install"); 151 ArrayList <ParameterMetaData> parameters = new ArrayList <ParameterMetaData>(); 152 parameters.add(new AbstractParameterMetaData(new ThisValueMetaData())); 153 installAspect.setParameters(parameters); 154 155 AbstractInstallMetaData installBinding = new AbstractInstallMetaData(); 156 installBinding.setBean(aspectBinding.getName()); 157 installBinding.setMethodName("rebind"); 158 parameters = new ArrayList <ParameterMetaData>(); 159 parameters.add(new AbstractParameterMetaData(new AbstractDependencyValueMetaData(aspect.getName(), "definition"))); 160 installBinding.setParameters(parameters); 161 162 List <InstallMetaData> installs = getInstalls(); 163 if (installs == null) 164 installs = new ArrayList <InstallMetaData>(); 165 installs.add(installAspect); 166 installs.add(installBinding); 167 setInstalls(installs); 168 169 AbstractInstallMetaData uninstallAspect = new AbstractInstallMetaData(); 170 uninstallAspect.setBean(aspect.getName()); 171 uninstallAspect.setMethodName("uninstall"); 172 173 List <InstallMetaData> uninstalls = getUninstalls(); 174 if (uninstalls == null) 175 uninstalls = new ArrayList <InstallMetaData>(); 176 uninstalls.add(uninstallAspect); 177 setUninstalls(uninstalls); 178 179 } 180 181 private void configureNoDependencies(AbstractBeanMetaData aspect, AbstractBeanMetaData aspectBinding) 182 { 183 aspect.addProperty(new AbstractPropertyMetaData("advice", new AbstractDependencyValueMetaData(name))); 184 } 185 186 187 private boolean hasInjectedBeans() 188 { 189 ArrayList <AbstractDependencyValueMetaData> dependencies = new ArrayList <AbstractDependencyValueMetaData>(); 190 getDependencies(dependencies, this); 191 192 for (AbstractDependencyValueMetaData dep : dependencies) 193 { 194 if(!((String )dep.getValue()).startsWith("jboss.kernel:service=")) 195 { 196 return true; 197 } 198 } 199 return false; 200 } 201 202 private void getDependencies(ArrayList <AbstractDependencyValueMetaData> dependencies, MetaDataVisitorNode node) 203 { 204 Iterator children = node.getChildren(); 205 206 if (children != null) 207 { 208 while (children.hasNext()) 209 { 210 MetaDataVisitorNode child = (MetaDataVisitorNode)children.next(); 211 if (child instanceof AbstractDependencyValueMetaData) 212 { 213 dependencies.add((AbstractDependencyValueMetaData)child); 214 } 215 getDependencies(dependencies, child); 216 } 217 } 218 } 219 220 } 221 | Popular Tags |