Search in sources :

Example 6 with ResourceFinder

use of org.apache.xbean.finder.ResourceFinder in project tomee by apache.

the class Container method synchronize.

private void synchronize(final File base, final String resourceBase) {
    if (resourceBase == null) {
        return;
    }
    try {
        final Map<String, URL> urls = new ResourceFinder("").getResourcesMap(resourceBase);
        for (final Map.Entry<String, URL> u : urls.entrySet()) {
            try (final InputStream is = u.getValue().openStream()) {
                final File to = new File(base, u.getKey());
                IO.copy(is, to);
                if ("server.xml".equals(u.getKey())) {
                    configuration.setServerXml(to.getAbsolutePath());
                }
            }
        }
    } catch (final IOException e) {
        throw new IllegalStateException(e);
    }
}
Also used : ResourceFinder(org.apache.xbean.finder.ResourceFinder) ReplaceStringsInputStream(org.codehaus.swizzle.stream.ReplaceStringsInputStream) InputStream(java.io.InputStream) IOException(java.io.IOException) Map(java.util.Map) HashMap(java.util.HashMap) File(java.io.File) URL(java.net.URL)

Example 7 with ResourceFinder

use of org.apache.xbean.finder.ResourceFinder in project tomee by apache.

the class ApplicationComposers method implicitRootUrl.

private static String implicitRootUrl(final PersistenceRootUrl annotation) {
    if (annotation != null) {
        return annotation.value();
    }
    final ResourceFinder finder = new ResourceFinder("", Thread.currentThread().getContextClassLoader());
    try {
        final URL url = DeploymentLoader.altDDSources(DeploymentLoader.mapDescriptors(finder), false).get("persistence.xml");
        if (url == null) {
            return "";
        }
        final File file = URLs.toFile(url);
        final String filename = file.getName();
        if (filename.endsWith("persistence.xml")) {
            final String parent = file.getParentFile().getName();
            if (parent.equalsIgnoreCase("META-INF")) {
                return file.getParentFile().getParentFile().getAbsolutePath();
            }
            return file.getParentFile().getAbsolutePath();
        } else if (filename.endsWith(".jar")) {
            return file.toURI().toURL().toExternalForm();
        }
        return url.toExternalForm();
    } catch (final IOException e) {
        return "";
    }
}
Also used : ResourceFinder(org.apache.xbean.finder.ResourceFinder) RESTResourceFinder(org.apache.openejb.rest.RESTResourceFinder) IOException(java.io.IOException) File(java.io.File) URL(java.net.URL)

Example 8 with ResourceFinder

use of org.apache.xbean.finder.ResourceFinder in project tomee by apache.

the class ApplicationComposers method addWebApp.

