1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package no.feide.moria.directory.index.tools;
21
22 import java.io.File;
23 import java.io.FileInputStream;
24 import java.io.FileOutputStream;
25 import java.io.IOException;
26 import java.io.ObjectInputStream;
27 import java.io.ObjectOutputStream;
28 import java.util.List;
29
30 import no.feide.moria.directory.index.SerializableIndex;
31 import org.jdom.Element;
32 import org.jdom.JDOMException;
33 import org.jdom.input.SAXBuilder;
34
35 /***
36 * Index creator. Will read a file (given by the first parameter) and parse it
37 * into a <code>SerializableIndex</code> object (actually uses
38 * <code>WritableSerializableIndex</code> internally). The index object is
39 * then written to file (given by the second parameter) for use with the
40 * Directory Manager.
41 */
42 public final class SerializableIndexCreator {
43
44 /*** Internal representation of the index. */
45 private static SerializableIndex generatedIndex = new SerializableIndex();
46
47
48 /***
49 * Default private constructor.
50 */
51 private SerializableIndexCreator() {
52
53
54
55 };
56
57
58 /***
59 * Main method. Reads the index file, writes the index object, and finally
60 * verifies that the generated and written contents match.
61 * @param args
62 * First element should be the index specification file, second
63 * element should be the index object output file.
64 * @throws IOException
65 * If unable to read from or write to file.
66 * @throws JDOMException
67 * If unable to parse the index file.
68 * @throws ClassNotFoundException
69 * If unable to read the <code>SerializableIndex</code> object
70 * from file when verifying results.
71 */
72 public static void main(final String[] args) throws IOException,
73 JDOMException,
74 ClassNotFoundException {
75
76
77 if (args.length != 2) {
78 System.out.println("Usage:\nSerializableIndexCreator indexFile outputFile");
79 return;
80 }
81
82
83 System.out.println("Reading file " + args[0]);
84 final Element rootElement = (new SAXBuilder()).build(new File(args[0])).getRootElement();
85
86
87 final List associations = rootElement.getChildren("Associations");
88 for (int i = 0; i < associations.size(); i++) {
89
90
91 final List realms = ((Element) associations.get(i)).getChildren("Realm");
92 for (int j = 0; j < realms.size(); j++) {
93
94
95 Element realm = (Element) realms.get(j);
96 final List bases = realm.getChildren("Base");
97 for (int k = 0; k < bases.size(); k++) {
98 Element base = (Element) bases.get(k);
99 generatedIndex.addAssociation(realm.getAttributeValue("name"), base.getAttributeValue("name"), base.getAttributeValue("username", ""), base.getAttributeValue("password", ""));
100 }
101
102 }
103
104 }
105
106
107 final List exceptions = rootElement.getChildren("Exception");
108 for (int i = 0; i < exceptions.size(); i++) {
109
110 Element exception = (Element) exceptions.get(i);
111 generatedIndex.addException(exception.getAttributeValue("id"), exception.getAttributeValue("reference"), exception.getAttributeValue("realm"));
112
113 }
114
115
116 System.out.println(generatedIndex.toString());
117
118
119 System.out.println("Writing to file " + args[1]);
120 ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(args[1]));
121 out.writeObject(generatedIndex);
122 out.close();
123
124
125 ObjectInputStream in = new ObjectInputStream(new FileInputStream(args[1]));
126 final SerializableIndex writtenIndex = (SerializableIndex) in.readObject();
127 in.close();
128
129
130 System.out.println(writtenIndex.toString());
131
132
133 if (generatedIndex.equals(writtenIndex))
134 System.out.println("Generated and written indexes match");
135 else
136 System.err.println("Generated and written indexes DO NOT match!");
137
138 }
139 }