Author: rwesten
Date: Fri Jul  1 14:22:49 2011
New Revision: 1141951

URL: http://svn.apache.org/viewvc?rev=1141951&view=rev
Log:
Updated the geonames.org based engine to follow the same schema as the others 
depending on a remote service.

This Engine now is unsatisfied as long as the user does not provide account 
information or explicitly activate the use of the server allowing anonymous 
access.

This change is mainly because the server allowing anonymous access most of the 
time returns with 503.

I also adapted the unit tests to use a account specificly created for stanbol. 
Note that this test is still deactivated by default to avaoid dependencies to 
the availability of remote service during the build process. 

Modified:
    
incubator/stanbol/trunk/enhancer/engines/geonames/src/main/java/org/apache/stanbol/enhancer/engines/geonames/impl/GeonamesAPIWrapper.java
    
incubator/stanbol/trunk/enhancer/engines/geonames/src/main/java/org/apache/stanbol/enhancer/engines/geonames/impl/LocationEnhancementEngine.java
    
incubator/stanbol/trunk/enhancer/engines/geonames/src/main/resources/OSGI-INF/metatype/metatype.properties
    
incubator/stanbol/trunk/enhancer/engines/geonames/src/test/java/org/apache/stanbol/enhancer/engines/geonames/impl/TestLocationEnhancementEngine.java

Modified: 
incubator/stanbol/trunk/enhancer/engines/geonames/src/main/java/org/apache/stanbol/enhancer/engines/geonames/impl/GeonamesAPIWrapper.java
URL: 
http://svn.apache.org/viewvc/incubator/stanbol/trunk/enhancer/engines/geonames/src/main/java/org/apache/stanbol/enhancer/engines/geonames/impl/GeonamesAPIWrapper.java?rev=1141951&r1=1141950&r2=1141951&view=diff
==============================================================================
--- 
incubator/stanbol/trunk/enhancer/engines/geonames/src/main/java/org/apache/stanbol/enhancer/engines/geonames/impl/GeonamesAPIWrapper.java
 (original)
+++ 
incubator/stanbol/trunk/enhancer/engines/geonames/src/main/java/org/apache/stanbol/enhancer/engines/geonames/impl/GeonamesAPIWrapper.java
 Fri Jul  1 14:22:49 2011
