001: /*
002: * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright
003: * notice. All rights reserved.
004: */
005: package com.tc.config;
006:
007: import org.apache.xmlbeans.SchemaType;
008: import org.apache.xmlbeans.XmlException;
009: import org.apache.xmlbeans.XmlOptions;
010:
011: import com.tc.config.schema.migrate.ConfigUpdate;
012: import com.tc.config.schema.migrate.V1toV2;
013: import com.tc.config.schema.migrate.V2toV3;
014:
015: import java.io.ByteArrayInputStream;
016: import java.io.File;
017: import java.io.FileInputStream;
018: import java.io.IOException;
019: import java.io.InputStream;
020: import java.net.URL;
021:
022: /**
023: * Responsible for creating a TcConfigDocument of the current version from any published configuration schema version.
024: */
025: public final class Loader {
026:
027: private final ConfigUpdate[] converters;
028:
029: public Loader() {
030: // order newest to oldest -- don't ever hotswap converters
031: converters = new ConfigUpdate[] { new V2toV3(), new V1toV2() };
032: }
033:
034: private com.terracottatech.config.TcConfigDocument convert(
035: InputStream in, XmlOptions xmlOptions) throws IOException,
036: XmlException {
037: byte[] data = new byte[in.available()];
038: in.read(data);
039: in.close();
040: ByteArrayInputStream ain = new ByteArrayInputStream(data);
041: SchemaType type = com.terracottatech.config.TcConfigDocument.type;
042: if (xmlOptions == null)
043: xmlOptions = new XmlOptions();
044: xmlOptions.setDocumentType(type);
045: try {
046: return com.terracottatech.config.TcConfigDocument.Factory
047: .parse(ain, xmlOptions);
048: } catch (XmlException e) {
049: ain.reset();
050: return com.terracottatech.config.TcConfigDocument.Factory
051: .parse(updateConfig(ain, 0, xmlOptions), xmlOptions);
052: }
053: }
054:
055: private synchronized InputStream updateConfig(InputStream in,
056: int index, XmlOptions xmlOptions) throws IOException,
057: XmlException {
058: byte[] data = new byte[in.available()];
059: in.read(data);
060: in.close();
061: ByteArrayInputStream ain = new ByteArrayInputStream(data);
062: try {
063: return converters[index].convert(ain, xmlOptions);
064: } catch (XmlException e) {
065: if (index == converters.length - 1)
066: throw e;
067: ain.reset();
068: return converters[index].convert(updateConfig(ain,
069: index + 1, xmlOptions), xmlOptions);
070: }
071: }
072:
073: public com.terracottatech.config.TcConfigDocument parse(File file)
074: throws IOException, XmlException {
075: return convert(new FileInputStream(file), null);
076: }
077:
078: public com.terracottatech.config.TcConfigDocument parse(File file,
079: XmlOptions xmlOptions) throws IOException, XmlException {
080: return convert(new FileInputStream(file), xmlOptions);
081: }
082:
083: public com.terracottatech.config.TcConfigDocument parse(
084: String xmlText) throws IOException, XmlException {
085: return convert(new ByteArrayInputStream(xmlText.getBytes()),
086: null);
087: }
088:
089: public com.terracottatech.config.TcConfigDocument parse(
090: String xmlText, XmlOptions xmlOptions) throws IOException,
091: XmlException {
092: return convert(new ByteArrayInputStream(xmlText.getBytes()),
093: xmlOptions);
094: }
095:
096: public com.terracottatech.config.TcConfigDocument parse(
097: InputStream stream) throws IOException, XmlException {
098: return convert(stream, null);
099: }
100:
101: public com.terracottatech.config.TcConfigDocument parse(
102: InputStream stream, XmlOptions xmlOptions)
103: throws IOException, XmlException {
104: return convert(stream, xmlOptions);
105: }
106:
107: public com.terracottatech.config.TcConfigDocument parse(URL url)
108: throws IOException, XmlException {
109: return convert(url.openStream(), null);
110: }
111:
112: public com.terracottatech.config.TcConfigDocument parse(URL url,
113: XmlOptions xmlOptions) throws IOException, XmlException {
114: return convert(url.openStream(), xmlOptions);
115: }
116:
117: public boolean testIsOld(File file) throws IOException,
118: XmlException {
119: return !testIsCurrent(file);
120: }
121:
122: public boolean testIsCurrent(File file) throws IOException,
123: XmlException {
124: com.terracottatech.config.TcConfigDocument.Factory
125: .parse(new FileInputStream(file));
126: return true;
127: }
128:
129: public void updateToCurrent(File file) throws IOException,
130: XmlException {
131: XmlOptions options = null;
132: synchronized (converters) {
133: options = converters[0].createDefaultXmlOptions();
134: }
135: com.terracottatech.config.TcConfigDocument doc = convert(
136: new FileInputStream(file), options);
137: doc.save(file);
138: }
139: }
|