Skip to content

Commit

Permalink
Added uxloader into default build. Solved issues with services not being
Browse files Browse the repository at this point in the history
available. Will refactor these out into a common module some time soon
(at least before I write another component, ;)).
  • Loading branch information
danatcaret committed Apr 6, 2009
1 parent 241bbfe commit 5aa684e
Show file tree
Hide file tree
Showing 10 changed files with 138 additions and 21 deletions.
5 changes: 5 additions & 0 deletions slingtests/osgikernel/app-bundles/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,11 @@
<artifactId>org.sakaiproject.kernel2.osgi.db</artifactId>
<version>0.1-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.sakaiproject.kernel2.osgi</groupId>
<artifactId>org.sakaiproject.kernel2.osgi.uxloader</artifactId>
<version>0.1-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.sakaiproject.kernel2.osgi</groupId>
<artifactId>org.sakaiproject.kernel2.osgi.utils</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,13 @@
import java.util.Map;
import java.util.Properties;

/* XXX PROBLEM. I've renamed this the StubConfigurationServiceImpl from KernelConstants. It does contain
* some kernel constants, but also is a stub configuration service. Really, the kernel constants should
* be moved into a separate file, for things like annotations, etc, to use. However, I'm not sure I
* understand what the scr plugin is doing here enough, yet, to do that refactor. This is something which
* is high on my todo-list. -- dan
*/

/**
* Holds the configuration properties that are used in the kernel. This is an OSGi Managed
* service that collects together all the configuation properties.
Expand Down
5 changes: 1 addition & 4 deletions slingtests/osgikernel/bundles/uxloader/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,8 @@
<instructions>
<Bundle-Category> sakaiux </Bundle-Category>
<Bundle-Activator> org.sakaiproject.kernel2.uxloader.Activator </Bundle-Activator>
<Export-Package>
<!-- Nothing to Export -->
</Export-Package>
<Export-Package>org.sakaiproject.kernel2.uxloader</Export-Package>
<Import-Package> * </Import-Package>
<Private-Package>org.sakaiproject.kernel2.uxloader</Private-Package>
<Embed-Transitive>true</Embed-Transitive>
<Embed-Dependency>
</Embed-Dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@ public class ActivationProcess {

private HttpService http;
private HttpContext context;
private CacheManagerService cache;
private ServiceResolver resolver;

public ActivationProcess(HttpService http,CacheManagerService cache) {
public ActivationProcess(ServiceResolver resolver,HttpService http) {
this.http=http;
this.context=http.createDefaultHttpContext();
this.cache=cache;
this.resolver=resolver;
}

public void map(String url,String filesystem) throws ServletException, NamespaceException {
Expand All @@ -32,6 +32,6 @@ public void map(String url,String filesystem) throws ServletException, Namespace
uxLoaderParams.put(FileServlet.BASE_FILE,filesystem);
uxLoaderParams.put(FileServlet.MAX_CACHE_SIZE, "102400");
uxLoaderParams.put(FileServlet.WELCOME_FILE, "index.html");
http.registerServlet(url, new FileServlet(cache),uxLoaderParams,context);
http.registerServlet(url,new FileServlet(resolver),uxLoaderParams,context);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,12 @@
*/

