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   import org.astrogrid.mySpace.mySpaceStatus.Logger;
9   import org.astrogrid.mySpace.mySpaceStatus.MySpaceStatus;
10  import org.astrogrid.mySpace.mySpaceStatus.MySpaceStatusCode;
11  
12  /***
13   * Program to create a new MySpace registry.  The MySpace registry
14   * comprises a database of two tables.  One table will hold the files
15   * known to the MySpace system and is left open.  The other holds the
16   * details of the servers comprising the MySpace system and is populated
17   * with a set of server details read from a text file.
18   *
19   * @author A C Davenhall (Edinburgh)
20   * @version Iteration 3.
21   */
22  
23  class CreateMySpaceRegistry
24  {  private Logger logger = new Logger(false, true, true, "./myspace.log");
25     private Configuration config = new Configuration(true, false,
26       Configuration.MANAGERONLY);
27  
28     private static MySpaceStatus status = new MySpaceStatus();
29  
30  /***
31   * Read the file containing details of the servers known to the
32   * MySpace system. 
33   */
34  
35     private Vector readServersFile(String serversFileName)
36     {  Vector servers = new Vector();
37  
38  //
39  //   Try to read the file
40  
41        try
42        {  File serversFile = new File(serversFileName);
43           FileReader in = new FileReader(serversFile);
44           char c[] = new char[(int)serversFile.length()];
45           in.read(c);
46           String s = new String(c);
47  
48  //
49  //      Convert the input file into a vector of individual lines.
50  
51           Vector inputLines = new Vector();
52  
53           StringTokenizer token = new StringTokenizer(s, "\n");
54  
55           while(token.hasMoreTokens())
56           {  inputLines.add(token.nextToken());
57           }
58  
59  //
60  //      Examine every line, decoding the details of the registry
61  //      configuration.
62  
63           String currentLine = "";
64           int lineCount = 0;
65           int numServers = 0;
66  
67           Iterator iter = inputLines.iterator();
68           while(iter.hasNext())
69           {  lineCount = lineCount + 1;
70  
71              currentLine = (String)iter.next();
72  
73              System.out.println("processing line " + lineCount 
74                + ": " + currentLine);
75  
76  //
77  //         Remove any comments.  Remember that comments start with a
78  //         '#' character.
79  
80              int commentPos = currentLine.indexOf("#");
81              if (commentPos > 0)
82              {  currentLine = currentLine.substring(0, commentPos-1);
83              }
84  
85  //
86  //         Split the remaining line into a series of elements (or
87  //         'tokens') separated by blanks.  Proceed if there are any
88  //         such elements.
89  
90              StringTokenizer currentLineToken =
91                new StringTokenizer(currentLine);
92  
93              int    currentElementCount = -1;
94              Vector currentElements = new Vector();
95  
96              while(currentLineToken.hasMoreTokens())
97              {  currentElementCount = currentElementCount + 1;
98  
99                 String currentElem = currentLineToken.nextToken();
100 //               System.out.println(currentElem);
101 
102                currentElements.add(
103                  currentElementCount, currentElem);
104             }
105 
106             int numElements = currentElements.size();
107 
108             if (numElements > 0)
109             {
110 //
111 //            Check if the first element corresponds to a server.
112 //            Currently servers are the only type of item which can
113 //            appear in the file.
114 
115                if (((String)currentElements.get(0)).equals("server"))
116                {  if (numElements > 4)
117                   {  String currentName = (String)currentElements.get(1);
118 
119                      String stringExpiry = (String)currentElements.get(2);
120                      Integer expiry = new Integer(stringExpiry);
121                      int currentExpiry = expiry.intValue();
122 
123                      String currentURI = (String)currentElements.get(3);
124                      String currentDirectory =
125                        (String)currentElements.get(4);
126 
127                      numServers = numServers + 1;
128 
129                      ServerDetails currentServer = new ServerDetails(
130                        currentName, currentExpiry, currentURI,
131                        currentDirectory);
132 
133                      servers.add(currentServer);
134                   }
135                   else
136                   {  MySpaceStatus status  =
137                        new MySpaceStatus(
138                          MySpaceStatusCode.AGMMCW00151,
139                          MySpaceStatusCode.WARN,
140                          MySpaceStatusCode.NOLOG,
141                          this.getClassName() );
142                   }
143                }
144             }
145          }
146 
147 //
148 //      Check that at least one server has been specified, and if not
149 //      report an error.
150 
151          if (servers.size() < 1)
152          {  MySpaceStatus status  =
153               new MySpaceStatus(MySpaceStatusCode.AGMMCE00104,
154                 MySpaceStatusCode.ERROR, MySpaceStatusCode.NOLOG,
155                 this.getClassName());
156 
157             servers = null;
158          }
159 
160          in.close();
161       }
162 
163 //
164 //   Note: any exception caught here is likely to be an I/O exception,
165 //   but other types are possible, so a catch-all exception is used.
166 
167       catch (Exception all)
168       {  MySpaceStatus status = new MySpaceStatus(
169            MySpaceStatusCode.AGMMCE00104, MySpaceStatusCode.ERROR,
170            MySpaceStatusCode.LOG, this.getClassName() );
171 
172          System.out.println("Cannot read input file.\n");
173          all.printStackTrace();
174 
175          servers = null;
176       }
177 
178       return servers;
179    }
180 
181 //
182 // -------------------------------------------------------------------
183 
184 /***
185  * Obtain the name of the current Java class.
186  */
187 
188    protected String getClassName()
189    { Class currentClass = this.getClass();
190      String name =  currentClass.getName();
191      int dotPos = name.lastIndexOf(".");
192      if (dotPos > -1)
193      {  name = name.substring(dotPos+1, name.length() );
194      }
195 
196      return name;
197    }
198 
199 //
200 // -------------------------------------------------------------------
201 
202    public static void main (String argv[])
203    {  if (argv.length == 1)
204       {  
205 //
206 //      argv[0] contains the name of the MySpace registry.
207 
208          String registryName = argv[0]; 
209 
210 //
211 //      Construct the name of the file holding the servers' details
212 //      from the registry name.
213 
214          String serversFileName = registryName + ".servers";
215 
216 //
217 //      Attempt to read and parse the file of servers' details and
218 //      proceed of ok.
219 
220          CreateMySpaceRegistry createReg = new CreateMySpaceRegistry();
221          Vector servers = createReg.readServersFile(serversFileName);
222          if (servers != null)
223          {
224 //
225 //         Attempt to create the MySpace registry.
226 
227             RegistryManager reg = new RegistryManager(registryName,
228               servers);
229             if (status.getSuccessStatus())
230             {  System.out.println("MySpace registry " + registryName +
231                  " created successfully.");
232             }
233             else
234             {  System.out.println("\n*** Failed to create MySpace " +
235                  "registry " + registryName + ".");
236             }
237          }
238          else
239          {  System.out.println("\n*** Failed to read file " +
240               serversFileName + " containing details of the servers.");
241          }
242       }
243       else
244       {  System.out.println("Usage:-");
245          System.out.println("  java CreateMySpaceRegistry registryName");
246       }
247    }
248 }