Author: rmannibucau
Date: Mon Nov  5 07:05:26 2012
New Revision: 1405702

URL: http://svn.apache.org/viewvc?rev=1405702&view=rev
Log:
OPENEJB-1929 some enhancements for rest

Added:
    
openejb/trunk/openejb/server/openejb-cxf-rs/src/test/java/org/apache/openejb/server/cxf/rs/SimpleApplicationTest.java
    
openejb/trunk/openejb/server/openejb-cxf-rs/src/test/java/org/apache/openejb/server/cxf/rs/SimplePojoTest.java
    
openejb/trunk/openejb/server/openejb-lightweight-web/src/main/java/org/apache/openejb/server/web/WebInitialContext.java
Modified:
    
openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/config/DeploymentLoader.java
    
openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/junit/ApplicationComposer.java
    openejb/trunk/openejb/server/openejb-cxf-rs/pom.xml
    
openejb/trunk/openejb/server/openejb-lightweight-web/src/main/java/org/apache/openejb/server/web/LightweightWebAppBuilder.java

Modified: 
openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/config/DeploymentLoader.java
URL: 
http://svn.apache.org/viewvc/openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/config/DeploymentLoader.java?rev=1405702&r1=1405701&r2=1405702&view=diff
==============================================================================
--- 
openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/config/DeploymentLoader.java
 (original)
+++ 
openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/config/DeploymentLoader.java
 Mon Nov  5 07:05:26 2012
@@ -628,7 +628,7 @@ public class DeploymentLoader implements
         addWebModule(webModule, appModule);
     }
 
