1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.apache.juddi.auth;
17
18 import org.apache.commons.logging.Log;
19 import org.apache.commons.logging.LogFactory;
20 import org.apache.juddi.util.Config;
21 import org.apache.juddi.util.Loader;
22
23 /***
24 * Implementation of Factory pattern used to create an implementation of
25 * the org.apache.juddi.auth.Authenticator interface.
26 *
27 * The name of the Authenticator implementation to create is passed to the
28 * getAuthenticator method. If a null value is passed then the default
29 * Authenticator implementation "org.apache.juddi.auth.SimpleAuthenticator" is
30 * created.
31 *
32 * @author Steve Viens (sviens@apache.org)
33 */
34 public class AuthenticatorFactory
35 {
36
37 private static Log log = LogFactory.getLog(AuthenticatorFactory.class);
38
39
40 private static final String IMPL_KEY = "juddi.auth";
41 private static final String DEFAULT_IMPL = "org.apache.juddi.auth.DefaultAuthenticator";
42
43
44 private static Authenticator auth = null;
45
46 /***
47 * Returns a new instance of a AuthenticatorFactory.
48 *
49 * @return Authenticator
50 */
51 public static Authenticator getAuthenticator()
52 {
53 if (auth == null)
54 auth = createAuthenticator();
55 return auth;
56 }
57
58 /***
59 * Returns a new instance of a Authenticator.
60 *
61 * @return Authenticator
62 */
63 private static synchronized Authenticator createAuthenticator()
64 {
65 if (auth != null)
66 return auth;
67
68
69 String className = Config.getStringProperty(IMPL_KEY,DEFAULT_IMPL);
70
71
72 log.debug("Authenticator Implementation = " + className);
73
74 Class authClass = null;
75 try
76 {
77
78 authClass = Loader.getClassForName(className);
79 }
80 catch(ClassNotFoundException e)
81 {
82 log.error("The specified Authenticator class '" + className +
83 "' was not found in classpath.");
84 log.error(e);
85 }
86
87 try
88 {
89
90 auth = (Authenticator)authClass.newInstance();
91 }
92 catch(Exception e)
93 {
94 log.error("Exception while attempting to instantiate the " +
95 "implementation of Authenticator: " + authClass.getName() +
96 "\n" + e.getMessage());
97 log.error(e);
98 }
99
100 return auth;
101 }
102
103
104 /****************************************************************************/
105 /****************************** TEST DRIVER *********************************/
106 /****************************************************************************/
107
108
109 public static void main(String[] args)
110 throws Exception
111 {
112 Authenticator auth = AuthenticatorFactory.getAuthenticator();
113 if (auth != null)
114 {
115 System.out.println("Got Authenticator: "+auth.getClass().getName());
116
117 String userID = auth.authenticate("sviens","password");
118
119 System.out.println("The id "+userID+" was successfully authenticated.");
120 }
121 else
122 System.out.println("Sorry: getAuthenticator returned 'null'.");
123 }
124 }