/**
 * Copyright (c) 2008-2012 EBM WebSourcing, 2012-2013 Linagora
 * 
 * This program/library is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 2.1 of the License, or (at your
 * option) any later version.
 * 
 * This program/library is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
 * for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program/library; If not, see <http://www.gnu.org/licenses/>
 * for the GNU Lesser General Public License version 2.1.
 */
package org.ow2.petals.se.jsr181;

import java.io.File;
import java.io.IOException;

import javax.jbi.JBIException;

import org.apache.commons.io.FileUtils;
import org.ow2.petals.component.framework.DefaultBootstrap;

/**
 * The JSR181 component bootstrap.
 * @author Christophe HAMERLING - EBM WebSourcing
 * @author Vincent Zurczak - EBM WebSourcing
 */
public class Jsr181Bootstrap extends DefaultBootstrap {

	/**
	 * The path to the "modules" folder.
	 */
	private static final String MODULES_PATH = "modules";

	/**
	 * The path to the "services" folder.
	 */
	private static final String SERVICES_PATH = "services";

	/**
	 * The extension for Axis archives.
	 */
	private static final String MODULE_ARCHIVE_EXTENSION = "mar";

	/**
	 * The name of the configuration file for Axis2.
	 */
	public static final String CONFIGURATION_FILE = "axis2.xml";


	/**
	 * Constructor.
	 */
	public Jsr181Bootstrap() {
		// nothing
	}


	/*
	 * (non-Javadoc)
	 * @see org.ow2.petals.component.framework.DefaultBootstrap
	 * #doInit()
	 */
	@Override
	public void doInit() throws JBIException {

		super.doInit();
		this.createWorkDirectories();
		this.copyConfigurationFiles();
	}


	/**
	 * Copies the configuration files.
	 * @throws JBIException
	 */
	protected void copyConfigurationFiles() throws JBIException {

		final File installRootFile = new File( this.installContext.getInstallRoot());

		// Copy the required files from the META-INF directory
		final File modules = new File(installRootFile, MODULES_PATH);
		final File[] metaInfFiles = new File( installRootFile, "META-INF" ).listFiles();

		for( final File file : metaInfFiles ) {
			// Copy modules archives

			// FIXME : To be deleted when rampart module will be included as
			// real dependency, for now it is not since it have SNAPSHOT dependencies.
			if( file.getName().endsWith( "." + MODULE_ARCHIVE_EXTENSION )) {
				final File destFile = new File( modules, file.getName());
				if( ! destFile.exists()) {
					try {
						FileUtils.copyFile( file, destFile );

					} catch( final IOException e ) {
						// do nothing
					}
				}
			}

			// Copy axis2.xml file
			if( file.getName().equals( CONFIGURATION_FILE )) {
				final File destFile = new File( this.installContext.getInstallRoot(), file.getName());
				if( ! destFile.exists()) {
					try {
						FileUtils.copyFile(file, destFile);

					} catch( final IOException e ) {
						throw new JBIException( "Cannot copy the Axis2 configuration file, Axis2 will not start properly." );
					}
				}
			}
		}

		// Copy the modules from the component root path
		final File[] rootFiles = installRootFile.listFiles();
		for( final File file : rootFiles ) {

			if( file.getName().endsWith( "." + MODULE_ARCHIVE_EXTENSION )) {
				final File destFile = new File( modules, file.getName());
				if( ! destFile.exists()) {
					try {
						FileUtils.copyFile( file, destFile );

					} catch( final IOException e ) {
						this.getLogger().warning( "The module '" + file.getName() + "' has not been copied to modules directory." );
					}
				}
			}
		}
	}


	/**
	 * Creates the work directories.
	 * @throws JBIException
	 */
	protected void createWorkDirectories() throws JBIException {

		final File modules = new File( this.installContext.getInstallRoot(), MODULES_PATH );
		final File services = new File( this.installContext.getInstallRoot(), SERVICES_PATH );

		if(( ! modules.exists() && ! modules.mkdirs())
				|| ( ! services.exists() && ! services.mkdirs()))
			throw new JBIException( "Could not create work directories." );
	}
}
