All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.directwebremoting.servlet.MonitorHandler Maven / Gradle / Ivy

Go to download

DWR is easy Ajax for Java. It makes it simple to call Java code directly from Javascript. It gets rid of almost all the boiler plate code between the web browser and your Java code.

There is a newer version: 3.0.2-RELEASE
Show newest version
/*
 * Copyright 2005 Joe Walker
 *
 * 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.directwebremoting.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.SortedMap;
import java.util.TreeMap;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.directwebremoting.Container;
import org.directwebremoting.WebContext;
import org.directwebremoting.WebContextFactory;
import org.directwebremoting.extend.Handler;
import org.directwebremoting.util.LocalUtil;
import org.directwebremoting.util.MimeConstants;

/**
 * A monitoring system so we can see what is going on inside DWR
 * @author Joe Walker [joe at getahead dot ltd dot uk]
 */
public class MonitorHandler implements Handler
{
    /* (non-Javadoc)
     * @see org.directwebremoting.extend.Handler#handle(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
     */
    public void handle(HttpServletRequest request, HttpServletResponse response) throws IOException
    {
        if (!debug)
        {
            log.warn("Failed attempt to access test pages outside of debug mode. Set the debug init-parameter to true to enable.");
            throw new SecurityException("Access to debug pages is denied.");
        }

        response.setContentType(MimeConstants.MIME_HTML);
        PrintWriter out = response.getWriter();
        out.print("DWR - System Monitor");

        WebContext webContext = WebContextFactory.get();

        out.print("

DWR - System Monitor

"); out.print("

Global Settings:

"); String contextPath = webContext.getContextPath(); out.print("

ContextPath: " + contextPath + "

"); out.print("

Current Page: " + webContext.getCurrentPage() + "

"); //ScriptSession scriptSession = webContext.getScriptSession(); Container container = webContext.getContainer(); SortedMap beans = new TreeMap(); SortedMap settings = new TreeMap(); SortedMap urls = new TreeMap(); for (String name : container.getBeanNames()) { Object bean = container.getBean(name); if (name.startsWith("url:")) { urls.put(name.substring(4), bean.getClass().getName()); } else if (bean instanceof String) { settings.put(name, bean.toString()); } else { beans.put(name, bean); } } // Add all the beans to an ID map for IdManager ids = new IdManager(); // Remove the URL re-writers from the Settings map for (Map.Entry urlEntry : urls.entrySet()) { for (Iterator> it = settings.entrySet().iterator(); it.hasNext();) { Map.Entry settingEntry = it.next(); if (urlEntry.getKey().equals(settingEntry.getValue())) { it.remove(); urls.put(urlEntry.getKey(), urlEntry.getValue() + " (" + settingEntry.getKey() + ")"); } } } out.print("

Beans:

"); for (Map.Entry entry : beans.entrySet()) { String name = entry.getKey(); Object object = entry.getValue(); digWhatever(out, ids, name, object); } out.print("

Settings:

"); for (Map.Entry entry : settings.entrySet()) { out.print("

" + entry.getKey() + ": \"" + entry.getValue() + "\"

"); } out.print("

URLs:

"); String prefix = contextPath + webContext.getHttpServletRequest().getServletPath(); for (Map.Entry entry : urls.entrySet()) { out.print("

" + entry.getKey() + ": " + entry.getValue() + "

"); } webContext.getContextPath(); out.print(""); } /** * @param out * @param ids * @param name * @param object */ private void digWhatever(PrintWriter out, IdManager ids, String name, Object object) { if (object == null || object instanceof Number || object instanceof Boolean) { digSimple(out, name, object); } else if (object instanceof Collection) { Collection collection = (Collection) object; boolean simple = true; for (Object child : collection) { if (!(child instanceof Number) && !(child instanceof Boolean) && !(child instanceof String)) { simple = false; } } if (simple) { digSimple(out, name, object); } else { digCollection(out, ids, name, collection); } } else if (object instanceof Map) { digMap(out, ids, name, (Map) object); } else if (object.getClass().getName().startsWith("java") || object.getClass().getName().startsWith("com.sun")) { digSimple(out, name, object); } else if (object instanceof String) { digString(out, name, (String) object); } else { digObject(out, ids, name, object); } } /** * @param out * @param name * @param object */ private void digSimple(PrintWriter out, String name, Object object) { out.print("
"); out.print("
" + name + ": " + object.toString() + "
"); out.print("
"); } /** * @param out * @param name * @param object */ private void digString(PrintWriter out, String name, String object) { out.print("
"); out.print("
" + name + ": \"" + object + "\"
"); out.print("
"); } /** * */ private void digCollection(PrintWriter out, IdManager ids, String name, Collection collection) { out.print("
"); String id = ids.getIfExists(collection); if (id != null) { out.print("
" + name + ": Map
"); } else { id = ids.allocate(collection); out.print(""); out.print("
    "); int i = 0; for (Object entry : collection) { digWhatever(out, ids, "" + i, entry.toString()); i++; } out.print("
"); } } /** * */ private void digMap(PrintWriter out, IdManager ids, String name, Map map) { out.print("
"); String id = ids.getIfExists(map); if (id != null) { out.print("
" + name + ": Map
"); } else { id = ids.allocate(map); out.print(""); out.print("
    "); for (Map.Entry entry : map.entrySet()) { Object value = entry.getValue(); digWhatever(out, ids, entry.getValue().toString(), value); } out.print("
"); } } /** * */ private void digException(PrintWriter out, String name, Throwable ex) { out.print("
"); out.print("
" + name + ": " + ex.getClass().getSimpleName() + "(" + ex.getMessage() + ")
"); out.print("
"); } /** * */ private void digObject(PrintWriter out, IdManager ids, String name, Object object) { Class type = object.getClass(); if (object instanceof Log) { return; } out.print("
"); String id = ids.getIfExists(object); if (id != null) { out.print(""); } else { id = ids.allocate(object); out.print("
" + name + ": " + type.getName() + "
"); out.print("
    "); for (Field field : LocalUtil.getAllFields(type)) { try { if (Modifier.isStatic(field.getModifiers())) { continue; } field.setAccessible(true); Object value = field.get(object); digWhatever(out, ids, field.getName(), value); } catch (Exception ex) { digException(out, field.getName(), ex); } } out.print("
"); } } protected static class IdManager { /** * */ public String getIfExists(Object object) { return allocated.get(object); } protected String allocate(Object object) { if (allocated.containsKey(object)) { throw new IllegalStateException("object already exists"); } String id = "id" + nextId; nextId++; allocated.put(object, id); return id; } private int nextId; private Map allocated = new HashMap(); } /** * Set the debug status * @param debug The new debug setting */ public void setDebug(boolean debug) { this.debug = debug; } /** * Are we in debug mode? */ protected boolean debug = false; /** * The log stream */ private static final Log log = LogFactory.getLog(MonitorHandler.class); }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy