[jsword-svn] r1239 - in trunk/incubator/org.crosswire.jsword/src/org/crosswire/jsword: index index/query index/search internal/osgi

Apache apache at www.crosswire.org
Sun Jan 7 15:59:42 MST 2007


Author: 
Date: 2007-01-07 15:59:41 -0700 (Sun, 07 Jan 2007)
New Revision: 1239

Modified:
   trunk/incubator/org.crosswire.jsword/src/org/crosswire/jsword/index/IndexManagerFactory.java
   trunk/incubator/org.crosswire.jsword/src/org/crosswire/jsword/index/query/QueryBuilderFactory.java
   trunk/incubator/org.crosswire.jsword/src/org/crosswire/jsword/index/query/QueryDecoratorFactory.java
   trunk/incubator/org.crosswire.jsword/src/org/crosswire/jsword/index/search/SearcherFactory.java
   trunk/incubator/org.crosswire.jsword/src/org/crosswire/jsword/internal/osgi/Activator.java
   trunk/incubator/org.crosswire.jsword/src/org/crosswire/jsword/internal/osgi/BookRegistry.java
   trunk/incubator/org.crosswire.jsword/src/org/crosswire/jsword/internal/osgi/FilterRegistry.java
   trunk/incubator/org.crosswire.jsword/src/org/crosswire/jsword/internal/osgi/ServiceUtil.java
Log:
[PRB] - Index classes now treated as OSGi services. Corresponding test added. Minor clean-up to existing registry and utility classes.

Modified: trunk/incubator/org.crosswire.jsword/src/org/crosswire/jsword/index/IndexManagerFactory.java
===================================================================
--- trunk/incubator/org.crosswire.jsword/src/org/crosswire/jsword/index/IndexManagerFactory.java	2006-12-31 22:07:58 UTC (rev 1238)
+++ trunk/incubator/org.crosswire.jsword/src/org/crosswire/jsword/index/IndexManagerFactory.java	2007-01-07 22:59:41 UTC (rev 1239)
@@ -21,11 +21,8 @@
  */
 package org.crosswire.jsword.index;
 
-import java.io.IOException;
+import org.crosswire.jsword.internal.osgi.IndexRegistry;
 
-import org.crosswire.common.util.ClassUtil;
-import org.crosswire.common.util.Logger;
-
 /**
  * A Factory class for IndexManagers.
  * 
@@ -48,48 +45,6 @@
      */
     public static IndexManager getIndexManager()
     {
-        return instance;
+        return IndexRegistry.getDefaultIndexManager();
     }
-
-    /**
-     * The singleton
-     */
-    private static IndexManager instance;
-
-    /**
-     * The log stream
-     */
-    private static final Logger log = Logger.getLogger(IndexManagerFactory.class);
-
-    /**
-     * Setup the instance
-     */
-    static
-    {
-        try
-        {
-            Class impl = ClassUtil.getImplementor(IndexManager.class);
-            instance = (IndexManager) impl.newInstance();
-        }
-        catch (IOException e)
-        {
-            log.error("createIndexManager failed", e); //$NON-NLS-1$
-        }
-        catch (ClassCastException e)
-        {
-            log.error("createIndexManager failed", e); //$NON-NLS-1$
-        }
-        catch (ClassNotFoundException e)
-        {
-            log.error("createIndexManager failed", e); //$NON-NLS-1$
-        }
-        catch (IllegalAccessException e)
-        {
-            log.error("createIndexManager failed", e); //$NON-NLS-1$
-        }
-        catch (InstantiationException e)
-        {
-            log.error("createIndexManager failed", e); //$NON-NLS-1$
-        }
-    }
 }

Modified: trunk/incubator/org.crosswire.jsword/src/org/crosswire/jsword/index/query/QueryBuilderFactory.java
===================================================================
--- trunk/incubator/org.crosswire.jsword/src/org/crosswire/jsword/index/query/QueryBuilderFactory.java	2006-12-31 22:07:58 UTC (rev 1238)
+++ trunk/incubator/org.crosswire.jsword/src/org/crosswire/jsword/index/query/QueryBuilderFactory.java	2007-01-07 22:59:41 UTC (rev 1239)
@@ -21,11 +21,8 @@
  */
 package org.crosswire.jsword.index.query;
 
-import java.io.IOException;
+import org.crosswire.jsword.internal.osgi.IndexRegistry;
 
