]> git.argeo.org Git - lgpl/argeo-commons.git/blob - BundleResourceLoader.java
cda00efcdeac88f239ca33f2b933e038fe1efef9
[lgpl/argeo-commons.git] / BundleResourceLoader.java
1 package org.argeo.cms.util;
2
3 import java.io.IOException;
4 import java.io.InputStream;
5 import java.net.URL;
6
7 import org.argeo.cms.CmsException;
8 import org.eclipse.rap.rwt.service.ResourceLoader;
9 import org.osgi.framework.Bundle;
10 import org.osgi.framework.BundleContext;
11
12 /** {@link ResourceLoader} implementation wrapping an {@link Bundle}. */
13 public class BundleResourceLoader implements ResourceLoader {
14 private final BundleContext bundleContext;
15
16 public BundleResourceLoader(BundleContext bundleContext) {
17 this.bundleContext = bundleContext;
18 }
19
20 @Override
21 public InputStream getResourceAsStream(String resourceName)
22 throws IOException {
23 // TODO deal with other bundles
24 Bundle bundle = bundleContext.getBundle();
25 // String location =
26 // bundle.getLocation().substring("initial@reference:".length());
27 // if (location.startsWith("file:")) {
28 // Path path = null;
29 // try {
30 // path = Paths.get(new URI(location));
31 // } catch (URISyntaxException e) {
32 // e.printStackTrace();
33 // }
34 // if (path != null) {
35 // Path resourcePath = path.resolve(resourceName);
36 // if (Files.exists(resourcePath))
37 // return Files.newInputStream(resourcePath);
38 // }
39 // }
40 URL res = bundle.getResource(resourceName);
41 if (res == null)
42 throw new CmsException("Resource " + resourceName
43 + " not found in bundle " + bundle.getSymbolicName());
44 return res.openStream();
45 }
46
47 }