Package org.apache.xbean.finder

Examples of org.apache.xbean.finder.ResourceFinder


            URL appUrl = getFileUrl(appDir);

            ClassLoader tmpClassLoader = new TemporaryClassLoader(new URL[]{appUrl}, OpenEJB.class.getClassLoader());

            ResourceFinder finder = new ResourceFinder("", tmpClassLoader, appUrl);

            Map<String, URL> appDescriptors;
            try {
                appDescriptors = finder.getResourcesMap("META-INF");
            } catch (IOException e) {
                throw new OpenEJBException("Unable to determine descriptors in jar: " + appUrl.toExternalForm(), e);
            }

            try {

                //
                // Find all the modules using either the application xml or by searching for all .jar, .war and .rar files.
                //

                Map<String, URL> ejbModules = new HashMap<String, URL>();
                Map<String, URL> clientModules = new HashMap<String, URL>();
                Map<String, URL> resouceModules = new HashMap<String, URL>();
                Map<String, URL> webModules = new HashMap<String, URL>();
                Map<String, String> webContextRoots = new HashMap<String, String>();

                URL applicationXmlUrl = appDescriptors.get("application.xml");

                Application application;
                if (applicationXmlUrl != null) {
                    application = unmarshal(Application.class, "application.xml", applicationXmlUrl);
                    for (Module module : application.getModule()) {
                        try {
                            if (module.getEjb() != null) {
                                URL url = finder.find(module.getEjb().trim());
                                ejbModules.put(module.getEjb(), url);
                            } else if (module.getJava() != null) {
                                URL url = finder.find(module.getJava().trim());
                                clientModules.put(module.getConnector(), url);
                            } else if (module.getConnector() != null) {
                                URL url = finder.find(module.getConnector().trim());
                                resouceModules.put(module.getConnector(), url);
                            } else if (module.getWeb() != null) {
                                URL url = finder.find(module.getWeb().getWebUri().trim());
                                webModules.put(module.getWeb().getWebUri(), url);
                                webContextRoots.put(module.getWeb().getWebUri(), module.getWeb().getContextRoot());
                            }
                        } catch (IOException e) {
                            throw new OpenEJBException("Invalid path to module " + e.getMessage(), e);
                        }
                    }
                } else {
                    application = new Application();
                    HashMap<String, URL> files = new HashMap<String, URL>();
                    scanDir(appDir, files, "");
                    files.remove("META-INF/MANIFEST.MF");
                    for (Map.Entry<String, URL> entry : files.entrySet()) {
                        if (entry.getKey().startsWith("lib/")) continue;
                        if (!entry.getKey().matches(".*\\.(jar|war|rar|ear)")) continue;

                        try {
                            ClassLoader moduleClassLoader = new TemporaryClassLoader(new URL[]{entry.getValue()}, tmpClassLoader);

                            Class moduleType = discoverModuleType(entry.getValue(), moduleClassLoader, true);
                            if (EjbModule.class.equals(moduleType)) {
                                ejbModules.put(entry.getKey(), entry.getValue());
                            } else if (ClientModule.class.equals(moduleType)) {
                                clientModules.put(entry.getKey(), entry.getValue());
                            } else if (ConnectorModule.class.equals(moduleType)) {
                                resouceModules.put(entry.getKey(), entry.getValue());
                            } else if (WebModule.class.equals(moduleType)) {
                                webModules.put(entry.getKey(), entry.getValue());
                            }
                        } catch (UnsupportedOperationException e) {
                            // Ignore it as per the javaee spec EE.8.4.2 section 1.d.iiilogger.info("Ignoring unknown module type: "+entry.getKey());
                        } catch (Exception e) {
                            throw new OpenEJBException("Unable to determine the module type of " + entry.getKey() + ": Exception: " + e.getMessage(), e);
                        }
                    }
                }

                //
                // Create a class loader for the application
                //

                // lib/*
                if (application.getLibraryDirectory() == null) {
                    application.setLibraryDirectory("lib/");
                } else {
                    String dir = application.getLibraryDirectory();
                    if (!dir.endsWith("/")) application.setLibraryDirectory(dir + "/");
                }
                List<URL> extraLibs = new ArrayList<URL>();
                try {
                    Map<String, URL> libs = finder.getResourcesMap(application.getLibraryDirectory());
                    extraLibs.addAll(libs.values());
                } catch (IOException e) {
                    logger.warning("Cannot load libs from '" + application.getLibraryDirectory() + "' : " + e.getMessage(), e);
                }

                // APP-INF/lib/*
                try {
                    Map<String, URL> libs = finder.getResourcesMap("APP-INF/lib/");
                    extraLibs.addAll(libs.values());
                } catch (IOException e) {
                    logger.warning("Cannot load libs from 'APP-INF/lib/' : " + e.getMessage(), e);
                }

                // META-INF/lib/*
                try {
                    Map<String, URL> libs = finder.getResourcesMap("META-INF/lib/");
                    extraLibs.addAll(libs.values());
                } catch (IOException e) {
                    logger.warning("Cannot load libs from 'META-INF/lib/' : " + e.getMessage(), e);
                }

                // All jars nested in the Resource Adapter
                HashMap<String, URL> rarLibs = new HashMap<String, URL>();
                for (Map.Entry<String, URL> entry : resouceModules.entrySet()) {
                    try {
                        // unpack the resource adapter archive
                        File rarFile = new File(entry.getValue().getPath());
                        rarFile = unpack(rarFile);
                        entry.setValue(rarFile.toURL());

                        scanDir(appDir, rarLibs, "");
                    } catch (MalformedURLException e) {
                        throw new OpenEJBException("Malformed URL to app. " + e.getMessage(), e);
                    }
                }
                for (Iterator<Map.Entry<String, URL>> iterator = rarLibs.entrySet().iterator(); iterator.hasNext();) {
                    // remove all non jars from the rarLibs
                    Map.Entry<String, URL> fileEntry = iterator.next();
                    if (!fileEntry.getKey().endsWith(".jar")) continue;
                    iterator.remove();
                }

                List<URL> classPath = new ArrayList<URL>();
                classPath.addAll(ejbModules.values());
                classPath.addAll(clientModules.values());
                classPath.addAll(rarLibs.values());
                classPath.addAll(extraLibs);
                URL[] urls = classPath.toArray(new URL[classPath.size()]);
                ClassLoader appClassLoader = new TemporaryClassLoader(urls, OpenEJB.class.getClassLoader());

                //
                // Create the AppModule and all nested module objects
                //

                AppModule appModule = new AppModule(appClassLoader, appDir.getAbsolutePath());
                appModule.getAdditionalLibraries().addAll(extraLibs);
                appModule.getAltDDs().putAll(appDescriptors);
                appModule.getWatchedResources().add(appDir.getAbsolutePath());
                if (applicationXmlUrl != null) {
                    appModule.getWatchedResources().add(applicationXmlUrl.getPath());
                }

                // EJB modules
                for (String moduleName : ejbModules.keySet()) {
                    try {
                        URL ejbUrl = ejbModules.get(moduleName);
                        File ejbFile = new File(ejbUrl.getPath());

                        Map<String, URL> descriptors = getDescriptors(ejbUrl);

                        EjbJar ejbJar = null;
                        URL ejbJarXmlUrl = descriptors.get("ejb-jar.xml");
                        if (ejbJarXmlUrl != null){
                            ejbJar = ReadDescriptors.readEjbJar(ejbJarXmlUrl);
                        }

                        EjbModule ejbModule = new EjbModule(appClassLoader, moduleName, ejbFile.getAbsolutePath(), ejbJar, null);

                        ejbModule.getAltDDs().putAll(descriptors);
                        ejbModule.getWatchedResources().add(ejbFile.getAbsolutePath());
                        if (ejbJarXmlUrl != null && "file".equals(ejbJarXmlUrl.getProtocol())) {
                            ejbModule.getWatchedResources().add(ejbJarXmlUrl.getPath());
                        }

                        // load webservices descriptor
                        addWebservices(ejbModule);

                        appModule.getEjbModules().add(ejbModule);
                    } catch (OpenEJBException e) {
                        logger.error("Unable to load EJBs from EAR: " + appDir.getAbsolutePath() + ", module: " + moduleName + ". Exception: " + e.getMessage(), e);
                    }
                }

                // Application Client Modules
                for (String moduleName : clientModules.keySet()) {
                    try {
                        URL clientUrl = clientModules.get(moduleName);
                        File clientFile = new File(clientUrl.getPath());
                        ResourceFinder clientFinder = new ResourceFinder(clientUrl);

                        URL manifestUrl = clientFinder.find("META-INF/MANIFEST.MF");
                        InputStream is = manifestUrl.openStream();
                        Manifest manifest = new Manifest(is);
                        String mainClass = manifest.getMainAttributes().getValue(Attributes.Name.MAIN_CLASS);

                        Map<String, URL> descriptors = getDescriptors(clientUrl);
View Full Code Here


        return connectorModule;
    }

    private void addPersistenceUnits(AppModule appModule, ClassLoader classLoader, URL... urls) {
        try {
            ResourceFinder finder = new ResourceFinder("", classLoader, urls);
            List<URL> persistenceUrls = finder.findAll("META-INF/persistence.xml");
            appModule.getAltDDs().put("persistence.xml", persistenceUrls);
        } catch (IOException e) {
            logger.warning("Cannot load persistence-units from 'META-INF/persistence.xml' : " + e.getMessage(), e);
        }
    }
View Full Code Here

        }
    }

    private Map<String, URL> getDescriptors(URL moduleUrl) throws OpenEJBException {
        try {
            ResourceFinder finder = new ResourceFinder(moduleUrl);

            return finder.getResourcesMap("META-INF/");

        } catch (IOException e) {
            throw new OpenEJBException("Unable to determine descriptors in jar.", e);
        }
    }
