You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@opennlp.apache.org by jo...@apache.org on 2012/03/19 19:05:38 UTC

svn commit: r1302576 - in /opennlp/sandbox/corpus-server-impl: ./ src/main/java/org/apache/opennlp/corpus_server/impl/

Author: joern
Date: Mon Mar 19 18:05:38 2012
New Revision: 1302576

URL: http://svn.apache.org/viewvc?rev=1302576&view=rev
Log:
OPENNLP-476 Added OSGi classes

Added:
    opennlp/sandbox/corpus-server-impl/src/main/java/org/apache/opennlp/corpus_server/impl/CorpusServerImpl.java
Modified:
    opennlp/sandbox/corpus-server-impl/   (props changed)
    opennlp/sandbox/corpus-server-impl/src/main/java/org/apache/opennlp/corpus_server/impl/Activator.java
    opennlp/sandbox/corpus-server-impl/src/main/java/org/apache/opennlp/corpus_server/impl/LuceneSearchService.java

Propchange: opennlp/sandbox/corpus-server-impl/
------------------------------------------------------------------------------
--- svn:ignore (added)
+++ svn:ignore Mon Mar 19 18:05:38 2012
@@ -0,0 +1,7 @@
+target
+
+.settings
+
+.classpath
+
+.project

Modified: opennlp/sandbox/corpus-server-impl/src/main/java/org/apache/opennlp/corpus_server/impl/Activator.java
URL: http://svn.apache.org/viewvc/opennlp/sandbox/corpus-server-impl/src/main/java/org/apache/opennlp/corpus_server/impl/Activator.java?rev=1302576&r1=1302575&r2=1302576&view=diff
==============================================================================
--- opennlp/sandbox/corpus-server-impl/src/main/java/org/apache/opennlp/corpus_server/impl/Activator.java (original)
+++ opennlp/sandbox/corpus-server-impl/src/main/java/org/apache/opennlp/corpus_server/impl/Activator.java Mon Mar 19 18:05:38 2012
@@ -17,15 +17,27 @@
 
 package org.apache.opennlp.corpus_server.impl;
 
+import java.util.Hashtable;
+
+import org.apache.opennlp.corpus_server.CorpusServer;
 import org.osgi.framework.BundleActivator;
 import org.osgi.framework.BundleContext;
 
 public class Activator implements BundleActivator {
+  
+  private CorpusServerImpl corpusServer;
+
   @Override
   public void start(BundleContext context) throws Exception {
+    corpusServer = new CorpusServerImpl();
+    
+    corpusServer.start();
+    
+    context.registerService(CorpusServer.class.getName(), corpusServer, new Hashtable());
   }
 
   @Override
   public void stop(BundleContext context) throws Exception {
+    corpusServer.stop();
   }
 }
\ No newline at end of file

Added: opennlp/sandbox/corpus-server-impl/src/main/java/org/apache/opennlp/corpus_server/impl/CorpusServerImpl.java
URL: http://svn.apache.org/viewvc/opennlp/sandbox/corpus-server-impl/src/main/java/org/apache/opennlp/corpus_server/impl/CorpusServerImpl.java?rev=1302576&view=auto
==============================================================================
--- opennlp/sandbox/corpus-server-impl/src/main/java/org/apache/opennlp/corpus_server/impl/CorpusServerImpl.java (added)
+++ opennlp/sandbox/corpus-server-impl/src/main/java/org/apache/opennlp/corpus_server/impl/CorpusServerImpl.java Mon Mar 19 18:05:38 2012
@@ -0,0 +1,159 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.opennlp.corpus_server.impl;
+
+import java.io.IOException;
+import java.util.logging.Level;
+import java.util.logging.Logger;
+
+import org.apache.opennlp.corpus_server.CorpusServer;
+import org.apache.opennlp.corpus_server.search.SearchService;
+import org.apache.opennlp.corpus_server.store.CorporaChangeListener;
+import org.apache.opennlp.corpus_server.store.CorporaStore;
+import org.apache.opennlp.corpus_server.store.CorpusStore;
+import org.apache.opennlp.corpus_server.taskqueue.MemoryTaskQueueService;
+import org.apache.opennlp.corpus_server.taskqueue.TaskQueueService;
+
+public class CorpusServerImpl implements CorpusServer {
+
+  static class IndexListener implements CorporaChangeListener {
+
+    private final SearchService searchService;
+
+    IndexListener(SearchService searchService) {
+      this.searchService = searchService;
+    }
+
+    @Override
+    public void addedCAS(CorpusStore store, String casId) {
+      try {
+        searchService.index(store, casId);
+      } catch (IOException e) {
+        // TODO: Also log store name!
+        LOGGER.log(Level.WARNING, "Failed to index cas: " + casId, e);
+      }
+    }
+
+    @Override
+    public void updatedCAS(CorpusStore store, String casId) {
+      addedCAS(store, casId);
+    }
+
+    @Override
+    public void addedCorpus(CorpusStore store) {
+      try {
+        searchService.createIndex(store);
+      } catch (IOException e) {
+        LOGGER.log(Level.WARNING,
+            "Failed to create index: " + store.getCorpusId(), e);
+      }
+    }
+
+    @Override
+    public void removedCAS(CorpusStore store, String casId) {
+      try {
+        searchService.removeFromIndex(store, casId);
+      } catch (IOException e) {
+        LOGGER.log(Level.WARNING, "Failed to remove cas " + casId
+            + "from  index " + store.getCorpusId(), e);
+      }
+    }
+  }
+
+  private final static Logger LOGGER = Logger.getLogger(CorpusServerImpl.class
+      .getName());
+  
+  private CorporaStore store;
+
+  private SearchService searchService;
+
+  private MemoryTaskQueueService taskQueueService;
+
+  private IndexListener indexListener;
+
+  public void start() {
+    store = new DerbyCorporaStore();
+    try {
+      store.initialize();
+    } catch (IOException e) {
+      LOGGER.log(Level.SEVERE, "Failed to start corpora store!", e);
+      return;
+    }
+    
+    LOGGER.info("Successfully loaded database.");
+    
+    searchService = new LuceneSearchService();
+    
+    try {
+      searchService.initialize(store);
+    } catch (IOException e) {
+      LOGGER.log(Level.SEVERE, "Failed to start search service!", e);
+      return;
+    }
+
+    LOGGER.info("Successfully started search service.");
+
+    indexListener = new IndexListener(searchService);
+    store.addCorpusChangeListener(indexListener);
+    
+    taskQueueService = new MemoryTaskQueueService();
+  }
+  
+  public void stop() {
+    taskQueueService = null;
+    
+  // Note: 
+  // Everything should be shutdown in the opposite
+  // order than the startup.
+  
+  taskQueueService = null;
+  
+  if (store != null && indexListener != null) {
+    store.removeCorpusChangeListener(indexListener);
+  }
+  
+  if (searchService != null) {
+    try {
+      searchService.shutdown();
+    } catch (IOException e) {
+      LOGGER.log(Level.SEVERE, "Failed to shutdown search service!", e);
+    }
+  }
+  
+  if (store != null) {
+    try {
+      store.shutdown();
+    } catch (IOException e) {
+      LOGGER.log(Level.SEVERE, "Failed to shutdown corpora store!", e);
+    }
+  }
+    
+  }
+  
+  public CorporaStore getStore() {
+    return store;
+  }
+
+  public SearchService getSearchService() {
+    return searchService;
+  }
+
+  public TaskQueueService getTaskQueueService() {
+    return taskQueueService;
+  }
+}

