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.data.OptionsTable;
35 import com.sun.dhcpmgr.bridge.ExistsException;
36
37 import java.lang.IllegalArgumentException;
38
39 /**
40 * The main class for the "add entry" functionality of dhtadm.
41 */
42 public class AddEntry extends DhtAdmFunction {
43
44 /**
45 * The valid options associated with adding an entry.
46 */
47 static final int supportedOptions[] = {
48 DhtAdm.MACRONAME,
49 DhtAdm.SYMBOLNAME,
50 DhtAdm.DEFINITION,
51 DhtAdm.RESOURCE,
52 DhtAdm.RESOURCE_CONFIG,
53 DhtAdm.PATH,
54 DhtAdm.SIGHUP
55 };
56
57 /**
58 * Constructs a AddEntry object.
59 */
60 public AddEntry() {
61
62 validOptions = supportedOptions;
63
64 } // constructor
65
66 /**
67 * Returns the option flag for this function.
68 * @returns the option flag for this function.
69 */
70 public int getFunctionFlag() {
71 return (DhtAdm.ADD_ENTRY);
72 }
73
74 /**
75 * Executes the "add entry" functionality.
76 * @return DhtAdm.SUCCESS, DhtAdm.EXISTS, DhtAdm.WARNING, or
77 * DhtAdm.CRITICAL
78 */
79 public int execute()
80 throws IllegalArgumentException {
81
82 int returnCode = DhtAdm.SUCCESS;
83
84 // Get macro or symbol name. One and only one should be set.
85 //
86 String macroName = options.valueOf(DhtAdm.MACRONAME);
87 String symbolName = options.valueOf(DhtAdm.SYMBOLNAME);
88
89 if (macroName != null && symbolName != null) {
90 String msg = getString("two_keys_error");
91 throw new IllegalArgumentException(msg);
92 }
93
94 if (macroName == null && symbolName == null) {
95 String msg = getString("no_keys_error");
96 throw new IllegalArgumentException(msg);
97 }
98
99 // Get the definition. It's an error if it does not exist.
100 //
101 String definition = options.valueOf(DhtAdm.DEFINITION);
102 if (definition == null) {
103 String msg = getString("no_definition_error");
104 throw new IllegalArgumentException(msg);
105 }
106
107 // Create a DhcptabRecord.
108 //
109 try {
110 DhcptabRecord dhcptabRecord = null;
111 if (macroName != null) {
112 OptionsTable optionsTable = OptionsTable.getTable();
113 optionsTable.add(
114 getDhcptabMgr().getOptions(getDhcpDatastore()));
115 Macro macro = new Macro(macroName);
116 macro.setValue(definition, false, true);
117 dhcptabRecord = macro;
118 } else if (symbolName != null) {
119 Option option =
120 getDhcptabMgr().createOption(symbolName, definition);
121 dhcptabRecord = option;
122 } else {
123 printErrMessage(getString("internal_error"));
124 returnCode = DhtAdm.CRITICAL;
125 }
126
127 // Add the entry.
128 //
129 if (returnCode == DhtAdm.SUCCESS) {
130 getDhcptabMgr().createRecord(dhcptabRecord, false,
131 getDhcpDatastore());
132 }
133 } catch (ExistsException e) {
134 printErrMessage(getMessage(e));
135 returnCode = DhtAdm.EXISTS;
136 } catch (Throwable e) {
137 printErrMessage(getMessage(e));
138 returnCode = DhtAdm.WARNING;
139 }
140
141 return (returnCode);
142
143 } // execute
144
145 } // AddEntry