[Mulgara-svn] r1723 - in trunk/src/jar: querylang/java/org/mulgara/itql querylang/java/org/mulgara/sparql util/java/org/mulgara/util

pag at mulgara.org pag at mulgara.org
Tue Jun 9 19:17:39 UTC 2009


Author: pag
Date: 2009-06-09 12:17:38 -0700 (Tue, 09 Jun 2009)
New Revision: 1723

Added:
   trunk/src/jar/util/java/org/mulgara/util/ServerInfoRef.java
Modified:
   trunk/src/jar/querylang/java/org/mulgara/itql/TqlInterpreter.java
   trunk/src/jar/querylang/java/org/mulgara/sparql/SparqlInterpreter.java
Log:
Added reflection support for accessing ServerInfo

Modified: trunk/src/jar/querylang/java/org/mulgara/itql/TqlInterpreter.java
===================================================================
--- trunk/src/jar/querylang/java/org/mulgara/itql/TqlInterpreter.java	2009-06-09 19:16:15 UTC (rev 1722)
+++ trunk/src/jar/querylang/java/org/mulgara/itql/TqlInterpreter.java	2009-06-09 19:17:38 UTC (rev 1723)
@@ -59,6 +59,7 @@
 import org.mulgara.query.*;
 import org.mulgara.query.rdf.*;
 import org.mulgara.server.Session;
+import org.mulgara.util.ServerInfoRef;
 import org.mulgara.util.URIUtil;
 
 
@@ -1496,7 +1497,7 @@
     String host = uri.getHost();
     if (host == null) return uri;
 
-    Set<String> hostnames = getHostnameAliases();
+    Set<String> hostnames = ServerInfoRef.getHostnameAliases();
     // Check with a DNS server to see if this host is recognised
     InetAddress addr = null;
     try {
@@ -1514,7 +1515,7 @@
     ) {
       // change the host name to one that is recognised
       // use the system uri to find the local host name
-      URI serverURI = getServerURI();
+      URI serverURI = ServerInfoRef.getServerURI();
       if (serverURI == null) {
         return uri;
       }
@@ -1529,50 +1530,6 @@
   }
 
 
