Improve resource loading
authorMathieu Baudier <mbaudier@argeo.org>
Mon, 26 Oct 2015 17:29:47 +0000 (17:29 +0000)
committerMathieu Baudier <mbaudier@argeo.org>
Mon, 26 Oct 2015 17:29:47 +0000 (17:29 +0000)
git-svn-id: https://svn.argeo.org/commons/trunk@8527 4cfe0d0a-d680-48aa-b62c-e0a02a3f76cc

org.argeo.cms/src/org/argeo/cms/util/BundleResourceLoader.java
org.argeo.cms/src/org/argeo/cms/util/SimpleApp.java
org.argeo.cms/src/org/argeo/cms/util/StyleSheetResourceLoader.java [new file with mode: 0644]

index 387e02ed4e83f7f5a9b5d9a442501c29021e3638..30aba1ab7ca820b3dc324c02835daaa4f8eba5f3 100644 (file)
@@ -2,7 +2,12 @@ package org.argeo.cms.util;
 
 import java.io.IOException;
 import java.io.InputStream;
+import java.net.URI;
+import java.net.URISyntaxException;
 import java.net.URL;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.nio.file.Paths;
 
 import org.argeo.cms.CmsException;
 import org.eclipse.rap.rwt.service.ResourceLoader;
@@ -22,11 +27,26 @@ public class BundleResourceLoader implements ResourceLoader {
                        throws IOException {
                // TODO deal with other bundles
                Bundle bundle = bundleContext.getBundle();
+               // String location =
+               // bundle.getLocation().substring("initial@reference:".length());
+               // if (location.startsWith("file:")) {
+               // Path path = null;
+               // try {
+               // path = Paths.get(new URI(location));
+               // } catch (URISyntaxException e) {
+               // e.printStackTrace();
+               // }
+               // if (path != null) {
+               // Path resourcePath = path.resolve(resourceName);
+               // if (Files.exists(resourcePath))
+               // return Files.newInputStream(resourcePath);
+               // }
+               // }
                URL res = bundle.getResource(resourceName);
                if (res == null)
                        throw new CmsException("Resource " + resourceName
                                        + " not found in bundle " + bundle.getSymbolicName());
-               return bundleContext.getBundle().getResource(resourceName).openStream();
+               return res.openStream();
        }
 
 }
index 9f0e32037f7c9c78c3080ba8c97325f4a1d0a1ec..222e43c45f70ffaacd2be05837e97382f7956ec2 100644 (file)
@@ -31,6 +31,7 @@ import org.eclipse.rap.rwt.application.EntryPointFactory;
 import org.eclipse.rap.rwt.application.ExceptionHandler;
 import org.eclipse.rap.rwt.client.WebClient;
 import org.eclipse.rap.rwt.service.ResourceLoader;
+import org.eclipse.swt.widgets.Composite;
 import org.osgi.framework.BundleContext;
 
 /** A basic generic app based on {@link SimpleErgonomics}. */
