You are viewing a plain text version of this content. The canonical link for it is here.
Posted to scm@geronimo.apache.org by ge...@apache.org on 2004/02/18 16:33:41 UTC
cvs commit: incubator-geronimo/applications/jmxdebug/src/java/org/apache/geronimo/jmxdebug/web/velocity BasicVelocityActionServlet.java DebugServlet.java ServletLogger.java WebappLoader.java velocity.defaults
geirm 2004/02/18 07:33:41
Added: applications/jmxdebug/src/java/org/apache/geronimo/jmxdebug/web/velocity
BasicVelocityActionServlet.java DebugServlet.java
ServletLogger.java WebappLoader.java
velocity.defaults
Log:
initial commit of simple infrastructure to do vel
Revision Changes Path
1.1 incubator-geronimo/applications/jmxdebug/src/java/org/apache/geronimo/jmxdebug/web/velocity/BasicVelocityActionServlet.java
Index: BasicVelocityActionServlet.java
===================================================================
/**
*
* Copyright 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.geronimo.jmxdebug.web.velocity;
import org.apache.velocity.app.VelocityEngine;
import org.apache.velocity.runtime.RuntimeConstants;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.Template;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.ServletException;
import javax.servlet.ServletContext;
import java.lang.reflect.Method;
import java.util.Properties;
import java.util.Enumeration;
import java.io.InputStream;
/**
* Simple servlet to dispatch based on 'action'. Also inits velocity in a
* simple way
*
* @version $Id: BasicVelocityActionServlet.java,v 1.1 2004/02/18 15:33:41 geirm Exp $
*/
public abstract class BasicVelocityActionServlet extends HttpServlet {
/**
* for dispatch purposes
*/
private final Class[] args =
{HttpServletRequest.class, HttpServletResponse.class};
/**
* velocity engine for this servlet
*/
private VelocityEngine velEngine = new VelocityEngine();
public static final String DEFAULT_PROPS =
"org/apache/geronimo/jmxdebug/web/velocity/velocity.defaults";
/**
* for dispatching to the method specified...
*
* @param req
* @param res
* @throws ServletException
* @throws java.io.IOException
*/
public void service(HttpServletRequest req, HttpServletResponse res)
throws ServletException, java.io.IOException {
String actionVerb = getActionVerb();
String what = req.getParameter(actionVerb);
if (what == null) {
what = "defaultAction";
}
try {
Method method = this.getClass().getMethod(what, args);
Object[] objs = {req, res};
method.invoke(this, objs);
}
catch (NoSuchMethodException nsme) {
unknownAction(req, res);
}
catch (Exception e) {
log("BasicVelocityActionServlet.service() : exception", e);
}
}
public void init()
throws ServletException {
/*
* get the default properties from the classloader
*/
Properties p = new Properties();
try {
InputStream is = getClass().getClassLoader().getResourceAsStream(DEFAULT_PROPS);
p.load(is);
}
catch (Exception e) {
log("BasicVelocityActionServlet : default " + DEFAULT_PROPS + " not found.", e);
throw new ServletException(e);
}
/*
* run through them and use them
*/
for (Enumeration en = p.propertyNames(); en.hasMoreElements();) {
String key = (String) en.nextElement();
velEngine.setProperty(key, p.getProperty(key));
}
/*
* for now, log to the servlet log
*/
org.apache.geronimo.jmxdebug.web.velocity.ServletLogger sl = new org.apache.geronimo.jmxdebug.web.velocity.ServletLogger(getServletContext());
velEngine.setProperty(RuntimeConstants.RUNTIME_LOG_LOGSYSTEM, sl);
/*
* set an app context for the webapploader if we are using it
*/
ServletAppContext vssac = new ServletAppContext(getServletContext());
velEngine.setApplicationAttribute(WebappLoader.KEY, vssac);
try {
velEngine.init();
}
catch (Exception e) {
log("BasicVelocityActionServlet", e);
throw new ServletException(e);
}
}
/**
* Defines the 'action verb' for the app
* @return
*/
protected abstract String getActionVerb();
/**
* Called when there is a request w/ no action verb
*
* @param req
* @param res
* @throws ServletException
* @throws java.io.IOException
*/
public abstract void defaultAction(HttpServletRequest req, HttpServletResponse res)
throws ServletException, java.io.IOException;
/**
* Called when there is a request w/ invalid action verb
*
* @param req
* @param res
* @throws ServletException
* @throws java.io.IOException
*/
public abstract void unknownAction(HttpServletRequest req, HttpServletResponse res)
throws ServletException, java.io.IOException;
protected VelocityEngine getVelocityEngine() {
return this.velEngine;
}
protected boolean renderTemplate(HttpServletRequest req,
HttpServletResponse res,
VelocityContext vc, String template) {
boolean result = false;
try {
Template t = getVelocityEngine().getTemplate(template);
t.merge(vc, res.getWriter());
result = true;
}
catch (Exception e) {
e.printStackTrace();
}
return result;
}
/**
* little wrapper class to safely pass the ServletContext to the loader
*/
public class ServletAppContext implements WebappLoader.WebappLoaderAppContext {
ServletContext servletContext = null;
ServletAppContext(ServletContext sc) {
servletContext = sc;
}
public ServletContext getServletContext() {
return servletContext;
}
}
}
1.1 incubator-geronimo/applications/jmxdebug/src/java/org/apache/geronimo/jmxdebug/web/velocity/DebugServlet.java
Index: DebugServlet.java
===================================================================
/**
*
* Copyright 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.geronimo.jmxdebug.web.velocity;
import org.apache.velocity.VelocityContext;
import org.apache.geronimo.jmxdebug.web.beanlib.MBeanServerHelper;
import org.apache.geronimo.jmxdebug.web.beanlib.MBeanInfoHelper;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.ServletException;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;
/**
* Simple servlet for looking at mbeans
*
* @version $Id: DebugServlet.java,v 1.1 2004/02/18 15:33:41 geirm Exp $
*/
public class DebugServlet extends BasicVelocityActionServlet {
public static String OBJECT_NAME_FILTER_KEY = "ObjectNameFilter";
protected String getActionVerb() {
return "action";
}
/**
* The only real action - just puts the mbean server helper in the
* context, and if there was a mbean specified for details, shoves
* a MBeanINfoHelper in the context
*
* @param req
* @param res
* @throws ServletException
* @throws IOException
*/
public void defaultAction(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {
String beanName = req.getParameter("MBeanName");
String filterKey = req.getParameter(OBJECT_NAME_FILTER_KEY);
if (filterKey == null || "".equals(filterKey)) {
filterKey = "*:*";
}
VelocityContext vc = new VelocityContext();
vc.put("mbctx", new MBeanServerHelper());
vc.put("encoder", new KickSunInHead());
vc.put(OBJECT_NAME_FILTER_KEY, filterKey);
if (beanName == null) {
vc.put("template", "nobean.vm");
}
else {
vc.put("template", "mbeaninfo.vm");
vc.put("beanInfo", new MBeanInfoHelper(beanName));
}
renderTemplate(req, res, vc, "index.vm");
}
public void unknownAction(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {
defaultAction(req, res);
}
/**
* Why oh why couldn't this be one class...
*/
public class KickSunInHead {
public String decode(String s) {
return URLDecoder.decode(s);
}
public String encode(String s) {
return URLEncoder.encode(s);
}
public String encode(String s, String encoding) {
try {
return URLEncoder.encode(s, encoding);
}
catch (UnsupportedEncodingException uee) {
uee.printStackTrace();
}
return null;
}
}
}
1.1 incubator-geronimo/applications/jmxdebug/src/java/org/apache/geronimo/jmxdebug/web/velocity/ServletLogger.java
Index: ServletLogger.java
===================================================================
/**
*
* Copyright 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.geronimo.jmxdebug.web.velocity;
import org.apache.velocity.runtime.log.LogSystem;
import org.apache.velocity.runtime.RuntimeServices;
import org.apache.velocity.runtime.RuntimeConstants;
import javax.servlet.ServletContext;
/**
* Simple wrapper for the servlet log.
*
* @version $Id: ServletLogger.java,v 1.1 2004/02/18 15:33:41 geirm Exp $
*/
public class ServletLogger {
private ServletContext servletContext = null;
private static final String PREFIX = " Velocity ";
public ServletLogger(ServletContext sc) {
servletContext = sc;
}
/**
* init()
*/
public void init(RuntimeServices rs)
throws Exception {
}
/**
* Send a log message from Velocity.
*/
public void logVelocityMessage(int level, String message) {
switch (level) {
case LogSystem.WARN_ID:
servletContext.log(PREFIX + RuntimeConstants.WARN_PREFIX + message);
break;
case LogSystem.INFO_ID:
servletContext.log(PREFIX + RuntimeConstants.INFO_PREFIX + message);
break;
case LogSystem.DEBUG_ID:
servletContext.log(PREFIX + RuntimeConstants.DEBUG_PREFIX + message);
break;
case LogSystem.ERROR_ID:
servletContext.log(PREFIX + RuntimeConstants.ERROR_PREFIX + message);
break;
default:
servletContext.log(PREFIX + " : " + message);
break;
}
}
}
1.1 incubator-geronimo/applications/jmxdebug/src/java/org/apache/geronimo/jmxdebug/web/velocity/WebappLoader.java
Index: WebappLoader.java
===================================================================
/**
*
* Copyright 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.geronimo.jmxdebug.web.velocity;
import org.apache.velocity.runtime.resource.loader.ResourceLoader;
import org.apache.velocity.runtime.resource.Resource;
import org.apache.velocity.exception.ResourceNotFoundException;
import javax.servlet.ServletContext;
import java.io.InputStream;
import org.apache.commons.collections.ExtendedProperties;
/**
* Simple webapp loader. This code has been recycled from contributions to Velocity
* by me.
*
* @version $Id: WebappLoader.java,v 1.1 2004/02/18 15:33:41 geirm Exp $
*/
public class WebappLoader extends ResourceLoader {
public ServletContext servletContext = null;
public static String KEY = "org.apache.geronimo.console.web.velocity.WebappLoader";
/**
* This is abstract in the base class, so we need it
*/
public void init(ExtendedProperties configuration) {
rsvc.info("WebappLoader : initialization starting.");
Object o = rsvc.getApplicationAttribute(KEY);
if (o instanceof WebappLoaderAppContext) {
servletContext = ((WebappLoaderAppContext) o).getServletContext();
}
else
rsvc.error("WebappLoader : unable to retrieve ServletContext");
rsvc.info("WebappLoader : initialization complete.");
}
/**
* Get an InputStream so that the Runtime can build a
* template with it.
*
* @param name name of template to get
* @return InputStream containing the template
* @throws org.apache.velocity.exception.ResourceNotFoundException
* if template not found
* in classpath.
*/
public synchronized InputStream getResourceStream(String name)
throws ResourceNotFoundException {
if (name == null || name.length() == 0) {
throw new ResourceNotFoundException("No template name provided");
}
try {
if (!name.startsWith("/"))
name = "/" + name;
return servletContext.getResourceAsStream(name);
}
catch (Exception fnfe) {
/*
* log and convert to a general Velocity ResourceNotFoundException
*/
throw new ResourceNotFoundException(fnfe.getMessage());
}
}
/**
* Defaults to return false.
*/
public boolean isSourceModified(Resource resource) {
return false;
}
/**
* Defaults to return 0
*/
public long getLastModified(Resource resource) {
return 0;
}
public interface WebappLoaderAppContext {
public ServletContext getServletContext();
}
}
1.1 incubator-geronimo/applications/jmxdebug/src/java/org/apache/geronimo/jmxdebug/web/velocity/velocity.defaults
Index: velocity.defaults
===================================================================
resource.loader = webapp
webapp.resource.loader.class = org.apache.geronimo.jmxdebug.web.velocity.WebappLoader