Search in sources :

Example 1 with ConfigSource

use of org.eclipse.jetty.start.config.ConfigSource in project jetty.project by eclipse.

the class BaseHome method toShortForm.

/**
     * Replace/Shorten arbitrary path with property strings <code>"${jetty.home}"</code> or <code>"${jetty.base}"</code> where appropriate.
     * 
     * @param path
     *            the path to shorten
     * @return the potentially shortened path
     */
public String toShortForm(final Path path) {
    Path apath = path.toAbsolutePath();
    for (ConfigSource source : sources) {
        if (source instanceof DirConfigSource) {
            DirConfigSource dirsource = (DirConfigSource) source;
            Path dir = dirsource.getDir();
            if (apath.startsWith(dir)) {
                if (dirsource.isPropertyBased()) {
                    Path relative = dir.relativize(apath);
                    return String.format("%s%c%s", dirsource.getId(), File.separatorChar, relative.toString());
                } else {
                    return apath.toString();
                }
            }
        }
    }
    return apath.toString();
}
Also used : Path(java.nio.file.Path) DirConfigSource(org.eclipse.jetty.start.config.DirConfigSource) JettyHomeConfigSource(org.eclipse.jetty.start.config.JettyHomeConfigSource) ConfigSource(org.eclipse.jetty.start.config.ConfigSource) JettyBaseConfigSource(org.eclipse.jetty.start.config.JettyBaseConfigSource) CommandLineConfigSource(org.eclipse.jetty.start.config.CommandLineConfigSource) DirConfigSource(org.eclipse.jetty.start.config.DirConfigSource)

Example 2 with ConfigSource

use of org.eclipse.jetty.start.config.ConfigSource in project jetty.project by eclipse.

the class Main method start.

public void start(StartArgs args) throws IOException, InterruptedException {
    StartLog.debug("StartArgs: %s", args);
    // Get Desired Classpath based on user provided Active Options.
    Classpath classpath = args.getClasspath();
    System.setProperty("java.class.path", classpath.toString());
    // Show the usage information and return
    if (args.isHelp()) {
        usage(true);
    }
    // Show the version information and return
    if (args.isListClasspath()) {
        dumpClasspathWithVersions(classpath);
    }
    // Show configuration
    if (args.isListConfig()) {
        listConfig(args);
    }
    // Show modules
    if (args.getListModules() != null) {
        listModules(args);
    }
    // Generate Module Graph File
    if (args.getModuleGraphFilename() != null) {
        Path outputFile = baseHome.getBasePath(args.getModuleGraphFilename());
        System.out.printf("Generating GraphViz Graph of Jetty Modules at %s%n", baseHome.toShortForm(outputFile));
        ModuleGraphWriter writer = new ModuleGraphWriter();
        writer.config(args.getProperties());
        writer.write(args.getAllModules(), outputFile);
    }
    // Show Command Line to execute Jetty
    if (args.isDryRun()) {
        CommandLineBuilder cmd = args.getMainArgs(true);
        System.out.println(cmd.toString(StartLog.isDebugEnabled() ? " \\\n" : " "));
    }
    if (args.isStopCommand()) {
        doStop(args);
    }
    if (args.isUpdateIni()) {
        for (ConfigSource config : baseHome.getConfigSources()) {
            System.out.printf("ConfigSource %s%n", config.getId());
            for (StartIni ini : config.getStartInis()) {
                for (String line : ini.getAllLines()) {
                    Matcher m = Module.SET_PROPERTY.matcher(line);
                    if (m.matches() && m.groupCount() == 3) {
                        String name = m.group(2);
                        String value = m.group(3);
                        Prop p = args.getProperties().getProp(name);
                        if (p != null && ("#".equals(m.group(1)) || !value.equals(p.value))) {
                            ini.update(baseHome, args.getProperties());
                            break;
                        }
                    }
                }
            }
        }
    }
    // Check base directory
    BaseBuilder baseBuilder = new BaseBuilder(baseHome, args);
    if (baseBuilder.build())
        StartLog.info("Base directory was modified");
    else if (args.isCreateFiles() || !args.getStartModules().isEmpty())
        StartLog.info("Base directory was not modified");
    // Check module dependencies
    args.getAllModules().checkEnabledModules();
    // Informational command line, don't run jetty
    if (!args.isRun()) {
        return;
    }
    // execute Jetty in another JVM
    if (args.isExec()) {
        CommandLineBuilder cmd = args.getMainArgs(true);
        cmd.debug();
        ProcessBuilder pbuilder = new ProcessBuilder(cmd.getArgs());
        StartLog.endStartLog();
        final Process process = pbuilder.start();
        Runtime.getRuntime().addShutdownHook(new Thread() {

            @Override
            public void run() {
                StartLog.debug("Destroying " + process);
                process.destroy();
            }
        });
        copyInThread(process.getErrorStream(), System.err);
        copyInThread(process.getInputStream(), System.out);
        copyInThread(System.in, process.getOutputStream());
        process.waitFor();
        // exit JVM when child process ends.
        System.exit(0);
        return;
    }
    if (args.hasJvmArgs() || args.hasSystemProperties()) {
        StartLog.warn("System properties and/or JVM args set.  Consider using --dry-run or --exec");
    }
    ClassLoader cl = classpath.getClassLoader();
    Thread.currentThread().setContextClassLoader(cl);
    // Invoke the Main Class
    try {
        invokeMain(cl, args);
    } catch (Throwable e) {
        e.printStackTrace();
        usageExit(e, ERR_INVOKE_MAIN, args.isTestingModeEnabled());
    }
}
Also used : Path(java.nio.file.Path) Matcher(java.util.regex.Matcher) Prop(org.eclipse.jetty.start.Props.Prop) ConfigSource(org.eclipse.jetty.start.config.ConfigSource) CommandLineConfigSource(org.eclipse.jetty.start.config.CommandLineConfigSource)

