org.beigesoft.ajetty.BootStrap Maven / Gradle / Ivy
Show all versions of a-jetty-base Show documentation
package org.beigesoft.ajetty;
/*
* Beigesoft ™
*
* Licensed under the Apache License, Version 2.0
*
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*/
import java.io.File;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.Connector;
import org.eclipse.jetty.server.ServerConnector;
import org.eclipse.jetty.server.Handler;
import org.eclipse.jetty.server.handler.DefaultHandler;
import org.eclipse.jetty.server.handler.HandlerCollection;
import org.eclipse.jetty.server.handler.ContextHandlerCollection;
import org.eclipse.jetty.deploy.DeploymentManager;
import org.eclipse.jetty.deploy.providers.WebAppProvider;
import org.eclipse.jetty.deploy.PropertiesConfigurationManager;
import org.beigesoft.afactory.IFactoryAppBeans;
/**
*
* BootStrap for Jetty for Android configured as minimum server
* with WebAppDeployer that deploy ordinal non-JSP web-app
* (JSP/JSTL must be precompiled into servlets) that must be unpacked
* into [jetty-base]/webapps.
* It must be follow working directory(jetty:base) containing:
*
* webdefault.xml
* webapps
*
*
*
* @author Yury Demidenko
*/
public class BootStrap {
/**
* Factory app-beans.
**/
private IFactoryAppBeans factoryAppBeans;
/**
* Port.
**/
private int port = 8080;
/**
* Jetty base.
**/
private String jettyBase = "";
/**
* Jetty.
**/
private Server server;
/**
* Is started.
**/
private boolean isStarted = false;
/**
* Deployment Manager.
**/
private DeploymentManager deploymentManager;
/**
* Create and configure server.
* @throws Exception an Exception
**/
public final void createServer() throws Exception {
// Create a basic jetty server object that will listen on port 8080.
// Note that if you set this to port 0 then a randomly available port
// will be assigned that you can either look in the logs for the port,
// or programmatically obtain it for use in test cases.
this.server = new Server();
ServerConnector connector = new ServerConnector(server);
connector.setPort(this.port);
connector.setHost("127.0.0.1");
server.setConnectors(new Connector[]{connector});
// Handlers:
HandlerCollection handlers = new HandlerCollection();
DefaultHandler defaultHandler = new DefaultHandler();
ContextHandlerCollection contextHandlerCollection =
new ContextHandlerCollection();
handlers.setHandlers(new Handler[] {contextHandlerCollection,
defaultHandler }); // !!! defaultHandler must be second
this.server.setHandler(handlers);
// Create the deployment manager:
deploymentManager = new DeploymentManager();
deploymentManager.setContexts(contextHandlerCollection);
WebAppProvider webAppProvider = new WebAppProvider();
webAppProvider.setFactoryAppBeans(this.factoryAppBeans);
webAppProvider.setMonitoredDirName(jettyBase + File.separator + "webapps");
webAppProvider.setDefaultsDescriptor(jettyBase + File.separator
+ "webdefault.xml");
webAppProvider.setExtractWars(false);
PropertiesConfigurationManager confManager =
new PropertiesConfigurationManager();
webAppProvider.setConfigurationManager(confManager);
deploymentManager.addAppProvider(webAppProvider);
this.server.addBean(deploymentManager);
}
/**
* Start server.
* @throws Exception an Exception
**/
public final void startServer() throws Exception {
this.server.start();
this.isStarted = true;
}
/**
* Stop server.
* @throws Exception an Exception
**/
public final void stopServer() throws Exception {
this.server.stop();
this.isStarted = false;
}
/**
* This start preconfigured Jetty on non-Android OS.
* It may takes up to tho parameters: port and jetty:base.
* Example:
*
* java -jar a-jetty-base.jar jetty:base=/home/my/a-jetty
* or
* java -jar a-jetty-base.jar jetty:base=/home/my/a-jetty port=8080
*
*
* @param pArgs arguments
**/
public static final void main(final String[] pArgs) {
try {
BootStrap bootStrap = new BootStrap();
for (String arg : pArgs) {
if (arg.contains("port=")) {
String strPort = arg.replace("port=", "").trim();
bootStrap.setPort(Integer.parseInt(strPort));
} else if (arg.contains("jetty:base=")) {
bootStrap.setJettyBase(arg.replace("jetty:base=", "").trim());
}
}
bootStrap.setFactoryAppBeans(new FactoryAppBeansStd());
bootStrap.createServer();
bootStrap.startServer();
// The use of server.join() the will make the current thread join and
// wait until the server is done executing.
// See http://docs.oracle.com/javase/7/docs/api/java/lang/Thread.html#join
bootStrap.getServer().join();
bootStrap.setIsStarted(false);
} catch (Exception e) {
e.printStackTrace();
}
}
//Simple getters and setters:
/**
* Getter for port.
* @return int
**/
public final int getPort() {
return this.port;
}
/**
* Setter for port.
* @param pPort reference
**/
public final void setPort(final int pPort) {
this.port = pPort;
}
/**
* Getter for jettyBase.
* @return String
**/
public final String getJettyBase() {
return this.jettyBase;
}
/**
* Setter for jettyBase.
* @param pJettyBase reference
**/
public final void setJettyBase(final String pJettyBase) {
this.jettyBase = pJettyBase;
}
/**
* Getter for server.
* @return Server
**/
public final Server getServer() {
return this.server;
}
/**
* Setter for server.
* @param pServer reference
**/
public final void setServer(final Server pServer) {
this.server = pServer;
}
/**
* Getter for isStarted.
* @return boolean
**/
public final boolean getIsStarted() {
return this.isStarted;
}
/**
* Setter for isStarted.
* @param pIsStarted reference
**/
public final void setIsStarted(final boolean pIsStarted) {
this.isStarted = pIsStarted;
}
/**
* Getter for factoryAppBeans.
* @return IFactoryAppBeans
**/
public final IFactoryAppBeans getFactoryAppBeans() {
return this.factoryAppBeans;
}
/**
* Setter for factoryAppBeans.
* @param pFactoryAppBeans reference
**/
public final void setFactoryAppBeans(
final IFactoryAppBeans pFactoryAppBeans) {
this.factoryAppBeans = pFactoryAppBeans;
}
/**
* Getter for deploymentManager.
* @return DeploymentManager
**/
public final DeploymentManager getDeploymentManager() {
return this.deploymentManager;
}
/**
* Setter for deploymentManager.
* @param pDeploymentManager reference
**/
public final void setDeploymentManager(
final DeploymentManager pDeploymentManager) {
this.deploymentManager = pDeploymentManager;
}
}