server.setConnectors(new Connector[] { connector, sslConnector });
} else {
server.setConnectors(new Connector[] { connector });
}
final WebAppContext webappcontext = new WebAppContext();
webappcontext.setContextPath(serverArgs.get("context"));
webappcontext.setWar(serverArgs.get("webroot"));
server.setHandler(webappcontext);
// --slowdown=/run/APP/PUBLISHED/*,/other/path/asd.jpg
// slows down specified paths
if (serverArgs.containsKey("slowdown")) {
String[] paths = serverArgs.get("slowdown").split(",");
for (String p : paths) {
System.out.println("Slowing down: " + p);
webappcontext.addFilter(SlowFilter.class, p,
EnumSet.of(DispatcherType.REQUEST));
}
}
// --cache=/run/APP/PUBLISHED/*,/other/path/asd.jpg
// caches specified paths
if (serverArgs.containsKey("cache")) {
String[] paths = serverArgs.get("cache").split(",");
for (String p : paths) {
System.out.println("Enabling cache for: " + p);
webappcontext.addFilter(CacheFilter.class, p,
EnumSet.of(DispatcherType.REQUEST));
}
}
// --autoreload=all --autoreload=WebContent/classes,other/path
// --scaninterval=1
// Configure Jetty to auto-reload when a any class is compiled in
// any folder included in the list of folders passed as arguments
// or in the entire classpath if the keyworkd all is passed.
if (serverArgs.containsKey("autoreload")) {
int interval = 1;
if (serverArgs.containsKey("scaninterval")) {
interval = Integer.parseInt(serverArgs.get("scaninterval"));
}
List<File> classFolders = new ArrayList<File>();
String[] paths = serverArgs.get("autoreload").split(",");
if (paths.length == 1 && "all".equals(paths[0])) {
ClassLoader cl = server.getClass().getClassLoader();
for (URL u : ((URLClassLoader) cl).getURLs()) {
File f = new File(u.getPath());
if (f.isDirectory()) {
classFolders.add(f);
}
}
} else {
for (String p : paths) {
File f = new File(p);
if (f.isDirectory()) {
classFolders.add(f);
}
}
}
if (!classFolders.isEmpty()) {
System.out
.println("Enabling context auto-reload.\n Scan interval: "
+ interval + " secs.\n Scanned folders: ");
for (File f : classFolders) {
System.out.println(" " + f.getAbsolutePath());
webappcontext.setExtraClasspath(f.getAbsolutePath());
}
System.out.println("");
Scanner scanner = new Scanner();
scanner.setScanInterval(interval);
scanner.setRecursive(true);
scanner.addListener(new Scanner.BulkListener() {
@Override
public void filesChanged(List<String> filenames)
throws Exception {
webappcontext.stop();
server.stop();
webappcontext.start();
server.start();
}
});
scanner.setReportExistingFilesOnStartup(false);
scanner.setFilenameFilter(new FilenameFilter() {
@Override
public boolean accept(File folder, String name) {
return name.endsWith(".class");
}
});
scanner.setScanDirs(classFolders);
scanner.start();
server.getContainer().addBean(scanner);
}
}
// Read web.xml to find all configured servlets
webappcontext.start();
// Reconfigure all servlets to avoid startup delay
for (ServletHolder servletHolder : webappcontext.getServletHandler()
.getServlets()) {
if (servletHolder
.getInitParameter("org.atmosphere.cpr.scanClassPath") == null) {
servletHolder.setInitParameter(
"org.atmosphere.cpr.scanClassPath", "false");