private void addWebApp(final AppModule appModule, final ManagedBean testBean, final Map<String, URL> additionalDescriptors, final Descriptors descriptors, final JaxrsProviders providers, final WebApp webapp, final Jars globalJarsAnnotation, final Jars jarsAnnotation, final Class<?>[] cdiClasses, final String[] excludes, final Class<?>[] cdiInterceptors, final Class<?>[] cdiAlternatives, final Class<?>[] cdiDecorators, final Class<?>[] cdiStereotypes, final boolean cdi, final boolean innerClassesAsBean, final boolean autoConfig) throws OpenEJBException {
    String root = webapp.getContextRoot();
    if (root == null) {
        root = "/openejb";
    }
    testBean.getEnvEntry().addAll(webapp.getEnvEntry());
    final WebModule webModule = new WebModule(webapp, root, Thread.currentThread().getContextClassLoader(), "", root);
    final File thisJar;
    if (autoConfig) {
        thisJar = jarLocation(testClass);
        try {
            webModule.getAltDDs().putAll(DeploymentLoader.mapDescriptors(new ResourceFinder("", webModule.getClassLoader(), thisJar.toURI().toURL())));
            webModule.getAltDDs().putAll(DeploymentLoader.getWebDescriptors(new File(thisJar.getParentFile().getParentFile(), "src/main/webapp")));
        } catch (final IOException e) {
            throw new IllegalStateException(e);
        }
    } else {
        thisJar = null;
    }
    webModule.getAltDDs().putAll(additionalDescriptors);
    for (final Descriptors d : asList(testClass.getAnnotation(Descriptors.class), descriptors)) {
        if (d != null) {
            webModule.getAltDDs().putAll(descriptorsToMap(d));
        }
    }
    final EjbModule ejbModule = DeploymentLoader.addWebModule(webModule, appModule);
    ejbModule.getProperties().put(CdiScanner.OPENEJB_CDI_FILTER_CLASSLOADER, "false");
    if (isCdi(cdi, cdiInterceptors, cdiAlternatives, cdiStereotypes, cdiDecorators)) {
        ejbModule.setBeans(beans(new Beans(), cdiDecorators, cdiInterceptors, cdiAlternatives, cdiStereotypes));
    }
    Class<?>[] classes = cdiClasses;
    final Class<?>[] providersClasses = providers == null ? null : providers.value();
    for (final JaxrsProviders p : asList(testClass.getAnnotation(JaxrsProviders.class), providers)) {
        if (p != null) {
            if (classes == null) {
                classes = p.value();
            } else {
                final Collection<Class<?>> newClasses = new ArrayList<>(asList(classes));
                newClasses.addAll(asList(p.value()));
                classes = newClasses.toArray(new Class<?>[newClasses.size()]);
            }
        }
    }
    if (innerClassesAsBean) {
        final Collection<Class<?>> inners = new LinkedList<>();
        for (final Class<?> clazz : testClass.getClasses()) {
            final int modifiers = clazz.getModifiers();
            try {
                if (Modifier.isPublic(modifiers) && Modifier.isStatic(modifiers) && clazz.getConstructor() != null) {
                    inners.add(clazz);
                }
            } catch (final NoSuchMethodException nsme) {
            // no-op, skip it
            }
        }
        if (!inners.isEmpty()) {
            final Collection<Class<?>> newClasses = new ArrayList<>(asList(classes));
            newClasses.addAll(inners);
            classes = newClasses.toArray(new Class<?>[newClasses.size()]);
        }
    }
    Collection<File> libs = null;
    for (final Jars jars : asList(jarsAnnotation, globalJarsAnnotation)) {
        final Collection<File> files = findFiles(jars);
        if (files != null) {
            if (libs == null) {
                libs = new LinkedList<>();
            }
            libs.addAll(files);
        }
    }
    if (autoConfig) {
        if (libs == null) {
            libs = new LinkedList<>();
        }
        libs.add(thisJar);
        if ("test-classes".equals(thisJar.getName()) && "target".equals(thisJar.getParentFile().getName())) {
            // mvn
            final File mainClasses = new File(thisJar.getParentFile(), "classes");
            if (mainClasses.exists()) {
                libs.add(mainClasses);
            }
        } else if ("test".equals(thisJar.getName()) && "classes".equals(thisJar.getParentFile().getName())) {
            // gradle
            final File mainClasses = new File(thisJar.getParentFile(), "main");
            if (mainClasses.exists()) {
                libs.add(mainClasses);
            }
        }
    }
    final IAnnotationFinder finder = finderFromClasses(webModule, classes, libs, excludes);
    webModule.setFinder(finder);
    ejbModule.setFinder(webModule.getFinder());
    if (providersClasses != null) {
        OpenejbJar openejbJar = ejbModule.getOpenejbJar();
        if (openejbJar == null) {
            openejbJar = new OpenejbJar();
            ejbModule.setOpenejbJar(openejbJar);
        }
        final PojoDeployment pojoDeployment = new PojoDeployment();
        pojoDeployment.setClassName(providers.applicationName());
        pojoDeployment.getProperties().setProperty("cxf.jaxrs.providers", Join.join(",", providersClasses).replace("class ", ""));
        // it is specified so skip scanning otherwise we'll get them twice
        pojoDeployment.getProperties().setProperty("cxf.jaxrs.skip-provider-scanning", "true");
        openejbJar.getPojoDeployment().add(pojoDeployment);
    }
}
Also used : ResourceFinder(org.apache.xbean.finder.ResourceFinder) RESTResourceFinder(org.apache.openejb.rest.RESTResourceFinder) IAnnotationFinder(org.apache.xbean.finder.IAnnotationFinder) EjbModule(org.apache.openejb.config.EjbModule) ArrayList(java.util.ArrayList) PojoDeployment(org.apache.openejb.jee.oejb3.PojoDeployment) WebModule(org.apache.openejb.config.WebModule) IOException(java.io.IOException) LinkedList(java.util.LinkedList) Beans(org.apache.openejb.jee.Beans) OpenejbJar(org.apache.openejb.jee.oejb3.OpenejbJar) File(java.io.File)

Example 9 with ResourceFinder

use of org.apache.xbean.finder.ResourceFinder in project tomee by apache.

the class DontStartServerException method main.

