⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 security.java

📁 linux下建立JAVA虚拟机的源码KAFFE
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/* Security.java --- Java base security class implementation   Copyright (C) 1999, 2001, 2002, 2003, 2004, 2005, 2006   Free Software Foundation, Inc.This file is part of GNU Classpath.GNU Classpath is free software; you can redistribute it and/or modifyit under the terms of the GNU General Public License as published bythe Free Software Foundation; either version 2, or (at your option)any later version.GNU Classpath is distributed in the hope that it will be useful, butWITHOUT ANY WARRANTY; without even the implied warranty ofMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNUGeneral Public License for more details.You should have received a copy of the GNU General Public Licensealong with GNU Classpath; see the file COPYING.  If not, write to theFree Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA02110-1301 USA.Linking this library statically or dynamically with other modules ismaking a combined work based on this library.  Thus, the terms andconditions of the GNU General Public License cover the wholecombination.As a special exception, the copyright holders of this library give youpermission to link this library with independent modules to produce anexecutable, regardless of the license terms of these independentmodules, and to copy and distribute the resulting executable underterms of your choice, provided that you also meet, for each linkedindependent module, the terms and conditions of the license of thatmodule.  An independent module is a module which is not derived fromor based on this library.  If you modify this library, you may extendthis exception to your version of the library, but you are notobligated to do so.  If you do not wish to do so, delete thisexception statement from your version. */package java.security;import gnu.classpath.SystemProperties;import gnu.classpath.Configuration;import gnu.classpath.VMStackWalker;import java.io.IOException;import java.io.InputStream;import java.net.URL;import java.util.Collections;import java.util.Enumeration;import java.util.HashMap;import java.util.HashSet;import java.util.Iterator;import java.util.LinkedHashSet;import java.util.Map;import java.util.Properties;import java.util.Set;import java.util.Vector;/** * This class centralizes all security properties and common security methods. * One of its primary uses is to manage security providers. * * @author Mark Benvenuto (ivymccough@worldnet.att.net) */public final class Security{  private static final String ALG_ALIAS = "Alg.Alias.";  private static Vector providers = new Vector();  private static Properties secprops = new Properties();    static    {      String base = SystemProperties.getProperty("gnu.classpath.home.url");      String vendor = SystemProperties.getProperty("gnu.classpath.vm.shortname");      // Try VM specific security file      boolean loaded = loadProviders (base, vendor);          // Append classpath standard provider if possible      if (!loadProviders (base, "classpath")	  && !loaded	  && providers.size() == 0)	  {	      if (Configuration.DEBUG)		  {		      /* No providers found and both security files failed to		       * load properly. Give a warning in case of DEBUG is		       * enabled. Could be done with java.util.logging later.		       */		      System.err.println			  ("WARNING: could not properly read security provider files:");		      System.err.println			  ("         " + base + "/security/" + vendor			   + ".security");		      System.err.println			  ("         " + base + "/security/" + "classpath"			   + ".security");		      System.err.println			  ("         Falling back to standard GNU security provider");		  }	      providers.addElement (new gnu.java.security.provider.Gnu());	  }    }  // This class can't be instantiated.  private Security()  {  }  /**   * Tries to load the vender specific security providers from the given base   * URL. Returns true if the resource could be read and completely parsed   * successfully, false otherwise.   */  private static boolean loadProviders(String baseUrl, String vendor)  {    if (baseUrl == null || vendor == null)      return false;    boolean result = true;    String secfilestr = baseUrl + "/security/" + vendor + ".security";    try      {	InputStream fin = new URL(secfilestr).openStream();	secprops.load(fin);	int i = 1;	String name;	while ((name = secprops.getProperty("security.provider." + i)) != null)	  {	    Exception exception = null;	    try	      {            ClassLoader sys = ClassLoader.getSystemClassLoader();		providers.addElement(Class.forName(name, true, sys).newInstance());	      }	    catch (ClassNotFoundException x)	      {	        exception = x;	      }	    catch (InstantiationException x)	      {	        exception = x;	      }	    catch (IllegalAccessException x)	      {	        exception = x;	      }	    if (exception != null)	      {		System.err.println ("WARNING: Error loading security provider "				    + name + ": " + exception);		result = false;	      }	    i++;	  }      }    catch (IOException ignored)      {	result = false;      }    return result;  }  /**   * Returns the value associated to a designated property name for a given   * algorithm.   *    * @param algName   *          the algorithm name.   * @param propName   *          the name of the property to return.   * @return the value of the specified property or <code>null</code> if none   *         found.   * @deprecated Use the provider-based and algorithm-independent   *             {@link AlgorithmParameters} and {@link KeyFactory} engine   *             classes instead.   */  public static String getAlgorithmProperty(String algName, String propName)  {    if (algName == null || propName == null)      return null;    String property = String.valueOf(propName) + "." + String.valueOf(algName);    Provider p;    for (Iterator i = providers.iterator(); i.hasNext(); )      {        p = (Provider) i.next();        for (Iterator j = p.keySet().iterator(); j.hasNext(); )          {            String key = (String) j.next();            if (key.equalsIgnoreCase(property))              return p.getProperty(key);          }      }    return null;  }  /**   * Inserts a new designated {@link Provider} at a designated (1-based)   * position in the current list of installed {@link Provider}s,   *    * @param provider   *          the new {@link Provider} to add.   * @param position   *          the position (starting from 1) of where to install   *          <code>provider</code>.   * @return the actual position, in the list of installed Providers. Returns   *         <code>-1</code> if <code>provider</code> was laready in the   *         list. The actual position may be different than the desired   *         <code>position</code>.   * @throws SecurityException   *           if a {@link SecurityManager} is installed and it disallows this   *           operation.   * @see #getProvider(String)   * @see #removeProvider(String)   * @see SecurityPermission   */  public static int insertProviderAt(Provider provider, int position)  {    SecurityManager sm = System.getSecurityManager();    if (sm != null)      sm.checkSecurityAccess("insertProvider." + provider.getName());    position--;    int max = providers.size ();    for (int i = 0; i < max; i++)      {	if (((Provider) providers.elementAt(i)).getName().equals(provider.getName()))	  return -1;      }    if (position < 0)      position = 0;    if (position > max)      position = max;    providers.insertElementAt(provider, position);    return position + 1;  }  /**   * Appends the designated new {@link Provider} to the current list of   * installed {@link Provider}s.   *    * @param provider   *          the new {@link Provider} to append.   * @return the position (starting from 1) of <code>provider</code> in the   *         current list of {@link Provider}s, or <code>-1</code> if   *         <code>provider</code> was already there.   * @throws SecurityException   *           if a {@link SecurityManager} is installed and it disallows this   *           operation.   * @see #getProvider(String)   * @see #removeProvider(String)   * @see SecurityPermission   */  public static int addProvider(Provider provider)  {    return insertProviderAt (provider, providers.size () + 1);  }  /**   * Removes an already installed {@link Provider}, given its name, from the   * current list of installed {@link Provider}s.   *    * @param name   *          the name of an already installed {@link Provider} to remove.   * @throws SecurityException   *           if a {@link SecurityManager} is installed and it disallows this   *           operation.   * @see #getProvider(String)   * @see #addProvider(Provider)   */  public static void removeProvider(String name)  {    SecurityManager sm = System.getSecurityManager();    if (sm != null)      sm.checkSecurityAccess("removeProvider." + name);    int max = providers.size ();    for (int i = 0; i < max; i++)      {	if (((Provider) providers.elementAt(i)).getName().equals(name))	  {	    providers.remove(i);	    break;	  }      }  }  /**   * Returns the current list of installed {@link Provider}s as an array   * ordered according to their installation preference order.   *    * @return an array of all the installed providers.   */  public static Provider[] getProviders()  {    Provider[] array = new Provider[providers.size ()];    providers.copyInto (array);    return array;  }  /**   * Returns an already installed {@link Provider} given its name.   *    * @param name   *          the name of an already installed {@link Provider}.   * @return the {@link Provider} known by <code>name</code>. Returns   *         <code>null</code> if the current list of {@link Provider}s does   *         not include one named <code>name</code>.   * @see #removeProvider(String)   * @see #addProvider(Provider)   */  public static Provider getProvider(String name)  {    if (name == null)      return null;    else      {        name = name.trim();        if (name.length() == 0)          return null;      }    Provider p;    int max = providers.size ();    for (int i = 0; i < max; i++)      {	p = (Provider) providers.elementAt(i);	if (p.getName().equals(name))	  return p;      }    return null;  }  /**   * Returns the value associated with a Security propery.   *    * @param key   *          the key of the property to fetch.   * @return the value of the Security property associated with   *         <code>key</code>. Returns <code>null</code> if no such property   *         was found.   * @throws SecurityException   *           if a {@link SecurityManager} is installed and it disallows this   *           operation.   * @see #setProperty(String, String)   * @see SecurityPermission

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -