You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@manifoldcf.apache.org by kw...@apache.org on 2013/12/03 03:04:00 UTC

svn commit: r1547262 - /manifoldcf/branches/CONNECTORS-781/framework/core/src/main/java/org/apache/manifoldcf/core/interfaces/ConnectorFactory.java

Author: kwright
Date: Tue Dec  3 02:03:59 2013
New Revision: 1547262

URL: http://svn.apache.org/r1547262
Log:
First cut of intended base class of all connector factories.

Added:
    manifoldcf/branches/CONNECTORS-781/framework/core/src/main/java/org/apache/manifoldcf/core/interfaces/ConnectorFactory.java
      - copied, changed from r1547257, manifoldcf/branches/CONNECTORS-781/framework/agents/src/main/java/org/apache/manifoldcf/agents/interfaces/OutputConnectorFactory.java

Copied: manifoldcf/branches/CONNECTORS-781/framework/core/src/main/java/org/apache/manifoldcf/core/interfaces/ConnectorFactory.java (from r1547257, manifoldcf/branches/CONNECTORS-781/framework/agents/src/main/java/org/apache/manifoldcf/agents/interfaces/OutputConnectorFactory.java)
URL: http://svn.apache.org/viewvc/manifoldcf/branches/CONNECTORS-781/framework/core/src/main/java/org/apache/manifoldcf/core/interfaces/ConnectorFactory.java?p2=manifoldcf/branches/CONNECTORS-781/framework/core/src/main/java/org/apache/manifoldcf/core/interfaces/ConnectorFactory.java&p1=manifoldcf/branches/CONNECTORS-781/framework/agents/src/main/java/org/apache/manifoldcf/agents/interfaces/OutputConnectorFactory.java&r1=1547257&r2=1547262&rev=1547262&view=diff
==============================================================================
--- manifoldcf/branches/CONNECTORS-781/framework/agents/src/main/java/org/apache/manifoldcf/agents/interfaces/OutputConnectorFactory.java (original)
+++ manifoldcf/branches/CONNECTORS-781/framework/core/src/main/java/org/apache/manifoldcf/core/interfaces/ConnectorFactory.java Tue Dec  3 02:03:59 2013
@@ -16,28 +16,25 @@
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
-package org.apache.manifoldcf.agents.interfaces;
+package org.apache.manifoldcf.core.interfaces;
 
-import org.apache.manifoldcf.core.interfaces.*;
-import org.apache.manifoldcf.agents.system.ManifoldCF;
+import org.apache.manifoldcf.core.system.ManifoldCF;
 
 import java.util.*;
 import java.io.*;
 import java.lang.reflect.*;
 
-/** This is the factory class for IOutputConnector objects.
+/** This is the base factory class for all IConnector objects.
 */
