You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@struts.apache.org by mr...@apache.org on 2008/02/25 11:04:26 UTC
svn commit: r630785 - in
/struts/sandbox/trunk/struts2-osgi-plugin/src/main/java/org/apache/struts2/osgi:
BundleAccessor.java BundlePackageLoader.java DefaultBundleAccessor.java
OsgiConfigurationProvider.java
Author: mrdon
Date: Mon Feb 25 02:04:17 2008
New Revision: 630785
URL: http://svn.apache.org/viewvc?rev=630785&view=rev
Log:
Adding code to ensure classes and resources are only resolved for the current bundle
Modified:
struts/sandbox/trunk/struts2-osgi-plugin/src/main/java/org/apache/struts2/osgi/BundleAccessor.java
struts/sandbox/trunk/struts2-osgi-plugin/src/main/java/org/apache/struts2/osgi/BundlePackageLoader.java
struts/sandbox/trunk/struts2-osgi-plugin/src/main/java/org/apache/struts2/osgi/DefaultBundleAccessor.java
struts/sandbox/trunk/struts2-osgi-plugin/src/main/java/org/apache/struts2/osgi/OsgiConfigurationProvider.java
Modified: struts/sandbox/trunk/struts2-osgi-plugin/src/main/java/org/apache/struts2/osgi/BundleAccessor.java
URL: http://svn.apache.org/viewvc/struts/sandbox/trunk/struts2-osgi-plugin/src/main/java/org/apache/struts2/osgi/BundleAccessor.java?rev=630785&r1=630784&r2=630785&view=diff
==============================================================================
--- struts/sandbox/trunk/struts2-osgi-plugin/src/main/java/org/apache/struts2/osgi/BundleAccessor.java (original)
+++ struts/sandbox/trunk/struts2-osgi-plugin/src/main/java/org/apache/struts2/osgi/BundleAccessor.java Mon Feb 25 02:04:17 2008
@@ -10,6 +10,8 @@
public interface BundleAccessor {
+ String CURRENT_BUNDLE_NAME = "__bundle_name__";
+
void setBundles(Map<String, Bundle> bundles);
void setBundleContext(BundleContext bundleContext);
@@ -20,4 +22,5 @@
URL loadResource(String name);
+ void setPackageToBundleMapping(Map<String, String> packageToBundle);
}
Modified: struts/sandbox/trunk/struts2-osgi-plugin/src/main/java/org/apache/struts2/osgi/BundlePackageLoader.java
URL: http://svn.apache.org/viewvc/struts/sandbox/trunk/struts2-osgi-plugin/src/main/java/org/apache/struts2/osgi/BundlePackageLoader.java?rev=630785&r1=630784&r2=630785&view=diff
==============================================================================
--- struts/sandbox/trunk/struts2-osgi-plugin/src/main/java/org/apache/struts2/osgi/BundlePackageLoader.java (original)
+++ struts/sandbox/trunk/struts2-osgi-plugin/src/main/java/org/apache/struts2/osgi/BundlePackageLoader.java Mon Feb 25 02:04:17 2008
@@ -2,16 +2,13 @@
import java.io.IOException;
import java.net.URL;
-import java.util.ArrayList;
-import java.util.Enumeration;
-import java.util.Iterator;
-import java.util.List;
-import java.util.Map;
+import java.util.*;
import org.osgi.framework.Bundle;
import org.osgi.framework.BundleContext;
import com.opensymphony.xwork2.ObjectFactory;
+import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.config.Configuration;
import com.opensymphony.xwork2.config.ConfigurationException;
import com.opensymphony.xwork2.config.entities.PackageConfig;
@@ -25,14 +22,27 @@
private static final Logger LOG = LoggerFactory.getLogger(BundlePackageLoader.class);
public List<PackageConfig> loadPackages(Bundle bundle, BundleContext bundleContext, ObjectFactory objectFactory, Map<String,PackageConfig> pkgConfigs) throws ConfigurationException {
- BundleConfigurationProvider prov = new BundleConfigurationProvider("struts.xml", bundle, bundleContext);
Configuration config = new DefaultConfiguration("struts.xml");
- for (PackageConfig pkg : pkgConfigs.values()) {
- config.addPackageConfig(pkg.getName(), pkg);
+ ActionContext ctx = ActionContext.getContext();
+ if (ctx == null) {
+ ctx = new ActionContext(new HashMap());
+ ActionContext.setContext(ctx);
+ }
+
+ try {
+ // Ensure all requested classes and resources will be resolved using the current bundle
+ ctx.put(BundleAccessor.CURRENT_BUNDLE_NAME, bundle.getSymbolicName());
+
+ BundleConfigurationProvider prov = new BundleConfigurationProvider("struts.xml", bundle, bundleContext);
+ for (PackageConfig pkg : pkgConfigs.values()) {
+ config.addPackageConfig(pkg.getName(), pkg);
+ }
+ prov.setObjectFactory(objectFactory);
+ prov.init(config);
+ prov.loadPackages();
+ } finally {
+ ctx.put(BundleAccessor.CURRENT_BUNDLE_NAME, null);
}
- prov.setObjectFactory(objectFactory);
- prov.init(config);
- prov.loadPackages();
return new ArrayList<PackageConfig>(config.getPackageConfigs().values());
}
Modified: struts/sandbox/trunk/struts2-osgi-plugin/src/main/java/org/apache/struts2/osgi/DefaultBundleAccessor.java
URL: http://svn.apache.org/viewvc/struts/sandbox/trunk/struts2-osgi-plugin/src/main/java/org/apache/struts2/osgi/DefaultBundleAccessor.java?rev=630785&r1=630784&r2=630785&view=diff
==============================================================================
--- struts/sandbox/trunk/struts2-osgi-plugin/src/main/java/org/apache/struts2/osgi/DefaultBundleAccessor.java (original)
+++ struts/sandbox/trunk/struts2-osgi-plugin/src/main/java/org/apache/struts2/osgi/DefaultBundleAccessor.java Mon Feb 25 02:04:17 2008
@@ -15,6 +15,8 @@
import com.opensymphony.xwork2.util.logging.Logger;
import com.opensymphony.xwork2.util.logging.LoggerFactory;
+import com.opensymphony.xwork2.ActionContext;
+import com.opensymphony.xwork2.config.entities.ActionConfig;
public class DefaultBundleAccessor implements BundleAccessor {
@@ -22,9 +24,9 @@
private static final Logger LOG = LoggerFactory.getLogger(DefaultBundleAccessor.class);
private Map<String, Bundle> bundles = new HashMap<String, Bundle>();
- private Map<String,String> classToBundle = new HashMap<String,String>();
private BundleContext bundleContext;
-
+ private Map<String, String> packageToBundle;
+
public DefaultBundleAccessor() {
self = this;
}
@@ -36,35 +38,22 @@
public void setBundles(Map<String,Bundle> bundles) {
this.bundles = bundles;
- classToBundle.clear();
}
- @Override
public void setBundleContext(BundleContext bundleContext) {
this.bundleContext = bundleContext;
}
public Class<?> loadClass(String className) throws ClassNotFoundException {
Class cls = null;
- if (classToBundle.containsKey(className)) {
- bundles.get(classToBundle.get(className)).loadClass(className);
- } else {
- for (Entry<String,Bundle> entry : bundles.entrySet()) {
- try {
- cls = entry.getValue().loadClass(className);
- if (cls != null) {
- classToBundle.put(className, entry.getKey());
- }
- } catch (ClassNotFoundException ex) {
- if (LOG.isDebugEnabled()) {
- LOG.debug("class #1 not found in bundle #2", className, entry.getValue().getSymbolicName());
- }
- }
- }
+ Bundle bundle = getCurrentBundle();
+ if (bundle != null) {
+ bundle.loadClass(className);
+ LOG.debug("Located class #1 in bundle #2", className, bundle.getSymbolicName());
}
-
+
if (cls == null) {
- //try to find a bean with that id
+ //try to find a bean with that id (hack for spring that searches all bundles)
try {
Object bean = SpringOSGiUtil.getBean(bundleContext, className);
if (bean != null)
@@ -80,31 +69,49 @@
}
return cls;
}
-
+
+ private Bundle getCurrentBundle() {
+ ActionContext ctx = ActionContext.getContext();
+ String bundleName = (String) ctx.get(CURRENT_BUNDLE_NAME);
+ if (bundleName != null) {
+ ActionConfig actionConfig = ctx.getActionInvocation().getProxy().getConfig();
+ bundleName = packageToBundle.get(actionConfig.getPackageName());
+ }
+ if (bundleName != null) {
+ return bundles.get(bundleName);
+ }
+ return null;
+ }
+
public List<URL> loadResources(String name) throws IOException {
- List<URL> resources = new ArrayList<URL>();
- for (Entry<String,Bundle> entry : bundles.entrySet()) {
- Enumeration e = entry.getValue().getResources(name);
+ Bundle bundle = getCurrentBundle();
+ if (bundle != null) {
+ List<URL> resources = new ArrayList<URL>();
+ Enumeration e = bundle.getResources(name);
while (e.hasMoreElements()) {
resources.add((URL) e.nextElement());
}
+ return resources;
}
- return resources;
+
+ return null;
}
public URL loadResource(String name) {
- URL url = null;
- for (Entry<String,Bundle> entry : bundles.entrySet()) {
- url = entry.getValue().getResource(name);
- if (url != null)
- break;
+ Bundle bundle = getCurrentBundle();
+ if (bundle != null) {
+ return bundle.getResource(name);
}
- return url;
+ return null;
+ }
+
+ public void setPackageToBundleMapping(Map<String, String> packageToBundle) {
+ this.packageToBundle = packageToBundle;
}
public InputStream loadResourceAsStream(String name) throws IOException {
URL url = loadResource(name);
- if (url != null) {
+ if (url != null) {
return url.openStream();
}
return null;
Modified: struts/sandbox/trunk/struts2-osgi-plugin/src/main/java/org/apache/struts2/osgi/OsgiConfigurationProvider.java
URL: http://svn.apache.org/viewvc/struts/sandbox/trunk/struts2-osgi-plugin/src/main/java/org/apache/struts2/osgi/OsgiConfigurationProvider.java?rev=630785&r1=630784&r2=630785&view=diff
==============================================================================
--- struts/sandbox/trunk/struts2-osgi-plugin/src/main/java/org/apache/struts2/osgi/OsgiConfigurationProvider.java (original)
+++ struts/sandbox/trunk/struts2-osgi-plugin/src/main/java/org/apache/struts2/osgi/OsgiConfigurationProvider.java Mon Feb 25 02:04:17 2008
@@ -58,7 +58,7 @@
private Map<String,Bundle> bundles = Collections.synchronizedMap(new HashMap<String,Bundle>());
private Configuration configuration;
private BundleContext bundleContext;
- private ServletContext servletContext;
+ private BundleAccessor bundleAccessor;
private boolean bundlesChanged = false;
private ObjectFactory objectFactory;
@@ -67,11 +67,7 @@
public void setBundleAccessor(BundleAccessor acc) {
acc.setBundles(bundles);
acc.setBundleContext(bundleContext);
- }
-
- @Inject
- public void setServletContext(ServletContext ctx) {
- this.servletContext = ctx;
+ this.bundleAccessor = acc;
}
@Inject
@@ -109,6 +105,7 @@
} catch (InvalidSyntaxException e) {
throw new ConfigurationException(e);
}
+ Map<String,String> packageToBundle = new HashMap<String,String>();
Set bundleNames = new HashSet();
if (refs != null) {
for (ServiceReference ref : refs) {
@@ -118,10 +115,12 @@
PackageLoader loader = (PackageLoader) bundleContext.getService(ref);
for (PackageConfig pkg : loader.loadPackages(ref.getBundle(), bundleContext, objectFactory, configuration.getPackageConfigs())) {
configuration.addPackageConfig(pkg.getName(), pkg);
+ packageToBundle.put(pkg.getName(), ref.getBundle().getSymbolicName());
}
}
}
}
+ bundleAccessor.setPackageToBundleMapping(packageToBundle);
bundlesChanged = false;
}
@@ -158,8 +157,8 @@
sb.append(path).append(" ");
}
- configMap.put(FelixConstants.AUTO_START_PROP + ".1",
- sb.toString());
+ //configMap.put(FelixConstants.AUTO_START_PROP + ".1",
+ // sb.toString());
configMap.put(BundleCache.CACHE_PROFILE_DIR_PROP, System.getProperty("java.io.tmpdir"));
configMap.put(BundleCache.CACHE_DIR_PROP, "jim");
configMap.put(FelixConstants.EMBEDDED_EXECUTION_PROP, "true");
@@ -239,9 +238,7 @@
* Scans for classes starting at the package provided and descending into subpackages.
* Each class is offered up to the Test as it is discovered, and if the Test returns
* true the class is retained. Accumulated classes can be fetched by calling
- * {@link #getClasses()}.
*
- * @param test an instance of {@link Test} that will be used to filter classes
* @param packageName the name of the package from which to start scanning for
* classes, e.g. {@code net.sourceforge.stripes}
*/
@@ -296,7 +293,6 @@
* the file is loaded and tested to see if it is acceptable according to the Test. Operates
* recursively to find classes within a folder structure matching the package structure.
*
- * @param test a Test used to filter the classes that are discovered
* @param parent the package name up to this directory in the package hierarchy. E.g. if
* /classes is in the classpath and we wish to examine files in /classes/org/apache then
* the values of <i>parent</i> would be <i>org/apache</i>
@@ -329,7 +325,6 @@
* matching the package structure. If the File is not a JarFile or does not exist a warning
* will be logged, but no error will be raised.
*
- * @param test a Test used to filter the classes that are discovered
* @param parent the parent package under which classes must be in order to be considered
* @param jarfile the jar file to be examined for classes
*/