You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Erik Pearson <er...@cariboulake.com> on 2000/10/03 16:19:11 UTC

Got Struts working with Weblogic 5.1 (sp5+)

After a considerable amount of chasing around e-mails on this subject on
both this and Bea's newsgroups, I was finally able to get the Struts example
working with Weblogic (verified with sp5 and sp6).  Here are the details:

There are three apparent problems with Weblogic that Struts gets into
trouble with.

1. Although Weblogic does support WAR files, They are loaded via an
alternate classloader, so the ApplicationResources.properties file can't be
found from java.util.ResourceBundle.

2. LoadOnStartup is ignored by Weblogic, so the Action can't be init'ed to
preload necessary objects into application scope.

3. Classes used by jsps and servlets must be loaded by jsps first to avoid
the dreaded 'ClassCastException' (there is prolonged discussion on this
point in the Weblogic newsgroups).

There definitely some strange behavior in Weblogic's dynamic class loading
mechanism -- man this thing is a mess!  There may still be problems with
dynamic jsp/servlet reload.  I extended code posted by Glencross, Christian"
<Ch...@gs.com>on this group a while ago to force init'ing
servlets at startup.  The following code takes a WebApp context name and
first pre-compiles all JSPs (this loads classes through the JSP classloader
first).  Then, it looks at Servlet descriptor information and loads all
servlets with a <loadOnStartup> property that's a positive integer. All
other loadOnStartup entries are ignored.  This code is attached at the
bottom.

1. Unjar the struts-example.war file and place it in a directory outside of
both the jav aCLASSPATH and weblogic CLASSPATH.

2. Add a line to add the struts WebApp to the weblogic.properties file:

    # Register Examples

weblogic.httpd.webApp.strutsExample=C:/work/weblogic/struts/struts-example_w
ar

3. Compile the code found at the bottom and place the class file someplace
like the classes directory in weblogic dir.

4. Add the following lines to force preloading of classfiles in the proper
order:

    # preload classes through WebAppStartup T3StartupDef
    weblogic.system.startupClass.strutsExampleStartup=WebAppStartup
    weblogic.system.startupArgs.strutsExampleStartup=\
        webAppContext=strutsExample

That should do it.  One last comment on this code -- let me reiterate
Chris's comment -- this is a nasty hack, which involves undocumented API
methods, and may well have unknown side-effects.  Use at your own risk:



import java.util.Collection;
import java.util.Iterator;
import java.util.TreeMap;
import java.util.Hashtable;
import weblogic.common.T3StartupDef;
import weblogic.common.T3ServicesDef;

public class WebAppStartup implements T3StartupDef {

    T3ServicesDef services;

    public WebAppStartup () {}

    public void setServices(T3ServicesDef services) {
        this.services = services;
    }

    public String startup(String name, Hashtable args) throws Exception
    {
        // Grab arguments
        String webAppContext = (String) args.get( "webAppContext" );
        if ( webAppContext == null ) {
            webAppContext = "";
        }

        // Get the servlet context
        weblogic.servlet.internal.ServletContextManager mgr =
                weblogic.t3.srvr.HttpServer.getServletContextManager();
        if ( mgr == null ) {
            throw new Exception(
                "HttpServer.getServletContextManager() returned null");
        }

        // retrieve servletcontextimpl
        weblogic.servlet.internal.ServletContextImpl context =
            mgr.getContext( webAppContext );
        if ( context == null ) {
            throw new Exception( "Web App context '" + webAppContext +
                    "' is not known" );
        }

        // precompile servlets -- forces jsp classes to be loaded by jsp
        // classloader?
        this.services.log().info("Pre-compiling JSPs...");
        try {
            context.precompileJSPs();
        } catch (Throwable t) {
            this.services.log().error("Error precompiling JSPs: ", t);
        }

        // retrieve servlet with load order > 0 from descriptors
        this.services.log().info(
        		"Loading servlets with specified load order...");
        weblogic.servlet.internal.dd.WebAppDescriptor wd =
        		context.getWebAppDescriptor();
        com.sun.java.util.collections.List csjl = wd.getServletsList();
        com.sun.java.util.collections.Iterator csjit = csjl.iterator();
        weblogic.servlet.internal.dd.ServletDescriptor sd;
        TreeMap tm = new TreeMap();
        int loadSequence;
        while (csjit.hasNext()) {
            sd = (weblogic.servlet.internal.dd.ServletDescriptor)
            			csjit.next();
            loadSequence = sd.getLoadSequence();
            if (loadSequence > 0) {
                tm.put(new Integer(loadSequence), sd);
            }
        }

        // load and invoke servlets to call init() methods as side-effect
        Collection col = tm.values();
        Iterator it = col.iterator();
        weblogic.servlet.internal.ServletStubImpl stub;
        while (it.hasNext()) {
            sd = (weblogic.servlet.internal.dd.ServletDescriptor)it.next();
            this.services.log().info("Loading servlet: "
                    + sd.getServletName() + " (load order: "
                    + sd.getLoadSequence() + ")");
            stub = context.getStubByName( sd.getServletName() );
            if (stub == null) {
                this.services.log().error("Servlet not found: "
                    	+ sd.getServletName() + " (in context: "
                    	+ webAppContext + ")" );
                continue;
            }
            // Invoke stub with null request/response to get init() called
            try {
                stub.invokeServlet(null, null);
                this.services.log().info( "Successfully loaded servlet: '"
                        + stub.getName() + " (in context: "
                        + webAppContext + ")" );
            }
            catch ( NullPointerException npe ) {
                // Nothing - this is to be expected. Yuk.
            }
            catch ( Throwable e ) {
                this.services.log().error( "Error loading servlet: "
                		+ stub.getName() + " (in context: "
                		+ webAppContext + ")", e);
            }
        }
        return "context " + webAppContext + " completed";
    }
}


  -- Erik

--
Erik Pearson                                       Caribou Lake Software
erik@cariboulake.com                         http://www.cariboulake.com/
612.837.9802x20 (office)
612.868.1392 (cell)