Source code file content
source-code-repository / trunk / src / jetty / classes / org / wembed / impl / JettyEmbeddedContainer.java
Size: 4625 bytes, 1 line
/*
* Wembed, an embedded servlet container web application launcher.
* Copyright (C) 2008 Universidad de las Islas Baleares(UIB),
* Cra. Valldemossa, km 7.5 07071 Palma de Mallorca(Illes Balears) Espaņa This
* software is the confidential intellectual property of the UIB; it is
* copyrighted and licensed, not sold. This program is free software; you can
* redistribute it and/or modify it under the terms of the GNU Lesser General
* Public License as published by the Free Software Foundation; either version
* 2.1 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
* details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation, Inc.,
* 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
package org.wembed.impl;
import java.io.File;
import java.io.IOException;
import org.mortbay.jetty.Server;
import org.mortbay.jetty.webapp.WebAppContext;
import org.wembed.Configuration;
import org.wembed.EmbeddedContainer;
public class JettyEmbeddedContainer implements EmbeddedContainer
{
private Server theServer = null;
@Override
public void initContainer(int port)
{
System.setProperty("java.naming.factory.initial", "org.mortbay.naming.InitialContextFactory");
this.theServer = new Server(port);
}
@Override
public void configureContext(Configuration theConfiguration)
{
if (this.theServer == null)
{
throw new RuntimeException("Container not yet initialised! Call initContainer(port) first!");
}
WebAppContext theTestContext = new WebAppContext(theConfiguration.getBasePath(), theConfiguration.getContext());
theTestContext.setConfigurationClasses(new String[] { "org.mortbay.jetty.webapp.WebInfConfiguration",
"org.mortbay.jetty.plus.webapp.EnvConfiguration", "org.mortbay.jetty.plus.webapp.Configuration",
"org.mortbay.jetty.webapp.JettyWebXmlConfiguration", "org.mortbay.jetty.webapp.TagLibConfiguration" });
try
{
File overrideWeb = new File(theConfiguration.getBasePath(), "WEB-INF/override-web.xml");
if (overrideWeb.exists())
{
theTestContext.setOverrideDescriptor(overrideWeb.getCanonicalPath());
}
}
catch (IOException e)
{
RuntimeException re = new RuntimeException("Web defaults could not be initialised!");
re.initCause(e);
throw re;
}
// Adding the extra classpath
try
{
File extraClasspathDirectory = new File(theConfiguration.getExtraPath());
if (extraClasspathDirectory.exists() && extraClasspathDirectory.isDirectory())
{
StringBuilder extraClasspath = new StringBuilder();
for (File extra_jar : extraClasspathDirectory.listFiles())
{
if (extra_jar.isFile() && extra_jar.getName().endsWith(".jar"))
{
extraClasspath.append(extra_jar.getCanonicalPath());
extraClasspath.append(File.pathSeparatorChar);
}
}
theTestContext.setExtraClasspath(extraClasspath.toString());
}
}
catch (IOException e)
{
RuntimeException re = new RuntimeException("Web defaults could not be initialised!");
re.initCause(e);
throw re;
}
this.theServer.setHandler(theTestContext);
}
@Override
public void join() throws InterruptedException
{
if (this.theServer == null)
{
throw new RuntimeException("Container not yet initialised! Call initContainer(port) first!");
}
this.theServer.join();
}
@Override
public void start() throws Exception
{
if (this.theServer == null)
{
throw new RuntimeException("Container not yet initialised! Call initContainer(port) first!");
}
this.theServer.start();
}
@Override
public void stop() throws Exception
{
if (this.theServer == null)
{
throw new RuntimeException("Container not yet initialised! Call initContainer(port) first!");
}
this.theServer.stop();
}
@Override
public boolean isStarted()
{
if (this.theServer == null)
{
throw new RuntimeException("Container not yet initialised! Call initContainer(port) first!");
}
return this.theServer.isStarted();
}
}





