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:com.runwaysdk.configuration.CommonsConfigurationReader.java

public CommonsConfigurationReader(ConfigGroupIF group, String config, CompositeConfiguration _cconfig) {
    this.cconfig = _cconfig;

    try {/*  w  ww.j ava2  s. co m*/
        String path = group.getPath() + config;
        ClassLoader loader = CommonsConfigurationReader.class.getClassLoader();
        URL clPath = loader.getResource(path);

        if (clPath == null) {
            throw new RunwayConfigurationException(
                    "The configuration resource [" + path + "] does not exist on the classpath.");
        }

        cconfig.addConfiguration(new PropertiesConfiguration(clPath));
        interpolate();
        CommonsConfigurationResolver.getInMemoryConfigurator().addInterpolateDependency(this);
    } catch (ConfigurationException e) {
        throw new RunwayConfigurationException(e);
    }
}

From source file:com.opensymphony.xwork2.util.classloader.ReloadingClassLoader.java

public ReloadingClassLoader(final ClassLoader pParent) {
    super(pParent);
    parent = pParent;//  ww w  .j a  v a  2s.  c  o m
    URL parentRoot = pParent.getResource("");
    FileManager fileManager = ActionContext.getContext().getInstance(FileManagerFactory.class).getFileManager();
    URL root = fileManager.normalizeToFileProtocol(parentRoot);
    root = ObjectUtils.defaultIfNull(root, parentRoot);
    try {
        if (root != null) {
            stores = new ResourceStore[] { new FileResourceStore(new File(root.toURI())) };
        } else {
            throw new XWorkException(
                    "Unable to start the reloadable class loader, consider setting 'struts.convention.classes.reload' to false");
        }
    } catch (URISyntaxException e) {
        throw new XWorkException(
                "Unable to start the reloadable class loader, consider setting 'struts.convention.classes.reload' to false",
                e);
    } catch (RuntimeException e) {
        // see WW-3121
        // TODO: Fix this for a reloading mechanism to be marked as stable
        if (root != null)
            LOG.error("Exception while trying to build the ResourceStore for URL [#0]", e, root.toString());
        else
            LOG.error("Exception while trying to get root resource from class loader", e);
        LOG.error("Consider setting struts.convention.classes.reload=false");
        throw e;
    }

    delegate = new ResourceStoreClassLoader(parent, stores);
}

From source file:com.openteach.diamond.container.DefaultClassLoaderDelegateHook.java

public URL preFindResource(String name, BundleClassLoader classloader, BundleData data)
        throws FileNotFoundException {
    for (ClassLoader thirdClassLoader : thirdClassLoaders) {
        if (thirdClassLoader != null) {
            try {
                return thirdClassLoader.getResource(name);
            } catch (Exception e) {
                // IGNORE
            }/*w w w.  j  a v a2s .  com*/
        }
    }
    return null;
}

From source file:com.wantez.eregparser.Parser.java

public Parser(final String xsl) throws TransformerConfigurationException {
    final TransformerFactory tFactory = TransformerFactory.newInstance();

    final ClassLoader classLoader = this.getClass().getClassLoader();
    final InputStream stream = classLoader.getResourceAsStream(xsl);
    final StreamSource streamSource = new StreamSource(stream);
    streamSource.setSystemId(classLoader.getResource(xsl).getPath());
    this.transformer = tFactory.newTransformer(streamSource);

}

From source file:com.talkdesk.geo.GeoCodeRepositoryBuilder.java

private void loadDataFileLocations() {
    FileInputStream fileInputStream;

    File file = new File("./conf/geo.properties");
    try {/*from  w  w w .  ja  v a 2s.c  o m*/
        if (file.exists()) {
            fileInputStream = new FileInputStream("./conf/geo.properties");
        } else {
            ClassLoader classLoader = getClass().getClassLoader();
            fileInputStream = new FileInputStream(classLoader.getResource("geo.properties").getFile());
        }
        Properties properties = new Properties();
        properties.load(fileInputStream);
        geocodeDataLocation = properties.getProperty("geoDataPath");
        countryDataLocation = properties.getProperty("countryDataPath");

    } catch (Exception e) {
        log.info("geo.properties not found using default property file ," + e.getMessage());
    }
}

From source file:$output.java

 private InputStream loadResourceAsStream(ClassLoader loader, boolean reload, String resourceName) throws IOException {
   if (!reload) {
      return loader.getResourceAsStream(resourceName);
   } else {/*from  ww w  .  j  a  v  a2s  .  c o m*/
      URL url = loader.getResource(resourceName);
      if (url == null) {
         return null;
      }
      URLConnection connection = url.openConnection();
      if (connection == null) {
         throw new IllegalArgumentException(resourceName + " could not be opened");
      }
      connection.setUseCaches(false);
      return connection.getInputStream();
   }
}

From source file:business.UploadTests.java

