001: /*
002: * Copyright (c) 2006 Deutsches Forschungszentrum fuer Kuenstliche Intelligenz DFKI GmbH.
003: * All rights reserved.
004: *
005: * Licensed under a BSD license (http://www.opensource.org/licenses/bsd-license.php)
006: */
007: package org.ontoware.rdf2go.osgi;
008:
009: import java.util.logging.Logger;
010:
011: import org.ontoware.rdf2go.ModelFactory;
012: import org.ontoware.rdf2go.RDF2Go;
013: import org.osgi.framework.BundleActivator;
014: import org.osgi.framework.BundleContext;
015: import org.osgi.framework.ServiceEvent;
016: import org.osgi.framework.ServiceListener;
017: import org.osgi.framework.ServiceReference;
018:
019: /**
020: * Activate the RDF2Go framework.
021: * Listens to registered OSGI services and searches for registered
022: * {@link ModelFactory} instances.
023: * The found factories are compared to a configuration value, if the
024: * configured factory is found, it is set as default. This is the configuration
025: * value to set:
026: * <code>org.ontoware.rdf2go.defaultmodelfactory=(classname of modelfactory)</code>
027: *
028: * @author sauermann
029: */
030: public class RDF2GoActivator implements BundleActivator,
031: ServiceListener {
032:
033: /**
034: * set this variable in the OSGI config to define the default driver
035: * for RDF2Go.
036: */
037: public static final String DEFAULTMODELFACTORY_CFG = "org.ontoware.rdf2go.defaultmodelfactory";
038:
039: private BundleContext bc;
040:
041: private String defaultFactoryClassName = null;
042:
043: private static final Logger log = Logger
044: .getLogger(RDF2GoActivator.class.getName());
045:
046: /**
047: * @see org.osgi.framework.BundleActivator#start(org.osgi.framework.BundleContext)
048: */
049: public void start(BundleContext context) throws Exception {
050: bc = context;
051:
052: defaultFactoryClassName = context
053: .getProperty(DEFAULTMODELFACTORY_CFG);
054: if (defaultFactoryClassName == null) {
055: log
056: .warning("RDF2Go cannot find configuration value for default RDF2Go factory. "
057: + "No Default ModelFactory will be available. Please set "
058: + DEFAULTMODELFACTORY_CFG);
059: }
060:
061: String filter = "(objectclass=" + ModelFactory.class.getName()
062: + ")";
063: context.addServiceListener(this , filter);
064:
065: ServiceReference references[] = context.getServiceReferences(
066: null, filter);
067:
068: for (int i = 0; references != null && i < references.length; i++) {
069: this .serviceChanged(new ServiceEvent(
070: ServiceEvent.REGISTERED, references[i]));
071: }
072: }
073:
074: /**
075: * @see org.osgi.framework.BundleActivator#stop(org.osgi.framework.BundleContext)
076: */
077: public void stop(BundleContext context) throws Exception {
078: bc = null;
079: }
080:
081: /**
082: * Listen to changes in the ModelFactories.
083: * @see org.osgi.framework.ServiceListener#serviceChanged(org.osgi.framework.ServiceEvent)
084: */
085: public void serviceChanged(ServiceEvent event) {
086: if (defaultFactoryClassName == null)
087: return;
088:
089: ModelFactory factory;
090: switch (event.getType()) {
091: case ServiceEvent.REGISTERED:
092: factory = (ModelFactory) bc.getService(event
093: .getServiceReference());
094: if (factory.getClass().getName().equals(
095: defaultFactoryClassName)) {
096: log.info("RDF2Go uses " + defaultFactoryClassName
097: + " as default ModelFactory");
098: RDF2Go.register(factory);
099: } else
100: bc.ungetService(event.getServiceReference());
101: break;
102: case ServiceEvent.UNREGISTERING:
103: factory = (ModelFactory) bc.getService(event
104: .getServiceReference());
105: if (factory.getClass().getName().equals(
106: defaultFactoryClassName)) {
107: log
108: .fine("RDF2Go unregistered the ModelFactory "
109: + defaultFactoryClassName
110: + " as default ModelFactory. No ModelFactory available now. The Bundle of the ModelFactory was unregistered.");
111: RDF2Go.register((ModelFactory) null);
112: }
113: bc.ungetService(event.getServiceReference());
114: break;
115: }
116: // We do not care for modified
117: //case ServiceEvent.MODIFIED:
118: }
119:
120: }
|