Example usage for java.lang ClassLoader getResource

List of usage examples for java.lang ClassLoader getResource

Introduction

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

Prototype

public URL getResource(String name) 

Source Link

Document

Finds the resource with the given name.

Usage

From source file:io.smartspaces.launcher.bootstrap.SmartSpacesFrameworkBootstrap.java

/**
 * Simple method to parse META-INF/services file for framework factory.
 * Currently, it assumes the first non-commented line is the class nodeName of
 * the framework factory implementation.
 *
 * @return the created <tt>FrameworkFactory</tt> instance
 *
 * @throws Exception/*  w w w  .  j ava  2 s.  c  o m*/
 *           if any errors occur.
 **/
private FrameworkFactory getFrameworkFactory() throws Exception {
    // using the ServiceLoader to get a factory.
    ClassLoader classLoader = SmartSpacesFrameworkBootstrap.class.getClassLoader();
    URL url = classLoader.getResource(OSGI_FRAMEWORK_LAUNCH_FRAMEWORK_FACTORY);
    if (url != null) {
        BufferedReader br = new BufferedReader(new InputStreamReader(url.openStream()));
        try {
            for (String s = br.readLine(); s != null; s = br.readLine()) {
                // Try to load first non-empty, non-commented line.
                s = s.trim();
                if (!s.isEmpty() && s.charAt(0) != META_INF_COMMENT_CHARACTER) {
                    return (FrameworkFactory) classLoader.loadClass(s).newInstance();
                }
            }
        } finally {
            if (br != null) {
                br.close();
            }
        }
    }

    throw new Exception("Could not find framework factory.");
}

From source file:org.apache.axis2.jaxws.util.WSDL4JWrapper.java

private URL getAbsoluteURL(final ClassLoader classLoader, final String filePath) {
    URL url = (URL) AccessController.doPrivileged(new PrivilegedAction() {
        public Object run() {
            return classLoader.getResource(filePath);
        }/*w  w w . ja  v a  2s .c o m*/
    });
    if (url == null) {
        if (log.isDebugEnabled()) {
            log.debug("Could not get URL from classloader. Looking in a jar.");
        }
        if (classLoader instanceof URLClassLoader) {
            final URLClassLoader urlLoader = (URLClassLoader) classLoader;

            url = (URL) AccessController.doPrivileged(new PrivilegedAction() {
                public Object run() {
                    return (getURLFromJAR(urlLoader, wsdlURL));
                }
            });

        } else {
            final URLClassLoader nestedLoader = (URLClassLoader) getNestedClassLoader(URLClassLoader.class,
                    classLoader);
            if (nestedLoader != null) {
                url = (URL) AccessController.doPrivileged(new PrivilegedAction() {
                    public Object run() {
                        return (getURLFromJAR(nestedLoader, wsdlURL));
                    }
                });
            }
        }
    }
    return url;
}

From source file:org.walkmod.conf.providers.SpringConfigurationProvider.java

@Override
public void loadBeanFactory() throws ConfigurationException {

    GenericApplicationContext ctx = new GenericApplicationContext();
    ClassLoader currentClassLoader = configuration.getClassLoader();
    if (currentClassLoader != Thread.currentThread().getContextClassLoader()) {
        XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(ctx);
        reader.setBeanClassLoader(currentClassLoader);
        reader.loadBeanDefinitions(new ClassPathResource(config, currentClassLoader));
        Collection<PluginConfig> plugins = configuration.getPlugins();
        if (plugins != null) {
            for (PluginConfig plugin : plugins) {
                String descriptorName = plugin.getArtifactId();
                if (!descriptorName.startsWith("walkmod-")) {
                    descriptorName = "walkmod-" + descriptorName;
                }//  w  w w.ja v  a  2s.  c o m
                if (!descriptorName.endsWith("-plugin")) {
                    descriptorName = descriptorName + "-plugin";
                }

                reader.loadBeanDefinitions(new ClassPathResource("META-INF/walkmod/" + descriptorName + ".xml",
                        configuration.getClassLoader()));

                URL url = currentClassLoader.getResource("META-INF/walkmod2/" + descriptorName + ".xml");
                if (url != null) {
                    reader.loadBeanDefinitions(new ClassPathResource(
                            "META-INF/walkmod2/" + descriptorName + ".xml", configuration.getClassLoader()));
                }
            }
        }
        configuration.setBeanDefinitionRegistry(reader.getRegistry());
        ctx.refresh();
    }

    configuration.setBeanFactory(ctx);
}

