alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

What this is

This file is included in the DevDaily.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Other links

The source code

/*
 *                 Sun Public License Notice
 * 
 * The contents of this file are subject to the Sun Public License
 * Version 1.0 (the "License"). You may not use this file except in
 * compliance with the License. A copy of the License is available at
 * http://www.sun.com/
 * 
 * The Original Code is NetBeans. The Initial Developer of the Original
 * Code is Sun Microsystems, Inc. Portions Copyright 1997-2003 Sun
 * Microsystems, Inc. All Rights Reserved.
 */

package org.netbeans.modules.tomcat5.ide;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.UnknownHostException;
import java.util.*;
import org.netbeans.api.web.dd.DDProvider;
import org.netbeans.api.web.dd.Filter;
import org.netbeans.api.web.dd.FilterMapping;
import org.netbeans.api.web.dd.InitParam;
import org.netbeans.api.web.dd.WebApp;
import org.netbeans.modules.j2ee.deployment.plugins.api.InstanceProperties;
import org.netbeans.modules.tomcat5.TomcatFactory;
import org.netbeans.modules.tomcat5.TomcatManager;

import org.openide.filesystems.*;
import org.openide.nodes.Node;
import org.openide.modules.ModuleInfo;
import org.openide.util.Lookup;
import org.openide.util.LookupListener;
import org.openide.util.LookupEvent;

import org.openide.ErrorManager;
import org.openide.modules.InstalledFileLocator;
import org.openide.util.NbBundle;
import org.xml.sax.SAXException;

import org.netbeans.modules.schema2beans.Common;
import org.netbeans.modules.schema2beans.BaseBean;

/** Monitor enabling/disabling utilities for Tomcat 5.
 *
 * @author Milan.Kuchtiak@sun.com, Petr Jiricka
 */
public class MonitorSupport {

    // monitor module enable status data
    public static final String MONITOR_ENABLED_PROPERTY_NAME = "monitor_enabled"; // NOI18N
    private static final String MONITOR_MODULE_NAME="org.netbeans.modules.web.monitor"; //NOI18N    
    private static ModuleInfo httpMonitorInfo;
    private static ModuleSpy monitorSpy;
    private static Lookup.Result res;
    private static MonitorInfoListener monitorInfoListener;
    private static MonitorLookupListener monitorLookupListener;
    
    // data for declaration in web.xml
    private static final String MONITOR_FILTER_NAME  = "HTTPMonitorFilter"; //NOI18N
    private static final String MONITOR_FILTER_CLASS = "org.netbeans.modules.web.monitor.server.MonitorFilter"; //NOI18N
    private static final String MONITOR_FILTER_PATTERN = "/*"; //NOI18N
    private static final String MONITOR_INTERNALPORT_PARAM_NAME = "netbeans.monitor.ide"; //NOI18N
    
    public static void setMonitorFlag(String managerURL, boolean enable) {
        InstanceProperties ip = InstanceProperties.getInstanceProperties(managerURL);
        ip.setProperty(MONITOR_ENABLED_PROPERTY_NAME, Boolean.toString(enable));
    }
    
    public static boolean getMonitorFlag(String managerURL) {
        InstanceProperties ip = InstanceProperties.getInstanceProperties(managerURL);
        String prop = ip.getProperty(MONITOR_ENABLED_PROPERTY_NAME);
        return (prop == null) ? true : Boolean.valueOf(prop).booleanValue();
    }
    
    public static void setMonitorFlag(TomcatManager tm, boolean enable) {
        setMonitorFlag(getTomcatManagerURL(tm), enable);
    }
    
    public static boolean getMonitorFlag(TomcatManager tm) {
        return getMonitorFlag(getTomcatManagerURL(tm));
    }
    
    private static String getTomcatManagerURL(TomcatManager tm) {
        return TomcatFactory.tomcatUriPrefix + tm.getUri();
    }
    
    public static void synchronizeMonitorWithFlag(TomcatManager tm, boolean alsoSetPort, boolean alsoCopyJars) throws IOException, SAXException {
        String url = getTomcatManagerURL(tm);
        boolean monitorFlag = getMonitorFlag(url);
        boolean monitorModuleAvailable = isMonitorEnabled();
        boolean shouldInstall = monitorModuleAvailable && monitorFlag;
        
        // find the web.xml file
        File webXML = getDefaultWebXML(tm);
        if (webXML == null) {
            ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, new Exception(url));
            return;
        }
        WebApp webApp = DDProvider.getDefault().getDDRoot(webXML);
        if (webApp == null) {
            ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, new Exception(url));
            return;
        }
        boolean needsSave = false;
        boolean result;
        if (shouldInstall) {
            result = changeFilterMonitor(webApp, true);
            needsSave = needsSave || result;
            if (alsoSetPort) {                  
                result = specifyFilterPortParameter(webApp);
                needsSave = needsSave || result;
            }
            if (alsoCopyJars) {
                addMonitorJars(tm);
            }
        }
        else {                               
            result = changeFilterMonitor(webApp, false);
            needsSave = needsSave || result; 
        }
        if (needsSave) {
            OutputStream os = new FileOutputStream(webXML);
            try {
                webApp.write(os);
            }
            finally {
                os.close();
            }
        }
    }
    
    private static File getDefaultWebXML(TomcatManager tm) {
        File cb = tm.getCatalinaBaseDir();
        if (cb == null)
            cb = tm.getCatalinaHomeDir();
        File webXML = new File(cb, "conf" + File.separator + "web.xml");
        if (webXML.exists())
            return webXML;
        return null;
    }
    
    private static void addMonitorJars(TomcatManager tm) throws IOException {
        // getting Tomcat4.0 Directory
        File instDir = tm.getCatalinaHomeDir();
        if (instDir==null) return;
        
        copyFromIDEInstToDir("modules/ext/httpmonitor.jar"  , instDir, "common/lib/httpmonitor.jar");  // NOI18N
        copyFromIDEInstToDir("modules/org-netbeans-modules-schema2beans.jar" , instDir, "common/lib/org-netbeans-modules-schema2beans.jar"); // NOI18N
        
        //copyFromIDEInstToDir("modules/ext/monitor-valve.jar", instDir, "server/lib/monitor-valve.jar"); // NOI18N
    }
    
    private static boolean changeFilterMonitor(WebApp webApp,boolean full) {
        boolean filterWasChanged=false;
        if (full) { // adding monitor filter/filter-mapping element
            //if (tomcat.isMonitorEnabled()) {
            boolean isFilter=false;
            Filter[] filters = webApp.getFilter();
            for(int i=0;i
... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.