This project has retired. For details please refer to its Attic page.
AuthenticatorFactory xref
View Javadoc
1   /*
2    * Copyright 2001-2008 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   */
17  
18  package org.apache.juddi.v3.auth;
19  
20  import org.apache.commons.configuration.ConfigurationException;
21  import org.apache.commons.logging.Log;
22  import org.apache.commons.logging.LogFactory;
23  import org.apache.juddi.ClassUtil;
24  import org.apache.juddi.config.AppConfig;
25  import org.apache.juddi.config.Property;
26  
27  
28  /**
29   * @author Steve Viens (sviens@apache.org)
30   * @author <a href="mailto:jfaath@apache.org">Jeff Faath</a>
31   */
32  public class AuthenticatorFactory {
33  	private static Log log = LogFactory.getLog(AuthenticatorFactory.class);
34  
35  	// Default authenticator implementation
36  	private static final String DEFAULT_IMPL = "org.apache.juddi.v3.auth.JUDDIAuthenticator";
37  
38  	// the shared Authenticator instance
39  	private static Authenticator auth = null;
40  
41  
42  	/**
43  	 * Returns a new instance of a AuthenticatorFactory.
44  	 * 
45  	 * @return Authenticator
46  	 */
47  	public static synchronized Authenticator getAuthenticator() {
48  		if (auth == null)
49  			auth = createAuthenticator();
50  		return auth;
51  	}
52  
53  	/**
54  	 * Returns a new instance of a Authenticator.
55  	 * 
56  	 * @return Authenticator
57  	 */
58  	private static synchronized Authenticator createAuthenticator() {
59  		if (auth != null)
60  			return auth;
61  	
62  		String className = DEFAULT_IMPL;
63  		try {
64  			// grab class name of the Authenticator implementation to create
65  			className = AppConfig.getConfiguration().getString(Property.JUDDI_AUTHENTICATOR, DEFAULT_IMPL);
66  		}
67  		catch(ConfigurationException ce) {
68  			log.error("Configuration exception occurred retrieving: " + Property.JUDDI_AUTHENTICATOR);
69  		}
70  	
71  		// write the Authenticator implementation name to the log
72  		log.debug("Authenticator Implementation = " + className);
73  	
74  		Class<?> authClass = null;
75  		try {
76  			// Use Loader to locate & load the Authenticator implementation
77  			authClass = ClassUtil.forName(className, AuthenticatorFactory.class);
78  		}
79  		catch(ClassNotFoundException e) {
80  			log.error("The specified Authenticator class '" + className + "' was not found in classpath.");
81  			log.error(e);
82  		}
83  	
84  		try {
85  			if (authClass!=null) {
86  				// try to instantiate the Authenticator implementation
87  				auth = (Authenticator)authClass.newInstance();
88  			} else {
89  				log.error("Could not load " + className + " authClass is null");
90  			}
91  		}
92  		catch(Exception e) {
93  			log.error("Exception while attempting to instantiate the implementation of Authenticator: " + className + "\n" + e.getMessage());
94  			log.error(e);
95  		}
96  	
97  		return auth;
98  	}
99  }