Author: ate
Date: Tue Aug 21 11:35:50 2012
New Revision: 1375477

URL: http://svn.apache.org/viewvc?rev=1375477&view=rev
Log:
RAVE-695 Create new rave-jcr-ocm module providing JCR based Object Content 
Mapping (OCM) support
- fix config loading from jar (classpath) resource

Modified:
    
rave/sandbox/content-services/rave-jcr-integration/page-configuration/src/main/java/org/apache/rave/jcr/config/ConfigUtils.java
    
rave/sandbox/content-services/rave-jcr-integration/page-configuration/src/main/java/org/apache/rave/jcr/config/FileConfigManager.java

Modified: 
rave/sandbox/content-services/rave-jcr-integration/page-configuration/src/main/java/org/apache/rave/jcr/config/ConfigUtils.java
URL: 
http://svn.apache.org/viewvc/rave/sandbox/content-services/rave-jcr-integration/page-configuration/src/main/java/org/apache/rave/jcr/config/ConfigUtils.java?rev=1375477&r1=1375476&r2=1375477&view=diff
==============================================================================
--- 
rave/sandbox/content-services/rave-jcr-integration/page-configuration/src/main/java/org/apache/rave/jcr/config/ConfigUtils.java
 (original)
+++ 
rave/sandbox/content-services/rave-jcr-integration/page-configuration/src/main/java/org/apache/rave/jcr/config/ConfigUtils.java
 Tue Aug 21 11:35:50 2012
@@ -20,6 +20,9 @@
 package org.apache.rave.jcr.config;
 
 import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileNotFoundException;
+import java.io.InputStream;
 import java.io.UnsupportedEncodingException;
 import java.net.URL;
 import java.net.URLDecoder;
@@ -80,4 +83,35 @@ public final class ConfigUtils {
         }
     }
 
+    public static InputStream getResourceInputStream(String resourcePath) {
+        if (resourcePath == null) {
+            throw new IllegalArgumentException("resourcePath parameter cannot 
be <null>");
+        }
+        if (resourcePath.startsWith(FILE_PATH_PREFIX)) {
+            String fileName = 
resourcePath.substring(FILE_PATH_PREFIX.length());
+            try {
+                return new FileInputStream(new File(fileName));
+            } catch (FileNotFoundException e) {
+                log.error("Cannot load resource file from '{}'", resourcePath, 
e);
+                return null;
+            }
+        } else if (resourcePath.startsWith(CLASSPATH_PREFIX)) {
+            String classPath = 
resourcePath.substring(CLASSPATH_PREFIX.length());
+            URL url = 
FileConfigManager.class.getClassLoader().getResource(classPath);
+            if (url == null) {
+                return null;
+            }
+            return 
FileConfigManager.class.getClassLoader().getResourceAsStream(classPath);
+        } else {
+            // fallback
+            log.info("Neither file or classpath prefix used, trying to get 
resource directly from '{}'", resourcePath);
+            try {
+                return new FileInputStream(new File(resourcePath));
+            } catch (FileNotFoundException e) {
+                log.error("Cannot load resource file from '{}'", resourcePath, 
e);
+                return null;
+            }
+        }
+    }
+
 }

Modified: 
rave/sandbox/content-services/rave-jcr-integration/page-configuration/src/main/java/org/apache/rave/jcr/config/FileConfigManager.java
URL: 
http://svn.apache.org/viewvc/rave/sandbox/content-services/rave-jcr-integration/page-configuration/src/main/java/org/apache/rave/jcr/config/FileConfigManager.java?rev=1375477&r1=1375476&r2=1375477&view=diff
==============================================================================
--- 
rave/sandbox/content-services/rave-jcr-integration/page-configuration/src/main/java/org/apache/rave/jcr/config/FileConfigManager.java
 (original)
+++ 
rave/sandbox/content-services/rave-jcr-integration/page-configuration/src/main/java/org/apache/rave/jcr/config/FileConfigManager.java
 Tue Aug 21 11:35:50 2012
@@ -19,9 +19,7 @@
 
 package org.apache.rave.jcr.config;
 
-import java.io.File;
-import java.io.FileInputStream;
-import java.io.FileNotFoundException;
+import java.io.InputStream;
 import java.io.StringWriter;
 
 import javax.xml.bind.JAXBContext;
@@ -52,15 +50,14 @@ public class FileConfigManager implement
             throw new InvalidConfigurationException("Provided 
PageConfiguration was null or configuration path was null or empty: " + path);
         }
         log.info("Loading configuration: {}", path);
-        FileInputStream is = null;
+        InputStream is = null;
         try {
-            File file = ConfigUtils.getResourceFile(path);
-            if (file == null) {
+            is = ConfigUtils.getResourceInputStream(path);
+            if (is == null) {
                 throw new InvalidConfigurationException("Configuration file 
not found for path: " + path);
             }
             JAXBContext context = JAXBContext.newInstance(UrlConfig.class);
             Unmarshaller unmarshaller = context.createUnmarshaller();
-            is = new FileInputStream(file);
             final UrlConfig config = (UrlConfig) unmarshaller.unmarshal(is);
             if (config != null) {
                 config.initialize(configuration);
@@ -69,8 +66,6 @@ public class FileConfigManager implement
             return config;
         } catch (JAXBException e) {
             throw new InvalidConfigurationException("Error parsing 
configuration file: " + path, e);
-        } catch (FileNotFoundException e) {
-            throw new InvalidConfigurationException("Configuration file not 
found for path: " + path, e);
         } finally {
             IOUtils.closeQuietly(is);
         }
@@ -80,15 +75,14 @@ public class FileConfigManager implement
     public PageConfiguration loadConfiguration(String path) {
         log.info("Loading configuration: {}", path);
 
-        FileInputStream is = null;
+        InputStream is = null;
         try {
-            File file = ConfigUtils.getResourceFile(path);
-            if (file == null) {
+            is = ConfigUtils.getResourceInputStream(path);
+            if (is == null) {
                 throw new InvalidConfigurationException("Configuration file 
not found for path: " + path);
             }
             JAXBContext context = 
JAXBContext.newInstance(FilePageConfiguration.class);
             Unmarshaller unmarshaller = context.createUnmarshaller();
-            is = new FileInputStream(file);
             final PageConfiguration filePageConfiguration = 
(PageConfiguration) unmarshaller.unmarshal(is);
             if (filePageConfiguration != null) {
                 filePageConfiguration.initialize();
@@ -97,8 +91,6 @@ public class FileConfigManager implement
             return filePageConfiguration;
         } catch (JAXBException e) {
             throw new InvalidConfigurationException("Error parsing 
configuration file: " + path, e);
-        } catch (FileNotFoundException e) {
-            throw new InvalidConfigurationException("Configuration file not 
found for path: " + path, e);
         } finally {
             IOUtils.closeQuietly(is);
         }


Reply via email to