1 |
779 |
jeremybenn |
/* ProviderUtil.java -- Security Provider related utilities
|
2 |
|
|
Copyright (C) 2006 Free Software Foundation, Inc.
|
3 |
|
|
|
4 |
|
|
This file is part of GNU Classpath.
|
5 |
|
|
|
6 |
|
|
GNU Classpath is free software; you can redistribute it and/or modify
|
7 |
|
|
it under the terms of the GNU General Public License as published by
|
8 |
|
|
the Free Software Foundation; either version 2, or (at your option)
|
9 |
|
|
any later version.
|
10 |
|
|
|
11 |
|
|
GNU Classpath is distributed in the hope that it will be useful, but
|
12 |
|
|
WITHOUT ANY WARRANTY; without even the implied warranty of
|
13 |
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
14 |
|
|
General Public License for more details.
|
15 |
|
|
|
16 |
|
|
You should have received a copy of the GNU General Public License
|
17 |
|
|
along with GNU Classpath; see the file COPYING. If not, write to the
|
18 |
|
|
Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
|
19 |
|
|
02110-1301 USA.
|
20 |
|
|
|
21 |
|
|
Linking this library statically or dynamically with other modules is
|
22 |
|
|
making a combined work based on this library. Thus, the terms and
|
23 |
|
|
conditions of the GNU General Public License cover the whole
|
24 |
|
|
combination.
|
25 |
|
|
|
26 |
|
|
As a special exception, the copyright holders of this library give you
|
27 |
|
|
permission to link this library with independent modules to produce an
|
28 |
|
|
executable, regardless of the license terms of these independent
|
29 |
|
|
modules, and to copy and distribute the resulting executable under
|
30 |
|
|
terms of your choice, provided that you also meet, for each linked
|
31 |
|
|
independent module, the terms and conditions of the license of that
|
32 |
|
|
module. An independent module is a module which is not derived from
|
33 |
|
|
or based on this library. If you modify this library, you may extend
|
34 |
|
|
this exception to your version of the library, but you are not
|
35 |
|
|
obligated to do so. If you do not wish to do so, delete this
|
36 |
|
|
exception statement from your version. */
|
37 |
|
|
|
38 |
|
|
|
39 |
|
|
package gnu.classpath.tools.common;
|
40 |
|
|
|
41 |
|
|
import java.security.AccessController;
|
42 |
|
|
import java.security.PrivilegedAction;
|
43 |
|
|
import java.security.Provider;
|
44 |
|
|
import java.security.Security;
|
45 |
|
|
import java.util.logging.Logger;
|
46 |
|
|
|
47 |
|
|
/**
|
48 |
|
|
* A <i>Helper</i> class containing general purpose utlity methods dealing with
|
49 |
|
|
* installing and removing <i>Security Providers</i> at runtime.
|
50 |
|
|
*/
|
51 |
|
|
public abstract class ProviderUtil
|
52 |
|
|
{
|
53 |
|
|
private static final Logger log = Logger.getLogger(ProviderUtil.class.getName());
|
54 |
|
|
|
55 |
|
|
// default 0-arguments constructor
|
56 |
|
|
|
57 |
|
|
// Class methods
|
58 |
|
|
// --------------------------------------------------------------------------
|
59 |
|
|
|
60 |
|
|
/**
|
61 |
|
|
* Attempt to (a) instantiate, and (b) add a designated {@link Provider} by
|
62 |
|
|
* inserting at at the top of the list of <i>Security Providers</i> already
|
63 |
|
|
* present at runtime, only if it is not already installed.
|
64 |
|
|
* <p>
|
65 |
|
|
* <b>IMPORTANT</b>: This method overrides the security check usually carried
|
66 |
|
|
* out by the security manager when inserting a new {@link Provider}.
|
67 |
|
|
*
|
68 |
|
|
* @param providerClass a fully qualified, non-null, class name of a
|
69 |
|
|
* <i>Security Provider</i> to add if it is not already installed.
|
70 |
|
|
* @return an instance of {@link SecurityProviderInfo} referencing the
|
71 |
|
|
* {@link Provider} instance created with the designated class name,
|
72 |
|
|
* and its position in the underlying JVM runtime.
|
73 |
|
|
*/
|
74 |
|
|
public static final SecurityProviderInfo addProvider(String providerClass)
|
75 |
|
|
{
|
76 |
|
|
log.entering(ProviderUtil.class.getName(), "addProvider", providerClass);
|
77 |
|
|
|
78 |
|
|
Provider provider = null;
|
79 |
|
|
try
|
80 |
|
|
{
|
81 |
|
|
provider = (Provider) Class.forName(providerClass.trim()).newInstance();
|
82 |
|
|
}
|
83 |
|
|
catch (InstantiationException x)
|
84 |
|
|
{
|
85 |
|
|
log.fine("InstantiationException while creating [" + providerClass
|
86 |
|
|
+ "]. Ignore");
|
87 |
|
|
}
|
88 |
|
|
catch (IllegalAccessException x)
|
89 |
|
|
{
|
90 |
|
|
log.fine("IllegalAccessException while creating [" + providerClass
|
91 |
|
|
+ "]. Ignore");
|
92 |
|
|
}
|
93 |
|
|
catch (ClassNotFoundException x)
|
94 |
|
|
{
|
95 |
|
|
log.fine("ClassNotFoundException while creating [" + providerClass
|
96 |
|
|
+ "]. Ignore");
|
97 |
|
|
}
|
98 |
|
|
|
99 |
|
|
int position = provider != null ? addProvider(provider) : -1;
|
100 |
|
|
SecurityProviderInfo result = new SecurityProviderInfo(provider, position);
|
101 |
|
|
|
102 |
|
|
log.exiting(ProviderUtil.class.getName(), "addProvider", result);
|
103 |
|
|
return result;
|
104 |
|
|
}
|
105 |
|
|
|
106 |
|
|
/**
|
107 |
|
|
* Attempt to add the designated {@link Provider} by inserting at at the top
|
108 |
|
|
* of the list of <i>Security Providers</i> already present at runtime, only
|
109 |
|
|
* if it is not already installed.
|
110 |
|
|
* <p>
|
111 |
|
|
* <b>IMPORTANT</b>: This method overrides the security check usually carried
|
112 |
|
|
* out by the security manager when inserting a new {@link Provider}.
|
113 |
|
|
*
|
114 |
|
|
* @param provider a non-null <i>Security Provider</i> to add if it is not
|
115 |
|
|
* already installed.
|
116 |
|
|
* @return the new position of the designated provider in the list if it was
|
117 |
|
|
* not already present, or <code>-1</code> if it was already
|
118 |
|
|
* installed.
|
119 |
|
|
*/
|
120 |
|
|
public static final int addProvider(final Provider provider)
|
121 |
|
|
{
|
122 |
|
|
log.entering(ProviderUtil.class.getName(), "addProvider", provider);
|
123 |
|
|
|
124 |
|
|
Integer actualPosition = (Integer) AccessController.doPrivileged(new PrivilegedAction()
|
125 |
|
|
{
|
126 |
|
|
public Object run()
|
127 |
|
|
{
|
128 |
|
|
int result = Security.insertProviderAt(provider, 1);
|
129 |
|
|
return Integer.valueOf(result);
|
130 |
|
|
}
|
131 |
|
|
});
|
132 |
|
|
|
133 |
|
|
int result = actualPosition.intValue();
|
134 |
|
|
log.fine("Provider [" + provider.getName() + "] installed? " + (result != - 1));
|
135 |
|
|
|
136 |
|
|
log.exiting(ProviderUtil.class.getName(), "addProvider", actualPosition);
|
137 |
|
|
return result;
|
138 |
|
|
}
|
139 |
|
|
|
140 |
|
|
/**
|
141 |
|
|
* Remove a designated <i>Security Provider</i>.
|
142 |
|
|
* <p>
|
143 |
|
|
* <b>IMPORTANT</b>: This method overrides the security check usually carried
|
144 |
|
|
* out by the security manager when removing a {@link Provider}.
|
145 |
|
|
*
|
146 |
|
|
* @param providerName the name of the {@link Provider} to remove.
|
147 |
|
|
*/
|
148 |
|
|
public static final void removeProvider(final String providerName)
|
149 |
|
|
{
|
150 |
|
|
log.entering(ProviderUtil.class.getName(), "removeProvider", providerName);
|
151 |
|
|
|
152 |
|
|
AccessController.doPrivileged(new PrivilegedAction()
|
153 |
|
|
{
|
154 |
|
|
public Object run()
|
155 |
|
|
{
|
156 |
|
|
Security.removeProvider(providerName);
|
157 |
|
|
return null;
|
158 |
|
|
}
|
159 |
|
|
});
|
160 |
|
|
|
161 |
|
|
log.exiting(ProviderUtil.class.getName(), "removeProvider");
|
162 |
|
|
}
|
163 |
|
|
}
|