1 package com.calipso.reportgenerator.reportmanager; 2 3 import com.calipso.reportgenerator.common.InfoException; 4 import java.io.*; 5 import com.calipso.reportgenerator.common.ReportGeneratorConfiguration; 6 import com.calipso.reportgenerator.common.LanguageTraslator; 7 import com.calipso.reportgenerator.services.FileSystemResolver; 8 import org.exolab.castor.xml.*; 9 import org.apache.commons.vfs.FileSystemManager; 10 import org.apache.commons.vfs.FileObject; 11 import org.xml.sax.InputSource ; 12 13 16 public abstract class Repository { 17 18 private String directoryName; 19 private FileSystemManager fileSystemaManager; 20 public boolean serialize=false; 22 23 public ReportGeneratorConfiguration reportGeneratorConfiguration; 24 25 public abstract Class getObjectClass(); 26 31 public Repository(String directoryName, ReportGeneratorConfiguration reportGeneratorConfiguration) { 32 this.directoryName = directoryName; 33 this.reportGeneratorConfiguration = reportGeneratorConfiguration; 34 } 35 36 37 40 public String getDirectoryName() { 41 return directoryName; 42 } 43 44 47 public void setDirectoryName(String directoryName) { 48 this.directoryName = directoryName; 49 } 50 51 57 public Object getFromID(String ID) throws InfoException { 58 try { 59 return load(ID); 60 } catch (Exception e) { 61 throw new InfoException(LanguageTraslator.traslate("51")+":"+ID, e); 62 } 63 }; 64 65 72 protected void save(Object object, String name) throws InfoException, IOException { 73 OutputStream os; 74 ObjectOutputStream oos; 75 String fileName; 76 fileName = directoryName + "/" + name.toUpperCase(); 77 78 if ((object == null) || (name == "")) { 79 throw new InfoException(LanguageTraslator.traslate("52")+":"+name); 80 } 81 else { 82 try { 83 try { 84 if (serialize){ 85 os = getFileSystemManager().resolveFile(fileName).getContent().getOutputStream(); 86 oos = new ObjectOutputStream(os); 87 oos.writeObject(object); 88 os = null; 89 oos.flush(); 90 oos.close(); 91 oos.reset(); 92 oos = null; 93 resetFileSystemManager(); 94 }else{ 95 try { 96 Writer writer = new OutputStreamWriter(getFileSystemManager().resolveFile(fileName).getContent().getOutputStream()); 97 Marshaller marshaller = new Marshaller(writer); 98 marshaller.setEncoding("ISO-8859-1"); 99 marshaller.marshal(object); 100 writer.flush(); 101 writer.close(); 102 resetFileSystemManager(); 103 }catch (Exception e){ 104 throw new InfoException(LanguageTraslator.traslate("195")+":"+fileName,e); 105 } 106 } 107 getCache().addObject(name, object); 108 } catch (IOException e) { 109 throw new IOException(LanguageTraslator.traslate("53")+":"+name+". "+e.toString()); 110 } 111 } catch (FileNotFoundException e) { 112 throw new FileNotFoundException(LanguageTraslator.traslate("54")+":"+name); 113 } 114 } 115 } 116 117 private void resetFileSystemManager() { 118 setFileSystemaManager(null); 119 FileSystemResolver.resetFileSystemManager(); 120 } 121 122 130 protected Object load(String id) throws IOException, ClassNotFoundException , InfoException { 131 InputStream is; 132 ObjectInputStream ois; 133 Object returnObject = null; 134 135 if (id.equalsIgnoreCase("")) { 136 throw new InfoException(LanguageTraslator.traslate("55")); 137 } 138 else { 139 if (getCache().containsObject(id)) { 140 return getCache().getObject(id); 141 } 142 else { 143 try { 144 if (serialize) { 145 is = getFileSystemManager().resolveFile(getDirectoryName() + "/" + id).getContent().getInputStream(); 146 ois = new ObjectInputStream(is); 147 returnObject = ois.readObject(); 148 }else { 149 InputStream inputStream = getFileSystemManager().resolveFile(getDirectoryName() + "/" + id).getContent().getInputStream(); 150 InputSource inputSource = new InputSource (inputStream); 151 try { 152 returnObject = Unmarshaller.unmarshal(getObjectClass(), inputSource); 153 } catch (XMLException e) { 154 throw new InfoException(LanguageTraslator.traslate("194")+":"+getDirectoryName() + "/" + id,e); 155 } 156 } 157 } catch (FileNotFoundException e) { 158 try { 159 if(returnObject==null){ 160 returnObject = saveFromSourceFiles(getReportGeneratorConfiguration(), id); 161 } 162 }catch(Exception ee){ 163 throw new InfoException(LanguageTraslator.traslate("56")+":"+id,ee); 164 } 165 } 166 getCache().addObject(id, returnObject); 167 return returnObject; 168 } 169 } 170 } 171 172 protected abstract Object saveFromSourceFiles(ReportGeneratorConfiguration reportGeneratorConfiguration, String id) throws InfoException; 173 174 180 protected boolean renameFile(String sourceName, String targetName) { 181 File sourceFile, targetFile = null; 182 try { 183 sourceFile = new File(getDirectoryName() + "/" + sourceName); 184 targetFile = new File(getDirectoryName() + "/" + targetName + ".tmp"); 185 sourceFile.renameTo(targetFile); 186 } catch (Exception e) { 187 return false; 188 } 189 return true; 190 } 191 192 198 protected boolean deleteFile(String fileName) throws InfoException { 199 FileObject fileObject; 200 if (fileName.equalsIgnoreCase("")) { 201 throw new InfoException(LanguageTraslator.traslate("57")); 202 } 203 else { 204 try { 205 fileObject = getFileSystemManager().resolveFile(new File(getDirectoryName()), fileName); 206 if (getCache().containsObject(fileName)){ 207 getCache().removeObject(fileName); 208 } 209 if (fileObject.exists()){ 210 fileObject.close(); 211 fileObject.delete(); 212 }; 213 resetFileSystemManager(); 214 return true; 215 } catch (Exception e) { 216 throw new InfoException(LanguageTraslator.traslate("58")+":"+fileName,e); 217 } 218 } 219 } 220 221 225 public abstract CacheRepository getCache(); 226 227 231 public ReportGeneratorConfiguration getReportGeneratorConfiguration() { 232 return reportGeneratorConfiguration; 233 } 234 235 public FileSystemManager getFileSystemManager() throws InfoException { 236 if (fileSystemaManager == null ) { 237 try { 238 fileSystemaManager = FileSystemResolver.getFileSystemManager(getReportGeneratorConfiguration()); 239 }catch(Exception e){ 240 throw new InfoException(LanguageTraslator.traslate("211"),e); 241 } 242 } 243 return fileSystemaManager; 244 } 245 246 public void setFileSystemaManager(FileSystemManager fileSystemaManager) { 247 this.fileSystemaManager = fileSystemaManager; 248 } 249 250 public void deleteAllFiles() throws InfoException { 251 FileObject fileObject; 252 String fileName=""; 253 try { 254 fileObject = getFileSystemManager().resolveFile(getDirectoryName()); 255 FileObject[] fileObjects = fileObject.getChildren(); 256 for (int i = 0; i < fileObjects.length; i++) { 257 fileName = fileObjects[i].getName().getBaseName(); 258 deleteFile(fileName); 259 } 260 resetFileSystemManager(); 261 } 262 catch (Exception e) { 263 throw new InfoException(LanguageTraslator.traslate("253")+":"+fileName+":"+getDirectoryName(),e); 264 } 265 }; 266 267 } 268 | Popular Tags |