-    public void addWebModule(final WebModule webModule, final AppModule 
appModule) throws OpenEJBException {
+    public static void addWebModule(final WebModule webModule, final AppModule 
appModule) throws OpenEJBException {
         // create and add the WebModule
         appModule.getWebModules().add(webModule);
         if (appModule.isStandaloneModule()) {
@@ -670,14 +670,18 @@ public class DeploymentLoader implements
             // TODO:  Put our scanning ehnancements back, here
             fillEjbJar(webModule, webEjbModule);
 
-            if (isMetadataComplete(webModule, webEjbModule)) {
-                final IAnnotationFinder finder = new 
org.apache.xbean.finder.AnnotationFinder(new ClassesArchive());
-                webModule.setFinder(finder);
-                webEjbModule.setFinder(finder);
-            }  else {
-                final IAnnotationFinder finder = 
FinderFactory.createFinder(webModule);
-                webModule.setFinder(finder);
-                webEjbModule.setFinder(finder);
+            if (webModule.getFinder() == null) {
+                if (isMetadataComplete(webModule, webEjbModule)) {
+                    final IAnnotationFinder finder = new 
org.apache.xbean.finder.AnnotationFinder(new ClassesArchive());
+                    webModule.setFinder(finder);
+                    webEjbModule.setFinder(finder);
+                }  else {
+                    final IAnnotationFinder finder = 
FinderFactory.createFinder(webModule);
+                    webModule.setFinder(finder);
+                    webEjbModule.setFinder(finder);
+                }
+            } else if (webEjbModule.getFinder() == null) {
+                webEjbModule.setFinder(webModule.getFinder());
             }
         } catch (Exception e) {
             throw new OpenEJBException("Unable to create annotation scanner 
for web module " + webModule.getModuleId(), e);
@@ -694,7 +698,7 @@ public class DeploymentLoader implements
      * @param webModule
      * @param ejbModule
      */
-    private void fillEjbJar(WebModule webModule, EjbModule ejbModule) {
+    private static void fillEjbJar(WebModule webModule, EjbModule ejbModule) {
         final Object o = webModule.getAltDDs().get("ejb-jar.xml");
         if (o != null) return;
         if (ejbModule.getEjbJar() != null) return;
@@ -707,7 +711,7 @@ public class DeploymentLoader implements
         ejbModule.setEjbJar(ejbJar);
     }
 
-    private boolean isMetadataComplete(WebModule webModule, EjbModule 
ejbModule) {
+    private static boolean isMetadataComplete(WebModule webModule, EjbModule 
ejbModule) {
         if (webModule.getWebApp() == null) return false;
         if (!webModule.getWebApp().isMetadataComplete()) return false;
 
@@ -921,7 +925,7 @@ public class DeploymentLoader implements
         return webClassPath.toArray(new URL[webClassPath.size()]);
     }
 
-    private void addWebservices(final WsModule wsModule) throws 
OpenEJBException {
+    private static void addWebservices(final WsModule wsModule) throws 
OpenEJBException {
         final boolean webservicesEnabled = 
SystemInstance.get().getOptions().get(ConfigurationFactory.WEBSERVICES_ENABLED, 
true);
         if (!webservicesEnabled) {
             wsModule.getAltDDs().remove("webservices.xml");

Modified: 
openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/junit/ApplicationComposer.java
URL: 
http://svn.apache.org/viewvc/openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/junit/ApplicationComposer.java?rev=1405702&r1=1405701&r2=1405702&view=diff
==============================================================================
--- 
openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/junit/ApplicationComposer.java
 (original)
+++ 
openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/junit/ApplicationComposer.java
 Mon Nov  5 07:05:26 2012
@@ -28,6 +28,7 @@ import org.apache.openejb.cdi.ScopeHelpe
 import org.apache.openejb.config.AppModule;
 import org.apache.openejb.config.ConfigurationFactory;
 import org.apache.openejb.config.ConnectorModule;
+import org.apache.openejb.config.DeploymentLoader;
 import org.apache.openejb.config.EjbModule;
 import org.apache.openejb.config.PersistenceModule;
 import org.apache.openejb.config.WebModule;
@@ -281,8 +282,8 @@ public class ApplicationComposer extends
 
                 final Object obj = method.invokeExplosively(testInstance);
 
-                if (obj instanceof WebModule) {
-                    appModule.getWebModules().add((WebModule) obj);
+                if (obj instanceof WebModule) { // will add the ejbmodule too
+                    DeploymentLoader.addWebModule((WebModule) obj, appModule);
                 } else if (obj instanceof EjbModule) {
                     final EjbModule ejbModule = (EjbModule) obj;
                     ejbModule.initAppModule(appModule);

Modified: openejb/trunk/openejb/server/openejb-cxf-rs/pom.xml
URL: 
http://svn.apache.org/viewvc/openejb/trunk/openejb/server/openejb-cxf-rs/pom.xml?rev=1405702&r1=1405701&r2=1405702&view=diff
==============================================================================
--- openejb/trunk/openejb/server/openejb-cxf-rs/pom.xml (original)
+++ openejb/trunk/openejb/server/openejb-cxf-rs/pom.xml Mon Nov  5 07:05:26 2012
@@ -118,6 +118,12 @@
       <artifactId>jettison</artifactId>
       <version>1.3</version>
     </dependency>
+    <dependency>
+      <groupId>org.apache.openejb</groupId>
+      <artifactId>openejb-lightweight-web</artifactId>
+      <version>${project.version}</version>
+      <scope>test</scope>
+    </dependency>
   </dependencies>
 
 </project>

Added: 
openejb/trunk/openejb/server/openejb-cxf-rs/src/test/java/org/apache/openejb/server/cxf/rs/SimpleApplicationTest.java
URL: 
http://svn.apache.org/viewvc/openejb/trunk/openejb/server/openejb-cxf-rs/src/test/java/org/apache/openejb/server/cxf/rs/SimpleApplicationTest.java?rev=1405702&view=auto
==============================================================================
--- 
openejb/trunk/openejb/server/openejb-cxf-rs/src/test/java/org/apache/openejb/server/cxf/rs/SimpleApplicationTest.java
 (added)
+++ 
openejb/trunk/openejb/server/openejb-cxf-rs/src/test/java/org/apache/openejb/server/cxf/rs/SimpleApplicationTest.java
 Mon Nov  5 07:05:26 2012
@@ -0,0 +1,109 @@
+/*
+ *     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.openejb.server.cxf.rs;
+
+import org.apache.cxf.jaxrs.client.WebClient;
+import org.apache.openejb.OpenEjbContainer;
+import org.apache.openejb.assembler.classic.WebAppBuilder;
+import org.apache.openejb.config.WebModule;
+import org.apache.openejb.jee.WebApp;
+import org.apache.openejb.junit.ApplicationComposer;
+import org.apache.openejb.junit.Component;
+import org.apache.openejb.junit.Configuration;
+import org.apache.openejb.junit.Module;
+import org.apache.openejb.server.cxf.rs.beans.MyRESTApplication;
+import org.apache.openejb.server.cxf.rs.beans.RestWithInjections;
+import org.apache.openejb.server.cxf.rs.beans.SimpleEJB;
+import org.apache.openejb.server.web.LightweightWebAppBuilder;
+import org.apache.xbean.finder.AnnotationFinder;
+import org.apache.xbean.finder.archive.ClassesArchive;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+
+import javax.servlet.http.HttpServletResponse;
+import javax.ws.rs.core.Response;
+import java.io.InputStream;
+import java.io.StringWriter;
+import java.util.Properties;
+
+import static org.junit.Assert.assertEquals;
+
+@RunWith(ApplicationComposer.class)
+public class SimpleApplicationTest {
+    public static final String BASE_URL = "http://localhost:4204/foo/my-app";;
+
+    @Component
+    public WebAppBuilder webAppBuilder() {
+        return new LightweightWebAppBuilder();
+    }
+
+    @Configuration
+    public Properties configuration() {
+        final Properties properties = new Properties();
+        properties.setProperty(OpenEjbContainer.OPENEJB_EMBEDDED_REMOTABLE, 
"true");
+        return properties;
+    }
+
+    @Module
+    public WebModule war() {
+        final WebModule webModule = new WebModule(new WebApp(), "/foo", 
Thread.currentThread().getContextClassLoader(), "", "foo");
+        webModule.getRestApplications().add(MyRESTApplication.class.getName());
+        webModule.setFinder(new AnnotationFinder(new 
ClassesArchive(RestWithInjections.class, SimpleEJB.class)));
+        return webModule;
+    }
+
+    @Test
+    public void first() {
+        String hi = 
WebClient.create(BASE_URL).path("/first/hi").get(String.class);
+        assertEquals("Hi from REST World!", hi);
+    }
+
+    @Test
+    public void second() {
+        String hi = 
WebClient.create(BASE_URL).path("/second/hi2/2nd").get(String.class);
+        assertEquals("hi 2nd", hi);
+    }
+
+    @Test
+    public void expert() throws Exception {
+        Response response = 
WebClient.create(BASE_URL).path("/expert/still-hi").post("Pink Floyd");
+        assertEquals(HttpServletResponse.SC_OK, response.getStatus());
+
+        InputStream is = (InputStream) response.getEntity();
+        StringWriter writer = new StringWriter();
+        int c;
+        while ((c = is.read()) != -1) {
+            writer.write(c);
+        }
+        assertEquals("hi Pink Floyd", writer.toString());
+    }
+
+    @Test
+    public void nonListed() { // default handler from openejb-http
+        assertEquals("", 
WebClient.create(BASE_URL).path("/non-listed/yata/foo").get(String.class));
+    }
+
+    @Test
+    public void hooked() {
+        assertEquals(true, 
WebClient.create(BASE_URL).path("/hooked/post").get(Boolean.class));
+    }
+
+    @Test
+    public void injectEjb() {
+        assertEquals(true, 
WebClient.create(BASE_URL).path("/inject/ejb").get(Boolean.class));
+    }
+}

Added: 
openejb/trunk/openejb/server/openejb-cxf-rs/src/test/java/org/apache/openejb/server/cxf/rs/SimplePojoTest.java
URL: 
http://svn.apache.org/viewvc/openejb/trunk/openejb/server/openejb-cxf-rs/src/test/java/org/apache/openejb/server/cxf/rs/SimplePojoTest.java?rev=1405702&view=auto
==============================================================================
--- 
openejb/trunk/openejb/server/openejb-cxf-rs/src/test/java/org/apache/openejb/server/cxf/rs/SimplePojoTest.java
 (added)
+++ 
openejb/trunk/openejb/server/openejb-cxf-rs/src/test/java/org/apache/openejb/server/cxf/rs/SimplePojoTest.java
 Mon Nov  5 07:05:26 2012
@@ -0,0 +1,65 @@
+/*
+ *     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.openejb.server.cxf.rs;
+
+import org.apache.cxf.jaxrs.client.WebClient;
+import org.apache.openejb.OpenEjbContainer;
+import org.apache.openejb.assembler.classic.WebAppBuilder;
+import org.apache.openejb.config.WebModule;
+import org.apache.openejb.core.WebContext;
+import org.apache.openejb.jee.WebApp;
+import org.apache.openejb.junit.ApplicationComposer;
+import org.apache.openejb.junit.Component;
+import org.apache.openejb.junit.Configuration;
+import org.apache.openejb.junit.Module;
+import org.apache.openejb.loader.SystemInstance;
+import org.apache.openejb.server.cxf.rs.beans.MyFirstRestClass;
+import org.apache.openejb.server.web.LightweightWebAppBuilder;
+import org.apache.openejb.spi.ContainerSystem;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+
+import java.util.Properties;
+
+import static org.junit.Assert.assertEquals;
+
+@RunWith(ApplicationComposer.class)
+public class SimplePojoTest {
+    @Component
+    public WebAppBuilder webAppBuilder() {
+        return new LightweightWebAppBuilder();
+    }
+
+    @Configuration
+    public Properties configuration() {
+        final Properties properties = new Properties();
+        properties.setProperty(OpenEjbContainer.OPENEJB_EMBEDDED_REMOTABLE, 
"true");
+        return properties;
+    }
+
+    @Module
+    public WebModule war() {
+        final WebModule webModule = new WebModule(new WebApp(), "/foo", 
Thread.currentThread().getContextClassLoader(), "", "foo");
+        webModule.getRestClasses().add(MyFirstRestClass.class.getName());
+        return webModule;
+    }
+
+    @Test
+    public void checkServiceWasDeployed() {
+        assertEquals("Hi from REST World!", 
WebClient.create("http://localhost:4204/foo";).path("/first/hi").get(String.class));
+    }
+}

Modified: 
openejb/trunk/openejb/server/openejb-lightweight-web/src/main/java/org/apache/openejb/server/web/LightweightWebAppBuilder.java
URL: 
http://svn.apache.org/viewvc/openejb/trunk/openejb/server/openejb-lightweight-web/src/main/java/org/apache/openejb/server/web/LightweightWebAppBuilder.java?rev=1405702&r1=1405701&r2=1405702&view=diff
==============================================================================
--- 
openejb/trunk/openejb/server/openejb-lightweight-web/src/main/java/org/apache/openejb/server/web/LightweightWebAppBuilder.java
 (original)
+++ 
openejb/trunk/openejb/server/openejb-lightweight-web/src/main/java/org/apache/openejb/server/web/LightweightWebAppBuilder.java
 Mon Nov  5 07:05:26 2012
@@ -20,6 +20,7 @@ import org.apache.openejb.AppContext;
 import org.apache.openejb.Injection;
 import org.apache.openejb.OpenEJBRuntimeException;
 import org.apache.openejb.assembler.classic.AppInfo;
+import org.apache.openejb.assembler.classic.InjectionBuilder;
 import org.apache.openejb.assembler.classic.JndiEncBuilder;
 import org.apache.openejb.assembler.classic.WebAppBuilder;
 import org.apache.openejb.assembler.classic.WebAppInfo;
@@ -28,7 +29,6 @@ import org.apache.openejb.core.WebContex
 import org.apache.openejb.loader.SystemInstance;
 import org.apache.openejb.spi.ContainerSystem;
 
-import javax.naming.InitialContext;
 import java.util.Collection;
 import java.util.Collections;
 import java.util.HashMap;
@@ -46,18 +46,20 @@ public class LightweightWebAppBuilder im
 
         for (WebAppInfo webAppInfo : appInfo.webApps) {
             final Collection<Injection> injections = 
appContext.getInjections();
+            injections.addAll(new 
InjectionBuilder(classLoader).buildInjections(webAppInfo.jndiEnc));
 
             final Map<String, Object> bindings = new HashMap<String, Object>();
             bindings.putAll(appContext.getBindings());
             bindings.putAll(new JndiEncBuilder(webAppInfo.jndiEnc, injections, 
webAppInfo.moduleId, "Bean", null, webAppInfo.uniqueId, 
classLoader).buildBindings(JndiEncBuilder.JndiScope.comp));
 
             final WebContext webContext = new WebContext(appContext);
-            webContext.setJndiEnc(new InitialContext());
+            webContext.setBindings(bindings);
+            webContext.setJndiEnc(WebInitialContext.create(bindings, 
appContext.getGlobalJndiContext()));
             webContext.setClassLoader(classLoader);
             webContext.setId(webAppInfo.moduleId);
             webContext.setContextRoot(webAppInfo.contextRoot);
-            webContext.setBindings(bindings);
             webContext.getInjections().addAll(injections);
+
             appContext.getWebContexts().add(webContext);
             cs.addWebContext(webContext);
         }

Added: 
openejb/trunk/openejb/server/openejb-lightweight-web/src/main/java/org/apache/openejb/server/web/WebInitialContext.java
URL: 
http://svn.apache.org/viewvc/openejb/trunk/openejb/server/openejb-lightweight-web/src/main/java/org/apache/openejb/server/web/WebInitialContext.java?rev=1405702&view=auto
==============================================================================
--- 
openejb/trunk/openejb/server/openejb-lightweight-web/src/main/java/org/apache/openejb/server/web/WebInitialContext.java
 (added)
+++ 
openejb/trunk/openejb/server/openejb-lightweight-web/src/main/java/org/apache/openejb/server/web/WebInitialContext.java
 Mon Nov  5 07:05:26 2012
@@ -0,0 +1,64 @@
+/*
+ *     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.openejb.server.web;
+
+import org.apache.openejb.core.ivm.naming.Reference;
+
+import javax.naming.Context;
+import javax.naming.LinkRef;
+import java.lang.reflect.InvocationHandler;
+import java.lang.reflect.Method;
+import java.lang.reflect.Proxy;
+import java.util.Map;
+
+public class WebInitialContext implements InvocationHandler {
+    private static final Class<?>[] INTERFACES = new Class<?>[]{ Context.class 
};
+
+    private final Map<String, Object> bindings;
+    private final Context delegate;
+
+    public WebInitialContext(final Map<String, Object> bindings, final Context 
ctx) {
+        this.bindings = bindings;
+        delegate = ctx;
+    }
+
+    @Override
+    public Object invoke(Object proxy, Method method, Object[] args) throws 
Throwable {
+        if ("lookup".equals(method.getName()) && 
method.getParameterTypes().length == 1 && 
String.class.equals(method.getParameterTypes()[0])) {
+            final Object lookedUp = bindings.get(normalize((String) args[0]));
+            if (lookedUp != null) {
+                if (lookedUp instanceof Reference) {
+                    return ((Reference) lookedUp).getObject();
+                } else if (lookedUp instanceof LinkRef) {
+                    return ((Context) proxy).lookup(((LinkRef) 
lookedUp).getLinkName());
+                }
+            }
+        }
+        return method.invoke(delegate, args);
+    }
+
+    private static String normalize(final String arg) {
+        if (arg.startsWith("java:")) {
+            return arg.substring("java:".length());
+        }
+        return arg;
+    }
+
+    public static Context create(final Map<String, Object> bindings, final 
Context fallback) {
+        return (Context) 
Proxy.newProxyInstance(Thread.currentThread().getContextClassLoader(), 
INTERFACES, new WebInitialContext(bindings, fallback));
+    }
+}


Reply via email to