Source Code Cross Referenced for PersistenceManagerXmlImpl.java in  » Portal » gridsphere » org » gridsphere » portletcontainer » impl » 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 » Portal » gridsphere » org.gridsphere.portletcontainer.impl 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * @author <a href="mailto:oliver@wehrens.de">Oliver Wehrens</a>
003:         * @author <a href="mailto:novotny@gridsphere.org">Jason Novotny</a>
004:         * @version $Id: PersistenceManagerXmlImpl.java 6385 2007-10-25 14:02:26Z wehrens $
005:         */
006:        package org.gridsphere.portletcontainer.impl;
007:
008:        import org.apache.commons.logging.Log;
009:        import org.apache.commons.logging.LogFactory;
010:        import org.exolab.castor.mapping.Mapping;
011:        import org.exolab.castor.mapping.MappingException;
012:        import org.exolab.castor.xml.MarshalException;
013:        import org.exolab.castor.xml.Marshaller;
014:        import org.exolab.castor.xml.Unmarshaller;
015:        import org.exolab.castor.xml.ValidationException;
016:        import org.gridsphere.services.core.persistence.PersistenceManagerException;
017:        import org.gridsphere.services.core.persistence.PersistenceManagerXml;
018:        import org.xml.sax.InputSource;
019:
020:        import java.io.*;
021:        import java.net.URL;
022:        import java.util.ArrayList;
023:        import java.util.List;
024:
025:        /**
026:         * The PersistenceManagerXmlImpl provides easy access to marshal/unmarshal Java objects to XML files
027:         */
028:        public class PersistenceManagerXmlImpl implements  PersistenceManagerXml {
029:
030:            protected Log log = LogFactory
031:                    .getLog(PersistenceManagerXmlImpl.class);
032:
033:            private List mappingPaths = new ArrayList();
034:            private String descriptorPath = null;
035:
036:            /**
037:             * PersistenceManagerXmlImpl default constructor
038:             */
039:            private PersistenceManagerXmlImpl() {
040:            }
041:
042:            /**
043:             * Creates an instance of PersistenceManagerXmlImpl from a descriptor and mapping URL
044:             *
045:             * @param descriptorPath the descriptor location
046:             * @param mappingPath    the mapping location
047:             */
048:            public PersistenceManagerXmlImpl(String descriptorPath,
049:                    String mappingPath) {
050:                this .descriptorPath = descriptorPath;
051:                addMappingPath(mappingPath);
052:            }
053:
054:            /**
055:             * Creates an instance of PersistenceManagerXmlImpl from a descriptor and mapping URL
056:             *
057:             * @param descriptorPath the descriptor location
058:             * @param mappingURL    the mapping location expressed as a URL
059:             */
060:            public PersistenceManagerXmlImpl(String descriptorPath,
061:                    URL mappingURL) {
062:                this .descriptorPath = descriptorPath;
063:                addMappingPath(mappingURL);
064:            }
065:
066:            public void addMappingPath(String path) {
067:                mappingPaths.add(path);
068:            }
069:
070:            public void addMappingPath(URL path) {
071:                mappingPaths.add(path);
072:            }
073:
074:            /**
075:             * Sets the mapping file path
076:             *
077:             * @param mappingPath the mapping file path
078:             */
079:            public void setMappingPath(String mappingPath) {
080:                mappingPaths.clear();
081:                mappingPaths.add(mappingPath);
082:            }
083:
084:            /**
085:             * Sets the mapping file url
086:             *
087:             * @param mappingURL the mapping url
088:             */
089:            public void setMappingPath(URL mappingURL) {
090:                mappingPaths.clear();
091:                mappingPaths.add(mappingURL);
092:            }
093:
094:            /**
095:             * Sets the descriptor file path
096:             *
097:             * @param descriptorPath the file path of the descriptor
098:             */
099:            public void setDescriptorPath(String descriptorPath) {
100:                this .descriptorPath = descriptorPath;
101:            }
102:
103:            /**
104:             * Returns the filename of the mappingfile
105:             *
106:             * @return name of the mappingfile
107:             */
108:            public String getDescriptorPath() {
109:                return descriptorPath;
110:            }
111:
112:            /**
113:             * Marshals the given object to an xml file
114:             *
115:             * @param object object to be marshalled
116:             * @throws PersistenceManagerException
117:             *                     if the configuration was wrong
118:             */
119:            public void save(Object object) throws PersistenceManagerException {
120:                try {
121:                    Writer w = new BufferedWriter(new OutputStreamWriter(
122:                            new FileOutputStream(descriptorPath), "UTF-8"));
123:                    FileWriter filewriter = new FileWriter(descriptorPath);
124:                    Marshaller marshal = new Marshaller(w);
125:                    Mapping map = new Mapping();
126:                    for (Object mappingObj : mappingPaths) {
127:                        log.debug("Loading mapping path " + mappingObj);
128:                        if (mappingObj instanceof  String) {
129:                            map.loadMapping((String) mappingObj);
130:                        } else if (mappingObj instanceof  URL) {
131:                            map.loadMapping((URL) mappingObj);
132:                        }
133:                    }
134:                    marshal.setMapping(map);
135:                    marshal.marshal(object);
136:                    filewriter.close();
137:                    Class cl = object.getClass();
138:                    log.debug("Wrote object of type " + cl.getName()
139:                            + " to XMLFile " + descriptorPath);
140:                } catch (ValidationException e) {
141:                    throw new PersistenceManagerException("Validation Error", e);
142:                } catch (MarshalException e) {
143:                    throw new PersistenceManagerException("Marshal Error: ", e);
144:                } catch (MappingException e) {
145:                    throw new PersistenceManagerException("Mapping Error", e);
146:                } catch (IOException e) {
147:                    throw new PersistenceManagerException("I/O Error", e);
148:                }
149:            }
150:
151:            /**
152:             * restores an object from an xml file
153:             *
154:             * @return object which was unmarshalled
155:             * @throws PersistenceManagerException if restore was not succsessful
156:             */
157:            public Object load() throws PersistenceManagerException {
158:                Object object;
159:                try {
160:                    log.debug("Using getConnectionURL() " + descriptorPath);
161:                    InputSource xmlSource = new InputSource(descriptorPath);
162:                    Mapping mapping = new Mapping();
163:                    for (Object mappingObj : mappingPaths) {
164:                        log.debug("Loading mapping path " + mappingObj);
165:                        if (mappingObj instanceof  String) {
166:                            mapping.loadMapping((String) mappingObj);
167:                        } else if (mappingObj instanceof  URL) {
168:                            mapping.loadMapping((URL) mappingObj);
169:                        }
170:                    }
171:                    Unmarshaller unmarshal = new Unmarshaller(mapping);
172:                    unmarshal.setValidation(true);
173:                    unmarshal.setIgnoreExtraElements(true);
174:                    unmarshal.setIgnoreExtraAttributes(true);
175:                    object = unmarshal.unmarshal(xmlSource);
176:                } catch (MappingException e) {
177:                    throw new PersistenceManagerException("Mapping Error", e);
178:                } catch (MarshalException e) {
179:                    throw new PersistenceManagerException("Marshal Error", e);
180:                } catch (ValidationException e) {
181:                    throw new PersistenceManagerException("Validation Error", e);
182:                } catch (IOException e) {
183:                    throw new PersistenceManagerException("IO Error", e);
184:                }
185:                return object;
186:            }
187:
188:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.