You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tomee.apache.org by rm...@apache.org on 2014/11/14 17:24:01 UTC

tomee git commit: TOMEE-1444 basic cxf rs events

Repository: tomee
Updated Branches:
  refs/heads/develop 7235407eb -> abf8de7a7


TOMEE-1444 basic cxf rs events


Project: http://git-wip-us.apache.org/repos/asf/tomee/repo
Commit: http://git-wip-us.apache.org/repos/asf/tomee/commit/abf8de7a
Tree: http://git-wip-us.apache.org/repos/asf/tomee/tree/abf8de7a
Diff: http://git-wip-us.apache.org/repos/asf/tomee/diff/abf8de7a

Branch: refs/heads/develop
Commit: abf8de7a772b25857630f158d73942fb6c004bf3
Parents: 7235407
Author: Romain Manni-Bucau <rm...@apache.org>
Authored: Fri Nov 14 17:23:30 2014 +0100
Committer: Romain Manni-Bucau <rm...@apache.org>
Committed: Fri Nov 14 17:23:30 2014 +0100

----------------------------------------------------------------------
 .../server/cxf/rs/CxfRsHttpListener.java        | 30 +++++++
 .../rs/event/ExtensionProviderRegistration.java | 41 +++++++++
 .../server/cxf/rs/event/ServerCreated.java      | 55 ++++++++++++
 .../server/cxf/rs/event/ServerDestroyed.java    | 38 ++++++++
 .../ExtensionProviderRegistrationTest.java      | 95 ++++++++++++++++++++
 .../server/cxf/rs/event/ServerCreatedTest.java  | 84 +++++++++++++++++
 6 files changed, 343 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/tomee/blob/abf8de7a/server/openejb-cxf-rs/src/main/java/org/apache/openejb/server/cxf/rs/CxfRsHttpListener.java
----------------------------------------------------------------------
diff --git a/server/openejb-cxf-rs/src/main/java/org/apache/openejb/server/cxf/rs/CxfRsHttpListener.java b/server/openejb-cxf-rs/src/main/java/org/apache/openejb/server/cxf/rs/CxfRsHttpListener.java
index 529d76a..f561311 100644
--- a/server/openejb-cxf-rs/src/main/java/org/apache/openejb/server/cxf/rs/CxfRsHttpListener.java
+++ b/server/openejb-cxf-rs/src/main/java/org/apache/openejb/server/cxf/rs/CxfRsHttpListener.java
@@ -57,12 +57,16 @@ import org.apache.openejb.api.jmx.ManagedOperation;
 import org.apache.openejb.assembler.classic.ServiceInfo;
 import org.apache.openejb.assembler.classic.util.ServiceConfiguration;
 import org.apache.openejb.assembler.classic.util.ServiceInfos;
+import org.apache.openejb.core.WebContext;
 import org.apache.openejb.dyni.DynamicSubclass;
 import org.apache.openejb.loader.IO;
 import org.apache.openejb.loader.SystemInstance;
 import org.apache.openejb.monitoring.LocalMBeanServer;
 import org.apache.openejb.monitoring.ObjectNameBuilder;
 import org.apache.openejb.rest.ThreadLocalContextManager;
+import org.apache.openejb.server.cxf.rs.event.ExtensionProviderRegistration;
+import org.apache.openejb.server.cxf.rs.event.ServerCreated;
+import org.apache.openejb.server.cxf.rs.event.ServerDestroyed;
 import org.apache.openejb.server.cxf.transport.HttpDestination;
 import org.apache.openejb.server.cxf.transport.util.CxfUtil;
 import org.apache.openejb.server.httpd.HttpRequest;
