I give up. MVC baby!

On Aug 21, 2005, at 7:24 PM, Philip Weaver wrote:


How can I programmatically load servlets and servlet mappings in Tomcat? I'm using Tomcat 5.0.28.

I'm trying to extend StandardContext to automatically map/assign a batch of servlets from a specified jar at startup. I'm having trouble. As I try to add these servlets programmatically as the StandardContext is starting up, I can't get them to register/load. The servlets loaded from the web.xml work fine, but the ones I'm trying to add programmatically return null ObjectNames when I query context.getServlets(). They are also ignored or not found when I get the URL.

Here's the code I'm trying... The most relevant code is in addServlet() below - which I'm calling after super.start().

package com.luminera.www;

import java.io.*;
import java.util.*;
import javax.management.*;
import javax.naming.*;
import javax.servlet.*;
import org.apache.catalina.*;
import org.apache.catalina.deploy.*;
import org.apache.catalina.loader.*;
import org.apache.catalina.mbeans.*;
import org.apache.catalina.startup.*;
import org.apache.catalina.util.*;
import org.apache.commons.logging.*;
import org.apache.commons.modeler.*;
import org.apache.naming.*;
import org.apache.naming.resources.*;
import org.apache.tomcat.util.compat.*;

public class StandardContext extends org.apache.catalina.core.StandardContext {
        
        public StandardContext() {
                
                super();
        }
        
        public synchronized void start() throws LifecycleException {
                
                super.start();
                
                this.loadServlets();
                
                String[] asServlets = this.getServlets();
                for (int i = 0; i < asServlets.length; i++) {
                        System.out.println("known servlet: " + asServlets[i]);
                }
        }
        
        private void loadServlets() {
                
                String sParameter = this.findParameter("servlets-jar");
                String sBasePath = this.getBasePath();
                System.out.println("sParameter: " + sParameter);
                System.out.println("sBasePath: " + sBasePath);
                if ((sParameter != null) && (sBasePath != null)) {
                        String sJarPath = sBasePath + sParameter;
                        System.out.println("servlet jar path: " + sJarPath);
                        ClassLoader classLoader = 
this.getLoader().getClassLoader();
//HashMap hashMap = ServletUtility.buildServletMap(sJarPath, classLoader); // external lib
                        HashMap hashMap = new HashMap();
for (Iterator iterator = hashMap.keySet().iterator(); iterator.hasNext();) {
                                String sPattern = (String)iterator.next();
                                String sClass = (String)hashMap.get(sPattern);
                                this.addServlet(sPattern, sClass);
                        }
                }
        }
        
        private void addServlet(String sPattern, String sClass) {
                
System.out.println("Trying to load servlet for url pattern: " + sPattern);
                
                Wrapper wrapper = null;
                try {
                        String sName = sClass;
                        wrapper = this.createWrapper();
                        wrapper.setName(sName);
                        wrapper.setServletClass(sClass);
                        this.addChild(wrapper);
                        this.addServletMapping(sPattern, sName);
                        if (wrapper instanceof Lifecycle) {
                                ((Lifecycle)wrapper).start();
                        }
                        System.out.println("succeeded");
                        System.out.println("sPattern: " + sPattern);
                        System.out.println("sName: " + sName);
                } catch (Throwable throwable) {
                        System.out.println("failed");
                        Log log = LogFactory.getLog(StandardContext.class);
                        log.error("Failed to load servlet: " + sClass, 
throwable);
                        this.removeServletMapping(sPattern);
                        this.removeChild(wrapper);
                }
        }
        
        private String getBasePath() {
                
                String docBase = null;
                Container container = this;
                while (container != null) {
                        if (container instanceof Host) {
                                break;
                        }
                        container = container.getParent();
                }
                File file = new File(getDocBase());
                if (!file.isAbsolute()) {
                        if (container == null) {
                                docBase = (new File(engineBase(), 
getDocBase())).getPath();
                        } else {
                                String appBase = ((Host)container).getAppBase();
                                file = new File(appBase);
                                if (!file.isAbsolute()) {
                                        file = new File(engineBase(), appBase);
                                }
                                docBase = (new File(file, 
getDocBase())).getPath();
                        }
                } else {
                        docBase = file.getPath();
                }
                return docBase;
        }
        
}


---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]



---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to