Source Code Cross Referenced for BeanStorage.java in  » Report » openi » org » openi » xml » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Report » openi » org.openi.xml 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


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:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.