Make JXL DAO support more robust
authorMathieu Baudier <mbaudier@argeo.org>
Tue, 6 Oct 2009 12:26:52 +0000 (12:26 +0000)
committerMathieu Baudier <mbaudier@argeo.org>
Tue, 6 Oct 2009 12:26:52 +0000 (12:26 +0000)
git-svn-id: https://svn.argeo.org/commons/trunk@3001 4cfe0d0a-d680-48aa-b62c-e0a02a3f76cc

server/runtime/org.argeo.server.jxl/src/main/java/org/argeo/server/jxl/dao/JxlDaoSupport.java

index 0960b6c42ccff392b17c3c325f95d51e55d94052..ef3dcf04b3de3532958278c69ee4befaf0a601d3 100644 (file)
@@ -13,17 +13,20 @@ import jxl.JXLException;
 import jxl.Sheet;
 import jxl.Workbook;
 
+import org.apache.commons.io.IOUtils;
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
 import org.argeo.server.ArgeoServerException;
 import org.springframework.beans.BeanWrapper;
 import org.springframework.beans.BeanWrapperImpl;
 import org.springframework.beans.BeansException;
+import org.springframework.beans.factory.InitializingBean;
 import org.springframework.context.ApplicationContext;
 import org.springframework.context.ApplicationContextAware;
+import org.springframework.core.io.Resource;
 import org.springframework.util.StringUtils;
 
-public class JxlDaoSupport implements ApplicationContextAware {
+public class JxlDaoSupport implements ApplicationContextAware, InitializingBean {
        private final static Log log = LogFactory.getLog(JxlDaoSupport.class);
 
        private ClassLoader classLoader = getClass().getClassLoader();
@@ -35,6 +38,21 @@ public class JxlDaoSupport implements ApplicationContextAware {
 
        private List<String> scannedPackages = new ArrayList<String>();
 
+       private List<Resource> workbooks = new ArrayList<Resource>();
+
+       public void afterPropertiesSet() throws Exception {
+               for (Resource res : workbooks) {
+                       InputStream in = null;
+                       try {
+                               in = res.getInputStream();
+                               load(in);
+                       } finally {
+                               IOUtils.closeQuietly(in);
+                       }
+               }
+
+       }
+
        public void load(InputStream in) {
                try {
                        // used to resolve inner references
@@ -184,10 +202,12 @@ public class JxlDaoSupport implements ApplicationContextAware {
 
                scannedPkgs: for (String pkg : scannedPackages) {
                        try {
-                               clss = classLoader.loadClass(pkg + "." + className);
+                               clss = classLoader.loadClass(pkg.trim() + "." + className);
                                break scannedPkgs;
                        } catch (ClassNotFoundException e) {
                                // silent
+                               if (log.isTraceEnabled())
+                                       log.trace(e.getMessage());
                        }
                }
 
@@ -243,6 +263,18 @@ public class JxlDaoSupport implements ApplicationContextAware {
                return scannedPackages;
        }
 
+       public void setWorkbooks(List<Resource> workbooks) {
+               this.workbooks = workbooks;
+       }
+
+       public List<Resource> getWorkbooks() {
+               return workbooks;
+       }
+
+       public void setClassLoader(ClassLoader classLoader) {
+               this.classLoader = classLoader;
+       }
+
        public static class Reference {
                private Object object;
                private String property;