/*
* Smith Servlet - Enable Smith in your webapp
* Copyright (C) 2007 Federico Fissore
*
* 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 it.fridrik.agent.servlet;
import it.fridrik.agent.Smith;
import it.fridrik.agent.SmithArgs;
import it.fridrik.agent.SmithLoader;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
/**
* Loads Smith together with the webapp into which SmithServlet is installed.
* You configure this servlet with the following piece of xml <br/>
*
* <pre>
* <servlet>
* <servlet-name>SmithServlet</servlet-name>
* <servlet-class>it.fridrik.agent.servlet.SmithServlet</servlet-class>
* <init-param>
* <param-name>smith.jar</param-name>
* <param-value>/usr/share/smith/lib/smith.jar</param-value>
* </init-param>
* <init-param>
* <param-name>smith.classes.path</param-name>
* <param-value>/WEB-INF/classes/</param-value>
* </init-param>
* <init-param>
* <param-name>smith.jars.path</param-name>
* <param-value>/WEB-INF/lib/</param-value>
* </init-param>
* <init-param>
* <param-name>smith.monitor.period</param-name>
* <param-value>1000</param-value>
* </init-param>
* <init-param>
* <param-name>smith.log.level</param-name>
* <param-value>SEVERE</param-value>
* </init-param>
* <load-on-startup>1</load-on-startup>
* </servlet>
* </pre>
*
* @author Federico Fissore (federico@fissore.org)
* @since 1.0
*/
public class SmithServlet extends HttpServlet {
private static final long serialVersionUID = 4236090740328025343L;
@Override
public void init() throws ServletException {
super.init();
String smithJar = getParameter("smith.jar");
String classesPath = getParameter("smith.classes.path");
String jarsPath = getParameter("smith.jars.path");
int monitorPeriod = Integer.parseInt(getParameter("smith.monitor.period"));
String logLevel = getParameter("smith.log.level");
SmithArgs args = new SmithArgs(classesPath, jarsPath, monitorPeriod, logLevel);
try {
SmithLoader.hotStart(smithJar, args);
} catch (Exception e) {
throw new ServletException(e);
}
}
@Override
public void destroy() {
super.destroy();
Smith.stopAll();
}
private String getParameter(String propParam) {
String result = this.getServletConfig().getInitParameter(propParam);
if (result.startsWith("/WEB-INF/")) {
String prefix = this.getServletContext().getRealPath("/");
result = prefix + result;
}
return result;
}
}