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: }
|