blob: bdb565711c7afb3e998ca251f60b10cade3a5dda [file] [log] [blame]
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE chapter PUBLIC "-//OASIS//DTD DocBook XML V4.4//EN"
"http://www.oasis-open.org/docbook/xml/4.4/docbookx.dtd"[
]>
<!--
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.
-->
<chapter id="tools.extension">
<title>Extending OpenNLP</title>
<para>
In OpenNLP extension can be used to add new functionality and to
heavily customize an existing component. Most components define
a factory class which can be implemented to customize the creation
of it. And some components allow to add new feature generators.
</para>
<section id="tools.extension.writing">
<title>Writing an extension</title>
<para>
In many places it is possible to pass in an extension class name to customize
some aspect of OpenNLP. The implementation class needs to implement the specified
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>