Example usage for java.lang Class getResource

List of usage examples for java.lang Class getResource

Introduction

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

Prototype

@CallerSensitive
public URL getResource(String name) 

Source Link

Document

Finds a resource with a given name.

Usage

From source file:org.kalypso.commons.eclipse.core.runtime.PluginImageProvider.java

/**
 * Copies a resource-file into the tmp-directory and returns a (file-)url to it.
 *
 * @param clazz/*from   w ww .ja  v a 2s  .com*/
 *          The class to which the resource path will be resolved
 * @param path
 *          Path into the java-resources
 * @see Class#getResource(java.lang.String)
 */
public URL getTmpUrl(final Class<?> clazz, final String path) {
    return getTmpUrl(path, clazz.getResource(path));
}

From source file:org.kawanfw.file.reflection.Reloader.java

private byte[] loadClassData(String className) throws IOException, ClassNotFoundException {

    DataInputStream dis = null;//  ww  w .  ja va2 s . co  m

    try {

        debug("className: " + className);

        /*
         * get the actual path using the original class loader
         */
        Class<?> clazz = orig.loadClass(className);

        String simpleName = StringUtils.substringAfterLast(className, ".");
        debug("clazz                : " + clazz);
        debug("clazz.getSimpleName(): " + simpleName);

        url = clazz.getResource(simpleName + ".class");
        debug("url: " + url);

        /*
         * force reload
         */
        File file = new File(url.toURI());
        int size = (int) file.length();
        byte buff[] = new byte[size];
        dis = new DataInputStream(new FileInputStream(file));
        dis.readFully(buff);

        return buff;
    } catch (ClassNotFoundException ex) {
        throw ex;
    } catch (Exception ex) {
        throw new IOException(ex);
    } finally {
        if (dis != null) {
            dis.close();
        }
    }
}

From source file:org.modelibra.util.PathLocator.java

/**
 * Gets the file URL given a local path anchored to the class base.
 * //from w  ww. j  a  v a 2  s  . co  m
 * @param claz
 *            class that is an anchor point
 * @param localPath
 *            local path
 * @return file URL
 */
public URL getClassBasedUrl(Class<?> claz, String localPath) {
    return claz.getResource(localPath);
}

From source file:org.openlegacy.modules.trail.TrailUtil.java

public void saveTestTrail(Session session) {
    if (!session.isConnected()) {
        return;//from   ww  w.j  av a 2 s  .c  o m
    }
    FileOutputStream out = null;
    try {
        String testClass = Thread.currentThread().getStackTrace()[2].getClassName();
        Class<?> clazz = Class.forName(testClass);
        String classLocation = clazz.getResource(".").getFile();
        File parentFile = new File(classLocation);
        while (!parentFile.getName().equals("target")) {
            parentFile = parentFile.getParentFile();
        }
        parentFile = parentFile.getParentFile();
        out = new FileOutputStream(parentFile + "/" + testTrail);
        trailWriter.write(session.getModule(Trail.class).getSessionTrail(), out);
    } catch (FileNotFoundException e) {
        throw (new RuntimeException(e));
    } catch (ClassNotFoundException e) {
        throw (new RuntimeException(e));
    } finally {
        IOUtils.closeQuietly(out);

    }
    logger.info(MessageFormat.format("A new trail file for test {0} was created in project root", testTrail));
}

From source file:org.openlegacy.rpc.loaders.support.RpcEntityAnnotationLoader.java

private static void loadSourceCode(Class<?> containingClass, String rpcEntityName,
        SimpleRpcEntityDefinition rpcEntityDefinition) {
    String srcResourceName = MessageFormat.format("{0}-resources/{1}.src", rpcEntityName,
            containingClass.getSimpleName());
    URL srcResource = containingClass.getResource(srcResourceName);
    if (srcResource != null) {
        try {/*from w w w  .  j  ava2 s  .c om*/
            String sourceCode = IOUtils.toString(srcResource);
            rpcEntityDefinition.setSourceCode(sourceCode);
        } catch (Exception e) {
            logger.warn("Failed to source code for " + rpcEntityName);
        }
    }
}

From source file:org.openlegacy.terminal.loaders.support.ScreenEntityAnnotationLoader.java