-  /**
-   * Method to ask the ServerInfo for the local server aliases.
-   * This will return an empty set if ServerInfo is not available -
-   * ie. being run on a host which has no local database, such an an iTQL client.
-   *
-   * @return The set of server aliases as strings
-   */
-  @SuppressWarnings("unchecked")
-  private static Set<String> getHostnameAliases() {
-    Set<String> names = (Set<String>)getServerInfoProperty("HostnameAliases");
-    return (names == null) ? (Set<String>)java.util.Collections.EMPTY_SET : names;
-  }
-
-
-  /**
-   * Method to ask the ServerInfo for the local server URI.
-   * This will return null if ServerInfo is not available -
-   * ie. being run on a host which has no local database, such an an iTQL client.
-   *
-   * @return The URI of the local server, or null if this is not a server.
-   */
-  private static URI getServerURI() {
-    return (URI)getServerInfoProperty("ServerURI");
-  }
-
-
-  /**
-   * Method to get the value of a property from the ServerInfo for the local database session.
-   * This will return null if ServerInfo is not available -
-   * ie. being run on a host which has no local database, such an an TQL client.
-   *
-   * @param property The property to return, with the correct case.
-   * @return The object returned from the accessor method named, or null if ServerInfo is not available.
-   */
-  private static Object getServerInfoProperty(String property) {
-    Object o = null;
-    try {
-      Class<?> rsf = Class.forName("org.mulgara.server.ServerInfo");
-      java.lang.reflect.Method getter = rsf.getMethod("get" + property, (Class<?>[])null);
-      o = getter.invoke(null, (Object[])null);
-    } catch (Exception e) { /* no op */ }
-    return o;
-  }
-
   private static class Lexer2 extends Lexer {
   
     int commandCount = 0;

Modified: trunk/src/jar/querylang/java/org/mulgara/sparql/SparqlInterpreter.java
===================================================================
--- trunk/src/jar/querylang/java/org/mulgara/sparql/SparqlInterpreter.java	2009-06-09 19:16:15 UTC (rev 1722)
+++ trunk/src/jar/querylang/java/org/mulgara/sparql/SparqlInterpreter.java	2009-06-09 19:17:38 UTC (rev 1723)
@@ -57,7 +57,6 @@
 import org.mulgara.query.rdf.LiteralImpl;
 import org.mulgara.query.rdf.Mulgara;
 import org.mulgara.query.rdf.URIReferenceImpl;
-import org.mulgara.server.ServerInfo;
 import org.mulgara.sparql.parser.ParseException;
 import org.mulgara.sparql.parser.QueryStructure;
 import org.mulgara.sparql.parser.QueryType;
@@ -70,6 +69,7 @@
 import org.mulgara.sparql.parser.cst.Ordering;
 import org.mulgara.sparql.parser.cst.RDFLiteral;
 import org.mulgara.sparql.parser.cst.Triple;
+import org.mulgara.util.ServerInfoRef;
 import org.mulgara.util.functional.C;
 import org.mulgara.util.functional.Fn1;
 
@@ -587,7 +587,7 @@
    * @return A list of the graphs to be used by default when no graphs are specified.
    */
   List<IRIReference> getSystemDefaultGraph() {
-    URI d = ServerInfo.getDefaultGraphURI();
+    URI d = ServerInfoRef.getDefaultURI();
     if (d != null) return Collections.singletonList(new IRIReference(d));
     // can't find a configured default, use the hard-coded default
     return toIRIs(INTERNAL_DEFAULT_GRAPH_URIS);

Added: trunk/src/jar/util/java/org/mulgara/util/ServerInfoRef.java
===================================================================
--- trunk/src/jar/util/java/org/mulgara/util/ServerInfoRef.java	                        (rev 0)
+++ trunk/src/jar/util/java/org/mulgara/util/ServerInfoRef.java	2009-06-09 19:17:38 UTC (rev 1723)
@@ -0,0 +1,203 @@
+/*
+ * Copyright 2009 Fedora Commons, Inc.
+ *
+ * Licensed 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.mulgara.util;
+
+import java.lang.reflect.Field;
+import java.lang.reflect.Method;
+import java.net.URI;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.Set;
+
+import org.apache.log4j.Logger;
+
+
+/**
+ * Provides access to the global ServerInfo class when it is not available as a dependency.
+ *
+ * @created June 08, 2009
+ * @author Paul Gearon
+ * @copyright &copy; 2008 <a href="http://www.fedora-commons.org/">Fedora Commons</a>
+ */
+public class ServerInfoRef {
+
+  /** Logger.  */
+  private static final Logger logger = Logger.getLogger(ServerInfoRef.class.getName());
+
+  /** Singleton of the ServerInfo class. */
+  private static Class<?> si = null;
+
+  /** Singleton getters, stored by name. */
+  private static Map<String,Method> getters = new HashMap<String,Method>();
+
+  /** Singleton setters, stored by name. */
+  private static Map<String,Method> setters = new HashMap<String,Method>();
+
+  /**
+   * Sets the hostnames on the ServerInfo object, if it is visible.
+   *
+   * @param names The set of hostnames to set on ServerInfo
+   */
+  public static void setHostnameAliases(Set<String> names) {
+    setServerInfoProperty("HostnameAliases", names);
+  }
+
+
+  /**
+   * Method to ask the ServerInfo for the local server aliases.
+   * This will return an empty set if ServerInfo is not available -
+   * ie. being run on a host which has no local database, such an an iTQL client.
+   *
+   * @return The set of server aliases as strings
+   */
+  @SuppressWarnings("unchecked")
+  public static Set<String> getHostnameAliases() {
+    Set<String> names = (Set<String>)getServerInfoProperty("HostnameAliases");
+    return (names == null) ? (Set<String>)java.util.Collections.EMPTY_SET : names;
+  }
+
+
+  /**
+   * Gets the hostnames on the ServerInfo object, if it is visible.
+   *
+   * @return The default graph URI, used by SPARQL
+   */
+  public static URI getDefaultURI() {
+    URI u = (URI)getServerInfoProperty("DefaultGraphURI");
+    if (u == null) {
+      String c = getMulgaraConstant("DEFAULT_GRAPH");
+      u = (c != null) ? URI.create(c) : URI.create("sys:default");
+    }
+    return u;
+  }
+
+
+  /**
+   * Method to ask the ServerInfo for the local server URI.
+   * This will return null if ServerInfo is not available -
+   * ie. being run on a host which has no local database, such an an iTQL client.
+   * 
+   * @return The server URI
+   */
+  public static URI getServerURI() {
+    return (URI)getServerInfoProperty("ServerURI");
+  }
+
+
+  /**
+   * Get the ServerInfo class as a singleton.
+   * @return The instance of the ServerInfo Class object.
+   * @throws ClassNotFoundException If ServerInfo is not on the classpath.
+   */
+  private static final Class<?> getServerInfoClass() throws ClassNotFoundException {
+    if (si == null) {
+      si = Class.forName("org.mulgara.server.ServerInfo");
+    }
+    return si;
+  }
+
+
+  /**
+   * Sets a property on the ServerInfo, if it is available
+   * @param name The name of the property. Case sensitive.
+   * @param value The value of the property to be set.
+   */
+  public static final void setServerInfoProperty(String name, Object value) {
+    try {
+      Method setter = findSetter(name, value);
+      setter.invoke(null, new Object[] { value });
+    } catch (Exception e) {
+      /* Not much that can be done here */
+      logger.info("Unable to set '" + name + "' for Server Info", e);
+    }
+  }
+
+
+  /**
+   * Gets a property from the ServerInfo, if it is available
+   * @param name The name of the property. Case sensitive.
+   * @return The value, or <code>null</code> if not available.
+   */
+  public static final Object getServerInfoProperty(String name) {
+    try {
+      Method getter = findGetter(name);
+      return getter.invoke(null, new Object[] { });
+    } catch (Exception e) {
+      /* Not much that can be done here */
+      logger.info("Unable to get '" + name + "' from Server Info", e);
+      return null;
+    }
+  }
+
+
+  /**
+   * Get the Getter for a property as a singleton to be stored by name.
+   * @param name The name of the property to find the getter for.
+   * @return The method used for getting the property.
+   * @throws SecurityException If the method is not allowed to be used.
+   * @throws NoSuchMethodException If the readable property does not exist.
+   * @throws ClassNotFoundException If the ServerInfo class is not on the classpath.
+   */
+  private static final Method findGetter(String name) throws SecurityException, NoSuchMethodException, ClassNotFoundException {
+    String fullName = "get" + name;
+    Method getter = getters.get(fullName);
+    if (fullName == null) {
+      getter = getServerInfoClass().getMethod(fullName, new Class[] { });
+      getters.put(fullName, getter);
+    }
+    return getter;
+  }
+
+
+  /**
+   * Get the Setter for a property as a singleton to be stored by name.
+   * @param name The name of the property to find the setter for.
+   * @param value The value of the property to be set.
+   * @return The method used for setting the property.
+   * @throws SecurityException If the method is not allowed to be used.
+   * @throws NoSuchMethodException If the writable property does not exist.
+   * @throws ClassNotFoundException If the ServerInfo class is not on the classpath.
+   */
+  private static final Method findSetter(String name, Object value) throws SecurityException, NoSuchMethodException, ClassNotFoundException {
+    String fullName = "set" + name;
+    Method setter = setters.get(fullName);
+    if (fullName == null) {
+      setter = getServerInfoClass().getMethod(fullName, new Class[] { value.getClass() });
+      setters.put(fullName, setter);
+    }
+    return setter;
+  }
+
+
+  /**
+   * Gets a string constant from the Mulgara class.
+   * @param name The name of the constant.
+   * @return The string value of the constant, or null if this cannot be retrieved.
+   */
+  private static final String getMulgaraConstant(String name) {
+    // reflect the data out of the Mulgara class
+    String c = null;
+    try {
+      Class<?> mulgara = Class.forName("org.mulgara.query.rdf.Mulgara");
+      Field field = mulgara.getDeclaredField(name);
+      c = (String)field.get(null);
+    } catch (Exception e) {
+      logger.error("Unable to find Mulgara constants.", e);
+    }
+    return c;
+  }
+}




More information about the Mulgara-svn mailing list