1 |
779 |
jeremybenn |
/* CallbackUtil.java -- Callback 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 gnu.javax.security.auth.callback.ConsoleCallbackHandler;
|
42 |
|
|
|
43 |
|
|
import java.security.Provider;
|
44 |
|
|
import java.security.Security;
|
45 |
|
|
import java.util.logging.Logger;
|
46 |
|
|
|
47 |
|
|
import javax.security.auth.callback.CallbackHandler;
|
48 |
|
|
|
49 |
|
|
/**
|
50 |
|
|
* A <i>Helper</i> class containing general purpose utlity methods dealing with
|
51 |
|
|
* callback handlers and their <i>Security Provider</i>.
|
52 |
|
|
*/
|
53 |
|
|
public abstract class CallbackUtil
|
54 |
|
|
{
|
55 |
|
|
private static final Logger log = Logger.getLogger(CallbackUtil.class.getName());
|
56 |
|
|
|
57 |
|
|
// default 0-arguments constructor
|
58 |
|
|
|
59 |
|
|
// Class methods
|
60 |
|
|
// --------------------------------------------------------------------------
|
61 |
|
|
|
62 |
|
|
/**
|
63 |
|
|
* Return an implementation of the {@link CallbackHandler}, from any
|
64 |
|
|
* {@link Provider}, capable of handling callbacks through the <i>console</i>;
|
65 |
|
|
* i.e. <code>System.in</code> and <code>System.out</code>.
|
66 |
|
|
* <p>
|
67 |
|
|
* If no <i>Security Provider</i> for this type of callback was found, this
|
68 |
|
|
* method returns the default GNU implementation.
|
69 |
|
|
*
|
70 |
|
|
* @return a console {@link CallbackHandler} implementation.
|
71 |
|
|
*/
|
72 |
|
|
public static final CallbackHandler getConsoleHandler()
|
73 |
|
|
{
|
74 |
|
|
CallbackHandler result = getHandler("Console");
|
75 |
|
|
if (result == null)
|
76 |
|
|
{
|
77 |
|
|
log.fine("No console callback handler found. Will use ours");
|
78 |
|
|
result = new ConsoleCallbackHandler();
|
79 |
|
|
}
|
80 |
|
|
return result;
|
81 |
|
|
}
|
82 |
|
|
|
83 |
|
|
/**
|
84 |
|
|
* Return a {@link CallbackHandler}, of a designated type, for interacting
|
85 |
|
|
* with the user.
|
86 |
|
|
* <p>
|
87 |
|
|
* This method first finds all currently installed <i>Security Providers</i>
|
88 |
|
|
* capable of providing such service and then in turn attempts to instantiate
|
89 |
|
|
* the handler from those providers. As soon as one provider returns a non-
|
90 |
|
|
* null instance of the callback handler, the search stops and that instance
|
91 |
|
|
* is returned.
|
92 |
|
|
*
|
93 |
|
|
* @return a {@link CallbackHandler} of the designated type, or
|
94 |
|
|
* <code>null</code> if no provider was found for theis type of
|
95 |
|
|
* callback.
|
96 |
|
|
*/
|
97 |
|
|
private static final CallbackHandler getHandler(String handlerType)
|
98 |
|
|
{
|
99 |
|
|
log.entering(CallbackUtil.class.getName(), "getHandler", handlerType);
|
100 |
|
|
|
101 |
|
|
CallbackHandler result = null;
|
102 |
|
|
String service = "CallbackHandler." + handlerType;
|
103 |
|
|
Provider[] providers = Security.getProviders(service);
|
104 |
|
|
if (providers != null)
|
105 |
|
|
for (int i = 0; i < providers.length; i++)
|
106 |
|
|
{
|
107 |
|
|
Provider p = providers[i];
|
108 |
|
|
String className = p.getProperty(service);
|
109 |
|
|
if (className != null)
|
110 |
|
|
try
|
111 |
|
|
{
|
112 |
|
|
result = (CallbackHandler) Class.forName(className.trim()).newInstance();
|
113 |
|
|
}
|
114 |
|
|
catch (InstantiationException x)
|
115 |
|
|
{
|
116 |
|
|
log.fine("InstantiationException while creating ["
|
117 |
|
|
+ className + "] from provider [" + p.getName()
|
118 |
|
|
+ "]. Ignore");
|
119 |
|
|
}
|
120 |
|
|
catch (IllegalAccessException x)
|
121 |
|
|
{
|
122 |
|
|
log.fine("IllegalAccessException while creating ["
|
123 |
|
|
+ className + "] from provider [" + p.getName()
|
124 |
|
|
+ "]. Ignore");
|
125 |
|
|
}
|
126 |
|
|
catch (ClassNotFoundException x)
|
127 |
|
|
{
|
128 |
|
|
log.fine("ClassNotFoundException while creating ["
|
129 |
|
|
+ className + "] from provider [" + p.getName()
|
130 |
|
|
+ "]. Ignore");
|
131 |
|
|
}
|
132 |
|
|
|
133 |
|
|
if (result != null)
|
134 |
|
|
{
|
135 |
|
|
|
136 |
|
|
log.fine("Will use [" + result.getClass().getName()
|
137 |
|
|
+ "] from [" + p.getName() + "]");
|
138 |
|
|
break;
|
139 |
|
|
}
|
140 |
|
|
}
|
141 |
|
|
|
142 |
|
|
log.exiting(CallbackUtil.class.getName(), "getHandler", result);
|
143 |
|
|
return result;
|
144 |
|
|
}
|
145 |
|
|
}
|