/*
* $Header: /home/cvs/jakarta-slide/src/wrappers/catalina/SlideServerListener.java,v 1.4 2001/10/04 06:43:40 remm Exp $
* $Revision: 1.4 $
* $Date: 2001/10/04 06:43:40 $
*
* ====================================================================
*
* The Apache Software License, Version 1.1
*
* Copyright (c) 1999 The Apache Software Foundation. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. The end-user documentation included with the redistribution, if
* any, must include the following acknowlegement:
* "This product includes software developed by the
* Apache Software Foundation (http://www.apache.org/)."
* Alternately, this acknowlegement may appear in the software itself,
* if and wherever such third-party acknowlegements normally appear.
*
* 4. The names "The Jakarta Project", "Tomcat", and "Apache Software
* Foundation" must not be used to endorse or promote products derived
* from this software without prior written permission. For written
* permission, please contact apache@apache.org.
*
* 5. Products derived from this software may not be called "Apache"
* nor may "Apache" appear in their names without prior written
* permission of the Apache Group.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation. For more
* information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*
* [Additional notices, if required by prior licensing conditions]
*
*/
package wrappers.catalina;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.IOException;
import java.util.Enumeration;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import javax.xml.parsers.FactoryConfigurationError;
import javax.xml.parsers.ParserConfigurationException;
import org.xml.sax.*;
import org.xml.sax.helpers.*;
import org.apache.catalina.Container;
import org.apache.catalina.Host;
import org.apache.catalina.Lifecycle;
import org.apache.catalina.LifecycleEvent;
import org.apache.catalina.LifecycleException;
import org.apache.catalina.LifecycleListener;
import org.apache.catalina.Server;
import org.apache.catalina.Service;
import org.apache.catalina.logger.FileLogger;
import org.apache.slide.common.Domain;
import org.apache.slide.common.EmbeddedDomain;
import org.apache.slide.common.Namespace;
import org.apache.slide.common.NamespaceAccessToken;
import org.apache.slide.util.conf.Configuration;
import org.apache.slide.util.conf.ConfigurationElement;
import org.apache.slide.util.conf.ConfigurationException;
import org.apache.slide.util.conf.Populate;
import org.apache.slide.util.logger.SimpleLogger;
/**
* Server listener responsible for setting up the Slide domain.
*
* @author Remy Maucherat
* @version $Revision: 1.4 $ $Date: 2001/10/04 06:43:40 $
*/
public class SlideServerListener
implements LifecycleListener {
// -------------------------------------------------------------- Constants
public static final String DEFAULT_CONFIG_FILE_NAME = "conf/slide.xml";
// ----------------------------------------------------------- Constructors
/**
* Acknowledge the occurrence of the specified event.
*
* @param event LifecycleEvent that has occurred
*/
public void lifecycleEvent(LifecycleEvent event) {
try {
server = (Server) event.getLifecycle();
// Disable static domain
Domain.setInitialized(true);
// Create a Catalina logger for the Slide domain
FileLogger catalinaLogger = new FileLogger();
catalinaLogger.setDirectory("logs");
catalinaLogger.setPrefix("slide_domain.");
catalinaLogger.setSuffix(".txt");
catalinaLogger.setTimestamp(false);
catalinaLogger.start();
SlideLogger logger = new SlideLogger();
logger.setLogger(catalinaLogger);
logger.setLoggerLevel(6);
domain.setLogger(logger);
if (event.getType() == Lifecycle.START_EVENT) {
setHosts();
System.out.println("Start Slide");
initializeDomain();
domain.start();
} else if (event.getType() == Lifecycle.STOP_EVENT) {
System.out.println("Stop Slide");
domain.stop();
}
} catch (Throwable t) {
t.printStackTrace();
}
}
// ----------------------------------------------------- Instance Variables
/**
* Slide domain.
*/
protected EmbeddedDomain domain = new EmbeddedDomain();
/**
* Associated server.
*/
protected Server server;
/**
* Container logger.
*/
protected SlideLogger logger;
/**
* Domain configuration file.
*/
protected String configFileName = DEFAULT_CONFIG_FILE_NAME;
/**
* Logging level.
*/
protected int logLevel = 6;
// ------------------------------------------------------------- Properties
/**
* Configuration file name setter.
*/
public void setConfigFileName(String configFileName) {
this.configFileName = configFileName;
}
/**
* Configuration file name getter.
*/
public String getConfigFileName() {
return configFileName;
}
/**
* Logging level setter.
*/
public void setLogLevel(int logLevel) {
this.logLevel = logLevel;
}
/**
* Log level getter.
*/
public int getLogLevel() {
return logLevel;
}
// --------------------------------------------------------- Public Methods
/**
* Get configuration object from a file.
*/
protected Configuration getConfiguration(File file)
throws IOException, SAXException, ParserConfigurationException,
FactoryConfigurationError {
// Get XML parser
SAXParserFactory factory = SAXParserFactory.newInstance();
factory.setNamespaceAware(false);
factory.setValidating(false);
SAXParser parser = factory.newSAXParser();
// Parse XML file
FileInputStream is = new FileInputStream(file);
Populate pop = new Populate();
return new ConfigurationElement(pop.load(new InputSource(is),
parser.getXMLReader()));
}
/**
* Setup domain.
*/
protected void initializeDomain()
throws IOException, SAXException, FactoryConfigurationError,
ParserConfigurationException {
File domainConfigFile = new File(configFileName);
if (!domainConfigFile.isAbsolute()) {
domainConfigFile = new File(System.getProperty("catalina.home"),
configFileName);
}
Configuration slideConfiguration = getConfiguration(domainConfigFile);
domain.setDefaultNamespace(slideConfiguration.getAttribute
("default", "slide"));
Enumeration namespaceDefinitions =
slideConfiguration.getConfigurations("namespace");
while (namespaceDefinitions.hasMoreElements()) {
loadNamespace(domainConfigFile.getParentFile(),
(Configuration) namespaceDefinitions.nextElement());
}
}
/**
* Load namespace.
*/
protected void loadNamespace(File path, Configuration xmlFragment)
throws IOException, SAXException, FactoryConfigurationError,
ParserConfigurationException {
String name = xmlFragment.getAttribute("name");
Configuration definition = xmlFragment.getConfiguration("definition");
Configuration configuration =
xmlFragment.getConfiguration("configuration");
Configuration baseData = xmlFragment.getConfiguration("data");
String defRef = definition.getAttribute("ref", "");
if (!defRef.equals("")) {
definition = getConfiguration(new File(path, defRef))
.getConfiguration("definition");
}
String confRef = configuration.getAttribute("ref", "");
if (!confRef.equals("")) {
configuration = getConfiguration(new File(path, confRef))
.getConfiguration("configuration");
}
String baseDataRef = baseData.getAttribute("ref", "");
if (!baseDataRef.equals("")) {
baseData = getConfiguration(new File(path, baseDataRef))
.getConfiguration("data");
}
// Create a Catalina logger for the Slide domain
FileLogger catalinaLogger = new FileLogger();
catalinaLogger.setDirectory("logs");
catalinaLogger.setPrefix("slide_ns_" + name + ".");
catalinaLogger.setSuffix(".txt");
catalinaLogger.setTimestamp(false);
try {
catalinaLogger.start();
} catch (LifecycleException e) {
// Never happens
e.printStackTrace();
}
SlideLogger nsLogger = new SlideLogger();
nsLogger.setLogger(catalinaLogger);
nsLogger.setLoggerLevel(logLevel);
domain.addNamespace(name, nsLogger, definition,
configuration, baseData);
}
/**
* Associate the hosts with the embedded domain.
*/
protected void setHosts() {
Service[] services = server.findServices();
for (int i = 0; i < services.length; i++) {
Container container = services[i].getContainer();
Container[] children = container.findChildren();
for (int j = 0; j < children.length; j++) {
Host host = (Host) children[j];
if (host instanceof SlideHost) {
System.out.println("Set domain for Slide host");
((SlideHost) host).setDomain(domain);
} else if (host instanceof WebdavHost) {
System.out.println("Set domain for Webdav host");
((WebdavHost) host).setDomain(domain);
} else if (host instanceof AdminHost) {
System.out.println("Set domain for Slide admin host");
((AdminHost) host).setDomain(domain);
}
}
}
}
}