Skip to content

Commit

Permalink
WW-3958 Updates Apache Felix and adds option to cooperate with Felix …
Browse files Browse the repository at this point in the history
…integrated in Glassfish

git-svn-id: https://svn.apache.org/repos/asf/struts/struts2/trunk@1484815 13f79535-47bb-0310-9956-ffa450edef68
  • Loading branch information
lukaszlenart committed May 21, 2013
1 parent 7df4af2 commit 1c5b931
Show file tree
Hide file tree
Showing 25 changed files with 890 additions and 522 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,8 @@
import com.opensymphony.xwork2.config.entities.PackageConfig;
import com.opensymphony.xwork2.inject.Inject;
import org.apache.struts2.osgi.BundleAccessor;
import org.apache.struts2.osgi.OsgiHost;
import org.apache.struts2.osgi.host.OsgiHost;
import org.apache.struts2.osgi.StrutsOsgiListener;
import org.apache.struts2.osgi.OsgiConfigurationProvider;
import org.apache.struts2.util.ServletContextAware;
import org.osgi.framework.Bundle;
import org.osgi.framework.BundleException;
Expand All @@ -40,10 +39,6 @@
import java.util.List;
import java.util.Collections;
import java.util.Comparator;
import java.util.Map;
import java.util.HashMap;
import java.util.Enumeration;
import java.util.Dictionary;