-import org.crosswire.common.util.ClassUtil;
-import org.crosswire.common.util.Logger;
-
 /**
  * A Factory class for QueryBuilder.
  * 
@@ -47,48 +44,6 @@
      */
     public static QueryBuilder getQueryBuilder()
     {
-        return instance;
+        return IndexRegistry.getDefaultIndexQueryBuilder();
     }
-
-    /**
-     * The singleton
-     */
-    private static QueryBuilder instance;
-
-    /**
-     * The log stream
-     */
-    private static final Logger log = Logger.getLogger(QueryBuilderFactory.class);
-
-    /**
-     * Setup the instance
-     */
-    static
-    {
-        try
-        {
-            Class impl = ClassUtil.getImplementor(QueryBuilder.class);
-            instance = (QueryBuilder) impl.newInstance();
-        }
-        catch (IOException e)
-        {
-            log.error("create QueryBuilder failed", e); //$NON-NLS-1$
-        }
-        catch (ClassCastException e)
-        {
-            log.error("create QueryBuilder failed", e); //$NON-NLS-1$
-        }
-        catch (ClassNotFoundException e)
-        {
-            log.error("create QueryBuilder failed", e); //$NON-NLS-1$
-        }
-        catch (InstantiationException e)
-        {
-            log.error("create QueryBuilder failed", e); //$NON-NLS-1$
-        }
-        catch (IllegalAccessException e)
-        {
-            log.error("create QueryBuilder failed", e); //$NON-NLS-1$
-        }
-    }
 }

Modified: trunk/incubator/org.crosswire.jsword/src/org/crosswire/jsword/index/query/QueryDecoratorFactory.java
===================================================================
--- trunk/incubator/org.crosswire.jsword/src/org/crosswire/jsword/index/query/QueryDecoratorFactory.java	2006-12-31 22:07:58 UTC (rev 1238)
+++ trunk/incubator/org.crosswire.jsword/src/org/crosswire/jsword/index/query/QueryDecoratorFactory.java	2007-01-07 22:59:41 UTC (rev 1239)
@@ -21,11 +21,8 @@
  */
 package org.crosswire.jsword.index.query;
 
-import java.io.IOException;
+import org.crosswire.jsword.internal.osgi.IndexRegistry;
 
-import org.crosswire.common.util.ClassUtil;
-import org.crosswire.common.util.Logger;
-
 /**
  * A Factory class for QueryDecorator.
  * 
@@ -47,48 +44,6 @@
      */
     public static QueryDecorator getSearchSyntax()
     {
-        return instance;
+        return IndexRegistry.getDefaultIndexQueryDecorator();
     }
-
-    /**
-     * The singleton
-     */
-    private static QueryDecorator instance;
-
-    /**
-     * The log stream
-     */
-    private static final Logger log = Logger.getLogger(QueryDecoratorFactory.class);
-
-    /**
-     * Setup the instance
-     */
-    static
-    {
-        try
-        {
-            Class impl = ClassUtil.getImplementor(QueryDecorator.class);
-            instance = (QueryDecorator) impl.newInstance();
-        }
-        catch (IOException e)
-        {
-            log.error("create QueryDecorator failed", e); //$NON-NLS-1$
-        }
-        catch (ClassCastException e)
-        {
-            log.error("create QueryDecorator failed", e); //$NON-NLS-1$
-        }
-        catch (ClassNotFoundException e)
-        {
-            log.error("create QueryDecorator failed", e); //$NON-NLS-1$
-        }
-        catch (InstantiationException e)
-        {
-            log.error("create QueryDecorator failed", e); //$NON-NLS-1$
-        }
-        catch (IllegalAccessException e)
-        {
-            log.error("create QueryDecorator failed", e); //$NON-NLS-1$
-        }
-    }
 }

Modified: trunk/incubator/org.crosswire.jsword/src/org/crosswire/jsword/index/search/SearcherFactory.java
===================================================================
--- trunk/incubator/org.crosswire.jsword/src/org/crosswire/jsword/index/search/SearcherFactory.java	2006-12-31 22:07:58 UTC (rev 1238)
+++ trunk/incubator/org.crosswire.jsword/src/org/crosswire/jsword/index/search/SearcherFactory.java	2007-01-07 22:59:41 UTC (rev 1239)
@@ -21,15 +21,13 @@
  */
 package org.crosswire.jsword.index.search;
 
-import java.io.IOException;
-
-import org.crosswire.common.util.ClassUtil;
 import org.crosswire.common.util.Logger;
 import org.crosswire.jsword.book.Book;
 import org.crosswire.jsword.book.BookException;
 import org.crosswire.jsword.index.Index;
 import org.crosswire.jsword.index.IndexManager;
 import org.crosswire.jsword.index.IndexManagerFactory;
