Author: musachy
Date: Mon May 5 07:52:02 2008
New Revision: 653496
URL: http://svn.apache.org/viewvc?rev=653496&view=rev
Log:
* Add static resource loader
* Add service lookup on bundle accessor singleton
Added:
struts/sandbox/trunk/struts2-osgi-plugin/plugin/src/main/java/org/apache/struts2/osgi/loaders/StaticContentBundleResourceLoader.java
Modified:
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/OsgiConfigurationProvider.java
struts/sandbox/trunk/struts2-osgi-plugin/plugin/src/main/resources/struts-plugin.xml
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=653496&r1=653495&r2=653496&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
Mon May 5 07:52:02 2008
@@ -8,6 +8,8 @@
import org.osgi.framework.Bundle;
import org.osgi.framework.BundleContext;
+import org.osgi.framework.InvalidSyntaxException;
+import org.osgi.framework.ServiceReference;
public interface BundleAccessor {
@@ -25,4 +27,8 @@
Set<String> getPackagesByBundle(Bundle bundle);
+ Object getService(ServiceReference ref);
+
+ ServiceReference getServiceReference(String className);
+
}
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=653496&r1=653495&r2=653496&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
Mon May 5 07:52:02 2008
@@ -8,6 +8,8 @@
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;
@@ -34,7 +36,15 @@
public static DefaultBundleAccessor getInstance() {
return self;
}
-
+
+ public Object getService(ServiceReference ref) {
+ return bundleContext.getService(ref);
+ }
+
+ public ServiceReference getServiceReference(String className) {
+ return bundleContext.getServiceReference(className);
+ }
+
public void init(Map<String,Bundle> bundles, BundleContext bundleContext,
Map<String, String> packageToBundle) {
this.bundles = Collections.unmodifiableMap(bundles);
this.bundleContext = bundleContext;
@@ -107,6 +117,25 @@
return null;
}
+ public URL loadResourceFromAllBundles(String name) throws IOException {
+ for (Map.Entry<String, Bundle> entry : bundles.entrySet()) {
+ Enumeration e = entry.getValue().getResources(name);
+ if (e.hasMoreElements()) {
+ return (URL) e.nextElement();
+ }
+ }
+
+ return null;
+ }
+
+ public InputStream loadResourceFromAllBundlesAsStream(String name) throws
IOException {
+ URL url = loadResourceFromAllBundles(name);
+ if (url != null) {
+ return url.openStream();
+ }
+ return null;
+ }
+
public URL loadResource(String name) {
Bundle bundle = getCurrentBundle();
if (bundle != null) {
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=653496&r1=653495&r2=653496&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
Mon May 5 07:52:02 2008
@@ -7,6 +7,8 @@
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLDecoder;
+import java.security.CodeSource;
+import java.security.ProtectionDomain;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Enumeration;
@@ -26,9 +28,11 @@
import org.apache.felix.framework.util.FelixConstants;
import org.apache.felix.framework.util.StringMap;
import org.apache.felix.main.AutoActivator;
+import org.apache.felix.shell.ShellService;
import org.apache.struts2.osgi.loaders.VelocityBundleResourceLoader;
import org.apache.struts2.views.velocity.VelocityManager;
import org.apache.velocity.app.Velocity;
+import org.apache.velocity.util.StringUtils;
import org.osgi.framework.Bundle;
import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;
@@ -56,6 +60,15 @@
private static final Logger LOG =
LoggerFactory.getLogger(OsgiConfigurationProvider.class);
+ private static final String SYSTEM_PACKAGES = "org.osgi.framework;
version=1.4.0,"
+ + "org.osgi.service.packageadmin; version=1.2.0," +
"org.osgi.service.startlevel; version=1.0.0,"
+ + "org.osgi.service.url; version=1.0.0," +
"org.apache.struts2.osgi; version=1.0.0,"
+ + "org.apache.struts2.dispatcher; version=1.0.0," +
"com.opensymphony.xwork2.config; version=1.0.0,"
+ + "com.opensymphony.xwork2.config.entities; version=1.0.0,"
+ + "com.opensymphony.xwork2.inject; version=1.0.0," +
"com.opensymphony.xwork2; version=1.0.0";
+
+ private static final String AUTO_START_BUNDLES =
"file:bundles/org.apache.felix.shell-1.1.0-SNAPSHOT.jar ";
+
private Felix felix;
private Map<String,Bundle> bundles = Collections.synchronizedMap(new
HashMap<String,Bundle>());
private Configuration configuration;
@@ -74,7 +87,7 @@
public void setObjectFactory(ObjectFactory factory) {
this.objectFactory = factory;
}
-
+
@Inject
public void setVelocityManager(VelocityManager vm) {
Properties props = new Properties();
@@ -83,16 +96,16 @@
props.setProperty(Velocity.RESOURCE_LOADER,
"strutsfile,strutsclass,osgi");
vm.setVelocityProperties(props);
}
-
+
public void destroy() {
try {
felix.stop();
} catch (BundleException e) {
LOG.error("Failed to stop Felix", e);
}
- bundles = null;
+ bundles = null;
}
-
+
public void init(Configuration configuration) throws
ConfigurationException {
loadOsgi();
this.configuration = configuration;
@@ -131,37 +144,28 @@
protected void loadOsgi() {
//configuration properties
Properties systemProperties = getProperties("default.properties");
-
+
//struts specific properties
Properties strutsProperties = getProperties("struts-osgi.properties");
-
+
Map configMap = new StringMap(false);
-
+
configMap.put(Constants.FRAMEWORK_SYSTEMPACKAGES,
- "org.osgi.framework; version=1.4.0," +
- "org.osgi.service.packageadmin; version=1.2.0," +
- "org.osgi.service.startlevel; version=1.0.0," +
- "org.osgi.service.url; version=1.0.0," +
- "org.apache.struts2.osgi; version=1.0.0," +
- "org.apache.struts2.dispatcher; version=1.0.0," +
- "com.opensymphony.xwork2.config; version=1.0.0," +
- "com.opensymphony.xwork2.config.entities; version=1.0.0," +
- "com.opensymphony.xwork2.inject; version=1.0.0," +
- "com.opensymphony.xwork2; version=1.0.0" +
- getSystemPackages(systemProperties) +
+ SYSTEM_PACKAGES +
+ getSystemPackages(systemProperties) +
strutsProperties.getProperty("xwork"));
Set<String> bundlePaths = new
HashSet<String>(findInPackage("bundles"));
LOG.info("Loading Struts bundles "+bundlePaths);
-
+
StringBuilder sb = new StringBuilder();
for (String path : bundlePaths) {
sb.append(path).append(" ");
}
-
+
configMap.put(AutoActivator.AUTO_START_PROP + ".1",
- sb.toString());
- configMap.put(BundleCache.CACHE_PROFILE_DIR_PROP,
System.getProperty("java.io.tmpdir"));
+ sb.toString() + getJarUrl(ShellService.class));
+ configMap.put(BundleCache.CACHE_PROFILE_DIR_PROP,
System.getProperty("java.io.tmpdir") + ".felix-cache");
configMap.put(BundleCache.CACHE_DIR_PROP, "jim");
configMap.put(FelixConstants.EMBEDDED_EXECUTION_PROP, "true");
configMap.put(FelixConstants.SERVICE_URLHANDLERS_PROP, "false");
@@ -182,7 +186,7 @@
catch (Exception ex) {
throw new ConfigurationException("Couldn't start Felix (OSGi)",
ex);
}
-
+
// Wait for all bundles to load
while (bundles.size() < bundlePaths.size()) {
try {
@@ -192,12 +196,19 @@
}
}
}
-
+
+ private String getJarUrl(Class clazz) {
+ ProtectionDomain protectionDomain = clazz.getProtectionDomain();
+ CodeSource codeSource = protectionDomain.getCodeSource();
+ URL loc = codeSource.getLocation();
+ return loc.toString();
+ }
+
private String getSystemPackages(Properties properties) {
String jreVersion = "jre-" +
System.getProperty("java.version").substring(0, 3);
return properties.getProperty(jreVersion);
}
-
+
private Properties getProperties(String fileName) {
URL propertiesURL =
OsgiConfigurationProvider.class.getClassLoader().getResource(
fileName);
@@ -218,7 +229,7 @@
}
return properties;
}
-
+
class BundleRegistration implements BundleActivator, BundleListener {
public void start(BundleContext context) throws Exception {
@@ -238,7 +249,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
Added:
struts/sandbox/trunk/struts2-osgi-plugin/plugin/src/main/java/org/apache/struts2/osgi/loaders/StaticContentBundleResourceLoader.java
URL:
http://svn.apache.org/viewvc/struts/sandbox/trunk/struts2-osgi-plugin/plugin/src/main/java/org/apache/struts2/osgi/loaders/StaticContentBundleResourceLoader.java?rev=653496&view=auto
==============================================================================
---
struts/sandbox/trunk/struts2-osgi-plugin/plugin/src/main/java/org/apache/struts2/osgi/loaders/StaticContentBundleResourceLoader.java
(added)
+++
struts/sandbox/trunk/struts2-osgi-plugin/plugin/src/main/java/org/apache/struts2/osgi/loaders/StaticContentBundleResourceLoader.java
Mon May 5 07:52:02 2008
@@ -0,0 +1,18 @@
+package org.apache.struts2.osgi.loaders;
+
+import java.io.IOException;
+import java.io.InputStream;
+
+import org.apache.struts2.dispatcher.DefaultStaticContentLoader;
+import org.apache.struts2.osgi.DefaultBundleAccessor;
+
+/**
+ * Loads static resources from bundles
+ *
+ */
+public class StaticContentBundleResourceLoader extends
DefaultStaticContentLoader {
+ @Override
+ protected InputStream findInputStream(String path) throws IOException {
+ return
DefaultBundleAccessor.getInstance().loadResourceFromAllBundlesAsStream(path);
+ }
+}
Modified:
struts/sandbox/trunk/struts2-osgi-plugin/plugin/src/main/resources/struts-plugin.xml
URL:
http://svn.apache.org/viewvc/struts/sandbox/trunk/struts2-osgi-plugin/plugin/src/main/resources/struts-plugin.xml?rev=653496&r1=653495&r2=653496&view=diff
==============================================================================
---
struts/sandbox/trunk/struts2-osgi-plugin/plugin/src/main/resources/struts-plugin.xml
(original)
+++
struts/sandbox/trunk/struts2-osgi-plugin/plugin/src/main/resources/struts-plugin.xml
Mon May 5 07:52:02 2008
@@ -12,5 +12,6 @@
<constant name="struts.objectFactory" value="osgi" />
<constant name="struts.objectFactory.delegate" value="struts" />
- <constant name="struts.freemarker.manager.classname"
value="org.apache.struts2.osgi.BundleFreemarkerManager" />
+ <constant name="struts.freemarker.manager.classname"
value="org.apache.struts2.osgi.BundleFreemarkerManager" />
+ <constant name="struts.staticContentLoader"
value="org.apache.struts2.osgi.loaders.StaticContentBundleResourceLoader" />
</struts>