415826 start.jar replace --enable with --initialize

This commit is contained in:
Greg Wilkins 2013-08-28 15:41:27 +10:00
parent 73cf943b5c
commit c1bd267c17
12 changed files with 74 additions and 541 deletions

View File

@ -1,12 +0,0 @@
# ===========================================================
# Enable the demonstration web applications
#
# To disable the demos, either delete this file, move it out of
# the start.d directory or rename it to not end with ".ini"
# ===========================================================
--module=demo
demo.realm=demo/realm.properties
jaas.login.conf=demo/login.conf

View File

@ -1,15 +1,15 @@
#===========================================================
# Jetty start.jar arguments
#
# The contents of this file, together with the start.ini
# fragments found in start.d directory are used to build
# The contents of this file, together with the *.ini
# files found in start.d directory are used to build
# the classpath and command line on a call to
# java -jar start.jar [arg...]
#
# Use the following command to see more options
# java -jar start.jar --help
#
# Each line in this file is prepended to the command line
# Each line in these files is prepended to the command line
# as arguments, which may be either:
# + A property like: name=value
# + A module to enable like: --module=jmx
@ -21,20 +21,10 @@
# + A JVM option like: -Xmx2000m
# + A System Property like: -Dcom.sun.management.jmxremote
#
#-----------------------------------------------------------
#
# NOTE: The lines in this file may be uncommented to activate
# features. Alternately, the lines may be copied to a ini file
# in the start.d directory to enabled configuration without
# editing this file. See start.d/demo.ini for an example.
# The options --initialize can be used to create a template
# start.d/module.ini file for defined modules. For example
# to create an ini template for https use
# java -jar start.jar --initialize=https
#
#===========================================================

View File

@ -3,6 +3,7 @@
DEPEND=server
etc/jetty-ssl.xml
DOWNLOAD=http://git.eclipse.org/c/jetty/org.eclipse.jetty.project.git/plain/jetty-server/src/main/config/etc/keystore:etc/keystore
INI=jetty.keystore=etc/keystore
INI=jetty.keystore.password=OBF:1vny1zlo1x8e1vnw1vn61x8g1zlu1vn4

View File

@ -1,4 +1,5 @@
DOWNLOAD=http://repo1.maven.org/maven2/org/mortbay/jetty/npn/npn-boot/1.1.5.v20130313/npn-boot-1.1.5.v20130313.jar:lib/npn/npn-boot-1.1.5.v20130313.jar
BOOTLIB=-Xbootclasspath/p:lib/npn/npn-boot-1.1.5.v20130313.jar
INI=-Xbootclasspath/p:lib/npn/npn-boot-1.1.5.v20130313.jar
INI=--exec

View File

