View Javadoc

1   /*
2    * Copyright (c) 2004 UNINETT FAS
3    *
4    * This program is free software; you can redistribute it and/or modify it
5    * under the terms of the GNU General Public License as published by the Free
6    * Software Foundation; either version 2 of the License, or (at your option)
7    * any later version.
8    *
9    * This program is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11   * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12   * more details.
13   *
14   * You should have received a copy of the GNU General Public License along with
15   * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
16   * Place - Suite 330, Boston, MA 02111-1307, USA.
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          // Does nothing.
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          // Show usage.
77          if (args.length != 2) {
78              System.out.println("Usage:\nSerializableIndexCreator indexFile outputFile");
79              return;
80          }
81  
82          // Read index file.
83          System.out.println("Reading file " + args[0]);
84          final Element rootElement = (new SAXBuilder()).build(new File(args[0])).getRootElement();
85  
86          // Process association elements (yes, we support more than one...)
87          final List associations = rootElement.getChildren("Associations");
88          for (int i = 0; i < associations.size(); i++) {
89  
90              // Process realms in this association.
91              final List realms = ((Element) associations.get(i)).getChildren("Realm");
92              for (int j = 0; j < realms.size(); j++) {
93  
94                  // Process bases in this realm.
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         // Process exception elements.
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         // Dump the index to console.
116         System.out.println(generatedIndex.toString());
117 
118         // Write the index to file.
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         // Read the index from file.
125         ObjectInputStream in = new ObjectInputStream(new FileInputStream(args[1]));
126         final SerializableIndex writtenIndex = (SerializableIndex) in.readObject();
127         in.close();
128 
129         // Dump written index to console.
130         System.out.println(writtenIndex.toString());
131 
132         // Verify index contents.
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 }