KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openi > xml > BeanStorage


1 /*********************************************************************************
2  * The contents of this file are subject to the OpenI Public License Version 1.0
3  * ("License"); You may not use this file except in compliance with the
4  * License. You may obtain a copy of the License at
5  * http://www.openi.org/docs/LICENSE.txt
6  *
7  * Software distributed under the License is distributed on an "AS IS" basis,
8  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
9  * the specific language governing rights and limitations under the License.
10  *
11  * The Original Code is: OpenI Open Source
12  *
13  * The Initial Developer of the Original Code is Loyalty Matrix, Inc.
14  * Portions created by Loyalty Matrix, Inc. are
15  * Copyright (C) 2005 Loyalty Matrix, Inc.; All Rights Reserved.
16  *
17  * Contributor(s): ______________________________________.
18  *
19  ********************************************************************************/

20 package org.openi.xml;
21
22 import com.thoughtworks.xstream.XStream;
23 import com.thoughtworks.xstream.converters.ConversionException;
24 import org.apache.log4j.LogManager;
25 import org.apache.log4j.Logger;
26 import java.io.File JavaDoc;
27 import java.io.FileNotFoundException JavaDoc;
28 import java.io.FileReader JavaDoc;
29 import java.io.FileWriter JavaDoc;
30 import java.io.IOException JavaDoc;
31 import java.io.Writer JavaDoc;
32
33
34 /**
35  * @author plucas
36  * @version $Revision: 1.7 $ $Date: 2006/04/12 00:39:12 $
37  *
38  * Utility methods for bean persistence.
39  */

40 public class BeanStorage {
41     private static Logger logger = LogManager.getLogger(BeanStorage.class);
42     private XStream dataBinder;
43
44     public BeanStorage() {
45         this.dataBinder = new XStream();
46     }
47
48     public String JavaDoc toXmlString(Object JavaDoc bean) {
49         return this.dataBinder.toXML(bean);
50     }
51
52     // TODO: synchronize! but for what/whom? application? this? bean?
53
public synchronized void saveBeanToFile(String JavaDoc filename, Object JavaDoc bean)
54         throws IOException JavaDoc {
55         // if doesn't exist, might be a parent directory issue, try and create
56
logger.debug("trying to save bean: to file: " + filename);
57
58         File JavaDoc file = new File JavaDoc(filename);
59
60         if (!file.exists()) {
61             File JavaDoc parentDir = file.getParentFile();
62
63             if (!parentDir.exists()) {
64                 parentDir.mkdirs();
65             }
66         }
67
68         FileWriter JavaDoc writer = new FileWriter JavaDoc(file);
69         saveBean(writer, bean);
70         writer.flush();
71         writer.close();
72     }
73
74     // TODO: synchronize! but for what/whom? application? this? bean?
75
public synchronized void saveBean(Writer JavaDoc writer, Object JavaDoc bean)
76         throws IOException JavaDoc {
77         logger.debug("saveBean: " + bean.getClass().getName() + " to writer");
78         this.dataBinder.toXML(bean, writer);
79     }
80
81     /**
82      *
83      * @param filename
84      * @return
85      * @throws IOException for a poorly formatted file, missing file
86      */

87     public Object JavaDoc restoreBeanFromFile(String JavaDoc filename)
88         throws IOException JavaDoc {
89         logger.debug("restoring bean from file: " + filename);
90
91         File JavaDoc analysisConfigFile = new File JavaDoc(filename);
92
93         if (!analysisConfigFile.exists()) {
94             throw new FileNotFoundException JavaDoc("could not find at: " + filename);
95         }
96
97         FileReader JavaDoc reader = new FileReader JavaDoc(analysisConfigFile);
98         Object JavaDoc obj = null;
99
100         try {
101             obj = this.dataBinder.fromXML(reader);
102             reader.close();
103         } catch (ConversionException e) {
104             // logger.error(e);
105
// TODO: this seems like a bad idea. choices:
106
// 1. custom exception for this component (BeanStorageException) - con - still need to wrap exception, and need to change all current callers
107
// 2. add ConversionException to throws - does not properly isolate caller from persistence mechanism
108
// leaving it for now, need to refactor during QA period
109
throw new IOException JavaDoc(
110                 "Trouble restoring bean, caught ConversionException: "
111                 + e.getMessage());
112         }
113
114         return obj;
115     }
116
117     /**
118      *
119      * Populating the fields of the given object instead of instantiating a new one.
120      * @param filename
121      * @param analysis
122      * @return
123      * @throws IOException for a poorly formatted file, missing file
124      */

125     public Object JavaDoc restoreBeanFromFile(String JavaDoc filename, Object JavaDoc root)
126         throws IOException JavaDoc {
127         logger.debug("restoring bean from file: " + filename);
128
129         File JavaDoc analysisConfigFile = new File JavaDoc(filename);
130
131         if (!analysisConfigFile.exists()) {
132             throw new FileNotFoundException JavaDoc("could not find at: " + filename);
133         }
134
135         FileReader JavaDoc reader = new FileReader JavaDoc(analysisConfigFile);
136         Object JavaDoc obj = null;
137
138         try {
139             obj = this.dataBinder.fromXML(reader, root);
140             reader.close();
141         } catch (ConversionException e) {
142             // logger.error(e);
143
// TODO: this seems like a bad idea. choices:
144
// 1. custom exception for this component (BeanStorageException) - con - still need to wrap exception, and need to change all current callers
145
// 2. add ConversionException to throws - does not properly isolate caller from persistence mechanism
146
// leaving it for now, need to refactor during QA period
147
throw new IOException JavaDoc(
148                 "Trouble restoring bean, caught ConversionException: "
149                 + e.getMessage());
150         }
151
152         return obj;
153     }
154
155     // TODO: synchronize! but for what/whom? application? this? bean?
156
public synchronized void deleteFile(String JavaDoc filename) {
157         logger.info("trying to delete file: " + filename);
158
159         File JavaDoc file = new File JavaDoc(filename);
160
161         if (file.exists()) {
162             logger.debug("delete result: " + file.delete());
163         }
164     }
165 }
166
Popular Tags