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 (c) 2001 by Sun Microsystems, Inc.
26 * All rights reserved.
27 */
28 package com.sun.dhcpmgr.cli.dhcpbatch;
29
30 import com.sun.dhcpmgr.bridge.BridgeException;
31 import com.sun.dhcpmgr.cli.common.DhcpCliPrint;
32 import com.sun.dhcpmgr.cli.common.DhcpCliFunction;
33 import com.sun.dhcpmgr.cli.common.DhcpCliProgram;
34 import com.sun.dhcpmgr.cli.dhtadm.DhtAdm;
35 import com.sun.dhcpmgr.cli.pntadm.PntAdm;
36
37 /**
38 * This class represents a DHCP CLI command.
39 */
40 public class DhcpCommand {
41
42 /**
43 * The dhtadm CLI command.
44 */
45 public static String DHTADM = "dhtadm";
46 public static DhtAdm dhtadm = null;
47
48 /**
49 * The pntadm CLI command.
50 */
51 public static String PNTADM = "pntadm";
52 public static PntAdm pntadm = null;
53
54 private String [] arglist;
55
56 /**
57 * The constructor for a DhcpCommand.
58 */
59 public DhcpCommand() {
60 // nothing to do.
61 } // constructor
62
63 /**
64 * Sets the arglist for the command.
65 * @param input the batch input line.
66 */
67 public void init(String input) throws BridgeException {
68 arglist = DhcpCliFunction.getSvcMgr().getArguments(input);
69 } // reset
70
71 /**
72 * Returns a localized string for this function
73 * @param key the resource bundle string identifier
74 * @return string from resource bundle.
75 */
76 public String getString(String key) {
77 return ResourceStrings.getString(key);
78 } // getString
79
80 /**
81 * Get the name of the CLI program.
82 * @return the name of the CLI for this command.
83 */
84 public String getProgram() {
85
86 String program = null;
87 if (arglist.length != 0) {
88 program = arglist[0];
89 }
90
91 return program;
92
93 } // getProgram
94
95 /**
96 * Returns the arguments for the CLI.
97 * @return the arguments.
98 */
99 public String [] getArgs() {
100
101 String [] args = new String[arglist.length - 1];
102
103 System.arraycopy(arglist, 1, args, 0, arglist.length - 1);
104 return args;
105
106 } // getArgs
107
108 /**
109 * Executes the CLI.
110 * @return return code as returned by caller.
111 */
112 public int execute() {
113 String program = getProgram();
114 String [] args = getArgs();
115 int returnCode = DhcpCliProgram.SUCCESS;
116
117 if (program == null) {
118 Object [] arguments = new Object[1];
119 arguments[0] = program;
120 DhcpBatch.printErrMessage(getString("dhcpcommand_invalid_command"),
121 arguments);
122 } else if (program.equals(PNTADM)) {
123 if (pntadm == null) {
124 pntadm = new PntAdm(args);
125 } else {
126 pntadm.reset(args);
127 }
128 returnCode = pntadm.execute();
129 } else if (program.equals(DHTADM)) {
130 if (dhtadm == null) {
131 dhtadm = new DhtAdm(args);
132 } else {
133 dhtadm.reset(args);
134 }
135 returnCode = dhtadm.execute();
136 } else {
137 Object [] arguments = new Object[1];
138 arguments[0] = program;
139 DhcpBatch.printErrMessage(getString("dhcpcommand_invalid_command"),
140 arguments);
141 returnCode = DhcpCliProgram.FAILURE;
142 }
143 return (returnCode);
144 } // execute
145
146 } // DhcpCommand