View Javadoc

1   /*
2    * GUMS2MapUser.java
3    *
4    * Created on June 9, 2004, 1:44 PM
5    */
6   package gov.bnl.gums.admin;
7   
8   
9   import org.apache.axis.AxisFault;
10  
11  import org.apache.commons.cli.*;
12  
13  import java.net.ConnectException;
14  import java.net.InetAddress;
15  import java.net.URL;
16  import gov.bnl.gums.command.AbstractCommand;
17  
18  
19  /***
20   * @author carcassi
21   */
22  public class Pool_AddRange extends RemoteCommand {
23      static {
24          command = new Pool_AddRange();
25      }
26  
27      /***
28       * Creates a new ManualGroup_Add object.
29       */
30      public Pool_AddRange() {
31          syntax = "PERSISTANCE GROUP RANGE";
32          description = "Adds range of accounts to a pool. " +
33              "PERSISTANCE is the 'persistenceFactory' as defined in the configuration for the group. " +
34              "GROUP is the 'name' as defined in the configuration for the pool. " +
35              "RANGE is the group of accounts to be added (i.e. grid0050-125).";
36      }
37  
38      protected org.apache.commons.cli.Options buildOptions() {
39          Options options = new Options();
40  
41          return options;
42      }
43  
44      protected void execute(org.apache.commons.cli.CommandLine cmd)
45          throws Exception {
46          if (cmd.getArgs().length < 3) {
47              failForWrongParameters("Missing parameters...");
48          }
49  
50          String[] userDN = (cmd.getArgs());
51          String persistenceFactory = cmd.getArgs()[0];
52          String groupName = cmd.getArgs()[1];
53  
54          for (int nArg = 2; nArg < cmd.getArgs().length; nArg++) {
55              addRange(persistenceFactory, groupName,
56                  cmd.getArgs()[nArg]);
57          }
58      }
59  
60      private void addRange(String persistenceFactory, String groupName, String range) throws Exception {
61          String firstAccount = range.substring(0, range.indexOf('-'));
62          String lastAccountN = range.substring(range.indexOf('-') + 1);
63          String firstAccountN = firstAccount.substring(firstAccount.length() - lastAccountN.length());
64          String accountBase = firstAccount.substring(0, firstAccount.length() - lastAccountN.length());
65          int nFirstAccount = Integer.parseInt(firstAccountN);
66          int nLastAccount = Integer.parseInt(lastAccountN);
67  
68          StringBuffer last = new StringBuffer(firstAccount);
69          String nLastAccountString = Integer.toString(nLastAccount);
70          last.replace(firstAccount.length() - nLastAccountString.length(), firstAccount.length(), nLastAccountString);
71          
72          System.out.println("Adding accounts between '" + firstAccount + "' and '" + last.toString() + "' to pool '" + groupName + "'");
73          
74          StringBuffer buf = new StringBuffer(firstAccount);
75          int len = firstAccount.length();
76          for (int account = nFirstAccount; account <= nLastAccount; account++) {
77              String nAccount = Integer.toString(account);
78              buf.replace(len - nAccount.length(), len, nAccount);
79              getGums().poolAddAccount(persistenceFactory, groupName, buf.toString());
80              System.out.println(buf.toString() + " added");
81          }
82      }
83  }