@@ -58,6 +59,11 @@ public class SimpleApp implements CmsConstants, ApplicationConfiguration,
 
        public void configure(Application application) {
                try {
+                       StyleSheetResourceLoader styleSheetRL = new StyleSheetResourceLoader(
+                                       bundleContext);
+                       BundleResourceLoader bundleRL = new BundleResourceLoader(
+                                       bundleContext);
+
                        application.setOperationMode(OperationMode.SWT_COMPATIBILITY);
                        // application.setOperationMode(OperationMode.JEE_COMPATIBILITY);
 
@@ -70,8 +76,7 @@ public class SimpleApp implements CmsConstants, ApplicationConfiguration,
                        application.addResource(NO_IMAGE, createResourceLoader(NO_IMAGE));
 
                        for (String resource : resources) {
-                               application.addResource(resource, new BundleResourceLoader(
-                                               bundleContext));
+                               application.addResource(resource, bundleRL);
                                if (log.isDebugEnabled())
                                        log.debug("Registered resource " + resource);
                        }
@@ -123,8 +128,7 @@ public class SimpleApp implements CmsConstants, ApplicationConfiguration,
                        for (String themeId : styleSheets.keySet()) {
                                List<String> cssLst = styleSheets.get(themeId);
                                for (String css : cssLst) {
-                                       application.addStyleSheet(themeId, css,
-                                                       new BundleResourceLoader(bundleContext));
+                                       application.addStyleSheet(themeId, css, styleSheetRL);
                                }
 
                        }
@@ -258,7 +262,14 @@ public class SimpleApp implements CmsConstants, ApplicationConfiguration,
                @Override
                public EntryPoint create() {
                        SimpleErgonomics entryPoint = new SimpleErgonomics(repository,
-                                       workspace, basePath, page, properties);
+                                       workspace, basePath, page, properties) {
+
+                               @Override
+                               protected void initUi(Composite parent) {
+                                       // TODO Auto-generated method stub
+                                       super.initUi(parent);
+                               }
+                       };
                        // entryPoint.setState("");
                        entryPoint.setHeader(header);
                        entryPoint.setHeaderHeight(headerHeight);
diff --git a/org.argeo.cms/src/org/argeo/cms/util/StyleSheetResourceLoader.java b/org.argeo.cms/src/org/argeo/cms/util/StyleSheetResourceLoader.java
new file mode 100644 (file)
index 0000000..a7e3b6e
--- /dev/null
@@ -0,0 +1,73 @@
+package org.argeo.cms.util;
+
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.net.URL;
+import java.util.LinkedHashMap;
+import java.util.Map;
+
+import org.apache.commons.io.IOUtils;
+import org.argeo.cms.CmsException;
+import org.eclipse.rap.rwt.service.ResourceLoader;
+import org.osgi.framework.Bundle;
+import org.osgi.framework.BundleContext;
+
+/** {@link ResourceLoader} caching stylesheets. */
+public class StyleSheetResourceLoader implements ResourceLoader {
+       private final BundleContext bundleContext;
+
+       private Map<String, StyleSheet> stylesheets = new LinkedHashMap<String, StyleSheet>();
+
+       public StyleSheetResourceLoader(BundleContext bundleContext) {
+               this.bundleContext = bundleContext;
+       }
+
+       @Override
+       public InputStream getResourceAsStream(String resourceName)
+                       throws IOException {
+               if (!stylesheets.containsKey(resourceName)) {
+                       // TODO deal with other bundles
+                       Bundle bundle = bundleContext.getBundle();
+                       // String location =
+                       // bundle.getLocation().substring("initial@reference:".length());
+                       // if (location.startsWith("file:")) {
+                       // Path path = null;
+                       // try {
+                       // path = Paths.get(new URI(location));
+                       // } catch (URISyntaxException e) {
+                       // e.printStackTrace();
+                       // }
+                       // if (path != null) {
+                       // Path resourcePath = path.resolve(resourceName);
+                       // if (Files.exists(resourcePath))
+                       // return Files.newInputStream(resourcePath);
+                       // }
+                       // }
+                       URL res = bundle.getResource(resourceName);
+                       if (res == null)
+                               throw new CmsException("Resource " + resourceName
+                                               + " not found in bundle " + bundle.getSymbolicName());
+                       ByteArrayOutputStream out = new ByteArrayOutputStream();
+                       IOUtils.copy(res.openStream(), out);
+                       stylesheets.put(resourceName, new StyleSheet(out.toByteArray()));
+               }
+               return new ByteArrayInputStream(stylesheets.get(resourceName).getData());
+               // return res.openStream();
+       }
+
+       private class StyleSheet {
+               private byte[] data;
+
+               public StyleSheet(byte[] data) {
+                       super();
+                       this.data = data;
+               }
+
+               public byte[] getData() {
+                       return data;
+               }
+
+       }
+}