Search in sources :

Example 11 with ResourceFinder

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

the class MainImpl method main.

public void main(String[] args) {
    args = processSystemProperties(args);
    finder = new ResourceFinder(BASE_PATH);
    locale = Locale.getDefault().getLanguage();
    descriptionI18n = descriptionBase + "." + locale;
    final CommandLineParser parser = new PosixParser();
    // create the Options
    final Options options = new Options();
    options.addOption(null, "version", false, "Display version");
    options.addOption("h", "help", false, "Display help");
    options.addOption("e", "errors", false, "Produce execution error messages");
    CommandLine line = null;
    String commandName = null;
    try {
        // parse the arguments up until the first
        // command, then let the rest fall into
        // the arguments array.
        line = parser.parse(options, args, true);
        // Get and remove the commandName (first arg)
        final List<String> list = line.getArgList();
        if (list.size() > 0) {
            commandName = list.get(0);
            list.remove(0);
        }
        // The rest of the args will be passed to the command
        args = line.getArgs();
    } catch (final ParseException exp) {
        exp.printStackTrace();
        System.exit(-1);
    }
    if (line.hasOption("version")) {
        OpenEjbVersion.get().print(System.out);
        System.exit(0);
    } else if (line.hasOption("help") || commandName == null || commandName.equals("help")) {
        help();
        System.exit(0);
    }
    Properties props = null;
    try {
        props = finder.findProperties(commandName);
    } catch (final IOException e1) {
        System.out.println("Unavailable command: " + commandName);
        help(false);
        System.exit(1);
    }
    if (props == null) {
        System.out.println("Unavailable command: " + commandName);
        help(false);
        System.exit(1);
    }
    // Shift the command name itself off the args list
    final String mainClass = props.getProperty(MAIN_CLASS_PROPERTY_NAME);
    if (mainClass == null) {
        throw new NullPointerException("Command " + commandName + " did not specify a " + MAIN_CLASS_PROPERTY_NAME + " property");
    }
    Class<?> clazz = null;
    try {
        clazz = Thread.currentThread().getContextClassLoader().loadClass(mainClass);
    } catch (final ClassNotFoundException cnfe) {
        throw new IllegalStateException("Main class of command " + commandName + " does not exist: " + mainClass, cnfe);
    }
    Method mainMethod = null;
    try {
        mainMethod = clazz.getMethod("main", String[].class);
    } catch (final Exception e) {
        throw new IllegalStateException("Main class of command " + commandName + " does not have a static main method: " + mainClass, e);
    }
    try {
        // WARNING, Definitely do *not* unwrap 'new Object[]{args}' to 'args'
        mainMethod.invoke(clazz, new Object[] { args });
    } catch (final Throwable e) {
        if (line.hasOption("errors")) {
            e.printStackTrace();
        }
        System.exit(-10);
    }
}
Also used : ResourceFinder(org.apache.xbean.finder.ResourceFinder) Options(org.apache.commons.cli.Options) PosixParser(org.apache.commons.cli.PosixParser) IOException(java.io.IOException) Method(java.lang.reflect.Method) Properties(java.util.Properties) IOException(java.io.IOException) ParseException(org.apache.commons.cli.ParseException) CommandLine(org.apache.commons.cli.CommandLine) CommandLineParser(org.apache.commons.cli.CommandLineParser) ParseException(org.apache.commons.cli.ParseException)

Example 12 with ResourceFinder

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

the class PasswordCipherFactory method doInternalPasswordCipher.

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.";
        throw new PasswordCipherException(message, t);
    }
    pwdCipher = impls.get(passwordCipherClass);
    // 
    final ClassLoader tccl = Thread.currentThread().getContextClassLoader();
    final URL url = tccl.getResource("META-INF/" + intf.getName() + "/" + passwordCipherClass);
    if (url != null) {
        try {
            final String clazz = new BufferedReader(new InputStreamReader(url.openStream())).readLine().trim();
            pwdCipher = tccl.loadClass(clazz).asSubclass(intf);
        } catch (final Exception e) {
        // ignored
        }
    }
    // we can try to load the class.
    if (null == pwdCipher) {
        try {
            try {
                pwdCipher = Class.forName(passwordCipherClass).asSubclass(intf);
            } catch (final ClassNotFoundException cnfe) {
                pwdCipher = tccl.loadClass(passwordCipherClass).asSubclass(intf);
            }
        } catch (final Throwable t) {
            final String message = "Cannot load password cipher class '" + passwordCipherClass + "'";
            throw new PasswordCipherException(message, t);
        }
    }
    // Create an instance
    try {
        return pwdCipher.newInstance();
    } catch (final Throwable t) {
        final String message = "Cannot create password cipher instance";
        throw new PasswordCipherException(message, t);
    }
}
Also used : ResourceFinder(org.apache.xbean.finder.ResourceFinder) InputStreamReader(java.io.InputStreamReader) URL(java.net.URL) BufferedReader(java.io.BufferedReader)