public class Activator extends SimpleBaseActivator implements BundleActivator {

public Activator() {
registerStartupService(ConfigurationService.class);
registerStartupService(HttpService.class);
}

private URLFileMapping[] createMapping(String raw) {
logger.info("raw ux config is "+raw);
List<URLFileMapping> out=new ArrayList<URLFileMapping>();
Expand All @@ -65,9 +71,8 @@ private List<URLFileMapping> loadMappings(BundleContext bc,ConfigurationService

public void go(BundleContext bc) throws Exception {
HttpService http=(HttpService)getService(bc,HttpService.class);
CacheManagerService cache=(CacheManagerService)getService(bc,CacheManagerService.class);
ConfigurationService config=(ConfigurationService)getService(bc,ConfigurationService.class);
ActivationProcess activation=new ActivationProcess(http,cache);
ActivationProcess activation=new ActivationProcess(new ServiceResolver(bc),http);
for(URLFileMapping m : loadMappings(bc,config))
activation.map(m.getURL(),m.getFileSystem());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,15 +62,14 @@ public class FileServlet extends HttpServlet {
private File baseFile;
private Map<String, String> mimeTypes;
private String welcomeFile;
private Cache<FileCache> cache;
private long maxCacheSize;

private ServiceResolver resolver;

/**
* @param cacheManagerService
*/
public FileServlet(CacheManagerService cacheManagerService) {
cache = cacheManagerService.getCache("file-servlet-cache", CacheScope.INSTANCE);
public FileServlet(ServiceResolver resolver) {
this.resolver=resolver;
}

private void loadMIMETypesFromFile() throws IOException {
Expand Down Expand Up @@ -157,10 +156,14 @@ protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws Se
return;
}
logger.debug("Sending "+to_send.getAbsolutePath());
try {
if(to_send.isDirectory())
sendDirectory(to_send,resp);
else
sendFile(to_send,resp);
} catch(ServiceDisappearedException x) {
throw new ServletException("No service available",x);
}
}

private void sendDirectory(File f, HttpServletResponse resp) throws IOException {
Expand All @@ -184,7 +187,7 @@ private boolean candidateForCache(File file) {
return file.length() < maxCacheSize;
}

private FileCache getNonExpiredFileCache(File file) {
private FileCache getNonExpiredFileCache(Cache<FileCache> cache,File file) {
if(!candidateForCache(file))
return null;
String path = file.getAbsolutePath();
Expand All @@ -196,14 +199,16 @@ private FileCache getNonExpiredFileCache(File file) {
return fc;
}

private void sendFile(File file, HttpServletResponse resp) throws IOException {
private void sendFile(File file, HttpServletResponse resp) throws IOException, ServiceDisappearedException {
CacheManagerService cache_service=(CacheManagerService)resolver.getService(CacheManagerService.class);
Cache<FileCache> cache=cache_service.getCache("file-servlet-cache", CacheScope.INSTANCE);
if(!file.exists()) {
resp.sendError(HttpServletResponse.SC_NOT_FOUND);
return;
}
if(candidateForCache(file)) {
// Cacheable
FileCache cache_entry=getNonExpiredFileCache(file);
FileCache cache_entry=getNonExpiredFileCache(cache,file);
if(cache_entry==null) {
cache_entry = new FileCache(file,getContentTypeFromExtension(file));
cache.put(file.getAbsolutePath(),cache_entry);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package org.sakaiproject.kernel2.uxloader;

public class ServiceDisappearedException extends Exception {
public ServiceDisappearedException() {
super();
}

public ServiceDisappearedException(String message, Throwable cause) {
super(message, cause);
}

public ServiceDisappearedException(String message) {
super(message);
}

public ServiceDisappearedException(Throwable cause) {
super(cause);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package org.sakaiproject.kernel2.uxloader;

import org.osgi.framework.BundleContext;
import org.osgi.framework.ServiceReference;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class ServiceResolver {
protected static final Logger logger = LoggerFactory.getLogger(ServiceResolver.class);

private BundleContext bc;

public ServiceResolver(BundleContext bc) { this.bc=bc; }

public Object getService(Class klass) throws ServiceDisappearedException {
ServiceReference ref=bc.getServiceReference(klass.getName());
if(ref==null) {
logger.warn("Service disappeared: "+klass.getCanonicalName());
throw new ServiceDisappearedException("Service disappeared: "+klass.getCanonicalName());
}
Object out=bc.getService(ref);
logger.info("Got "+out);
return out;

}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package org.sakaiproject.kernel2.uxloader;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.osgi.framework.BundleActivator;
Expand All @@ -11,10 +13,55 @@
import org.slf4j.LoggerFactory;

public abstract class SimpleBaseActivator implements BundleActivator {
protected static final Logger logger = LoggerFactory.getLogger(Activator.class);

private class GoInvoker implements Runnable {
private BundleContext bc;
private Class<?> outer;

private GoInvoker(Class<?> outer,BundleContext bc) {
this.bc=bc;
this.outer=outer;
}

private boolean is_good() {
for(Class<?> svc : startup_services) {
Object out=getService(bc,svc);
if(out==null) {
logger.info("Cannot start yet : "+svc.getCanonicalName());
return false;
}
}
logger.info("All services ready");
return true;
}

public synchronized void run() {
int f1=1000,f2=1000;

try {
while(!is_good()) {
Thread.sleep(f1);
// Fibionacci backoff: exponential with base of golden-mean: slower than 2.
int t=f1+f2;
f1=f2;
f2=t;
}
go(bc);
} catch (Exception x) {
logger.error("Could not start service ",x);
}
}

}

private Map<Class<?>,ServiceReference> services=new HashMap<Class<?>,ServiceReference>();
private Map<Class<?>,ServiceReference[]> mservices=new HashMap<Class<?>,ServiceReference[]>();
protected static final Logger logger = LoggerFactory.getLogger(Activator.class);

private List<Class<?>> startup_services=new ArrayList<Class<?>>();

protected void registerStartupService(Class svc) {
startup_services.add(svc);
}

public abstract void go(BundleContext bc) throws Exception;

Expand All @@ -25,11 +72,15 @@ protected Object getService(BundleContext bc,Class<?> klass) {
ref=bc.getServiceReference(klass.getName());
services.put(klass,ref);
}
if(ref==null) {
logger.error("Cannot find service for "+klass.getName());
return null;
}
Object out=bc.getService(ref);
logger.info("Got "+out);
return out;
}

protected Object[] getServices(BundleContext bc,Class<?> klass) throws InvalidSyntaxException {
ServiceReference[] refs=mservices.get(klass);
if(refs==null) {
Expand All @@ -45,7 +96,8 @@ protected Object[] getServices(BundleContext bc,Class<?> klass) throws InvalidSy
}

public void start(BundleContext bc) throws Exception {
go(bc);
Thread t=new Thread(new GoInvoker(this.getClass(),bc));
t.start();
}

public void stop(BundleContext bc) throws Exception {
Expand Down
1 change: 1 addition & 0 deletions slingtests/osgikernel/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
<module>bundles/db</module>
<module>bundles/kernel</module>
<module>bundles/memory</module>
<module>bundles/uxloader</module>
<module>libraries/utils</module>
</modules>
</profile>
Expand Down

0 comments on commit 5aa684e

Please sign in to comment.