Example usage for java.lang ClassLoader getSystemResource

List of usage examples for java.lang ClassLoader getSystemResource

Introduction

In this page you can find the example usage for java.lang ClassLoader getSystemResource.

Prototype

public static URL getSystemResource(String name) 

Source Link

Document

Find a resource of the specified name from the search path used to load classes.

Usage

From source file:org.openpnp.Main.java

public static void main(String[] args) {
    // http://developer.apple.com/library/mac/#documentation/Java/Conceptual/Java14Development/07-NativePlatformIntegration/NativePlatformIntegration.html#//apple_ref/doc/uid/TP40001909-212952-TPXREF134
    System.setProperty("apple.laf.useScreenMenuBar", "true");
    try {//from w  w w  .j  a va  2s  .c o  m
        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
    } catch (Exception e) {
        throw new Error(e);
    }

    File configurationDirectory = new File(System.getProperty("user.home"));
    configurationDirectory = new File(configurationDirectory, ".openpnp");

    // If the log4j.properties is not in the configuration directory, copy
    // the default over.
    File log4jConfigurationFile = new File(configurationDirectory, "log4j.properties");
    if (!log4jConfigurationFile.exists()) {
        try {
            FileUtils.copyURLToFile(ClassLoader.getSystemResource("log4j.properties"), log4jConfigurationFile);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    // Use the local configuration if it exists.
    if (log4jConfigurationFile.exists()) {
        System.setProperty("log4j.configuration", log4jConfigurationFile.toURI().toString());
    }

    // We don't create a logger until log4j has been configured or it tries
    // to configure itself.
    logger = LoggerFactory.getLogger(Main.class);

    logger.debug(String.format("OpenPnP %s Started.", Main.getVersion()));

    Configuration.initialize(configurationDirectory);
    final Configuration configuration = Configuration.get();
    final JobProcessor jobProcessor = new JobProcessor(configuration);
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                MainFrame frame = new MainFrame(configuration, jobProcessor);
                frame.setVisible(true);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}

From source file:org.apache.rya.jena.jenasesame.example.JenaSesameExample.java

public static void main(final String args[]) throws Exception {
    Repository repo = null;//from w  w w  .java2 s.  c  o  m
    RepositoryConnection addConnection = null;
    RepositoryConnection queryConnection = null;
    QueryExecution queryExecution = null;
    try {
        repo = new SailRepository(new MemoryStore());
        repo.initialize();

        // Load some data.
        addConnection = repo.getConnection();

        // Reads files relative from target/test-classes which should have been copied from src/test/resources
        final URL url = ClassLoader.getSystemResource("rdf_format_files/turtle_files/turtle_data.ttl");
        final File file = ResourceUtils.getFile(url);
        final String fileName = file.getAbsolutePath();
        final RDFFormat rdfFormat = RDFFormat.forFileName(fileName);

        log.info("Added RDF file with " + rdfFormat.getName() + " format: " + fileName);
        addConnection.add(file, "http://base/", rdfFormat);
        addConnection.close();

        queryConnection = repo.getConnection();

        final Dataset dataset = JenaSesame.createDataset(queryConnection);

        final Model model = dataset.getDefaultModel();
        log.info(model.getNsPrefixMap());

        final String object = "susandillon@gmail.com";
        final String queryString = "prefix : <http://example/> SELECT * { ?s ?p '" + object + "' }";
        final Query query = QueryFactory.create(queryString);
        queryExecution = QueryExecutionFactory.create(query, dataset);
        QueryExecUtils.executeQuery(query, queryExecution);
    } catch (final Exception e) {
        log.error("Encountered an exception while performing query.", e);
    } finally {
        if (queryExecution != null) {
            queryExecution.close();
        }
        if (addConnection != null) {
            addConnection.close();
        }
        if (queryConnection != null) {
            queryConnection.close();
        }
        if (repo != null) {
            repo.shutDown();
        }
    }
}

From source file:org.apache.rya.jena.jenasesame.example.JenaReasoningWithRulesExample.java

public static void main(final String args[]) throws Exception {
    Repository repo = null;//w  w w.j  a  v  a  2s  . c  o m
    RepositoryConnection addConnection = null;
    RepositoryConnection queryConnection = null;
    try {
        repo = new SailRepository(new MemoryStore());
        repo.initialize();

        // Load some data.
        addConnection = repo.getConnection();

        // Reads files relative from target/test-classes which should have been copied from src/test/resources
        final URL url = ClassLoader.getSystemResource("rdf_format_files/notation3_files/n3_data.n3");
        final File file = ResourceUtils.getFile(url);
        final String fileName = file.getAbsolutePath();
        final RDFFormat rdfFormat = RDFFormat.forFileName(fileName);

        log.info("Added RDF file with " + rdfFormat.getName() + " format: " + fileName);
        addConnection.add(file, "http://base/", rdfFormat);
        addConnection.close();

        queryConnection = repo.getConnection();

        final Dataset dataset = JenaSesame.createDataset(queryConnection);

        final Model model = dataset.getDefaultModel();
        log.info(model.getNsPrefixMap());

        final URL rulesUrl = ClassLoader
                .getSystemResource("rdf_format_files/notation3_files/rule_files/rules.txt");
        final File rulesFile = ResourceUtils.getFile(rulesUrl);
        final String rulesFileName = rulesFile.getAbsolutePath();

        final Reasoner reasoner = new GenericRuleReasoner(Rule.rulesFromURL(rulesFileName));

        final InfModel infModel = ModelFactory.createInfModel(reasoner, model);

        final StmtIterator iterator = infModel.listStatements();

        while (iterator.hasNext()) {
            final Statement stmt = iterator.nextStatement();

            final Resource subject = stmt.getSubject();
            final Property predicate = stmt.getPredicate();
            final RDFNode object = stmt.getObject();

            log.info(subject.toString() + " " + predicate.toString() + " " + object.toString());
        }
    } catch (final Exception e) {
        log.error("Encountered an exception while running reasoner.", e);
    } finally {
        if (addConnection != null) {
            addConnection.close();
        }
        if (queryConnection != null) {
            queryConnection.close();
        }
        if (repo != null) {
            repo.shutDown();
        }
    }
}

From source file:org.tolven.plugin.boot.TPFBoot.java

public static void main(String[] args) throws Exception {
    URL manifestURL = ClassLoader.getSystemResource("tolven-plugin.xml");
    TPF_VERSION = RepositoryMetadata.getVersion(manifestURL);
    System.setProperty(TPF_VERSION_PROPERTY, TPF_VERSION);
    Option confOption = new Option(CMD_LINE_CONF_OPTION, CMD_LINE_CONF_OPTION, true, "configuration directory");
    OptionGroup optionGroup = new OptionGroup();
    optionGroup.setRequired(true);//from w ww  .  jav  a  2 s  . c o  m
    Option upgradeOption = new Option(CMD_LINE_GETPLUGINS_OPTION, CMD_LINE_GETPLUGINS_OPTION, false,
            "getPlugins from repositories");
    optionGroup.addOption(upgradeOption);
    Option pluginOption = new Option(CMD_LINE_PLUGIN_OPTION, CMD_LINE_PLUGIN_OPTION, true,
            "execute one or more plugins");
    optionGroup.addOption(pluginOption);
    Option genMetadataOption = new Option(CMD_LINE_GENMETADATA_OPTION, CMD_LINE_GENMETADATA_OPTION, true,
            "generate a repository metadata file");
    optionGroup.addOption(genMetadataOption);
    Option versionOption = new Option(CMD_LINE_VERSION_OPTION, CMD_LINE_VERSION_OPTION, false, "TPF Version");
    optionGroup.addOption(versionOption);
    Option repositorySnapshotOption = new Option(CMD_LINE_REPOSITORYSNAPSHOT_OPTION,
            CMD_LINE_REPOSITORYSNAPSHOT_OPTION, false, "create snapshot of repositories");
    optionGroup.addOption(repositorySnapshotOption);
    Options cmdLineOptions = new Options();
    cmdLineOptions.addOption(confOption);
    cmdLineOptions.addOptionGroup(optionGroup);
    try {
        GnuParser parser = new GnuParser();
        CommandLine commandLine = parser.parse(cmdLineOptions, args, true);
        String configDirname = getCommandLineConfigDir(commandLine);
        File configDir = new File(configDirname);
        pluginsWrapper = new ConfigPluginsWrapper();
        pluginsWrapper.loadConfigDir(configDir);
        Logger.getLogger(TPFBoot.class.getName()).info("TPF Version: " + TPF_VERSION);
        Logger.getLogger(TPFBoot.class.getName()).info("Loaded configDir " + configDir.getPath());
        boolean upgrade = commandLine.hasOption(CMD_LINE_GETPLUGINS_OPTION);
        String plugins = commandLine.getOptionValue(CMD_LINE_PLUGIN_OPTION);
        boolean genMetadata = commandLine.hasOption(CMD_LINE_GENMETADATA_OPTION);
        boolean versionRequest = commandLine.hasOption(CMD_LINE_VERSION_OPTION);
        boolean repositorySnapshot = commandLine.hasOption(CMD_LINE_REPOSITORYSNAPSHOT_OPTION);
        if (upgrade) {
            //RepositoryUpgrade.main(commandLine.getArgs()); //CLI seems to only recognize the first letter of an option?
            StringBuffer buff = new StringBuffer();
            for (String arg : args) {
                if (!("-" + CMD_LINE_GETPLUGINS_OPTION).equals(arg)) {
                    buff.append(arg + ",");
                }
            }
            RepositoryUpgrade.main(buff.toString().split(","));
        } else if (genMetadata) {
            StringBuffer buff = new StringBuffer();
            for (String arg : args) {
                if (!("-" + CMD_LINE_GENMETADATA_OPTION).equals(arg)) {
                    buff.append(arg + ",");
                }
            }
            RepositoryMetadata.main(buff.toString().split(","));
        } else if (versionRequest) {
            System.out.println("\nTPF Version: " + TPF_VERSION);
        } else if (repositorySnapshot) {
            StringBuffer buff = new StringBuffer();
            for (String arg : args) {
                if (!("-" + CMD_LINE_REPOSITORYSNAPSHOT_OPTION).equals(arg)) {
                    buff.append(arg + ",");
                }
            }
            RepositorySnapshot.main(buff.toString().split(","));
        } else if (plugins != null) {
            addBootRepositoryRuntime(pluginsWrapper, configDir);
            Boot.main(args);
        }
    } catch (ParseException ex) {
        HelpFormatter formatter = new HelpFormatter();
        formatter.printHelp(TPFBoot.class.getName(), cmdLineOptions);
        throw new RuntimeException("Could not parse command line for: " + TPFBoot.class.getName(), ex);
    }
}

From source file:volker.streaming.music.gui.NowPlayingFrame.java

public static void main(String[] args) {
    String configFileName = "nowplaying.properties";
    URL configUrl = ClassLoader.getSystemResource(configFileName);
    if (configUrl == null) {
        LOG.warn("Failed to find config file " + configFileName);
    }//from w ww  .ja  v a  2  s . c  o m
    File config = configUrl == null ? new File(configFileName) : new File(configUrl.getFile());

    final NowPlayingFrame frame = new NowPlayingFrame(config);

    java.awt.EventQueue.invokeLater(new Runnable() {
        public void run() {
            frame.setVisible(true);
        }
    });
}

From source file:ch.sdi.core.impl.ftp.FTPClientExample.java

public static void main(String[] aArgs) throws UnknownHostException {
    List<String> args = new ArrayList<String>(Arrays.asList(aArgs));

    args.add("-s"); // store file on sesrver
    args.add("-b"); // binary transfer mode
    args.add("-#");

    args.add("192.168.99.1");
    args.add("heri"); // user
    args.add("heri"); // pw
    args.add("/var/www/log4j2.xml");

    URL url = ClassLoader.getSystemResource("sdimain_test.properties");
    // URL url = ClassLoader.getSystemResource( "log4j2.xml" );
    args.add(url.getFile());/*  w ww .j a va 2s . co  m*/

    FTPClientExample example = new FTPClientExample();
    try {
        example.init(args.toArray(new String[args.size()]));
        example.run();
    } catch (Throwable t) {
        myLog.error("Exception caught", t);
        myLog.info(USAGE);
        System.exit(1);
    }

}

From source file:Main.java

public static ImageIcon getImageIcon(String resource) {
    URL imageURL = ClassLoader.getSystemResource(resource);
    ImageIcon image = new ImageIcon(imageURL);
    return image;
}

From source file:com.base2.kagura.services.camel.utils.TestUtils.java

public static String getResourcePath(String path) {
    URL dir_url = ClassLoader.getSystemResource(path);
    return dir_url.getFile();
}

From source file:cn.ipanel.apps.portalBackOffice.util.TestBeanFactory.java

private static String getFilePath(String xmlName) {
    return ClassLoader.getSystemResource(xmlName).toString();
}

From source file:org.apache.hadoop.test.TestUtils.java

public static URL getResourceUrl(Class clazz, String name) throws FileNotFoundException {
    name = getResourceName(clazz, name);
    URL url = ClassLoader.getSystemResource(name);
    if (url == null) {
        throw new FileNotFoundException(name);
    }//from  w w w .  j  a  v a  2  s .  c  om
    return url;
}