Author: musachy Date: Sun Apr 12 16:41:19 2009 New Revision: 764299 URL: http://svn.apache.org/viewvc?rev=764299&view=rev Log: Add OSGi interceptor
Added: struts/sandbox/trunk/struts2-osgi-plugin/demo-bundle/src/main/java/actions/osgi/BundlesAction.java struts/sandbox/trunk/struts2-osgi-plugin/demo-bundle/src/main/resources/content/osgi/bundles.ftl Modified: struts/sandbox/trunk/struts2-osgi-plugin/admin-bundle/src/main/java/org/apache/struts2/osgi/admin/actions/BundlesAction.java struts/sandbox/trunk/struts2-osgi-plugin/demo-bundle/src/main/java/actions/osgi/HelloWorldAction.java struts/sandbox/trunk/struts2-osgi-plugin/plugin/src/main/java/org/apache/struts2/osgi/BundleAccessor.java struts/sandbox/trunk/struts2-osgi-plugin/plugin/src/main/java/org/apache/struts2/osgi/DefaultBundleAccessor.java struts/sandbox/trunk/struts2-osgi-plugin/plugin/src/main/java/org/apache/struts2/osgi/FelixOsgiHost.java struts/sandbox/trunk/struts2-osgi-plugin/plugin/src/main/java/org/apache/struts2/osgi/OsgiConfigurationProvider.java struts/sandbox/trunk/struts2-osgi-plugin/plugin/src/main/java/org/apache/struts2/osgi/OsgiHost.java struts/sandbox/trunk/struts2-osgi-plugin/plugin/src/main/java/org/apache/struts2/osgi/interceptor/BundleContextAware.java struts/sandbox/trunk/struts2-osgi-plugin/plugin/src/main/java/org/apache/struts2/osgi/interceptor/OsgiInterceptor.java struts/sandbox/trunk/struts2-osgi-plugin/plugin/src/main/java/org/apache/struts2/osgi/interceptor/ServiceAware.java struts/sandbox/trunk/struts2-osgi-plugin/plugin/src/main/resources/beanRefContext.xml Modified: struts/sandbox/trunk/struts2-osgi-plugin/admin-bundle/src/main/java/org/apache/struts2/osgi/admin/actions/BundlesAction.java URL: http://svn.apache.org/viewvc/struts/sandbox/trunk/struts2-osgi-plugin/admin-bundle/src/main/java/org/apache/struts2/osgi/admin/actions/BundlesAction.java?rev=764299&r1=764298&r2=764299&view=diff ============================================================================== --- struts/sandbox/trunk/struts2-osgi-plugin/admin-bundle/src/main/java/org/apache/struts2/osgi/admin/actions/BundlesAction.java (original) +++ struts/sandbox/trunk/struts2-osgi-plugin/admin-bundle/src/main/java/org/apache/struts2/osgi/admin/actions/BundlesAction.java Sun Apr 12 16:41:19 2009 @@ -29,6 +29,7 @@ import org.apache.struts2.dispatcher.DefaultActionSupport; import org.apache.struts2.osgi.BundleAccessor; import org.apache.struts2.osgi.DefaultBundleAccessor; +import org.apache.struts2.osgi.OsgiHost; import org.osgi.framework.Bundle; import org.osgi.framework.BundleException; @@ -42,6 +43,7 @@ private BundleAccessor bundleAccessor; private Configuration configuration; + private OsgiHost osgiHost; public BundlesAction() { this.bundleAccessor = DefaultBundleAccessor.getInstance(); @@ -56,7 +58,7 @@ } public String start() throws BundleException { - Bundle bundle = bundleAccessor.getBundles().get(id); + Bundle bundle = osgiHost.getBundles().get(id); try { bundle.start(); } catch (Exception e) { @@ -67,7 +69,7 @@ } public String stop() throws BundleException { - Bundle bundle = bundleAccessor.getBundles().get(id); + Bundle bundle = osgiHost.getBundles().get(id); try { bundle.stop(); } catch (Exception e) { @@ -78,7 +80,7 @@ } public String update() throws BundleException { - Bundle bundle = bundleAccessor.getBundles().get(id); + Bundle bundle = osgiHost.getBundles().get(id); try { bundle.update(); } catch (Exception e) { @@ -97,7 +99,7 @@ } public Bundle getBundle() { - return bundleAccessor.getBundles().get(id); + return osgiHost.getBundles().get(id); } public List<PackageConfig> getPackages() { @@ -110,7 +112,7 @@ } public Collection<Bundle> getBundles() { - return bundleAccessor.getBundles().values(); + return osgiHost.getBundles().values(); } public String displayProperty(Object obj) { @@ -157,4 +159,9 @@ public void setConfiguration(Configuration configuration) { this.configuration = configuration; } + + @Inject + public void setOsgiHost(OsgiHost osgiHost) { + this.osgiHost = osgiHost; + } } Added: struts/sandbox/trunk/struts2-osgi-plugin/demo-bundle/src/main/java/actions/osgi/BundlesAction.java URL: http://svn.apache.org/viewvc/struts/sandbox/trunk/struts2-osgi-plugin/demo-bundle/src/main/java/actions/osgi/BundlesAction.java?rev=764299&view=auto ============================================================================== --- struts/sandbox/trunk/struts2-osgi-plugin/demo-bundle/src/main/java/actions/osgi/BundlesAction.java (added) +++ struts/sandbox/trunk/struts2-osgi-plugin/demo-bundle/src/main/java/actions/osgi/BundlesAction.java Sun Apr 12 16:41:19 2009 @@ -0,0 +1,57 @@ +/* + * $Id$ + * + * 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 actions.osgi; + +import org.osgi.framework.BundleContext; +import org.osgi.framework.Bundle; +import org.apache.struts2.osgi.interceptor.BundleContextAware; +import org.apache.struts2.osgi.interceptor.ServiceAware; +import org.apache.struts2.convention.annotation.ResultPath; +import org.springframework.context.ApplicationContext; + +import java.util.List; + +import com.opensymphony.xwork2.ActionSupport; + +/** + * This action shows how to interact with the OSGi container, using the OSGi interceptor + */ +...@resultpath("/content") +public class BundlesAction extends ActionSupport implements BundleContextAware, ServiceAware<ApplicationContext> { + private BundleContext bundleContext; + private List<ApplicationContext> services; + + public void setServices(List<ApplicationContext> services) { + this.services = services; + } + + public void setBundleContext(BundleContext bundleContext) { + this.bundleContext = bundleContext; + } + + public int getApplicationContextsCount() { + return services.size(); + } + + public Bundle[] getBundles() { + return bundleContext.getBundles(); + } +} Modified: struts/sandbox/trunk/struts2-osgi-plugin/demo-bundle/src/main/java/actions/osgi/HelloWorldAction.java URL: http://svn.apache.org/viewvc/struts/sandbox/trunk/struts2-osgi-plugin/demo-bundle/src/main/java/actions/osgi/HelloWorldAction.java?rev=764299&r1=764298&r2=764299&view=diff ============================================================================== --- struts/sandbox/trunk/struts2-osgi-plugin/demo-bundle/src/main/java/actions/osgi/HelloWorldAction.java (original) +++ struts/sandbox/trunk/struts2-osgi-plugin/demo-bundle/src/main/java/actions/osgi/HelloWorldAction.java Sun Apr 12 16:41:19 2009 @@ -11,9 +11,8 @@ import java.util.List; @ResultPath("/content") -public class HelloWorldAction extends ActionSupport implements BundleContextAware, ServiceAware<ApplicationContext> { +public class HelloWorldAction extends ActionSupport { private Message message; - private BundleContext bundleContext; @Action("hello-convention") public String execute() { @@ -38,12 +37,4 @@ sb.append("}"); return sb.toString(); } - - public void setBundleContext(BundleContext context) { - this.bundleContext = context; - } - - public void setServices(List<ApplicationContext> service) { - //To change body of implemented methods use File | Settings | File Templates. - } } \ No newline at end of file Added: struts/sandbox/trunk/struts2-osgi-plugin/demo-bundle/src/main/resources/content/osgi/bundles.ftl URL: http://svn.apache.org/viewvc/struts/sandbox/trunk/struts2-osgi-plugin/demo-bundle/src/main/resources/content/osgi/bundles.ftl?rev=764299&view=auto ============================================================================== --- struts/sandbox/trunk/struts2-osgi-plugin/demo-bundle/src/main/resources/content/osgi/bundles.ftl (added) +++ struts/sandbox/trunk/struts2-osgi-plugin/demo-bundle/src/main/resources/content/osgi/bundles.ftl Sun Apr 12 16:41:19 2009 @@ -0,0 +1,18 @@ +<html> + <head> + <title>OSGi integration</title> + </head> + <body> + This action was mapped by <b>Convention</b>, and shows how to get access to the <b>BundleContext</b> + and registered services + <br /> + Spring Application Contexts: ${applicationContextsCount!} + <br> + Bundles List: + <ul> + <#list bundles as bundle> + <li>${bundle.symbolicName!}</li> + </#list> + </ul> + </body> +</html> \ No newline at end of file Modified: struts/sandbox/trunk/struts2-osgi-plugin/plugin/src/main/java/org/apache/struts2/osgi/BundleAccessor.java URL: http://svn.apache.org/viewvc/struts/sandbox/trunk/struts2-osgi-plugin/plugin/src/main/java/org/apache/struts2/osgi/BundleAccessor.java?rev=764299&r1=764298&r2=764299&view=diff ============================================================================== --- struts/sandbox/trunk/struts2-osgi-plugin/plugin/src/main/java/org/apache/struts2/osgi/BundleAccessor.java (original) +++ struts/sandbox/trunk/struts2-osgi-plugin/plugin/src/main/java/org/apache/struts2/osgi/BundleAccessor.java Sun Apr 12 16:41:19 2009 @@ -46,8 +46,6 @@ URL loadResourceFromAllBundles(String name) throws IOException; - Map<String, Bundle> getBundles(); - Set<String> getPackagesByBundle(Bundle bundle); Object getService(ServiceReference ref); @@ -62,5 +60,5 @@ void setBundleContext(BundleContext bundleContext); - void setBundles(Map<String, Bundle> bundles); + void setOsgiHost(OsgiHost osgiHost); } Modified: struts/sandbox/trunk/struts2-osgi-plugin/plugin/src/main/java/org/apache/struts2/osgi/DefaultBundleAccessor.java URL: http://svn.apache.org/viewvc/struts/sandbox/trunk/struts2-osgi-plugin/plugin/src/main/java/org/apache/struts2/osgi/DefaultBundleAccessor.java?rev=764299&r1=764298&r2=764299&view=diff ============================================================================== --- struts/sandbox/trunk/struts2-osgi-plugin/plugin/src/main/java/org/apache/struts2/osgi/DefaultBundleAccessor.java (original) +++ struts/sandbox/trunk/struts2-osgi-plugin/plugin/src/main/java/org/apache/struts2/osgi/DefaultBundleAccessor.java Sun Apr 12 16:41:19 2009 @@ -38,6 +38,7 @@ import com.opensymphony.xwork2.ActionContext; import com.opensymphony.xwork2.ActionInvocation; import com.opensymphony.xwork2.ActionProxy; +import com.opensymphony.xwork2.inject.Inject; import com.opensymphony.xwork2.config.entities.ActionConfig; /** @@ -48,10 +49,10 @@ private static DefaultBundleAccessor self; private static final Logger LOG = LoggerFactory.getLogger(DefaultBundleAccessor.class); - private Map<String, Bundle> bundles = new HashMap<String, Bundle>(); private BundleContext bundleContext; private Map<String, String> packageToBundle; private Map<Bundle, Set<String>> packagesByBundle; + private OsgiHost osgiHost; public DefaultBundleAccessor() { self = this; @@ -76,7 +77,7 @@ } catch (InvalidSyntaxException e) { //cannot happen we are passing null as the param if (LOG.isErrorEnabled()) - LOG.error("Invalid syntaxt for service lookup", e); + LOG.error("Invalid syntax for service lookup", e); } } @@ -91,7 +92,7 @@ this.packageToBundle = packageToBundle; this.packagesByBundle = new HashMap<Bundle, Set<String>>(); for (Map.Entry<String, String> entry : packageToBundle.entrySet()) { - Bundle bundle = bundles.get(entry.getValue()); + Bundle bundle = osgiHost.getActiveBundles().get(entry.getValue()); addPackageFromBundle(bundle, entry.getKey()); } } @@ -133,7 +134,7 @@ bundleName = packageToBundle.get(actionConfig.getPackageName()); } if (bundleName != null) { - return bundles.get(bundleName); + return osgiHost.getActiveBundles().get(bundleName); } return null; } @@ -157,7 +158,7 @@ } public URL loadResourceFromAllBundles(String name) throws IOException { - for (Map.Entry<String, Bundle> entry : bundles.entrySet()) { + for (Map.Entry<String, Bundle> entry : osgiHost.getActiveBundles().entrySet()) { Enumeration e = entry.getValue().getResources(name); if (e.hasMoreElements()) { return (URL) e.nextElement(); @@ -187,7 +188,7 @@ return translate ? OsgiUtil.translateBundleURLToJarURL(url, getCurrentBundle()) : url; } catch (Exception e) { if (LOG.isErrorEnabled()) { - LOG.error("Unable to translate bunfle URL to jar URL", e); + LOG.error("Unable to translate bundle URL to jar URL", e); } return null; @@ -197,10 +198,6 @@ return null; } - public Map<String, Bundle> getBundles() { - return bundles; - } - public Set<String> getPackagesByBundle(Bundle bundle) { return packagesByBundle.get(bundle); } @@ -217,7 +214,7 @@ this.bundleContext = bundleContext; } - public void setBundles(Map<String, Bundle> bundles) { - this.bundles = Collections.unmodifiableMap(bundles); + public void setOsgiHost(OsgiHost osgiHost) { + this.osgiHost = osgiHost; } } Modified: struts/sandbox/trunk/struts2-osgi-plugin/plugin/src/main/java/org/apache/struts2/osgi/FelixOsgiHost.java URL: http://svn.apache.org/viewvc/struts/sandbox/trunk/struts2-osgi-plugin/plugin/src/main/java/org/apache/struts2/osgi/FelixOsgiHost.java?rev=764299&r1=764298&r2=764299&view=diff ============================================================================== --- struts/sandbox/trunk/struts2-osgi-plugin/plugin/src/main/java/org/apache/struts2/osgi/FelixOsgiHost.java (original) +++ struts/sandbox/trunk/struts2-osgi-plugin/plugin/src/main/java/org/apache/struts2/osgi/FelixOsgiHost.java Sun Apr 12 16:41:19 2009 @@ -71,7 +71,7 @@ private Felix felix; private Map<String, Bundle> bundles = Collections.synchronizedMap(new HashMap<String, Bundle>()); - private static Pattern versionPattern = Pattern.compile("([\\d])+[\\.-]"); + private static final Pattern versionPattern = Pattern.compile("([\\d])+[\\.-]"); private ServletContext servletContext; protected void startFelix() { @@ -87,7 +87,7 @@ addExportedPackages(strutsConfigProps, configProps); //find bundles and adde em to autostart property - int bundlePaths = addAutoStartBundles(configProps); + addAutoStartBundles(configProps); // Bundle cache String storageDir = System.getProperty("java.io.tmpdir") + ".felix-cache"; @@ -342,10 +342,24 @@ } } + /** + * This bundle map will not change, but the status of the bundles can change over time. + * Use getActiveBundles() for active bundles + */ public Map<String, Bundle> getBundles() { return Collections.unmodifiableMap(bundles); } + public Map<String, Bundle> getActiveBundles() { + Map<String, Bundle> activeBundleMap = new HashMap<String, Bundle>(); + for (Map.Entry<String, Bundle> entry : bundles.entrySet()) { + if (entry.getValue().getState() == Bundle.ACTIVE) + activeBundleMap.put(entry.getKey(), entry.getValue()); + } + + return activeBundleMap; + } + public BundleContext getBundleContext() { return felix.getBundleContext(); } @@ -358,7 +372,7 @@ } } - public void init(ServletContext servletContext) throws Exception { + public void init(ServletContext servletContext) { this.servletContext = servletContext; startFelix(); } @@ -368,8 +382,13 @@ */ public void bundleChanged(BundleEvent evt) { Bundle bundle = evt.getBundle(); - if (evt.getType() == BundleEvent.STARTED && bundle.getSymbolicName() != null) { - this.bundles.put(bundle.getSymbolicName(), bundle); + String bundleName = bundle.getSymbolicName(); + if (bundleName != null) { + switch (evt.getType()) { + case BundleEvent.STARTED: + this.bundles.put(bundleName, bundle); + break; + } } } } Modified: struts/sandbox/trunk/struts2-osgi-plugin/plugin/src/main/java/org/apache/struts2/osgi/OsgiConfigurationProvider.java URL: http://svn.apache.org/viewvc/struts/sandbox/trunk/struts2-osgi-plugin/plugin/src/main/java/org/apache/struts2/osgi/OsgiConfigurationProvider.java?rev=764299&r1=764298&r2=764299&view=diff ============================================================================== --- struts/sandbox/trunk/struts2-osgi-plugin/plugin/src/main/java/org/apache/struts2/osgi/OsgiConfigurationProvider.java (original) +++ struts/sandbox/trunk/struts2-osgi-plugin/plugin/src/main/java/org/apache/struts2/osgi/OsgiConfigurationProvider.java Sun Apr 12 16:41:19 2009 @@ -67,8 +67,10 @@ public void init(Configuration configuration) throws ConfigurationException { osgiHost = (OsgiHost) servletContext.getAttribute(StrutsOsgiListener.OSGI_HOST); bundleContext = osgiHost.getBundleContext(); - bundleAccessor.setBundles(osgiHost.getBundles()); bundleAccessor.setBundleContext(bundleContext); + // I can't figure out why BundleAccessor doesn't get the OsgiHost injected + //for reason it always gets a new instace...weird + bundleAccessor.setOsgiHost(osgiHost); this.configuration = configuration; } Modified: struts/sandbox/trunk/struts2-osgi-plugin/plugin/src/main/java/org/apache/struts2/osgi/OsgiHost.java URL: http://svn.apache.org/viewvc/struts/sandbox/trunk/struts2-osgi-plugin/plugin/src/main/java/org/apache/struts2/osgi/OsgiHost.java?rev=764299&r1=764298&r2=764299&view=diff ============================================================================== --- struts/sandbox/trunk/struts2-osgi-plugin/plugin/src/main/java/org/apache/struts2/osgi/OsgiHost.java (original) +++ struts/sandbox/trunk/struts2-osgi-plugin/plugin/src/main/java/org/apache/struts2/osgi/OsgiHost.java Sun Apr 12 16:41:19 2009 @@ -37,7 +37,8 @@ String OSGI_BUNDLE_CONTEXT = "__struts_osgi_bundle_context"; void destroy() throws Exception; - void init(ServletContext servletContext) throws Exception; + void init(ServletContext servletContext); Map<String, Bundle> getBundles(); + Map<String, Bundle> getActiveBundles(); BundleContext getBundleContext(); } Modified: struts/sandbox/trunk/struts2-osgi-plugin/plugin/src/main/java/org/apache/struts2/osgi/interceptor/BundleContextAware.java URL: http://svn.apache.org/viewvc/struts/sandbox/trunk/struts2-osgi-plugin/plugin/src/main/java/org/apache/struts2/osgi/interceptor/BundleContextAware.java?rev=764299&r1=764298&r2=764299&view=diff ============================================================================== --- struts/sandbox/trunk/struts2-osgi-plugin/plugin/src/main/java/org/apache/struts2/osgi/interceptor/BundleContextAware.java (original) +++ struts/sandbox/trunk/struts2-osgi-plugin/plugin/src/main/java/org/apache/struts2/osgi/interceptor/BundleContextAware.java Sun Apr 12 16:41:19 2009 @@ -3,7 +3,7 @@ import org.osgi.framework.BundleContext; /** - * Action implementing this interface will receive an instance of the BundleContext, + * Actions implementing this interface will receive an instance of the BundleContext, * the OsgiInterceptor must be applied to the action. */ public interface BundleContextAware { Modified: struts/sandbox/trunk/struts2-osgi-plugin/plugin/src/main/java/org/apache/struts2/osgi/interceptor/OsgiInterceptor.java URL: http://svn.apache.org/viewvc/struts/sandbox/trunk/struts2-osgi-plugin/plugin/src/main/java/org/apache/struts2/osgi/interceptor/OsgiInterceptor.java?rev=764299&r1=764298&r2=764299&view=diff ============================================================================== --- struts/sandbox/trunk/struts2-osgi-plugin/plugin/src/main/java/org/apache/struts2/osgi/interceptor/OsgiInterceptor.java (original) +++ struts/sandbox/trunk/struts2-osgi-plugin/plugin/src/main/java/org/apache/struts2/osgi/interceptor/OsgiInterceptor.java Sun Apr 12 16:41:19 2009 @@ -19,7 +19,7 @@ /** * If a class implements BundleContextAware, this interceptor will call the setBundleContext(BundleContext) - * method on it + * method on it. If a class implements ServiceAware<T>, this interceptor will call setService(List<T>) */ public class OsgiInterceptor extends AbstractInterceptor { private static final Logger LOG = LoggerFactory.getLogger(OsgiInterceptor.class); Modified: struts/sandbox/trunk/struts2-osgi-plugin/plugin/src/main/java/org/apache/struts2/osgi/interceptor/ServiceAware.java URL: http://svn.apache.org/viewvc/struts/sandbox/trunk/struts2-osgi-plugin/plugin/src/main/java/org/apache/struts2/osgi/interceptor/ServiceAware.java?rev=764299&r1=764298&r2=764299&view=diff ============================================================================== --- struts/sandbox/trunk/struts2-osgi-plugin/plugin/src/main/java/org/apache/struts2/osgi/interceptor/ServiceAware.java (original) +++ struts/sandbox/trunk/struts2-osgi-plugin/plugin/src/main/java/org/apache/struts2/osgi/interceptor/ServiceAware.java Sun Apr 12 16:41:19 2009 @@ -2,6 +2,11 @@ import java.util.List; +/** + * Classes implementing this interface, will be injected a list of services + * registered with the type of the parameterized type + * @param <T> The type of the service + */ public interface ServiceAware<T> { - void setServices(List<T> service); + void setServices(List<T> services); } Modified: struts/sandbox/trunk/struts2-osgi-plugin/plugin/src/main/resources/beanRefContext.xml URL: http://svn.apache.org/viewvc/struts/sandbox/trunk/struts2-osgi-plugin/plugin/src/main/resources/beanRefContext.xml?rev=764299&r1=764298&r2=764299&view=diff ============================================================================== --- struts/sandbox/trunk/struts2-osgi-plugin/plugin/src/main/resources/beanRefContext.xml (original) +++ struts/sandbox/trunk/struts2-osgi-plugin/plugin/src/main/resources/beanRefContext.xml Sun Apr 12 16:41:19 2009 @@ -4,7 +4,7 @@ <bean id="parent-context-bean" class="org.springframework.context.support.ClassPathXmlApplicationContext"> <constructor-arg> <list> - <value>classpath*:applicationContext.xml</value> + <value>classpath*:/spring/*.xml</value> </list> </constructor-arg> </bean>