KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > calipso > reportgenerator > reportmanager > Repository


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 JavaDoc;
12
13 /**
14  * Esta clase abstracta se encarga de la persistencia genérica de los datos dentro de repositorios.
15  */

16 public abstract class Repository {
17
18   private String JavaDoc directoryName;
19   private FileSystemManager fileSystemaManager;
20   public boolean serialize=false; // Indica si se serializa en el repositorio o se guarda como xml
21

22
23   public ReportGeneratorConfiguration reportGeneratorConfiguration;
24
25   public abstract Class JavaDoc getObjectClass();
26   /** Crea el repositorio asignándole un directorio destino.
27    *
28    * @param directoryName Nombre del directorio destino
29    * @param reportGeneratorConfiguration
30    */

31   public Repository(String JavaDoc directoryName, ReportGeneratorConfiguration reportGeneratorConfiguration) {
32     this.directoryName = directoryName;
33     this.reportGeneratorConfiguration = reportGeneratorConfiguration;
34   }
35
36
37   /** Retorna el nombre del directorio destino
38    * @return directorio destino
39    */

40   public String JavaDoc getDirectoryName() {
41     return directoryName;
42   }
43
44   /** Asigna el directorio destino
45    * @param directoryName directorio destino
46    */

47   public void setDirectoryName(String JavaDoc directoryName) {
48     this.directoryName = directoryName;
49   }
50
51   /**
52    * Instancia un objeto del repositorio.
53    * @param ID Nombre del archivo
54    * @return Objeto instanciado
55    * @throws InfoException Si no se pudo objeter el objeto
56    */

57   public Object JavaDoc getFromID(String JavaDoc ID) throws InfoException {
58     try {
59       return load(ID);
60     } catch (Exception JavaDoc e) {
61       throw new InfoException(LanguageTraslator.traslate("51")+":"+ID, e);
62     }
63   };
64
65   /**
66    * Graba el objeto dentro del repositorio
67    * @param object Objeto a grabar
68    * @param name - nombre
69    * @throws InfoException - Si el Object o el name vienen vacíos
70    * @throws IOException - Si no pudo grabar el
71    */

72   protected void save(Object JavaDoc object, String JavaDoc name) throws InfoException, IOException {
73     OutputStream os;
74     ObjectOutputStream oos;
75     String JavaDoc 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 JavaDoc 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   /**
123    * Carga un objeto de un repositorio
124    * @param id Identificador de objeto
125    * @return Objeto instanciado
126    * @throws IOException Si no se pudo obtener el archivo
127    * @throws ClassNotFoundException
128    * @throws InfoException Si no se indicó el nombre
129    */

130   protected Object JavaDoc load(String JavaDoc id) throws IOException, ClassNotFoundException JavaDoc, InfoException {
131     InputStream is;
132     ObjectInputStream ois;
133     Object JavaDoc 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 JavaDoc inputSource = new InputSource JavaDoc(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 JavaDoc 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 JavaDoc saveFromSourceFiles(ReportGeneratorConfiguration reportGeneratorConfiguration, String JavaDoc id) throws InfoException;
173
174   /**
175    * Renombra un objeto dentro del repositorio
176    * @param sourceName Nombre origen
177    * @param targetName Nombre destino
178    * @return Indica si fué renombrado
179    */

180   protected boolean renameFile(String JavaDoc sourceName, String JavaDoc 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 JavaDoc e) {
187       return false;
188     }
189     return true;
190   }
191
192   /**
193    * Borra un objeto del repositorio
194    * @param fileName Nombre del objeto
195    * @return Indica si el objeto fué borrado
196    * @throws InfoException Si no se pudo borrar el archivo o no se indica el nombre
197    */

198   protected boolean deleteFile(String JavaDoc 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 JavaDoc e) {
216         throw new InfoException(LanguageTraslator.traslate("58")+":"+fileName,e);
217       }
218     }
219   }
220
221   /**
222    * Devuelve el objeto cache del repositorio
223    * @return
224    */

225   public abstract CacheRepository getCache();
226
227   /**
228    * Deveulve la configuracion del report manager
229    * @return
230    */

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 JavaDoc 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 JavaDoc 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 JavaDoc e) {
263       throw new InfoException(LanguageTraslator.traslate("253")+":"+fileName+":"+getDirectoryName(),e);
264     }
265   };
266
267 }
268
Popular Tags