@ -1,5 +1,5 @@
DEPEND=server
DEPEND=ssl
DEPEND=npn
LIB=lib/spdy/*.jar

View File

@ -43,6 +43,7 @@ import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Locale;
import java.util.Set;
import java.util.regex.Pattern;
import org.eclipse.jetty.start.StartArgs.DownloadArg;
@ -332,12 +333,6 @@ public class Main
modules.dumpEnabledTree();
}
private ModulePersistence loadModulePersistence() throws IOException
{
File file = baseHome.getBaseFile("modules/enabled");
return new ModulePersistence(file);
}
/**
* Convenience for <code>processCommandLine(cmdLine.toArray(new String[cmdLine.size()]))</code>
*/
@ -411,7 +406,6 @@ public class Main
List<String> sources = args.getSources(enabledModule);
modules.enable(enabledModule,sources);
}
modules.enable(loadModulePersistence());
StartLog.debug("Building Module Graph");
modules.buildGraph();
@ -444,6 +438,7 @@ public class Main
}
// Various Downloads
// TODO should this only be done by init?
for (DownloadArg url : args.getDownloads())
{
download(url);
@ -474,23 +469,6 @@ public class Main
System.out.println(cmd.toString());
}
// Enables/Disable
ModulePersistence persistence = loadModulePersistence();
if (args.isModulePersistenceChanging())
{
System.out.println("Persistent Module Management:");
System.out.println("-----------------------------");
System.out.printf("Persistence file: %s%n",baseHome.toShortForm(persistence.getFile()));
for (String module : args.getModulePersistDisable())
{
persistence.disableModule(args,module);
}
for (String module : args.getModulePersistEnable())
{
persistence.enableModule(args,module);
}
}
if (args.isStopCommand())
{
int stopPort = Integer.parseInt(args.getProperties().getProperty("STOP.PORT"));
@ -639,54 +617,73 @@ public class Main
File start_d=baseHome.getFile("start.d");
// Is this a module?
Module module=args.getAllModules().get(name);
Modules modules=args.getAllModules();
Module module=modules.get(name);
if (module==null)
{
StartLog.warn("ERROR: No known module for %s",name);
return;
}
Set<String> sources=module.getSources();
boolean explictly_enabled=sources!=null && sources.size()>0;
// Is it already enabled
File ini=new File(start_d,name+".ini");
String short_ini = baseHome.toShortForm(ini);
if (ini.exists())
{
if (new StartIni(ini).getLineMatches(Pattern.compile("--module=(.*, *)*"+name)).size()==0)
StartLog.warn("ERROR: %s not initialised in %s!",name,baseHome.toShortForm(ini));
StartLog.warn("WARNING: %s not initialised in %s!",name,short_ini);
else
StartLog.warn("%-15s initialised in %s",name,baseHome.toShortForm(ini));
return;
}
else
{
// Should we create an ini file?
if (topLevel || module.getInitialise().size()>0)
{
if (ini.createNewFile())
{
StartLog.warn("%-15s initialised in %s (created)",name,baseHome.toShortForm(ini));
StartLog.warn("%-15s initialised in %s",name,short_ini);
// Create an ini
try(PrintWriter out = new PrintWriter(ini))
{
out.println("# Initialize module "+name);
out.println("--module="+name);
for (String line : module.getInitialise())
out.println(line);
}
args.parse(baseHome, new StartIni(ini));
}
else
{
StartLog.warn("ERROR: %s cannot be initialised in %s! ",name,baseHome.toShortForm(ini));
return;
}
}
// If this is a top level init, or a transitive dependency with init lines that has not already been explicitly enabled
else if (topLevel || (module.getInitialise().size()>0 && !explictly_enabled))
{
// Create a new ini file for it
if (!ini.createNewFile())
{
StartLog.warn("ERROR: %s cannot be initialised in %s! ",name,short_ini);
return;
}
StartLog.warn("%-15s initialised in %s (created)",name,short_ini);
// Create an ini
try(PrintWriter out = new PrintWriter(ini))
{
out.println("# Initialize module "+name);
out.println("--module="+name);
for (String line : module.getInitialise())
out.println(line);
}
StartIni start_ini=new StartIni(ini);
args.parse(baseHome, start_ini);
for (String enable:start_ini.getLineMatches(Pattern.compile("--module=.*")))
{
modules.enable(enable.substring(enable.indexOf('=')+1).trim(),Collections.singletonList(short_ini));
}
}
// transitive module already enabled, so only list sources
for(String source:sources)
{
if (!short_ini.equals(source))
StartLog.warn("%-15s enabled in %s",name,baseHome.toShortForm(source));
}
// Do downloads now
for (String download : module.getDownloads())
download(StartArgs.toDownloadArg(download));
// Process dependencies
if (module!=null)
for (String parent:module.getParentNames())
initialize(args,parent,false);
if (module!=null && topLevel)
{
for (String parent:modules.resolveParentModulesOf(name))
if (!name.equals(parent))
initialize(args,parent,false);
}
}
public void usage(boolean exit)

View File

@ -90,10 +90,7 @@ public class Module extends TextFile
/** List of library options for this Module */
private List<String> libs;
/** List of downloads for this Module */
private List<String> downloads;
/** List of bootlib for this Module */
private List<String> bootlibs;
private List<String> downloads;
/** Is this Module enabled via start.jar command line, start.ini, or start.d/*.ini ? */
@ -210,11 +207,6 @@ public class Module extends TextFile
return downloads;
}
public List<String> getBootLibs()
{
return bootlibs;
}
@Override
public int hashCode()
{
@ -240,7 +232,6 @@ public class Module extends TextFile
initialise=new ArrayList<>();
libs=new ArrayList<>();
downloads=new ArrayList<>();
bootlibs=new ArrayList<>();
}
public boolean isEnabled()
@ -283,10 +274,6 @@ public class Module extends TextFile
downloads.add(value);
handled = true;
break;
case "BOOTLIB":
bootlibs.add(value);
handled = true;
break;
case "INI":
initialise.add(value);
handled = true;

View File

