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 /*** Default private constructor. */
49 private SerializableIndexCreator() { };
50
51 /***
52 * Main method. Reads the index file, writes the index object, and
53 * finally verifies that the generated and written contents match.
54 * @param args
55 * First element should be the index specification file, second
56 * element should be the index object output file.
57 * @throws IOException
58 * If unable to read from or write to file.
59 * @throws JDOMException
60 * If unable to parse the index file.
61 * @throws ClassNotFoundException
62 * If unable to read the <code>SerializableIndex</code> object
63 * from file when verifying results.
64 */
65 public static void main(final String[] args)
66 throws IOException, JDOMException, ClassNotFoundException {
67
68
69 if (args.length != 2) {
70 System.out.println("Usage:\nSerializableIndexCreator indexFile outputFile");
71 return;
72 }
73
74
75 System.out.println("Reading file " + args[0]);
76 final Element rootElement = (new SAXBuilder()).build(new File(args[0])).getRootElement();
77
78
79 final List associations = rootElement.getChildren("Associations");
80 for (int i = 0; i < associations.size(); i++) {
81
82
83 final List realms = ((Element) associations.get(i)).getChildren("Realm");
84 for (int j = 0; j < realms.size(); j++) {
85
86
87 Element realm = (Element) realms.get(j);
88 final List bases = realm.getChildren("Base");
89 for (int k = 0; k < bases.size(); k++) {
90 Element base = (Element) bases.get(k);
91 generatedIndex.addAssociation(realm.getAttributeValue("name"), base.getAttributeValue("name"), base.getAttributeValue("username", ""), base.getAttributeValue("password", ""));
92 }
93
94 }
95
96 }
97
98
99 final List exceptions = rootElement.getChildren("Exception");
100 for (int i = 0; i < exceptions.size(); i++) {
101
102 Element exception = (Element) exceptions.get(i);
103 generatedIndex.addException(exception.getAttributeValue("id"), exception.getAttributeValue("reference"),
104 exception.getAttributeValue("realm"));
105
106 }
107
108
109 System.out.println(generatedIndex.toString());
110
111
112 System.out.println("Writing to file " + args[1]);
113 ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(args[1]));
114 out.writeObject(generatedIndex);
115 out.close();
116
117
118 ObjectInputStream in = new ObjectInputStream(new FileInputStream(args[1]));
119 final SerializableIndex writtenIndex = (SerializableIndex) in.readObject();
120 in.close();
121
122
123 System.out.println(writtenIndex.toString());
124
125
126 if (generatedIndex.equals(writtenIndex))
127 System.out.println("Generated and written indexes match");
128 else
129 System.err.println("Generated and written indexes DO NOT match!");
130
131 }
132 }