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/26 19:59:38 UTC

[2/2] tomee git commit: TOMEE-1458 @WebServiceRef cxf config support - missing files

TOMEE-1458 @WebServiceRef cxf config support - missing files


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

Branch: refs/heads/develop
Commit: 13f9589b50be2093c791434403281b2d69a550f2
Parents: 613a9ab
Author: Romain Manni-Bucau <rm...@apache.org>
Authored: Wed Nov 26 19:58:53 2014 +0100
Committer: Romain Manni-Bucau <rm...@apache.org>
Committed: Wed Nov 26 19:58:53 2014 +0100

----------------------------------------------------------------------
 .../testing/ApplicationConfiguration.java       |  28 +++++
 .../client/WebServiceInjectionConfigurator.java | 113 +++++++++++++++++++
 .../server/cxf/WebServiceInjectionTest.java     |  85 ++++++++++++++
 3 files changed, 226 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/tomee/blob/13f9589b/container/openejb-core/src/main/java/org/apache/openejb/testing/ApplicationConfiguration.java
----------------------------------------------------------------------
diff --git a/container/openejb-core/src/main/java/org/apache/openejb/testing/ApplicationConfiguration.java b/container/openejb-core/src/main/java/org/apache/openejb/testing/ApplicationConfiguration.java
new file mode 100644
index 0000000..09819b2
--- /dev/null
+++ b/container/openejb-core/src/main/java/org/apache/openejb/testing/ApplicationConfiguration.java
@@ -0,0 +1,28 @@
+/*
+ * 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.testing;
+
+import java.lang.annotation.ElementType;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import java.lang.annotation.Target;
+
+@Target(ElementType.METHOD)
+@Retention(RetentionPolicy.RUNTIME)
+public @interface ApplicationConfiguration {
+}

http://git-wip-us.apache.org/repos/asf/tomee/blob/13f9589b/server/openejb-cxf/src/main/java/org/apache/openejb/server/cxf/client/WebServiceInjectionConfigurator.java
----------------------------------------------------------------------
diff --git a/server/openejb-cxf/src/main/java/org/apache/openejb/server/cxf/client/WebServiceInjectionConfigurator.java b/server/openejb-cxf/src/main/java/org/apache/openejb/server/cxf/client/WebServiceInjectionConfigurator.java
new file mode 100644
index 0000000..78565a1
--- /dev/null
+++ b/server/openejb-cxf/src/main/java/org/apache/openejb/server/cxf/client/WebServiceInjectionConfigurator.java
@@ -0,0 +1,113 @@
+/**
+ *
+ * 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.client;
+
+import org.apache.cxf.endpoint.Client;
+import org.apache.cxf.frontend.ClientProxy;
+import org.apache.openejb.OpenEJBException;
+import org.apache.openejb.assembler.classic.OpenEjbConfiguration;
+import org.apache.openejb.assembler.classic.ServiceInfo;
+import org.apache.openejb.config.ConfigurationFactory;
+import org.apache.openejb.config.sys.Openejb;
+import org.apache.openejb.config.sys.Service;
+import org.apache.openejb.core.ivm.naming.JaxWsServiceReference;
+import org.apache.openejb.loader.SystemInstance;
+import org.apache.openejb.util.LogCategory;
+import org.apache.openejb.util.Logger;
+
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.List;
+import java.util.Properties;
+
+import static java.util.Arrays.asList;
+import static org.apache.openejb.server.cxf.ConfigureCxfSecurity.getPropsFromProperties;
+import static org.apache.openejb.server.cxf.ConfigureCxfSecurity.setupWSS4JChain;
+import static org.apache.openejb.server.cxf.transport.util.CxfUtil.configureInterceptors;
+
+/**
+ * Used to configure a @WebServiceRef.
+ * Config uses application.properties.
+ * Format is:
+ *
+ * # global for all clients
+ * cxf.jaxws.client.out-interceptors = com.foo.MyInInterceptor
+ * # specific
+ * cxf.jaxws.client.{ns}MyPort.out-interceptors = com.foo.MyInInterceptor
+ *
+ * Services can be defines in tomee.xml or application.properties.
+ *
+ * Note: resources.xml are ignored for now (to be enhanced)
+ */
+public class WebServiceInjectionConfigurator implements JaxWsServiceReference.WebServiceClientCustomizer {
+    @Override
+    public void customize(final Object o, final Properties properties) {
+        try {
+            configure(ClientProxy.getClient(o), properties);
+        } catch (final Exception e) {
+            Logger.getInstance(LogCategory.CXF, WebServiceInjectionConfigurator.class.getName())
+                    .error(e.getMessage(), e);
+        }
+    }
+
+    private void configure(final Client client, final Properties properties) {
+        if (properties == null) {
+            return;
+        }
+
+        for (final String suffix : asList("", client.getEndpoint().getEndpointInfo().getName().toString() + ".")) {
+            // wss4j which is historically quite particular
+            setupWSS4JChain(client,
+                    getPropsFromProperties(properties, "cxf.jaxws.client.wss4j.in." + suffix),
+                    getPropsFromProperties(properties, "cxf.jaxws.client.wss4j.out." + suffix));
+
+            // here (ie at runtime) we have no idea which services were linked to the app
+            // so using tomee.xml ones for now (not that shocking since we externalize the config with this class)
+            final OpenEjbConfiguration config = SystemInstance.get().getComponent(OpenEjbConfiguration.class);
+            final List<ServiceInfo> services = new ArrayList<ServiceInfo>(config.facilities != null && config.facilities.services != null ? config.facilities.services : Collections.<ServiceInfo>emptyList());
+            services.addAll(getServices(properties));
+            configureInterceptors(client, "cxf.jaxws.client." + suffix, services, properties);
+        }
+    }
+
+    private Collection<ServiceInfo> getServices(final Properties properties) {
+        final ConfigurationFactory cf = SystemInstance.get().getComponent(ConfigurationFactory.class);
+        if (cf == null || !ConfigurationFactory.class.isInstance(cf)) {
+            return Collections.emptyList();
+        }
+
+        final Openejb openejb = new Openejb();
+        ConfigurationFactory.fillOpenEjb(openejb, properties);
+
+        final List<Service> services = openejb.getServices();
+        if (services.isEmpty()) {
+            return Collections.emptyList();
+        }
+
+        final Collection<ServiceInfo> info = new ArrayList<ServiceInfo>(services.size());
+        for (final Service s : services) {
+            try {
+                info.add(cf.configureService(s, ServiceInfo.class));
+            } catch (final OpenEJBException e) {
+                throw new IllegalArgumentException(e);
+            }
+        }
+        return info;
+    }
+}

