Author: rmannibucau Date: Mon Apr 14 20:30:51 2014 New Revision: 1587310 URL: http://svn.apache.org/r1587310 Log: OPENEJB-2088 jaxws handler cdi integration
Added: tomee/tomee/trunk/server/openejb-cxf/src/test/java/org/apache/openejb/server/cxf/CdiHandlersTest.java tomee/tomee/trunk/server/openejb-cxf/src/test/java/org/apache/openejb/server/cxf/handler/ tomee/tomee/trunk/server/openejb-cxf/src/test/java/org/apache/openejb/server/cxf/handler/SimpleHandler.java tomee/tomee/trunk/server/openejb-cxf/src/test/resources/handlers.xml Modified: tomee/tomee/trunk/container/openejb-core/src/main/java/org/apache/openejb/core/webservices/HandlerResolverImpl.java Modified: tomee/tomee/trunk/container/openejb-core/src/main/java/org/apache/openejb/core/webservices/HandlerResolverImpl.java URL: http://svn.apache.org/viewvc/tomee/tomee/trunk/container/openejb-core/src/main/java/org/apache/openejb/core/webservices/HandlerResolverImpl.java?rev=1587310&r1=1587309&r2=1587310&view=diff ============================================================================== --- tomee/tomee/trunk/container/openejb-core/src/main/java/org/apache/openejb/core/webservices/HandlerResolverImpl.java (original) +++ tomee/tomee/trunk/container/openejb-core/src/main/java/org/apache/openejb/core/webservices/HandlerResolverImpl.java Mon Apr 14 20:30:51 2014 @@ -16,9 +16,17 @@ */ package org.apache.openejb.core.webservices; -import org.apache.openejb.Injection; -import org.apache.openejb.InjectionProcessor; +import static org.apache.openejb.InjectionProcessor.unwrap; +import java.util.ArrayList; +import java.util.Collection; +import java.util.Collections; +import java.util.Iterator; +import java.util.List; +import java.util.Set; + +import javax.enterprise.inject.InjectionException; +import javax.enterprise.inject.spi.Bean; import javax.naming.Context; import javax.xml.namespace.QName; import javax.xml.ws.WebServiceException; @@ -26,15 +34,18 @@ import javax.xml.ws.handler.Handler; import javax.xml.ws.handler.HandlerResolver; import javax.xml.ws.handler.LogicalHandler; import javax.xml.ws.handler.PortInfo; -import java.util.ArrayList; -import java.util.Collection; -import java.util.Collections; -import java.util.Iterator; -import java.util.List; -import static org.apache.openejb.InjectionProcessor.unwrap; +import org.apache.openejb.Injection; +import org.apache.openejb.InjectionProcessor; +import org.apache.openejb.util.LogCategory; +import org.apache.openejb.util.Logger; +import org.apache.webbeans.config.WebBeansContext; +import org.apache.webbeans.container.BeanManagerImpl; +import org.apache.webbeans.context.creational.CreationalContextImpl; public class HandlerResolverImpl implements HandlerResolver { + private static final Logger LOGGER = Logger.getInstance(LogCategory.OPENEJB_WS, HandlerResolverImpl.class); + private final List<HandlerChainData> handlerChains; private final Collection<Injection> injections; private final Context context; @@ -74,7 +85,37 @@ public class HandlerResolverImpl impleme final List<Handler> handlers = new ArrayList<Handler>(handlerChain.getHandlers().size()); for (final HandlerData handler : handlerChain.getHandlers()) { - try { + final WebBeansContext webBeansContext = WebBeansContext.currentInstance(); + if (webBeansContext != null) { // cdi + final BeanManagerImpl bm = webBeansContext.getBeanManagerImpl(); + if (bm.isInUse()) { + try { + final Set<Bean<?>> beans = bm.getBeans(handler.getHandlerClass()); + final Bean<?> bean = bm.resolve(beans); + if (bean != null) { // proxy so faster to do it + final boolean normalScoped = bm.isNormalScope(bean.getScope()); + final CreationalContextImpl<?> creationalContext = bm.createCreationalContext(bean); + final Handler instance = Handler.class.cast(bm.getReference(bean, bean.getBeanClass(), creationalContext)); + + // hack for destroyHandlers() + handlers.add(instance); + handlerInstances.add(new InjectionProcessor<Handler>(instance, Collections.<Injection>emptySet(), null) { + @Override + public void preDestroy() { + if (!normalScoped) { + creationalContext.release(); + } + } + }); + continue; + } + } catch (final InjectionException ie) { + LOGGER.info(ie.getMessage(), ie); + } + } + } + + try { // old way final Class<? extends Handler> handlerClass = handler.getHandlerClass().asSubclass(Handler.class); final InjectionProcessor<Handler> processor = new InjectionProcessor<Handler>(handlerClass, injections, Added: tomee/tomee/trunk/server/openejb-cxf/src/test/java/org/apache/openejb/server/cxf/CdiHandlersTest.java URL: http://svn.apache.org/viewvc/tomee/tomee/trunk/server/openejb-cxf/src/test/java/org/apache/openejb/server/cxf/CdiHandlersTest.java?rev=1587310&view=auto ============================================================================== --- tomee/tomee/trunk/server/openejb-cxf/src/test/java/org/apache/openejb/server/cxf/CdiHandlersTest.java (added) +++ tomee/tomee/trunk/server/openejb-cxf/src/test/java/org/apache/openejb/server/cxf/CdiHandlersTest.java Mon Apr 14 20:30:51 2014 @@ -0,0 +1,79 @@ +/** + * 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 static org.junit.Assert.assertTrue; + +import java.net.MalformedURLException; +import java.net.URL; + +import javax.jws.HandlerChain; +import javax.jws.WebService; +import javax.xml.namespace.QName; +import javax.xml.ws.Service; + +import org.apache.openejb.jee.WebApp; +import org.apache.openejb.junit.ApplicationComposer; +import org.apache.openejb.server.cxf.handler.SimpleHandler; +import org.apache.openejb.testing.Classes; +import org.apache.openejb.testing.EnableServices; +import org.apache.openejb.testing.Module; +import org.junit.Test; +import org.junit.runner.RunWith; + +@EnableServices("jax-ws") +@RunWith(ApplicationComposer.class) +public class CdiHandlersTest { + @Module + @Classes(value = { MyHandledWebservice.class, ACdiSimpleTaste.class, SimpleHandler.class }, cdi = true) + public WebApp module() { + return new WebApp().contextRoot("/test").addServlet("ws", MyHandledWebservice.class.getName(), "/ws"); + } + + @Test + public void checkHandlersAreCDIBeans() throws MalformedURLException { + SimpleHandler.reset(); + Service.create(new URL("http://localhost:4204/test/ws?wsdl"), + new QName("http://cxf.server.openejb.apache.org/", "MyHandledWebserviceService")) + .getPort(MyHandledWsApi.class).test(); + assertTrue(SimpleHandler.close); + assertTrue(SimpleHandler.handled); + assertTrue(SimpleHandler.pre); + assertTrue(SimpleHandler.post); + } + + public static class ACdiSimpleTaste { + public String ok() { + return "ok"; + } + } + + @WebService + public static interface MyHandledWsApi { + String test(); + } + + @WebService + @HandlerChain(file = "/handlers.xml") + public static class MyHandledWebservice implements MyHandledWsApi { + @Override + public String test() { + return "ok"; + } + } +} + Added: tomee/tomee/trunk/server/openejb-cxf/src/test/java/org/apache/openejb/server/cxf/handler/SimpleHandler.java URL: http://svn.apache.org/viewvc/tomee/tomee/trunk/server/openejb-cxf/src/test/java/org/apache/openejb/server/cxf/handler/SimpleHandler.java?rev=1587310&view=auto ============================================================================== --- tomee/tomee/trunk/server/openejb-cxf/src/test/java/org/apache/openejb/server/cxf/handler/SimpleHandler.java (added) +++ tomee/tomee/trunk/server/openejb-cxf/src/test/java/org/apache/openejb/server/cxf/handler/SimpleHandler.java Mon Apr 14 20:30:51 2014 @@ -0,0 +1,70 @@ +/* + * 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.handler; + +import javax.annotation.PostConstruct; +import javax.annotation.PreDestroy; +import javax.enterprise.context.RequestScoped; +import javax.inject.Inject; +import javax.xml.ws.handler.Handler; +import javax.xml.ws.handler.MessageContext; + +import org.apache.openejb.server.cxf.CdiHandlersTest; + +@RequestScoped // otherwise can't test pre/post hooks +public class SimpleHandler implements Handler { + public static boolean pre = false; + public static boolean post = false; + public static boolean handled = false; + public static boolean close = false; + + public static void reset() { + handled = false; + close = false; + pre = false; + post = false; + } + + @Inject + private CdiHandlersTest.ACdiSimpleTaste cdi; + + @PostConstruct + public void post() { + post = true; + } + + @PreDestroy + public void pre() { + pre = true; + } + + @Override + public void close(final MessageContext messageContext) { + close = cdi != null && "ok".equals(cdi.ok()); + } + + @Override + public boolean handleFault(final MessageContext messageContext) { + return false; + } + + @Override + public boolean handleMessage(final MessageContext messageContext) { + handled = cdi != null && "ok".equals(cdi.ok()); + return handled; + } +} Added: tomee/tomee/trunk/server/openejb-cxf/src/test/resources/handlers.xml URL: http://svn.apache.org/viewvc/tomee/tomee/trunk/server/openejb-cxf/src/test/resources/handlers.xml?rev=1587310&view=auto ============================================================================== --- tomee/tomee/trunk/server/openejb-cxf/src/test/resources/handlers.xml (added) +++ tomee/tomee/trunk/server/openejb-cxf/src/test/resources/handlers.xml Mon Apr 14 20:30:51 2014 @@ -0,0 +1,28 @@ +<?xml version="1.0" encoding="UTF-8"?> +<!-- + 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. +--> + +<!-- $Rev: 602704 $ $Date: 2007-12-09 18:58:22 +0100 (dim., 09 déc. 2007) $ --> + +<jws:handler-chains xmlns:jws="http://java.sun.com/xml/ns/javaee"> + <jws:handler-chain> + <jws:handler> + <jws:handler-name>GreeterHandler</jws:handler-name> + <jws:handler-class>org.apache.openejb.server.cxf.handler.SimpleHandler</jws:handler-class> + </jws:handler> + </jws:handler-chain> +</jws:handler-chains>