WW-4963 Implements new BundleContextAware interface

that uses withBundleContext instead of setBundleContext
This commit is contained in:
Lukasz Lenart
2018-09-25 08:23:45 +02:00
parent 384d418039
commit 01016b458e
5 changed files with 85 additions and 2 deletions
@@ -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;
}
@@ -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);
}
@@ -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);
}
@@ -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();
@@ -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);