Example 3 with ConfigSource

use of org.eclipse.jetty.start.config.ConfigSource in project jetty.project by eclipse.

the class BaseHome method getPaths.

/**
     * Get a List of {@link Path}s from a provided pattern.
     * <p>
     * Resolution Steps:
     * <ol>
     * <li>If the pattern starts with "regex:" or "glob:" then a standard {@link PathMatcher} is built using
     * {@link java.nio.file.FileSystem#getPathMatcher(String)} as a file search.</li>
     * <li>If pattern starts with a known filesystem root (using information from {@link java.nio.file.FileSystem#getRootDirectories()}) then this is assumed to
     * be a absolute file system pattern.</li>
     * <li>All other patterns are treated as relative to BaseHome information:
     * <ol>
     * <li>Search ${jetty.home} first</li>
     * <li>Search ${jetty.base} for overrides</li>
     * </ol>
     * </li>
     * </ol>
     * <p>
     * Pattern examples:
     * <dl>
     * <dt><code>lib/logging/*.jar</code></dt>
     * <dd>Relative pattern, not recursive, search <code>${jetty.home}</code> then <code>${jetty.base}</code> for lib/logging/*.jar content</dd>
     * 
     * <dt><code>lib/**&#47;*-dev.jar</code></dt>
     * <dd>Relative pattern, recursive search <code>${jetty.home}</code> then <code>${jetty.base}</code> for files under <code>lib</code> ending in
     * <code>-dev.jar</code></dd>
     * 
     * <dt><code>etc/jetty.xml</code></dt>
     * <dd>Relative pattern, no glob, search for <code>${jetty.home}/etc/jetty.xml</code> then <code>${jetty.base}/etc/jetty.xml</code></dd>
     * 
     * <dt><code>glob:/opt/app/common/*-corp.jar</code></dt>
     * <dd>PathMapper pattern, glob, search <code>/opt/app/common/</code> for <code>*-corp.jar</code></dd>
     * 
     * </dl>
     * 
     * <p>
     * Notes:
     * <ul>
     * <li>FileSystem case sensitivity is implementation specific (eg: linux is case-sensitive, windows is case-insensitive).<br>
     * See {@link java.nio.file.FileSystem#getPathMatcher(String)} for more details</li>
     * <li>Pattern slashes are implementation neutral (use '/' always and you'll be fine)</li>
     * <li>Recursive searching is limited to 30 levels deep (not configurable)</li>
     * <li>File System loops are detected and skipped</li>
     * </ul>
     * 
     * @param pattern
     *            the pattern to search.
     * @return the collection of paths found
     * @throws IOException
     *             if error during search operation
     */
