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.DhcpCliFunction;
31 import com.sun.dhcpmgr.data.DhcptabRecord;
32 import com.sun.dhcpmgr.data.Macro;
33 import com.sun.dhcpmgr.data.Option;
34 import com.sun.dhcpmgr.bridge.NoEntryException;
35
36 import java.lang.IllegalArgumentException;
37
38 /**
39 * The main class for the "modify entry" functionality of dhtadm.
40 */
41 public class ModifyEntry extends DhtAdmFunction {
42
43 /**
44 * The valid options associated with modifying an entry.
45 */
46 static final int supportedOptions[] = {
47 DhtAdm.MACRONAME,
48 DhtAdm.SYMBOLNAME,
49 DhtAdm.NEWNAME,
50 DhtAdm.DEFINITION,
51 DhtAdm.EDITSYMBOL,
52 DhtAdm.RESOURCE,
53 DhtAdm.RESOURCE_CONFIG,
54 DhtAdm.PATH,
55 DhtAdm.SIGHUP
56 };
57
58 /**
59 * Constructs a ModifyEntry object.
60 */
61 public ModifyEntry() {
62
63 validOptions = supportedOptions;
64
65 } // constructor
66
67 /**
68 * Returns the option flag for this function.
69 * @returns the option flag for this function.
70 */
71 public int getFunctionFlag() {
72 return (DhtAdm.MODIFY_ENTRY);
73 }
74
75 /**
76 * Executes the "modify entry" functionality.
77 * @return DhtAdm.SUCCESS, DhtAdm.ENOENT, DhtAdm.WARNING, or
78 * DhtAdm.CRITICAL
79 */
80 public int execute()
81 throws IllegalArgumentException {
82
83 int returnCode = DhtAdm.SUCCESS;
84
85 // Get macro or symbol name. One and only one should be set.
86 //
87 String macroName = options.valueOf(DhtAdm.MACRONAME);
88 String symbolName = options.valueOf(DhtAdm.SYMBOLNAME);
89
90 if (macroName != null && symbolName != null) {
91 String msg = getString("two_keys_error");
92 throw new IllegalArgumentException(msg);
93 }
94
95 if (macroName == null && symbolName == null) {
96 String msg = getString("no_keys_error");
97 throw new IllegalArgumentException(msg);
98 }
99
100 // Get the modify "sub-functions"
101 // One and only one of the options should be set.
102 //
103 int count = 0;
104 String newName = options.valueOf(DhtAdm.NEWNAME);
105 if (newName != null) {
106 count++;
107 }
108
109 String definition = options.valueOf(DhtAdm.DEFINITION);
110 if (definition != null) {
111 count++;
112 }
113
114 String editSymbol = options.valueOf(DhtAdm.EDITSYMBOL);
115 if (editSymbol != null) {
116 if (symbolName != null) {
117 printErrMessage(getString("symbol_edit_error"));
118 return (DhtAdm.CRITICAL);
119 }
120 count++;
121 }
122
123 if (count != 1) {
124 printErrMessage(getString("modify_no_function_error"));
125 return (DhtAdm.CRITICAL);
126 }
127
128 // Get macro or symbol name. One and only one should be set.
129 // Then go get the instance of the macro or symbol.
130 //
131 try {
132 DhcptabRecord oldDhcptabRecord = null;
133 if (macroName != null) {
134 oldDhcptabRecord =
135 getDhcptabMgr().getMacro(macroName, getDhcpDatastore());
136 } else if (symbolName != null) {
137 oldDhcptabRecord =
138 getDhcptabMgr().getOption(symbolName,
139 getDhcpDatastore());
140 } else {
141 printErrMessage(getString("internal_error"));
142 return (DhtAdm.CRITICAL);
143 }
144
145 // Identify the function and create the newDhcptabRecord
146 // given the function.
147 //
148 DhcptabRecord newDhcptabRecord = null;
149
150 if (definition != null) {
151 if (macroName != null) {
152 Macro newMacro = new Macro(macroName);
153 newMacro.setValue(definition, false, true);
154 newDhcptabRecord = newMacro;
155 } else if (symbolName != null) {
156 newDhcptabRecord =
157 getDhcptabMgr().createOption(symbolName, definition);
158 } else {
159 printErrMessage(getString("internal_error"));
160 return (DhtAdm.CRITICAL);
161 }
162
163
164 } else if (newName != null) {
165 definition = oldDhcptabRecord.getValue().toString();
166 if (macroName != null) {
167 Macro newMacro = new Macro(newName);
168 newMacro.setValue(definition, false, true);
169 newDhcptabRecord = newMacro;
170 } else if (symbolName != null) {
171 newDhcptabRecord =
172 getDhcptabMgr().createOption(newName, definition);
173 } else {
174 printErrMessage(getString("internal_error"));
175 return (DhtAdm.CRITICAL);
176 }
177
178 } else if (editSymbol != null) {
179
180 Macro oldMacro = (Macro)oldDhcptabRecord;
181 Macro newMacro = (Macro)oldMacro.clone();
182 newMacro.editOption(editSymbol);
183 newDhcptabRecord = newMacro;
184
185 } else {
186 printErrMessage(getString("internal_error"));
187 return (DhtAdm.CRITICAL);
188 }
189
190 getDhcptabMgr().modifyRecord(oldDhcptabRecord, newDhcptabRecord,
191 false, getDhcpDatastore());
192 } catch (NoEntryException e) {
193 printErrMessage(getMessage(e));
194 returnCode = DhtAdm.ENOENT;
195 } catch (Throwable e) {
196 printErrMessage(getMessage(e));
197 returnCode = DhtAdm.WARNING;
198 }
199
200 return (returnCode);
201
202 } // execute
203
204 } // ModifyEntry