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 2001-2002 Sun Microsystems, Inc. All rights reserved. 26 * Use is subject to license terms. 27 */ 28 package com.sun.dhcpmgr.cli.dhtadm; 29 30 import com.sun.dhcpmgr.cli.common.*; 31 import com.sun.dhcpmgr.server.*; 32 33 import java.lang.IllegalArgumentException; 34 import java.text.MessageFormat; 35 36 /** 37 * This class represents the entry point to the DHCP CLI dhcptab 38 * administration. 39 */ 40 public class DhtAdm 41 extends DhcpCliProgram { 42 43 /** 44 * The program signature. 45 */ 46 public static final String SIGNATURE = "dhtadm: "; 47 48 /** 49 * The valid options for all DhtAdm administration. 50 */ 51 private static String optString = "ACDIMPRvgr:p:u:s:m:n:e:d:B;"; 52 53 public static final int ADD_ENTRY = 'A'; 54 public static final int MODIFY_ENTRY = 'M'; 55 public static final int DELETE_ENTRY = 'D'; 56 public static final int CREATE_TABLE = 'C'; 57 public static final int REMOVE_TABLE = 'R'; 58 public static final int DISPLAY_TABLE = 'P'; 59 public static final int BATCH_EXECUTION = 'B'; 60 61 public static final int MACRONAME = 'm'; 62 public static final int SYMBOLNAME = 's'; 63 public static final int NEWNAME = 'n'; 64 public static final int DEFINITION = 'd'; 65 public static final int EDITSYMBOL = 'e'; 66 public static final int RESOURCE = 'r'; 67 public static final int RESOURCE_CONFIG = 'u'; 68 public static final int PATH = 'p'; 69 public static final int VERBOSE = 'v'; 70 public static final int SIGHUP = 'g'; 71 72 /** 73 * Constructs a dhtadm command. 74 * @param args the options to the command. 75 */ 76 public DhtAdm(String [] args) { 77 78 reset(args); 79 // Set the options. 80 // 81 options = new DhcpCliOptions(); 82 this.args = args; 83 84 } // constructor 85 86 /** 87 * Resets a DhtAdm for reuse. Used by DhcpBatch program. 88 * @param args the options to the command. 89 */ 90 public void reset(String [] args) { 91 92 clearFunction(); 93 options = new DhcpCliOptions(); 94 this.args = args; 95 96 } 97 98 /** 99 * Returns the manpage signature for the program. 100 * @return the manpage signature for the program. 101 */ 102 public String getManPage() { 103 return "dhtadm(1M)"; 104 } 105 106 /** 107 * Displays program usage. 108 */ 109 public void usage() { 110 111 DhcpCliPrint.printErrMessage(getString("usage")); 112 113 } // usage 114 115 /** 116 * Executes the program function. 117 * @return SUCCESS, EXISTS, ENOENT, WARNING, or CRITICAL 118 */ 119 public int execute() { 120 121 int returnCode = SUCCESS; 122 123 // Get the options and go exec the correct function. 124 // 125 GetOpt getopt = new GetOpt(args, optString); 126 try { 127 int option; 128 while ((option = getopt.getNextOption()) != -1) { 129 processArg(option, getopt.getOptionArg()); 130 } 131 132 int lastIndex = getopt.getNextOptionIndex(); 133 if (args.length != lastIndex) { 134 Object [] arguments = new Object[1]; 135 arguments[0] = args[lastIndex]; 136 MessageFormat form = 137 new MessageFormat(getString("invalid_arg")); 138 throw new IllegalArgumentException(form.format(arguments)); 139 } 140 141 if (function == null) { 142 String msg = getString("no_function_error"); 143 throw new IllegalArgumentException(msg); 144 } 145 146 // Check the validity of the data store version. 147 // 148 if (!function.isVersionValid(false)) { 149 return (CRITICAL); 150 } 151 152 // Create a DHCP datastore object with the user specified objects. 153 // 154 function.setDhcpDatastore(options.valueOf(RESOURCE), 155 options.valueOf(PATH), options.valueOf(RESOURCE_CONFIG)); 156 157 function.setOptions(options); 158 function.setStandardOptions(); 159 returnCode = function.execute(); 160 161 } catch (IllegalArgumentException e) { 162 StringBuffer msg = new StringBuffer(SIGNATURE); 163 msg.append(DhcpCliFunction.getMessage(e)); 164 DhcpCliPrint.printErrMessage(msg.toString()); 165 DhcpCliPrint.printErrMessage(""); 166 usage(); 167 returnCode = CRITICAL; 168 } catch (Throwable e) { 169 StringBuffer msg = new StringBuffer(SIGNATURE); 170 msg.append(DhcpCliFunction.getMessage(e)); 171 DhcpCliPrint.printErrMessage(msg.toString()); 172 returnCode = CRITICAL; 173 } 174 175 // Signal server if requested by user and main operation successful 176 if (returnCode == SUCCESS && options.isSet(SIGHUP)) { 177 try { 178 DhcpMgr dhcpMgr = new DhcpMgrImpl(); 179 dhcpMgr.getDhcpServiceMgr().reload(); 180 } catch (Throwable e) { 181 returnCode = WARNING; 182 // Display warning 183 StringBuffer msg = new StringBuffer(SIGNATURE); 184 msg.append(getString("sighup_failed")); 185 DhcpCliPrint.printErrMessage(msg.toString()); 186 } 187 } 188 189 return (returnCode); 190 191 } // execute 192 193 /** 194 * Processes one program argument. 195 * @param option the option flag 196 * @param value the option value(if any) 197 * @exception IllegalArgumentException if an invalid argument was entered 198 */ 199 public void processArg(int option, String value) 200 throws IllegalArgumentException { 201 202 switch (option) { 203 204 case ADD_ENTRY: 205 setFunction(new AddEntry()); 206 break; 207 case MODIFY_ENTRY: 208 setFunction(new ModifyEntry()); 209 break; 210 case DELETE_ENTRY: 211 setFunction(new DeleteEntry()); 212 break; 213 case CREATE_TABLE: 214 setFunction(new CreateTable()); 215 break; 216 case REMOVE_TABLE: 217 setFunction(new RemoveTable()); 218 break; 219 case DISPLAY_TABLE: 220 setFunction(new DisplayTable()); 221 break; 222 case BATCH_EXECUTION: 223 setFunction(new DhtAdmBatch(value)); 224 break; 225 default: 226 options.setOption(option, value); 227 } 228 229 } // processArg 230 231 /** 232 * Returns a localized string for this function 233 * @param key the resource bundle string identifier 234 * @return string from resource bundle. 235 */ 236 public String getString(String key) { 237 238 return ResourceStrings.getString(key); 239 240 } // getString 241 242 /** 243 * The entry point for the program. 244 * @param args the program arguments 245 */ 246 public static void main(String[] args) { 247 248 DhtAdm dhtadm = new DhtAdm(args); 249 int returnCode = DhtAdm.CRITICAL; 250 if (dhtadm.isValidUser()) { 251 returnCode = dhtadm.execute(); 252 } 253 System.exit(returnCode); 254 255 } // main 256 257 } // DhtAdm