Example usage for org.springframework.core.io Resource getClass

List of usage examples for org.springframework.core.io Resource getClass

Introduction

In this page you can find the example usage for org.springframework.core.io Resource getClass.

Prototype

@HotSpotIntrinsicCandidate
public final native Class<?> getClass();

Source Link

Document

Returns the runtime class of this Object .

Usage

From source file:com.home.ln_spring.ch5.event.ResourceDemo.java

private static void displayInfo(Resource res) throws Exception {

    System.out.println(res.getClass());
    System.out.println(res.getURL().getContent());
    System.out.println("");
}

From source file:org.paxml.core.ResourceLocator.java

/**
 * Find all resources with a spring path pattern, from the added resources.
 * //from   w  w  w. j  a va 2 s . c  o  m
 * @param springPattern
 *            the spring path pattern.
 * @param baseFile
 *            the file used to resolve relative path with, can be null if
 *            the pattern is not relative.
 * @return all resource found, never null.
 */
public static Set<PaxmlResource> findResources(String springPattern, Resource baseFile) {

    springPattern = springPattern.trim();

    if (StringUtils.isBlank(springPattern)) {
        throw new PaxmlRuntimeException("Cannot have empty file pattern!");
    }

    Set<PaxmlResource> set = new LinkedHashSet<PaxmlResource>();
    if (!springPattern.startsWith("file:") && !springPattern.startsWith("classpath:")
            && !springPattern.startsWith("classpath*:")) {
        springPattern = getRelativeResource(baseFile, springPattern);
    }
    if (log.isInfoEnabled()) {
        log.info("Searching paxml resource with pattern: " + springPattern);
    }
    try {
        for (Resource res : new PathMatchingResourcePatternResolver().getResources(springPattern)) {

            if (res instanceof ClassPathResource) {
                set.add(new ClasspathResource((ClassPathResource) res));
            } else if (res instanceof UrlResource && res.getURI().toString().startsWith("jar:")) {

                set.add(new ClasspathResource(
                        new ClassPathResource(StringUtils.substringAfterLast(res.getURI().toString(), "!"))));

            } else {
                try {
                    File file = res.getFile();
                    if (file.isFile()) {
                        set.add(new FileSystemResource(file));
                    }
                } catch (IOException e) {
                    throw new PaxmlRuntimeException("Unsupported spring resource: " + res.getURI()
                            + ", of type: " + res.getClass().getName());
                }
            }
        }
    } catch (IOException e) {
        throw new PaxmlRuntimeException("Cannot find resources with spring pattern: " + springPattern, e);
    }
    return set;
}

From source file:org.powertac.server.ServerPropertiesService.java

private boolean validXmlResource(Resource xml) {
    log.debug("resource class: " + xml.getClass().getName());
    try {//w w w .  ja v  a 2  s  .  co  m
        String path = xml.getURI().toString();
        for (String regex : excludedPaths) {
            if (path.matches(regex)) {
                log.debug("invalid path " + path);
                return false;
            }
        }
        return true;
    } catch (IOException e) {
        log.error("Should not happen: " + e.toString());
        return false;
    }
}

From source file:spring.osgi.io.OsgiBundleResourceTest.java

/**
 * Test method for//from  w ww . j  av  a 2  s . com
 * {@link spring.osgi.io.OsgiBundleResource#createRelative(java.lang.String)}.
 */
@Test
public void testCreateRelativeString() {
    String location = "foo";
    Resource res = resource.createRelative(location);
    assertSame(OsgiBundleResource.class, res.getClass());
    assertEquals("spring/osgi/io/" + location, ((OsgiBundleResource) res).getPath());
}

From source file:com.flipkart.aesop.runtime.spring.RuntimeComponentContainer.java

/**
 * Interface method implementation. Loads/Reloads runtime(s) defined in the specified {@link FileSystemResource} 
 * @see org.trpr.platform.runtime.spi.component.ComponentContainer#loadComponent(org.springframework.core.io.Resource)
 *///  w  ww  .ja  va 2s. c om
