View Javadoc

1   package org.astrogrid.mySpace.mySpaceDemo;
2   
3   import java.io.*;
4   import java.util.*;
5   
6   import org.astrogrid.mySpace.mySpaceManager.*;
7   
8   /***
9    * Minimal program to create a add new users to a MySpace registry.
10   * For each user entries for his top-level container (corresponding to
11   * him as a user) and his  second-level containers for all(corresponding
12   * each of the servers which he can access) should be added.  The full
13   * names of these containers are read from a file.
14   *
15   * @author A C Davenhall (Edinburgh)
16   * @version Iteration 3.
17   */
18  
19  class AddUsersToRegistry
20  {  public static void main (String argv[])
21     {  if (argv.length == 1)
22        {  String registryName = argv[0]; //argv[0] is "example"
23  
24           String registryFileName = registryName + ".reg";
25           String newUsersFileName = registryName + ".newusers";
26  
27  //
28  //      Attempt to open the input file from which the list
29  //      of top- and second-level containers for the new users are to be
30  //      read.
31  
32           try
33           {  File newUsersFile = new File(newUsersFileName);
34              FileReader in = new FileReader(newUsersFile);
35              char c[] = new char[(int)newUsersFile.length()];
36              in.read(c);
37              String s = new String(c);
38  
39  //
40  //         Convert the input file into a vector of individual lines.
41  
42              Vector inputLines = new Vector();
43  
44              StringTokenizer token = new StringTokenizer(s, "\n");
45  
46              while(token.hasMoreTokens())
47              {  inputLines.add(token.nextToken());
48              }
49  
50  //
51  //         Create a registry manager to which the new entries will be
52  //         written.
53  
54              RegistryManager reg = new RegistryManager(registryName);
55  
56              DataItemRecord itemRec = new DataItemRecord();
57  
58              Date creation = new Date();
59              creation = Calendar.getInstance().getTime();
60  
61  //
62  //         Loop through the lines read from the input file creating an
63  //         entry in the registry for each one.
64  
65              for (int loop = 0; loop < inputLines.size(); loop++)
66              {  String containerName = (String)inputLines.get(loop);
67  
68  //
69  //            Check that this container does not already exist.
70  
71                 Vector vec = reg.lookupDataItemRecords(containerName);
72                 if (vec.size() == 0)
73                 {
74  
75  //
76  //               Create an entry for the current entry.
77  
78                    int dataItemID = reg.getNextDataItemID();
79  
80                    itemRec = new DataItemRecord(containerName,
81                      dataItemID, "none", "sysadmin", creation, creation, 0,
82                      DataItemRecord.CON, "permissions");
83  
84  //
85  //               Add the entry to the registry.
86  
87                    boolean addSuccess = reg.addDataItemRecord(itemRec);
88                    if (addSuccess)
89                    {  System.out.println("  Added: " + containerName);
90                    }
91                    else
92                    {  System.out.println("  *** Failed to add: " +
93                         containerName);
94                    }
95                 }
96                 else
97                 {  System.out.println("  *** Container " + containerName +
98                      " already exists in the registry.");
99                 }
100             }
101 
102 //
103 //         Write the registry file to disk.
104 
105             reg.rewriteRegistryFile();
106 
107 //
108 //         Close the input file.
109 
110             in.close();
111          }
112          catch (java.io.IOException ex)
113          {  System.out.println(
114               "Failed to read the file of new users " +
115               newUsersFileName);
116 
117             ex.printStackTrace();
118          }
119       }
120       else
121       {  System.out.println("Usage:-");
122          System.out.println("  java AddUsersToRegistry registryName");
123       }
124    }
125 }