1 /* 2 * CDDL HEADER START 3 * 4 * The contents of this file are subject to the terms of the 5 * Common Development and Distribution License, Version 1.0 only 6 * (the "License"). You may not use this file except in compliance 7 * with the License. 8 * 9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 10 * or http://www.opensolaris.org/os/licensing. 11 * See the License for the specific language governing permissions 12 * and limitations under the License. 13 * 14 * When distributing Covered Code, include this CDDL HEADER in each 15 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 16 * If applicable, add the following below this CDDL HEADER, with the 17 * fields enclosed by brackets "[]" replaced with your own identifying 18 * information: Portions Copyright [yyyy] [name of copyright owner] 19 * 20 * CDDL HEADER END 21 */ 22 /* 23 * ident "%Z%%M% %I% %E% SMI" 24 * 25 * Copyright 2005 Sun Microsystems, Inc. All rights reserved. 26 * Use is subject to license terms. 27 */ 28 package com.sun.dhcpmgr.cli.dhcpbatch; 29 30 import java.io.*; 31 import java.text.MessageFormat; 32 33 import com.sun.dhcpmgr.bridge.BridgeException; 34 import com.sun.dhcpmgr.cli.common.DhcpCliPrint; 35 import com.sun.dhcpmgr.cli.common.DhcpCliProgram; 36 import com.sun.dhcpmgr.cli.common.DhcpCliFunction; 37 38 /** 39 * This class represents the entry point to the DHCP CLI batch 40 * administration. 41 */ 42 public class DhcpBatch 43 extends DhcpCliProgram { 44 45 /** 46 * The program signature. 47 */ 48 public static final String SIGNATURE = "dhcpbatch: "; 49 50 /** 51 * The source of the batch input. Either a fullpath to an 52 * input file or null if standard input is the source. 53 */ 54 String inputSource = null; 55 56 /** 57 * Flag indicating whether or not the batch processing 58 * should be verbose. 59 */ 60 boolean verbose = false; 61 62 /** 63 * The constructor used by the DHCP CLIs. 64 * @param inputSource a filepath or null if STDIN is to be used. 65 */ 66 public DhcpBatch(String inputSource) { 67 this(); 68 this.inputSource = inputSource; 69 } // constructor 70 71 /** 72 * The easy constructor that does nothing for now. 73 */ 74 public DhcpBatch() { 75 } // constructor 76 77 /** 78 * Required by DhcpCliProgram. 79 * @return null 80 */ 81 public String getManPage() { 82 return null; 83 } 84 85 /** 86 * Returns a localized string for this function 87 * @param key the resource bundle string identifier 88 * @return string from resource bundle. 89 */ 90 public String getString(String key) { 91 return ResourceStrings.getString(key); 92 } // getString 93 94 /** 95 * Sets the inputSource value. 96 * @param inputSource the input source. 97 */ 98 public void setInputSource(String inputSource) { 99 this.inputSource = inputSource; 100 } // setInputSource 101 102 /** 103 * Sets the verbose value. 104 * @param value the new value for the verbose attribute. 105 */ 106 public void setVerbose(boolean value) { 107 verbose = value; 108 } // setVerbose 109 110 /** 111 * Executes the batch. 112 * @return return code as returned by batched program 113 */ 114 public int execute() { 115 116 int returnCode = SUCCESS; 117 118 // By default read input from standard input. 119 // 120 InputStream in = System.in; 121 122 // If an argument was was provided, then it must be the batch file. 123 // Read input from the file rather than standard input. 124 // 125 if (inputSource != null) { 126 try { 127 in = new FileInputStream(inputSource); 128 } catch (FileNotFoundException e) { 129 Object [] arguments = new Object[1]; 130 arguments[0] = inputSource; 131 printErrMessage(getString("dhcpbatch_file_not_found"), 132 arguments); 133 return (CRITICAL); 134 } catch (Throwable e) { 135 Object [] arguments = new Object[1]; 136 arguments[0] = inputSource; 137 printErrMessage(getString("dhcpbatch_open_failed"), arguments); 138 printErrMessage(DhcpCliFunction.getMessage(e)); 139 return (CRITICAL); 140 } 141 } 142 143 // Really just want to read lines at a time so, a BufferedReader 144 // will do the trick. 145 // 146 BufferedReader bufferedIn = 147 new BufferedReader(new InputStreamReader(in)); 148 149 // Read a line at a time and exec the appropriate DHCP CLI command. 150 // 151 StringBuffer line = new StringBuffer(200); 152 line.append("> "); 153 DhcpCommand command = new DhcpCommand(); 154 for (boolean end = false; end != true; ) { 155 try { 156 // Read a line. End of file seems to result in null line. 157 // 158 String input = bufferedIn.readLine(); 159 if (input == null) { 160 // eof 161 end = true; 162 continue; 163 } else if (input.length() == 0) { 164 // empty line 165 } else if (input.charAt(0) == '#' && line.length() == 2) { 166 // comment 167 continue; 168 } else { 169 line.append(input); 170 if (input.charAt(input.length() - 1) == '\\') { 171 // continuation 172 continue; 173 } 174 } 175 176 if (verbose) { 177 DhcpCliPrint.printMessage(line.toString()); 178 } 179 command.init(line.substring(2)); 180 command.execute(); 181 } catch (BridgeException e) { 182 // Failed to process command; print message and continue on 183 Object [] arguments = new Object[2]; 184 arguments[0] = line.substring(2); 185 arguments[1] = DhcpCliFunction.getMessage(e); 186 printErrMessage(getString("dhcpbatch_cmd_error"), arguments); 187 returnCode = CRITICAL; 188 } catch (EOFException e) { 189 // Don't ever seem to get this, but just in case. 190 // 191 end = true; 192 } catch (IOException e) { 193 Object [] arguments = new Object[1]; 194 arguments[0] = inputSource; 195 printErrMessage(getString("dhcpbatch_read_failed"), arguments); 196 printErrMessage(DhcpCliFunction.getMessage(e)); 197 end = true; 198 returnCode = CRITICAL; 199 } finally { 200 // Reset buffer to process next command 201 line.setLength(2); 202 } 203 } 204 205 // Close the stream. Ignore errors as we're exiting anyway. 206 // 207 try { 208 bufferedIn.close(); 209 } catch (IOException e) { 210 // Ignore it. 211 } 212 213 return (returnCode); 214 215 } // execute 216 217 /** 218 * Prints an error message. 219 * @param msg the message to print. 220 */ 221 public static void printErrMessage(String msg) { 222 StringBuffer fullmsg = new StringBuffer(SIGNATURE); 223 fullmsg.append(msg); 224 DhcpCliPrint.printErrMessage(fullmsg.toString()); 225 } // printErrMessage 226 227 /** 228 * Prints an error message. 229 * @param msg the message to print. 230 */ 231 public static void printErrMessage(String msg, Object [] arguments) { 232 StringBuffer fullmsg = new StringBuffer(SIGNATURE); 233 fullmsg.append(msg); 234 MessageFormat form = new MessageFormat(fullmsg.toString()); 235 DhcpCliPrint.printErrMessage(form.format(arguments)); 236 } // printErrMessage 237 238 /** 239 * The entry point for the program. 240 * @param args the program arguments 241 */ 242 public static void main(String[] args) { 243 244 DhcpBatch dhcpbatch = new DhcpBatch(); 245 246 // Check usage. 247 // 248 String source = null; 249 if (args.length == 1) { 250 source = args[0]; 251 } else if (args.length > 1) { 252 DhcpCliPrint.printErrMessage( 253 dhcpbatch.getString("dhcpbatch_usage")); 254 return; 255 } 256 257 dhcpbatch.setInputSource(source); 258 dhcpbatch.execute(); 259 260 } // main 261 262 } // DhcpBatch