@ -1,182 +0,0 @@
//
// ========================================================================
// Copyright (c) 1995-2013 Mort Bay Consulting Pty. Ltd.
// ------------------------------------------------------------------------
// All rights reserved. This program and the accompanying materials
// are made available under the terms of the Eclipse Public License v1.0
// and Apache License v2.0 which accompanies this distribution.
//
// The Eclipse Public License is available at
// http://www.eclipse.org/legal/epl-v10.html
//
// The Apache License v2.0 is available at
// http://www.opensource.org/licenses/apache2.0.php
//
// You may elect to redistribute this code under either of these licenses.
// ========================================================================
//
package org.eclipse.jetty.start;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.ListIterator;
import java.util.Set;
/**
* Represents the optional, managed by jetty command line, file at ${jetty.base}/modules/enabled
*/
public class ModulePersistence extends TextFile
{
public ModulePersistence(File file) throws FileNotFoundException, IOException
{
super(file);
}
public boolean disableModule(StartArgs args, String moduleName) throws IOException
{
// capture of what modules were disabled by this action
List<String> modulesThatWereDisabled = new ArrayList<>();
// capture of what modules were automatically enabled by this action
// this list can occur if you attempt to disable a leaf node, the parent nodes
// of that leaf would then become enabled
List<String> modulesThatWereEnabled = new ArrayList<>();
// set of child modules that should be disabled by this action
Set<String> resolvedModulesToDisable = args.getAllModules().resolveChildModulesOf(moduleName);
// Show user what could be disabled
System.out.printf(" - Disabling Module: %s%n",moduleName);
if (resolvedModulesToDisable.size() > 1)
{
System.out.printf(" - (Resolved to) : ");
boolean needDelim = false;
for (String name : resolvedModulesToDisable)
{
if (needDelim)
{
System.out.print(" -> ");
}
System.out.print(name);
needDelim = true;
}
System.out.println();
}
// Do the disabling
// Step 1: set parent modules to enabled.
// This is to handle the case where the leaf is disabled, you still
// want the branch itself to be active upto the step before that leaf
Modules modules = args.getAllModules();
Module leaf = modules.get(moduleName);
// no children, this is a leaf
if (leaf.getChildEdges().size() <= 0)
{
// mark all parents as enabled
List<String> sources = new ArrayList<>();
sources.add("<module-persistence>");
for (Module parent : leaf.getParentEdges())
{
parent.setEnabled(true);
parent.addSources(sources);
addUniqueLine(parent.getName());
modulesThatWereEnabled.add(parent.getName());
}
}
// Step 2: mark the leaf nodes disabled
ListIterator<String> iter = super.listIterator();
while (iter.hasNext())
{
String line = iter.next().trim();
if (resolvedModulesToDisable.contains(line))
{
iter.remove();
modulesThatWereDisabled.add(line);
}
}
// Save file
saveFile();
// Show user what was disabled
if (modulesThatWereDisabled.size() > 0)
{
System.out.printf("Disabled %d module%s%n",modulesThatWereDisabled.size(),modulesThatWereDisabled.size() > 1?"s":"");
for (String name : modulesThatWereDisabled)
{
System.out.printf(" - %s%n",name);
}
return true;
}
else if (modulesThatWereEnabled.size() > 0)
{
System.out.printf("Module %s was has been effectively disabled.%n",moduleName);
return true;
}
else
{
System.out.printf("Module %s not found, no changes made to module persistence.%n",moduleName);
return false;
}
}
public boolean enableModule(StartArgs args, String moduleName) throws IOException
{
boolean ret = false;
System.out.printf(" - Enabling Module: %s%n",moduleName);
if (getLines().contains(moduleName))
{
// duplicate
System.out.printf(" - Already present, not adding again%n");
}
else
{
// add it
getLines().add(moduleName);
System.out.printf(" - Adding module %s%n",moduleName);
Set<String> transitiveNames = args.getAllModules().resolveParentModulesOf(moduleName);
if (transitiveNames.size() > 1)
{
System.out.print(" - Enabled: ");
boolean needDelim = false;
for (String name : transitiveNames)
{
if (needDelim)
{
System.out.print(" -> ");
}
System.out.print(name);
needDelim = true;
}
System.out.println();
}
saveFile();
}
return ret;
}
public List<String> getEnabled()
{
return getLines();
}
private void saveFile() throws IOException
{
File file = getFile();
File parent = file.getParentFile();
FS.ensureDirectoryExists(parent);
try (FileWriter writer = new FileWriter(file,false))
{
for (String line : getLines())
{
writer.append(line).append('\n');
}
}
}
}

