You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@harmony.apache.org by le...@apache.org on 2007/08/16 06:13:47 UTC
svn commit: r566478 - in
/harmony/enhanced/classlib/branches/java6/modules/auth/src:
main/java/common/javax/security/auth/login/
test/java/common/org/apache/harmony/auth/tests/javax/security/auth/login/
Author: leoli
Date: Wed Aug 15 21:13:42 2007
New Revision: 566478
URL: http://svn.apache.org/viewvc?view=rev&rev=566478
Log:
Add Java 6 new feature in auth module (javax.security.auth.login.Configuration.getInstance and javax.security.auth.login.ConfigurationSpi) and Related modification in security module(java.security.Provider.Service.newInstance() to support javax.security.auth.login.Configuration).
Added:
harmony/enhanced/classlib/branches/java6/modules/auth/src/main/java/common/javax/security/auth/login/ConfigurationSpi.java (with props)
Modified:
harmony/enhanced/classlib/branches/java6/modules/auth/src/main/java/common/javax/security/auth/login/Configuration.java
harmony/enhanced/classlib/branches/java6/modules/auth/src/test/java/common/org/apache/harmony/auth/tests/javax/security/auth/login/ConfigurationTest.java
Modified: harmony/enhanced/classlib/branches/java6/modules/auth/src/main/java/common/javax/security/auth/login/Configuration.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/branches/java6/modules/auth/src/main/java/common/javax/security/auth/login/Configuration.java?view=diff&rev=566478&r1=566477&r2=566478
==============================================================================
--- harmony/enhanced/classlib/branches/java6/modules/auth/src/main/java/common/javax/security/auth/login/Configuration.java (original)
+++ harmony/enhanced/classlib/branches/java6/modules/auth/src/main/java/common/javax/security/auth/login/Configuration.java Wed Aug 15 21:13:42 2007
@@ -18,87 +18,308 @@
package javax.security.auth.login;
import java.security.AccessController;
+import java.security.NoSuchAlgorithmException;
+import java.security.NoSuchProviderException;
+import java.security.Provider;
+import java.security.Security;
+
import javax.security.auth.AuthPermission;
import org.apache.harmony.security.fortress.PolicyUtils;
public abstract class Configuration {
- // the current configuration
- private static Configuration configuration;
+ // the current configuration
+ private static Configuration configuration;
- // creates a AuthPermission object with a specify property
- private static final AuthPermission GET_LOGIN_CONFIGURATION = new AuthPermission(
- "getLoginConfiguration"); //$NON-NLS-1$
-
- // creates a AuthPermission object with a specify property
- private static final AuthPermission SET_LOGIN_CONFIGURATION = new AuthPermission(
- "setLoginConfiguration"); //$NON-NLS-1$
+ // creates a AuthPermission object with a specify property
+ private static final AuthPermission GET_LOGIN_CONFIGURATION = new AuthPermission(
+ "getLoginConfiguration"); //$NON-NLS-1$
- // Key to security properties, defining default configuration provider.
- private static final String LOGIN_CONFIGURATION_PROVIDER = "login.configuration.provider"; //$NON-NLS-1$
-
- /**
+ // creates a AuthPermission object with a specify property
+ private static final AuthPermission SET_LOGIN_CONFIGURATION = new AuthPermission(
+ "setLoginConfiguration"); //$NON-NLS-1$
+
+ // Key to security properties, defining default configuration provider.
+ private static final String LOGIN_CONFIGURATION_PROVIDER = "login.configuration.provider"; //$NON-NLS-1$
+
+ /**
* A marker interface for Configuration parameters.
*
* @since 1.6
*/
- public static interface Parameters {
- // a marker interface
+ public static interface Parameters {
+ // a marker interface
+ }
+
+ protected Configuration() {
+ super();
+ }
+
+ public static Configuration getConfiguration() {
+ SecurityManager sm = System.getSecurityManager();
+ if (sm != null) {
+ sm.checkPermission(GET_LOGIN_CONFIGURATION);
+ }
+ return getAccessibleConfiguration();
+ }
+
+ /**
+ * Reads name of default configuration provider from security.properties,
+ * loads the class and instantiates the provider.<br> In case of any
+ * exception, wraps it with SecurityException and throws further.
+ */
+ private static final Configuration getDefaultProvider() {
+ return AccessController
+ .doPrivileged(new PolicyUtils.ProviderLoader<Configuration>(
+ LOGIN_CONFIGURATION_PROVIDER, Configuration.class));
+ }
+
+ /**
+ * Shortcut accessor for friendly classes, to skip security checks.
+ * If active configuration was set to <code>null</code>, tries to load a default
+ * provider, so this method never returns <code>null</code>. <br>
+ * This method is synchronized with setConfiguration()
+ */
+ static Configuration getAccessibleConfiguration() {
+ Configuration current = configuration;
+ if (current == null) {
+ synchronized (Configuration.class) {
+ if (configuration == null) {
+ configuration = getDefaultProvider();
+ }
+ return configuration;
+ }
+ }
+ return current;
+ }
+
+ public static void setConfiguration(Configuration configuration) {
+ SecurityManager sm = System.getSecurityManager();
+ if (sm != null) {
+ sm.checkPermission(SET_LOGIN_CONFIGURATION);
+ }
+ Configuration.configuration = configuration;
+ }
+
+ public abstract AppConfigurationEntry[] getAppConfigurationEntry(
+ String applicationName);
+
+ /**
+ * refresh and reload the configuration
+ * @throws SecurityException
+ * if does not have the permission to refresh the Configuration
+ */
+ public void refresh() {
+ return;
}
-
- protected Configuration() {
- super();
+ /**
+ * return the type of this Configuration
+ * @return
+ * the type the type of this Configuration
+ */
+ public String getType(){
+ return null;
+ }
+
+ /**
+ * return the provider of this Configuration
+ * @return
+ * the type the provider of this Configuration
+ */
+ public Provider getProvider(){
+ return null;
+ }
+
+ /**
+ * return the parameter of this Configuration
+ * @return
+ * the type the parameter of this Configuration
+ */
+ public Parameters getParameters(){
+ return null;
}
+
+ private static class InnerConfiguration extends Configuration {
+ private ConfigurationSpi configurationSpi;
+
+ private Provider provider;
+
+ private String type;
+
+ private Configuration.Parameters parameters;
+
+ public InnerConfiguration(ConfigurationSpi configurationSpi,
+ Provider provider, String type,
+ Configuration.Parameters parameters) {
+ this.configurationSpi = configurationSpi;
+ this.provider = provider;
+ this.type = type;
+ this.parameters = parameters;
+ }
- public static Configuration getConfiguration() {
- SecurityManager sm = System.getSecurityManager();
- if (sm != null) {
- sm.checkPermission(GET_LOGIN_CONFIGURATION);
+ @Override
+ public Configuration.Parameters getParameters() {
+ return parameters;
+ }
+
+ @Override
+ public Provider getProvider() {
+ return provider;
+ }
+
+ @Override
+ public String getType() {
+ return type;
}
- return getAccessibleConfiguration();
- }
+ @Override
+ public AppConfigurationEntry[] getAppConfigurationEntry(String applicationName) {
+ return configurationSpi.engineGetAppConfigurationEntry(applicationName);
+ }
+
+ @Override
+ public void refresh() {
+ configurationSpi.engineRefresh();
+ }
+ }
+
/**
- * Reads name of default configuration provider from security.properties,
- * loads the class and instantiates the provider.<br> In case of any
- * exception, wraps it with SecurityException and throws further.
+ * Answers a Configuration object with the specified type and the specified
+ * parameter.
+ *
+ * A new Configuration object encapsulating the ConfigurationSpi
+ * implementation from the first Provider that supports the specified type
+ * is returned.
+ *
+ * @param type
+ * the specified Configuration type.
+ * @param params
+ * parameters for the Configuration, which may be null.
+ * @param provider
+ * provider which will provide Configuration
+ * @return the new Configuration object.
+ * @throws NoSuchAlgorithmException
+ * if no Provider supports a ConfigurationSpi implementation for the
+ * specified type.
+ * @throws SecurityException
+ * if the caller does not have permission to get a Configuration
+ * instance for the specified type.
+ * @throws NullPointerException
+ * if the specified type is null.
+ * @throws IllegalArgumentException
+ * if the specified parameters' type are not allowed by the
+ * ConfigurationSpi implementation from the selected Provider.
+ *
+ * @since 1.6
*/
- private static final Configuration getDefaultProvider() {
- return AccessController.doPrivileged(new PolicyUtils.ProviderLoader<Configuration>(
- LOGIN_CONFIGURATION_PROVIDER, Configuration.class));
+ public static Configuration getInstance(String type,
+ Configuration.Parameters params, Provider provider)
+ throws NoSuchAlgorithmException {
+ if (type == null) {
+ throw new NullPointerException();
+ }
+ if(provider == null){
+ throw new IllegalArgumentException();
+ }
+ Provider.Service service = provider.getService("Configuration", type);
+ if(service == null){
+ throw new NoSuchAlgorithmException(type);
+ }
+ ConfigurationSpi configurationSpi;
+ configurationSpi = (ConfigurationSpi) service.newInstance(params);
+ return new InnerConfiguration(configurationSpi, provider, type, params);
}
-
+
/**
- * Shortcut accessor for friendly classes, to skip security checks.
- * If active configuration was set to <code>null</code>, tries to load a default
- * provider, so this method never returns <code>null</code>. <br>
- * This method is synchronized with setConfiguration()
+ * Answers a Configuration object with the specified type and the specified
+ * parameter.
+ *
+ * A new Configuration object encapsulating the ConfigurationSpi
+ * implementation from the first Provider that supports the specified type
+ * is returned.
+ *
+ * @param type
+ * the specified Configuration type.
+ * @param params
+ * parameters for the Configuration, which may be null.
+ * @param provider
+ * provider which will provide Configuration
+ * @return the new Configuration object.
+ * @throws NoSuchAlgorithmException
+ * if no Provider supports a ConfigurationSpi implementation for the
+ * specified type.
+ * @throws SecurityException
+ * if the caller does not have permission to get a Configuration
+ * instance for the specified type.
+ * @throws NullPointerException
+ * if the specified type is null.
+ * @throws IllegalArgumentException
+ * if the specified parameters' type are not allowed by the
+ * ConfigurationSpi implementation from the selected Provider.
+ * @throws NoSuchProviderException
+ * if the specified provider is not registered in the provider list
+ *
+ * @since 1.6
*/
- static Configuration getAccessibleConfiguration() {
- Configuration current = configuration;
- if (current == null) {
- synchronized (Configuration.class) {
- if (configuration == null) {
- configuration = getDefaultProvider();
- }
- return configuration;
- }
+ public static Configuration getInstance(String type, Configuration.Parameters params, String provider) throws NoSuchAlgorithmException, NoSuchProviderException{
+ Provider pro = Security.getProvider(provider);
+ if(provider == null || provider.length() == 0){
+ throw new IllegalArgumentException();
+ }
+ if(pro == null){
+ throw new NoSuchProviderException();
}
- return current;
+ return getInstance(type, params, pro);
}
-
- public static void setConfiguration(Configuration configuration) {
- SecurityManager sm = System.getSecurityManager();
- if (sm != null) {
- sm.checkPermission(SET_LOGIN_CONFIGURATION);
+
+ /**
+ * Answers a Configuration object with the specified type and the specified
+ * parameter.
+ *
+ * Traverses the list of registered security providers, beginning with the
+ * most preferred Provider. A new Configuration object encapsulating the ConfigurationSpi
+ * implementation from the first Provider that supports the specified type
+ * is returned.
+ *
+ * Note that the list of registered providers may be retrieved via the
+ * Security.getProviders() method.
+ *
+ * @param type
+ * the specified Configuration type.
+ * @param params
+ * parameters for the Configuration, which may be null.
+ * @return the new Configuration object.
+ * @throws NoSuchAlgorithmException
+ * if no Provider supports a ConfigurationSpi implementation for the
+ * specified type.
+ * @throws SecurityException
+ * if the caller does not have permission to get a Configuration
+ * instance for the specified type.
+ * @throws NullPointerException
+ * if the specified type is null.
+ * @throws IllegalArgumentException
+ * if the specified parameters' type are not allowed by the
+ * ConfigurationSpi implementation from the selected Provider.
+ *
+ * @since 1.6
+ */
+ public static Configuration getInstance(String type, Configuration.Parameters params) throws NoSuchAlgorithmException{
+ Configuration configuration = null;
+ for(Provider provider: Security.getProviders()){
+ try
+ {
+ configuration = getInstance(type, params, provider);
+ break;
+ }
+ catch(NoSuchAlgorithmException e){
+
+ }
+ }
+ if(configuration == null){
+ throw new NoSuchAlgorithmException();
}
- Configuration.configuration = configuration;
+ return configuration;
}
-
- public abstract AppConfigurationEntry[] getAppConfigurationEntry(String applicationName);
-
- public abstract void refresh();
-
}
Added: harmony/enhanced/classlib/branches/java6/modules/auth/src/main/java/common/javax/security/auth/login/ConfigurationSpi.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/branches/java6/modules/auth/src/main/java/common/javax/security/auth/login/ConfigurationSpi.java?view=auto&rev=566478
==============================================================================
--- harmony/enhanced/classlib/branches/java6/modules/auth/src/main/java/common/javax/security/auth/login/ConfigurationSpi.java (added)
+++ harmony/enhanced/classlib/branches/java6/modules/auth/src/main/java/common/javax/security/auth/login/ConfigurationSpi.java Wed Aug 15 21:13:42 2007
@@ -0,0 +1,64 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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 javax.security.auth.login;
+
+
+/**
+ * Represents the Service Provider Interface (SPI) for
+ * javax.security.auth.login.Configuration class.
+ *
+ * If there is any class that wants to provide a Configuration implementation, all
+ * abstract methods in this SPI should be implemented.
+ *
+ * The detailed implementations should offer a public constructor, in which a
+ * Configuration.Paramters implementation acts as an input parameter. If the
+ * Configuration.Paramters input cannot by understood by the constructor, an
+ * IllegalArgumentException will be thrown.
+ *
+ * @since 1.6
+ */
+public abstract class ConfigurationSpi {
+
+ public ConfigurationSpi() {
+ //default constructor
+ }
+
+ /**
+ * Get AppConfigurationEntries form the specified name
+ * @param name
+ * The name to get the configuration
+ * @return
+ * an array of AppConfigurationEntries
+ */
+ protected abstract AppConfigurationEntry[] engineGetAppConfigurationEntry(
+ String name);
+
+ /**
+ * Refreshes/reloads the configuration. The behavior of this method
+ * depends on the implementation. For example, calling refresh on a
+ * file-based configuration will cause the file to be re-read.
+ *
+ * The default implementation of this method does nothing. This method
+ * should be overridden if a refresh operation is supported by the
+ * implementation.
+ *
+ */
+ protected void engineRefresh() {
+ //do nothing in default implementation
+ }
+}
Propchange: harmony/enhanced/classlib/branches/java6/modules/auth/src/main/java/common/javax/security/auth/login/ConfigurationSpi.java
------------------------------------------------------------------------------
svn:eol-style = native
Modified: harmony/enhanced/classlib/branches/java6/modules/auth/src/test/java/common/org/apache/harmony/auth/tests/javax/security/auth/login/ConfigurationTest.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/branches/java6/modules/auth/src/test/java/common/org/apache/harmony/auth/tests/javax/security/auth/login/ConfigurationTest.java?view=diff&rev=566478&r1=566477&r2=566478
==============================================================================
--- harmony/enhanced/classlib/branches/java6/modules/auth/src/test/java/common/org/apache/harmony/auth/tests/javax/security/auth/login/ConfigurationTest.java (original)
+++ harmony/enhanced/classlib/branches/java6/modules/auth/src/test/java/common/org/apache/harmony/auth/tests/javax/security/auth/login/ConfigurationTest.java Wed Aug 15 21:13:42 2007
@@ -24,6 +24,9 @@
import java.io.File;
import java.io.FileOutputStream;
+import java.security.NoSuchAlgorithmException;
+import java.security.NoSuchProviderException;
+import java.security.Provider;
import java.security.Security;
import java.util.HashMap;
import java.util.Map;
@@ -32,6 +35,7 @@
import javax.security.auth.AuthPermission;
import javax.security.auth.login.AppConfigurationEntry;
import javax.security.auth.login.Configuration;
+import javax.security.auth.login.ConfigurationSpi;
import junit.framework.TestCase;
@@ -272,6 +276,196 @@
Support_Exec.execJava(arg, null, true);
}
+
+ /**
+ * @tests javax.security.auth.login.Configuration#getInstance(java.lang.String, javax.security.auth.login.Configuration.Parameters, java.security.Provider)
+ */
+ public void test_getInstance_String_Parameters_Provider() throws NoSuchAlgorithmException{
+ MockConfigurationParameters mcp = new MockConfigurationParameters();
+ MockProvider mp = new MockProvider();
+ Configuration cf = Configuration.getInstance("MockType", mcp, mp);
+ assertEquals("Configuration parameters got should be equals to parameters provided",cf.getParameters(),mcp);
+ assertEquals("Configuration provider got should be equals to provider provided",cf.getProvider(),mp);
+ assertEquals("Configuration type got should be equals to type provided",cf.getType(),"MockType");
+ try{
+ Configuration.getInstance(null, mcp, mp);
+ fail("Should throw NullPointerException here");
+ }
+ catch(NullPointerException e){
+ //expect to catch NullPointerException here
+ }
+
+ try{
+ Configuration.getInstance("MockType2", mcp, mp);
+ fail("Should throw NoSuchAlgorithmException here");
+ }
+ catch(NoSuchAlgorithmException e){
+ //expect to catch NoSuchAlgorithmException here
+ }
+
+ try{
+ Configuration.getInstance("MockType2", mcp, (Provider)null);
+ fail("Should throw IllegalArgumentException here");
+ }
+ catch(IllegalArgumentException e){
+ //expect to catch NoSuchAlgorithmException here
+ }
+
+ cf = Configuration.getInstance("MockType", null, mp);
+ assertEquals("MockType", cf.getType());
+ assertNull(cf.getParameters());
+ }
+
+ /**
+ * @tests javax.security.auth.login.Configuration#getInstance(java.lang.String, javax.security.auth.login.Configuration.Parameters, java.lang.String)
+ */
+ public void test_getInstance_String_Parameters_String() throws NoSuchAlgorithmException, NoSuchProviderException{
+ MockConfigurationParameters mcp = new MockConfigurationParameters();
+ MockProvider mp = new MockProvider();
+ Security.addProvider(mp);
+ Configuration cf = Configuration.getInstance("MockType", mcp, "MockProvider");
+
+ assertEquals("Configuration parameters got should be equals to parameters provided",cf.getParameters(),mcp);
+ assertEquals("Configuration provider got should be equals to provider provided",cf.getProvider(),mp);
+ assertEquals("Configuration type got should be equals to type provided",cf.getType(),"MockType");
+ try{
+ Configuration.getInstance(null, mcp, "MockProvider");
+ fail("Should throw NullPointerException here");
+ }
+ catch(NullPointerException e){
+ //expect to catch NullPointerException here
+ }
+
+ try{
+ Configuration.getInstance("MockType2", mcp, "MockProvider");
+ fail("Should throw NoSuchAlgorithmException here");
+ }
+ catch(NoSuchAlgorithmException e){
+ //expect to catch NoSuchAlgorithmException here
+ }
+
+ try{
+ Configuration.getInstance("MockType2", mcp, (String)null);
+ fail("Should throw IllegalArgumentException here");
+ }
+ catch(IllegalArgumentException e){
+ //expect to catch NoSuchAlgorithmException here
+ }
+
+ try{
+ Configuration.getInstance("MockType2", mcp, "");
+ fail("Should throw IllegalArgumentException here");
+ }
+ catch(IllegalArgumentException e){
+ //expect to catch NoSuchAlgorithmException here
+ }
+
+ try{
+ Configuration.getInstance("MockType2", mcp, "not_exist_provider");
+ fail("Should throw NoSuchProviderException here");
+ }
+ catch(NoSuchProviderException e){
+ //expect to catch NoSuchAlgorithmException here
+ }
+
+ Security.removeProvider("MockProvider");
+ }
+
+ /**
+ * @tests javax.security.auth.login.Configuration#getInstance(java.lang.String, javax.security.auth.login.Configuration.Parameters)
+ */
+ public void test_getInstance_String_Parameters() throws NoSuchAlgorithmException{
+ MockConfigurationParameters mcp = new MockConfigurationParameters();
+ MockProvider mp = new MockProvider();
+ Security.addProvider(mp);
+ Configuration cf = Configuration.getInstance("MockType", mcp);
+
+ assertEquals("Configuration parameters got should be equals to parameters provided",cf.getParameters(),mcp);
+ assertEquals("Configuration provider got should be equals to provider provided",cf.getProvider(),mp);
+ assertEquals("Configuration type got should be equals to type provided",cf.getType(),"MockType");
+
+ try{
+ Configuration.getInstance(null, mcp);
+ fail("Should throw NullPointerException here");
+ }
+ catch(NullPointerException e){
+ //expect to catch NullPointerException here
+ }
+
+ try{
+ Configuration.getInstance("MockType2", mcp);
+ fail("Should throw NoSuchAlgorithmException here");
+ }
+ catch(NoSuchAlgorithmException e){
+ //expect to catch NoSuchAlgorithmException here
+ }
+
+ Security.removeProvider("MockProvider");
+ }
+
+ /**
+ * @throws NoSuchAlgorithmException
+ * @tests javax.security.auth.login.Configuration#getProvider()
+ */
+ public void test_getProvider() throws NoSuchAlgorithmException{
+ MockConfigurationParameters mcp = new MockConfigurationParameters();
+ MockProvider mp = new MockProvider();
+ Configuration cf = Configuration.getInstance("MockType", mcp, mp);
+ assertEquals("Configuration provider got should be equals to provider provided",cf.getProvider(),mp);
+ }
+
+ /**
+ * @throws NoSuchAlgorithmException
+ * @tests javax.security.auth.login.Configuration#getProvider()
+ */
+ public void test_getParameter() throws NoSuchAlgorithmException{
+ MockConfigurationParameters mcp = new MockConfigurationParameters();
+ MockProvider mp = new MockProvider();
+ Configuration cf = Configuration.getInstance("MockType", mcp, mp);
+ assertEquals("Configuration parameters got should be equals to parameters provided",cf.getParameters(),mcp);
+ }
+
+ /**
+ * @throws NoSuchAlgorithmException
+ * @tests javax.security.auth.login.Configuration#getProvider()
+ */
+ public void test_getType() throws NoSuchAlgorithmException{
+ MockConfigurationParameters mcp = new MockConfigurationParameters();
+ MockProvider mp = new MockProvider();
+ Configuration cf = Configuration.getInstance("MockType", mcp, mp);
+ assertEquals("Configuration type got should be equals to type provided",cf.getType(),"MockType");
+ }
+
+ private static class MockConfigurationParameters implements Configuration.Parameters{
+
+ }
+
+ public static class MockConfiguration extends ConfigurationSpi {
+
+ public MockConfiguration(Configuration.Parameters params) {
+
+ }
+
+ @Override
+ protected AppConfigurationEntry[] engineGetAppConfigurationEntry(
+ String name) {
+ return null;
+ }
+ }
+
+ private static class MockProvider extends Provider{
+ /**
+ *
+ */
+ private static final long serialVersionUID = 1L;
+
+ public MockProvider(){
+ super("MockProvider",1.0,"MokeProvider for configuration test");
+ put("Configuration.MockType", MockConfiguration.class.getName());
+ }
+ }
+
+
public static class SecurityPropertiesToBeRead {