public List<Path> getPaths(String pattern) throws IOException {
    StartLog.debug("getPaths('%s')", pattern);
    List<Path> hits = new ArrayList<>();
    if (PathMatchers.isAbsolute(pattern)) {
        // Perform absolute path pattern search
        // The root to start search from
        Path root = PathMatchers.getSearchRoot(pattern);
        // The matcher for file hits
        PathMatcher matcher = PathMatchers.getMatcher(pattern);
        if (FS.isValidDirectory(root)) {
            PathFinder finder = new PathFinder();
            finder.setIncludeDirsInResults(true);
            finder.setFileMatcher(matcher);
            finder.setBase(root);
            Files.walkFileTree(root, SEARCH_VISIT_OPTIONS, MAX_SEARCH_DEPTH, finder);
            hits.addAll(finder.getHits());
        }
    } else {
        // Perform relative path pattern search
        Path relativePath = PathMatchers.getSearchRoot(pattern);
        PathMatcher matcher = PathMatchers.getMatcher(pattern);
        PathFinder finder = new PathFinder();
        finder.setIncludeDirsInResults(true);
        finder.setFileMatcher(matcher);
        // walk config sources backwards ...
        ListIterator<ConfigSource> iter = sources.reverseListIterator();
        while (iter.hasPrevious()) {
            ConfigSource source = iter.previous();
            if (source instanceof DirConfigSource) {
                DirConfigSource dirsource = (DirConfigSource) source;
                Path dir = dirsource.getDir();
                Path deepDir = dir.resolve(relativePath);
                if (FS.isValidDirectory(deepDir)) {
                    finder.setBase(dir);
                    Files.walkFileTree(deepDir, SEARCH_VISIT_OPTIONS, MAX_SEARCH_DEPTH, finder);
                }
            }
        }
        hits.addAll(finder.getHits());
    }
    Collections.sort(hits, new NaturalSort.Paths());
    return hits;
}
Also used : Path(java.nio.file.Path) DirConfigSource(org.eclipse.jetty.start.config.DirConfigSource) JettyHomeConfigSource(org.eclipse.jetty.start.config.JettyHomeConfigSource) ConfigSource(org.eclipse.jetty.start.config.ConfigSource) JettyBaseConfigSource(org.eclipse.jetty.start.config.JettyBaseConfigSource) CommandLineConfigSource(org.eclipse.jetty.start.config.CommandLineConfigSource) PathMatcher(java.nio.file.PathMatcher) ArrayList(java.util.ArrayList) DirConfigSource(org.eclipse.jetty.start.config.DirConfigSource)

Example 4 with ConfigSource

use of org.eclipse.jetty.start.config.ConfigSource in project jetty.project by eclipse.

the class BaseHome method getPath.

/**
     * Get a specific path reference.
     * <p>
     * Path references are searched based on the config source search order.
     * <ol>
     * <li>If provided path is an absolute reference., and exists, return that reference</li>
     * <li>If exists relative to <code>${jetty.base}</code>, return that reference</li>
     * <li>If exists relative to and <code>include-jetty-dir</code> locations, return that reference</li>
     * <li>If exists relative to <code>${jetty.home}</code>, return that reference</li>
     * <li>Return standard {@link Path} reference obtained from {@link java.nio.file.FileSystem#getPath(String, String...)} (no exists check performed)</li>
     * </ol>
     * 
     * @param path
     *            the path to get.
     * @return the path reference.
     */