+import org.crosswire.jsword.internal.osgi.IndexRegistry;
 
 /**
  * Factory method for creating a new Searcher.
@@ -57,17 +55,11 @@
             IndexManager imanager = IndexManagerFactory.getIndexManager();
             Index index = imanager.getIndex(book);
 
-            Class impl = ClassUtil.getImplementor(Searcher.class);
-            Searcher parser = (Searcher) impl.newInstance();
+            Searcher parser = IndexRegistry.getDefaultIndexSearcher();
             parser.init(index);
 
             return parser;
         }
-        catch (IOException e)
-        {
-            log.error("createSearcher failed", e); //$NON-NLS-1$
-            throw new InstantiationException();
-        }
         catch (BookException e)
         {
             log.error("createSearcher failed", e); //$NON-NLS-1$
@@ -78,16 +70,6 @@
             log.error("createSearcher failed", e); //$NON-NLS-1$
             throw new InstantiationException();
         }
-        catch (ClassNotFoundException e)
-        {
-            log.error("createSearcher failed", e); //$NON-NLS-1$
-            throw new InstantiationException();
-        }
-        catch (IllegalAccessException e)
-        {
-            log.error("createSearcher failed", e); //$NON-NLS-1$
-            throw new InstantiationException();
-        }
     }
 
     /**

Modified: trunk/incubator/org.crosswire.jsword/src/org/crosswire/jsword/internal/osgi/Activator.java
===================================================================
--- trunk/incubator/org.crosswire.jsword/src/org/crosswire/jsword/internal/osgi/Activator.java	2006-12-31 22:07:58 UTC (rev 1238)
+++ trunk/incubator/org.crosswire.jsword/src/org/crosswire/jsword/internal/osgi/Activator.java	2007-01-07 22:59:41 UTC (rev 1239)
@@ -45,6 +45,7 @@
         //The registries manage the services themselves. 
         FilterRegistry.register(context);
         BookRegistry.register(context);
+        IndexRegistry.register(context);
     }
 
     /**
@@ -55,5 +56,6 @@
         Activator.currentContext = null;
         FilterRegistry.unregister(context);
         BookRegistry.unregister(context);
+        IndexRegistry.unregister(context);
     }
 }

Modified: trunk/incubator/org.crosswire.jsword/src/org/crosswire/jsword/internal/osgi/BookRegistry.java
===================================================================
--- trunk/incubator/org.crosswire.jsword/src/org/crosswire/jsword/internal/osgi/BookRegistry.java	2006-12-31 22:07:58 UTC (rev 1238)
+++ trunk/incubator/org.crosswire.jsword/src/org/crosswire/jsword/internal/osgi/BookRegistry.java	2007-01-07 22:59:41 UTC (rev 1239)
@@ -23,7 +23,6 @@
 import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.EventListener;
-import java.util.Hashtable;
 
 import org.crosswire.common.util.EventListenerList;
 import org.crosswire.common.util.Filter;
@@ -32,8 +31,6 @@
 import org.crosswire.jsword.book.readings.ReadingsBookDriver;
 import org.crosswire.jsword.book.sword.SwordBookDriver;
 import org.osgi.framework.BundleContext;
-import org.osgi.framework.FrameworkUtil;
-import org.osgi.framework.InvalidSyntaxException;
 import org.osgi.framework.ServiceReference;
 import org.osgi.util.tracker.ServiceTracker;
 
@@ -48,9 +45,6 @@
 public final class BookRegistry
 {
 
-    private static ArrayList books = new ArrayList();
-    private static ServiceTracker driverTracker;
-
     /**
      * This method returns an array of the available
      * book drivers. No drivers are available unless
@@ -105,8 +99,9 @@
     static void register(BundleContext context)
     {
         String bookDriverClassName = BookDriver.class.getName();
-        context.registerService(bookDriverClassName, new SwordBookDriver(), createBookDriverProperties("sword"));
-        context.registerService(bookDriverClassName, new ReadingsBookDriver(), createBookDriverProperties("readings"));
+        context.registerService(bookDriverClassName, new SwordBookDriver(), ServiceUtil.createIdDictionary(ID_BOOKDRIVER, "sword"));
+        context.registerService(bookDriverClassName, new ReadingsBookDriver(), ServiceUtil.createIdDictionary(ID_BOOKDRIVER, "sword"));
+        
         driverTracker = new ServiceTracker(context, bookDriverClassName, null)
         {
             public Object addingService(ServiceReference reference)
@@ -207,44 +202,9 @@
      */
     public static BookDriver getBookDriverById(String bookDriverId)
     {
-        if (Activator.currentContext == null || bookDriverId == null) {
-            return null;
-        }
-        
-        //Don't use the driverTracker here. Just create a new
-        //OSGi filter and let the framework find a match. 
-        org.osgi.framework.Filter osgiFilter;
-        try
-        {
-            osgiFilter = FrameworkUtil.createFilter("(&(objectclass=" + Filter.class.getName() + ")(bookdriver.id=" + bookDriverId + "))");
-        }
-        catch (InvalidSyntaxException e)
-        {
-            //This will never occur... but in case it does, bail out to the highest level.
-            throw new RuntimeException("Unexpected syntax exception", e);
-        }
-        Object result = ServiceUtil.runOperation(Activator.currentContext, osgiFilter, new ServiceOperation() {
-            public Object run(OperationContext context) throws Exception
-            {
-                return context.getService();
-            }
-        });
-        
-        if (result == null) {
-            //the requested driver wasn't found
-            return null;
-        } 
-        
-        return (BookDriver) result;
+        return (BookDriver) ServiceUtil.getServiceById(BookDriver.class, ID_BOOKDRIVER, bookDriverId);
     }
     