View Full Code Here

        }
    }


    public static Class<? extends DeploymentModule> discoverModuleType(URL baseUrl, ClassLoader classLoader, boolean searchForDescriptorlessApplications) throws IOException, UnknownModuleTypeException {
        ResourceFinder finder = new ResourceFinder("", classLoader, baseUrl);

        Map<String, URL> descriptors = finder.getResourcesMap("META-INF");

        String path = baseUrl.getPath();
        if (path.endsWith("/")) path = path.substring(0, path.length() - 1);
       
        if (descriptors.containsKey("application.xml") || path.endsWith(".ear")) {
View Full Code Here

        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 (IOException 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 (IOException e) {
                System.err.println("Unable to print examples:");
                e.printStackTrace();
View Full Code Here

        public ServiceFinder(String basePath) {
            this(basePath, Thread.currentThread().getContextClassLoader());
        }

        public ServiceFinder(String basePath, ClassLoader classLoader) {
            this.resourceFinder = new ResourceFinder(basePath, classLoader);
            this.classLoader = classLoader;
        }
View Full Code Here

    public static ServicesJar readServicesJar(String providerName) throws OpenEJBException {
        InputStream in = null;
        URL url = null;
        try {
            ResourceFinder finder = new ResourceFinder("META-INF/", Thread.currentThread().getContextClassLoader());
            url = finder.find(providerName + "/service-jar.xml");
            in = url.openStream();

            ServicesJar servicesJar = parseServicesJar(in);

//            ServicesJar servicesJar = unmarshal(ServicesJar.class, in);
View Full Code Here

    private OpenEjbVersion() {
        Properties info = new Properties();

        try {
            ResourceFinder finder = new ResourceFinder();
            info = finder.findProperties("openejb-version.properties");
        } catch (java.io.IOException e) {
            e.printStackTrace();
        }

        copyright = info.getProperty("copyright");
View Full Code Here

    private static <T extends PasswordCipher> T doInternalPasswordCipher(final Class<T> intf, final String passwordCipherClass) {
        // Load the password cipher class
        Class<? extends T> pwdCipher;

        // try looking for implementation in /META-INF/org.apache.openejb.cipher.PasswordCipher
        final ResourceFinder finder = new ResourceFinder("META-INF/");
        final Map<String, Class<? extends T>> impls;
        try {
            impls = finder.mapAllImplementations(intf);

        } catch (final Throwable t) {
            final String message =
                "Password cipher '" + passwordCipherClass +
                    "' not found in META-INF/org.apache.openejb.cipher.PasswordCipher.";
View Full Code Here

        }
        return new String[]{path};
    }

    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;
    }
View Full Code Here

TOP

Related Classes of org.apache.xbean.finder.ResourceFinder

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.