When I first saw your email, I did wonder whether the bean manager code
would be enough, and maybe I was just missing the ArchiveAppender stuff to
get the Enricher over to the server side. I've retested with the EJB lookup
commented out, and unfortunately, It didn't work without my change for the
test case I was using (arquillian-showcase-ejb).

Jon

On Thu, Oct 27, 2011 at 5:29 AM, Romain Manni-Bucau
<[email protected]>wrote:

> Hi,
>
> Why beanmanager stuff is not enough?
>
> - Romain
>
> ---------- Message transféré ----------
> De : <[email protected]>
> Date : 27 oct. 2011 01:08
> Objet : svn commit: r1189526 - in /openejb/trunk/arquillian-tomee: ./
> arquillian-tomee-common/src/main/java/org/apache/openejb/arquillian/common/
> arquillian-tomee-remote/
> arquillian-tomee-remote/src/main/java/org/apache/openejb/arquillian/remote/
> arquillian-to...
> À : <[email protected]>
>
> Author: jgallimore
> Date: Wed Oct 26 23:08:05 2011
> New Revision: 1189526
>
> URL: http://svn.apache.org/viewvc?rev=1189526&view=rev
> Log:
> Progress on supporting enriching tests with @EJB fields
>
> Added:
>
>
>  
> openejb/trunk/arquillian-tomee/arquillian-tomee-remote/src/main/java/org/apache/openejb/arquillian/remote/RemoteTomEEEJBEnricherArchiveAppender.java
>
>
>  
> openejb/trunk/arquillian-tomee/arquillian-tomee-remote/src/main/java/org/apache/openejb/arquillian/remote/RemoteTomEEEJBEnricherExtension.java
>
>
>  
> openejb/trunk/arquillian-tomee/arquillian-tomee-remote/src/main/java/org/apache/openejb/arquillian/remote/SecurityActions.java
> Removed:
>
>
>  
> openejb/trunk/arquillian-tomee/arquillian-tomee-remote/src/main/resources/META-INF/services/org.jboss.arquillian.spi.TestEnricher
> Modified:
>
>
>  
> openejb/trunk/arquillian-tomee/arquillian-tomee-common/src/main/java/org/apache/openejb/arquillian/common/TomEEContainer.java
>   openejb/trunk/arquillian-tomee/arquillian-tomee-remote/pom.xml
>
>
>  
> openejb/trunk/arquillian-tomee/arquillian-tomee-remote/src/main/java/org/apache/openejb/arquillian/remote/RemoteTomEEContainer.java
>
>
>  
> openejb/trunk/arquillian-tomee/arquillian-tomee-remote/src/main/java/org/apache/openejb/arquillian/remote/RemoteTomEEEnricher.java
>
>
>  
> openejb/trunk/arquillian-tomee/arquillian-tomee-remote/src/main/java/org/apache/openejb/arquillian/remote/RemoteTomEEExtension.java
>
>
>  
> openejb/trunk/arquillian-tomee/arquillian-tomee-remote/src/test/java/org/apache/openejb/arquillian/TomEEContainerTest.java
>   openejb/trunk/arquillian-tomee/pom.xml
>
> Modified:
>
> openejb/trunk/arquillian-tomee/arquillian-tomee-common/src/main/java/org/apache/openejb/arquillian/common/TomEEContainer.java
> URL:
>
> http://svn.apache.org/viewvc/openejb/trunk/arquillian-tomee/arquillian-tomee-common/src/main/java/org/apache/openejb/arquillian/common/TomEEContainer.java?rev=1189526&r1=1189525&r2=1189526&view=diff
>
> ==============================================================================
> ---
>
> openejb/trunk/arquillian-tomee/arquillian-tomee-common/src/main/java/org/apache/openejb/arquillian/common/TomEEContainer.java
> (original)
> +++
>
> openejb/trunk/arquillian-tomee/arquillian-tomee-common/src/main/java/org/apache/openejb/arquillian/common/TomEEContainer.java
> Wed Oct 26 23:08:05 2011
> @@ -36,6 +36,7 @@ import org.jboss.arquillian.container.sp
>  import
> org.jboss.arquillian.container.spi.client.protocol.metadata.Servlet;
>  import org.jboss.shrinkwrap.api.Archive;
>  import org.jboss.shrinkwrap.api.exporter.ZipExporter;
> +import org.jboss.shrinkwrap.api.spec.WebArchive;
>  import org.jboss.shrinkwrap.descriptor.api.Descriptor;
>
>  public abstract class TomEEContainer implements
> DeployableContainer<TomEEConfiguration> {
> @@ -86,7 +87,7 @@ public abstract class TomEEContainer imp
>    }
>
>    public ProtocolDescription getDefaultProtocol() {
> -        return new ProtocolDescription("Servlet 3.0");
> +        return new ProtocolDescription("Servlet 2.5");
>    }
>
>    public ProtocolMetaData deploy(Archive<?> archive) throws
> DeploymentException {
> @@ -107,7 +108,12 @@ public abstract class TomEEContainer imp
>            moduleIds.put(archive.getName(), file.getAbsolutePath());
>
>            HTTPContext httpContext = new HTTPContext("0.0.0.0",
> configuration.getHttpPort());
> -            httpContext.add(new Servlet("ArquillianServletRunner", "/" +
> getArchiveNameWithoutExtension(archive)));
> +            if (archive instanceof WebArchive) {
> +               httpContext.add(new Servlet("ArquillianServletRunner", "/"
> +
> getArchiveNameWithoutExtension(archive)));
> +            } else {
> +               httpContext.add(new Servlet("ArquillianServletRunner",
> "/arquillian-protocol"));
> +            }
> +
>            // we should probably get all servlets and add them to the
> context
>            return new ProtocolMetaData().addContext(httpContext);
>        } catch (Exception e) {
>
> Modified: openejb/trunk/arquillian-tomee/arquillian-tomee-remote/pom.xml
> URL:
>
> http://svn.apache.org/viewvc/openejb/trunk/arquillian-tomee/arquillian-tomee-remote/pom.xml?rev=1189526&r1=1189525&r2=1189526&view=diff
>
> ==============================================================================
> --- openejb/trunk/arquillian-tomee/arquillian-tomee-remote/pom.xml
> (original)
> +++ openejb/trunk/arquillian-tomee/arquillian-tomee-remote/pom.xml Wed Oct
> 26 23:08:05 2011
> @@ -333,5 +333,12 @@
>       <artifactId>swizzle-stream</artifactId>
>       <version>1.0.2</version>
>    </dependency>
> +    <dependency>
> +       <groupId>org.jboss.shrinkwrap.descriptors</groupId>
> +       <artifactId>shrinkwrap-descriptors-impl</artifactId>
> +       <version>1.1.0-alpha-2</version>
> +       <type>jar</type>
> +       <scope>compile</scope>
> +    </dependency>
>  </dependencies>
>  </project>
>
> Modified:
>
> openejb/trunk/arquillian-tomee/arquillian-tomee-remote/src/main/java/org/apache/openejb/arquillian/remote/RemoteTomEEContainer.java
> URL:
>
> http://svn.apache.org/viewvc/openejb/trunk/arquillian-tomee/arquillian-tomee-remote/src/main/java/org/apache/openejb/arquillian/remote/RemoteTomEEContainer.java?rev=1189526&r1=1189525&r2=1189526&view=diff
>
> ==============================================================================
> ---
>
> openejb/trunk/arquillian-tomee/arquillian-tomee-remote/src/main/java/org/apache/openejb/arquillian/remote/RemoteTomEEContainer.java
> (original)
> +++
>
> openejb/trunk/arquillian-tomee/arquillian-tomee-remote/src/main/java/org/apache/openejb/arquillian/remote/RemoteTomEEContainer.java
> Wed Oct 26 23:08:05 2011
> @@ -77,6 +77,7 @@ public class RemoteTomEEContainer extend
>
>                   System.setProperty("catalina.home",
> openejbHome.getAbsolutePath());
>                           System.setProperty("catalina.base",
> openejbHome.getAbsolutePath());
> +
>
> System.setProperty("openejb.deploymentId.format","{appId}/{ejbJarId}/{ejbName}");
>                           Paths paths = new Paths(new
> File(openejbHome.getAbsolutePath(), "/webapps/openejb"));
>                           Installer installer = new Installer(paths, true);
>                           installer.installAll();
>
> Added:
>
> openejb/trunk/arquillian-tomee/arquillian-tomee-remote/src/main/java/org/apache/openejb/arquillian/remote/RemoteTomEEEJBEnricherArchiveAppender.java
> URL:
>
> http://svn.apache.org/viewvc/openejb/trunk/arquillian-tomee/arquillian-tomee-remote/src/main/java/org/apache/openejb/arquillian/remote/RemoteTomEEEJBEnricherArchiveAppender.java?rev=1189526&view=auto
>
> ==============================================================================
> ---
>
> openejb/trunk/arquillian-tomee/arquillian-tomee-remote/src/main/java/org/apache/openejb/arquillian/remote/RemoteTomEEEJBEnricherArchiveAppender.java
> (added)
> +++
>
> openejb/trunk/arquillian-tomee/arquillian-tomee-remote/src/main/java/org/apache/openejb/arquillian/remote/RemoteTomEEEJBEnricherArchiveAppender.java
> Wed Oct 26 23:08:05 2011
> @@ -0,0 +1,18 @@
> +package org.apache.openejb.arquillian.remote;
> +
> +import org.jboss.arquillian.container.test.spi.RemoteLoadableExtension;
> +import
>
> org.jboss.arquillian.container.test.spi.client.deployment.AuxiliaryArchiveAppender;
> +import org.jboss.shrinkwrap.api.Archive;
> +import org.jboss.shrinkwrap.api.ShrinkWrap;
> +import org.jboss.shrinkwrap.api.spec.JavaArchive;
> +
> +public class RemoteTomEEEJBEnricherArchiveAppender implements
> AuxiliaryArchiveAppender
> +{
> +          @Override
> +          public Archive<?> createAuxiliaryArchive()
> +          {
> +             return ShrinkWrap.create(JavaArchive.class,
> "arquillian-tomee-testenricher-ejb.jar")
> +                         .addClasses(RemoteTomEEEnricher.class,
> RemoteTomEEEJBEnricherExtension.class, SecurityActions.class)
> +
> .addAsServiceProvider(RemoteLoadableExtension.class,
> RemoteTomEEEJBEnricherExtension.class);
> +          }
> +       }
> \ No newline at end of file
>
> Added:
>
> openejb/trunk/arquillian-tomee/arquillian-tomee-remote/src/main/java/org/apache/openejb/arquillian/remote/RemoteTomEEEJBEnricherExtension.java
> URL:
>
> http://svn.apache.org/viewvc/openejb/trunk/arquillian-tomee/arquillian-tomee-remote/src/main/java/org/apache/openejb/arquillian/remote/RemoteTomEEEJBEnricherExtension.java?rev=1189526&view=auto
>
> ==============================================================================
> ---
>
> openejb/trunk/arquillian-tomee/arquillian-tomee-remote/src/main/java/org/apache/openejb/arquillian/remote/RemoteTomEEEJBEnricherExtension.java
> (added)
> +++
>
> openejb/trunk/arquillian-tomee/arquillian-tomee-remote/src/main/java/org/apache/openejb/arquillian/remote/RemoteTomEEEJBEnricherExtension.java
> Wed Oct 26 23:08:05 2011
> @@ -0,0 +1,13 @@
> +package org.apache.openejb.arquillian.remote;
> +
> +import org.jboss.arquillian.container.test.spi.RemoteLoadableExtension;
> +import org.jboss.arquillian.test.spi.TestEnricher;
> +
> +public class RemoteTomEEEJBEnricherExtension implements
> RemoteLoadableExtension {
> +    @Override public void register(ExtensionBuilder builder) {
> +       // only load if EJB is on ClassPath
> +        if(Validate.classExists("javax.ejb.EJB")) {
> +           builder.service(TestEnricher.class,
> org.apache.openejb.arquillian.remote.RemoteTomEEEnricher.class);
> +        }
> +    }
> +}
>
> Modified:
>
> openejb/trunk/arquillian-tomee/arquillian-tomee-remote/src/main/java/org/apache/openejb/arquillian/remote/RemoteTomEEEnricher.java
> URL:
>
> http://svn.apache.org/viewvc/openejb/trunk/arquillian-tomee/arquillian-tomee-remote/src/main/java/org/apache/openejb/arquillian/remote/RemoteTomEEEnricher.java?rev=1189526&r1=1189525&r2=1189526&view=diff
>
> ==============================================================================
> ---
>
> openejb/trunk/arquillian-tomee/arquillian-tomee-remote/src/main/java/org/apache/openejb/arquillian/remote/RemoteTomEEEnricher.java
> (original)
> +++
>
> openejb/trunk/arquillian-tomee/arquillian-tomee-remote/src/main/java/org/apache/openejb/arquillian/remote/RemoteTomEEEnricher.java
> Wed Oct 26 23:08:05 2011
> @@ -3,45 +3,122 @@ package org.apache.openejb.arquillian.re
>  import org.apache.openejb.cdi.ThreadSingletonServiceImpl;
>  import org.jboss.arquillian.test.spi.TestEnricher;
>
> +import javax.ejb.EJB;
>  import javax.enterprise.context.spi.CreationalContext;
>  import javax.enterprise.inject.spi.AnnotatedType;
>  import javax.enterprise.inject.spi.BeanManager;
>  import javax.enterprise.inject.spi.InjectionTarget;
> +import javax.naming.Context;
> +import javax.naming.InitialContext;
> +import javax.naming.NamingException;
> +
> +import java.lang.annotation.Annotation;
> +import java.lang.reflect.Field;
>  import java.lang.reflect.Method;
> +import java.util.List;
>
>  public class RemoteTomEEEnricher implements TestEnricher {
> -    @Override public void enrich(Object testCase) {
> -//        BeanManager mgr = getBeanManager();
> -//        if (mgr != null) {
> -//            AnnotatedType<?> type =
>  mgr.createAnnotatedType(getClass());
> -//            InjectionTarget<Object> injectionTarget =
> (InjectionTarget<Object>) mgr.createInjectionTarget(type);
> -//            CreationalContext<Object> context =
> mgr.createCreationalContext(null);
> -//
> -//            injectionTarget.inject(testCase, context);
> -//            injectionTarget.postConstruct(this);
> -//        } else {
> -//            throw new NullPointerException("bean manager is null");
> -//        }
> -
> -        /* TODO: see if this code could be called after tests
> -                *
> -                * if (injectionTarget != null) {
> -                *        injectionTarget.preDestroy(this);
> -                *    }
> -                *   if (context != null) {
> -                *        context.release();
> -                *    }
> -                *
> -                *   injectionTarget = null;
> -                *   context = null;
> -                */
> -    }
> -
> -    @Override public Object[] resolve(Method method) {
> -        return new Object[method.getParameterTypes().length];
> -    }
> -
> -    public BeanManager getBeanManager() {
> -        return ThreadSingletonServiceImpl.get().getBeanManagerImpl();
> -    }
> +
> +       private static final String ANNOTATION_NAME = "javax.ejb.EJB";
> +
> +       public void enrich(Object testCase) {
> +               BeanManager mgr = getBeanManager();
> +               if (mgr != null) {
> +                       AnnotatedType<?> type =
> mgr.createAnnotatedType(getClass());
> +                       InjectionTarget<Object> injectionTarget =
> (InjectionTarget<Object>) mgr.createInjectionTarget(type);
> +                       CreationalContext<Object> context =
> mgr.createCreationalContext(null);
> +
> +                       injectionTarget.inject(testCase, context);
> +                       injectionTarget.postConstruct(this);
> +               }
> +
> +               try {
> +                       if
> (SecurityActions.isClassPresent(ANNOTATION_NAME))
> {
> +                               @SuppressWarnings("unchecked")
> +                               Class<? extends Annotation> ejbAnnotation =
> (Class<? extends Annotation>)
> SecurityActions.getThreadContextClassLoader().loadClass(ANNOTATION_NAME);
> +
> +                               List<Field> annotatedFields =
> SecurityActions.getFieldsWithAnnotation(testCase.getClass(),
> ejbAnnotation);
> +
> +                               for (Field field : annotatedFields) {
> +                                       if (field.get(testCase) == null) {
> +                                               EJB fieldAnnotation = (EJB)
> field.getAnnotation(ejbAnnotation);
> +                                               Object ejb =
> lookupEJB(field.getType(), fieldAnnotation.mappedName());
> +                                               field.set(testCase, ejb);
> +                                       }
> +                               }
> +
> +                               List<Method> methods =
> SecurityActions.getMethodsWithAnnotation(testCase.getClass(),
> ejbAnnotation);
> +
> +                               for (Method method : methods) {
> +                                       if
> (method.getParameterTypes().length != 1) {
> +                                               throw new
> RuntimeException("@EJB only allowed on single argument methods");
> +                                       }
> +                                       if
> (!method.getName().startsWith("set")) {
> +                                               throw new
> RuntimeException("@EJB only allowed on 'set' methods");
> +                                       }
> +                                       EJB parameterAnnotation = null; //
> method.getParameterAnnotations()[0]
> +                                       for (Annotation annotation :
> method.getParameterAnnotations()[0]) {
> +                                               if
> (EJB.class.isAssignableFrom(annotation.annotationType())) {
> +                                                       parameterAnnotation
> = (EJB) annotation;
> +                                               }
> +                                       }
> +                                       String mappedName =
> parameterAnnotation == null ? null : parameterAnnotation.mappedName();
> +                                       Object ejb =
> lookupEJB(method.getParameterTypes()[0], mappedName);
> +                                       method.invoke(testCase, ejb);
> +                               }
> +
> +                       }
> +               } catch (Exception e) {
> +
> +               }
> +
> +       }
> +
> +       protected Object lookupEJB(Class<?> fieldType, String mappedName)
> throws Exception {
> +               // TODO: figure out test context ?
> +               Context initcontext = new InitialContext();
> +
> +               // TODO: These names are not spec compliant; fieldType
> needs
> to be a
> +               // bean type here,
> +               // but usually is just an interface of a bean. These seldom
> work.
> +               String[] jndiNames = {
> "openejb:global/global/test/test.jar/" + fieldType.getSimpleName() +
> "Bean",
> +                               "openejb:global/global/test/test.jar/" +
> fieldType.getSimpleName(),
> +                               "java:global/test/test.jar/" +
> fieldType.getSimpleName() + "Bean",
> +                               "java:global/test/test.jar/" +
> fieldType.getSimpleName(),
> +                               "java:global/test.ear/test/" +
> fieldType.getSimpleName() + "Bean",
> +                               "java:global/test.ear/test/" +
> fieldType.getSimpleName(),
> +                               "java:global/test/" +
> fieldType.getSimpleName(),
> +                               "java:global/test/" +
> fieldType.getSimpleName() + "Bean",
> +                               "java:global/test/" +
> fieldType.getSimpleName() + "/no-interface",
> +                               "test/" + fieldType.getSimpleName() +
> "Bean/local",
> +                               "test/" + fieldType.getSimpleName() +
> "Bean/remote",
> +                               "test/" + fieldType.getSimpleName() +
> "/no-interface",
> +                               fieldType.getSimpleName() + "Bean/local",
> +                               fieldType.getSimpleName() + "Bean/remote",
> +                               fieldType.getSimpleName() +
> "/no-interface",
> +                               "ejblocal:" + fieldType.getCanonicalName(),
> +                               fieldType.getCanonicalName() };
> +               if ((mappedName != null) && (!mappedName.equals(""))) {
> +                       // Use only the mapped name to lookup this EJB
> +                       jndiNames = new String[] { mappedName };
> +               }
> +
> +               for (String jndiName : jndiNames) {
> +                       try {
> +                               return initcontext.lookup(jndiName);
> +                       } catch (NamingException e) {
> +                               // no-op, try next
> +                       }
> +               }
> +               throw new NamingException("No EJB found in JNDI");
> +       }
> +
> +       @Override
> +       public Object[] resolve(Method method) {
> +               return new Object[method.getParameterTypes().length];
> +       }
> +
> +       public BeanManager getBeanManager() {
> +               return
> ThreadSingletonServiceImpl.get().getBeanManagerImpl();
> +       }
>  }
>
> Modified:
>
> openejb/trunk/arquillian-tomee/arquillian-tomee-remote/src/main/java/org/apache/openejb/arquillian/remote/RemoteTomEEExtension.java
> URL:
>
> http://svn.apache.org/viewvc/openejb/trunk/arquillian-tomee/arquillian-tomee-remote/src/main/java/org/apache/openejb/arquillian/remote/RemoteTomEEExtension.java?rev=1189526&r1=1189525&r2=1189526&view=diff
>
> ==============================================================================
> ---
>
> openejb/trunk/arquillian-tomee/arquillian-tomee-remote/src/main/java/org/apache/openejb/arquillian/remote/RemoteTomEEExtension.java
> (original)
> +++
>
> openejb/trunk/arquillian-tomee/arquillian-tomee-remote/src/main/java/org/apache/openejb/arquillian/remote/RemoteTomEEExtension.java
> Wed Oct 26 23:08:05 2011
> @@ -1,12 +1,12 @@
>  package org.apache.openejb.arquillian.remote;
>
>  import
> org.jboss.arquillian.container.spi.client.container.DeployableContainer;
> +import
>
> org.jboss.arquillian.container.test.spi.client.deployment.AuxiliaryArchiveAppender;
>  import org.jboss.arquillian.core.spi.LoadableExtension;
> -import org.jboss.arquillian.test.spi.TestEnricher;
>
>  public class RemoteTomEEExtension implements LoadableExtension {
>    @Override public void register(ExtensionBuilder builder) {
>        builder.service(DeployableContainer.class,
> RemoteTomEEContainer.class)
> -            .service(TestEnricher.class, RemoteTomEEEnricher.class);
> +            .service(AuxiliaryArchiveAppender.class,
> RemoteTomEEEJBEnricherArchiveAppender.class);
>    }
>  }
>
> Added:
>
> openejb/trunk/arquillian-tomee/arquillian-tomee-remote/src/main/java/org/apache/openejb/arquillian/remote/SecurityActions.java
> URL:
>
> http://svn.apache.org/viewvc/openejb/trunk/arquillian-tomee/arquillian-tomee-remote/src/main/java/org/apache/openejb/arquillian/remote/SecurityActions.java?rev=1189526&view=auto
>
> ==============================================================================
> ---
>
> openejb/trunk/arquillian-tomee/arquillian-tomee-remote/src/main/java/org/apache/openejb/arquillian/remote/SecurityActions.java
> (added)
> +++
>
> openejb/trunk/arquillian-tomee/arquillian-tomee-remote/src/main/java/org/apache/openejb/arquillian/remote/SecurityActions.java
> Wed Oct 26 23:08:05 2011
> @@ -0,0 +1,249 @@
> +/*
> + * JBoss, Home of Professional Open Source
> + * Copyright 2009, Red Hat Middleware LLC, and individual contributors
> + * by the @authors tag. See the copyright.txt in the distribution for a
> + * full listing of individual contributors.
> + *
> + * Licensed 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.openejb.arquillian.remote;
> +
> +import java.lang.annotation.Annotation;
> +import java.lang.reflect.Constructor;
> +import java.lang.reflect.Field;
> +import java.lang.reflect.Method;
> +import java.security.AccessController;
> +import java.security.PrivilegedAction;
> +import java.security.PrivilegedActionException;
> +import java.security.PrivilegedExceptionAction;
> +import java.util.ArrayList;
> +import java.util.List;
> +
> +/**
> + * SecurityActions
> + *
> + * A set of privileged actions that are not to leak out
> + * of this package
> + *
> + * @author <a href="mailto:[email protected]";>ALR</a>
> + * @version $Revision: $
> + */
> +final class SecurityActions
> +{
> +
> +
>
> //-------------------------------------------------------------------------------||
> +   // Constructor
> ------------------------------------------------------------------||
> +
>
> //-------------------------------------------------------------------------------||
> +
> +   /**
> +    * No instantiation
> +    */
> +   private SecurityActions()
> +   {
> +      throw new UnsupportedOperationException("No instantiation");
> +   }
> +
> +
>
> //-------------------------------------------------------------------------------||
> +   // Utility Methods
> --------------------------------------------------------------||
> +
>
> //-------------------------------------------------------------------------------||
> +
> +   /**
> +    * Obtains the Thread Context ClassLoader
> +    */
> +   static ClassLoader getThreadContextClassLoader()
> +   {
> +      return AccessController.doPrivileged(GetTcclAction.INSTANCE);
> +   }
> +
> +   /**
> +    * Obtains the Constructor specified from the given Class and argument
> types
> +    * @param clazz
> +    * @param argumentTypes
> +    * @return
> +    * @throws NoSuchMethodException
> +    */
> +   static Constructor<?> getConstructor(final Class<?> clazz, final
> Class<?>... argumentTypes)
> +         throws NoSuchMethodException
> +   {
> +      try
> +      {
> +         return AccessController.doPrivileged(new
> PrivilegedExceptionAction<Constructor<?>>()
> +         {
> +            public Constructor<?> run() throws NoSuchMethodException
> +            {
> +               return clazz.getConstructor(argumentTypes);
> +            }
> +         });
> +      }
> +      // Unwrap
> +      catch (final PrivilegedActionException pae)
> +      {
> +         final Throwable t = pae.getCause();
> +         // Rethrow
> +         if (t instanceof NoSuchMethodException)
> +         {
> +            throw (NoSuchMethodException) t;
> +         }
> +         else
> +         {
> +            // No other checked Exception thrown by Class.getConstructor
> +            try
> +            {
> +               throw (RuntimeException) t;
> +            }
> +            // Just in case we've really messed up
> +            catch (final ClassCastException cce)
> +            {
> +               throw new RuntimeException("Obtained unchecked Exception;
> this code should never be reached", t);
> +            }
> +         }
> +      }
> +   }
> +
> +   /**
> +    * Create a new instance by finding a constructor that matches the
> argumentTypes signature
> +    * using the arguments for instantiation.
> +    *
> +    * @param className Full classname of class to create
> +    * @param argumentTypes The constructor argument types
> +    * @param arguments The constructor arguments
> +    * @return a new instance
> +    * @throws IllegalArgumentException if className, argumentTypes, or
> arguments are null
> +    * @throws RuntimeException if any exceptions during creation
> +    * @author <a href="mailto:[email protected]";>Aslak Knutsen</a>
> +    * @author <a href="mailto:[email protected]";>ALR</a>
> +    */
> +   static <T> T newInstance(final String className, final Class<?>[]
> argumentTypes, final Object[] arguments,
> +         final Class<T> expectedType)
> +   {
> +      if (className == null)
> +      {
> +         throw new IllegalArgumentException("ClassName must be
> specified");
> +      }
> +      if (argumentTypes == null)
> +      {
> +         throw new IllegalArgumentException("ArgumentTypes must be
> specified. Use empty array if no arguments");
> +      }
> +      if (arguments == null)
> +      {
> +         throw new IllegalArgumentException("Arguments must be specified.
> Use empty array if no arguments");
> +      }
> +      final Object obj;
> +      try
> +      {
> +         final ClassLoader tccl = getThreadContextClassLoader();
> +         final Class<?> implClass = Class.forName(className, false, tccl);
> +         Constructor<?> constructor = getConstructor(implClass,
> argumentTypes);
> +         obj = constructor.newInstance(arguments);
> +      }
> +      catch (Exception e)
> +      {
> +         throw new RuntimeException("Could not create new instance of " +
> className
> +               + ", missing package from classpath?", e);
> +      }
> +
> +      // Cast
> +      try
> +      {
> +         return expectedType.cast(obj);
> +      }
> +      catch (final ClassCastException cce)
> +      {
> +         // Reconstruct so we get some useful information
> +         throw new ClassCastException("Incorrect expected type, " +
> expectedType.getName() + ", defined for "
> +               + obj.getClass().getName());
> +      }
> +   }
> +
> +   public static boolean isClassPresent(String name)
> +   {
> +      try
> +      {
> +         ClassLoader classLoader = getThreadContextClassLoader();
> +         classLoader.loadClass(name);
> +         return true;
> +      }
> +      catch (ClassNotFoundException e)
> +      {
> +         return false;
> +      }
> +   }
> +
> +   public static List<Field> getFieldsWithAnnotation(final Class<?>
> source,
> final Class<? extends Annotation> annotationClass)
> +   {
> +      List<Field> declaredAccessableFields =
> AccessController.doPrivileged(new PrivilegedAction<List<Field>>()
> +      {
> +         public List<Field> run()
> +         {
> +            List<Field> foundFields = new ArrayList<Field>();
> +            Class<?> nextSource = source;
> +            while (nextSource != Object.class) {
> +               for(Field field : nextSource.getDeclaredFields())
> +               {
> +                  if(field.isAnnotationPresent(annotationClass))
> +                  {
> +                     if(!field.isAccessible())
> +                     {
> +                        field.setAccessible(true);
> +                     }
> +                     foundFields.add(field);
> +                  }
> +               }
> +               nextSource = nextSource.getSuperclass();
> +            }
> +            return foundFields;
> +         }
> +      });
> +      return declaredAccessableFields;
> +   }
> +
> +   public static List<Method> getMethodsWithAnnotation(final Class<?>
> source, final Class<? extends Annotation> annotationClass)
> +   {
> +      List<Method> declaredAccessableMethods =
> AccessController.doPrivileged(new PrivilegedAction<List<Method>>()
> +      {
> +         public List<Method> run()
> +         {
> +            List<Method> foundMethods = new ArrayList<Method>();
> +            for(Method method : source.getDeclaredMethods())
> +            {
> +               if(method.isAnnotationPresent(annotationClass))
> +               {
> +                  if(!method.isAccessible())
> +                  {
> +                     method.setAccessible(true);
> +                  }
> +                  foundMethods.add(method);
> +               }
> +            }
> +            return foundMethods;
> +         }
> +      });
> +      return declaredAccessableMethods;
> +   }
> +
> +
>
> //-------------------------------------------------------------------------------||
> +   // Inner Classes
> ----------------------------------------------------------------||
> +
>
> //-------------------------------------------------------------------------------||
> +
> +   /**
> +    * Single instance to get the TCCL
> +    */
> +   private enum GetTcclAction implements PrivilegedAction<ClassLoader> {
> +      INSTANCE;
> +
> +      public ClassLoader run()
> +      {
> +         return Thread.currentThread().getContextClassLoader();
> +      }
> +
> +   }
> +
> +}
>
> Modified:
>
> openejb/trunk/arquillian-tomee/arquillian-tomee-remote/src/test/java/org/apache/openejb/arquillian/TomEEContainerTest.java
> URL:
>
> http://svn.apache.org/viewvc/openejb/trunk/arquillian-tomee/arquillian-tomee-remote/src/test/java/org/apache/openejb/arquillian/TomEEContainerTest.java?rev=1189526&r1=1189525&r2=1189526&view=diff
>
> ==============================================================================
> ---
>
> openejb/trunk/arquillian-tomee/arquillian-tomee-remote/src/test/java/org/apache/openejb/arquillian/TomEEContainerTest.java
> (original)
> +++
>
> openejb/trunk/arquillian-tomee/arquillian-tomee-remote/src/test/java/org/apache/openejb/arquillian/TomEEContainerTest.java
> Wed Oct 26 23:08:05 2011
> @@ -20,6 +20,8 @@ import java.io.ByteArrayOutputStream;
>  import java.io.InputStream;
>  import java.net.URL;
>
> +import javax.ejb.EJB;
> +
>  import junit.framework.Assert;
>
>  import org.jboss.arquillian.container.test.api.Deployment;
> @@ -35,15 +37,21 @@ import org.junit.runner.RunWith;
>  @RunWith(Arquillian.class)
>  public class TomEEContainerTest {
>
> -    @Deployment(testable = false)
> +    @Deployment
>    public static WebArchive createDeployment() {
> -        return ShrinkWrap.create(WebArchive.class,
> "test.war").addClass(TestServlet.class).addClass(TestEjb.class)
> +        return ShrinkWrap.create(WebArchive.class,
>
> "test.war").addClass(TestServlet.class).addClass(TestEjb.class).addClass(TomEEContainerTest.class)
>                .setWebXML(new
>
> StringAsset(Descriptors.create(WebAppDescriptor.class).version("3.0").servlet(TestServlet.class,
> "/Test").exportAsString()));
>    }
>
> -    @Test
> -    public void nothing(){}
> +    @EJB
> +    private TestEjb ejb;
> +
> +    @Test
> +    public void testEjbIsNotNull() throws Exception {
> +       Assert.assertNotNull(ejb);
> +    }
>
> +    @Test
>    public void testShouldBeAbleToAccessServletAndEjb() throws Exception {
>        InputStream is = new URL("http://localhost:9080/test/Test
> ").openStream();
>        ByteArrayOutputStream os = new ByteArrayOutputStream();
>
> Modified: openejb/trunk/arquillian-tomee/pom.xml
> URL:
>
> http://svn.apache.org/viewvc/openejb/trunk/arquillian-tomee/pom.xml?rev=1189526&r1=1189525&r2=1189526&view=diff
>
> ==============================================================================
> --- openejb/trunk/arquillian-tomee/pom.xml (original)
> +++ openejb/trunk/arquillian-tomee/pom.xml Wed Oct 26 23:08:05 2011
> @@ -49,7 +49,7 @@
>
>  <properties>
>    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
> -    <version.arquillian>1.0.0.CR1</version.arquillian>
> +    <version.arquillian>1.0.0.CR4</version.arquillian>
>    <version.cdi-api>1.0</version.cdi-api>
>    <version.openejb>4.0.0-beta-2-SNAPSHOT</version.openejb>
>    <version.org.apache.tomcat>7.0.21</version.org.apache.tomcat>
>

Reply via email to