From source file:com.joyent.manta.client.crypto.MantaEncryptedObjectInputStreamTest.java

public MantaEncryptedObjectInputStreamTest() {
    final ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
    this.testURL = classLoader.getResource("test-data/chaucer.txt");

    try {//  w  w w.j  av a  2s.co m
        testFile = Paths.get(testURL.toURI());
    } catch (URISyntaxException e) {
        throw new AssertionError(e);
    }

    this.plaintextSize = (int) testFile.toFile().length();

    try (InputStream in = this.testURL.openStream()) {
        this.plaintextBytes = IOUtils.readFully(in, plaintextSize);
    } catch (IOException e) {
        throw new UncheckedIOException(e);
    }
}

From source file:org.apache.hadoop.yarn.server.resourcemanager.webapp.TestRMWebServicesReservation.java

private String loadJsonFile(String filename) throws IOException {
    ClassLoader cL = Thread.currentThread().getContextClassLoader();
    if (cL == null) {
        cL = Configuration.class.getClassLoader();
    }// ww  w  . j ava  2  s  .c  o  m
    URL submitURI = cL.getResource(filename);

    String reservationJson = FileUtils.readFileToString(new File(submitURI.getFile()));
    return reservationJson;
}

From source file:com.rapidminer.tools.Tools.java

public static URL getResource(ClassLoader loader, String prefix, String name) {
    return loader.getResource(prefix + name);
}

From source file:ffx.ui.KeywordPanel.java