private static void loadSnapshot(Class<?> containingClass, String screenEntityName,
        SimpleScreenEntityDefinition screenEntityDefinition) {
    String snapshotXmlResourceName = MessageFormat.format("{0}-resources/{1}.xml", screenEntityName,
            containingClass.getSimpleName());
    URL snapshotXmlResource = containingClass.getResource(snapshotXmlResourceName);
    if (snapshotXmlResource != null) {
        try {//from  ww w.ja v a  2 s .co m
            TerminalPersistedSnapshot snapshot = XmlSerializationUtil
                    .deserialize(TerminalPersistedSnapshot.class, snapshotXmlResource.openStream());
            screenEntityDefinition.setSnapshot(snapshot);
        } catch (Exception e) {
            logger.warn("Failed to load snapshot for " + screenEntityName);
        }
    }
}

From source file:org.openmrs.module.formentry.FormEntryUtil.java

/**
 * Finds a folder path within resources and returns it as a File
 * /*from   www  .ja va  2 s . co  m*/
 * @should throw a FileNotFoundException if the folderPath does not exist
 * @should return both directories and files
 * @param resourcePath path to the resource
 * @return file handle for the resource
 * @throws IOException
 */
public static File getResourceFile(String resourcePath) throws IOException {

    log.debug("Getting URL directory: " + resourcePath);

    Class<FormEntryUtil> c = FormEntryUtil.class;

    // get the location of the starter documents
    URL url = c.getResource(resourcePath);
    if (url == null) {
        String err = "Could not open resource folder directory: " + resourcePath;
        log.error(err);
        throw new FileNotFoundException(err);
    }

    return OpenmrsUtil.url2file(url);
}

From source file:org.opensingular.form.processor.TypeProcessorAttributeReadFromFile.java

/** Verifica se h um arquivos com valores de atributos associados a classe informada. */
@Nullable//  w w  w  .j  av a2s .c  o m
private URL lookForFile(@Nonnull Class<?> typeClass) {
    String name = typeClass.getSimpleName();
    Class<?> context = typeClass;
    for (; context.isMemberClass(); context = context.getEnclosingClass()) {
        name = concatNames(context, name);
    }
    return context.getResource(name + SUFFIX_PROPERTIES);
}

From source file:org.overlord.sramp.repository.jcr.modeshape.ModeshapeRepository.java

/**
* Gets the configuration to use for the JCR repository.
* @throws Exception/*www . j  a va2 s .c  o m*/
*/
private URL getModeshapeConfigurationUrl() throws Exception {
    String configUrl = sramp.getConfigProperty("sramp.modeshape.config.url", null); //$NON-NLS-1$
    if (configUrl == null) {
        return null;
    }
    if (configUrl.startsWith("classpath:")) { //$NON-NLS-1$
        Pattern p = Pattern.compile("classpath:/?/?([^/]*)/(.*)$"); //$NON-NLS-1$
        Matcher matcher = p.matcher(configUrl);
        if (matcher.matches()) {
            String className = matcher.group(1);
            String path = "/" + matcher.group(2); //$NON-NLS-1$
            Class<?> clazz = Class.forName(className);
            URL resourceUrl = clazz.getResource(path);
            if (resourceUrl == null)
                throw new Exception(Messages.i18n.format("MISSING_CONFIG", configUrl)); //$NON-NLS-1$
            return resourceUrl;
        }
        throw new Exception(Messages.i18n.format("INVALID_CLASSPATH_URL", configUrl)); //$NON-NLS-1$
    } else {
        try {
            File f = new File(configUrl);
            if (f.isFile()) {
                return f.toURI().toURL();
            }
        } catch (Exception e) {
            // eat the error and try the next option
        }

        try {
            return new URL(configUrl);
        } catch (Exception e) {
            // eat the error and try the next option
        }

        return null;
    }
}

From source file:org.pentaho.reporting.engine.classic.core.testsupport.DebugReportRunner.java

public static MasterReport parseLocalReport(final String name, Class<?> context) throws ResourceException {
    final URL file = context.getResource(name);
    if (file == null) {
        throw new ResourceException("Unable to locate report '" + name + "' near class " + context);
    }//from w w  w.  j a  v a  2  s  . co m

    final ResourceManager mgr = new ResourceManager();
    mgr.registerDefaults();
    return (MasterReport) mgr.createDirectly(file, MasterReport.class).getResource();
}