-public class OutputConnectorFactory
+public class ConnectorFactory
 {
   public static final String _rcsid = "@(#)$Id: OutputConnectorFactory.java 988245 2010-08-23 18:39:35Z kwright $";
 
   // Pool hash table.
   // Keyed by PoolKey; value is Pool
-  protected static Map poolHash = new HashMap();
+  protected final static Map<PoolKey,Pool> poolHash = new HashMap<PoolKey,Pool>();
 
-  // private static HashMap checkedOutConnectors = new HashMap();
-
-  private OutputConnectorFactory()
+  private ConnectorFactory()
   {
   }
 
@@ -47,7 +44,7 @@ public class OutputConnectorFactory
   public static void install(IThreadContext threadContext, String className)
     throws ManifoldCFException
   {
-    IOutputConnector connector = getConnectorNoCheck(className);
+    IConnector connector = getConnectorNoCheck(className);
     connector.install(threadContext);
   }
 
@@ -57,32 +54,17 @@ public class OutputConnectorFactory
   public static void deinstall(IThreadContext threadContext, String className)
     throws ManifoldCFException
   {
-    IOutputConnector connector = getConnectorNoCheck(className);
+    IConnector connector = getConnectorNoCheck(className);
     connector.deinstall(threadContext);
   }
 
-  /** Get the activities supported by this connector.
-  *@param className is the class name.
-  *@return the list of activities.
-  */
-  public static String[] getActivitiesList(IThreadContext threadContext, String className)
-    throws ManifoldCFException
-  {
-    IOutputConnector connector = getConnector(threadContext, className);
-    if (connector == null)
-      return null;
-    String[] values = connector.getActivitiesList();
-    java.util.Arrays.sort(values);
-    return values;
-  }
-
   /** Output the configuration header section.
   */
   public static void outputConfigurationHeader(IThreadContext threadContext, String className,
     IHTTPOutput out, Locale locale, ConfigParams parameters, ArrayList tabsArray)
     throws ManifoldCFException, IOException
   {
-    IOutputConnector connector = getConnector(threadContext, className);
+    IConnector connector = getConnector(threadContext, className);
     if (connector == null)
       return;
     connector.outputConfigurationHeader(threadContext,out,locale,parameters,tabsArray);
@@ -94,7 +76,7 @@ public class OutputConnectorFactory
     IHTTPOutput out, Locale locale, ConfigParams parameters, String tabName)
     throws ManifoldCFException, IOException
   {
-    IOutputConnector connector = getConnector(threadContext, className);
+    IConnector connector = getConnector(threadContext, className);
     if (connector == null)
       return;
     connector.outputConfigurationBody(threadContext,out,locale,parameters,tabName);
@@ -106,7 +88,7 @@ public class OutputConnectorFactory
     IPostParameters variableContext, Locale locale, ConfigParams configParams)
     throws ManifoldCFException
   {
-    IOutputConnector connector = getConnector(threadContext, className);
+    IConnector connector = getConnector(threadContext, className);
     if (connector == null)
       return null;
     return connector.processConfigurationPost(threadContext,variableContext,locale,configParams);
@@ -118,18 +100,18 @@ public class OutputConnectorFactory
     IHTTPOutput out, Locale locale, ConfigParams configParams)
     throws ManifoldCFException, IOException
   {
-    IOutputConnector connector = getConnector(threadContext, className);
+    IConnector connector = getConnector(threadContext, className);
     // We want to be able to view connections even if they have unregistered connectors.
     if (connector == null)
       return;
     connector.viewConfiguration(threadContext,out,locale,configParams);
   }
 
-  /** Get an output connector instance, without checking for installed connector.
+  /** Get a connector instance, without checking for installed connector.
   *@param className is the class name.
   *@return the instance.
   */
-  public static IOutputConnector getConnectorNoCheck(String className)
+  public static IConnector getConnectorNoCheck(String className)
     throws ManifoldCFException
   {
     try
@@ -140,9 +122,9 @@ public class OutputConnectorFactory
       Constructor c = theClass.getConstructor(argumentClasses);
       Object[] arguments = new Object[0];
       Object o = c.newInstance(arguments);
-      if (!(o instanceof IOutputConnector))
-        throw new ManifoldCFException("Class '"+className+"' does not implement IOutputConnector.");
-      return (IOutputConnector)o;
+      if (!(o instanceof IConnector))
+        throw new ManifoldCFException("Class '"+className+"' does not implement IConnector.");
+      return (IConnector)o;
     }
     catch (InvocationTargetException e)
     {
@@ -151,28 +133,30 @@ public class OutputConnectorFactory
         throw (Error)z;
       else if (z instanceof RuntimeException)
         throw (RuntimeException)z;
-      else
+      else if (z instanceof ManifoldCFException)
         throw (ManifoldCFException)z;
+      else
+        throw new RuntimeException("Unknown exception type: "+z.getClass().getName()+": "+z.getMessage(),z);
     }
     catch (ClassNotFoundException e)
     {
-      throw new ManifoldCFException("No output connector class '"+className+"' was found.",
+      throw new ManifoldCFException("No connector class '"+className+"' was found.",
         e);
     }
     catch (NoSuchMethodException e)
     {
-      throw new ManifoldCFException("No appropriate constructor for IOutputConnector implementation '"+
+      throw new ManifoldCFException("No appropriate constructor for IConnector implementation '"+
         className+"'.  Need xxx().",
         e);
     }
     catch (SecurityException e)
     {
-      throw new ManifoldCFException("Protected constructor for IOutputConnector implementation '"+className+"'",
+      throw new ManifoldCFException("Protected constructor for IConnector implementation '"+className+"'",
         e);
     }
     catch (IllegalAccessException e)
     {
-      throw new ManifoldCFException("Unavailable constructor for IOutputConnector implementation '"+className+"'",
+      throw new ManifoldCFException("Unavailable constructor for IConnector implementation '"+className+"'",
         e);
     }
     catch (IllegalArgumentException e)
@@ -181,26 +165,35 @@ public class OutputConnectorFactory
     }
     catch (InstantiationException e)
     {
-      throw new ManifoldCFException("InstantiationException for IOutputConnector implementation '"+className+"'",
+      throw new ManifoldCFException("InstantiationException for IConnector implementation '"+className+"'",
         e);
     }
     catch (ExceptionInInitializerError e)
     {
-      throw new ManifoldCFException("ExceptionInInitializerError for IOutputConnector implementation '"+className+"'",
+      throw new ManifoldCFException("ExceptionInInitializerError for IConnector implementation '"+className+"'",
         e);
     }
 
   }
 
-  /** Get an output connector instance.
+  // Protected methods
+  
+  /** Override this method to hook into a connector manager.
+  */
+  protected static boolean isInstalled(IThreadContext tc, String className)
+    throws ManifoldCFException
+  {
+    return false;
+  }
+  
+  /** Get a connector instance.
   *@param className is the class name.
   *@return the instance.
   */
-  protected static IOutputConnector getConnector(IThreadContext threadContext, String className)
+  protected static IConnector getConnector(IThreadContext threadContext, String className)
     throws ManifoldCFException
   {
-    IOutputConnectorManager connMgr = OutputConnectorManagerFactory.make(threadContext);
-    if (connMgr.isInstalled(className) == false)
+    if (!isInstalled(threadContext,className))
       return null;
 
     try
@@ -211,9 +204,9 @@ public class OutputConnectorFactory
       Constructor c = theClass.getConstructor(argumentClasses);
       Object[] arguments = new Object[0];
       Object o = c.newInstance(arguments);
-      if (!(o instanceof IOutputConnector))
-        throw new ManifoldCFException("Class '"+className+"' does not implement IOutputConnector.");
-      return (IOutputConnector)o;
+      if (!(o instanceof IConnector))
+        throw new ManifoldCFException("Class '"+className+"' does not implement IConnector.");
+      return (IConnector)o;
     }
     catch (InvocationTargetException e)
     {
@@ -222,33 +215,35 @@ public class OutputConnectorFactory
         throw (Error)z;
       else if (z instanceof RuntimeException)
         throw (RuntimeException)z;
-      else
+      else if (z instanceof ManifoldCFException)
         throw (ManifoldCFException)z;
+      else
+        throw new RuntimeException("Unknown exception type: "+z.getClass().getName()+": "+z.getMessage(),z);
     }
     catch (ClassNotFoundException e)
     {
       // This MAY mean that an existing connector has been uninstalled; check out this possibility!
       // We return null because that is the signal that we cannot get a connector instance for that reason.
-      if (connMgr.isInstalled(className) == false)
+      if (!isInstalled(threadContext,className))
         return null;
 
-      throw new ManifoldCFException("No output connector class '"+className+"' was found.",
+      throw new ManifoldCFException("No connector class '"+className+"' was found.",
         e);
     }
     catch (NoSuchMethodException e)
     {
-      throw new ManifoldCFException("No appropriate constructor for IOutputConnector implementation '"+
+      throw new ManifoldCFException("No appropriate constructor for IConnector implementation '"+
         className+"'.  Need xxx(ConfigParams).",
         e);
     }
     catch (SecurityException e)
     {
-      throw new ManifoldCFException("Protected constructor for IOutputConnector implementation '"+className+"'",
+      throw new ManifoldCFException("Protected constructor for IConnector implementation '"+className+"'",
         e);
     }
     catch (IllegalAccessException e)
     {
-      throw new ManifoldCFException("Unavailable constructor for IOutputConnector implementation '"+className+"'",
+      throw new ManifoldCFException("Unavailable constructor for IConnector implementation '"+className+"'",
         e);
     }
     catch (IllegalArgumentException e)
@@ -257,46 +252,43 @@ public class OutputConnectorFactory
     }
     catch (InstantiationException e)
     {
-      throw new ManifoldCFException("InstantiationException for IOutputConnector implementation '"+className+"'",
+      throw new ManifoldCFException("InstantiationException for IConnector implementation '"+className+"'",
         e);
     }
     catch (ExceptionInInitializerError e)
     {
-      throw new ManifoldCFException("ExceptionInInitializerError for IOutputConnector implementation '"+className+"'",
+      throw new ManifoldCFException("ExceptionInInitializerError for IConnector implementation '"+className+"'",
         e);
     }
 
   }
 
-  /** Get multiple output connectors, all at once.  Do this in a particular order
+  /** Get multiple connectors, all at once.  Do this in a particular order
   * so that any connector exhaustion will not cause a deadlock.
   */
-  public static IOutputConnector[] grabMultiple(IThreadContext threadContext,
+  public static IConnector[] grabMultiple(IThreadContext threadContext,
     String[] orderingKeys, String[] classNames, ConfigParams[] configInfos, int[] maxPoolSizes)
     throws ManifoldCFException
   {
-    IOutputConnector[] rval = new IOutputConnector[classNames.length];
-    HashMap orderMap = new HashMap();
-    int i = 0;
-    while (i < orderingKeys.length)
+    IConnector[] rval = new IConnector[classNames.length];
+    Map<String,Integer> orderMap = new HashMap<String,Integer>();
+    for (int i = 0; i < orderingKeys.length; i++)
     {
       if (orderMap.get(orderingKeys[i]) != null)
         throw new ManifoldCFException("Found duplicate order key");
       orderMap.put(orderingKeys[i],new Integer(i));
-      i++;
     }
     java.util.Arrays.sort(orderingKeys);
-    i = 0;
-    while (i < orderingKeys.length)
+    for (int i = 0; i < orderingKeys.length; i++)
     {
       String orderingKey = orderingKeys[i];
-      int index = ((Integer)orderMap.get(orderingKey)).intValue();
+      int index = orderMap.get(orderingKey).intValue();
       String className = classNames[index];
       ConfigParams cp = configInfos[index];
       int maxPoolSize = maxPoolSizes[index];
       try
       {
-        IOutputConnector connector = grab(threadContext,className,cp,maxPoolSize);
+        IConnector connector = grab(threadContext,className,cp,maxPoolSize);
         rval[index] = connector;
       }
       catch (Throwable e)
@@ -305,7 +297,7 @@ public class OutputConnectorFactory
         {
           i--;
           orderingKey = orderingKeys[i];
-          index = ((Integer)orderMap.get(orderingKey)).intValue();
+          index = orderMap.get(orderingKey).intValue();
           try
           {
             release(rval[index]);
@@ -318,21 +310,23 @@ public class OutputConnectorFactory
           throw (ManifoldCFException)e;
         else if (e instanceof RuntimeException)
           throw (RuntimeException)e;
-        throw (Error)e;
+        else if (e instanceof Error)
+          throw (Error)e;
+        else
+          throw new RuntimeException("Unexpected exception type: "+e.getClass().getName()+": "+e.getMessage(),e);
       }
-      i++;
     }
     return rval;
   }
 
-  /** Get an output connector.
+  /** Get a connector.
   * The connector is specified by its class and its parameters.
   *@param threadContext is the current thread context.
   *@param className is the name of the class to get a connector for.
   *@param configInfo are the name/value pairs constituting configuration info
   * for this class.
   */
-  public static IOutputConnector grab(IThreadContext threadContext,
+  public static IConnector grab(IThreadContext threadContext,
     String className, ConfigParams configInfo, int maxPoolSize)
     throws ManifoldCFException
   {
@@ -346,7 +340,7 @@ public class OutputConnectorFactory
     Pool p;
     synchronized (poolHash)
     {
-      p = (Pool)poolHash.get(pk);
+      p = poolHash.get(pk);
       if (p == null)
       {
         pk = new PoolKey(className,configInfo.duplicate());
@@ -355,7 +349,7 @@ public class OutputConnectorFactory
       }
     }
 
-    IOutputConnector rval = p.getConnector(threadContext);
+    IConnector rval = p.getConnector(threadContext);
 
     return rval;
 
@@ -363,14 +357,14 @@ public class OutputConnectorFactory
 
   /** Release multiple output connectors.
   */
-  public static void releaseMultiple(IOutputConnector[] connectors)
+  public static void releaseMultiple(IConnector[] connectors)
     throws ManifoldCFException
   {
     int i = 0;
     ManifoldCFException currentException = null;
     while (i < connectors.length)
     {
-      IOutputConnector c = connectors[i++];
+      IConnector c = connectors[i++];
       try
       {
         release(c);
@@ -388,7 +382,7 @@ public class OutputConnectorFactory
   /** Release an output connector.
   *@param connector is the connector to release.
   */
-  public static void release(IOutputConnector connector)
+  public static void release(IConnector connector)
     throws ManifoldCFException
   {
     // If the connector is null, skip the release, because we never really got the connector in the first place.
@@ -400,7 +394,7 @@ public class OutputConnectorFactory
     Pool p;
     synchronized (poolHash)
     {
-      p = (Pool)poolHash.get(pk);
+      p = poolHash.get(pk);
     }
 
     p.releaseConnector(connector);
@@ -423,10 +417,10 @@ public class OutputConnectorFactory
     // Go through the whole pool and notify everyone
     synchronized (poolHash)
     {
-      Iterator iter = poolHash.values().iterator();
+      Iterator<Pool> iter = poolHash.values().iterator();
       while (iter.hasNext())
       {
-        Pool p = (Pool)iter.next();
+        Pool p = iter.next();
         p.pollAll(threadContext);
       }
     }
@@ -446,10 +440,10 @@ public class OutputConnectorFactory
     // Go through the whole pool and clean it out
     synchronized (poolHash)
     {
-      Iterator iter = poolHash.values().iterator();
+      Iterator<Pool> iter = poolHash.values().iterator();
       while (iter.hasNext())
       {
-        Pool p = (Pool)iter.next();
+        Pool p = iter.next();
         p.releaseAll(threadContext);
       }
     }
@@ -459,8 +453,8 @@ public class OutputConnectorFactory
   */
   public static class PoolKey
   {
-    protected String className;
-    protected ConfigParams configInfo;
+    protected final String className;
+    protected final ConfigParams configInfo;
 
     /** Constructor.
     */
@@ -516,8 +510,8 @@ public class OutputConnectorFactory
   */
   public static class Pool
   {
-    protected ArrayList stack = new ArrayList();
-    protected PoolKey key;
+    protected final List<IConnector> stack = new ArrayList<IConnector>();
+    protected final PoolKey key;
     protected int numFree;
 
     /** Constructor
@@ -528,11 +522,11 @@ public class OutputConnectorFactory
       numFree = maxCount;
     }
 
-    /** Grab an output connector.
+    /** Grab a connector.
     * If none exists, construct it using the information in the pool key.
     *@return the connector, or null if no connector could be connected.
     */
-    public synchronized IOutputConnector getConnector(IThreadContext threadContext)
+    public synchronized IConnector getConnector(IThreadContext threadContext)
       throws ManifoldCFException
     {
       while (numFree == 0)
@@ -552,8 +546,7 @@ public class OutputConnectorFactory
         String className = key.getClassName();
         ConfigParams configParams = key.getParams();
 
-        IOutputConnectorManager connMgr = OutputConnectorManagerFactory.make(threadContext);
-        if (connMgr.isInstalled(className) == false)
+        if (!isInstalled(threadContext,className))
           return null;
 
         try
@@ -564,9 +557,9 @@ public class OutputConnectorFactory
           Constructor c = theClass.getConstructor(argumentClasses);
           Object[] arguments = new Object[0];
           Object o = c.newInstance(arguments);
-          if (!(o instanceof IOutputConnector))
-            throw new ManifoldCFException("Class '"+className+"' does not implement IOutputConnector.");
-          IOutputConnector newrc = (IOutputConnector)o;
+          if (!(o instanceof IConnector))
+            throw new ManifoldCFException("Class '"+className+"' does not implement IConnector.");
+          IConnector newrc = (IConnector)o;
           newrc.connect(configParams);
           stack.add(newrc);
         }
@@ -577,34 +570,36 @@ public class OutputConnectorFactory
             throw (Error)z;
           else if (z instanceof RuntimeException)
             throw (RuntimeException)z;
-          else
+          else if (z instanceof ManifoldCFException)
             throw (ManifoldCFException)z;
+          else
+            throw new RuntimeException("Unknown exception type: "+z.getClass().getName()+": "+z.getMessage(),z);
         }
         catch (ClassNotFoundException e)
         {
           // If we see this exception, it COULD mean that the connector was uninstalled, and we happened to get here
           // after that occurred.
           // We return null because that is the signal that we cannot get a connector instance for that reason.
-          if (connMgr.isInstalled(className) == false)
+          if (!isInstalled(threadContext,className))
             return null;
 
-          throw new ManifoldCFException("No output connector class '"+className+"' was found.",
+          throw new ManifoldCFException("No connector class '"+className+"' was found.",
             e);
         }
         catch (NoSuchMethodException e)
         {
-          throw new ManifoldCFException("No appropriate constructor for IOutputConnector implementation '"+
+          throw new ManifoldCFException("No appropriate constructor for IConnector implementation '"+
             className+"'.  Need xxx(ConfigParams).",
             e);
         }
         catch (SecurityException e)
         {
-          throw new ManifoldCFException("Protected constructor for IOutputConnector implementation '"+className+"'",
+          throw new ManifoldCFException("Protected constructor for IConnector implementation '"+className+"'",
             e);
         }
         catch (IllegalAccessException e)
         {
-          throw new ManifoldCFException("Unavailable constructor for IOutputConnector implementation '"+className+"'",
+          throw new ManifoldCFException("Unavailable constructor for IConnector implementation '"+className+"'",
             e);
         }
         catch (IllegalArgumentException e)
@@ -613,18 +608,18 @@ public class OutputConnectorFactory
         }
         catch (InstantiationException e)
         {
-          throw new ManifoldCFException("InstantiationException for IOutputConnector implementation '"+className+"'",
+          throw new ManifoldCFException("InstantiationException for IConnector implementation '"+className+"'",
             e);
         }
         catch (ExceptionInInitializerError e)
         {
-          throw new ManifoldCFException("ExceptionInInitializerError for IOutputConnector implementation '"+className+"'",
+          throw new ManifoldCFException("ExceptionInInitializerError for IConnector implementation '"+className+"'",
             e);
         }
       }
       
       // Since thread context set can fail, do that before we remove it from the pool.
-      IOutputConnector rc = (IOutputConnector)stack.get(stack.size()-1);
+      IConnector rc = (IConnector)stack.get(stack.size()-1);
       rc.setThreadContext(threadContext);
       stack.remove(stack.size()-1);
       numFree--;
@@ -632,10 +627,10 @@ public class OutputConnectorFactory
       return rc;
     }
 
-    /** Release an output connector to the pool.
+    /** Release a connector to the pool.
     *@param connector is the connector.
     */
-    public synchronized void releaseConnector(IOutputConnector connector)
+    public synchronized void releaseConnector(IConnector connector)
       throws ManifoldCFException
     {
       if (connector == null)
@@ -679,7 +674,7 @@ public class OutputConnectorFactory
       while (stack.size() > 0)
       {
         // Disconnect
-        IConnector rc = (IConnector)stack.get(stack.size()-1);
+        IConnector rc = stack.get(stack.size()-1);
         rc.setThreadContext(threadContext);
         try
         {