001/* Provider.java -- Security provider information
002   Copyright (C) 1998, 1999, 2000, 2002, 2006 Free Software Foundation, Inc.
003
004This file is part of GNU Classpath.
005
006GNU Classpath is free software; you can redistribute it and/or modify
007it under the terms of the GNU General Public License as published by
008the Free Software Foundation; either version 2, or (at your option)
009any later version.
010
011GNU Classpath is distributed in the hope that it will be useful, but
012WITHOUT ANY WARRANTY; without even the implied warranty of
013MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
014General Public License for more details.
015
016You should have received a copy of the GNU General Public License
017along with GNU Classpath; see the file COPYING.  If not, write to the
018Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
01902110-1301 USA.
020
021Linking this library statically or dynamically with other modules is
022making a combined work based on this library.  Thus, the terms and
023conditions of the GNU General Public License cover the whole
024combination.
025
026As a special exception, the copyright holders of this library give you
027permission to link this library with independent modules to produce an
028executable, regardless of the license terms of these independent
029modules, and to copy and distribute the resulting executable under
030terms of your choice, provided that you also meet, for each linked
031independent module, the terms and conditions of the license of that
032module.  An independent module is a module which is not derived from
033or based on this library.  If you modify this library, you may extend
034this exception to your version of the library, but you are not
035obligated to do so.  If you do not wish to do so, delete this
036exception statement from your version. */
037
038package java.security;
039
040import java.io.Serializable;
041import java.util.Properties;
042
043/**
044 * This class represents a Java security architecture service provider. The
045 * services provided by a such a provider can range from security algorithms to
046 * key generation.
047 * <p>
048 * Providers are installed by name and version number. See the static
049 * initializer of the {@link java.security.Security} class for the default
050 * security providers installed by this class library.
051 * 
052 * @author Aaron M. Renn (arenn@urbanophile.com)
053 */
054public abstract class Provider
055    extends Properties
056    implements Serializable
057{
058  private static final long serialVersionUID = -4298000515446427739L;
059
060  /**
061   * This is a textual description of the provider
062   */
063  private String info;
064
065  /**
066   * This is the name of the provider
067   */
068  private String name;
069
070  /**
071   * This is the version number of the provider
072   */
073  private double version;
074
075  /**
076   * This method initializes a new instance of <code>Provider</code> to have
077   * the specified name, version, and description information.
078   *
079   * @param name The name to assign to this <code>Provider</code>.
080   * @param version The version number for this <code>Provider</code>.
081   * @param info A textual description of this provider.
082   */
083  protected Provider(String name, double version, String info)
084  {
085    this.name = name;
086    this.version = version;
087    this.info = info;
088  }
089
090  /**
091   * This method returns the name assigned to this <code>Provider</code>.
092   *
093   * @return The <code>Provider</code>'s name.
094   */
095  public String getName()
096  {
097    return (name);
098  }
099
100  /**
101   * This method retunrs the version number of this <code>Provider</code>.
102   * 
103   * @return The <code>Provider</code>'s version number.
104   */
105  public double getVersion()
106  {
107    return (version);
108  }
109
110  /**
111   * This method returns a textual description of the <code>Provider</code>.
112   *
113   * @return A description of the <code>Provider</code>.
114   */
115  public String getInfo()
116  {
117    return (info);
118  }
119
120  /**
121   * Maps a key property to a designated value.
122   * <p>
123   * If there is an installed {@link SecurityManager} object in the underlying
124   * VM, its {@link SecurityManager#checkSecurityAccess(String)} method is
125   * called with the string <code>"putProviderProperty." + name</code>, where
126   * <code>name</code> is this provider's name. For the default implementation
127   * this translates into a {@link SecurityManager#checkPermission(Permission)}
128   * for a <code>SecurityPermission("putProviderProperty." + name)</code>.
129   * 
130   * @param key The property key.
131   * @param value The property value.
132   * @return The previous value of the specified property (<code>key</code>),
133   *         or <code>null</code> if it did not have one.
134   * @throws SecurityException If a security manager is installed and its
135   *           {@link SecurityManager#checkSecurityAccess(String)} method
136   *           disallows adding properties at run-time.
137   * @since Classpath 0.4+cvs, JDK 1.2
138   * @see java.lang.Object#equals(Object)
139   * @see java.util.Hashtable#get(Object)
140   */
141  public Object put(Object key, Object value)
142  {
143    SecurityManager sm = System.getSecurityManager();
144    if (sm != null)
145      sm.checkSecurityAccess("putProviderProperty." + this.name);
146    return super.put(toCanonicalKey(key), value);
147  }
148
149  // overrides same in java.util.Hashtable
150  public Object get(Object key)
151  {
152    return super.get(toCanonicalKey(key));
153  }
154
155  /**
156   * This method removes the specified key entry (and its associated value)
157   * from the property mapping collection.
158   * <p>
159   * If there is an installed {@link SecurityManager} object in the underlying
160   * VM, its {@link SecurityManager#checkSecurityAccess(String)} method is
161   * called with the string <code>"removeProviderProperty." + name</code>, where
162   * <code>name</code> is this provider's name. For the default implementation
163   * this translates into a {@link SecurityManager#checkPermission(Permission)}
164   * for a <code>SecurityPermission("removeProviderProperty." + name)</code>.
165   * 
166   * @param key The key to remove
167   * @return The previous value for this key, or <code>null</code> if no
168   * previous value.
169   */
170  public Object remove(Object key)
171  {
172    SecurityManager sm = System.getSecurityManager();
173    if (sm != null)
174      sm.checkSecurityAccess("removeProviderProperty." + this.name);
175    return super.remove(toCanonicalKey(key));
176  }
177
178  /**
179   * This method clears the entire property collection such that it no longer
180   * contains the properties used to look up the services provided by
181   * this <code>Provider</code>.
182   * <p>
183   * If there is an installed {@link SecurityManager} object in the underlying
184   * VM, its {@link SecurityManager#checkSecurityAccess(String)} method is
185   * called with the string <code>"clearProviderProperties." + name</code>,
186   * where <code>name</code> is this provider's name. For the default
187   * implementation this translates into a
188   * {@link SecurityManager#checkPermission(Permission)} for a
189   * <code>SecurityPermission("clearProviderProperties." + name)</code>.
190   */
191  public void clear()
192  {
193    SecurityManager sm = System.getSecurityManager();
194    if (sm != null)
195      sm.checkSecurityAccess("clearProviderProperties." + this.name);
196    super.clear();
197  }
198
199  /**
200   * This method returns a <code>String</code> representation of this
201   * object.  This will include the <code>Provider</code> name and
202   * version number.
203   *
204   * @return A <code>String</code> representation of this object.
205   */
206  public String toString()
207  {
208    return (getClass().getName() + ": name=" + getName() + " version=" +
209            version);
210  }
211
212  private Object toCanonicalKey(Object key)
213  {
214    if (key.getClass().isAssignableFrom(String.class)) // is it ours?
215      return ((String) key).toUpperCase(); // use default locale
216    return key;
217  }
218}