Modified: opennlp/sandbox/corpus-server-impl/src/main/java/org/apache/opennlp/corpus_server/impl/LuceneSearchService.java
URL: http://svn.apache.org/viewvc/opennlp/sandbox/corpus-server-impl/src/main/java/org/apache/opennlp/corpus_server/impl/LuceneSearchService.java?rev=1302576&r1=1302575&r2=1302576&view=diff
==============================================================================
--- opennlp/sandbox/corpus-server-impl/src/main/java/org/apache/opennlp/corpus_server/impl/LuceneSearchService.java (original)
+++ opennlp/sandbox/corpus-server-impl/src/main/java/org/apache/opennlp/corpus_server/impl/LuceneSearchService.java Mon Mar 19 18:05:38 2012
@@ -34,18 +34,15 @@ import java.util.logging.Logger;
 import org.apache.lucene.analysis.standard.StandardAnalyzer;
 import org.apache.lucene.document.Document;
 import org.apache.lucene.index.IndexReader;
-import org.apache.lucene.index.Term;
 import org.apache.lucene.queryParser.ParseException;
 import org.apache.lucene.queryParser.QueryParser;
 import org.apache.lucene.search.Collector;
 import org.apache.lucene.search.IndexSearcher;
 import org.apache.lucene.search.Query;
 import org.apache.lucene.search.Scorer;
-import org.apache.lucene.search.TermQuery;
 import org.apache.lucene.store.Directory;
 import org.apache.lucene.store.FSDirectory;
 import org.apache.lucene.util.Version;
-import org.apache.opennlp.corpus_server.CorpusServer;
 import org.apache.opennlp.corpus_server.search.SearchService;
 import org.apache.opennlp.corpus_server.store.CorporaStore;
 import org.apache.opennlp.corpus_server.store.CorpusStore;
@@ -73,6 +70,8 @@ public class LuceneSearchService impleme
   
   private final static Logger LOGGER = Logger.getLogger(
       LuceneSearchService.class .getName());
+
+  private CorporaStore store;
   
   /**
    * Maps the corpus id to the Lucas Indexer Analysis Engine.
@@ -92,10 +91,10 @@ public class LuceneSearchService impleme
   private void createIndexWriter(String corpusId, boolean createIndex) throws IOException {
     
     // Set the index mapping file for this corpus in the analysis engine descriptor
-    CorpusStore corpusStore = CorpusServer.getInstance().getStore().getCorpus(corpusId);
+    CorpusStore corpusStore = store.getCorpus(corpusId);
     
     XMLInputSource in = new XMLInputSource(LuceneSearchService.class.getResourceAsStream(
-        "/org/apache/opennlp/corpus_server/search/LuceneIndexer.xml"), new File(""));
+        "/org/apache/opennlp/corpus_server/impl/LuceneIndexer.xml"), new File(""));
     
     try {
       AnalysisEngineDescription specifier;
@@ -151,7 +150,7 @@ public class LuceneSearchService impleme
       try {
         // TODO: Retrieve file form somewhere for this corpus
         indexWriterPropertiesIn = LuceneSearchService.class.getResourceAsStream(
-            "/org/apache/opennlp/corpus_server/search/IndexWriter.properties");
+            "/org/apache/opennlp/corpus_server/impl/IndexWriter.properties");
       
         indexWriterProperties.load(indexWriterPropertiesIn);
       }
@@ -203,9 +202,11 @@ public class LuceneSearchService impleme
   }
   
   @Override
-  public synchronized void initialize(CorporaStore corporaStore) throws IOException {
+  public synchronized void initialize(CorporaStore store) throws IOException {
+    
+    this.store = store;
     
-    for (String corpusId : corporaStore.getCorpusIds()) {
+    for (String corpusId : store.getCorpusIds()) {
       createIndexWriter(corpusId, false);
       LOGGER.info("Created Index Writer for " + corpusId + "corpus.");
     }
@@ -227,7 +228,7 @@ public class LuceneSearchService impleme
     AnalysisEngine indexer = corpusIndexerMap.get(corpusId);
     
     InputStream indexTsIn = LuceneSearchService.class.getResourceAsStream(
-        "/org/apache/opennlp/corpus_server/search/TypeSystem.xml");
+        "/org/apache/opennlp/corpus_server/impl/TypeSystem.xml");
     
     TypeSystemDescription indexTypeDesc;
     try {