Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * JBoss, Home of Professional Open Source
   * Copyright 2005, JBoss Inc., and individual contributors as indicated
   * by the @authors tag. See the copyright.txt in the distribution for a
   * full listing of individual contributors.
   *
   * This is free software; you can redistribute it and/or modify it
   * under the terms of the GNU Lesser General Public License as
   * published by the Free Software Foundation; either version 2.1 of
  * the License, or (at your option) any later version.
  *
  * This software is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  * Lesser General Public License for more details.
  *
  * You should have received a copy of the GNU Lesser General Public
  * License along with this software; if not, write to the Free
  * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
  * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
  */
 package org.jboss.security.auth.login;
 
 import java.io.File;
 import java.net.URL;
 
 
 import  org.jboss.security.PicketBoxLogger;
 import  org.jboss.security.PicketBoxMessages;
 import  org.jboss.security.config.ApplicationPolicy;
 import  org.jboss.security.config.ApplicationPolicyRegistration;
 import  org.jboss.security.config.PolicyConfig;
 import  org.jboss.security.config.SecurityConfiguration;
 import  org.jboss.security.config.parser.StaxBasedConfigParser;

An concrete implementation of the javax.security.auth.login.Configuration class that parses an xml configuration of the form: <policy> <application-policy name = "test-domain"> <authentication> <login-module code = "org.jboss.security.plugins.samples.IdentityLoginModule" flag = "required"> <module-option name = "principal">starksm</module-option> </login-module> </authentication> </application-policy> </policy>

