[CVS spice] Adding a PicoJettyContainer, with same functionality as AvalonJettyContainer, plus a configuration bean and a minimal/unfinished testcase.

sjoberg-yCVjj/[email protected] 10 Aug 2005 22:27:40 -0000
Newsgroups gmane.comp.java.spice.cvs
Message-ID <[email protected]>
<html>
<head>
<style><!--
  body {background-color:#ffffff;}
  .file {border:1px solid #eeeeee;margin-top:1em;margin-bottom:1em;}
  .pathname {font-family:monospace; float:right;}
  .fileheader {margin-bottom:.5em;}
  .diff {margin:0;}
  .tasklist {padding:4px;border:1px dashed #000000;margin-top:1em;}
  .tasklist ul {margin-top:0;margin-bottom:0;}
  tr.alt {background-color:#eeeeee}
  #added {background-color:#ddffdd;}
  #addedchars {background-color:#99ff99;font-weight:bolder;}
  tr.alt #added {background-color:#ccf7cc;}
  #removed {background-color:#ffdddd;}
  #removedchars {background-color:#ff9999;font-weight:bolder;}
  tr.alt #removed {background-color:#f7cccc;}
  #info {color:#888888;}
  #context {background-color:#eeeeee;}
  td {padding-left:.3em;padding-right:.3em;}
  tr.head {border-bottom-width:1px;border-bottom-style:solid;}
  tr.head td {padding:0;padding-top:.2em;}
  .task {background-color:#ffff00;}
  .comment {padding:4px;border:1px dashed #000000;background-color:#ffffdd}
  .error {color:red;}
  hr {border-width:0px;height:2px;background:black;}
--></style>
</head>
<body>
<table cellspacing="0" cellpadding="0" border="0" rules="cols">
<tr class="head"><td colspan="4">Commit in <b><tt>spice/components/jervlet/src</tt></b><span id="info"> on MAIN</span></td></tr>
<tr><td><tt>java/org/codehaus/spice/jervlet/containers/jetty/pico/<a href="#file1"><span id="added">DefaultJettyContainerConfiguration.java</span></a></tt></td><td align="right" id="added">+109</td><td></td><td nowrap="nowrap" align="right">added 1.1</td></tr>
<tr class="alt"><td><tt>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;/<a href="#file2"><span id="added">JettyContainerConfiguration.java</span></a></tt></td><td align="right" id="added">+49</td><td></td><td nowrap="nowrap" align="right">added 1.1</td></tr>
<tr><td><tt>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;/<a href="#file3"><span id="added">PicoJettyContainer.java</span></a></tt></td><td align="right" id="added">+221</td><td></td><td nowrap="nowrap" align="right">added 1.1</td></tr>
<tr class="alt"><td><tt>test/org/codehaus/spice/jervlet/containers/jetty/pico/<a href="#file4"><span id="added">PicoJettyContainerTestCase.java</span></a></tt></td><td align="right" id="added">+95</td><td></td><td nowrap="nowrap" align="right">added 1.1</td></tr>
<tr><td></td><td align="right" id="added">+474</td><td></td><td></td></tr>
</table>
<small id="info">4 added files</small><br />
<pre class="comment">
Adding a PicoJettyContainer, with same functionality as AvalonJettyContainer, plus a configuration bean and a minimal/unfinished testcase.
</pre>
<hr /><a name="file1" /><div class="file">
<span class="pathname" id="added">spice/components/jervlet/src/java/org/codehaus/spice/jervlet/containers/jetty/pico<br /></span>
<div class="fileheader" id="added"><big><b>DefaultJettyContainerConfiguration.java</b></big> <small id="info">added at 1.1</small></div>
<pre class="diff"><small id="info">diff -N DefaultJettyContainerConfiguration.java
--- /dev/null	1 Jan 1970 00:00:00 -0000
+++ DefaultJettyContainerConfiguration.java	10 Aug 2005 22:27:40 -0000	1.1
@@ -0,0 +1,109 @@
</small></pre><pre class="diff" id="added">+/*
+ * Copyright (C) The Spice Group. All rights reserved.
+ *
+ * This software is published under the terms of the Spice
+ * Software License version 1.1, a copy of which has been included
+ * with this distribution in the LICENSE.txt file.
+ */
+package org.codehaus.spice.jervlet.containers.jetty.pico;
+
+import java.util.Properties;
+import java.net.URL;
+
+/**
+ * Default Jetty container configuration
+ *
+ * @author Johan Sjoberg
+ */
+public class DefaultJettyContainerConfiguration
+    implements JettyContainerConfiguration
+{
+    /** Shield Jetty or not? */
+    private boolean m_shieldJetty = false;
+
+    /** Jetty's system properties */
+    private Properties m_jettyProperties = null;
+
+    /** Jetty's configuration */
+    private Object m_jettyConfiguration = null;
+
+    /**
+     * Flag indication whether the Jetty instance should be shielded
+     * or not. If not, no Jetty properties can be used.
+     *
+     * @return true if Jetty should be shielded, else false
+     */
+    public boolean shieldJetty()
+    {
+        return m_shieldJetty;
+    }
+
+    /**
+     * Jetty properties can be used to set any so called system
+     * parameter Jetty understands. Note, if shielding of Jetty
+     * is not turned on any possible parameters here will not
+     * be used.
+     *
+     * @return A properties object with system parameters for Jetty,
+     *         or null
+     */
+    public Properties getProperties()
+    {
+        return m_jettyProperties;
+    }
+
+    /**
+     * Possible configuration for Jetty. There are three ways
+     * to configure Jetty, with a String holding the path to
+     * Jetty's XML configuration file, a String that IS the
+     * XML configuration itself or with a URL also pointing
+     * at the configuration file.
+     *
+     * @return Information about Jetty's configuration file or null
+     */
+    public Object getConfiguration()
+    {
+        return m_jettyConfiguration;
+    }
+
+    /**
+     * Set Jetty's properties
+     *
+     * @param properties the properties to add to Jetty
+     */
+    public void setProperties( final Properties properties )
+    {
+        if( null == properties )
+        {
+            m_shieldJetty = false;
+        }
+        else
+        {
+            m_shieldJetty = true;
+        }
+        m_jettyProperties = properties;
+    }
+
+    /**
+     * Set Jetty's configuration. There are three ways
+     * to configure Jetty, with a String holding the path to
+     * Jetty's XML configuration file, a String that IS the
+     * XML configuration itself or with a URL also pointing
+     * at the configuration file.
+     *
+     * @param configuration a String, a URL or null to reset
+     */
+    public void setConfiguration( final Object configuration )
+    {
+        if( null == configuration ||
+            configuration instanceof String ||
+            configuration instanceof URL )
+        {
+            m_jettyConfiguration = configuration;
+        }
+        else
+        {
+            throw new IllegalArgumentException( "Only String, URL or null are acceptable.");
+        }
+    }
+}
</pre></div>
<hr /><a name="file2" /><div class="file">
<span class="pathname" id="added">spice/components/jervlet/src/java/org/codehaus/spice/jervlet/containers/jetty/pico<br /></span>
<div class="fileheader" id="added"><big><b>JettyContainerConfiguration.java</b></big> <small id="info">added at 1.1</small></div>
<pre class="diff"><small id="info">diff -N JettyContainerConfiguration.java
--- /dev/null	1 Jan 1970 00:00:00 -0000
+++ JettyContainerConfiguration.java	10 Aug 2005 22:27:40 -0000	1.1
@@ -0,0 +1,49 @@
</small></pre><pre class="diff" id="added">+/*
+ * Copyright (C) The Spice Group. All rights reserved.
+ *
+ * This software is published under the terms of the Spice
+ * Software License version 1.1, a copy of which has been included
+ * with this distribution in the LICENSE.txt file.
+ */
+package org.codehaus.spice.jervlet.containers.jetty.pico;
+
+import java.net.URL;
+import java.util.Properties;
+
+/**
+ * Configuration for a JettyContainer.
+ *
+ * @author Johan Sjoberg
+ */
+public interface JettyContainerConfiguration
+{
+    /**
+     * Flag indication whether the Jetty instance should be shielded
+     * or not. If not, no Jetty properties can be used.
+     *
+     * @return true if Jetty should be shielded, else false
+     */
+    boolean shieldJetty();
+
+    /**
+     * Jetty properties can be used to set any so called system
+     * parameter Jetty understands. Note, if shielding of Jetty
+     * is not turned on any possible parameters here will not
+     * be used.
+     *
+     * @return A properties object with system parameters for Jetty,
+     *         or null
+     */
+    Properties getProperties();
+
+    /**
+     * Possible configuration for Jetty. There are three ways
+     * to configure Jetty, with a String holding the path to
+     * Jetty's XML configuration file, a String that IS the
+     * XML configuration itself or with an URL also pointing
+     * at the configuration file.
+     *
+     * @return Information about Jetty's configuration file or null
+     */
+    Object getConfiguration();
+}
</pre><pre class="diff"><small id="info">\ No newline at end of file
</small></pre></div>
<hr /><a name="file3" /><div class="file">
<span class="pathname" id="added">spice/components/jervlet/src/java/org/codehaus/spice/jervlet/containers/jetty/pico<br /></span>
<div class="fileheader" id="added"><big><b>PicoJettyContainer.java</b></big> <small id="info">added at 1.1</small></div>
<pre class="diff"><small id="info">diff -N PicoJettyContainer.java
--- /dev/null	1 Jan 1970 00:00:00 -0000
+++ PicoJettyContainer.java	10 Aug 2005 22:27:40 -0000	1.1
@@ -0,0 +1,221 @@
</small></pre><pre class="diff" id="added">+/*
+ * Copyright (C) The Spice Group. All rights reserved.
+ *
+ * This software is published under the terms of the Spice
+ * Software License version 1.1, a copy of which has been included
+ * with this distribution in the LICENSE.txt file.
+ */
+package org.codehaus.spice.jervlet.containers.jetty.pico;
+
+import java.io.File;
+import java.util.List;
+
+import org.picocontainer.Startable;
+
+import org.codehaus.spice.jervlet.Container;
+import org.codehaus.spice.jervlet.ContextHandler;
+import org.codehaus.spice.jervlet.ContextMonitor;
+import org.codehaus.spice.jervlet.Listener;
+import org.codehaus.spice.jervlet.ListenerException;
+import org.codehaus.spice.jervlet.ListenerHandler;
+import org.codehaus.spice.jervlet.ListenerMonitor;
+import org.codehaus.spice.jervlet.containers.jetty.DefaultJettyContainer;
+import org.codehaus.spice.jervlet.containers.jetty.JettyContainer;
+import org.codehaus.spice.jervlet.containers.jetty.ShieldingJettyContainer;
+
+/**
+ * Pico component wrapping a Jetty Server
+ *
+ * @author Johan Sjoberg
+ */
+public class PicoJettyContainer implements Container, ListenerHandler, Startable
+{
+    private JettyContainer m_container;
+
+    public PicoJettyContainer( JettyContainerConfiguration configuration,
+                               ContextMonitor contextMonitor,
+                               ListenerMonitor listenerMonitor )
+
+    {
+        if( null != configuration &amp;&amp; configuration.shieldJetty() )
+        {
+            final ShieldingJettyContainer shieldingJettyContainer =
+              new ShieldingJettyContainer();
+            shieldingJettyContainer.addJettyProperties(
+              configuration.getProperties() );
+            m_container = shieldingJettyContainer;
+        }
+        else
+        {
+            m_container = new DefaultJettyContainer();
+        }
+        Object jettyConfiguration = getJettyConfiguration( configuration );
+        if( null != jettyConfiguration )
+        {
+            m_container.setJettyConfiguration( jettyConfiguration );
+        }
+        if( null != contextMonitor )
+        {
+            m_container.setContextMonitor( contextMonitor );
+        }
+        if( null != listenerMonitor )
+        {
+            m_container.setListenerMonitor( listenerMonitor );
+        }
+    }
+
+    /**
+     * Start the Jetty container
+     *
+     * @throws RuntimeException on all errors
+     */
+    public void start()
+    {
+        try
+        {
+            m_container.initialize();
+            m_container.start();
+        }
+        catch( Exception e )
+        {
+            throw new RuntimeException( e );
+        }
+    }
+
+    /**
+     * Stop the Jetty container
+     *
+     * @throws RuntimeException on all errors
+     */
+    public void stop()
+    {
+        try
+        {
+            m_container.stop();
+        }
+        catch( Exception e )
+        {
+            throw new RuntimeException( e );
+        }
+    }
+
+    /**
+     * Fetch the configuration object to instantiate Jetty with.
+     * &lt;br/&gt;&lt;/br&gt;
+     * There are four possible return values;
+     * (i) a &lt;code&gt;String&lt;/code&gt; representing a Jetty configuration file,
+     * (ii) a &lt;code&gt;String&lt;/code&gt; that IS the Jetty XML configuration,
+     * (iii) a &lt;code&gt;URL&lt;/code&gt; pointing at a Jetty configuration and
+     * (iv) null.
+     * No checks about the configuration's correctness are done here.
+     * Note that Jetty's XML configuration always starts with a
+     * &lt;b&gt;&amp;lt;Configure&amp;gt;&lt;/b&gt; element.
+     *
+     * @param configuration the configuration
+     * @return &lt;code&gt;String&lt;/code&gt;, &lt;code&gt;URL&lt;/code&gt; or &lt;code&gt;null&lt;/code&gt;
+     */
+    private Object getJettyConfiguration( final JettyContainerConfiguration configuration )
+    {
+        if( null == configuration || null == configuration.getConfiguration() )
+        {
+            return null;
+        }
+        if( configuration.getConfiguration() instanceof String )
+        {
+            String configurationString = (String)configuration.getConfiguration();
+            configurationString.trim();
+            if( configurationString.startsWith( "&lt;" ) )
+            {
+                if( configurationString.indexOf( "?&gt;" ) &gt; 0 )
+                {
+                    configurationString = configurationString.substring(
+                      configurationString.indexOf( "?&gt;" ) + 2 );
+                }
+                return configurationString;
+            }
+            else
+            {
+                configurationString = configurationString.replace( '\\', File.separatorChar );
+                configurationString = configurationString.replace( '/', File.separatorChar );
+                return configurationString;
+            }
+        }
+        return configuration.getConfiguration();
+    }
+
+    /**
+     * Create a new context handler.
+     *
+     * @return A new ContextHandler.
+     */
+    public ContextHandler createContextHandler()
+    {
+        return m_container;
+    }
+
+    /**
+     * Destroy a context handler. If the given context
+     * handler still has contexts, they will be stopped
+     * and removed before destruction.
+     *
+     * @param contextHandler The ContextHandler to destroy
+     */
+    public void destroyContextHandler( ContextHandler contextHandler )
+    {
+        m_container.destroyContextHandler( contextHandler );
+    }
+
+    /**
+     * Add a &lt;code&gt;Listener&lt;/code&gt; the container.
+     */
+    public void addListener( Listener listener ) throws ListenerException
+    {
+        m_container.addListener( listener );
+    }
+
+    /**
+     * Remove a &lt;code&gt;Listener&lt;/code&gt; from the container.
+     */
+    public void removeListener( Listener listener ) throws ListenerException
+    {
+        m_container.removeListener( listener );
+    }
+
+    /**
+     * Start a &lt;code&gt;Listener&lt;/code&gt;.
+     */
+    public void startListener( Listener listener ) throws ListenerException
+    {
+        m_container.startListener( listener );
+    }
+
+    /**
+     * Stop a &lt;code&gt;Listener&lt;/code&gt;.
+     */
+    public void stopListener( Listener listener ) throws ListenerException
+    {
+        m_container.stopListener( listener );
+    }
+
+    /**
+     * Fetch a list of all current &lt;code&gt;Listener&lt;/code&gt;s. If there are
+     * no listener the returned list can be empty.
+     *
+     * @return All new list all current listeners.
+     */
+    public List getListeners()
+    {
+        return m_container.getListeners();
+    }
+
+    /**
+     * Check if a &lt;code&gt;Listener&lt;/code&gt; is started or not.
+     *
+     * @param listener The listener
+     * @return True if the listener is started, else false.
+     */
+    public boolean isStarted( Listener listener )
+    {
+        return m_container.isStarted( listener );
+    }
+}
</pre></div>
<hr /><a name="file4" /><div class="file">
<span class="pathname" id="added">spice/components/jervlet/src/test/org/codehaus/spice/jervlet/containers/jetty/pico<br /></span>
<div class="fileheader" id="added"><big><b>PicoJettyContainerTestCase.java</b></big> <small id="info">added at 1.1</small></div>
<pre class="diff"><small id="info">diff -N PicoJettyContainerTestCase.java
--- /dev/null	1 Jan 1970 00:00:00 -0000
+++ PicoJettyContainerTestCase.java	10 Aug 2005 22:27:40 -0000	1.1
@@ -0,0 +1,95 @@
</small></pre><pre class="diff" id="added">+/*
+ * Copyright (C) The Spice Group. All rights reserved.
+ *
+ * This software is published under the terms of the Spice
+ * Software License version 1.1, a copy of which has been included
+ * with this distribution in the LICENSE.txt file.
+ */
+package org.codehaus.spice.jervlet.containers.jetty.pico;
+
+import junit.framework.TestCase;
+
+import java.util.Properties;
+
+import org.codehaus.spice.jervlet.impl.NoopContextMonitor;
+import org.codehaus.spice.jervlet.impl.NoopListenerMonitor;
+
+/**
+ * TestCase for PicoJettyContainer
+ *
+ * @author Johan Sjoberg
+ */
+public class PicoJettyContainerTestCase extends TestCase
+{
+    private static final String m_defaultConfiguration =
+      "./testdata/jetty/jetty.xml";
+
+    /**
+     * Create an empty container and start/stop it.
+     *
+     * @throws Exception
+     */
+    public void testEmpty() throws Exception
+    {
+        PicoJettyContainer container = new PicoJettyContainer( null, null, null );
+        container.start();
+        container.stop();
+    }
+
+    /**
+     * Create an empty container with Noop monitors and and empty
+     * configuration (same result as testEmpty()).
+     *
+     * @throws Exception
+     */
+    public void testEmptyWithNoopMonitors() throws Exception
+    {
+        PicoJettyContainer container =
+          new PicoJettyContainer( new DefaultJettyContainerConfiguration(),
+                                  new NoopContextMonitor(),
+                                  new NoopListenerMonitor() );
+        container.start();
+        container.stop();
+    }
+
+    /**
+     * Create a container with the default configuration file
+     * that comes with Jetty, and start/stop the container.
+     *
+     * @throws Exception
+     */
+    public void testDefaultConfigurationFile() throws Exception
+    {
+        DefaultJettyContainerConfiguration configuration =
+          new DefaultJettyContainerConfiguration();
+        configuration.setConfiguration( m_defaultConfiguration );
+        PicoJettyContainer container = new PicoJettyContainer( configuration,
+                                                               null,
+                                                               null );
+        container.start();
+        container.stop();
+    }
+
+    /**
+     * Create a container with one property (jetty.port) and
+     * start/stop it using the default configuration.
+     *
+     * @throws Exception
+     */
+    public void testProperties() throws Exception
+    {
+        Properties properties = new Properties();
+        properties.setProperty( "jetty.port", "8421" );
+
+        DefaultJettyContainerConfiguration configuration =
+          new DefaultJettyContainerConfiguration();
+        configuration.setConfiguration( m_defaultConfiguration );
+        configuration.setProperties( properties );
+
+        PicoJettyContainer container = new PicoJettyContainer( configuration,
+                                                               new NoopContextMonitor(),
+                                                               new NoopListenerMonitor() );
+        container.start();
+        container.stop();
+    }
+}
</pre></div>
<center><small><a href="http://www.badgers-in-foil.co.uk/projects/cvsspam/" title="commit -&gt; email">CVSspam</a> 0.2.8</small></center>
</body></html>