View File

@ -202,76 +202,6 @@ public class Modules implements Iterable<Module>
}
}
/**
* Enable all modules found in module persistence
*
* @param persistence
* the module persistence to use
*/
public void enable(ModulePersistence persistence)
{
List<String> source = new ArrayList<>();
source.add("<module-persistence>");
if (persistence.getEnabled().size() > 0)
{
// show user warning about module enablement + module persistence
List<String> enabled = new ArrayList<>();
for (Module module : modules.values())
{
if (module.isEnabled())
{
if (!persistence.getEnabled().contains(module.getName()))
{
// only add if not already covered by module persistence
enabled.add(module.getName());
}
}
}
if (enabled.size() > 0)
{
StringBuilder err = new StringBuilder();
err.append("WARNING: Module Persistence Mode is being used.\n");
err.append("The following ");
if (enabled.size() > 1)
{
err.append(enabled.size()).append("modules");
}
else
{
err.append("module");
}
err.append(", defined outside of the module persistence mechanism, ");
if (enabled.size() > 1)
{
err.append("are ");
}
else
{
err.append("is ");
}
err.append("being ignored.");
System.err.println(err);
for (int i = 0; i < enabled.size(); i++)
{
String name = enabled.get(i);
Module module = modules.get(name);
System.err.printf(" [%d] \"%s\" - defined in [%s]%n",i + 1,name,Main.join(module.getSources(),", "));
module.setEnabled(false);
module.clearSources();
}
}
// use module persistence mode
for (String module : persistence.getEnabled())
{
enable(module,source);
}
}
}
public void enable(String name, List<String> sources)
{
Module module = modules.get(name);

View File

@ -168,7 +168,7 @@ public class StartArgs
classpath = new Classpath();
}
private void addDownload(String uriLocation)
static DownloadArg toDownloadArg(String uriLocation)
{
String parts[] = uriLocation.split(":",3);
if (parts.length != 3)
@ -186,7 +186,12 @@ public class StartArgs
DownloadArg arg = new DownloadArg();
arg.uri = String.format("%s:%s",parts[0],parts[1]);
arg.location = parts[2];
return arg;
}
private void addDownload(String uriLocation)
{
DownloadArg arg=toDownloadArg(uriLocation);
if (!downloads.contains(arg))
{
downloads.add(arg);
@ -428,14 +433,6 @@ public class StartArgs
StartLog.debug("Adding module specified download: %s",download);
addDownload(download);
}
// Register BootLib references
for (String bootlib : module.getBootLibs())
{
StartLog.debug("Adding module specified bootlib: %s",bootlib);
exec = true;
jvmArgs.add(bootlib);
}
}
}

View File

