From: <to...@us...> - 2007-03-03 01:08:05
|
Revision: 95 http://techne-dev.svn.sourceforge.net/techne-dev/?rev=95&view=rev Author: tonit Date: 2007-03-02 17:08:05 -0800 (Fri, 02 Mar 2007) Log Message: ----------- Initial import. Added Paths: ----------- sandbox/tonit/techne.extender/.classpath sandbox/tonit/techne.extender/.project sandbox/tonit/techne.extender/pom.xml sandbox/tonit/techne.extender/src/ sandbox/tonit/techne.extender/src/main/ sandbox/tonit/techne.extender/src/main/java/ sandbox/tonit/techne.extender/src/main/java/techne/ sandbox/tonit/techne.extender/src/main/java/techne/extender/ sandbox/tonit/techne.extender/src/main/java/techne/extender/Activator.java sandbox/tonit/techne.extender/src/main/java/techne/extender/Registry.java sandbox/tonit/techne.extender/src/test/ Added: sandbox/tonit/techne.extender/.classpath =================================================================== --- sandbox/tonit/techne.extender/.classpath (rev 0) +++ sandbox/tonit/techne.extender/.classpath 2007-03-03 01:08:05 UTC (rev 95) @@ -0,0 +1,10 @@ +<classpath> + <classpathentry kind="src" path="src/main/java"/> + <classpathentry kind="output" path="target/classes"/> + <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/> + <classpathentry kind="var" path="M2_REPO/org/apache/felix/javax.servlet/0.9.0-incubator-SNAPSHOT/javax.servlet-0.9.0-incubator-SNAPSHOT.jar"/> + <classpathentry kind="var" path="M2_REPO/javax/servlet/servlet-api/2.3/servlet-api-2.3.jar"/> + <classpathentry kind="var" path="M2_REPO/org/apache/felix/org.osgi.foundation/0.9.0-incubator-SNAPSHOT/org.osgi.foundation-0.9.0-incubator-SNAPSHOT.jar"/> + <classpathentry kind="var" path="M2_REPO/org/apache/felix/org.osgi.core/0.9.0-incubator-SNAPSHOT/org.osgi.core-0.9.0-incubator-SNAPSHOT.jar"/> + <classpathentry kind="var" path="M2_REPO/org/apache/felix/org.osgi.compendium/0.9.0-incubator-SNAPSHOT/org.osgi.compendium-0.9.0-incubator-SNAPSHOT.jar"/> +</classpath> \ No newline at end of file Added: sandbox/tonit/techne.extender/.project =================================================================== --- sandbox/tonit/techne.extender/.project (rev 0) +++ sandbox/tonit/techne.extender/.project 2007-03-03 01:08:05 UTC (rev 95) @@ -0,0 +1,13 @@ +<projectDescription> + <name>techne.extender</name> + <comment/> + <projects/> + <buildSpec> + <buildCommand> + <name>org.eclipse.jdt.core.javabuilder</name> + </buildCommand> + </buildSpec> + <natures> + <nature>org.eclipse.jdt.core.javanature</nature> + </natures> +</projectDescription> \ No newline at end of file Added: sandbox/tonit/techne.extender/pom.xml =================================================================== --- sandbox/tonit/techne.extender/pom.xml (rev 0) +++ sandbox/tonit/techne.extender/pom.xml 2007-03-03 01:08:05 UTC (rev 95) @@ -0,0 +1,38 @@ +<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> + <modelVersion>4.0.0</modelVersion> + <groupId>techne</groupId> + <artifactId>techne.extender</artifactId> + <packaging>bundle</packaging> + <version>1.0</version> + <name>techne.extender</name> + <url>http://maven.apache.org</url> + <dependencies> + <dependency> + <groupId>org.apache.felix</groupId> + <artifactId>org.osgi.core</artifactId> + <version>0.9.0-incubator-SNAPSHOT</version> + </dependency> + <dependency> + <groupId>org.apache.felix</groupId> + <artifactId>org.osgi.compendium</artifactId> + <version>0.9.0-incubator-SNAPSHOT</version> + </dependency> + </dependencies> + <build> + <plugins> + <plugin> + <groupId>org.apache.felix</groupId> + <artifactId>maven-bundle-plugin</artifactId> + <extensions>true</extensions> + <configuration> + <instructions> + <Bundle-SymbolicName>techne.extender</Bundle-SymbolicName> + <Private-Package>techne.extender</Private-Package> + <Service-Component>techne.extender.Activator;log=org.osgi.service.log.LogService;http=org.osgi.service.http.HttpService</Service-Component> + </instructions> + </configuration> + </plugin> + </plugins> + </build> +</project> Added: sandbox/tonit/techne.extender/src/main/java/techne/extender/Activator.java =================================================================== --- sandbox/tonit/techne.extender/src/main/java/techne/extender/Activator.java (rev 0) +++ sandbox/tonit/techne.extender/src/main/java/techne/extender/Activator.java 2007-03-03 01:08:05 UTC (rev 95) @@ -0,0 +1,50 @@ +package techne.extender; + +import org.osgi.framework.Bundle; +import org.osgi.framework.BundleContext; +import org.osgi.framework.BundleEvent; +import org.osgi.framework.BundleListener; +import org.osgi.service.component.ComponentContext; +import org.osgi.service.http.HttpService; +import org.osgi.service.log.LogService; + +public class Activator implements BundleListener { + BundleContext context; + Registry registry = new Registry(); + + protected void activate(ComponentContext cc) { + this.context = cc.getBundleContext(); + context.addBundleListener(this); + Bundle bundles[] = context.getBundles(); + for (int i = 0; i < bundles.length; i++) { + if ((bundles[i].getState() & (Bundle.STARTING | Bundle.ACTIVE)) != 0) + registry.register(bundles[i]); + } + } + + protected void deactivate(ComponentContext context) throws Exception { + this.context.removeBundleListener(this); + registry.close(); + } + + public void bundleChanged(BundleEvent event) { + switch (event.getType()) { + case BundleEvent.STARTED: + registry.register(event.getBundle()); + break; + + case BundleEvent.STOPPED: + registry.unregister(event.getBundle()); + break; + } + } + + public void setHttp(HttpService http) { + registry.http = http; + } + + public void setLog(LogService log) { + registry.log = log; + } + +} \ No newline at end of file Added: sandbox/tonit/techne.extender/src/main/java/techne/extender/Registry.java =================================================================== --- sandbox/tonit/techne.extender/src/main/java/techne/extender/Registry.java (rev 0) +++ sandbox/tonit/techne.extender/src/main/java/techne/extender/Registry.java 2007-03-03 01:08:05 UTC (rev 95) @@ -0,0 +1,94 @@ +package techne.extender; + +import java.util.HashMap; +import java.util.HashSet; +import java.util.Iterator; +import java.util.Map; +import java.util.Set; + +import javax.servlet.http.HttpServlet; + +import org.osgi.framework.Bundle; +import org.osgi.service.http.HttpService; +import org.osgi.service.log.LogService; + +public class Registry { + Set activated = new HashSet(); + HttpService http; + LogService log; + + public void register(Bundle bundle) { + synchronized (activated) { + if (activated.contains(bundle)) return; + } + Map clauses = getClauses(bundle); + + for (Iterator e = clauses.entrySet().iterator(); e + .hasNext();) { + Map.Entry entry = (Map.Entry) e.next(); + try { + String alias = (String) entry.getKey(); + String className = (String) entry.getValue(); + registerServlet(bundle, alias, className); + } catch (Throwable t) { + log.log(LogService.LOG_ERROR, + "[extender] Activating servlet from " + + bundle.getLocation(), t); + } + } + synchronized (activated) { + activated.add(bundle); + } + } + + void registerServlet(Bundle bundle, String alias, + String className) throws Exception { + if (!alias.startsWith("/")) throw new IllegalArgumentException( + "Alias must start with / : " + alias); + + Class clazz = bundle.loadClass(className); + if (clazz != null) { + HttpServlet servlet = (HttpServlet) clazz + .newInstance(); + http.registerServlet(alias, servlet, null, null); + } else throw new IllegalArgumentException( + "Can not find class " + className); + } + + public void unregister(Bundle bundle) { + synchronized (activated) { + if (!activated.contains(bundle)) return; + activated.remove(bundle); + } + + Map clauses = getClauses(bundle); + for (Iterator i = clauses.keySet().iterator(); i + .hasNext();) { + String alias = (String) i.next(); + http.unregister(alias); + } + } + + Map getClauses(Bundle bundle) { + Map map = new HashMap(); + String header = (String) bundle.getHeaders().get( + "ServletMap"); + if (header != null) { + String clauses[] = header.split(","); + for (int i = 0; i < clauses.length; i++) { + String parts[] = clauses[i].trim().split( + "\\s*=\\s*"); + if (parts.length == 2) map.put(parts[0], parts[1]); + } + } + return map; + } + + void close() { + for (Iterator i = activated.iterator(); i.hasNext();) { + Bundle bundle = (Bundle) i.next(); + unregister(bundle); + } + } + } + This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |