Skip to main content
summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMichael P. Masterson2021-12-09 20:24:00 +0000
committerMichael P. Masterson2021-12-13 20:50:30 +0000
commit65ae1741eda4aa71c2dd4a1e293ef902902fd2c1 (patch)
treef86151b9f481cf8e3ebb5c8c47103d922664e7cd
parentcff26143789cc2a21cd9e3aac38fe5b338573d05 (diff)
downloadorg.eclipse.osee-0.26.0.tar.gz
org.eclipse.osee-0.26.0.tar.xz
org.eclipse.osee-0.26.0.zip
feature: Adding new way to load OseeInf resources0.26.0
This will allow for downloading resources using the classloader instead of depending on the bundle. This is mostly useful for those cases where you are trying to load from OSEE-INF in a workspace instead of via a compiled product. Change-Id: I2a0a433112e701e58584a634b040bc80c41290ad
-rw-r--r--plugins/org.eclipse.osee.framework.core/src/org/eclipse/osee/framework/core/util/OseeInf.java34
1 files changed, 30 insertions, 4 deletions
diff --git a/plugins/org.eclipse.osee.framework.core/src/org/eclipse/osee/framework/core/util/OseeInf.java b/plugins/org.eclipse.osee.framework.core/src/org/eclipse/osee/framework/core/util/OseeInf.java
index 3f0eef16134..66413ea2e94 100644
--- a/plugins/org.eclipse.osee.framework.core/src/org/eclipse/osee/framework/core/util/OseeInf.java
+++ b/plugins/org.eclipse.osee.framework.core/src/org/eclipse/osee/framework/core/util/OseeInf.java
@@ -23,12 +23,38 @@ import org.eclipse.osee.framework.jdk.core.type.OseeCoreException;
*/
public class OseeInf {
+ public static final String ROOT_DIR = "OSEE-INF/";
+
public static InputStream getResourceAsStream(String path, Class<?> classFromBundle) {
- return OsgiUtil.getResourceAsStream(classFromBundle, "OSEE-INF/" + path);
+ return OsgiUtil.getResourceAsStream(classFromBundle, ROOT_DIR + path);
+ }
+
+ /**
+ * Uses the default classloader for the provided object to load the resource.
+ *
+ * @param filePath File path of resource to load. Sometimes the classloader will assume OSEE-INF is the root dir of
+ * the path and sometimes it doesn't. If the resource doesn't exist at the root then the OSEE-INF dir is prepended to
+ * the path and tried again.
+ * @param obj Object whose classloader is to be used
+ * @return The resource found at the classloader root or in OSEE-INF, in that order.
+ * @throws OseeCoreException If resource not found
+ */
+ public static InputStream getResourceAsStream(String filePath, Object obj) throws OseeCoreException {
+ ClassLoader classLoader = obj.getClass().getClassLoader();
+ InputStream resourceAsStream = classLoader.getResourceAsStream(filePath);
+ if (resourceAsStream == null) {
+ resourceAsStream = classLoader.getResourceAsStream(ROOT_DIR + filePath);
+ }
+
+ if (resourceAsStream != null) {
+ return resourceAsStream;
+ } else {
+ throw new OseeCoreException("Resource not found at %s nor %s", filePath, ROOT_DIR + filePath);
+ }
}
public static String getResourceContents(String path, Class<?> classFromBundle) {
- return OsgiUtil.getResourceAsString(classFromBundle, "OSEE-INF/" + path);
+ return OsgiUtil.getResourceAsString(classFromBundle, ROOT_DIR + path);
}
public static File getResourceAsFile(String path, Class<?> clazz) {
@@ -37,11 +63,11 @@ public class OseeInf {
String uri = new URI(url.toString().replace(" ", "%20")).getPath();
return new File(uri);
} catch (Exception ex) {
- throw new OseeCoreException(ex, "Error getting resource [%s] as file", path);
+ throw OseeCoreException.wrap(ex);
}
}
public static URL getResourceAsUrl(String path, Class<?> clazz) {
- return OsgiUtil.getResourceAsUrl(clazz, "OSEE-INF/" + path);
+ return OsgiUtil.getResourceAsUrl(clazz, ROOT_DIR + path);
}
} \ No newline at end of file

Back to the top