@Test(groups = "upload", dependsOnMethods = "createRequest")
public void uploadFileNoMimetype() throws IOException {
    UserAuthenticationToken requester = getRequester();
    SecurityContext securityContext = SecurityContextHolder.getContext();
    securityContext.setAuthentication(requester);

    RequestRepresentation representation = requestController.getRequestById(requester, processInstanceId);
    log.info("Status: " + representation.getStatus());
    int attachmentCount = representation.getAttachments().size();

    ClassLoader classLoader = getClass().getClassLoader();
    URL resource = classLoader.getResource("test/Utrecht_Oude_Gracht_Hamburgerbrug_(LOC).jpg");
    InputStream input = resource.openStream();
    MultipartFile file = new MockMultipartFile(resource.getFile(), input);
    //MultipartFile file = new MockMultipartFile(resource.getFile(), resource.getFile().toString(), "undefined", input);

    Integer flowTotalChunks = 1;//from   w ww  .  j  av a2  s .c o m
    Integer flowChunkNumber = 1;
    String flowIdentifier = "flow";

    representation = requestController.uploadRequestAttachment(requester, processInstanceId, resource.getFile(),
            flowTotalChunks, flowChunkNumber, flowIdentifier, file);

    assertEquals(attachmentCount + 1, representation.getAttachments().size());
    printFiles(representation.getAttachments());

    SecurityContextHolder.clearContext();
}

From source file:business.UploadTests.java

@Test(groups = "upload", dependsOnMethods = "createRequest")
public void uploadFileInvalidMimetype() throws IOException {
    UserAuthenticationToken requester = getRequester();
    SecurityContext securityContext = SecurityContextHolder.getContext();
    securityContext.setAuthentication(requester);

    RequestRepresentation representation = requestController.getRequestById(requester, processInstanceId);
    log.info("Status: " + representation.getStatus());
    int attachmentCount = representation.getAttachments().size();

    ClassLoader classLoader = getClass().getClassLoader();
    URL resource = classLoader.getResource("test/Utrecht_Oude_Gracht_Hamburgerbrug_(LOC).jpg");
    InputStream input = resource.openStream();
    MultipartFile file = new MockMultipartFile(resource.getFile(), resource.getFile().toString(), "undefined",
            input);//from  w w  w .ja  v a2  s.c o m

    Integer flowTotalChunks = 1;
    Integer flowChunkNumber = 1;
    String flowIdentifier = "flow";

    representation = requestController.uploadRequestAttachment(requester, processInstanceId, resource.getFile(),
            flowTotalChunks, flowChunkNumber, flowIdentifier, file);

    assertEquals(attachmentCount + 1, representation.getAttachments().size());
    printFiles(representation.getAttachments());

    SecurityContextHolder.clearContext();
}

From source file:business.UploadTests.java

@Test(groups = "upload", dependsOnMethods = "createRequest")
public void uploadFileSuccess() throws IOException {
    UserAuthenticationToken requester = getRequester();
    SecurityContext securityContext = SecurityContextHolder.getContext();
    securityContext.setAuthentication(requester);

    RequestRepresentation representation = requestController.getRequestById(requester, processInstanceId);
    log.info("Status: " + representation.getStatus());
    int attachmentCount = representation.getAttachments().size();

    ClassLoader classLoader = getClass().getClassLoader();
    URL resource = classLoader.getResource("test/Utrecht_Oude_Gracht_Hamburgerbrug_(LOC).jpg");
    InputStream input = resource.openStream();
    MultipartFile file = new MockMultipartFile(resource.getFile(), resource.getFile().toString(), "image/jpeg",
            input);/* w  w  w  .jav  a 2 s  .  c  om*/

    Integer flowTotalChunks = 1;
    Integer flowChunkNumber = 1;
    String flowIdentifier = "flow";

    representation = requestController.uploadRequestAttachment(requester, processInstanceId, resource.getFile(),
            flowTotalChunks, flowChunkNumber, flowIdentifier, file);

    assertEquals(attachmentCount + 1, representation.getAttachments().size());
    printFiles(representation.getAttachments());

    SecurityContextHolder.clearContext();
}

From source file:org.apache.hadoop.chukwa.hicc.HiccWebServer.java

public List<String> getResourceListing(String path) throws URISyntaxException, IOException {
    ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader();
    URL dirURL = contextClassLoader.getResource(path);

    if (dirURL == null) {
        dirURL = contextClassLoader.getResource(path);
    }//from w ww. jav a  2s  .c  om

    if (dirURL.getProtocol().equals("jar")) {
        /* A JAR path */
        String jarPath = dirURL.getPath().substring(5, dirURL.getPath().indexOf("!")); //strip out only the JAR file
        JarFile jar = new JarFile(jarPath);
        Enumeration<JarEntry> entries = jar.entries(); //gives ALL entries in jar
        List<String> result = new ArrayList<String>(); //avoid duplicates in case it is a subdirectory
        while (entries.hasMoreElements()) {
            String name = entries.nextElement().getName();
            if (name.startsWith(path)) { //filter according to the path
                String entry = name.substring(path.length());
                int checkSubdir = entry.indexOf("/");
                if (checkSubdir == 0 && entry.length() > 1) {
                    // if it is a subdirectory, we just return the directory name
                    result.add(name);
                }
            }
        }
        return result;
    }

    throw new UnsupportedOperationException("Cannot list files for URL " + dirURL);
}