some jaspi spec compliance fixes, and minor pom cleanup

git-svn-id: https://svn.apache.org/repos/asf/geronimo/components/jaspi/trunk@773639 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/geronimo-jaspi/src/main/java/org/apache/geronimo/components/jaspi/AuthConfigFactoryImpl.java b/geronimo-jaspi/src/main/java/org/apache/geronimo/components/jaspi/AuthConfigFactoryImpl.java
index 82b8bbf..2e377ac 100644
--- a/geronimo-jaspi/src/main/java/org/apache/geronimo/components/jaspi/AuthConfigFactoryImpl.java
+++ b/geronimo-jaspi/src/main/java/org/apache/geronimo/components/jaspi/AuthConfigFactoryImpl.java
@@ -42,13 +42,12 @@
 /**

  * Implementation of the AuthConfigFactory.

  *

- *

  * @version $Rev: $ $Date: $

  */

 public class AuthConfigFactoryImpl extends AuthConfigFactory {

 

-//    private static final File DEFAULT_CONFIG_FILE = new File("config/jaspi.xml");

-    public static File staticConfigFile;// = DEFAULT_CONFIG_FILE;

+    public static final String JASPI_CONFIGURATION_FILE = "org.apache.geronimo.jaspi.configurationFile";

+    private static final File DEFAULT_CONFIG_FILE = new File("var/config/security/jaspi/jaspi.xml");

     public static CallbackHandler staticCallbackHandler;

 

     private static ClassLoader contextClassLoader;

@@ -60,14 +59,14 @@
 

     static {

         contextClassLoader = java.security.AccessController

-                        .doPrivileged(new java.security.PrivilegedAction<ClassLoader>() {

-                            public ClassLoader run() {

-                                return Thread.currentThread().getContextClassLoader();

-                            }

-                        });

+                .doPrivileged(new java.security.PrivilegedAction<ClassLoader>() {

+                    public ClassLoader run() {

+                        return Thread.currentThread().getContextClassLoader();

+                    }

+                });

     }

 

-    public AuthConfigFactoryImpl(ClassLoaderLookup classLoaderLookup, CallbackHandler callbackHandler, File configFile) throws AuthException {

+    public AuthConfigFactoryImpl(ClassLoaderLookup classLoaderLookup, CallbackHandler callbackHandler, File configFile) {

         JaspiXmlUtil.initialize(classLoaderLookup, callbackHandler);

         this.classLoaderLookup = classLoaderLookup;

         this.callbackHandler = callbackHandler;

@@ -75,10 +74,34 @@
         loadConfig();

     }

 

-    public AuthConfigFactoryImpl() throws AuthException {

-        this(new ConstantClassLoaderLookup(contextClassLoader), staticCallbackHandler, staticConfigFile);

+    public AuthConfigFactoryImpl() {

+        this(new ConstantClassLoaderLookup(contextClassLoader), staticCallbackHandler, getConfigFile());

     }

-    

+

+    private static File getConfigFile() {

+        String fileLocation = java.security.AccessController

+                .doPrivileged(new java.security.PrivilegedAction<String>() {

+                    public String run() {

+                        return System.getProperty(JASPI_CONFIGURATION_FILE);

+                    }

+                });

+        File file;

+        if (fileLocation == null) {

+            file = DEFAULT_CONFIG_FILE;

+        } else {

+            file = new File(fileLocation);

+        }

+//        if (!file.exists()) {

+//            file.getParentFile().mkdirs();

+//            try {

+//                file.createNewFile();

+//            } catch (IOException e) {

+//                throw new SecurityException("Could not initialize jaspi configuration file");

+//            }

+//        }

+        return file;

+    }

+

     public synchronized String[] detachListener(RegistrationListener listener, String layer, String appContext) throws SecurityException {

         SecurityManager sm = System.getSecurityManager();

         if (sm != null) {

@@ -143,7 +166,7 @@
         return ids.toArray(new String[ids.size()]);

     }

 

-    public synchronized void refresh() throws AuthException, SecurityException {

+    public synchronized void refresh() throws SecurityException {

         SecurityManager sm = System.getSecurityManager();

         if (sm != null) {

             sm.checkPermission(new AuthPermission("refreshAuth"));

@@ -151,7 +174,7 @@
         loadConfig();

     }

 

-    public String registerConfigProvider(AuthConfigProvider authConfigProvider, String layer, String appContext, String description) throws AuthException, SecurityException {

+    public String registerConfigProvider(AuthConfigProvider authConfigProvider, String layer, String appContext, String description) throws SecurityException {

         SecurityManager sm = System.getSecurityManager();

         if (sm != null) {

             sm.checkPermission(new AuthPermission("registerAuthConfigProvider"));

@@ -159,7 +182,7 @@
         return registerConfigProvider(authConfigProvider, layer, appContext, description, false, null, null);

     }

 

-    public synchronized String registerConfigProvider(final String className, final Map constructorParam, String layer, String appContext, String description) throws AuthException, SecurityException {

+    public synchronized String registerConfigProvider(final String className, final Map constructorParam, String layer, String appContext, String description) throws SecurityException {

         SecurityManager sm = System.getSecurityManager();

         if (sm != null) {

             sm.checkPermission(new AuthPermission("registerAuthConfigProvider"));

@@ -169,12 +192,12 @@
         return key;

     }

 

-    private String registerConfigProvider(AuthConfigProvider provider, String layer, String appContext, String description, boolean persistent, Map<String, String> constructorParam, String className) throws AuthException {

+    private String registerConfigProvider(AuthConfigProvider provider, String layer, String appContext, String description, boolean persistent, Map<String, String> constructorParam, String className) {

         String key = ConfigProviderType.getRegistrationKey(layer, appContext);

         // Get or create context

         ConfigProviderType ctx = getRegistrations().get(key);

         if (ctx == null) {

-            ctx = new ConfigProviderType(layer, appContext, persistent);

+            ctx = new ConfigProviderType(layer, appContext, persistent, persistent? null: this);

             getRegistrations().put(key, ctx);

         } else {

             if (persistent != ctx.isPersistent()) {

@@ -212,11 +235,7 @@
             sm.checkPermission(new AuthPermission("removeAuthRegistration"));

         }

         ConfigProviderType ctx = getRegistrations().remove(registrationID);

-        try {

-            saveConfig();

-        } catch (AuthException e) {

-            throw new SecurityException(e);

-        }

+        saveConfig();

         if (ctx != null) {

             List<RegistrationListener> listeners = ctx.getListeners();

             for (RegistrationListener listener : listeners) {

@@ -226,31 +245,31 @@
         }

         return false;

     }

-    

-    private void loadConfig() throws AuthException {

-        if (configFile != null) {

-        try {

-            FileReader in = new FileReader(configFile);

+

+    private void loadConfig() {

+        if (configFile != null && configFile.length() > 0) {

             try {

-                jaspiType = JaspiXmlUtil.loadJaspi(in);

-            } finally {

-                in.close();

+                FileReader in = new FileReader(configFile);

+                try {

+                    jaspiType = JaspiXmlUtil.loadJaspi(in);

+                } finally {

+                    in.close();

+                }

+            } catch (ParserConfigurationException e) {

+                throw new SecurityException("Could not read config", e);

+            } catch (IOException e) {

+                throw new SecurityException("Could not read config", e);

+            } catch (SAXException e) {

+                throw new SecurityException("Could not read config", e);

+            } catch (JAXBException e) {

+                throw new SecurityException("Could not read config", e);

+            } catch (XMLStreamException e) {

+                throw new SecurityException("Could not read config", e);

             }

-        } catch (ParserConfigurationException e) {

-            throw (AuthException)new AuthException("Could not read config").initCause(e);

-        } catch (IOException e) {

-            throw (AuthException)new AuthException("Could not read config").initCause(e);

-        } catch (SAXException e) {

-            throw (AuthException)new AuthException("Could not read config").initCause(e);

-        } catch (JAXBException e) {

-            throw (AuthException)new AuthException("Could not read config").initCause(e);

-        } catch (XMLStreamException e) {

-            throw (AuthException)new AuthException("Could not read config").initCause(e);

-        }

         }

     }

-    

-    private void saveConfig() throws AuthException {

+

+    private void saveConfig() {

         if (configFile != null) {

             try {

                 FileWriter out = new FileWriter(configFile);

@@ -260,14 +279,14 @@
                     out.close();

                 }

             } catch (IOException e) {

-                throw (AuthException)new AuthException("Could not write config").initCause(e);

+                throw new SecurityException("Could not write config", e);

             } catch (XMLStreamException e) {

-                throw (AuthException)new AuthException("Could not write config").initCause(e);

+                throw new SecurityException("Could not write config", e);

             } catch (JAXBException e) {

-                throw (AuthException)new AuthException("Could not write config").initCause(e);

+                throw new SecurityException("Could not write config", e);

             }

         }

     }

-    

+

 

 }

diff --git a/geronimo-jaspi/src/main/java/org/apache/geronimo/components/jaspi/model/ClientAuthConfigType.java b/geronimo-jaspi/src/main/java/org/apache/geronimo/components/jaspi/model/ClientAuthConfigType.java
index 72f7610..c269464 100644
--- a/geronimo-jaspi/src/main/java/org/apache/geronimo/components/jaspi/model/ClientAuthConfigType.java
+++ b/geronimo-jaspi/src/main/java/org/apache/geronimo/components/jaspi/model/ClientAuthConfigType.java
@@ -296,7 +296,7 @@
             return clientAuthConfigType.isProtected();
         }
 
-        public void refresh() throws AuthException, SecurityException {
+        public void refresh() throws SecurityException {
         }
     }
 }
diff --git a/geronimo-jaspi/src/main/java/org/apache/geronimo/components/jaspi/model/ConfigProviderType.java b/geronimo-jaspi/src/main/java/org/apache/geronimo/components/jaspi/model/ConfigProviderType.java
index 1965e7e..54018a7 100644
--- a/geronimo-jaspi/src/main/java/org/apache/geronimo/components/jaspi/model/ConfigProviderType.java
+++ b/geronimo-jaspi/src/main/java/org/apache/geronimo/components/jaspi/model/ConfigProviderType.java
@@ -117,6 +117,8 @@
     protected String classLoaderName;
 
     @XmlTransient
+    private AuthConfigFactory authConfigFactory;
+    @XmlTransient
     private final List<RegistrationListener> listeners = new ArrayList<RegistrationListener>();
     @XmlTransient
     private AuthConfigProvider provider;
@@ -125,10 +127,11 @@
     public ConfigProviderType() {
     }
 
-    public ConfigProviderType(String messageLayer, String appContext, boolean persistent) {
+    public ConfigProviderType(String messageLayer, String appContext, boolean persistent, AuthConfigFactory authConfigFactory) {
         this.messageLayer = messageLayer;
         this.appContext = appContext;
         this.persistent = persistent;
+        this.authConfigFactory = authConfigFactory;
     }
 
     /**
@@ -353,7 +356,7 @@
         this.classLoaderName = classLoaderName;
     }
 
-    public void initialize(ClassLoaderLookup classLoaderLookup, CallbackHandler callbackHandler) throws AuthException {
+    public void initialize(ClassLoaderLookup classLoaderLookup, CallbackHandler callbackHandler) {
         if (className == null) {
             provider = new ConfigProviderImpl(getClientAuthConfig(), getServerAuthConfig(), classLoaderLookup);
         } else {
@@ -363,20 +366,20 @@
                 .doPrivileged(new PrivilegedExceptionAction<AuthConfigProvider>() {
                     public AuthConfigProvider run() throws ClassNotFoundException, SecurityException, NoSuchMethodException, IllegalArgumentException, InstantiationException, IllegalAccessException, InvocationTargetException {
                         Class<? extends AuthConfigProvider> cl = (Class<? extends AuthConfigProvider>) Class.forName(className, true, classLoader);
-                        Constructor<? extends AuthConfigProvider> cnst = cl.getConstructor(Map.class);
-                        return cnst.newInstance(properties);
+                        Constructor<? extends AuthConfigProvider> cnst = cl.getConstructor(Map.class, AuthConfigFactory.class);
+                        return cnst.newInstance(properties, authConfigFactory);
                     }
                 });
             } catch (PrivilegedActionException e) {
                 Exception inner = e.getException();
                 if (inner instanceof InstantiationException) {
-                    throw (AuthException) new AuthException("AuthConfigFactory error:"
-                                    + inner.getCause().getMessage()).initCause(inner.getCause());
+                    throw new SecurityException("AuthConfigFactory error:"
+                                    + inner.getCause().getMessage(), inner.getCause());
                 } else {
-                    throw (AuthException) new AuthException("AuthConfigFactory error: " + inner).initCause(inner);
+                    throw new SecurityException("AuthConfigFactory error: " + inner, inner);
                 }
             } catch (Exception e) {
-                throw (AuthException) new AuthException("AuthConfigFactory error: " + e).initCause(e);
+                throw new SecurityException("AuthConfigFactory error: " + e, e);
             }
         }
     }
@@ -459,7 +462,7 @@
             throw new AuthException("No suitable ServerAuthConfig");
         }
 
-        public void refresh() throws AuthException, SecurityException {
+        public void refresh() throws SecurityException {
         }
     }
 
diff --git a/geronimo-jaspi/src/main/java/org/apache/geronimo/components/jaspi/model/ServerAuthConfigType.java b/geronimo-jaspi/src/main/java/org/apache/geronimo/components/jaspi/model/ServerAuthConfigType.java
index 8240c69..9122ccf 100644
--- a/geronimo-jaspi/src/main/java/org/apache/geronimo/components/jaspi/model/ServerAuthConfigType.java
+++ b/geronimo-jaspi/src/main/java/org/apache/geronimo/components/jaspi/model/ServerAuthConfigType.java
@@ -301,7 +301,7 @@
             return serverAuthConfigType.isProtected();
         }
 
-        public void refresh() throws AuthException, SecurityException {
+        public void refresh() throws SecurityException {
         }
     }
 }
diff --git a/geronimo-jaspi/src/test/java/org/apache/geronimo/components/jaspi/AuthConfigFactoryImplTest.java b/geronimo-jaspi/src/test/java/org/apache/geronimo/components/jaspi/AuthConfigFactoryImplTest.java
index 98f590d..f65478a 100644
--- a/geronimo-jaspi/src/test/java/org/apache/geronimo/components/jaspi/AuthConfigFactoryImplTest.java
+++ b/geronimo-jaspi/src/test/java/org/apache/geronimo/components/jaspi/AuthConfigFactoryImplTest.java
@@ -41,12 +41,12 @@
 

     protected void setUp() throws Exception {

         URL url = getClass().getClassLoader().getResource("test-jaspi.xml");

-        AuthConfigFactoryImpl.staticConfigFile = new File(url.getPath());

+        System.setProperty(AuthConfigFactoryImpl.JASPI_CONFIGURATION_FILE, url.getPath());

         CallbackHandler callbackHandler = null;

         AuthConfigFactoryImpl.staticCallbackHandler = callbackHandler;

         AuthConfigFactory.setFactory(null);

     }

-    

+

     public void testFactory() throws Exception {

         AuthConfigFactory factory1 = AuthConfigFactory.getFactory();

         assertNotNull(factory1);

@@ -54,27 +54,27 @@
         assertNotNull(factory2);

         assertSame(factory1, factory2);

     }

-    

+

     public void testBadConstructorProvider() throws Exception {

+        AuthConfigFactory factory = AuthConfigFactory.getFactory();

         try {

-            AuthConfigFactory factory = AuthConfigFactory.getFactory();

             factory.registerConfigProvider(BadConstructorProvider.class.getName(), null, "layer1", "appContext1", "description");

             fail("An exception should have been thrown");

-        } catch (AuthException e) {

-            //e.printStackTrace();

+        } catch (SecurityException e) {

+

         }

     }

-    

+

     public void testBadImplementProvider() throws Exception {

+        AuthConfigFactory factory = AuthConfigFactory.getFactory();

         try {

-            AuthConfigFactory factory = AuthConfigFactory.getFactory();

             factory.registerConfigProvider(BadImplementProvider.class.getName(), null, "layer2", "appContext2", "description");

             fail("An exception should have been thrown");

-        } catch (AuthException e) {

+        } catch (SecurityException e) {

             //e.printStackTrace();

         }

     }

-    

+

     public void testRegisterUnregister() throws Exception {

         AuthConfigFactory factory = AuthConfigFactory.getFactory();

         String regId = factory.registerConfigProvider(DummyProvider.class.getName(), null, "layer3", "appContext3", "description");

@@ -86,11 +86,11 @@
         assertEquals("description", regContext.getDescription());

 

         assertTrue(factory.removeRegistration(regId));

-        

+

         regContext = factory.getRegistrationContext(regId);

         assertNull(regContext);

     }

-    

+

     public void testProviderWithLayerAndContext() throws Exception {

         AuthConfigFactory factory = AuthConfigFactory.getFactory();

         String registrationID = factory.registerConfigProvider(DummyProvider.class.getName(), null, "layer4", "appContext4", "description");

@@ -101,7 +101,7 @@
         factory.removeRegistration(registrationID);

         assertNull(factory.getRegistrationContext(registrationID));

     }

-    

+

     public void testProviderWithLayer() throws Exception {

         AuthConfigFactory factory = AuthConfigFactory.getFactory();

         String registrationID = factory.registerConfigProvider(DummyProvider.class.getName(), null, "layer5", null, "description");

@@ -112,7 +112,7 @@
         factory.removeRegistration(registrationID);

         assertNull(factory.getRegistrationContext(registrationID));

     }

-    

+

     public void testProviderContextLayer() throws Exception {

         AuthConfigFactory factory = AuthConfigFactory.getFactory();

         String registrationID = factory.registerConfigProvider(DummyProvider.class.getName(), null, null, "appContext6", "description");

@@ -123,7 +123,7 @@
         factory.removeRegistration(registrationID);

         assertNull(factory.getRegistrationContext(registrationID));

     }

-    

+

     public void testProviderDefault() throws Exception {

         AuthConfigFactory factory = AuthConfigFactory.getFactory();

         String registrationID = factory.registerConfigProvider(DummyProvider.class.getName(), null, null, null, "description");

@@ -135,7 +135,7 @@
         factory.removeRegistration(registrationID);

         assertNull(factory.getRegistrationContext(registrationID));

     }

-    

+

     public void testListenerOnRegister() throws Exception {

         AuthConfigFactory factory = AuthConfigFactory.getFactory();

         String registrationID = factory.registerConfigProvider(DummyProvider.class.getName(), null, null, null, "description");

@@ -146,7 +146,7 @@
         factory.removeRegistration(registrationID);

         assertNull(factory.getRegistrationContext(registrationID));

     }

-    

+

     public void testListenerOnUnregister() throws Exception {

         AuthConfigFactory factory = AuthConfigFactory.getFactory();

         String regId = factory.registerConfigProvider(DummyProvider.class.getName(), null, null, null, "description");

@@ -168,7 +168,7 @@
         factory.removeRegistration(regId);

         assertTrue(listener.notified);

     }

-    

+

     public void testWrapServerAuthModule() throws Exception {

         AuthConfigFactory factory = AuthConfigFactory.getFactory();

         AuthModuleType<ServerAuthModule> authModuleType = new AuthModuleType<ServerAuthModule>();

@@ -182,12 +182,13 @@
         assertTrue(listener.notified);

     }

 

-    

+

     public static class DummyListener implements RegistrationListener {

         public boolean notified = true;

+

         public void notify(String layer, String appContext) {

             notified = true;

         }

     }

-    

+

 }

diff --git a/geronimo-jaspi/src/test/java/org/apache/geronimo/components/jaspi/providers/DummyProvider.java b/geronimo-jaspi/src/test/java/org/apache/geronimo/components/jaspi/providers/DummyProvider.java
index f01f748..17c4441 100644
--- a/geronimo-jaspi/src/test/java/org/apache/geronimo/components/jaspi/providers/DummyProvider.java
+++ b/geronimo-jaspi/src/test/java/org/apache/geronimo/components/jaspi/providers/DummyProvider.java
@@ -23,10 +23,11 @@
 import javax.security.auth.message.config.AuthConfigProvider;

 import javax.security.auth.message.config.ClientAuthConfig;

 import javax.security.auth.message.config.ServerAuthConfig;

+import javax.security.auth.message.config.AuthConfigFactory;

 

 public class DummyProvider implements AuthConfigProvider {

 

-    public DummyProvider(Map props) {

+    public DummyProvider(Map props, AuthConfigFactory authConfigFactory) {

         

     }

     

@@ -40,7 +41,7 @@
         return null;

     }

 

-    public void refresh() throws AuthException, SecurityException {

+    public void refresh() throws SecurityException {

         // TODO Auto-generated method stub

         

     }

diff --git a/pom.xml b/pom.xml
index 5f0c40e..05a3d98 100644
--- a/pom.xml
+++ b/pom.xml
@@ -210,6 +210,11 @@
                         </execution>
                     </executions>
                 </plugin>
+              <plugin>
+                <groupId>org.codehaus.mojo</groupId>
+                <artifactId>ianal-maven-plugin</artifactId>
+                <version>1.0-alpha-1</version>
+              </plugin>
             </plugins>
         </pluginManagement>
 
@@ -233,31 +238,10 @@
                 <!--</executions>-->
             <!--</plugin>-->
 
-            <plugin>
-                <groupId>org.apache.maven.plugins</groupId>
-                <artifactId>maven-enforcer-plugin</artifactId>
-                <executions>
-                    <execution>
-                        <phase>validate</phase>
-                        <goals>
-                            <goal>enforce</goal>
-                        </goals>
-                        <configuration>
-                            <rules>
-                                <!-- Allow any Java >= 1.5, but not 1.6 or above -->
-                                <requireJavaVersion>
-                                    <version>[1.5,1.6)</version>
-                                </requireJavaVersion>
-
-                                <!-- Allow any Maven >= 2.0.5 -->
-                                <requireMavenVersion>
-                                    <version>[2.0.9,)</version>
-                                </requireMavenVersion>
-                            </rules>
-                        </configuration>
-                    </execution>
-                </executions>
-            </plugin>
+          <plugin>
+            <groupId>org.codehaus.mojo</groupId>
+            <artifactId>ianal-maven-plugin</artifactId>
+          </plugin>
 
             <plugin>
                 <groupId>org.apache.geronimo.genesis.plugins</groupId>
@@ -287,17 +271,6 @@
                     <target>1.5</target>
                 </configuration>
             </plugin>
-
-            <plugin>
-                <groupId>org.apache.maven.plugins</groupId>
-                <artifactId>maven-idea-plugin</artifactId>
-                <version>2.1</version>
-                <configuration>
-                    <jdkName>1.5</jdkName>
-                    <jdkLevel>1.5</jdkLevel>
-                    <linkModules>true</linkModules>
-                </configuration>
-            </plugin>
             <!--
                         <plugin>
                             <groupId>org.apache.felix</groupId>