Author(s):
Scott.Stark@jboss.org
Anil.Saldhana@jboss.org
Version:
$Revision: 57482 $
See also:
javax.security.auth.login.Configuration
 
 @SuppressWarnings({"rawtypes","unchecked"})
 public class XMLLoginConfigImpl extends Configuration implements Serializable, ApplicationPolicyRegistration
 {
   
The serialVersionUID
 
    private static final long serialVersionUID = -8965860493224188277L;
 
    private static final String DEFAULT_APP_CONFIG_NAME = "other";
 
    private static final AuthPermission REFRESH_PERM = new AuthPermission("refreshLoginConfiguration");
 
    transient PolicyConfig appConfigs = new PolicyConfig();

   
The URL to the XML or Sun login configuration
 
    protected URL loginConfigURL;

   
The inherited configuration we delegate to
 
    protected Configuration parentConfig;

   
A flag indicating if XML configs should be validated
 
    private boolean validateDTD = true;
 
    private static final XMLLoginConfigImpl instance = new XMLLoginConfigImpl();

   

Private constructor to implement the singleton pattern.

 
    private XMLLoginConfigImpl()
    {
    }

   

Obtains a reference to the singleton.

Returns:
a reference to the singleton XMLLoginConfigImpl instance.
 
   public static XMLLoginConfigImpl getInstance()
   {
      return ;
   }
   // --- Begin Configuration method overrrides
   public void refresh()
   {
      SecurityManager sm = System.getSecurityManager();
      if (sm != null)
         sm.checkPermission();
      .clear();
      loadConfig();
   }
   {
      PicketBoxLogger.LOGGER.traceBeginGetAppConfigEntry(appName.size());
      // Load the config if PolicyConfig is empty
      if (this..size() == 0)
         this.loadConfig();
      AppConfigurationEntry[] entry = null;
      ApplicationPolicy aPolicy = this.getApplicationPolicy(appName);
      BaseAuthenticationInfo authInfo = null;
      if (aPolicy != null)
         authInfo = aPolicy.getAuthenticationInfo();
      if (authInfo == null)
      {
         PicketBoxLogger.LOGGER.traceGetAppConfigEntryViaParent(appName != null ? .toString() : null);
         if ( != null)
            entry = .getAppConfigurationEntry(appName);
         if (entry == null)
         {
            PicketBoxLogger.LOGGER.traceGetAppConfigEntryViaDefault(appName);
            ApplicationPolicy defPolicy = .get();
            authInfo = defPolicy != null ? (AuthenticationInfodefPolicy.getAuthenticationInfo() : null;
         }
      }
      if (authInfo != null)
      {
         PicketBoxLogger.LOGGER.traceEndGetAppConfigEntryWithSuccess(appNameauthInfo.toString());
         // Make a copy of the authInfo object
         final BaseAuthenticationInfo theAuthInfo = authInfo;
         {
            public AppConfigurationEntry[] run()
            {
               return theAuthInfo.copyAppConfigurationEntry();
            }
         };
         entry = AccessController.doPrivileged(action);
      }
      else
      {
         PicketBoxLogger.LOGGER.traceEndGetAppConfigEntryWithFailure(appName);
      }
      return entry;
   }
   // --- End Configuration method overrrides

   
Set the URL of the XML login configuration file that should be loaded by this mbean on startup.
   public URL getConfigURL()
   {
      return ;
   }

   
Set the URL of the XML login configuration file that should be loaded by this mbean on startup.
   public void setConfigURL(URL loginConfigURL)
   {
      this. = loginConfigURL;
   }
   public void setConfigResource(String resourceNamethrows IOException
   {
      ClassLoader tcl = SecurityActions.getContextClassLoader();
       = tcl.getResource(resourceName);
      if ( == null)
         throw PicketBoxMessages.MESSAGES.failedToFindResource(resourceName);
   }
   public void setParentConfig(Configuration parentConfig)
   {
      this. = parentConfig;
   }

   
Get whether the login config xml document is validated againsts its DTD
   public boolean getValidateDTD()
   {
      return this.;
   }

   
Set whether the login config xml document is validated againsts its DTD
   public void setValidateDTD(boolean flag)
   {
      this. = flag;
   }

   

See also:
ApplicationPolicyRegistration.addApplicationPolicy(String, ApplicationPolicy)
   public void addApplicationPolicy(String appName, ApplicationPolicy aPolicy)
   {
      SecurityManager sm = System.getSecurityManager();
      if (sm != null)
         sm.checkPermission();
      .add(aPolicy);
      handleJASPIDelegation(aPolicy);
      SecurityConfiguration.addApplicationPolicy(aPolicy);
   }

   
Add an application configuration
   public void addAppConfig(String appNameAppConfigurationEntry[] entries)
   {
      SecurityManager sm = System.getSecurityManager();
      if (sm != null)
         sm.checkPermission();
      AuthenticationInfo authInfo = new AuthenticationInfo(appName);
      authInfo.setAppConfigurationEntry(entries);
      PicketBoxLogger.LOGGER.traceAddAppConfig(appNameauthInfo.toString());
      ApplicationPolicy aPolicy = new ApplicationPolicy(appNameauthInfo);
      .add(aPolicy);
      SecurityConfiguration.addApplicationPolicy(aPolicy);
   }
   
   public void copy(PolicyConfig policyConfig)
   {
      this..copy(policyConfig);
   }

   

Deprecated:
Parameters:
appName
See also:
removeApplicationPolicy(String)
   public void removeAppConfig(String appName)
   {
      SecurityManager sm = System.getSecurityManager();
      if (sm != null)
         sm.checkPermission();
      .remove(appName);
      SecurityConfiguration.removeApplicationPolicy(appName);
   }

   

See also:
ApplicationPolicyRegistration.getApplicationPolicy(String)
   public ApplicationPolicy getApplicationPolicy(String domainName)
   {
      if ( == null || .size() == 0)
         loadConfig();
      ApplicationPolicy aPolicy = null;
      if( != null )
         aPolicy = .get(domainName);
      if (aPolicy != null)
         SecurityConfiguration.addApplicationPolicy(aPolicy);
      return aPolicy;
   }

   

See also:
ApplicationPolicyRegistration.removeApplicationPolicy(String)
   public boolean removeApplicationPolicy(String appName)
   {
      SecurityManager sm = System.getSecurityManager();
      if (sm != null)
         sm.checkPermission();
      PicketBoxLogger.LOGGER.traceRemoveAppConfig(appName);
      .remove(appName);
      SecurityConfiguration.removeApplicationPolicy(appName);
      return true;
   }

   
Method that returns the parsed AuthenticationInfo needed by the JASPI framework until a seperate Configuration mechanism for JASPI is established

Returns:
the parsed AuthenticationInfo object
   {
      ApplicationPolicy aPolicy = getApplicationPolicy(domainName);
      return aPolicy != null ? aPolicy.getAuthenticationInfo() : null;
   }
   public void clear()
   {
   }

   
Called to try to load the config from the java.security.auth.login.config property value when there is no loginConfigURL.
   @SuppressWarnings("deprecation")
   public void loadConfig()
   {
      // Try to load the java.security.auth.login.config property
      String loginConfig = System.getProperty("java.security.auth.login.config");
      if (loginConfig == null)
         loginConfig = "login-config.xml";
      // If there is no loginConfigURL build it from the loginConfig
      if ( == null)
      {
         try
         {
            // Try as a URL
             = new URL(loginConfig);
         }
         catch (MalformedURLException e)
         {
            // Try as a resource
            try
            {
               setConfigResource(loginConfig);
            }
            catch (IOException ignore)
            {
               // Try as a file
               File configFile = new File(loginConfig);
               try
               {
                  setConfigURL(configFile.toURL());
               }
               catch (MalformedURLException ignore2)
               {
               }
            }
         }
      }
      if ( == null)
      {
         PicketBoxLogger.LOGGER.warnFailureToFindConfig(loginConfig);
         return;
      }
      PicketBoxLogger.LOGGER.traceBeginLoadConfig();
      // Try to load the config if found
      try
      {
         loadConfig();
         PicketBoxLogger.LOGGER.traceEndLoadConfigWithSuccess();
      }
      catch (Exception e)
      {
         PicketBoxLogger.LOGGER.warnEndLoadConfigWithFailure(e);
      }
   }
 
   protected String[] loadConfig(URL configthrows Exception
   {
      SecurityManager sm = System.getSecurityManager();
      if (sm != null)
         sm.checkPermission();
      ArrayList configNames = new ArrayList();
      PicketBoxLogger.LOGGER.debugLoadConfigAsXML(config);
      try
      {
         loadXMLConfig(configconfigNames);
      }
      catch (Throwable e)
      {
         PicketBoxLogger.LOGGER.debugLoadConfigAsSun(confige);
         loadSunConfig(configconfigNames);
      }
      String[] names = new String[configNames.size()];
      configNames.toArray(names);
      return names;
   }

   
Handle the case when JASPI Info may have login module stack holder which delegates to a login module stack

Parameters:
aPolicy
   private void handleJASPIDelegation(ApplicationPolicy aPolicy)
   {
      BaseAuthenticationInfo bai = aPolicy.getAuthenticationInfo();
      if (bai instanceof JASPIAuthenticationInfo)
      {
         JASPIAuthenticationInfo jai = (JASPIAuthenticationInfobai;
         LoginModuleStackHolder[] lmsharr = jai.getLoginModuleStackHolder();
         for (LoginModuleStackHolder lmsh : lmsharr)
         {
            this.addAppConfig(lmsh.getName(), lmsh.getAppConfigurationEntry());
         }
      }
   }
 
   private void loadSunConfig(URL sunConfigArrayList configNamesthrows Exception
   {
      InputStream is = null;
      InputStreamReader configFile = null;
      try
      {
         is = sunConfig.openStream();
         configFile = new InputStreamReader(is);
         SunConfigParser.doParse(configFilethis, PicketBoxLogger.LOGGER.isTraceEnabled());
      }
      finally
      {
         safeClose(configFile);
         safeClose(is);
      }
   }
 
   private void loadXMLConfig(URL loginConfigURLArrayList configNamesthrows Exception
   {
      InputStream is = null;
      try
      {
         is = loginConfigURL.openStream();
         StaxBasedConfigParser parser = new StaxBasedConfigParser();
         parser.parse(is);
      }
      finally
      {
         safeClose(is);
      }
   }
   
   private void safeClose(InputStream fis)
   {
      try
      {
         if(fis != null)
         {
            fis.close();
         }
      }
      catch(Exception e)
      {}
   }
   private void safeClose(InputStreamReader fis)
   {
      try
      {
         if(fis != null)
         {
            fis.close();
         }
      }
      catch(Exception e)
      {}
   }
New to GrepCode? Check out our FAQ X