/*
 * JOnAS: Java(TM) Open Application Server
 * Copyright (C) 1999 Bull S.A.
 * Contact: jonas-team@objectweb.org
 * 
 * This 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 any later version.
 * 
 * This 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 library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
 * USA
 *
 * --------------------------------------------------------------------------
 * $Id$
 * --------------------------------------------------------------------------
 */

package org.objectweb.jonas.tomcat;

import javax.naming.Context;
import javax.naming.NamingException;
import java.io.File;
import java.io.FilenameFilter;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.StringTokenizer;

import org.apache.tomcat.core.TomcatException;
import org.apache.tomcat.startup.EmbededTomcat;
import org.apache.tomcat.startup.Main;
import org.objectweb.jonas.common.Log;
import org.objectweb.jonas.service.ServiceException;
import org.objectweb.jonas.service.AbsDynamicServiceImpl;
import org.objectweb.util.monolog.api.Logger;
import org.objectweb.util.monolog.api.BasicLevel;

/**
 * Tomcat service implementation for Tomcat 3.3
 *
 * @author Philippe Coq
 *  Contributor(s):
 * Julien Lehembre - Libelis 
 *
 */

public class EmbeddedTomcatImpl33 extends AbsDynamicServiceImpl  implements TomcatService {

    static private Logger logger = null;

    private EmbededTomcat tcAp;
    private String tomcatHome = null;
    private String argsforTomcat[]= null;
    private static final String TOMCAT_ARGS = "args";

    public void doInit(Context ctx) {
        if( logger == null)
            logger = Log.getLogger(Log.JONAS_TOMCAT_PREFIX);
	logger.log(BasicLevel.DEBUG, "entering");
        try {
            tomcatHome = System.getProperties().getProperty("tomcat.home");
            if ((tomcatHome == null) || (tomcatHome.equalsIgnoreCase(""))) {
                logger.log(BasicLevel.ERROR, "The tomcat.home isn't set during JonAS's launching");
                throw new ServiceException("tomcat.home property is not set");
            }
            try{
                String args  = (String) ctx.lookup(TOMCAT_ARGS); 
                java.util.StringTokenizer st = new StringTokenizer(args);
                java.util.ArrayList al = new java.util.ArrayList();
                int i = 0;
                while (st.hasMoreTokens()) {
                    al.add((String)st.nextToken());
                    i++;
                }
                argsforTomcat = new String[i];
                for(int j=0; j<argsforTomcat.length;j++)
                    argsforTomcat[j] = (String)al.get(j);
                
             } catch (NamingException e) {
             }

            
            tcAp = new EmbededTomcat();
        } catch (Throwable e) {
            e.printStackTrace();
            throw new ServiceException("Error while initializing the Tomcat service.", e);
        }
    }

    public void run() throws ServiceException {
	logger.log(BasicLevel.DEBUG, "entering");
        try{
            tcAp.setArgs(argsforTomcat);
            tcAp.execute();
        }catch(Exception e){
            throw new ServiceException(e.getMessage());
        }
    }

    public void doStop() throws ServiceException {
	logger.log(BasicLevel.DEBUG, "entering");
        if(isStarted()){
            try {
                tcAp.stop();
            } catch (TomcatException te) {
                te.printStackTrace();
                throw new ServiceException(te.getMessage());
            }
        }
    }
}

