[jboss-cvs] JBossAS SVN: r77020 - trunk/tomcat/src/main/org/jboss/web/tomcat/service/injection.
jboss-cvs-commits at lists.jboss.org
jboss-cvs-commits at lists.jboss.org
Wed Aug 13 12:51:15 EDT 2008
Author: emuckenhuber
Date: 2008-08-13 12:51:15 -0400 (Wed, 13 Aug 2008)
New Revision: 77020
Added:
trunk/tomcat/src/main/org/jboss/web/tomcat/service/injection/TomcatInjectionUtils.java
trunk/tomcat/src/main/org/jboss/web/tomcat/service/injection/WebServiceRefInjectionHandler.java
Log:
[JBAS-5673] Utils for jboss web injection
Added: trunk/tomcat/src/main/org/jboss/web/tomcat/service/injection/TomcatInjectionUtils.java
===================================================================
--- trunk/tomcat/src/main/org/jboss/web/tomcat/service/injection/TomcatInjectionUtils.java (rev 0)
+++ trunk/tomcat/src/main/org/jboss/web/tomcat/service/injection/TomcatInjectionUtils.java 2008-08-13 16:51:15 UTC (rev 77020)
@@ -0,0 +1,88 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2008, Red Hat Middleware LLC, and individual contributors
+ * as indicated by the @author tags. See the copyright.txt file in the
+ * distribution for a full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+package org.jboss.web.tomcat.service.injection;
+
+import java.lang.reflect.AccessibleObject;
+import java.util.Collection;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.Map;
+
+import org.jboss.injection.InjectionContainer;
+import org.jboss.injection.InjectionHandler;
+import org.jboss.injection.InjectionUtil;
+import org.jboss.injection.Injector;
+import org.jboss.injection.InjectorFactory;
+import org.jboss.injection.lang.reflect.BeanProperty;
+import org.jboss.injection.lang.reflect.BeanPropertyFactory;
+import org.jboss.logging.Logger;
+import org.jboss.metadata.javaee.spec.RemoteEnvironment;
+import org.jboss.metadata.javaee.spec.ResourceInjectionTargetMetaData;
+import org.jboss.web.tomcat.service.TomcatInjectionContainer;
+
+/**
+ * TomcatInjectionContainer injection utils.
+ *
+ * @author <a href="mailto:emuckenh at redhat.com">Emanuel Muckenhuber</a>
+ * @version $Revision$
+ */
+public class TomcatInjectionUtils extends InjectionUtil
+{
+
+ public <X extends RemoteEnvironment> void processDynamicBeanAnnotations(InjectionContainer container, Collection<InjectionHandler<X>> handlers, Class<?> clazz)
+ {
+ Map<AccessibleObject, Injector> classInjectors = container.getEncInjections().get(clazz.getName());
+ if(classInjectors == null)
+ {
+ classInjectors = new HashMap<AccessibleObject, Injector>();
+ container.getEncInjections().put(clazz.getName(), classInjectors);
+ }
+
+ HashSet<String> visitedMethods = new HashSet<String>();
+ collapseXmlMethodInjectors(visitedMethods, clazz, container.getEncInjections(), classInjectors);
+
+ processClassAnnotations(container, handlers, clazz);
+ visitedMethods = new HashSet<String>();
+ processMethodAnnotations(container, handlers, visitedMethods, clazz, classInjectors);
+ processFieldAnnotations(container, handlers, clazz, classInjectors);
+ }
+
+
+ public static void createInjectors(Map<String, Map<AccessibleObject, Injector>> injectors, ClassLoader classLoader, InjectorFactory<?> factory, Collection<ResourceInjectionTargetMetaData> injectionTargets)
+ {
+ for(ResourceInjectionTargetMetaData injectionTarget : injectionTargets)
+ {
+ Map<AccessibleObject, Injector> map = injectors.get(injectionTarget.getInjectionTargetClass());
+ if(map == null)
+ {
+ map = new HashMap<AccessibleObject, Injector>();
+ injectors.put(injectionTarget.getInjectionTargetClass(), map);
+ }
+
+ AccessibleObject ao = InjectionUtil.findInjectionTarget(classLoader, injectionTarget);
+ BeanProperty property = BeanPropertyFactory.create(ao);
+ map.put(ao, factory.create(property));
+ }
+ }
+
+}
+
Added: trunk/tomcat/src/main/org/jboss/web/tomcat/service/injection/WebServiceRefInjectionHandler.java
===================================================================
--- trunk/tomcat/src/main/org/jboss/web/tomcat/service/injection/WebServiceRefInjectionHandler.java (rev 0)
+++ trunk/tomcat/src/main/org/jboss/web/tomcat/service/injection/WebServiceRefInjectionHandler.java 2008-08-13 16:51:15 UTC (rev 77020)
@@ -0,0 +1,127 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2008, Red Hat Middleware LLC, and individual contributors
+ * as indicated by the @author tags. See the copyright.txt file in the
+ * distribution for a full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+package org.jboss.web.tomcat.service.injection;
+
+import java.lang.reflect.AccessibleObject;
+import java.lang.reflect.AnnotatedElement;
+import java.lang.reflect.Field;
+import java.lang.reflect.Method;
+import java.util.HashMap;
+import java.util.Map;
+
+import org.jboss.injection.InjectionContainer;
+import org.jboss.injection.InjectionUtil;
+import org.jboss.injection.Injector;
+import org.jboss.injection.JndiFieldInjector;
+import org.jboss.injection.JndiMethodInjector;
+import org.jboss.injection.ServiceRefInjector;
+import org.jboss.injection.WebServiceRefHandler;
+import org.jboss.logging.Logger;
+import org.jboss.metadata.javaee.spec.RemoteEnvironment;
+import org.jboss.metadata.javaee.spec.ResourceInjectionTargetMetaData;
+import org.jboss.metadata.javaee.spec.ServiceReferenceMetaData;
+import org.jboss.metadata.javaee.spec.ServiceReferencesMetaData;
+
+/**
+ * A WebServiceRef injection Handler.
+ *
+ * @author <a href="mailto:emuckenh at redhat.com">Emanuel Muckenhuber</a>
+ * @version $Revision$
+ */
+public class WebServiceRefInjectionHandler<X extends RemoteEnvironment> extends WebServiceRefHandler<X>
+{
+ private static final Logger log = Logger.getLogger(WebServiceRefInjectionHandler.class);
+ private Map<String, ServiceReferenceMetaData> srefMap = new HashMap<String, ServiceReferenceMetaData>();
+
+ public void loadXml(X xml, InjectionContainer container)
+ {
+ if (xml == null) return;
+ ServiceReferencesMetaData serviceRefs = xml.getServiceReferences();
+ if (serviceRefs == null) return;
+
+ for (ServiceReferenceMetaData sref : serviceRefs)
+ {
+ log.debug("service-ref: " + sref);
+ if (srefMap.get(sref.getServiceRefName()) != null)
+ throw new IllegalStateException ("Duplicate <service-ref-name> in " + sref);
+
+ srefMap.put(sref.getServiceRefName(), sref);
+
+ String encName = "env/" + sref.getServiceRefName();
+ AnnotatedElement annotatedElement = sref.getAnnotatedElement();
+ if(sref.getInjectionTargets() != null)
+ {
+ for(ResourceInjectionTargetMetaData trg : sref.getInjectionTargets())
+ {
+ // Find the annotatedElement
+ annotatedElement = InjectionUtil.findInjectionTarget(container.getClassloader(), trg);
+
+ // Add a JndiPropertyInjector
+ addInjector(container, encName, annotatedElement);
+
+ // Add the ServicerefEncInjector
+ if(! container.getEncInjections().containsKey(encName))
+ container.getEncInjectors().put(encName, new ServiceRefInjector(encName, annotatedElement, sref));
+ }
+ }
+ else
+ {
+ if(container.getEncInjections().containsKey(encName))
+ return;
+
+ // Class annotations don't create a injectionTarget
+ if(sref.getAnnotatedElement() != null)
+ container.getEncInjectors().put(encName, new ServiceRefInjector(encName, sref.getAnnotatedElement(), sref));
+ }
+ }
+ }
+
+ private void addInjector(InjectionContainer container, String encName, AnnotatedElement annotatedElement)
+ {
+ Injector jndiInjector;
+ if(annotatedElement instanceof Method)
+ {
+ Method method = (Method)annotatedElement;
+ jndiInjector = new JndiMethodInjector((Method)annotatedElement, encName, container.getEnc());
+ addInjector(container, method, method.getDeclaringClass(), jndiInjector);
+ }
+ else if(annotatedElement instanceof Field)
+ {
+ Field field = (Field) annotatedElement;
+ jndiInjector = new JndiFieldInjector((Field)annotatedElement, encName, container.getEnc());
+ addInjector(container, field, field.getDeclaringClass(), jndiInjector);
+ }
+ else
+ throw new IllegalStateException("Annotated element for '" + encName + "' is niether Method nor Field: " + annotatedElement);
+ }
+
+ private void addInjector(InjectionContainer container, AccessibleObject ao, Class<?> declaringClass, Injector injector)
+ {
+ Map<AccessibleObject, Injector> map = container.getEncInjections().get(declaringClass.getName());
+ if(map == null)
+ {
+ map = new HashMap<AccessibleObject, Injector>();
+ container.getEncInjections().put(declaringClass.getName(), map);
+ }
+ map.put(ao, injector);
+ }
+}
More information about the jboss-cvs-commits
mailing list