@@ -349,6 +353,8 @@ public class CxfRsHttpListener implements RsHttpListener {
 
             server = factory.create();
             destination = (HttpDestination) server.getDestination();
+
+            fireServerCreated(oldLoader);
         } finally {
             if (oldLoader != null) {
                 CxfUtil.clearBusLoader(oldLoader);
@@ -356,6 +362,26 @@ public class CxfRsHttpListener implements RsHttpListener {
         }
     }
 
+    private void fireServerCreated(final ClassLoader oldLoader) {
+        final Object ctx = AppFinder.findAppContextOrWeb(oldLoader, new AppFinder.Transformer<Object>() {
+            @Override
+            public Object from(final AppContext appCtx) {
+                return appCtx;
+            }
+
+            @Override
+            public Object from(final WebContext webCtx) {
+                return webCtx;
+            }
+        });
+        final AppContext appCtx = AppContext.class.isInstance(ctx) ? AppContext.class.cast(ctx) : WebContext.class.cast(ctx).getAppContext();
+        WebContext webContext = appCtx == ctx ? null : WebContext.class.cast(ctx);
+        if (webContext == null && appCtx.getWebContexts().size() == 1 && appCtx.getWebContexts().get(0).getClassLoader() == oldLoader) {
+            webContext = appCtx.getWebContexts().get(0);
+        }
+        SystemInstance.get().fireEvent(new ServerCreated(server, appCtx, webContext));
+    }
+
     private List<Object> providers(final Collection<ServiceInfo> services, final Collection<Object> additionalProviders, final WebBeansContext ctx) {
         final List<Object> instances = new ArrayList<>();
         final BeanManagerImpl bm = ctx == null ? null : ctx.getBeanManagerImpl();
@@ -447,6 +473,7 @@ public class CxfRsHttpListener implements RsHttpListener {
         Thread.currentThread().setContextClassLoader(CxfUtil.initBusLoader());
         try {
             server.destroy();
+            SystemInstance.get().fireEvent(new ServerDestroyed(server));
         } catch (final RuntimeException ise) {
             LOGGER.warning("Can't stop correctly the endpoint " + server);
             if (LOGGER.isDebugEnabled()) {
@@ -520,6 +547,7 @@ public class CxfRsHttpListener implements RsHttpListener {
 
             try {
                 server = factory.create();
+                fireServerCreated(oldLoader);
             } finally {
                 try {
                     SERVER_IMPL_LOGGER.setLevel(level);
@@ -770,6 +798,8 @@ public class CxfRsHttpListener implements RsHttpListener {
             addMandatoryProviders(providers);
         }
 
+        SystemInstance.get().fireEvent(new ExtensionProviderRegistration(providers));
+
         LOGGER.info("Using providers:");
         for (final Object provider : providers) {
             LOGGER.info("     " + provider);

http://git-wip-us.apache.org/repos/asf/tomee/blob/abf8de7a/server/openejb-cxf-rs/src/main/java/org/apache/openejb/server/cxf/rs/event/ExtensionProviderRegistration.java
----------------------------------------------------------------------
diff --git a/server/openejb-cxf-rs/src/main/java/org/apache/openejb/server/cxf/rs/event/ExtensionProviderRegistration.java b/server/openejb-cxf-rs/src/main/java/org/apache/openejb/server/cxf/rs/event/ExtensionProviderRegistration.java
new file mode 100644
index 0000000..56aaef4
--- /dev/null
+++ b/server/openejb-cxf-rs/src/main/java/org/apache/openejb/server/cxf/rs/event/ExtensionProviderRegistration.java
@@ -0,0 +1,41 @@
+/*
+ *     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.event;
+
+import org.apache.openejb.observer.Event;
+
+import java.util.List;
+
+// using list reference to be able to get updates in CxfrsHttpListener automatically
+// this event can allow to add/remove/resort providers
+@Event
+public class ExtensionProviderRegistration {
+    private final List<Object> providers;
+
+    public ExtensionProviderRegistration(final List<Object> existings) {
+        this.providers = existings;
+    }
+
+    public List<Object> getProviders() {
+        return providers;
+    }
+
+    @Override
+    public String toString() {
+        return "ExtensionProviderRegistration{}";
+    }
+}

http://git-wip-us.apache.org/repos/asf/tomee/blob/abf8de7a/server/openejb-cxf-rs/src/main/java/org/apache/openejb/server/cxf/rs/event/ServerCreated.java
----------------------------------------------------------------------
diff --git a/server/openejb-cxf-rs/src/main/java/org/apache/openejb/server/cxf/rs/event/ServerCreated.java b/server/openejb-cxf-rs/src/main/java/org/apache/openejb/server/cxf/rs/event/ServerCreated.java
new file mode 100644
index 0000000..b35d78f
--- /dev/null
+++ b/server/openejb-cxf-rs/src/main/java/org/apache/openejb/server/cxf/rs/event/ServerCreated.java
@@ -0,0 +1,55 @@
+/*
+ *     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.event;
+
+import org.apache.cxf.endpoint.Server;
+import org.apache.openejb.AppContext;
+import org.apache.openejb.core.WebContext;
+import org.apache.openejb.observer.Event;
+
+@Event
+public class ServerCreated {
+    private final Server server;
+    private final AppContext appContext;
+    private final WebContext webContext;
+
+    public ServerCreated(final Server server, final AppContext appContext, final WebContext webContext) {
+        this.server = server;
+        this.appContext = appContext;
+        this.webContext = webContext;
+    }
+
+    public Server getServer() {
+        return server;
+    }
+
+    public AppContext getAppContext() {
+        return appContext;
+    }
+
+    public WebContext getWebContext() {
+        return webContext;
+    }
+
+    @Override
+    public String toString() {
+        return "ServerCreated{" +
+                "appContext=" + appContext.getId() +
+                ", webContext=" + webContext.getHost() + '/' + webContext.getId() +
+                '}';
+    }
+}

http://git-wip-us.apache.org/repos/asf/tomee/blob/abf8de7a/server/openejb-cxf-rs/src/main/java/org/apache/openejb/server/cxf/rs/event/ServerDestroyed.java
----------------------------------------------------------------------
diff --git a/server/openejb-cxf-rs/src/main/java/org/apache/openejb/server/cxf/rs/event/ServerDestroyed.java b/server/openejb-cxf-rs/src/main/java/org/apache/openejb/server/cxf/rs/event/ServerDestroyed.java
new file mode 100644
index 0000000..bc3eadc
--- /dev/null
+++ b/server/openejb-cxf-rs/src/main/java/org/apache/openejb/server/cxf/rs/event/ServerDestroyed.java
@@ -0,0 +1,38 @@
+/*
+ *     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.event;
+
+import org.apache.cxf.endpoint.Server;
+import org.apache.openejb.observer.Event;
+
+@Event
+public class ServerDestroyed {
+    private final Server server;
+
+    public ServerDestroyed(final Server server) {
+        this.server = server;
+    }
+
+    public Server getServer() {
+        return server;
+    }
+
+    @Override
+    public String toString() {
+        return "ServerCreated{}";
+    }
+}

http://git-wip-us.apache.org/repos/asf/tomee/blob/abf8de7a/server/openejb-cxf-rs/src/test/java/org/apache/openejb/server/cxf/rs/event/ExtensionProviderRegistrationTest.java
----------------------------------------------------------------------
diff --git a/server/openejb-cxf-rs/src/test/java/org/apache/openejb/server/cxf/rs/event/ExtensionProviderRegistrationTest.java b/server/openejb-cxf-rs/src/test/java/org/apache/openejb/server/cxf/rs/event/ExtensionProviderRegistrationTest.java
new file mode 100644
index 0000000..99edc4e
--- /dev/null
+++ b/server/openejb-cxf-rs/src/test/java/org/apache/openejb/server/cxf/rs/event/ExtensionProviderRegistrationTest.java
@@ -0,0 +1,95 @@
+/*
+ *     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.event;
+
+import org.apache.openejb.jee.WebApp;
+import org.apache.openejb.junit.ApplicationComposer;
+import org.apache.openejb.loader.IO;
+import org.apache.openejb.observer.Observes;
+import org.apache.openejb.server.cxf.rs.event.ExtensionProviderRegistration;
+import org.apache.openejb.testing.Classes;
+import org.apache.openejb.testing.Configuration;
+import org.apache.openejb.testing.EnableServices;
+import org.apache.openejb.testing.Module;
+import org.apache.openejb.testng.PropertiesBuilder;
+import org.apache.openejb.util.NetworkUtil;
+import org.junit.BeforeClass;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+
+import javax.ws.rs.GET;
+import javax.ws.rs.Path;
+import javax.ws.rs.core.Response;
+import javax.ws.rs.ext.ExceptionMapper;
+import javax.ws.rs.ext.Provider;
+import java.io.IOException;
+import java.net.URL;
+import java.util.Properties;
+
+import static org.junit.Assert.assertEquals;
+
+@EnableServices("jaxrs")
+@RunWith(ApplicationComposer.class)
+public class ExtensionProviderRegistrationTest {
+    private static int port = -1;
+
+    @BeforeClass
+    public static void beforeClass() {
+        port = NetworkUtil.getNextAvailablePort();
+    }
+
+    @Configuration
+    public Properties props() {
+        return new PropertiesBuilder()
+                .p("httpejbd.port", Integer.toString(port))
+                .p("observer", "new://Service?class-name=" + Observer.class.getName())
+                .build();
+    }
+
+    @Module
+    @Classes(ServerCreatedEndpoint.class)
+    public WebApp war() {
+        return new WebApp().contextRoot("foo");
+    }
+
+    @Test
+    public void checkEvent() throws IOException {
+        assertEquals("foo", IO.slurp(new URL("http://localhost:" + port + "/foo/ExtensionProviderRegistrationTest/")));
+    }
+
+    @Path("ExtensionProviderRegistrationTest")
+    public static class ServerCreatedEndpoint {
+        @GET
+        public String useless() {
+            throw new IllegalArgumentException("foo");
+        }
+    }
+
+    @Provider
+    public static class MyMapper implements ExceptionMapper<IllegalArgumentException> {
+        @Override
+        public Response toResponse(final IllegalArgumentException e) {
+            return Response.ok(e.getMessage()).build();
+        }
+    }
+
+    public static class Observer {
+        public void obs(@Observes final ExtensionProviderRegistration event) {
+            event.getProviders().add(new MyMapper());
+        }
+    }
+}

http://git-wip-us.apache.org/repos/asf/tomee/blob/abf8de7a/server/openejb-cxf-rs/src/test/java/org/apache/openejb/server/cxf/rs/event/ServerCreatedTest.java
----------------------------------------------------------------------
diff --git a/server/openejb-cxf-rs/src/test/java/org/apache/openejb/server/cxf/rs/event/ServerCreatedTest.java b/server/openejb-cxf-rs/src/test/java/org/apache/openejb/server/cxf/rs/event/ServerCreatedTest.java
new file mode 100644
index 0000000..100a7ab
--- /dev/null
+++ b/server/openejb-cxf-rs/src/test/java/org/apache/openejb/server/cxf/rs/event/ServerCreatedTest.java
@@ -0,0 +1,84 @@
+/*
+ *     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.event;
+
+import org.apache.openejb.jee.WebApp;
+import org.apache.openejb.junit.ApplicationComposer;
+import org.apache.openejb.observer.Observes;
+import org.apache.openejb.server.cxf.rs.event.ServerCreated;
+import org.apache.openejb.testing.Classes;
+import org.apache.openejb.testing.Configuration;
+import org.apache.openejb.testing.EnableServices;
+import org.apache.openejb.testing.Module;
+import org.apache.openejb.testng.PropertiesBuilder;
+import org.apache.openejb.util.NetworkUtil;
+import org.junit.BeforeClass;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+
+import javax.ws.rs.HEAD;
+import javax.ws.rs.Path;
+import java.util.Properties;
+
+import static org.junit.Assert.assertNotNull;
+
+@EnableServices("jaxrs")
+@RunWith(ApplicationComposer.class)
+public class ServerCreatedTest {
+    private static int port = -1;
+
+    @BeforeClass
+    public static void beforeClass() {
+        port = NetworkUtil.getNextAvailablePort();
+    }
+
+    @Configuration
+    public Properties props() {
+        return new PropertiesBuilder()
+                .p("httpejbd.port", Integer.toString(port))
+                .p("observer", "new://Service?class-name=" + Observer.class.getName())
+                .build();
+    }
+
+    @Module
+    @Classes(ServerCreatedEndpoint.class)
+    public WebApp war() {
+        return new WebApp().contextRoot("foo");
+    }
+
+    @Test
+    public void checkEvent() {
+        assertNotNull(Observer.event);
+        assertNotNull(Observer.event.getAppContext());
+        assertNotNull(Observer.event.getWebContext());
+        assertNotNull(Observer.event.getServer());
+    }
+
+    @Path("ServerCreatedTest")
+    public static class ServerCreatedEndpoint {
+        @HEAD
+        public void useless() {}
+    }
+
+    public static class Observer {
+        public static ServerCreated event;
+
+        public void obs(@Observes final ServerCreated event) {
+            Observer.event = event;
+        }
+    }
+}