-    private static Hashtable createBookDriverProperties(String bookDriverId)
-    {
-        Hashtable properties = new Hashtable();
-        properties.put("bookdriver.id", bookDriverId);
-        return properties;
-    }
-
     private static BookDriver[] getBookDrivers(Filter filter)
     {
         BundleContext context = Activator.currentContext;
@@ -315,4 +275,9 @@
             addBooks(driver);
         }
     }
+    
+    private static final String ID_BOOKDRIVER = "bookdriver.id";
+    private static ArrayList books = new ArrayList();
+    private static ServiceTracker driverTracker;
+    
 }

Modified: trunk/incubator/org.crosswire.jsword/src/org/crosswire/jsword/internal/osgi/FilterRegistry.java
===================================================================
--- trunk/incubator/org.crosswire.jsword/src/org/crosswire/jsword/internal/osgi/FilterRegistry.java	2006-12-31 22:07:58 UTC (rev 1238)
+++ trunk/incubator/org.crosswire.jsword/src/org/crosswire/jsword/internal/osgi/FilterRegistry.java	2007-01-07 22:59:41 UTC (rev 1239)
@@ -20,16 +20,12 @@
  */
 package org.crosswire.jsword.internal.osgi;
 
-import java.util.Hashtable;
-
 import org.crosswire.jsword.book.filter.Filter;
 import org.crosswire.jsword.book.filter.gbf.GBFFilter;
 import org.crosswire.jsword.book.filter.osis.OSISFilter;
 import org.crosswire.jsword.book.filter.plaintext.PlainTextFilter;
 import org.crosswire.jsword.book.filter.thml.THMLFilter;
 import org.osgi.framework.BundleContext;