public class BundlesAction extends ActionSupport implements ServletContextAware {

Expand Down
8 changes: 8 additions & 0 deletions plugins/osgi/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,14 @@
<groupId>org.apache.felix</groupId>
<artifactId>org.apache.felix.main</artifactId>
</dependency>
<dependency>
<groupId>org.apache.felix</groupId>
<artifactId>org.apache.felix.shell</artifactId>
</dependency>
<dependency>
<groupId>org.apache.felix</groupId>
<artifactId>org.apache.felix.shell.tui</artifactId>
</dependency>

<dependency>
<groupId>org.apache.velocity</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,17 @@

package org.apache.struts2.osgi;

import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.Map;
import java.util.Set;

import org.apache.struts2.osgi.host.OsgiHost;
import org.osgi.framework.Bundle;
import org.osgi.framework.BundleContext;
import org.osgi.framework.InvalidSyntaxException;
import org.osgi.framework.ServiceReference;

import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.Set;

public interface BundleAccessor {

String CURRENT_BUNDLE_NAME = "__bundle_name__";
Expand Down Expand Up @@ -59,4 +59,5 @@ public interface BundleAccessor {
void setBundleContext(BundleContext bundleContext);

void setOsgiHost(OsgiHost osgiHost);

}
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,19 @@

import com.opensymphony.xwork2.util.finder.ClassLoaderInterface;

import java.net.URL;
import java.util.Enumeration;
import java.util.Collections;
import java.io.IOException;
import java.io.InputStream;

import org.osgi.framework.Bundle;
import java.net.URL;
import java.util.Collection;
import java.util.Collections;
import java.util.Enumeration;
import java.util.Hashtable;

/**
* ClassLoaderInterface instance that delegates to the singleton of DefaultBundleAccessor
*/
public class BundleClassLoaderInterface implements ClassLoaderInterface {

public Class<?> loadClass(String name) throws ClassNotFoundException {
return DefaultBundleAccessor.getInstance().loadClass(name);
}
Expand All @@ -44,6 +45,10 @@ public URL getResource(String name) {
}

public Enumeration<URL> getResources(String name) throws IOException {
Collection<URL> coll = DefaultBundleAccessor.getInstance().loadResources(name, true);
if (coll == null){
return new Hashtable<Object, URL>().elements();
}
return Collections.enumeration(DefaultBundleAccessor.getInstance().loadResources(name, true));
}

Expand All @@ -54,4 +59,5 @@ public InputStream getResourceAsStream(String name) throws IOException {
public ClassLoaderInterface getParent() {
return null;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@

package org.apache.struts2.osgi;

import com.opensymphony.xwork2.FileManager;
import com.opensymphony.xwork2.FileManagerFactory;
import com.opensymphony.xwork2.ObjectFactory;
import com.opensymphony.xwork2.config.Configuration;
import com.opensymphony.xwork2.config.ConfigurationException;
Expand Down Expand Up @@ -49,13 +49,14 @@ public class BundlePackageLoader implements PackageLoader {
private static final Logger LOG = LoggerFactory.getLogger(BundlePackageLoader.class);

public List<PackageConfig> loadPackages(Bundle bundle, BundleContext bundleContext, ObjectFactory objectFactory,
FileManager fileManager, Map<String, PackageConfig> pkgConfigs) throws ConfigurationException {
FileManagerFactory fileManagerFactory, Map<String, PackageConfig> pkgConfigs) throws ConfigurationException {
Configuration config = new DefaultConfiguration("struts.xml");
BundleConfigurationProvider prov = new BundleConfigurationProvider("struts.xml", bundle, bundleContext);
for (PackageConfig pkg : pkgConfigs.values()) {
config.addPackageConfig(pkg.getName(), pkg);
}
prov.setObjectFactory(objectFactory);
prov.setFileManagerFactory(fileManagerFactory);
prov.init(config);
prov.loadPackages();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,25 +21,28 @@

package org.apache.struts2.osgi;

import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.MalformedURLException;
import java.util.*;
import java.util.Map.Entry;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.ActionProxy;
import com.opensymphony.xwork2.config.entities.ActionConfig;
import com.opensymphony.xwork2.util.logging.Logger;
import com.opensymphony.xwork2.util.logging.LoggerFactory;
import org.apache.struts2.osgi.host.OsgiHost;
import org.osgi.framework.Bundle;
import org.osgi.framework.BundleContext;
import org.osgi.framework.InvalidSyntaxException;
import org.osgi.framework.ServiceReference;

import com.opensymphony.xwork2.util.logging.Logger;
import com.opensymphony.xwork2.util.logging.LoggerFactory;
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;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
* Helper class that find resources and loads classes from the list of bundles
Expand Down Expand Up @@ -139,8 +142,10 @@ public List<URL> loadResources(String name, boolean translate) throws IOExceptio
if (bundle != null) {
List<URL> resources = new ArrayList<URL>();
Enumeration e = bundle.getResources(name);
while (e.hasMoreElements()) {
resources.add(translate ? OsgiUtil.translateBundleURLToJarURL((URL) e.nextElement(), getCurrentBundle()) : (URL) e.nextElement());
if (e != null) {
while (e.hasMoreElements()) {
resources.add(translate ? OsgiUtil.translateBundleURLToJarURL((URL) e.nextElement(), getCurrentBundle()) : (URL) e.nextElement());
}
}
return resources;
}
Expand All @@ -151,7 +156,7 @@ public List<URL> loadResources(String name, boolean translate) throws IOExceptio
public URL loadResourceFromAllBundles(String name) throws IOException {
for (Map.Entry<String, Bundle> entry : osgiHost.getActiveBundles().entrySet()) {
Enumeration e = entry.getValue().getResources(name);
if (e.hasMoreElements()) {
if (e != null && e.hasMoreElements()) {
return (URL) e.nextElement();
}
}
Expand Down Expand Up @@ -208,4 +213,5 @@ public void setBundleContext(BundleContext bundleContext) {
public void setOsgiHost(OsgiHost osgiHost) {
this.osgiHost = osgiHost;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,16 @@

package org.apache.struts2.osgi;

import java.util.Map;

import com.opensymphony.xwork2.ObjectFactory;
import com.opensymphony.xwork2.config.PackageProvider;
import com.opensymphony.xwork2.inject.Container;
import com.opensymphony.xwork2.inject.Inject;
import org.apache.struts2.util.ObjectFactoryDestroyable;

import java.util.Map;

public class DelegatingObjectFactory extends ObjectFactory implements ObjectFactoryDestroyable {

private ObjectFactory delegateObjectFactory;
private BundleAccessor bundleResourceLoader;
private OsgiConfigurationProvider osgiConfigurationProvider;
Expand Down Expand Up @@ -88,4 +89,5 @@ public void destroy() {
public void setOsgiConfigurationProvider(PackageProvider osgiConfigurationProvider) {
this.osgiConfigurationProvider = (OsgiConfigurationProvider) osgiConfigurationProvider;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
package org.apache.struts2.osgi;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.FileManager;
import com.opensymphony.xwork2.FileManagerFactory;
import com.opensymphony.xwork2.ObjectFactory;
import com.opensymphony.xwork2.config.Configuration;
Expand All @@ -34,6 +33,7 @@
import com.opensymphony.xwork2.util.logging.Logger;
import com.opensymphony.xwork2.util.logging.LoggerFactory;
import org.apache.commons.lang3.StringUtils;
import org.apache.struts2.osgi.host.OsgiHost;
import org.apache.struts2.osgi.loaders.VelocityBundleResourceLoader;
import org.apache.struts2.views.velocity.VelocityManager;
import org.apache.velocity.app.Velocity;
Expand All @@ -52,11 +52,12 @@
* Struts package provider that starts the OSGi container and deelgates package loading
*/
public class OsgiConfigurationProvider implements PackageProvider, BundleListener {

private static final Logger LOG = LoggerFactory.getLogger(OsgiConfigurationProvider.class);

private Configuration configuration;
private ObjectFactory objectFactory;
private FileManager fileManager;
private FileManagerFactory fileManagerFactory;

private OsgiHost osgiHost;
private BundleContext bundleContext;
Expand All @@ -81,10 +82,10 @@ public synchronized void loadPackages() throws ConfigurationException {
if (LOG.isTraceEnabled())
LOG.trace("Loading packages from XML and Convention on startup");

//init action contect
//init action context
ActionContext ctx = ActionContext.getContext();
if (ctx == null) {
ctx = new ActionContext(new HashMap());
ctx = createActionContext();
ActionContext.setContext(ctx);
}

Expand All @@ -95,7 +96,7 @@ public synchronized void loadPackages() throws ConfigurationException {
String bundleName = bundle.getSymbolicName();
if (shouldProcessBundle(bundle) && !bundleNames.contains(bundleName)) {
bundleNames.add(bundleName);
//load XML and COnvention config
//load XML and Convention config
loadConfigFromBundle(bundle);
}
}
Expand All @@ -104,6 +105,10 @@ public synchronized void loadPackages() throws ConfigurationException {
bundleContext.addBundleListener(this);
}

protected ActionContext createActionContext() {
return new ActionContext(new HashMap<String, Object>());
}

/**
* Loads XML config as well as Convention config from a bundle
* Limitation: Constants and Beans are ignored on XML config
Expand All @@ -117,7 +122,7 @@ protected void loadConfigFromBundle(Bundle bundle) {
//init action context
ActionContext ctx = ActionContext.getContext();
if (ctx == null) {
ctx = new ActionContext(new HashMap());
ctx = createActionContext();
ActionContext.setContext(ctx);
}

Expand All @@ -133,14 +138,14 @@ protected void loadConfigFromBundle(Bundle bundle) {

//XML config
PackageLoader loader = new BundlePackageLoader();
for (PackageConfig pkg : loader.loadPackages(bundle, bundleContext, objectFactory, fileManager, configuration.getPackageConfigs())) {
for (PackageConfig pkg : loader.loadPackages(bundle, bundleContext, objectFactory, fileManagerFactory, configuration.getPackageConfigs())) {
configuration.addPackageConfig(pkg.getName(), pkg);
bundleAccessor.addPackageFromBundle(bundle, pkg.getName());
}

//Convention
//get the existing packages before reloading the provider (se we can figure out what are the new packages)
Set<String> packagesBeforeLoading = new HashSet(configuration.getPackageConfigNames());
Set<String> packagesBeforeLoading = new HashSet<String>(configuration.getPackageConfigNames());

PackageProvider conventionPackageProvider = configuration.getContainer().getInstance(PackageProvider.class, "convention.packageProvider");
if (conventionPackageProvider != null) {
Expand All @@ -149,7 +154,7 @@ protected void loadConfigFromBundle(Bundle bundle) {
conventionPackageProvider.loadPackages();
}

Set<String> packagesAfterLoading = new HashSet(configuration.getPackageConfigNames());
Set<String> packagesAfterLoading = new HashSet<String>(configuration.getPackageConfigNames());
packagesAfterLoading.removeAll(packagesBeforeLoading);
if (!packagesAfterLoading.isEmpty()) {
//add the new packages to the map of bundle -> package
Expand All @@ -172,7 +177,7 @@ protected void loadConfigFromBundle(Bundle bundle) {
* Checks for "Struts2-Enabled" header in the bundle
*/
protected boolean shouldProcessBundle(Bundle bundle) {
String strutsEnabled = (String) bundle.getHeaders().get(OsgiHost.OSGI_HEADER_STRUTS_ENABLED);
String strutsEnabled = bundle.getHeaders().get(OsgiHost.OSGI_HEADER_STRUTS_ENABLED);

return "true".equalsIgnoreCase(strutsEnabled);
}
Expand Down Expand Up @@ -205,16 +210,18 @@ public void setServletContext(ServletContext servletContext) {
this.servletContext = servletContext;
}

@Inject
public void setFileManagerFactory(FileManagerFactory fmFactory) {
this.fileManager = fmFactory.getFileManager();
this.fileManagerFactory = fmFactory;
}

public void destroy() {
try {
osgiHost.destroy();
} catch (Exception e) {
if (LOG.isErrorEnabled())
if (LOG.isErrorEnabled()) {
LOG.error("Failed to stop OSGi container", e);
}
}
}

Expand Down Expand Up @@ -246,10 +253,13 @@ public void bundleChanged(BundleEvent bundleEvent) {
protected void onBundleStopped(Bundle bundle) {
Set<String> packages = bundleAccessor.getPackagesByBundle(bundle);
if (!packages.isEmpty()) {
if (LOG.isTraceEnabled())
if (LOG.isTraceEnabled()) {
LOG.trace("The bundle [#0] has been stopped. The packages [#1] will be disabled", bundle.getSymbolicName(), StringUtils.join(packages, ","));
for (String packageName : packages)
}
for (String packageName : packages) {
configuration.removePackageConfig(packageName);
}
}
}

}
Loading

0 comments on commit 1c5b931

Please sign in to comment.