http://git-wip-us.apache.org/repos/asf/tomee/blob/13f9589b/server/openejb-cxf/src/test/java/org/apache/openejb/server/cxf/WebServiceInjectionTest.java
----------------------------------------------------------------------
diff --git a/server/openejb-cxf/src/test/java/org/apache/openejb/server/cxf/WebServiceInjectionTest.java b/server/openejb-cxf/src/test/java/org/apache/openejb/server/cxf/WebServiceInjectionTest.java
new file mode 100644
index 0000000..159e40c
--- /dev/null
+++ b/server/openejb-cxf/src/test/java/org/apache/openejb/server/cxf/WebServiceInjectionTest.java
@@ -0,0 +1,85 @@
+/**
+ * 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;
+
+import org.apache.cxf.endpoint.Client;
+import org.apache.cxf.frontend.ClientProxy;
+import org.apache.cxf.interceptor.LoggingOutInterceptor;
+import org.apache.openejb.jee.WebApp;
+import org.apache.openejb.junit.ApplicationComposer;
+import org.apache.openejb.testing.ApplicationConfiguration;
+import org.apache.openejb.testing.Classes;
+import org.apache.openejb.testing.EnableServices;
+import org.apache.openejb.testing.Module;
+import org.apache.openejb.testng.PropertiesBuilder;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+
+import javax.ejb.Singleton;
+import javax.jws.WebService;
+import javax.xml.ws.WebServiceRef;
+import java.util.Properties;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertTrue;
+
+@EnableServices("jax-ws")
+@RunWith(ApplicationComposer.class)
+public class WebServiceInjectionTest {
+    @Module
+    @Classes(innerClassesAsBean = true)
+    public WebApp module() {
+        return new WebApp();
+    }
+
+    @WebServiceRef
+    private MyWsApi api;
+
+    @Test
+    public void checkInjection() {
+        // assertEquals("ok", api.test()); // local call so skip it but check config which is actually the only interesting thing
+        final Client client = ClientProxy.getClient(api);
+        assertNotNull(client);
+        assertEquals(1, client.getOutInterceptors().size());
+        assertTrue(LoggingOutInterceptor.class.isInstance(client.getOutInterceptors().iterator().next()));
+    }
+
+    @ApplicationConfiguration
+    public Properties props() {
+        // return new PropertiesBuilder().p("cxf.jaxws.client.out-interceptors", LoggingOutInterceptor.class.getName()).build();
+        // return new PropertiesBuilder().p("cxf.jaxws.client.{http://cxf.server.openejb.apache.org/}MyWebservicePort.out-interceptors", LoggingOutInterceptor.class.getName()).build();
+        return new PropertiesBuilder()
+                .p("cxf.jaxws.client.{http://cxf.server.openejb.apache.org/}MyWebservicePort.out-interceptors", "loo")
+                .p("loo", "new://Service?class-name=" + LoggingOutInterceptor.class.getName())
+                .build();
+    }
+
+    @WebService
+    public static interface MyWsApi {
+        String test();
+    }
+
+    @WebService
+    @Singleton
+    public static class MyWebservice implements MyWsApi {
+        @Override
+        public String test() {
+            return "ok";
+        }
+    }
+}