001: /*********************************************************************************
002: * The contents of this file are subject to the OpenI Public License Version 1.0
003: * ("License"); You may not use this file except in compliance with the
004: * License. You may obtain a copy of the License at
005: * http://www.openi.org/docs/LICENSE.txt
006: *
007: * Software distributed under the License is distributed on an "AS IS" basis,
008: * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
009: * the specific language governing rights and limitations under the License.
010: *
011: * The Original Code is: OpenI Open Source
012: *
013: * The Initial Developer of the Original Code is Loyalty Matrix, Inc.
014: * Portions created by Loyalty Matrix, Inc. are
015: * Copyright (C) 2005 Loyalty Matrix, Inc.; All Rights Reserved.
016: *
017: * Contributor(s): ______________________________________.
018: *
019: ********************************************************************************/package org.openi.xml;
020:
021: import com.thoughtworks.xstream.XStream;
022: import com.thoughtworks.xstream.converters.ConversionException;
023: import org.apache.log4j.LogManager;
024: import org.apache.log4j.Logger;
025: import java.io.File;
026: import java.io.FileNotFoundException;
027: import java.io.FileReader;
028: import java.io.FileWriter;
029: import java.io.IOException;
030: import java.io.Writer;
031:
032: /**
033: * @author plucas
034: * @version $Revision: 1.8 $ $Date: 2006/05/16 00:54:02 $
035: *
036: * Utility methods for bean persistence.
037: */
038: public class BeanStorage {
039: private static Logger logger = LogManager
040: .getLogger(BeanStorage.class);
041: private XStream dataBinder;
042:
043: public BeanStorage() {
044: this .dataBinder = new XStream();
045: }
046:
047: public String toXmlString(Object bean) {
048: return this .dataBinder.toXML(bean);
049: }
050:
051: // TODO: synchronize! but for what/whom? application? this? bean?
052: public synchronized void saveBeanToFile(String filename, Object bean)
053: throws IOException {
054: // if doesn't exist, might be a parent directory issue, try and create
055: logger.debug("trying to save bean: to file: " + filename);
056:
057: File file = new File(filename);
058:
059: if (!file.exists()) {
060: File parentDir = file.getParentFile();
061:
062: if (!parentDir.exists()) {
063: parentDir.mkdirs();
064: }
065: }
066:
067: FileWriter writer = new FileWriter(file);
068: saveBean(writer, bean);
069: writer.flush();
070: writer.close();
071: }
072:
073: public synchronized void saveBeanToTmp(Class clazz,
074: String filename, Object bean) throws IOException {
075: String file = rename(clazz) + "/" + filename;
076: saveBeanToFile(file, bean);
077: }
078:
079: public String rename(Class clazz) {
080: String name = this .getClass().getName();
081: return name.replaceAll("\\.", "/");
082: }
083:
084: // TODO: synchronize! but for what/whom? application? this? bean?
085: public synchronized void saveBean(Writer writer, Object bean)
086: throws IOException {
087: logger.debug("saveBean: " + bean.getClass().getName()
088: + " to writer");
089: this .dataBinder.toXML(bean, writer);
090: }
091:
092: /**
093: *
094: * @param filename
095: * @return
096: * @throws IOException for a poorly formatted file, missing file
097: */
098: public Object restoreBeanFromFile(String filename)
099: throws IOException {
100: logger.debug("restoring bean from file: " + filename);
101:
102: File analysisConfigFile = new File(filename);
103:
104: if (!analysisConfigFile.exists()) {
105: throw new FileNotFoundException("could not find at: "
106: + filename);
107: }
108:
109: FileReader reader = new FileReader(analysisConfigFile);
110: Object obj = null;
111:
112: try {
113: obj = this .dataBinder.fromXML(reader);
114: reader.close();
115: } catch (ConversionException e) {
116: // logger.error(e);
117: // TODO: this seems like a bad idea. choices:
118: // 1. custom exception for this component (BeanStorageException) - con - still need to wrap exception, and need to change all current callers
119: // 2. add ConversionException to throws - does not properly isolate caller from persistence mechanism
120: // leaving it for now, need to refactor during QA period
121: throw new IOException(
122: "Trouble restoring bean, caught ConversionException: "
123: + e.getMessage());
124: }
125:
126: return obj;
127: }
128:
129: public Object restoreBeanFromTmpFile(Class clazz, String file)
130: throws IOException {
131: return this
132: .restoreBeanFromFile(this .rename(clazz) + "/" + file);
133: }
134:
135: /**
136: *
137: * Populating the fields of the given object instead of instantiating a new one.
138: * @param filename
139: * @param analysis
140: * @return
141: * @throws IOException for a poorly formatted file, missing file
142: */
143: public Object restoreBeanFromFile(String filename, Object root)
144: throws IOException {
145: logger.debug("restoring bean from file: " + filename);
146:
147: File analysisConfigFile = new File(filename);
148:
149: if (!analysisConfigFile.exists()) {
150: throw new FileNotFoundException("could not find at: "
151: + filename);
152: }
153:
154: FileReader reader = new FileReader(analysisConfigFile);
155: Object obj = null;
156:
157: try {
158: obj = this .dataBinder.fromXML(reader, root);
159: reader.close();
160: } catch (ConversionException e) {
161: // logger.error(e);
162: // TODO: this seems like a bad idea. choices:
163: // 1. custom exception for this component (BeanStorageException) - con - still need to wrap exception, and need to change all current callers
164: // 2. add ConversionException to throws - does not properly isolate caller from persistence mechanism
165: // leaving it for now, need to refactor during QA period
166: throw new IOException(
167: "Trouble restoring bean, caught ConversionException: "
168: + e.getMessage());
169: }
170:
171: return obj;
172: }
173:
174: // TODO: synchronize! but for what/whom? application? this? bean?
175: public synchronized void deleteFile(String filename) {
176: logger.info("trying to delete file: " + filename);
177:
178: File file = new File(filename);
179:
180: if (file.exists()) {
181: logger.debug("delete result: " + file.delete());
182: }
183: }
184: }
|