KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > juddi > auth > AuthenticatorFactory


1 /*
2  * Copyright 2001-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
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   // private reference to the jUDDI logger
37
private static Log log = LogFactory.getLog(AuthenticatorFactory.class);
38
39   // Authenticator property key & default implementation
40
private static final String JavaDoc IMPL_KEY = "juddi.auth";
41   private static final String JavaDoc DEFAULT_IMPL = "org.apache.juddi.auth.DefaultAuthenticator";
42
43   // the shared Authenticator instance
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     // grab class name of the Authenticator implementation to create
69
String JavaDoc className = Config.getStringProperty(IMPL_KEY,DEFAULT_IMPL);
70
71     // write the Authenticator implementation name to the log
72
log.debug("Authenticator Implementation = " + className);
73
74     Class JavaDoc authClass = null;
75     try
76     {
77       // Use Loader to locate & load the Authenticator implementation
78
authClass = Loader.getClassForName(className);
79     }
80     catch(ClassNotFoundException JavaDoc 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       // try to instantiate the Authenticator implementation
90
auth = (Authenticator)authClass.newInstance();
91     }
92     catch(Exception JavaDoc 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 JavaDoc[] args)
110     throws Exception JavaDoc
111   {
112       Authenticator auth = AuthenticatorFactory.getAuthenticator();
113       if (auth != null)
114       {
115         System.out.println("Got Authenticator: "+auth.getClass().getName());
116
117         String JavaDoc 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 }
Popular Tags