KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tc > config > Loader


1 /*
2  * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright
3  * notice. All rights reserved.
4  */

5 package com.tc.config;
6
7 import org.apache.xmlbeans.SchemaType;
8 import org.apache.xmlbeans.XmlException;
9 import org.apache.xmlbeans.XmlOptions;
10
11 import com.tc.config.schema.migrate.ConfigUpdate;
12 import com.tc.config.schema.migrate.V1toV2;
13 import com.tc.config.schema.migrate.V2toV3;
14
15 import java.io.ByteArrayInputStream JavaDoc;
16 import java.io.File JavaDoc;
17 import java.io.FileInputStream JavaDoc;
18 import java.io.IOException JavaDoc;
19 import java.io.InputStream JavaDoc;
20 import java.net.URL JavaDoc;
21
22 /**
23  * Responsible for creating a TcConfigDocument of the current version from any published configuration schema version.
24  */

25 public final class Loader {
26
27   private final ConfigUpdate[] converters;
28
29   public Loader() {
30     // order newest to oldest -- don't ever hotswap converters
31
converters = new ConfigUpdate[] { new V2toV3(), new V1toV2() };
32   }
33
34   private com.terracottatech.config.TcConfigDocument convert(InputStream JavaDoc in, XmlOptions xmlOptions) throws IOException JavaDoc,
35       XmlException {
36     byte[] data = new byte[in.available()];
37     in.read(data);
38     in.close();
39     ByteArrayInputStream JavaDoc ain = new ByteArrayInputStream JavaDoc(data);
40     SchemaType type = com.terracottatech.config.TcConfigDocument.type;
41     if (xmlOptions == null) xmlOptions = new XmlOptions();
42     xmlOptions.setDocumentType(type);
43     try {
44       return com.terracottatech.config.TcConfigDocument.Factory.parse(ain, xmlOptions);
45     } catch (XmlException e) {
46       ain.reset();
47       return com.terracottatech.config.TcConfigDocument.Factory.parse(updateConfig(ain, 0, xmlOptions), xmlOptions);
48     }
49   }
50
51   private synchronized InputStream JavaDoc updateConfig(InputStream JavaDoc in, int index, XmlOptions xmlOptions) throws IOException JavaDoc,
52       XmlException {
53     byte[] data = new byte[in.available()];
54     in.read(data);
55     in.close();
56     ByteArrayInputStream JavaDoc ain = new ByteArrayInputStream JavaDoc(data);
57     try {
58       return converters[index].convert(ain, xmlOptions);
59     } catch (XmlException e) {
60       if (index == converters.length - 1) throw e;
61       ain.reset();
62       return converters[index].convert(updateConfig(ain, index + 1, xmlOptions), xmlOptions);
63     }
64   }
65
66   public com.terracottatech.config.TcConfigDocument parse(File JavaDoc file) throws IOException JavaDoc, XmlException {
67     return convert(new FileInputStream JavaDoc(file), null);
68   }
69
70   public com.terracottatech.config.TcConfigDocument parse(File JavaDoc file, XmlOptions xmlOptions) throws IOException JavaDoc,
71       XmlException {
72     return convert(new FileInputStream JavaDoc(file), xmlOptions);
73   }
74
75   public com.terracottatech.config.TcConfigDocument parse(String JavaDoc xmlText) throws IOException JavaDoc, XmlException {
76     return convert(new ByteArrayInputStream JavaDoc(xmlText.getBytes()), null);
77   }
78
79   public com.terracottatech.config.TcConfigDocument parse(String JavaDoc xmlText, XmlOptions xmlOptions) throws IOException JavaDoc,
80       XmlException {
81     return convert(new ByteArrayInputStream JavaDoc(xmlText.getBytes()), xmlOptions);
82   }
83
84   public com.terracottatech.config.TcConfigDocument parse(InputStream JavaDoc stream) throws IOException JavaDoc, XmlException {
85     return convert(stream, null);
86   }
87
88   public com.terracottatech.config.TcConfigDocument parse(InputStream JavaDoc stream, XmlOptions xmlOptions)
89       throws IOException JavaDoc, XmlException {
90     return convert(stream, xmlOptions);
91   }
92
93   public com.terracottatech.config.TcConfigDocument parse(URL JavaDoc url) throws IOException JavaDoc, XmlException {
94     return convert(url.openStream(), null);
95   }
96
97   public com.terracottatech.config.TcConfigDocument parse(URL JavaDoc url, XmlOptions xmlOptions) throws IOException JavaDoc,
98       XmlException {
99     return convert(url.openStream(), xmlOptions);
100   }
101
102   public boolean testIsOld(File JavaDoc file) throws IOException JavaDoc, XmlException {
103     return !testIsCurrent(file);
104   }
105
106   public boolean testIsCurrent(File JavaDoc file) throws IOException JavaDoc, XmlException {
107     com.terracottatech.config.TcConfigDocument.Factory.parse(new FileInputStream JavaDoc(file));
108     return true;
109   }
110
111   public void updateToCurrent(File JavaDoc file) throws IOException JavaDoc, XmlException {
112     XmlOptions options = null;
113     synchronized (converters) {
114       options = converters[0].createDefaultXmlOptions();
115     }
116     com.terracottatech.config.TcConfigDocument doc = convert(new FileInputStream JavaDoc(file), options);
117     doc.save(file);
118   }
119 }
120
Popular Tags