@@ -38,10 +38,18 @@ public class GeonamesAPIWrapper {
 
     static final Logger log = 
LoggerFactory.getLogger(GeonamesAPIWrapper.class);
     /**
-     * URI of the geonames.orf web services. This URI is used for the free as
-     * well as premium accounts without an own sub domain.
+     * URI of the geonames.org web services used as default. Currently this is
+     * the server that requires authentication with a free user account. The
+     * server that allows anonymous requests is often overloaded and is 
therefore
+     * no longer the default. Note that the anonymous server is still used in 
+     * case this class is initialised with the default constructor.
      */
-    public static final String GEONAMES_ORG_WEBSERVICE_URL = 
"http://ws.geonames.org/";;
+    public static final String DEFAULT_GEONAMES_ORG_WEBSERVICE_URL = 
"http://api.geonames.org/";;
+    /**
+     * The geonames.org web service URI allowing anonymous connections. Often
+     * overloaded. Will be used with the default constructor
+     */
+    public static final String ANONYMOUS_GEONAMES_ORG_WEBSERVICE_URL = 
"http://ws.geonames.org/";;
     /**
      * Relative path to the search service
      */
@@ -53,16 +61,16 @@ public class GeonamesAPIWrapper {
 
     /**
      * The access url for the search service. As default
-     * {@link #GEONAMES_ORG_WEBSERVICE_URL}+{@link #SEARCH_SERVICE_PATH}
-     * ({@value #GEONAMES_ORG_WEBSERVICE_URL}{@value #SEARCH_SERVICE_PATH})
+     * {@link #DEFAULT_GEONAMES_ORG_WEBSERVICE_URL}+{@link 
#SEARCH_SERVICE_PATH}
+     * ({@value #DEFAULT_GEONAMES_ORG_WEBSERVICE_URL}{@value 
#SEARCH_SERVICE_PATH})
      * is used. Users with a premium account including an own sub domain
      * might need to change this.
      */
     protected String searchServiceUrl;
     /**
      * The access url for the hierarchy service. As default
-     * {@link #GEONAMES_ORG_WEBSERVICE_URL}+{@link #HIERARCHY_SERVICE_PATH}
-     * ({@value #GEONAMES_ORG_WEBSERVICE_URL}{@value #HIERARCHY_SERVICE_PATH})
+     * {@link #DEFAULT_GEONAMES_ORG_WEBSERVICE_URL}+{@link 
#HIERARCHY_SERVICE_PATH}
+     * ({@value #DEFAULT_GEONAMES_ORG_WEBSERVICE_URL}{@value 
#HIERARCHY_SERVICE_PATH})
      * is used. Users with a premium account including an own sub domain
      * might need to change this.
      */
@@ -282,8 +290,8 @@ public class GeonamesAPIWrapper {
      * Initialises a the geonames API wrapper as used for the free service
      */
     public GeonamesAPIWrapper() {
-        this(GEONAMES_ORG_WEBSERVICE_URL + SEARCH_SERVICE_PATH,
-                GEONAMES_ORG_WEBSERVICE_URL + HIERARCHY_SERVICE_PATH,
+        this(ANONYMOUS_GEONAMES_ORG_WEBSERVICE_URL + SEARCH_SERVICE_PATH,
+                DEFAULT_GEONAMES_ORG_WEBSERVICE_URL + HIERARCHY_SERVICE_PATH,
                 null, null);
     }
 
@@ -328,10 +336,19 @@ public class GeonamesAPIWrapper {
      * If no valid user name is parsed the token will be ignored.
      */
     public GeonamesAPIWrapper(String searchService, String hierarchyService, 
String userName, String token) {
-        this.searchServiceUrl = searchService != null ? searchService : 
(GEONAMES_ORG_WEBSERVICE_URL + SEARCH_SERVICE_PATH);
-        this.hierarchyServiceUrl = hierarchyService != null ? hierarchyService 
: (GEONAMES_ORG_WEBSERVICE_URL + HIERARCHY_SERVICE_PATH);
         this.userName = userName == null || userName.isEmpty() ? null : 
userName;
         this.token = this.userName == null ? null : token;
+        if(this.userName != null && this.token == null){
+            throw new IllegalArgumentException("The Token MUST NOT be NULL nor 
empty of a User-Name is parsed!");
+        }
+        String defaultServer; //only used if the parsed service urls are null
+        if(this.userName != null){
+            defaultServer = DEFAULT_GEONAMES_ORG_WEBSERVICE_URL;
+        } else {
+            defaultServer = ANONYMOUS_GEONAMES_ORG_WEBSERVICE_URL;
+        }
+        this.searchServiceUrl = searchService != null ? searchService : 
(defaultServer + SEARCH_SERVICE_PATH);
+        this.hierarchyServiceUrl = hierarchyService != null ? hierarchyService 
: (defaultServer + HIERARCHY_SERVICE_PATH);
     }
 
     public List<Toponym> searchToponyms(String name) throws IOException {

Modified: 
incubator/stanbol/trunk/enhancer/engines/geonames/src/main/java/org/apache/stanbol/enhancer/engines/geonames/impl/LocationEnhancementEngine.java
URL: 
http://svn.apache.org/viewvc/incubator/stanbol/trunk/enhancer/engines/geonames/src/main/java/org/apache/stanbol/enhancer/engines/geonames/impl/LocationEnhancementEngine.java?rev=1141951&r1=1141950&r2=1141951&view=diff
==============================================================================
--- 
incubator/stanbol/trunk/enhancer/engines/geonames/src/main/java/org/apache/stanbol/enhancer/engines/geonames/impl/LocationEnhancementEngine.java
 (original)
+++ 
incubator/stanbol/trunk/enhancer/engines/geonames/src/main/java/org/apache/stanbol/enhancer/engines/geonames/impl/LocationEnhancementEngine.java
 Fri Jul  1 14:22:49 2011
@@ -61,6 +61,7 @@ import org.apache.stanbol.enhancer.servi
 import org.apache.stanbol.enhancer.servicesapi.helper.EnhancementEngineHelper;
 import org.apache.stanbol.enhancer.servicesapi.rdf.NamespaceEnum;
 import org.apache.stanbol.commons.stanboltools.offline.OnlineMode;
+import org.osgi.service.cm.ConfigurationException;
 import org.osgi.service.component.ComponentContext;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
@@ -114,8 +115,14 @@ public class LocationEnhancementEngine i
     public static final String MIN_HIERARCHY_SCORE = 
"org.apache.stanbol.enhancer.engines.geonames.locationEnhancementEngine.min-hierarchy-score";
 
     public static final UriRef CONCEPT_GEONAMES_FEATURE = new 
UriRef(NamespaceEnum.geonames.toString() + "Feature");
-    @Property(value = GeonamesAPIWrapper.GEONAMES_ORG_WEBSERVICE_URL)
+    @Property(value = GeonamesAPIWrapper.DEFAULT_GEONAMES_ORG_WEBSERVICE_URL)
     public static final String GEONAMES_SERVER_URL = 
"org.apache.stanbol.enhancer.engines.geonames.locationEnhancementEngine.serverURL";
+    /**
+     * The useage of the anonymous server is deactivated by default because it
+     * is often overloaded and therefore causes randomly errors.
+     */
+    @Property(boolValue=false)
+    public static final String GEONAMES_ANONYMOUS_SERVICE_STATE = 
"org.apache.stanbol.enhancer.engines.geonames.locationEnhancementEngine.allow-anonymous-service";
     @Property
     public static final String GEONAMES_USERNAME = 
"org.apache.stanbol.enhancer.engines.geonames.locationEnhancementEngine.username";
     @Property
@@ -229,7 +236,7 @@ public class LocationEnhancementEngine i
     private Double minHierarchyScore;
 
     @SuppressWarnings("unchecked")
-    protected void activate(ComponentContext ce) throws IOException {
+    protected void activate(ComponentContext ce) throws IOException, 
ConfigurationException {
         Dictionary<String, Object> properties = ce.getProperties();
         log.debug("activating ...");
         //NOTE: The type of the values is ensured by the default values in the
@@ -237,12 +244,40 @@ public class LocationEnhancementEngine i
         setMinScore((Double) properties.get(MIN_SCORE));
         setMaxLocationEnhancements((Integer) 
properties.get(MAX_LOCATION_ENHANCEMENTS));
         setMinHierarchyScore((Double) properties.get(MIN_HIERARCHY_SCORE));
-        String serverUrl = (String) properties.get(GEONAMES_SERVER_URL);
-        if (serverUrl != null && serverUrl.isEmpty()) {
-            serverUrl = null; //prevent empty serverURLs (e.g. if the user 
deletes an value)
+        //parse geonames.org service specific configuration
+        Object value = properties.get(GEONAMES_ANONYMOUS_SERVICE_STATE);
+        boolean allowAnonymous;
+        if(value instanceof Boolean){
+            allowAnonymous = ((Boolean)value).booleanValue();
+        } else if(value != null){
+            allowAnonymous = Boolean.parseBoolean(value.toString());
+        } else {
+            allowAnonymous = false;
         }
+        String serverUrl = (String) properties.get(GEONAMES_SERVER_URL);
         String userName = (String) properties.get(GEONAMES_USERNAME);
         String token = (String) properties.get(GEONAMES_TOKEN);
+        if(userName == null || userName.isEmpty()){
+            if(allowAnonymous) {
+                log.info("Anonymous Access is enabled and no User-Name is 
configured." +
+                               "Ignore configred server URL {} and will use 
the anonymous server {}",
+                               
serverUrl,GeonamesAPIWrapper.ANONYMOUS_GEONAMES_ORG_WEBSERVICE_URL);
+                serverUrl = 
GeonamesAPIWrapper.ANONYMOUS_GEONAMES_ORG_WEBSERVICE_URL;
+            } else {
+                throw new ConfigurationException(GEONAMES_USERNAME, 
+                    "A User-Name MUST be configured if anonymous access to 
'http://ws.geonames.org' is deactivated");
+            }
+        } else {
+            if( token == null || token.isEmpty()){
+                throw new ConfigurationException(GEONAMES_TOKEN, 
+                    "The Token MUST NOT be NULL nor empty if a User-Name is 
defined!");
+            }
+            if(serverUrl == null || serverUrl.isEmpty()){
+                log.info("No ServerUrl is configured. Will use the default {}",
+                    GeonamesAPIWrapper.DEFAULT_GEONAMES_ORG_WEBSERVICE_URL);
+                serverUrl = 
GeonamesAPIWrapper.DEFAULT_GEONAMES_ORG_WEBSERVICE_URL;
+            }
+        }
         log.info(String.format("create Geonames Client for server: %s and 
user: %s (token not logged)",
                 serverUrl, userName));
         geonamesService = new GeonamesAPIWrapper(serverUrl, userName, token);

Modified: 
incubator/stanbol/trunk/enhancer/engines/geonames/src/main/resources/OSGI-INF/metatype/metatype.properties
URL: 
http://svn.apache.org/viewvc/incubator/stanbol/trunk/enhancer/engines/geonames/src/main/resources/OSGI-INF/metatype/metatype.properties?rev=1141951&r1=1141950&r2=1141951&view=diff
==============================================================================
--- 
incubator/stanbol/trunk/enhancer/engines/geonames/src/main/resources/OSGI-INF/metatype/metatype.properties
 (original)
+++ 
incubator/stanbol/trunk/enhancer/engines/geonames/src/main/resources/OSGI-INF/metatype/metatype.properties
 Fri Jul  1 14:22:49 2011
@@ -2,6 +2,12 @@
 #Properties and Options used to configure LocationEnhancementEngine
 
#===============================================================================
 
+org.apache.stanbol.enhancer.engines.geonames.impl.LocationEnhancementEngine.name=Apache
 Stanbol Enhancement Engine for geonames.org
+org.apache.stanbol.enhancer.engines.geonames.impl.LocationEnhancementEngine.description=This
 Enhancement Engine uses the geonames.org webservices to lookup named entities 
extracted from parsed content. It only processes TextAnnotations of the dc:type 
dbpedia-ont:Place
+
+org.apache.stanbol.enhancer.engines.geonames.locationEnhancementEngine.allow-anonymous-service.name=Allow
 anonymous access
+org.apache.stanbol.enhancer.engines.geonames.locationEnhancementEngine.allow-anonymous-service.description=Geonames.org
 supports two freely available servers. "http://ws.geonames.org"; can be 
accessed anonymously and "http://api.geonames.org"; requires to authentication 
and therefore to first create a free user account via the geonamed.org webpage. 
This property is currently used to deactivate the usage of 
"http://ws.geonames.org"; by default. This has the reason that this server is 
often overloaded and errors related to this may cause the enhancement process 
to fail randomly. However for testing this engine without the requirement to 
create an user account users might want to enable this.
+
 
org.apache.stanbol.enhancer.engines.geonames.locationEnhancementEngine.min-score.name=Minimum
 score
 
org.apache.stanbol.enhancer.engines.geonames.locationEnhancementEngine.min-score.description=Only
 locations with an equals or higher score will be included as Entity 
Enhancements
 

Modified: 
incubator/stanbol/trunk/enhancer/engines/geonames/src/test/java/org/apache/stanbol/enhancer/engines/geonames/impl/TestLocationEnhancementEngine.java
URL: 
http://svn.apache.org/viewvc/incubator/stanbol/trunk/enhancer/engines/geonames/src/test/java/org/apache/stanbol/enhancer/engines/geonames/impl/TestLocationEnhancementEngine.java?rev=1141951&r1=1141950&r2=1141951&view=diff
==============================================================================
--- 
incubator/stanbol/trunk/enhancer/engines/geonames/src/test/java/org/apache/stanbol/enhancer/engines/geonames/impl/TestLocationEnhancementEngine.java
 (original)
+++ 
incubator/stanbol/trunk/enhancer/engines/geonames/src/test/java/org/apache/stanbol/enhancer/engines/geonames/impl/TestLocationEnhancementEngine.java
 Fri Jul  1 14:22:49 2011
@@ -51,6 +51,7 @@ import org.apache.stanbol.enhancer.servi
 import org.junit.AfterClass;
 import org.junit.BeforeClass;
 import org.junit.Test;
+import org.osgi.service.cm.ConfigurationException;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -84,8 +85,13 @@ public class TestLocationEnhancementEngi
     static LocationEnhancementEngine locationEnhancementEngine = new 
LocationEnhancementEngine();
 
     @BeforeClass
-    public static void setUpServices() throws IOException {
+    public static void setUpServices() throws IOException, 
ConfigurationException {
         Dictionary<String, Object> properties = new Hashtable<String, 
Object>();
+        // use the anonymous service for the unit tests
+        properties.put(LocationEnhancementEngine.GEONAMES_USERNAME, 
+            "\u0073\u0074\u0061\u006E\u0062\u006F\u006C");
+        properties.put(LocationEnhancementEngine.GEONAMES_TOKEN, 
+            "\u0073\u0074\u006E\u0062\u006C\u002E\u0075\u0074");
         MockComponentContext context = new MockComponentContext(properties);
         locationEnhancementEngine.activate(context);
     }


Reply via email to