private void initialize() {
    // Load the Keyword Definitions
    loadXML();/*  w w  w. j  a v  a2s .  co m*/
    // TextAreas
    flatfileTextArea = new JTextArea();
    flatfileTextArea.setEditable(false);
    flatfileTextArea.setFont(Font.decode("monospaced plain 12"));
    Insets insets = flatfileTextArea.getInsets();
    insets.set(5, 5, 5, 5);
    flatfileTextArea.setMargin(insets);
    // Keyword Edit Panel
    editPanel = new JPanel(flowLayout);
    ClassLoader loader = getClass().getClassLoader();
    ImageIcon icKeyPanel = new ImageIcon(loader.getResource("ffx/ui/icons/page_key.png"));
    noSystemLabel.setIcon(icKeyPanel);
    ImageIcon icon = new ImageIcon(loader.getResource("ffx/ui/icons/information.png"));
    noKeywordLabel.setIcon(icon);
    noKeywordPanel.add(noKeywordLabel);
    editScrollPane = new JScrollPane(editPanel, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
            JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
    descriptScrollPane = new JScrollPane(descriptTextArea, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
            JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
    Border eb = BorderFactory.createEtchedBorder(EtchedBorder.RAISED);
    descriptScrollPane.setBorder(eb);
    // Add the Keyword Group Panel and Decription Panel to a JSplitPane
    splitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT, editScrollPane, descriptScrollPane);
    splitPane.setResizeWeight(1.0);
    splitPane.setOneTouchExpandable(true);
    statusLabel.setBorder(eb);
    // Add the main pieces to the overall KeywordPanel (except the ToolBar)
    setLayout(new BorderLayout());
    add(splitPane, BorderLayout.CENTER);
    add(statusLabel, BorderLayout.SOUTH);
    // Init the GridBagContraints
    gridBagConstraints.gridx = 0;
    gridBagConstraints.gridy = 0;
    gridBagConstraints.anchor = GridBagConstraints.WEST;
    gridBagConstraints.gridheight = 1;
    gridBagConstraints.gridwidth = 1;
    gridBagConstraints.fill = GridBagConstraints.HORIZONTAL;
    initToolBar();
    add(toolBar, BorderLayout.NORTH);
    setParamPath();
    loadPrefs();
    loadKeywordGroup();
}

From source file:ffx.ui.KeywordPanel.java

/**
 * <p>/*from   w  w w  . j ava  2  s .  c om*/
 * initToolBar</p>
 */
public void initToolBar() {
    toolBar = new JToolBar("Keyword Editor");
    toolBar.setLayout(flowLayout);
    ClassLoader loader = getClass().getClassLoader();
    JButton jbopen = new JButton(new ImageIcon(loader.getResource("ffx/ui/icons/folder_page.png")));
    jbopen.setActionCommand("Open...");
    jbopen.setToolTipText("Open a *.KEY File for Editing");
    jbopen.addActionListener(this);
    Insets insets = jbopen.getInsets();
    insets.top = 2;
    insets.bottom = 2;
    insets.left = 2;
    insets.right = 2;
    jbopen.setMargin(insets);
    //toolBar.add(jbopen);
    JButton jbsave = new JButton(new ImageIcon(loader.getResource("ffx/ui/icons/disk.png")));
    jbsave.setActionCommand("Save");
    jbsave.setToolTipText("Save the Active *.KEY File");
    jbsave.addActionListener(this);
    jbsave.setMargin(insets);
    toolBar.add(jbsave);
    JButton jbsaveas = new JButton(new ImageIcon(loader.getResource("ffx/ui/icons/page_save.png")));
    jbsaveas.setActionCommand("Save As...");
    jbsaveas.setToolTipText("Save the Active *.KEY File Under a New Name");
    jbsaveas.addActionListener(this);
    jbsaveas.setMargin(insets);
    toolBar.add(jbsaveas);
    JButton jbclose = new JButton(new ImageIcon(loader.getResource("ffx/ui/icons/cancel.png")));
    jbclose.setActionCommand("Close");
    jbclose.setToolTipText("Close the Active *.KEY File");
    jbclose.addActionListener(this);
    jbclose.setMargin(insets);
    //toolBar.add(jbclose);
    toolBar.addSeparator();
    groupComboBox.setMaximumSize(groupComboBox.getPreferredSize());
    groupComboBox.addActionListener(this);
    groupComboBox.setEditable(false);
    toolBar.add(groupComboBox);
    toolBar.addSeparator();
    ImageIcon icinfo = new ImageIcon(loader.getResource("ffx/ui/icons/information.png"));
    descriptCheckBox = new JCheckBoxMenuItem(icinfo);
    descriptCheckBox.setActionCommand("Description");
    descriptCheckBox.setToolTipText("Show/Hide Keyword Descriptions");
    descriptCheckBox.addActionListener(this);
    descriptCheckBox.setMargin(insets);
    toolBar.add(descriptCheckBox);
    toolBar.add(new JLabel(""));
    toolBar.setBorderPainted(false);
    toolBar.setFloatable(false);
    toolBar.setRollover(true);
    toolBar.setOrientation(JToolBar.HORIZONTAL);
}

From source file:com.aurel.track.dbase.InitDatabase.java

private static void addCustomResourceToDatabase(Locale loc, String locCode) {
    ClassLoader cl = HandleHome.class.getClassLoader();
    File res = new File(HandleHome.getTrackplus_Home() + File.separator + HandleHome.XRESOURCES_DIR);
    if (res.exists() && res.isDirectory() && res.canWrite()) {
        String pfix = "_" + locCode;
        String lang = loc.getDisplayName();
        if (locCode == null) {
            pfix = "";
            lang = "Standard";
        }/*w w  w  .j  a v  a2  s  .  com*/
        try {
            URL propURL = cl.getResource("MyApplicationResources" + pfix + propertiesStr);
            InputStream in = propURL.openStream();
            LocalizeBL.saveResources(in, locCode, false, LocalizeBL.RESOURCE_CATEGORIES.UI_TEXT, false);
            LOGGER.info("Synchronized custom user interface resources for " + lang);
        } catch (Exception e) {
            LOGGER.debug(ExceptionUtils.getStackTrace(e));
        }
        try {
            URL propURL = cl.getResource("MyBoxResources" + pfix + propertiesStr);
            InputStream in = propURL.openStream();
            LocalizeBL.saveResources(in, locCode, false, LocalizeBL.RESOURCE_CATEGORIES.DB_ENTITY, false);
            LOGGER.info("Synchronized custom database entity resources for " + lang);
        } catch (Exception e) {
            LOGGER.debug(ExceptionUtils.getStackTrace(e), e);
        }
    }
}