public void loadComponent(Resource resource) {
    if (!FileSystemResource.class.isAssignableFrom(resource.getClass())
            || !resource.getFilename().equalsIgnoreCase(this.getRuntimeConfigFileName())) {
        throw new UnsupportedOperationException("Runtimes can be loaded only from files by name : "
                + this.getRuntimeConfigFileName() + ". Specified resource is : " + resource.toString());
    }
    loadRuntimeContext(new ServerContainerConfigInfo(((FileSystemResource) resource).getFile()));
}

From source file:com.flipkart.phantom.runtime.impl.spring.ServiceProxyComponentContainer.java

/**
 * Interface method implementation. Loads/Reloads proxy handler(s) defined in the specified {@link FileSystemResource}
 * @see org.trpr.platform.runtime.spi.component.ComponentContainer#loadComponent(org.springframework.core.io.Resource)
 *//* w w w .  j  a  v a  2s  . c o m*/
public void loadComponent(Resource resource) {
    if (!FileSystemResource.class.isAssignableFrom(resource.getClass()) || !resource.getFilename()
            .equalsIgnoreCase(ServiceProxyFrameworkConstants.SPRING_PROXY_HANDLER_CONFIG)) {
        throw new UnsupportedOperationException("Proxy handers can be loaded only from files by name : "
                + ServiceProxyFrameworkConstants.SPRING_PROXY_HANDLER_CONFIG + ". Specified resource is : "
                + resource.toString());
    }
    loadProxyHandlerContext(new HandlerConfigInfo(((FileSystemResource) resource).getFile()));
}

From source file:mvctest.web.TestController.java

@RequestMapping(value = "/appCtxGetResourcesLikeSwfFlowDefinitionResourceFactory")
public void appCtxGetResourcesLikeSwfFlowDefinitionResourceFactory(HttpServletRequest request,
        HttpServletResponse response) throws Exception {

    final String pattern = getRequiredStringParameter(request, "pattern");
    final String basePath = getStringParameter(request, "basePath", null);
    final String title = "From ApplicationContext/ResourceLoader's getResources() like SWF's FlowDefinitionResourceFactory";
    StringBuilder builder = new StringBuilder();
    builder.append("<html>\n<head>\n<title>").append(title).append("</title>\n</head>\n<body>\n");
    builder.append("<h1>").append(title).append("</h1>\n");
    builder.append("base path: ").append(basePath).append("<br />\n");
    builder.append("pattern: ").append(pattern).append("<br />\n");
    builder.append("<hr />\n");

    if (this.resourceLoader instanceof ResourcePatternResolver) {
        ResourcePatternResolver resolver = (ResourcePatternResolver) this.resourceLoader;
        Resource[] resources;//from   ww  w . j av a  2  s.  c  o m
        if (basePath == null) {
            resources = resolver.getResources(pattern);
        } else {
            if (basePath.endsWith(SLASH) || pattern.startsWith(SLASH)) {
                resources = resolver.getResources(basePath + pattern);
            } else {
                resources = resolver.getResources(basePath + SLASH + pattern);
            }
        }

        for (Resource resource : resources) {
            builder.append("<h3>Resource: ").append(resource.getURL()).append("</h3>\n");
            if (resource instanceof ContextResource) {
                ContextResource contextResource = (ContextResource) resource;
                builder.append("<p>PathWithinContext: ").append(contextResource.getPathWithinContext())
                        .append("</p>\n");
            } else {
                builder.append("<p>Resource is not a ContextResource but rather a [")
                        .append(resource.getClass().getName()).append("].</p>\n");
            }
            builder.append("<p>Flow ID: ").append(getFlowId(basePath, resource)).append("</p>\n");
            builder.append("<hr />\n");
        }
    }

    builder.append("</body>\n</html>\n");
    response.getWriter().write(builder.toString());
}

From source file:org.springframework.http.codec.ResourceHttpMessageWriter.java

private static long lengthOf(Resource resource) {
    // Don't consume InputStream...
    if (InputStreamResource.class != resource.getClass()) {
        try {// ww w  .  j ava2 s . c  o  m
            return resource.contentLength();
        } catch (IOException ignored) {
        }
    }
    return -1;
}