You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@opennlp.apache.org by co...@apache.org on 2012/07/13 16:43:30 UTC

svn commit: r1361242 - in /opennlp/trunk/opennlp-tools/src: main/java/opennlp/tools/postag/ main/java/opennlp/tools/sentdetect/ main/java/opennlp/tools/tokenize/ main/java/opennlp/tools/util/ test/java/opennlp/tools/postag/ test/java/opennlp/tools/sent...

Author: colen
Date: Fri Jul 13 14:43:29 2012
New Revision: 1361242

URL: http://svn.apache.org/viewvc?rev=1361242&view=rev
Log:
OPENNLP-500: Now the BaseToolFactory uses the ExtensionLoader to instantiate a ToolFactory. ToolFactories now need an empty constructor, and the constructor that takes an ArtifactProvider was replaced by a init method in the BaseToolFactory.

Modified:
    opennlp/trunk/opennlp-tools/src/main/java/opennlp/tools/postag/POSTaggerFactory.java
    opennlp/trunk/opennlp-tools/src/main/java/opennlp/tools/sentdetect/SentenceDetectorFactory.java
    opennlp/trunk/opennlp-tools/src/main/java/opennlp/tools/tokenize/TokenizerFactory.java
    opennlp/trunk/opennlp-tools/src/main/java/opennlp/tools/util/BaseToolFactory.java
    opennlp/trunk/opennlp-tools/src/test/java/opennlp/tools/postag/DummyPOSTaggerFactory.java
    opennlp/trunk/opennlp-tools/src/test/java/opennlp/tools/postag/POSTaggerFactoryTest.java
    opennlp/trunk/opennlp-tools/src/test/java/opennlp/tools/sentdetect/DummySentenceDetectorFactory.java
    opennlp/trunk/opennlp-tools/src/test/java/opennlp/tools/tokenize/DummyTokenizerFactory.java

Modified: opennlp/trunk/opennlp-tools/src/main/java/opennlp/tools/postag/POSTaggerFactory.java
URL: http://svn.apache.org/viewvc/opennlp/trunk/opennlp-tools/src/main/java/opennlp/tools/postag/POSTaggerFactory.java?rev=1361242&r1=1361241&r2=1361242&view=diff
==============================================================================
--- opennlp/trunk/opennlp-tools/src/main/java/opennlp/tools/postag/POSTaggerFactory.java (original)
+++ opennlp/trunk/opennlp-tools/src/main/java/opennlp/tools/postag/POSTaggerFactory.java Fri Jul 13 14:43:29 2012
@@ -34,7 +34,6 @@ import opennlp.tools.dictionary.Dictiona
 import opennlp.tools.util.BaseToolFactory;
 import opennlp.tools.util.InvalidFormatException;
 import opennlp.tools.util.SequenceValidator;
-import opennlp.tools.util.model.ArtifactProvider;
 import opennlp.tools.util.model.ArtifactSerializer;
 import opennlp.tools.util.model.UncloseableInputStream;
 
