diff --git a/bundles/demo/src/main/java/actions/osgi/BundlesAction.java b/bundles/demo/src/main/java/actions/osgi/BundlesAction.java index eff79ccce..9c5464fcb 100644 --- a/bundles/demo/src/main/java/actions/osgi/BundlesAction.java +++ b/bundles/demo/src/main/java/actions/osgi/BundlesAction.java @@ -22,7 +22,7 @@ package actions.osgi; import com.opensymphony.xwork2.ActionSupport; import org.apache.struts2.convention.annotation.ResultPath; -import org.apache.struts2.osgi.interceptor.BundleContextAware; +import org.apache.struts2.osgi.action.BundleContextAware; import org.osgi.framework.Bundle; import org.osgi.framework.BundleContext; @@ -33,7 +33,7 @@ import org.osgi.framework.BundleContext; public class BundlesAction extends ActionSupport implements BundleContextAware { private BundleContext bundleContext; - public void setBundleContext(BundleContext bundleContext) { + public void withBundleContext(BundleContext bundleContext) { this.bundleContext = bundleContext; } diff --git a/plugins/osgi/src/main/java/org/apache/struts2/osgi/action/BundleContextAware.java b/plugins/osgi/src/main/java/org/apache/struts2/osgi/action/BundleContextAware.java new file mode 100644 index 000000000..c2be80aaa --- /dev/null +++ b/plugins/osgi/src/main/java/org/apache/struts2/osgi/action/BundleContextAware.java @@ -0,0 +1,31 @@ +/* + * 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.struts2.osgi.action; + +import org.osgi.framework.BundleContext; + +/** + * Actions implementing this interface will receive an instance of the BundleContext, + * the OsgiInterceptor must be applied to the action. + */ +public interface BundleContextAware { + + void withBundleContext(BundleContext bundleContext); + +} diff --git a/plugins/osgi/src/main/java/org/apache/struts2/osgi/interceptor/BundleContextAware.java b/plugins/osgi/src/main/java/org/apache/struts2/osgi/interceptor/BundleContextAware.java index d9eaf60e4..d4ad98d3c 100644 --- a/plugins/osgi/src/main/java/org/apache/struts2/osgi/interceptor/BundleContextAware.java +++ b/plugins/osgi/src/main/java/org/apache/struts2/osgi/interceptor/BundleContextAware.java @@ -23,9 +23,16 @@ import org.osgi.framework.BundleContext; /** * Actions implementing this interface will receive an instance of the BundleContext, * the OsgiInterceptor must be applied to the action. + * + * @deprecated please use {@link org.apache.struts2.osgi.action.BundleContextAware} instead */ +@Deprecated public interface BundleContextAware { + /** + * @deprecated please use {@link org.apache.struts2.osgi.action.BundleContextAware#withBundleContext(BundleContext)} instead + */ + @Deprecated void setBundleContext(BundleContext bundleContext); } diff --git a/plugins/osgi/src/main/java/org/apache/struts2/osgi/interceptor/OsgiInterceptor.java b/plugins/osgi/src/main/java/org/apache/struts2/osgi/interceptor/OsgiInterceptor.java index 09b976927..11dd1f9a0 100644 --- a/plugins/osgi/src/main/java/org/apache/struts2/osgi/interceptor/OsgiInterceptor.java +++ b/plugins/osgi/src/main/java/org/apache/struts2/osgi/interceptor/OsgiInterceptor.java @@ -51,6 +51,10 @@ public class OsgiInterceptor extends AbstractInterceptor { if (action instanceof BundleContextAware) ((BundleContextAware)action).setBundleContext(bundleContext); + if (action instanceof org.apache.struts2.osgi.action.BundleContextAware) { + ((org.apache.struts2.osgi.action.BundleContextAware) action).withBundleContext(bundleContext); + } + //inject service implementations if (action instanceof ServiceAware) { Type[] types = action.getClass().getGenericInterfaces(); diff --git a/plugins/osgi/src/test/java/org/apache/struts2/osgi/interceptor/OsgiInterceptorTest.java b/plugins/osgi/src/test/java/org/apache/struts2/osgi/interceptor/OsgiInterceptorTest.java index 9c4228079..96f287569 100644 --- a/plugins/osgi/src/test/java/org/apache/struts2/osgi/interceptor/OsgiInterceptorTest.java +++ b/plugins/osgi/src/test/java/org/apache/struts2/osgi/interceptor/OsgiInterceptorTest.java @@ -53,6 +53,28 @@ public class OsgiInterceptorTest extends TestCase { EasyMock.verify(bundleContextAware); } + public void testActionBundleContextAware() throws Exception { + ServletContext servletContext = EasyMock.createStrictMock(ServletContext.class); + BundleContext bundleContext = EasyMock.createStrictMock(BundleContext.class); + ActionInvocation actionInvocation = EasyMock.createStrictMock(ActionInvocation.class); + org.apache.struts2.osgi.action.BundleContextAware bundleContextAware = EasyMock.createStrictMock(org.apache.struts2.osgi.action.BundleContextAware.class); + + EasyMock.expect(servletContext.getAttribute(OsgiHost.OSGI_BUNDLE_CONTEXT)).andReturn(bundleContext); + EasyMock.expect(actionInvocation.getAction()).andReturn(bundleContextAware); + bundleContextAware.withBundleContext(bundleContext); + EasyMock.expect(actionInvocation.invoke()).andReturn(""); + + EasyMock.replay(bundleContextAware); + EasyMock.replay(servletContext); + EasyMock.replay(actionInvocation); + + OsgiInterceptor osgiInterceptor = new OsgiInterceptor(); + osgiInterceptor.setServletContext(servletContext); + osgiInterceptor.intercept(actionInvocation); + + EasyMock.verify(bundleContextAware); + } + public void testBundleContextAwareNegative() throws Exception { ServletContext servletContext = EasyMock.createStrictMock(ServletContext.class); ActionInvocation actionInvocation = EasyMock.createStrictMock(ActionInvocation.class); @@ -72,6 +94,25 @@ public class OsgiInterceptorTest extends TestCase { EasyMock.verify(bundleContextAware); } + public void testActionBundleContextAwareNegative() throws Exception { + ServletContext servletContext = EasyMock.createStrictMock(ServletContext.class); + ActionInvocation actionInvocation = EasyMock.createStrictMock(ActionInvocation.class); + org.apache.struts2.osgi.action.BundleContextAware bundleContextAware = EasyMock.createStrictMock(org.apache.struts2.osgi.action.BundleContextAware.class); + + EasyMock.expect(servletContext.getAttribute(OsgiHost.OSGI_BUNDLE_CONTEXT)).andReturn(null); + EasyMock.expect(actionInvocation.invoke()).andReturn(""); + + EasyMock.replay(bundleContextAware); + EasyMock.replay(servletContext); + EasyMock.replay(actionInvocation); + + OsgiInterceptor osgiInterceptor = new OsgiInterceptor(); + osgiInterceptor.setServletContext(servletContext); + osgiInterceptor.intercept(actionInvocation); + + EasyMock.verify(bundleContextAware); + } + public void testServiceAware() throws Exception { ServletContext servletContext = EasyMock.createStrictMock(ServletContext.class); BundleContext bundleContext = EasyMock.createStrictMock(BundleContext.class);