public Path getPath(final String path) {
    Path apath = FS.toPath(path);
    if (apath.isAbsolute()) {
        if (FS.exists(apath)) {
            return apath;
        }
    }
    for (ConfigSource source : sources) {
        if (source instanceof DirConfigSource) {
            DirConfigSource dirsource = (DirConfigSource) source;
            Path file = dirsource.getDir().resolve(apath);
            if (FS.exists(file)) {
                return file;
            }
        }
    }
    // Finally, as an anonymous path
    return FS.toPath(path);
}
Also used : Path(java.nio.file.Path) DirConfigSource(org.eclipse.jetty.start.config.DirConfigSource) JettyHomeConfigSource(org.eclipse.jetty.start.config.JettyHomeConfigSource) ConfigSource(org.eclipse.jetty.start.config.ConfigSource) JettyBaseConfigSource(org.eclipse.jetty.start.config.JettyBaseConfigSource) CommandLineConfigSource(org.eclipse.jetty.start.config.CommandLineConfigSource) DirConfigSource(org.eclipse.jetty.start.config.DirConfigSource)

Example 5 with ConfigSource

use of org.eclipse.jetty.start.config.ConfigSource in project jetty.project by eclipse.

the class StartArgs method dumpEnvironment.

public void dumpEnvironment() {
    // Java Details
    System.out.println();
    System.out.println("Java Environment:");
    System.out.println("-----------------");
    dumpSystemProperty("java.home");
    dumpSystemProperty("java.vm.vendor");
    dumpSystemProperty("java.vm.version");
    dumpSystemProperty("java.vm.name");
    dumpSystemProperty("java.vm.info");
    dumpSystemProperty("java.runtime.name");
    dumpSystemProperty("java.runtime.version");
    dumpSystemProperty("java.io.tmpdir");
    dumpSystemProperty("user.dir");
    dumpSystemProperty("user.language");
    dumpSystemProperty("user.country");
    // Jetty Environment
    System.out.println();
    System.out.println("Jetty Environment:");
    System.out.println("-----------------");
    dumpProperty("jetty.version");
    dumpProperty("jetty.tag.version");
    dumpProperty("jetty.home");
    dumpProperty("jetty.base");
    // Jetty Configuration Environment
    System.out.println();
    System.out.println("Config Search Order:");
    System.out.println("--------------------");
    for (ConfigSource config : baseHome.getConfigSources()) {
        System.out.printf(" %s", config.getId());
        if (config instanceof DirConfigSource) {
            DirConfigSource dirsource = (DirConfigSource) config;
            if (dirsource.isPropertyBased()) {
                System.out.printf(" -> %s", dirsource.getDir());
            }
        }
        System.out.println();
    }
    // Jetty Se
    System.out.println();
}
Also used : DirConfigSource(org.eclipse.jetty.start.config.DirConfigSource) ConfigSource(org.eclipse.jetty.start.config.ConfigSource) DirConfigSource(org.eclipse.jetty.start.config.DirConfigSource)

Aggregations

ConfigSource (org.eclipse.jetty.start.config.ConfigSource)5 Path (java.nio.file.Path)4 CommandLineConfigSource (org.eclipse.jetty.start.config.CommandLineConfigSource)4 DirConfigSource (org.eclipse.jetty.start.config.DirConfigSource)4 JettyBaseConfigSource (org.eclipse.jetty.start.config.JettyBaseConfigSource)3 JettyHomeConfigSource (org.eclipse.jetty.start.config.JettyHomeConfigSource)3 PathMatcher (java.nio.file.PathMatcher)1 ArrayList (java.util.ArrayList)1 Matcher (java.util.regex.Matcher)1 Prop (org.eclipse.jetty.start.Props.Prop)1