@@ -57,20 +56,6 @@ public class POSTaggerFactory extends Ba
   }
 
   /**
-   * Creates a {@link POSTaggerFactory} with an {@link ArtifactProvider} that
-   * will be used to retrieve artifacts. This constructor will try to get the ngram
-   * and POS tags dictionaries from the artifact provider.
-   * <p>
-   * Sub-classes should implement a constructor with this signatures and call
-   * this constructor.
-   * <p>
-   * This will be used to load the factory from a serialized POSModel.
-   */
-  public POSTaggerFactory(ArtifactProvider artifactProvider) {
-    super(artifactProvider);
-  }
-
-  /**
    * Creates a {@link POSTaggerFactory}. Use this constructor to
    * programmatically create a factory.
    * 

Modified: opennlp/trunk/opennlp-tools/src/main/java/opennlp/tools/sentdetect/SentenceDetectorFactory.java
URL: http://svn.apache.org/viewvc/opennlp/trunk/opennlp-tools/src/main/java/opennlp/tools/sentdetect/SentenceDetectorFactory.java?rev=1361242&r1=1361241&r2=1361242&view=diff
==============================================================================
--- opennlp/trunk/opennlp-tools/src/main/java/opennlp/tools/sentdetect/SentenceDetectorFactory.java (original)
+++ opennlp/trunk/opennlp-tools/src/main/java/opennlp/tools/sentdetect/SentenceDetectorFactory.java Fri Jul 13 14:43:29 2012
@@ -26,7 +26,6 @@ import opennlp.tools.dictionary.Dictiona
 import opennlp.tools.sentdetect.lang.Factory;
 import opennlp.tools.util.BaseToolFactory;
 import opennlp.tools.util.InvalidFormatException;
-import opennlp.tools.util.model.ArtifactProvider;
 
 /**
  * The factory that provides SentenceDetecor default implementations and
@@ -51,22 +50,6 @@ public class SentenceDetectorFactory ext
   }
 
   /**
-   * Creates a {@link SentenceDetectorFactory} with an {@link ArtifactProvider}
-   * that will be used to retrieve artifacts. This constructor will try to get
-   * the language code, abbreviation dictionary and EOS characters from the
-   * {@link ArtifactProvider}.
-   * <p>
-   * Sub-classes should implement a constructor with this signatures and call
-   * this constructor.
-   * <p>
-   * This will be used to load the factory from a serialized
-   * {@link SentenceModel}.
-   */
-  public SentenceDetectorFactory(ArtifactProvider artifactProvider) {
-    super(artifactProvider);
-  }
-
-  /**
    * Creates a {@link SentenceDetectorFactory}. Use this constructor to
    * programmatically create a factory.
    * 

Modified: opennlp/trunk/opennlp-tools/src/main/java/opennlp/tools/tokenize/TokenizerFactory.java
URL: http://svn.apache.org/viewvc/opennlp/trunk/opennlp-tools/src/main/java/opennlp/tools/tokenize/TokenizerFactory.java?rev=1361242&r1=1361241&r2=1361242&view=diff
==============================================================================
--- opennlp/trunk/opennlp-tools/src/main/java/opennlp/tools/tokenize/TokenizerFactory.java (original)
+++ opennlp/trunk/opennlp-tools/src/main/java/opennlp/tools/tokenize/TokenizerFactory.java Fri Jul 13 14:43:29 2012
@@ -53,22 +53,6 @@ public class TokenizerFactory extends Ba
   }
 
   /**
-   * Creates a {@link TokenizerFactory} with an {@link ArtifactProvider} that
-   * will be used to retrieve artifacts. This constructor will try to get the
-   * language code, abbreviation dictionary etc from the
-   * {@link ArtifactProvider}.
-   * <p>
-   * Sub-classes should implement a constructor with this signatures and call
-   * this constructor.
-   * <p>
-   * This will be used to load the factory from a serialized
-   * {@link TokenizerModel}.
-   */
-  public TokenizerFactory(ArtifactProvider artifactProvider) {
-    super(artifactProvider);
-  }
-
-  /**
    * Creates a {@link TokenizerFactory}. Use this constructor to
    * programmatically create a factory.
    * 

Modified: opennlp/trunk/opennlp-tools/src/main/java/opennlp/tools/util/BaseToolFactory.java
URL: http://svn.apache.org/viewvc/opennlp/trunk/opennlp-tools/src/main/java/opennlp/tools/util/BaseToolFactory.java?rev=1361242&r1=1361241&r2=1361242&view=diff
==============================================================================
--- opennlp/trunk/opennlp-tools/src/main/java/opennlp/tools/util/BaseToolFactory.java (original)
+++ opennlp/trunk/opennlp-tools/src/main/java/opennlp/tools/util/BaseToolFactory.java Fri Jul 13 14:43:29 2012
@@ -17,10 +17,10 @@
 
 package opennlp.tools.util;
 
-import java.lang.reflect.Constructor;
 import java.util.HashMap;
 import java.util.Map;
 
+import opennlp.tools.util.ext.ExtensionLoader;
 import opennlp.tools.util.model.ArtifactProvider;
 import opennlp.tools.util.model.ArtifactSerializer;
 import opennlp.tools.util.model.BaseModel;
@@ -36,19 +36,18 @@ import opennlp.tools.util.model.BaseMode
  */
 public abstract class BaseToolFactory {
 
-  protected final ArtifactProvider artifactProvider;
+  protected ArtifactProvider artifactProvider;
 
   /**
    * All sub-classes should have an empty constructor
    */
   public BaseToolFactory() {
-    this.artifactProvider = null;
   }
 
-  /**
-   * All sub-classes should have a constructor whith this signature
-   */
-  public BaseToolFactory(ArtifactProvider artifactProvider) {
+ /**
+  * Initializes the ToolFactory with an artifact provider.
+  */
+  public void init(ArtifactProvider artifactProvider) {
     this.artifactProvider = artifactProvider;
   }
 
@@ -101,27 +100,21 @@ public abstract class BaseToolFactory {
   public static BaseToolFactory create(String subclassName,
       ArtifactProvider artifactProvider) throws InvalidFormatException {
     BaseToolFactory theFactory = null;
-    Class<? extends BaseToolFactory> factoryClass = loadSubclass(subclassName);
-    if (factoryClass != null) {
-      try {
-        Constructor<?> constructor = null;
-        constructor = factoryClass.getConstructor(ArtifactProvider.class);
-        theFactory = (BaseToolFactory) constructor
-            .newInstance(artifactProvider);
-      } catch (NoSuchMethodException e) {
-        String msg = "Could not instantiate the "
-            + subclassName
-            + ". The mandatry constructor (ArtifactProvider) is missing.";
-        System.err.println(msg);
-        throw new IllegalArgumentException(msg);
-      } catch (Exception e) {
-        String msg = "Could not instantiate the "
-            + subclassName
-            + ". The constructor (ArtifactProvider) throw an exception.";
-        System.err.println(msg);
-        e.printStackTrace();
-        throw new InvalidFormatException(msg);
+    
+    try {
+      // load the ToolFactory using the default constructor
+      theFactory = ExtensionLoader.instantiateExtension(
+          BaseToolFactory.class, subclassName);
+
+      if (theFactory != null) {
+        theFactory.init(artifactProvider);
       }
+    } catch (Exception e) {
+      String msg = "Could not instantiate the " + subclassName
+          + ". The initialization throw an exception.";
+      System.err.println(msg);
+      e.printStackTrace();
+      throw new InvalidFormatException(msg, e);
     }
     return theFactory;
   }
@@ -131,23 +124,15 @@ public abstract class BaseToolFactory {
     BaseToolFactory theFactory = null;
     if (factoryClass != null) {
       try {
-        Constructor<?> constructor = null;
-        constructor = factoryClass.getConstructor(ArtifactProvider.class);
-        theFactory = (BaseToolFactory) constructor
-            .newInstance(artifactProvider);
-      } catch (NoSuchMethodException e) {
-        String msg = "Could not instantiate the "
-            + factoryClass.getCanonicalName()
-            + ". The mandatry constructor (ArtifactProvider) is missing.";
-        System.err.println(msg);
-        throw new IllegalArgumentException(msg);
+        theFactory = factoryClass.newInstance();
+        theFactory.init(artifactProvider);
       } catch (Exception e) {
         String msg = "Could not instantiate the "
             + factoryClass.getCanonicalName()
-            + ". The constructor (ArtifactProvider) throw an exception.";
+            + ". The initialization throw an exception.";
         System.err.println(msg);
         e.printStackTrace();
-        throw new InvalidFormatException(msg);
+        throw new InvalidFormatException(msg, e);
       }
     }
     return theFactory;

Modified: opennlp/trunk/opennlp-tools/src/test/java/opennlp/tools/postag/DummyPOSTaggerFactory.java
URL: http://svn.apache.org/viewvc/opennlp/trunk/opennlp-tools/src/test/java/opennlp/tools/postag/DummyPOSTaggerFactory.java?rev=1361242&r1=1361241&r2=1361242&view=diff
==============================================================================
--- opennlp/trunk/opennlp-tools/src/test/java/opennlp/tools/postag/DummyPOSTaggerFactory.java (original)
+++ opennlp/trunk/opennlp-tools/src/test/java/opennlp/tools/postag/DummyPOSTaggerFactory.java Fri Jul 13 14:43:29 2012
@@ -25,7 +25,6 @@ import java.util.Map;
 import opennlp.tools.dictionary.Dictionary;
 import opennlp.tools.util.InvalidFormatException;
 import opennlp.tools.util.SequenceValidator;
-import opennlp.tools.util.model.ArtifactProvider;
 import opennlp.tools.util.model.ArtifactSerializer;
 import opennlp.tools.util.model.UncloseableInputStream;
 
@@ -35,15 +34,14 @@ public class DummyPOSTaggerFactory exten
   private static final String DUMMY_POSDICT = "DUMMY_POSDICT";
   private DummyPOSDictionary dict;
   
+  public DummyPOSTaggerFactory() {
+  }
+
   public DummyPOSTaggerFactory(Dictionary ngramDictionary, DummyPOSDictionary posDictionary) {
     super(ngramDictionary, null);
     this.dict = posDictionary;
   }
   
-  public DummyPOSTaggerFactory(ArtifactProvider artifactProvider) {
-    super(artifactProvider);
-  }
-  
   @Override
   public SequenceValidator<String> getSequenceValidator() {
     return new DummyPOSSequenceValidator();

Modified: opennlp/trunk/opennlp-tools/src/test/java/opennlp/tools/postag/POSTaggerFactoryTest.java
URL: http://svn.apache.org/viewvc/opennlp/trunk/opennlp-tools/src/test/java/opennlp/tools/postag/POSTaggerFactoryTest.java?rev=1361242&r1=1361241&r2=1361242&view=diff
==============================================================================
--- opennlp/trunk/opennlp-tools/src/test/java/opennlp/tools/postag/POSTaggerFactoryTest.java (original)
+++ opennlp/trunk/opennlp-tools/src/test/java/opennlp/tools/postag/POSTaggerFactoryTest.java Fri Jul 13 14:43:29 2012
@@ -112,7 +112,7 @@ public class POSTaggerFactoryTest {
     assertTrue(factory.getDictionary() instanceof Dictionary);
   }
   
-  @Test(expected = NoClassDefFoundError.class)
+  @Test(expected = InvalidFormatException.class)
   public void testCreateWithInvalidName() throws InvalidFormatException {
     BaseToolFactory.create("X", null);
   }
@@ -122,7 +122,7 @@ public class POSTaggerFactoryTest {
     POSTaggerFactory.create("X", null, null);
   }
 
-  @Test(expected = IllegalArgumentException.class)
+  @Test(expected = InvalidFormatException.class)
   public void testCreateWithHierarchy() throws InvalidFormatException {
     BaseToolFactory.create(Object.class.getCanonicalName(), null);
   }

Modified: opennlp/trunk/opennlp-tools/src/test/java/opennlp/tools/sentdetect/DummySentenceDetectorFactory.java
URL: http://svn.apache.org/viewvc/opennlp/trunk/opennlp-tools/src/test/java/opennlp/tools/sentdetect/DummySentenceDetectorFactory.java?rev=1361242&r1=1361241&r2=1361242&view=diff
==============================================================================
--- opennlp/trunk/opennlp-tools/src/test/java/opennlp/tools/sentdetect/DummySentenceDetectorFactory.java (original)
+++ opennlp/trunk/opennlp-tools/src/test/java/opennlp/tools/sentdetect/DummySentenceDetectorFactory.java Fri Jul 13 14:43:29 2012
@@ -25,7 +25,6 @@ import java.util.Set;
 
 import opennlp.tools.dictionary.Dictionary;
 import opennlp.tools.util.InvalidFormatException;
-import opennlp.tools.util.model.ArtifactProvider;
 import opennlp.tools.util.model.ArtifactSerializer;
 
 public class DummySentenceDetectorFactory extends SentenceDetectorFactory {
@@ -33,16 +32,15 @@ public class DummySentenceDetectorFactor
   private static final String DUMMY_DICT = "dummy";
   private DummyDictionary dict;
 
+  public DummySentenceDetectorFactory() {
+  }
+  
   public DummySentenceDetectorFactory(String languageCode, boolean useTokenEnd,
       Dictionary abbreviationDictionary, char[] eosCharacters) {
     super(languageCode, useTokenEnd, null, eosCharacters);
     this.dict = new DummyDictionary(abbreviationDictionary);
   }
 
-  public DummySentenceDetectorFactory(ArtifactProvider provider) {
-    super(provider);
-  }
-
   @Override
   public DummyDictionary getAbbreviationDictionary() {
     if (this.dict == null && artifactProvider != null) {

Modified: opennlp/trunk/opennlp-tools/src/test/java/opennlp/tools/tokenize/DummyTokenizerFactory.java
URL: http://svn.apache.org/viewvc/opennlp/trunk/opennlp-tools/src/test/java/opennlp/tools/tokenize/DummyTokenizerFactory.java?rev=1361242&r1=1361241&r2=1361242&view=diff
==============================================================================
--- opennlp/trunk/opennlp-tools/src/test/java/opennlp/tools/tokenize/DummyTokenizerFactory.java (original)
+++ opennlp/trunk/opennlp-tools/src/test/java/opennlp/tools/tokenize/DummyTokenizerFactory.java Fri Jul 13 14:43:29 2012
@@ -26,7 +26,6 @@ import java.util.regex.Pattern;
 
 import opennlp.tools.dictionary.Dictionary;
 import opennlp.tools.util.InvalidFormatException;
-import opennlp.tools.util.model.ArtifactProvider;
 import opennlp.tools.util.model.ArtifactSerializer;
 
 public class DummyTokenizerFactory extends TokenizerFactory {
@@ -34,6 +33,9 @@ public class DummyTokenizerFactory exten
   private static final String DUMMY_DICT = "dummy";
   private DummyDictionary dict;
 
+  public DummyTokenizerFactory() {
+  }
+  
   public DummyTokenizerFactory(String languageCode,
       Dictionary abbreviationDictionary, boolean useAlphaNumericOptimization,
       Pattern alphaNumericPattern) {
@@ -42,10 +44,6 @@ public class DummyTokenizerFactory exten
     this.dict = new DummyDictionary(abbreviationDictionary);
   }
 
-  public DummyTokenizerFactory(ArtifactProvider provider) {
-    super(provider);
-  }
-
   @Override
   public DummyDictionary getAbbreviationDictionary() {
     if (this.dict == null && artifactProvider != null) {