pier 2004/05/04 10:38:54
Modified: src/kernel/org/apache/cocoon/kernel/startup
AbstractLogger.java
Added: src/kernel/org/apache/cocoon/kernel/startup
KernelLoader.java KernelServlet.java
ServletWrapper.java
Removed: src/kernel/org/apache/cocoon/kernel/startup Main.java
Servlet.java
Log:
Slightly refactored the kernel startup package
Revision Changes Path
1.6 +0 -0
cocoon-2.2/src/kernel/org/apache/cocoon/kernel/startup/AbstractLogger.java
Index: AbstractLogger.java
===================================================================
RCS file:
/home/cvs/cocoon-2.2/src/kernel/org/apache/cocoon/kernel/startup/AbstractLogger.java,v
retrieving revision 1.5
retrieving revision 1.6
diff -u -r1.5 -r1.6
1.1
cocoon-2.2/src/kernel/org/apache/cocoon/kernel/startup/KernelLoader.java
Index: KernelLoader.java
===================================================================
/*
* Copyright 1999-2004 The Apache Software Foundation.
*
* 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.apache.cocoon.kernel.startup;
import org.apache.cocoon.kernel.Installer;
import org.apache.cocoon.kernel.KernelDeployer;
import org.apache.cocoon.kernel.configuration.ConfigurationBuilder;
/**
*
* @author <a href="mailto:[EMAIL PROTECTED]">Pier Fumagalli</a>
* @version 1.0 (CVS $Revision: 1.1 $)
*/
public class KernelLoader {
public static void main(String args[]) {
if (args.length < 1) {
System.err.println("Usage: " + KernelLoader.class.getName() + "
<blocks "
+ "configuration> <deployment configuration>");
System.exit(1);
}
/* Create a logger for startup operations */
Logger logger = new ConsoleLogger();
try {
/* Now let's create our core deployer */
KernelDeployer deployer = new KernelDeployer();
deployer.logger(logger);
deployer.configure(ConfigurationBuilder.parse(args[0]));
/* Instantiate an installer and process deployment */
Installer installer = new Installer(deployer);
installer.process(ConfigurationBuilder.parse(args[1]));
} catch (Throwable t) {
logger.fatal("An error occurred initializing", t);
}
}
}
1.1
cocoon-2.2/src/kernel/org/apache/cocoon/kernel/startup/KernelServlet.java
Index: KernelServlet.java
===================================================================
/*
* Copyright 1999-2004 The Apache Software Foundation.
*
* 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.apache.cocoon.kernel.startup;
import java.io.IOException;
import java.net.URL;
import javax.servlet.Servlet;
import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import org.apache.cocoon.kernel.CoreWirings;
import org.apache.cocoon.kernel.Installer;
import org.apache.cocoon.kernel.KernelDeployer;
import org.apache.cocoon.kernel.composition.Wirings;
import org.apache.cocoon.kernel.configuration.Configuration;
import org.apache.cocoon.kernel.configuration.ConfigurationBuilder;
/**
* <p>.</p>
*
* @author <a href="mailto:[EMAIL PROTECTED]">Pier Fumagalli</a>
* @version 1.0 (CVS $Revision: 1.1 $)
*/
public class KernelServlet implements Servlet {
protected static KernelServlet instance = null;
private Logger logger = null;
private Wirings wirings = null;
private ServletConfig config = null;
public synchronized void init(ServletConfig config)
throws ServletException {
if (KernelServlet.instance != null) {
throw new ServletException("Kernel cannot be initialized twice");
}
/* Create a logger */
ServletContext ctxt = config.getServletContext();
String level =
config.getInitParameter("org.apache.cocoon.kernel.logging.level");
String temp =
config.getInitParameter("org.apache.cocoon.kernel.logging.trace");
boolean trace = ("true".equalsIgnoreCase(temp) ? true : false);
if ("fatal".equalsIgnoreCase(level)) {
this.logger = new ServletLogger(ServletLogger.FATAL, trace, ctxt);
} else if ("error".equalsIgnoreCase(level)) {
this.logger = new ServletLogger(ServletLogger.ERROR, trace, ctxt);
} else if ("warn".equalsIgnoreCase(level)) {
this.logger = new ServletLogger(ServletLogger.WARN, trace, ctxt);
} else if ("info".equalsIgnoreCase(level)) {
this.logger = new ServletLogger(ServletLogger.INFO, trace, ctxt);
} else if ("debug".equalsIgnoreCase(level)) {
this.logger = new ServletLogger(ServletLogger.DEBUG, trace, ctxt);
} else {
this.logger = new ServletLogger(ServletLogger.INFO, trace, ctxt);
}
/* Find our configurations */
String deplconf =
config.getInitParameter("org.apache.cocoon.kernel.configuration.deployer");
String instconf =
config.getInitParameter("org.apache.cocoon.kernel.configuration.installer");
if (deplconf == null) {
String message = "Parameter \"deployer-config\" not specified";
logger.fatal(message);
throw new ServletException(message);
} else if (instconf == null) {
String message = "Parameter \"installer-config\" not specified";
logger.fatal(message);
throw new ServletException(message);
}
/* Let's start up */
this.logger.info("Kernel startup");
try {
URL deplurl = ctxt.getResource(deplconf);
if (deplurl == null) {
String message = "Unable to find deployer configurations
\""
+ deplconf + "\"";
logger.fatal(message);
throw new ServletException(message);
}
URL insturl = ctxt.getResource(instconf);
if (insturl == null) {
String message = "Unable to find installer configuration
\""
+ deplconf + "\"";
logger.fatal(message);
throw new ServletException(message);
}
Configuration conf = null;
/* Now let's create our core deployer */
KernelDeployer deployer = new KernelDeployer();
deployer.logger(logger);
conf = ConfigurationBuilder.parse(deplurl);
deployer.configure(conf);
/* Instantiate an installer and process deployment */
Installer installer = new Installer(deployer);
conf = ConfigurationBuilder.parse(insturl);
installer.process(conf);
/* Store the current kernel configuration */
this.config = config;
this.wirings = new CoreWirings(deployer);
KernelServlet.instance = this;
} catch (Throwable throwable) {
String message = "An error occurred initializing the kernel";
logger.fatal(message, throwable);
throw new ServletException(message);
}
}
public void destroy() {
this.logger.info("Kernel shutdown");
}
public void service(ServletRequest request, ServletResponse response)
throws ServletException, IOException {
throw new ServletException("Servlet non serviceable");
}
public ServletConfig getServletConfig() {
return(this.config);
}
public String getServletInfo() {
return("Apache Cocoon Kernel Servlet");
}
public Logger getLogger() {
return(this.logger);
}
public Wirings getWirings() {
return(this.wirings);
}
}
1.1
cocoon-2.2/src/kernel/org/apache/cocoon/kernel/startup/ServletWrapper.java
Index: ServletWrapper.java
===================================================================
/*
* Copyright 1999-2004 The Apache Software Foundation.
*
* 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.apache.cocoon.kernel.startup;
import java.io.IOException;
import javax.servlet.Servlet;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import org.apache.cocoon.kernel.composition.Wire;
import org.apache.cocoon.kernel.composition.Wirings;
/**
* <p>.</p>
*
* @author <a href="mailto:[EMAIL PROTECTED]">Pier Fumagalli</a>
* @version 1.0 (CVS $Revision: 1.1 $)
*/
public class ServletWrapper implements Servlet {
private Servlet servlet = null;
private Wirings wirings = null;
private Logger logger = null;
private boolean expose = false;
public void init(ServletConfig config)
throws ServletException {
/* Get the deployed block name exposing the servlet */
String inst =
config.getInitParameter("org.apache.cocoon.kernel.block.name");
if (inst == null) {
throw new ServletException("Block instance not specified in \""
+ "org.apache.cocoon.kernel.block\" servlet property");
}
/* Check if we have to expose the wirings in the request attributes */
String expose =
config.getInitParameter("org.apache.cocoon.kernel.wirings.expose");
this.expose = "true".equalsIgnoreCase(expose);
/* Get a hold on the kernel servlet, its wirings and logger */
KernelServlet instance = KernelServlet.instance;
if (instance == null) {
throw new ServletException("Framework not initialized");
}
this.wirings = instance.getWirings();
this.logger = instance.getLogger();
/* Get a hold on the wrapped servlet and initialize it */
try {
this.servlet = (Servlet) this.wirings.lookup(Servlet.class, inst);
this.servlet.init(config);
} catch (ServletException e) {
throw (e);
} catch (Throwable t) {
this.logger.fatal("Unable to access wrapped servlet", t);
throw new ServletException("Unable to access wrapped servlet");
}
}
public void destroy() {
this.servlet.destroy();
((Wire)this.servlet).release();
}
public void service(ServletRequest request, ServletResponse response)
throws ServletException, IOException {
if (this.expose) {
request.setAttribute("org.apache.cocoon.kernel.wirings", wirings);
}
this.servlet.service(request, response);
}
public ServletConfig getServletConfig() {
return(this.servlet.getServletConfig());
}
public String getServletInfo() {
return(this.servlet.getServletInfo());
}
}