View Javadoc

1   /**
2    * Copyright 2010, CSIRO Australia.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *         http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package au.csiro.netcdf.cli;
18  
19  import java.io.IOException;
20  import java.io.InputStream;
21  import java.util.Properties;
22  
23  import org.apache.log4j.Logger;
24  
25  import au.csiro.netcdf.NcCSVExtract;
26  import au.csiro.netcdf.NcDefineAttributes;
27  import au.csiro.netcdf.NcDefineDimension;
28  import au.csiro.netcdf.NcDefineVariable;
29  import au.csiro.netcdf.NcWriteVariable;
30  
31  /**
32   * This is the main interface for the NetCDF Tool command line application.
33   * <p>
34   * It allows the caller to write to a netCDF files via series of command line operations.
35   * <p>
36   * Copyright 2010, CSIRO Australia All rights reserved.
37   * 
38   * @author Robert Bridle on 09/03/2010
39   * @version $Revision: 85 $ $Date: 2010-08-30 16:10:19 +1000 (Mon, 30 Aug 2010) $
40   */
41  public class NetCDFTool
42  {
43      /**
44       * Constant that defines the logger to be used.
45       */
46      private static final Logger LOG = Logger.getLogger(NetCDFTool.class.getName());
47      
48      /**
49       * The application's properties file.
50       */
51      private static final String PROPERTIES_FILENAME = "/application.properties";
52      
53      /**
54       * Name of the application which is displayed in command line usage message.
55       */
56      private static String APPLICATION_NAME = "";
57  
58      /**
59       * NetCDFTool application starting point.
60       * 
61       * @param args
62       *            command line arguments.
63       */
64      public static void main(String[] args)
65      {
66          loadProperties();
67  
68          Command command = constructCommand(args);
69          if (command != null)
70          {
71              String error = command.validCommand(args);
72              if (error == null || !error.isEmpty())
73              {
74                  System.out.println("Error: " + error);
75                  System.out.println(command.getUsageString());
76                  System.exit(1);
77              }
78              else
79              {
80                  try
81                  {
82                      command.execute(args);
83                      System.exit(0);
84                  }
85                  catch (Exception e)
86                  {
87                      LOG.error(e);
88  //                    e.printStackTrace();
89                      System.out.println("Error: " + e.getMessage());
90                      System.out.println(command.getUsageString());
91                      System.exit(2);
92                  }
93              }
94          }
95          else
96          {
97              String commandName = (args.length > 0) ? args[0] : " ";
98              System.out.println("Invalid Command:" + commandName);
99              System.exit(0);
100         }
101     }
102 
103       
104     /**
105      * @return the available commands
106      */
107     private static Command constructCommand(String[] args)
108     {
109         String commandName = new String();
110         Command command = null;
111         if (args.length > 0)
112         {
113             commandName = args[0];
114         }
115         if (NcCSVExtract.EXTRACT_COMMAND_NAME.equals(commandName))
116         {
117             command = new NcCSVExtract();
118         }
119         else if (NcDefineAttributes.NC_DEFINE_ATT_COMMAND_NAME.equals(commandName))
120         {
121             command = new NcDefineAttributes();
122         }
123         else if (NcDefineDimension.NC_DEFINE_DIM_COMMAND_NAME.equals(commandName))
124         {
125             command = new NcDefineDimension();
126         }
127         else if (NcDefineVariable.NC_DEFINE_VAR_COMMAND_NAME.equals(commandName))
128         {
129             command = new NcDefineVariable();
130         }
131         else if (NcWriteVariable.NC_WRITE_VAR_COMMAND_NAME.equals(commandName))
132         {
133             command = new NcWriteVariable();
134         }
135 
136         return command;
137 
138     }
139 
140     
141     /**
142      * Loads the specified properties file.
143      */
144     private static void loadProperties()
145     {
146         InputStream is = NetCDFTool.class.getResourceAsStream(PROPERTIES_FILENAME);
147         Properties props = new Properties();
148         try
149         {
150             props.load(is);
151             NetCDFTool.APPLICATION_NAME = props.getProperty("application.name");
152         }
153         catch (IOException e)
154         {
155             LOG.error("Could not load properties file: " + PROPERTIES_FILENAME, e);
156         }
157     }
158 }