org.sakaiproject.genericdao.springutil.ResourceFinder Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of generic-dao Show documentation
Show all versions of generic-dao Show documentation
Generic Dao is a Java package which allows a developer to skip writing
DAOs for their persistence objects when they are using Spring and/or Hibernate.
The package was originally created by Aaron Zeckoski for the Evaluation System
project but was repackaged to make it distributable by request. It is used in the
RSF framework (http://www2.caret.cam.ac.uk/rsfwiki/). Note about the BeanUtils
provided dependency: BeanUtils is not required if you are not using it in your
project. Note about the Hibernate provided dependency: Hibernate is not required
if you are not using it in your project.
The newest version!
/**
* $Id$
* $URL$
* ResourceFinder.java - blog-wow - May 6, 2008 9:59:05 PM - azeckoski
**************************************************************************
* Copyright (c) 2008 Aaron Zeckoski
* Licensed under the Apache License, Version 2
*
* A copy of the Apache License, Version 2 has been included in this
* distribution and is available at: http://www.apache.org/licenses/LICENSE-2.0.txt
*
* Aaron Zeckoski ([email protected]) ([email protected]) ([email protected])
*/
package org.sakaiproject.genericdao.springutil;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
/**
* Allows for easy resolution of resources in the current {@link ClassLoader},
* (which may not be the {@link ClassLoader} being used by Spring)
* Primarily useful when you need to locate resources while running in a Sakai component
* (like HBM files or SQL files)
* In Sakai, this allows us to find resources in our pack since the Sakai context {@link ClassLoader} is wrong,
* too bad it is not correct, that would be cool, but it is wrong and it is not cool
* Takes a list of paths to resources and turns them into legitimate resources
*
org/sakaiproject/blogwow/dao/hbm/BlogWowBlog.hbm.xml
org/sakaiproject/blogwow/dao/hbm/BlogWowEntry.hbm.xml
org/sakaiproject/blogwow/dao/hbm/BlogWowComment.hbm.xml
*
* @author Aaron Zeckoski ([email protected])
*/
public class ResourceFinder {
private static List makeResources(List paths) {
List rs = new ArrayList();
if (paths != null && !paths.isEmpty()) {
ClassLoader cl = ResourceFinder.class.getClassLoader();
for (String path : paths) {
Resource r = new ClassPathResource(path, cl);
if (r.exists()) {
rs.add(r);
}
}
}
return rs;
}
/**
* Resolves a list of paths into resources within the current classloader
* @param paths a list of paths to resources (org/sakaiproject/mystuff/Thing.xml)
* @return an array of Spring Resource objects
*/
public static Resource[] getResources(List paths) {
List l = makeResources(paths);
return l.toArray(new Resource[l.size()]);
}
/**
* Resolves a list of paths into resources within the current classloader
* @param paths a list of paths to resources (org/sakaiproject/mystuff/Thing.xml)
* @return an array of File objects
*/
public static File[] getFiles(List paths) {
List rs = makeResources(paths);
File[] files = new File[rs.size()];
for (int i = 0; i < rs.size(); i++) {
Resource r = rs.get(i);
try {
files[i] = r.getFile();
} catch (IOException e) {
throw new RuntimeException("Failed to get file for: " + r.getFilename(), e);
}
}
return files;
}
/**
* Resolves a list of paths into resources within the current classloader
* @param paths a list of paths to resources (org/sakaiproject/mystuff/Thing.xml)
* @return an array of InputStreams
*/
public static InputStream[] getInputStreams(List paths) {
List rs = makeResources(paths);
InputStream[] streams = new InputStream[rs.size()];
for (int i = 0; i < rs.size(); i++) {
Resource r = rs.get(i);
try {
streams[i] = r.getInputStream();
} catch (IOException e) {
throw new RuntimeException("Failed to get inputstream for: " + r.getFilename(), e);
}
}
return streams;
}
}