Changed arp repository factory so that classes can be plugged in from runtime config.
authorwassa <wassa@ab3bd59b-922f-494d-bb5f-6f0a3c29deca>
Tue, 17 Sep 2002 01:08:56 +0000 (01:08 +0000)
committerwassa <wassa@ab3bd59b-922f-494d-bb5f-6f0a3c29deca>
Tue, 17 Sep 2002 01:08:56 +0000 (01:08 +0000)
(submitted by Parviz Dousti)

git-svn-id: https://subversion.switch.ch/svn/shibboleth/java-idp/trunk@299 ab3bd59b-922f-494d-bb5f-6f0a3c29deca

src/edu/internet2/middleware/shibboleth/aa/AAServlet.java
src/edu/internet2/middleware/shibboleth/aa/ArpFileFactory.java
src/edu/internet2/middleware/shibboleth/aa/ArpRepository.java
webApplication/WEB-INF/web.xml

index f3980d4..52c897e 100755 (executable)
@@ -79,7 +79,6 @@ public class AAServlet extends HttpServlet {
     String dirUrl;
     String uidSyntax;
     String arpFactoryMethod;
-    String arpFactoryData;
     String ctxFactory;
     AAResponder responder;
     HandleRepositoryFactory hrf;
@@ -100,19 +99,22 @@ public class AAServlet extends HttpServlet {
            ctxFactory = getInitParameter("ctxFactoryClass");
            if(ctxFactory == null)
                ctxFactory = "com.sun.jndi.ldap.LdapCtxFactory";
-           arpFactoryMethod = getInitParameter("arpFactoryMethod");
-           arpFactoryData = getInitParameter("arpFactoryData");
-           if(arpFactoryMethod.equalsIgnoreCase("file") &&
-              arpFactoryData == null){
-               String realPath = getServletContext().getRealPath("/");
-               realPath += "arps";
-               log.debug("shib dir = "+ realPath);
-               arpFactoryData = realPath;
-           }
-
-
-      
-           arpFactory = ArpRepository.getInstance(arpFactoryMethod, arpFactoryData);
+            // build a properties object to be handed to ArpFactories
+            // include all parameters :-(
+            Enumeration en = getInitParameterNames();
+            Properties props = new Properties();
+            while(en.hasMoreElements()){
+                String key = (String)en.nextElement();
+                String val = getInitParameter(key);
+                props.setProperty(key, val);
+            }
+            props.setProperty("arpFactoryRealPath",
+                              getServletContext().getRealPath("/"));
+
+            arpFactoryMethod = getInitParameter("arpFactoryMethod");
+
+   
+            arpFactory = ArpRepository.getInstance(arpFactoryMethod, props);
 
            log.info("Using "+ctxFactory+" as directory for attributes.");
 
index 013c8bb..e8de130 100755 (executable)
 
 package edu.internet2.middleware.shibboleth.aa;
 
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileNotFoundException;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.io.ObjectInput;
+import java.io.ObjectInputStream;
+import java.io.ObjectOutput;
+import java.io.ObjectOutputStream;
+import java.security.acl.NotOwnerException;
+import java.util.Date;
+import java.util.Properties;
+
+import org.apache.log4j.Logger;
+
 /**
  *  Attribute Authority & Release Policy
  *  File implementation of a repository for ARPs
@@ -57,110 +72,113 @@ package edu.internet2.middleware.shibboleth.aa;
  * @created    June, 2002
  */
 
+public class ArpFileFactory implements ArpFactory {
+
+       static String dataStore;
+       private static Logger log = Logger.getLogger(ArpFileFactory.class.getName());
+       public ArpFileFactory(Properties props) {
+               String pathData = props.getProperty("arpFactoryData");
+               if (pathData == null) {
+                       String realPath = props.getProperty("arpFactoryRealPath");
+                       realPath += "arps";
+                       log.debug("shib dir = " + realPath);
+                       pathData = realPath;
+               }
+               dataStore = pathData;
+       }
 
-import java.io.*;
-import java.util.Date;
-import java.security.acl.*;
-import java.security.Principal;
-import org.apache.log4j.Logger;
-
-public class ArpFileFactory implements ArpFactory{
-
-    static String dataStore;
-    private static Logger log = Logger.getLogger(ArpFileFactory.class.getName());    
-    public ArpFileFactory(String pathData){
-       dataStore = pathData;
-    }
-
-    /**
-     * returns an Arp instance. It tries to retrieve the Arp from file system
-     * If not found then creates a new emplty Arp.  
-     * Arp can be check by its isNew() to see how it was generated
-     */
-
-    public Arp getInstance(String arpName, boolean isAdmin)
-    throws AAException{
-       try{
-
-           String fileName = dataStore+System.getProperty("file.separator")+arpName;
-           log.info("AA: Looking for ARP "+fileName);
-
-           FileInputStream f = new FileInputStream(fileName);
-           ObjectInput s = new ObjectInputStream(f);
-           Arp arp = (Arp)s.readObject();
-           if(!arpName.equals(arp.getName()))
-              throw new AAException("Wrong ARP name.  ARP maybe renamed in datastore. ");
-           arp.setNew(false);
-           arp.setLastRead(new Date());
-           log.info("AA: Found and using ARP "+arpName);
-           return arp;
-           
-       }catch(FileNotFoundException e){
-           // check the IO error to make sure "file not found"
-           log.info("AA: Got File Not Found for "+arpName+" in "+dataStore);
-           try{
-               Arp arp = new Arp(arpName, isAdmin);
-               arp.setNew(true);
-               arp.setLastRead(new Date());
-               return arp;
-           }catch(NotOwnerException noe){
-               throw new AAException("Cannot create an ARP. Not owner.");
-           }
-
-       }catch(IOException fe){
-           throw new AAException("Reading ARP failed: "+fe);
-       }catch(ClassNotFoundException ce){
-           throw new AAException("ARP retrival failed: "+ce);
-       }catch(Exception oe){
-           throw new AAException(oe.toString());
+       /**
+        * returns an Arp instance. It tries to retrieve the Arp from file system
+        * If not found then creates a new emplty Arp.  
+        * Arp can be check by its isNew() to see how it was generated
+        */
+
+       public Arp getInstance(String arpName, boolean isAdmin) throws AAException {
+               try {
+
+                       String fileName = dataStore + System.getProperty("file.separator") + arpName;
+                       log.info("AA: Looking for ARP " + fileName);
+
+                       FileInputStream f = new FileInputStream(fileName);
+                       ObjectInput s = new ObjectInputStream(f);
+                       Arp arp = (Arp) s.readObject();
+                       if (!arpName.equals(arp.getName()))
+                               throw new AAException("Wrong ARP name.  ARP maybe renamed in datastore. ");
+                       arp.setNew(false);
+                       arp.setLastRead(new Date());
+                       log.info("AA: Found and using ARP " + arpName);
+                       return arp;
+
+               } catch (FileNotFoundException e) {
+                       // check the IO error to make sure "file not found"
+                       log.info("AA: Got File Not Found for " + arpName + " in " + dataStore);
+                       try {
+                               Arp arp = new Arp(arpName, isAdmin);
+                               arp.setNew(true);
+                               arp.setLastRead(new Date());
+                               return arp;
+                       } catch (NotOwnerException noe) {
+                               throw new AAException("Cannot create an ARP. Not owner.");
+                       }
+
+               } catch (IOException fe) {
+                       throw new AAException("Reading ARP failed: " + fe);
+               } catch (ClassNotFoundException ce) {
+                       throw new AAException("ARP retrival failed: " + ce);
+               } catch (Exception oe) {
+                       throw new AAException(oe.toString());
+               }
        }
-    }
-
-    public void write(Arp arp) throws AAException{
-       // XXX do we need to check any permissions?
-       try{
-           String fileName = dataStore+System.getProperty("file.separator")+arp.getName();
-           FileOutputStream f = new FileOutputStream(fileName);
-           ObjectOutput s = new ObjectOutputStream(f);
-           arp.setNew(false);
-           s.writeObject(arp);
-           s.flush();      
-       }catch(IOException e){
-           throw new AAException("IO Problem:"+e);
+
+       public void write(Arp arp) throws AAException {
+               // XXX do we need to check any permissions?
+               try {
+                       String fileName = dataStore + System.getProperty("file.separator") + arp.getName();
+                       FileOutputStream f = new FileOutputStream(fileName);
+                       ObjectOutput s = new ObjectOutputStream(f);
+                       arp.setNew(false);
+                       s.writeObject(arp);
+                       s.flush();
+               } catch (IOException e) {
+                       throw new AAException("IO Problem:" + e);
+               }
        }
-    }
-
-    /**
-     * Reread the arp from file system if the copy on disk
-     * is newer than the copy in memory.
-     */
-
-    public Arp reread(Arp arp) throws AAException{
-       String fileName = dataStore+System.getProperty("file.separator")+arp.getName();
-       File file = new File(fileName);
-       if(file == null)
-           throw new AAException("Arp not found on disk while trying to re-read. :"+arp);
-       Date timeStamp = new Date(file.lastModified());
-       log.info("AA: Check ARP's freshness: in memory ("+arp.getLastRead()+") vs on disk ("+timeStamp+")");
-       if(timeStamp.after(arp.getLastRead())){
-           log.info("AA: ARP has been modified on disk. Re-read "+arp.getName());
-           return getInstance(arp.getName(), arp.isAdmin());
+
+       /**
+        * Reread the arp from file system if the copy on disk
+        * is newer than the copy in memory.
+        */
+
+       public Arp reread(Arp arp) throws AAException {
+               String fileName = dataStore + System.getProperty("file.separator") + arp.getName();
+               File file = new File(fileName);
+               if (file == null)
+                       throw new AAException("Arp not found on disk while trying to re-read. :" + arp);
+               Date timeStamp = new Date(file.lastModified());
+               log.info(
+                       "AA: Check ARP's freshness: in memory ("
+                               + arp.getLastRead()
+                               + ") vs on disk ("
+                               + timeStamp
+                               + ")");
+               if (timeStamp.after(arp.getLastRead())) {
+                       log.info("AA: ARP has been modified on disk. Re-read " + arp.getName());
+                       return getInstance(arp.getName(), arp.isAdmin());
+               }
+               return arp; // return the old one.
        }
-       return arp;  // return the old one.
-    }
-
-    public void remove(Arp arp) throws AAException{
-       try{
-           String fileName = dataStore+System.getProperty("file.separator")+arp.getName();
-           File f = new File(fileName);
-           f.delete();
-       }catch(Exception e){
-           throw new AAException("IO Problem:"+e);
+
+       public void remove(Arp arp) throws AAException {
+               try {
+                       String fileName = dataStore + System.getProperty("file.separator") + arp.getName();
+                       File f = new File(fileName);
+                       f.delete();
+               } catch (Exception e) {
+                       throw new AAException("IO Problem:" + e);
+               }
        }
-    }
 
-    public String toString(){
-       return "ArpFileFactory:dir="+dataStore;
-    }
+       public String toString() {
+               return "ArpFileFactory:dir=" + dataStore;
+       }
 }
-
index 7e38265..13b3875 100755 (executable)
@@ -56,26 +56,43 @@ package edu.internet2.middleware.shibboleth.aa;
  * @author     Parviz Dousti (dousti@cmu.edu)
  * @created    June, 2002
  */
+import java.util.*;
+import java.lang.reflect.*;
 
-public class ArpRepository{
 
+public class ArpRepository{
 
     /**
-     * This is a method to allow implementation of different 
+     * This is a class to allow implementation of different 
      * repositories for ARPs. e.g. File system, SQL database, or LDAP
-     * It returns an implementation based on the given method.  
-     * It passes the given data string to the implementation.  Data string is 
-     * opeque and only meaningful to the specific implementation.
-     * e.g. it might be a directory path to file system implementation.
+     * It returns an implementation based on the given class name.  
+     * It passes a Properties object with all configured key-value pairs 
+     * Each implementation can extract their own configuration values
+     * from Properties.
      */
 
-    public static ArpFactory getInstance(String method, String pathData)
-       throws AAException{
-       if(method.equalsIgnoreCase("file"))
-           return new ArpFileFactory(pathData);
-       else
-           throw new AAException("Unknown repository or not implemented yet:" +method);
+    public static ArpFactory getInstance(String implementor, Properties props)
+        throws AAException{
 
+        try{
+            Class implementorClass = Class.forName(implementor);
+            Class[] params = new Class[1];
+            params[0] = Class.forName("java.util.Properties");
+            Constructor implementorConstructor = implementorClass.getConstructor(params);
+            Object[] args = new Object[1];
+            args[0] = props;
+            return (ArpFactory) implementorConstructor.newInstance(args);
+
+        }catch(ClassNotFoundException cnfe){
+            throw new AAException("Failed to instantiate an ArpFactory: "+cnfe);
+        }catch(InstantiationException ie){
+            throw new AAException("Failed to instantiate an ArpFactory: "+ie);
+        }catch(IllegalAccessException iae){
+            throw new AAException("Failed to instantiate an ArpFactory: "+iae);
+        }catch(NoSuchMethodException nsme){
+            throw new AAException("Failed to instantiate an ArpFactory: "+nsme);
+        }catch(InvocationTargetException ite){
+            throw new AAException("Failed to instantiate an ArpFactory: "+ite);
+        }
     }
 }
-
index 8f805c3..4f1653c 100755 (executable)
                </init-param>
                <init-param>
                    <param-name>arpFactoryMethod</param-name>
-                   <param-value>file</param-value>
+                   <param-value>edu.internet2.middleware.shibboleth.aa.ArpFileFactory</param-value>
                </init-param>
        </servlet>
        <servlet>