Daniel Fagerstrom wrote:
Might be that it will not that easy to create a mock servlet context that contain exactly what is needed for creating beans in a XmlWebApplicationContext. In my experience getting the different context used within Cocoon is never easy. But I can assure you that creating a mock servlet context is a much simpler solution to the problem that you want to solve than refactoring Cocoon so that it doesn't depend on having a servlet context available.

We should of course provide a mock servlet context so that users doesn't need to implement one themselves.

For the particular problem you get, I would need to see what the malformed URL looks like to have any clue about what goes wrong.

It was my mistake in ServetContext.getResource().

Seems I have a running cocoon with no servlet container:
public class Test {
        public static class EmptyEnumeration implements Enumeration {
                public boolean hasMoreElements() {
                        return false;
                }

                public Object nextElement() {
                        return null;
                }
        }

        public static final Enumeration EMPTY_ENUMERATION       = new 
EmptyEnumeration();

        public static class MockServletContext implements ServletContext {
                private static final Log        logger  = LogFactory.getLog( 
ServletContext.class );

                public Object getAttribute( String name ) {
                        if ( name.equals( "javax.servlet.context.tempdir" ) )
                                return new File( "target" );
                        return null;
                }

                public Enumeration getAttributeNames() {
                        return EMPTY_ENUMERATION;
                }

                public ServletContext getContext( String path ) {
                        return null;
                }

                public String getInitParameter( String name ) {
                        return null;
                }

                public Enumeration getInitParameterNames() {
                        return EMPTY_ENUMERATION;
                }

                public int getMajorVersion() {
                        return 2;
                }

                public String getMimeType( String file ) {
                        return null;
                }

                public int getMinorVersion() {
                        return 4;
                }

                public RequestDispatcher getNamedDispatcher( String name ) {
                        return null;
                }

                public String getRealPath( String path ) {
                        return null;
                }

                public RequestDispatcher getRequestDispatcher( String path ) {
                        return null;
                }

                public URL getResource( String path ) throws 
MalformedURLException {
                        String p = path;
                        if ( p.startsWith( "/" ) )
                                p = path.substring( 1 );

                        logger.info( "getResource: " + p );
                        return new File( p ).toURL();
                }

                public InputStream getResourceAsStream( String path ) {
                        try {
                                URL url = getResource( path );
                                if ( url == null )
                                        return null;
                                return url.openStream();
                        } catch ( Exception e ) {
                                logger.debug( e );
                                return null;
                        }
                }

                public Set getResourcePaths( String path ) {
                        // TODO Auto-generated method stub
                        return null;
                }

                public String getServerInfo() {
                        return "mock";
                }

                public Servlet getServlet( String name ) throws 
ServletException {
                        return null;
                }

                public String getServletContextName() {
                        return null;
                }

                public Enumeration getServletNames() {
                        return EMPTY_ENUMERATION;
                }

                public Enumeration getServlets() {
                        return EMPTY_ENUMERATION;
                }

                public void log( String msg ) {
                        logger.info( msg );
                }

                public void log( Exception ex, String text ) {
                        log(    text,
                                        ex );
                }

                public void log( String text, Throwable ex ) {
                        logger.error(   text,
                                                        ex );
                }

                public void removeAttribute( String name ) {
                }

                public void setAttribute( String name, Object value ) {
                }
        }

        public static void main( String[] args ) {
                StopWatch watch = new StopWatch();
                watch.start();
                ServletContext servletContext = new MockServletContext();
                ConfigurableWebApplicationContext context = new 
XmlWebApplicationContext();
                context.setServletContext( servletContext );
                context.setConfigLocations( new String[]{ 
"applicationContext.xml" } );
                context.refresh();
                context.close();
                watch.stop();
                System.out.println( "running time: " + watch.toString() );
        }
}

Now if somebody could show me how to build a pipeline...

--
Leszek Gawron                                    CTO at MobileBox Ltd.

Reply via email to