This is an automated email from the ASF dual-hosted git repository.

jzemerick pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/opennlp.git


The following commit(s) were added to refs/heads/main by this push:
     new ee7f4a7f OPENNLP-1135 : Removed support for OSGi (#485)
ee7f4a7f is described below

commit ee7f4a7fa135181bdc046f2417f3e001dae799c3
Author: Atita Arora <[email protected]>
AuthorDate: Mon Jan 9 19:34:29 2023 +0100

    OPENNLP-1135 : Removed support for OSGi (#485)
    
    * OPENNLP-1135 : Removed support for OSGi
---
 opennlp-docs/src/docbkx/extension.xml              |  22 -----
 opennlp-tools/pom.xml                              |  36 +------
 .../opennlp/tools/util/ext/ExtensionLoader.java    |  32 +------
 .../util/ext/ExtensionNotLoadedException.java      |  15 ---
 .../tools/util/ext/ExtensionServiceKeys.java       |   2 +-
 .../tools/util/ext/OSGiExtensionLoader.java        | 105 ---------------------
 pom.xml                                            |   1 -
 7 files changed, 3 insertions(+), 210 deletions(-)

diff --git a/opennlp-docs/src/docbkx/extension.xml 
b/opennlp-docs/src/docbkx/extension.xml
index bdb56571..afd5b032 100644
--- a/opennlp-docs/src/docbkx/extension.xml
+++ b/opennlp-docs/src/docbkx/extension.xml
@@ -38,26 +38,4 @@ of it. And some components allow to add new feature 
generators.
        interface and should have a public no-argument constructor.
        </para>
 </section>
-
-<section id="tools.extension.osgi">
-       <title>Running in an OSGi container</title>
-       <para>
-       The traditional way of loading an extension via Class.forName does not 
work
-       in an OSGi environment because the class paths of the OpenNLP Tools and 
extension
-       bundle are isolated. OSGi uses services to provide functionality from 
one bundle
-       to another. The extension bundle must register its extensions as 
services so that
-       the OpenNLP tools bundle can use them.
-       The following code illustrates how that can be done:
-                               <programlisting language="java">
-                               <![CDATA[
-Dictionary<String, String> props = new Hashtable<String, String>();
-props.put(ExtensionServiceKeys.ID, "org.test.SuperTokenizer");
-context.registerService(Tokenizer.class.getName(), new 
org.test.SuperTokenizer(), props);]]>
-               </programlisting>
-       The service OpenNLP is looking for might not be (yet) available. In 
this case OpenNLP
-       waits until a timeout is reached. If loading the extension fails an 
ExtensionNotLoadedException
-       is thrown. This exception is also thrown when the thread is interrupted 
while it is waiting for the
-       extension, the interrupted flag will be set again and the calling code 
has a chance to handle it.
-       </para>
-</section>
 </chapter>
\ No newline at end of file
diff --git a/opennlp-tools/pom.xml b/opennlp-tools/pom.xml
index 50d8c083..13b315b1 100644
--- a/opennlp-tools/pom.xml
+++ b/opennlp-tools/pom.xml
@@ -30,26 +30,10 @@
   </parent>
 
   <artifactId>opennlp-tools</artifactId>
-  <packaging>bundle</packaging>
+  <packaging>jar</packaging>
   <name>Apache OpenNLP Tools</name>
 
   <dependencies>
-    <dependency>
-      <groupId>org.osgi</groupId>
-      <artifactId>org.osgi.core</artifactId>
-      <version>${osgi.version}</version>
-      <scope>provided</scope>
-      <optional>true</optional>
-    </dependency>
-
-    <dependency>
-      <groupId>org.osgi</groupId>
-      <artifactId>org.osgi.compendium</artifactId>
-      <version>${osgi.version}</version>
-      <scope>provided</scope>
-      <optional>true</optional>
-    </dependency>
-
     <dependency>
       <groupId>org.junit.jupiter</groupId>
       <artifactId>junit-jupiter-api</artifactId>
@@ -127,24 +111,6 @@
           </execution>
         </executions>
       </plugin>
-
-      <plugin>
-        <groupId>org.apache.felix</groupId>
-        <artifactId>maven-bundle-plugin</artifactId>
-        <extensions>true</extensions>
-        <configuration>
-          <instructions>
-            
<Bundle-Activator>opennlp.tools.util.ext.OSGiExtensionLoader</Bundle-Activator>
-            
<Bundle-RequiredExecutionEnvironment>JavaSE-1.8</Bundle-RequiredExecutionEnvironment>
-            <Export-Package>
-              !opennlp.tools.cmdline.*,
-              opennlp.tools.*
-            </Export-Package>
-            <Import-Package>*</Import-Package>
-          </instructions>
-        </configuration>
-      </plugin>
-
     </plugins>
   </build>
 
diff --git 
a/opennlp-tools/src/main/java/opennlp/tools/util/ext/ExtensionLoader.java 
b/opennlp-tools/src/main/java/opennlp/tools/util/ext/ExtensionLoader.java
index f96e047c..216b8e78 100644
--- a/opennlp-tools/src/main/java/opennlp/tools/util/ext/ExtensionLoader.java
+++ b/opennlp-tools/src/main/java/opennlp/tools/util/ext/ExtensionLoader.java
@@ -30,25 +30,14 @@ import opennlp.tools.commons.Internal;
 @Internal
 public class ExtensionLoader {
 
-  private static boolean isOsgiAvailable = false;
-
   private ExtensionLoader() {
   }
 
-  static boolean isOSGiAvailable() {
-    return isOsgiAvailable;
-  }
-
-  static void setOSGiAvailable() {
-    isOsgiAvailable = true;
-  }
-
   // Pass in the type (interface) of the class to load
   /**
    * Instantiates a user provided extension to OpenNLP.
    * <p>
-   * The extension is either loaded from the class path or if running
-   * inside an OSGi environment via an OSGi service.
+   * The extension is loaded from the class path.
    * <p>
    * Initially, the load is conducted using the public no-arg constructor.
    * If no such constructor is not found, it is checked if the class follows 
the
@@ -102,25 +91,6 @@ public class ExtensionLoader {
       // Class is not on classpath
     }
 
-    // Loading from class path failed
-
-    // Either something is wrong with the class name or OpenNLP is
-    // running in an OSGi environment. The extension classes are not
-    // on our classpath in this case.
-    // In OSGi we need to use services to get access to extensions.
-
-    // Determine if OSGi class is on class path
-
-    // Now load class which depends on OSGi API
-    if (isOsgiAvailable) {
-
-      // The OSGIExtensionLoader class will be loaded when the next line
-      // is executed, but not prior, and that is why it is safe to directly
-      // reference it here.
-      OSGiExtensionLoader extLoader = OSGiExtensionLoader.getInstance();
-      return extLoader.getExtension(clazz, extensionClassName);
-    }
-
     throw new ExtensionNotLoadedException("Unable to find implementation for " 
+
           clazz.getName() + ", the class or service " + extensionClassName +
           " could not be located!");
diff --git 
a/opennlp-tools/src/main/java/opennlp/tools/util/ext/ExtensionNotLoadedException.java
 
b/opennlp-tools/src/main/java/opennlp/tools/util/ext/ExtensionNotLoadedException.java
index c52b159e..d95bb55a 100644
--- 
a/opennlp-tools/src/main/java/opennlp/tools/util/ext/ExtensionNotLoadedException.java
+++ 
b/opennlp-tools/src/main/java/opennlp/tools/util/ext/ExtensionNotLoadedException.java
@@ -23,26 +23,11 @@ package opennlp.tools.util.ext;
 @SuppressWarnings("serial")
 public class ExtensionNotLoadedException extends RuntimeException {
 
-  private final boolean isOSGiEnvironment;
-
   public ExtensionNotLoadedException(String message) {
     super(message);
-
-    isOSGiEnvironment = ExtensionLoader.isOSGiAvailable();
   }
 
   public ExtensionNotLoadedException(Throwable t) {
     super(t);
-
-    isOSGiEnvironment = ExtensionLoader.isOSGiAvailable();
-  }
-
-  /**
-   * Indicates if OpenNLP is running in an OSGi environment or not.
-   *
-   * @return {@code true} if running in an OSGi environment, {@code false} 
otherwise.
-   */
-  public boolean isOSGiEnvironment() {
-    return isOSGiEnvironment;
   }
 }
diff --git 
a/opennlp-tools/src/main/java/opennlp/tools/util/ext/ExtensionServiceKeys.java 
b/opennlp-tools/src/main/java/opennlp/tools/util/ext/ExtensionServiceKeys.java
index 7e2cf401..09f93ac1 100644
--- 
a/opennlp-tools/src/main/java/opennlp/tools/util/ext/ExtensionServiceKeys.java
+++ 
b/opennlp-tools/src/main/java/opennlp/tools/util/ext/ExtensionServiceKeys.java
@@ -21,7 +21,7 @@ public final class ExtensionServiceKeys {
 
   /**
    * Property key for the unique {@code id} which identifies an
-   * OSGi OpenNLP extension service.
+   * OpenNLP extension service.
    */
   public static final String ID = "OPENLP_EXTENSION_ID";
 }
diff --git 
a/opennlp-tools/src/main/java/opennlp/tools/util/ext/OSGiExtensionLoader.java 
b/opennlp-tools/src/main/java/opennlp/tools/util/ext/OSGiExtensionLoader.java
deleted file mode 100644
index 7e7f97e7..00000000
--- 
a/opennlp-tools/src/main/java/opennlp/tools/util/ext/OSGiExtensionLoader.java
+++ /dev/null
@@ -1,105 +0,0 @@
-/*
- * 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 opennlp.tools.util.ext;
-
-import org.osgi.framework.BundleActivator;
-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;
-
-import opennlp.tools.commons.Internal;
-
-/**
- * OSGi bundle activator which can use an OSGi service as
- * an OpenNLP extension.
- * <p>
- * <b>Note:</b>
- * Do not use this class, internal use only!
- */
-@Internal
-public class OSGiExtensionLoader implements BundleActivator {
-
-  private static OSGiExtensionLoader instance;
-
-  private BundleContext context;
-
-  @Override
-  public void start(BundleContext context) throws Exception {
-    instance = this;
-    this.context = context;
-    ExtensionLoader.setOSGiAvailable();
-  }
-
-  @Override
-  public void stop(BundleContext context) throws Exception {
-    instance = null;
-    this.context = null;
-  }
-
-  /**
-   * @param clazz A reference to {@link Class<T>}.
-   * @param id The unique identifier for the extension to retrieve.
-   *           
-   * @return Retrieves the {@link T extension}.
-   * @throws ExtensionNotLoadedException Thrown if the extension is not found 
or loaded.
-   */
-  <T> T getExtension(Class<T> clazz, String id) {
-
-    if (context == null) {
-      throw new IllegalStateException("OpenNLP Tools Bundle is not active!");
-    }
-
-    Filter filter;
-    try {
-      filter = FrameworkUtil.createFilter("(&(objectclass=" + clazz.getName() 
+ ")(" +
-          "opennlp" + "=" + id + "))");
-    } catch (InvalidSyntaxException e) {
-      // Might happen when the provided IDs are invalid in some way.
-      throw new ExtensionNotLoadedException(e);
-    }
-
-    // NOTE: In 4.3 the parameters are <T, T>
-    ServiceTracker extensionTracker = new ServiceTracker(context, filter, 
null);
-
-    T extension = null;
-
-    try {
-      extensionTracker.open();
-
-      try {
-        extension = (T) extensionTracker.waitForService(30000);
-      } catch (InterruptedException e) {
-        Thread.currentThread().interrupt();
-      }
-    } finally {
-      extensionTracker.close();
-    }
-
-    if (extension == null) {
-      throw new ExtensionNotLoadedException("No suitable extension found. 
Extension name: " + id);
-    }
-
-    return extension;
-  }
-
-  static OSGiExtensionLoader getInstance() {
-    return instance;
-  }
-}
diff --git a/pom.xml b/pom.xml
index 406e9569..2a45c984 100644
--- a/pom.xml
+++ b/pom.xml
@@ -158,7 +158,6 @@
                <glassfish.version>2.30.1</glassfish.version>
                <junit.version>5.9.1</junit.version>
                <morfologik.version>2.1.7</morfologik.version>
-               <osgi.version>4.2.0</osgi.version>
                <checkstyle.plugin.version>3.2.0</checkstyle.plugin.version>
                <onnxruntime.version>1.10.0</onnxruntime.version>
                <opennlp.forkCount>1.0C</opennlp.forkCount>

Reply via email to