Example 13 with ResourceFinder

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

the class CheckDescriptorLocation method retrieveDescriptors.

private static Map<String, URL> retrieveDescriptors(final List<String> descriptorsToSearch, final URL... locationsToSearch) {
    final Map<String, URL> descriptorAndWrongLocation = new HashMap<>();
    final ResourceFinder finder = new ResourceFinder(locationsToSearch);
    for (final String descriptor : descriptorsToSearch) {
        final URL resource = finder.getResource(descriptor);
        if (resource != null) {
            descriptorAndWrongLocation.put(descriptor, resource);
        }
    }
    return descriptorAndWrongLocation;
}
Also used : ResourceFinder(org.apache.xbean.finder.ResourceFinder) HashMap(java.util.HashMap) URL(java.net.URL)

Example 14 with ResourceFinder

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

the class InstantdbDataSourcePlugin method updatedUrl.

@Override
public String updatedUrl(final String jdbcUrl) {
    // jdbc:idb:conf/instantdb.properties
    final String prefix = "jdbc:idb:";
    final int index = jdbcUrl.indexOf(prefix);
    if (index == -1) {
        return jdbcUrl;
    }
    final String confFile = jdbcUrl.substring(index + prefix.length());
    final File base = SystemInstance.get().getBase().getDirectory();
    final File file = new File(base, confFile);
    if (file.exists()) {
        // The instantdb properties file is there, we're good
        return jdbcUrl;
    }
    if (!file.getParentFile().exists()) {
        // doesn't exist, don't bother
        return jdbcUrl;
    }
    try {
        final ResourceFinder finder = new ResourceFinder("");
        final String defaultProperties = finder.findString("default.instantdb.properties");
        IO.copy(defaultProperties.getBytes(), file);
    } catch (final IOException e) {
        // TODO; Handle this
        e.printStackTrace();
    }
    return jdbcUrl;
}
Also used : ResourceFinder(org.apache.xbean.finder.ResourceFinder) IOException(java.io.IOException) File(java.io.File)

Example 15 with ResourceFinder

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

the class BasicDataSourceUtil method getDataSourcePlugin.

public static DataSourcePlugin getDataSourcePlugin(final String jdbcUrl) throws SQLException {
    // determine the vendor based on the jdbcUrl stirng "jdbc:${Vendor}:properties"
    final String vendor = getJdbcName(jdbcUrl);
    // no vendor so no plugin
    if (vendor == null) {
        return null;
    }
    // find the plugin class
    String pluginClassName = null;
    try {
        final ResourceFinder finder = new ResourceFinder("META-INF");
        final Map<String, String> plugins = finder.mapAvailableStrings(DataSourcePlugin.class.getName());
        pluginClassName = plugins.get(vendor);
    } catch (final IOException ignored) {
    // couldn't determine the plugins, which isn't fatal
    }
    // no plugin found
    if (pluginClassName == null || pluginClassName.length() <= 0) {
        return null;
    }
    // create the plugin
    try {
        final Class pluginClass = Class.forName(pluginClassName);
        return (DataSourcePlugin) pluginClass.newInstance();
    } catch (final ClassNotFoundException e) {
        throw new SQLException("Unable to load data source helper class '" + pluginClassName + "' for database '" + vendor + "'");
    } catch (final Exception e) {
        throw (SQLException) new SQLException("Unable to create data source helper class '" + pluginClassName + "' for database '" + vendor + "'").initCause(e);
    }
}
Also used : ResourceFinder(org.apache.xbean.finder.ResourceFinder) DataSourcePlugin(org.apache.openejb.resource.jdbc.plugin.DataSourcePlugin) SQLException(java.sql.SQLException) IOException(java.io.IOException) SQLException(java.sql.SQLException) IOException(java.io.IOException)

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