001/**
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.activemq.security;
018
019import java.net.URL;
020
021import org.apache.activemq.broker.Broker;
022import org.apache.activemq.broker.BrokerPlugin;
023
024/**
025 * Adds a JAAS based authentication security plugin
026 * 
027 * @org.apache.xbean.XBean description="Provides a JAAS based authentication plugin"
028 * 
029 * 
030 */
031public class JaasAuthenticationPlugin implements BrokerPlugin {
032    protected String configuration = "activemq-domain";
033    protected boolean discoverLoginConfig = true;
034
035    public Broker installPlugin(Broker broker) {
036        initialiseJaas();
037        return new JaasAuthenticationBroker(broker, configuration);
038    }
039
040
041    // Properties
042    // -------------------------------------------------------------------------
043    public String getConfiguration() {
044        return configuration;
045    }
046
047    /**
048     * Sets the JAAS configuration domain name used
049     */
050    public void setConfiguration(String jaasConfiguration) {
051        this.configuration = jaasConfiguration;
052    }
053
054
055    public boolean isDiscoverLoginConfig() {
056        return discoverLoginConfig;
057    }
058
059    /**
060     * Enables or disables the auto-discovery of the login.config file for JAAS to initialize itself. 
061     * This flag is enabled by default such that if the <b>java.security.auth.login.config</b> system property
062     * is not defined then it is set to the location of the <b>login.config</b> file on the classpath.
063     */
064    public void setDiscoverLoginConfig(boolean discoverLoginConfig) {
065        this.discoverLoginConfig = discoverLoginConfig;
066    }
067
068    // Implementation methods
069    // -------------------------------------------------------------------------
070    protected void initialiseJaas() {
071        if (discoverLoginConfig) {
072            String path = System.getProperty("java.security.auth.login.config");
073            if (path == null) {
074                //URL resource = Thread.currentThread().getContextClassLoader().getResource("login.config");
075                URL resource = null;
076                if (resource == null) {
077                    resource = getClass().getClassLoader().getResource("login.config");
078                }
079                if (resource != null) {
080                    path = resource.getFile();
081                    System.setProperty("java.security.auth.login.config", path);
082                }
083            }
084        }
085    }
086}