/** 
 * 
 * Copyright 2004 Protique Ltd
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); 
 * you may not use this file except in compliance with the License. 
 * You may obtain a copy of the License at 
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, 
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
 * See the License for the specific language governing permissions and 
 * limitations under the License. 
 * 
 **/
package org.codehaus.activemq.gbean;

import java.util.Properties;

import javax.jms.JMSException;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.geronimo.gbean.GBeanInfo;
import org.apache.geronimo.gbean.GBeanInfoBuilder;
import org.apache.geronimo.gbean.GBeanLifecycle;
import org.apache.geronimo.gbean.WaitingException;
import org.apache.geronimo.system.serverinfo.ServerInfo;
import org.codehaus.activemq.broker.BrokerAdmin;
import org.codehaus.activemq.broker.BrokerContainer;
import org.codehaus.activemq.broker.BrokerContext;
import org.codehaus.activemq.broker.impl.BrokerContainerImpl;
import org.codehaus.activemq.security.jassjacc.JassJaccSecurityAdapter;
import org.codehaus.activemq.security.jassjacc.PropertiesConfigLoader;
import org.codehaus.activemq.store.jdbm.JdbmPersistenceAdapter;

/**
 * Default implementation of the ActiveMQ Message Server
 *
 * @version $Revision: 1.5 $
 */
public class ActiveMQContainerGBean implements GBeanLifecycle, ActiveMQContainer {

    private Log log = LogFactory.getLog(getClass().getName());

    private final String brokerName;

    private BrokerContext context = BrokerContext.getInstance();
    private BrokerContainer container;

    private final ServerInfo serverInfo;
	private final String dataDirectory;
	private final String jassConfiguration;
	private final Properties securityRoles;

    //default constructor for use as gbean endpoint.
    public ActiveMQContainerGBean() {
    	serverInfo=null;
        brokerName = null;
        dataDirectory = "/var/activemq";
        jassConfiguration = null;
        securityRoles = null;
    }
	
    public ActiveMQContainerGBean(ServerInfo serverInfo, String brokerName, String dataDirectory, String jassConfiguration, Properties securityRoles) {
		this.securityRoles = securityRoles;
		assert serverInfo != null;
		assert brokerName != null;
		assert dataDirectory != null;
		this.serverInfo=serverInfo;
        this.brokerName = brokerName;
        this.dataDirectory = dataDirectory;
        this.jassConfiguration=jassConfiguration;
    }

    public synchronized BrokerContainer getBrokerContainer() {
        return container;
    }

    /**
     * @see org.codehaus.activemq.gbean.ActiveMQContainer#getBrokerAdmin()
     */
    public BrokerAdmin getBrokerAdmin() {
        return container.getBroker().getBrokerAdmin();
    }

    public synchronized void doStart() throws WaitingException, Exception {
    	ClassLoader old = Thread.currentThread().getContextClassLoader();
    	Thread.currentThread().setContextClassLoader(ActiveMQContainerGBean.class.getClassLoader());
    	try {
	        if (container == null) {
	            container = createContainer();
	            container.start();
	        }
    	} finally {
        	Thread.currentThread().setContextClassLoader(old);
    	}
    }

    public synchronized void doStop() throws WaitingException, Exception {
        if (container != null) {
            BrokerContainer temp = container;
            container = null;
            temp.stop();
        }
    }

    public synchronized void doFail() {
        if (container != null) {
            BrokerContainer temp = container;
            container = null;
            try {
                temp.stop();
            }
            catch (JMSException e) {
                log.info("Caught while closing due to failure: " + e, e);
            }
        }
    }

    protected BrokerContainer createContainer() throws Exception {
    	BrokerContainerImpl answer = new BrokerContainerImpl(brokerName, context);
    	JdbmPersistenceAdapter pa = new JdbmPersistenceAdapter( serverInfo.resolve(dataDirectory) );
    	answer.setPersistenceAdapter( pa );
    	if( jassConfiguration != null ) {
    		answer.setSecurityAdapter(new JassJaccSecurityAdapter(jassConfiguration));
    	}
    	if( securityRoles != null ) {
    		// Install JACC configuration.
    		PropertiesConfigLoader loader = new PropertiesConfigLoader(brokerName, securityRoles);
    		loader.installSecurity();
    	}
    	return answer;
    }

    public static final GBeanInfo GBEAN_INFO;

    static {
        GBeanInfoBuilder infoFactory = new GBeanInfoBuilder("ActiveMQ Message Broker", ActiveMQContainerGBean.class);
        infoFactory.addReference("serverInfo", ServerInfo.class);
        infoFactory.addAttribute("brokerName", String.class, true);
        infoFactory.addAttribute("dataDirectory", String.class, true);
        infoFactory.addAttribute("jassConfiguration", String.class, true);
        infoFactory.addAttribute("securityRoles", Properties.class, true);
        
        infoFactory.addInterface(ActiveMQContainer.class);
        infoFactory.setConstructor(new String[]{"serverInfo", "brokerName", "dataDirectory", "jassConfiguration", "securityRoles"});

        GBEAN_INFO = infoFactory.getBeanInfo();
    }

    public static GBeanInfo getGBeanInfo() {
        return GBEAN_INFO;
    }

	/**
	 * @return Returns the brokerName.
	 */
	public String getBrokerName() {
		return brokerName;
	}
	
	/**
	 * @return Returns the dataDirectory.
	 */
	public String getDataDirectory() {
		return dataDirectory;
	}
	
	/**
	 * @return Returns the jassConfiguration.
	 */
	public String getJassConfiguration() {
		return jassConfiguration;
	}
	
	/**
	 * @return Returns the securityRoles.
	 */
	public Properties getSecurityRoles() {
		return securityRoles;
	}
	
}