@ -1,177 +0,0 @@
//
// ========================================================================
// Copyright (c) 1995-2013 Mort Bay Consulting Pty. Ltd.
// ------------------------------------------------------------------------
// All rights reserved. This program and the accompanying materials
// are made available under the terms of the Eclipse Public License v1.0
// and Apache License v2.0 which accompanies this distribution.
//
// The Eclipse Public License is available at
// http://www.eclipse.org/legal/epl-v10.html
//
// The Apache License v2.0 is available at
// http://www.opensource.org/licenses/apache2.0.php
//
// You may elect to redistribute this code under either of these licenses.
// ========================================================================
//
package org.eclipse.jetty.start;
import static org.hamcrest.Matchers.*;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.eclipse.jetty.toolchain.test.MavenTestingUtils;
import org.eclipse.jetty.toolchain.test.TestingDir;
import org.eclipse.jetty.toolchain.test.FS;
import org.junit.Assert;
import org.junit.Rule;
import org.junit.Test;
public class ModulePersistenceTest
{
@Rule
public TestingDir testdir = new TestingDir();
@Test
public void testLoadNoFile() throws IOException
{
File baseDir = testdir.getEmptyDir();
File mfile = new File(baseDir,"modules.p");
ModulePersistence persistence = new ModulePersistence(mfile);
Assert.assertThat("persistence.enabled.size",persistence.getEnabled().size(),is(0));
}
@Test
public void testOneLine() throws IOException
{
File baseDir = testdir.getEmptyDir();
File mfile = new File(baseDir,"modules.p");
writeFile(mfile,"hello");
ModulePersistence persistence = new ModulePersistence(mfile);
Assert.assertThat("persistence.enabled",persistence.getEnabled(),containsInAnyOrder("hello"));
}
@Test
public void testDuplicateLines() throws IOException
{
File baseDir = testdir.getEmptyDir();
File mfile = new File(baseDir,"modules.p");
writeFile(mfile,"hello","there","earthling","hello");
ModulePersistence persistence = new ModulePersistence(mfile);
Assert.assertThat("persistence.enabled",persistence.getEnabled(),containsInAnyOrder("hello","there","earthling"));
}
@Test
public void testDisableHttp() throws Exception
{
File homeDir = MavenTestingUtils.getTestResourceDir("usecases/home");
File baseDir = testdir.getEmptyDir();
File modulesDir = testdir.getFile("modules");
FS.ensureEmpty(modulesDir);
File mfile = new File(modulesDir,"enabled");
writeFile(mfile,"http","websocket");
ModulePersistence persistence = new ModulePersistence(mfile);
Assert.assertThat("persistence.enabled",persistence.getEnabled(),containsInAnyOrder("http","websocket"));
Main main = new Main();
List<String> cmds = new ArrayList<>();
cmds.add("jetty.home=" + homeDir.getAbsolutePath());
cmds.add("jetty.base=" + baseDir.getAbsolutePath());
cmds.add("--disable-module=http");
StartArgs args = main.processCommandLine(cmds);
Assert.assertThat("isRun", args.isRun(), is(false));
main.start(args);
// Load persistence file again
persistence = new ModulePersistence(mfile);
Assert.assertThat("persistence.enabled",persistence.getEnabled(),containsInAnyOrder("server","websocket"));
}
@Test
public void testDisableAnnotations() throws Exception
{
File homeDir = MavenTestingUtils.getTestResourceDir("usecases/home");
File baseDir = testdir.getEmptyDir();
File modulesDir = testdir.getFile("modules");
FS.ensureEmpty(modulesDir);
File mfile = new File(modulesDir,"enabled");
writeFile(mfile,"http","websocket");
ModulePersistence persistence = new ModulePersistence(mfile);
Assert.assertThat("persistence.enabled",persistence.getEnabled(),containsInAnyOrder("http","websocket"));
Main main = new Main();
List<String> cmds = new ArrayList<>();
cmds.add("jetty.home=" + homeDir.getAbsolutePath());
cmds.add("jetty.base=" + baseDir.getAbsolutePath());
cmds.add("--disable-module=annotations");
StartArgs args = main.processCommandLine(cmds);
Assert.assertThat("isRun", args.isRun(), is(false));
main.start(args);
// Load persistence file again
persistence = new ModulePersistence(mfile);
Assert.assertThat("persistence.enabled",persistence.getEnabled(),containsInAnyOrder("http"));
}
@Test
public void testEnableWebSocket() throws Exception
{
File homeDir = MavenTestingUtils.getTestResourceDir("usecases/home");
File baseDir = testdir.getEmptyDir();
File modulesDir = testdir.getFile("modules");
FS.ensureEmpty(modulesDir);
File mfile = new File(modulesDir,"enabled");
writeFile(mfile,"http");
ModulePersistence persistence = new ModulePersistence(mfile);
Assert.assertThat("persistence.enabled",persistence.getEnabled(),containsInAnyOrder("http"));
Main main = new Main();
List<String> cmds = new ArrayList<>();
cmds.add("jetty.home=" + homeDir.getAbsolutePath());
cmds.add("jetty.base=" + baseDir.getAbsolutePath());
cmds.add("--enable-module=websocket");
StartArgs args = main.processCommandLine(cmds);
Assert.assertThat("isRun", args.isRun(), is(false));
main.start(args);
// Load persistence file again
persistence = new ModulePersistence(mfile);
Assert.assertThat("persistence.enabled",persistence.getEnabled(),containsInAnyOrder("http","websocket"));
}
private void writeFile(File mfile, String... lines) throws IOException
{
final String LN = System.getProperty("line.separator");
try (FileWriter writer = new FileWriter(mfile,false))
{
for (String line : lines)
{
writer.append(line).append(LN);
}
}
}
}

View File

@ -2,8 +2,8 @@
# Jetty Demo Module
#
DEPEND=jndi
DEPEND=jaas
DEPEND=jndi
DEPEND=rewrite
DEPEND=client
DEPEND=annotations
@ -15,5 +15,6 @@ LIB=demo/lib/*.jar
demo/test-realm.xml
demo/jetty-demo.xml
INI=--module=jaas
INI=demo.realm=demo/realm.properties
INI=jaas.login.conf=demo/login.conf