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      /*** 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          // Show usage.
69          if (args.length != 2) {
70              System.out.println("Usage:\nSerializableIndexCreator indexFile outputFile");
71              return;
72          }
73  
74          // Read index file.
75          System.out.println("Reading file " + args[0]);
76          final Element rootElement = (new SAXBuilder()).build(new File(args[0])).getRootElement();
77  
78          // Process association elements (yes, we support more than one...)
79          final List associations = rootElement.getChildren("Associations");
80          for (int i = 0; i < associations.size(); i++) {
81  
82              // Process realms in this association.
83              final List realms = ((Element) associations.get(i)).getChildren("Realm");
84              for (int j = 0; j < realms.size(); j++) {
85  
86                  // Process bases in this realm.
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          // Process exception elements.
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         // Dump the index to console.
109         System.out.println(generatedIndex.toString());
110 
111         // Write the index to file.
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         // Read the index from file.
118         ObjectInputStream in = new ObjectInputStream(new FileInputStream(args[1]));
119         final SerializableIndex writtenIndex = (SerializableIndex) in.readObject();
120         in.close();
121 
122         // Dump written index to console.
123         System.out.println(writtenIndex.toString());
124 
125         // Verify index contents.
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 }