public static void main(String[] args) throws SystemExitException {
    CommandLineParser parser = new PosixParser();
    // create the Options
    Options options = new Options();
    options.addOption(option("v", "version", "cmd.start.opt.version"));
    options.addOption(option("h", "help", "cmd.start.opt.help"));
    options.addOption(option(null, "conf", "file", "cmd.start.opt.conf"));
    options.addOption(option(null, "local-copy", "boolean", "cmd.start.opt.localCopy"));
    options.addOption(option(null, "examples", "cmd.start.opt.examples"));
    ResourceFinder finder = new ResourceFinder("META-INF/");
    Set<String> services = null;
    try {
        Map<String, Properties> serviceEntries = finder.mapAvailableProperties(ServerService.class.getName());
        services = serviceEntries.keySet();
        for (String service : services) {
            options.addOption(option(null, service + "-port", "int", "cmd.start.opt.port", service));
            options.addOption(option(null, service + "-bind", "host", "cmd.start.opt.bind", service));
        }
    } catch (Exception e) {
        services = Collections.EMPTY_SET;
    }
    CommandLine line = null;
    try {
        // parse the command line arguments
        line = parser.parse(options, args);
    } catch (ParseException exp) {
        help(options);
        throw new SystemExitException(-1);
    }
    if (line.hasOption("help")) {
        help(options);
        return;
    } else if (line.hasOption("version")) {
        OpenEjbVersion.get().print(System.out);
        return;
    } else if (line.hasOption("examples")) {
        try {
            String text = finder.findString("org.apache.openejb.cli/start.examples");
            System.out.println(text);
            return;
        } catch (Exception e) {
            System.err.println("Unable to print examples:");
            e.printStackTrace(System.err);
            throw new SystemExitException(-2);
        }
    }
    Properties props = SystemInstance.get().getProperties();
    if (line.hasOption("conf")) {
        props.setProperty("openejb.configuration", line.getOptionValue("conf"));
    } else if (line.hasOption("local-copy")) {
        String value = line.getOptionValue("local-copy");
        if (value.equalsIgnoreCase("false") || value.equalsIgnoreCase("no") || value.equalsIgnoreCase("off")) {
            props.setProperty("openejb.localcopy", "false");
        } else {
            props.setProperty("openejb.localcopy", "true");
        }
    }
    for (String service : services) {
        String[] opts = { "port", "bind" };
        for (String opt : opts) {
            String option = service + "-" + opt;
            if (line.hasOption(option)) {
                props.setProperty(service + "." + opt, line.getOptionValue(option));
            }
        }
    }
    try {
        final SystemInstance system = SystemInstance.get();
        File libs = system.getHome().getDirectory("lib");
        system.getClassPath().addJarsToPath(libs);
        initServer(system);
    } catch (DontStartServerException ignored) {
    } catch (Exception e) {
        e.printStackTrace();
    }
}
Also used : Options(org.apache.commons.cli.Options) ResourceFinder(org.apache.xbean.finder.ResourceFinder) PosixParser(org.apache.commons.cli.PosixParser) SystemExitException(org.apache.openejb.cli.SystemExitException) Properties(java.util.Properties) ParseException(org.apache.commons.cli.ParseException) SystemExitException(org.apache.openejb.cli.SystemExitException) CommandLine(org.apache.commons.cli.CommandLine) SystemInstance(org.apache.openejb.loader.SystemInstance) CommandLineParser(org.apache.commons.cli.CommandLineParser) ParseException(org.apache.commons.cli.ParseException) File(java.io.File)

Example 10 with ResourceFinder

use of org.apache.xbean.finder.ResourceFinder in project tomee by apache.

the class ConfigUtils method createConfig.

public static File createConfig(final File config) throws IOException {
    final ResourceFinder finder = new ResourceFinder("");
    final URL defaultConfig = finder.find("default.openejb.conf");
    IO.copy(IO.read(defaultConfig), config);
    return config;
}
Also used : ResourceFinder(org.apache.xbean.finder.ResourceFinder) URL(java.net.URL)

Aggregations

ResourceFinder (org.apache.xbean.finder.ResourceFinder)20 IOException (java.io.IOException)12 URL (java.net.URL)12 File (java.io.File)7 HashMap (java.util.HashMap)6 Map (java.util.Map)5 ArrayList (java.util.ArrayList)4 Properties (java.util.Properties)4 InputStream (java.io.InputStream)3 TreeMap (java.util.TreeMap)3 Options (org.apache.commons.cli.Options)3 ParseException (org.apache.commons.cli.ParseException)3 MalformedURLException (java.net.MalformedURLException)2 URLClassLoader (java.net.URLClassLoader)2 LinkedHashMap (java.util.LinkedHashMap)2 LinkedList (java.util.LinkedList)2 CommandLine (org.apache.commons.cli.CommandLine)2 CommandLineParser (org.apache.commons.cli.CommandLineParser)2 PosixParser (org.apache.commons.cli.PosixParser)2 OpenEJBException (org.apache.openejb.OpenEJBException)2