-import org.osgi.framework.FrameworkUtil;
-import org.osgi.framework.InvalidSyntaxException;
 
 /**
  * This class provides Filter-related data to the classes
@@ -49,11 +45,11 @@
         String filterClassName = Filter.class.getName();
         //don't reuse the same PlainText service object for the two ids, 
         //we want them to be managed separately.
-        context.registerService(filterClassName, new PlainTextFilter(), createFilterProperties("plaintext"));
-        context.registerService(filterClassName, new PlainTextFilter(), createFilterProperties("default"));
-        context.registerService(filterClassName, new GBFFilter(), createFilterProperties("gbf"));
-        context.registerService(filterClassName, new OSISFilter(), createFilterProperties("osis"));
-        context.registerService(filterClassName, new THMLFilter(), createFilterProperties("thml"));
+        context.registerService(filterClassName, new PlainTextFilter(), ServiceUtil.createIdDictionary(ID_FILTER, "plaintext"));
+        context.registerService(filterClassName, new PlainTextFilter(), ServiceUtil.createIdDictionary(ID_FILTER, "default"));
+        context.registerService(filterClassName, new GBFFilter(), ServiceUtil.createIdDictionary(ID_FILTER, "gbf"));
+        context.registerService(filterClassName, new OSISFilter(), ServiceUtil.createIdDictionary(ID_FILTER, "osis"));
+        context.registerService(filterClassName, new THMLFilter(), ServiceUtil.createIdDictionary(ID_FILTER, "thml"));
 
     }
 
@@ -62,13 +58,6 @@
         //Nothing to purge.
     }
 
-    private static Hashtable createFilterProperties(String filterId)
-    {
-        Hashtable properties = new Hashtable();
-        properties.put("filter.id", filterId);
-        return properties;
-    }
-
     /**
      * This method returns the filter for the given id. The default filter
      * is returned if the filter id has no corresponding filter 
@@ -81,24 +70,7 @@
      */
     public static Filter getFilterById(String filterId)
     {
-        BundleContext context = Activator.currentContext;
-        if (context == null)
-        {
-            return null;
-        }
-
-        org.osgi.framework.Filter osgiFilter;
-        try
-        {
-            osgiFilter = FrameworkUtil.createFilter("(&(objectclass=" + Filter.class.getName() + ")(filter.id=" + filterId + "))");
-        }
-        catch (InvalidSyntaxException e)
-        {
-            //This will never occur... but in case it does, bail out to the highest level.
-            throw new RuntimeException("Unexpected syntax exception", e);
-        }
-
-        return (Filter) ServiceUtil.runOperation(context, osgiFilter, new FindFilterOperation());
+        return (Filter) ServiceUtil.getServiceById(Filter.class, ID_FILTER, filterId);
     }
 
     /**
@@ -110,12 +82,7 @@
     {
         return getFilterById("default");
     }
-
-    private static final class FindFilterOperation implements ServiceOperation
-    {
-        public Object run(OperationContext context) throws Exception
-        {
-            return context.getService();
-        }
-    }
+    
+    private static final String ID_FILTER = "filter.id";
+    
 }

Modified: trunk/incubator/org.crosswire.jsword/src/org/crosswire/jsword/internal/osgi/ServiceUtil.java
===================================================================
--- trunk/incubator/org.crosswire.jsword/src/org/crosswire/jsword/internal/osgi/ServiceUtil.java	2006-12-31 22:07:58 UTC (rev 1238)
+++ trunk/incubator/org.crosswire.jsword/src/org/crosswire/jsword/internal/osgi/ServiceUtil.java	2007-01-07 22:59:41 UTC (rev 1239)
@@ -20,9 +20,14 @@
  */
 package org.crosswire.jsword.internal.osgi;
 
+import java.util.Dictionary;
+import java.util.Hashtable;
+
 import org.crosswire.common.util.Logger;
 import org.osgi.framework.BundleContext;
 import org.osgi.framework.Filter;
+import org.osgi.framework.FrameworkUtil;
+import org.osgi.framework.InvalidSyntaxException;
 import org.osgi.util.tracker.ServiceTracker;
 
 /**
@@ -68,6 +73,52 @@
     }
 
     /**
+     * This method returns the first service of the given class with the given id value.
+     * 
+     * @param klass The returned service will implement this class. The service must have been
+     * registered under this class name.
+     * @param idName The name of the id property.
+     * @param idValue The value of the id property. 
+     * @return A service that implements a particular class with a particular id, or <code>null</code>
+     * if not found.
+     */
+    public static Object getServiceById(Class klass, String idName, String idValue) {
+        BundleContext context = Activator.currentContext;
+        if (context == null)
+        {
+            //The bundle isn't registered.
+            return null;
+        }
+
+        org.osgi.framework.Filter osgiFilter;
+        try
+        {
+            osgiFilter = FrameworkUtil.createFilter("(&(objectclass=" + klass.getName() + ")(" + idName + "=" + idValue + "))");
+        }
+        catch (InvalidSyntaxException e)
+        {
+            //This will never occur... but in case it does, bail out to the highest level.
+            throw new RuntimeException("Unexpected syntax exception", e);
+        }
+
+        return ServiceUtil.runOperation(context, osgiFilter, new GetServiceOperation());
+    }
+    
+    /**
+     * This method creates a dictionary with a single name-value pair, comprised
+     * of the two given parameters. The returned dictionary is fit for use as a 
+     * service's properties. 
+     * @param idName The name of the id property to associate with a service.
+     * @param idValue The value of the id property to associate with a service. 
+     * @return A dictionary containing a single name-value pair.
+     */
+    public static Dictionary createIdDictionary(String idName, String idValue) {
+        Hashtable properties = new Hashtable();
+        properties.put(idName, idValue);
+        return properties;
+    }
+    
+    /**
      * This method runs the operation. The given tracker is converted to a 
      * <code>ServiceContext</code> object and passed to the operation.
      * @param tracker A service tracker, built by one of this class's methods. 




More information about the jsword-svn mailing list