Search in sources :

Example 1 with UsageException

use of org.eclipse.jetty.start.UsageException in project jetty.project by eclipse.

the class CommandLineConfigSource method findJettyHomePath.

private final Path findJettyHomePath() {
    // If a jetty property is defined, use it
    Prop prop = this.props.getProp(BaseHome.JETTY_HOME, false);
    if (prop != null && !Utils.isBlank(prop.value)) {
        return FS.toPath(prop.value);
    }
    // If a system property is defined, use it
    String val = System.getProperty(BaseHome.JETTY_HOME);
    if (!Utils.isBlank(val)) {
        return FS.toPath(val);
    }
    // Attempt to find path relative to content in jetty's start.jar
    // based on lookup for the Main class (from jetty's start.jar)
    String classRef = "org/eclipse/jetty/start/Main.class";
    URL jarfile = this.getClass().getClassLoader().getResource(classRef);
    if (jarfile != null) {
        Matcher m = Pattern.compile("jar:(file:.*)!/" + classRef).matcher(jarfile.toString());
        if (m.matches()) {
            // ${jetty.home} is relative to found BaseHome class
            try {
                return new File(new URI(m.group(1))).getParentFile().toPath();
            } catch (URISyntaxException e) {
                throw new UsageException(UsageException.ERR_UNKNOWN, e);
            }
        }
    }
    // Lastly, fall back to ${user.dir} default
    Path home = FS.toPath(System.getProperty("user.dir", "."));
    setProperty(BaseHome.JETTY_HOME, home.toString(), ORIGIN_INTERNAL_FALLBACK);
    return home;
}
Also used : Path(java.nio.file.Path) UsageException(org.eclipse.jetty.start.UsageException) Prop(org.eclipse.jetty.start.Props.Prop) Matcher(java.util.regex.Matcher) URISyntaxException(java.net.URISyntaxException) File(java.io.File) URI(java.net.URI) URL(java.net.URL)

Example 2 with UsageException

use of org.eclipse.jetty.start.UsageException in project jetty.project by eclipse.

the class ConfigSourcesTest method testBadDoubleRef.

@Test
public void testBadDoubleRef() throws Exception {
    // Create home
    Path home = testdir.getPathFile("home");
    FS.ensureEmpty(home);
    TestEnv.copyTestDir("dist-home", home);
    // Create common
    Path common = testdir.getPathFile("common");
    FS.ensureEmpty(common);
    // Create corp
    Path corp = testdir.getPathFile("corp");
    FS.ensureEmpty(corp);
    TestEnv.makeFile(corp, "start.ini", // standard property
    "jetty.http.port=9090", // INTENTIONAL BAD Reference (duplicate)
    "--include-jetty-dir=" + common.toString());
    // Populate common
    TestEnv.makeFile(common, "start.ini", // standard property
    "jetty.http.port=8080", // reference to corp
    "--include-jetty-dir=" + corp.toString());
    // Create base
    Path base = testdir.getPathFile("base");
    FS.ensureEmpty(base);
    //
    TestEnv.makeFile(//
    base, //
    "start.ini", //
    "jetty.http.host=127.0.0.1", "--include-jetty-dir=" + common.toString());
    ConfigSources sources = new ConfigSources();
    try {
        String[] cmdLine = new String[0];
        sources.add(new CommandLineConfigSource(cmdLine));
        sources.add(new JettyHomeConfigSource(home));
        sources.add(new JettyBaseConfigSource(base));
        Assert.fail("Should have thrown a UsageException");
    } catch (UsageException e) {
        Assert.assertThat("UsageException", e.getMessage(), containsString("Duplicate"));
    }
}
Also used : Path(java.nio.file.Path) UsageException(org.eclipse.jetty.start.UsageException) Matchers.containsString(org.hamcrest.Matchers.containsString) Test(org.junit.Test)

Example 3 with UsageException

use of org.eclipse.jetty.start.UsageException in project jetty.project by eclipse.

the class ConfigSources method add.

public void add(ConfigSource source) throws IOException {
    if (sources.contains(source)) {
        // TODO: needs a better/more clear error message
        throw new UsageException(ERR_BAD_ARG, "Duplicate Configuration Source Reference: " + source);
    }
    sources.add(source);
    Collections.sort(sources, new WeightedConfigSourceComparator());
    updateProps();
    // look for --include-jetty-dir entries
    for (RawArgs.Entry arg : source.getArgs()) {
        if (arg.startsWith("--include-jetty-dir")) {
            String ref = getValue(arg.getLine());
            String dirName = props.expand(ref);
            Path dir = FS.toPath(dirName).normalize().toAbsolutePath();
            DirConfigSource dirsource = new DirConfigSource(ref, dir, sourceWeight.incrementAndGet(), true);
            add(dirsource);
        }
    }
}
Also used : Path(java.nio.file.Path) UsageException(org.eclipse.jetty.start.UsageException) RawArgs(org.eclipse.jetty.start.RawArgs)

Aggregations

Path (java.nio.file.Path)3 UsageException (org.eclipse.jetty.start.UsageException)3 File (java.io.File)1 URI (java.net.URI)1 URISyntaxException (java.net.URISyntaxException)1 URL (java.net.URL)1 Matcher (java.util.regex.Matcher)1 Prop (org.eclipse.jetty.start.Props.Prop)1 RawArgs (org.eclipse.jetty.start.RawArgs)1 Matchers.containsString (org.hamcrest.Matchers.containsString)1 Test (org.junit.Test)1