geronimo-scm mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From djen...@apache.org
Subject svn commit: r1053013 - in /geronimo/server/trunk/plugins/openwebbeans/geronimo-openwebbeans/src/main/java/org/apache/geronimo/openwebbeans: GeronimoResourceInjectionService.java OpenWebBeansWebInitializer.java
Date Mon, 27 Dec 2010 07:24:24 GMT
Author: djencks
Date: Mon Dec 27 07:24:23 2010
New Revision: 1053013

URL: http://svn.apache.org/viewvc?rev=1053013&view=rev
Log:
GERONIMO-5050 we need our own ResourceInjectionService

Added:
    geronimo/server/trunk/plugins/openwebbeans/geronimo-openwebbeans/src/main/java/org/apache/geronimo/openwebbeans/GeronimoResourceInjectionService.java
Modified:
    geronimo/server/trunk/plugins/openwebbeans/geronimo-openwebbeans/src/main/java/org/apache/geronimo/openwebbeans/OpenWebBeansWebInitializer.java

Added: geronimo/server/trunk/plugins/openwebbeans/geronimo-openwebbeans/src/main/java/org/apache/geronimo/openwebbeans/GeronimoResourceInjectionService.java
URL: http://svn.apache.org/viewvc/geronimo/server/trunk/plugins/openwebbeans/geronimo-openwebbeans/src/main/java/org/apache/geronimo/openwebbeans/GeronimoResourceInjectionService.java?rev=1053013&view=auto
==============================================================================
--- geronimo/server/trunk/plugins/openwebbeans/geronimo-openwebbeans/src/main/java/org/apache/geronimo/openwebbeans/GeronimoResourceInjectionService.java
(added)
+++ geronimo/server/trunk/plugins/openwebbeans/geronimo-openwebbeans/src/main/java/org/apache/geronimo/openwebbeans/GeronimoResourceInjectionService.java
Mon Dec 27 07:24:23 2010
@@ -0,0 +1,112 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *  http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+
+package org.apache.geronimo.openwebbeans;
+
+import java.lang.annotation.Annotation;
+import java.lang.reflect.Field;
+import java.lang.reflect.Modifier;
+
+import javax.enterprise.inject.Produces;
+import javax.naming.Context;
+import javax.naming.InitialContext;
+import javax.naming.NamingException;
+import org.apache.webbeans.exception.WebBeansException;
+import org.apache.webbeans.spi.ResourceInjectionService;
+import org.apache.webbeans.spi.api.ResourceReference;
+import org.apache.webbeans.util.AnnotationUtil;
+import org.apache.webbeans.util.SecurityUtil;
+
+/**
+ * @version $Rev:$ $Date:$
+ */
+public class GeronimoResourceInjectionService implements ResourceInjectionService {
+
+    private final Context context;
+
+    public GeronimoResourceInjectionService() {
+        try {
+            this.context = new InitialContext();
+        } catch (NamingException e) {
+            throw new WebBeansException("could not set up naming context", e);
+        }
+    }
+
+    //from StandaloneResourceInjectionService
+    @Override
+    public void injectJavaEEResources(Object managedBeanInstance) throws Exception {
+        Class<?> currentClass = managedBeanInstance.getClass();
+
+        while (currentClass != null && Object.class != currentClass)
+        {
+            Field[] fields = SecurityUtil.doPrivilegedGetDeclaredFields(currentClass);
+
+            for(Field field : fields)
+            {
+                if(!field.isAnnotationPresent(Produces.class))
+                {
+                    if(!Modifier.isStatic(field.getModifiers()))
+                    {
+                        Annotation ann = AnnotationUtil.hasOwbInjectableResource(field.getDeclaredAnnotations());
+                        if(ann != null)
+                        {
+                            @SuppressWarnings("unchecked")
+                            ResourceReference<Object, ?> resourceRef = new ResourceReference(field.getDeclaringClass(),
field.getName(), field.getType(), ann);
+                            boolean acess = field.isAccessible();
+                            try
+                            {
+                                SecurityUtil.doPrivilegedSetAccessible(field, true);
+                                field.set(managedBeanInstance, getResourceReference(resourceRef));
+
+                            }
+                            catch(Exception e)
+                            {
+                                throw new WebBeansException("Unable to inject field" + field,
e);
+
+                            }
+                            finally
+                            {
+                                SecurityUtil.doPrivilegedSetAccessible(field, acess);
+                            }
+                        }
+                    }
+                }
+            }
+
+            currentClass = currentClass.getSuperclass();
+        }
+    }
+
+    @Override
+    public <X, T extends Annotation> X getResourceReference(ResourceReference<X,
T> resourceReference) {
+        String jndiName = resourceReference.getJndiName();
+        Class<X> type = resourceReference.getResourceType();
+
+        try {
+            return type.cast(context.lookup(jndiName));
+        } catch (NamingException e) {
+            throw new WebBeansException("Could not get resource of type " + type + " at jndi
name " + jndiName, e);
+        }
+    }
+
+    @Override
+    public void clear() {
+    }
+}

Modified: geronimo/server/trunk/plugins/openwebbeans/geronimo-openwebbeans/src/main/java/org/apache/geronimo/openwebbeans/OpenWebBeansWebInitializer.java
URL: http://svn.apache.org/viewvc/geronimo/server/trunk/plugins/openwebbeans/geronimo-openwebbeans/src/main/java/org/apache/geronimo/openwebbeans/OpenWebBeansWebInitializer.java?rev=1053013&r1=1053012&r2=1053013&view=diff
==============================================================================
--- geronimo/server/trunk/plugins/openwebbeans/geronimo-openwebbeans/src/main/java/org/apache/geronimo/openwebbeans/OpenWebBeansWebInitializer.java
(original)
+++ geronimo/server/trunk/plugins/openwebbeans/geronimo-openwebbeans/src/main/java/org/apache/geronimo/openwebbeans/OpenWebBeansWebInitializer.java
Mon Dec 27 07:24:23 2010
@@ -25,7 +25,6 @@ import org.apache.webbeans.config.OpenWe
 import org.apache.webbeans.config.WebBeansContext;
 import org.apache.webbeans.el.el22.EL22Adaptor;
 import org.apache.webbeans.lifecycle.LifecycleFactory;
-import org.apache.webbeans.resource.spi.se.StandaloneResourceInjectionService;
 import org.apache.webbeans.spi.ContainerLifecycle;
 import org.apache.webbeans.spi.JNDIService;
 import org.apache.webbeans.spi.ResourceInjectionService;
@@ -72,7 +71,7 @@ public class OpenWebBeansWebInitializer 
         configuration.setProperty(OpenWebBeansConfiguration.JNDI_SERVICE, NoopJndiService.class.getName());
         configuration.setProperty(OpenWebBeansConfiguration.SCANNER_SERVICE, OsgiMetaDataScannerService.class.getName());
         configuration.setProperty(OpenWebBeansConfiguration.CONTEXTS_SERVICE, WebContextsService.class.getName());
-        configuration.setProperty(ResourceInjectionService.class.getName(), StandaloneResourceInjectionService.class.getName());
+        configuration.setProperty(ResourceInjectionService.class.getName(), GeronimoResourceInjectionService.class.getName());
         configuration.setProperty(ELAdaptor.class.getName(), EL22Adaptor.class.getName());
     }
 



Mime
View raw message