001 /*
002 * Sonar, open source software quality management tool.
003 * Copyright (C) 2008-2011 SonarSource
004 * mailto:contact AT sonarsource DOT com
005 *
006 * Sonar is free software; you can redistribute it and/or
007 * modify it under the terms of the GNU Lesser General Public
008 * License as published by the Free Software Foundation; either
009 * version 3 of the License, or (at your option) any later version.
010 *
011 * Sonar is distributed in the hope that it will be useful,
012 * but WITHOUT ANY WARRANTY; without even the implied warranty of
013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014 * Lesser General Public License for more details.
015 *
016 * You should have received a copy of the GNU Lesser General Public
017 * License along with Sonar; if not, write to the Free Software
018 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02
019 */
020 package org.sonar.server.ui;
021
022 import org.apache.commons.lang.StringUtils;
023 import org.slf4j.Logger;
024 import org.slf4j.LoggerFactory;
025 import org.sonar.api.CoreProperties;
026 import org.sonar.api.ServerComponent;
027 import org.sonar.api.config.Settings;
028 import org.sonar.api.security.LoginPasswordAuthenticator;
029
030 public class AuthenticatorFactory implements ServerComponent {
031
032 private static final Logger LOG = LoggerFactory.getLogger(AuthenticatorFactory.class);
033 private static final Logger INFO = LoggerFactory.getLogger("org.sonar.INFO");
034
035 private LoginPasswordAuthenticator authenticator = null;
036 private String classname;
037 private boolean ignoreStartupFailure;
038 private LoginPasswordAuthenticator[] authenticators;
039
040 public AuthenticatorFactory(Settings settings, LoginPasswordAuthenticator[] authenticators) {
041 classname = settings.getString(CoreProperties.CORE_AUTHENTICATOR_CLASS);
042 ignoreStartupFailure = settings.getBoolean(CoreProperties.CORE_AUTHENTICATOR_IGNORE_STARTUP_FAILURE);
043 this.authenticators = authenticators;
044 }
045
046 /**
047 * This constructor is used when there aren't any authentication plugins.
048 */
049 public AuthenticatorFactory(Settings settings) {
050 this(settings, null);
051 }
052
053 /**
054 * Start the authenticator selected in sonar configuration. If no authentication plugin is selected, then
055 * the default authentication mechanism is used and null is returned.
056 * <p/>
057 * Throws a unchecked exception if the authenticator can not be started.
058 */
059
060 public void start() {
061 // check authentication plugin at startup
062 if (StringUtils.isEmpty(classname)) {
063 // use sonar internal authenticator
064 return;
065 }
066
067 authenticator = searchAuthenticator();
068 if (authenticator == null) {
069 LOG.error("Authentication plugin not found. Please check the property '" + CoreProperties.CORE_AUTHENTICATOR_CLASS + "' in conf/sonar.properties");
070 throw new AuthenticatorNotFoundException(classname);
071 }
072
073 try {
074 INFO.info("Authentication plugin: class " + classname);
075 authenticator.init();
076 INFO.info("Authentication plugin started");
077
078 } catch (RuntimeException e) {
079 if (ignoreStartupFailure) {
080 LOG.error("IGNORED - Authentication plugin fails to start: " + e.getMessage());
081 } else {
082 LOG.error("Authentication plugin fails to start: " + e.getMessage());
083 throw e;
084 }
085 }
086 }
087
088 public LoginPasswordAuthenticator getAuthenticator() {
089 return authenticator;
090 }
091
092 private LoginPasswordAuthenticator searchAuthenticator() {
093 if (authenticators != null) {
094 for (LoginPasswordAuthenticator lpa : authenticators) {
095 if (lpa.getClass().getName().equals(classname)) {
096 return lpa;
097 }
098 }
099 }
100 return null;
101 }
102 }