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.common;
29
30 import java.text.MessageFormat;
31
32 /**
33 * This class is the base class extended by the DHCP CLI program classes.
34 */
35 public abstract class DhcpCliProgram {
36
37 /**
38 * Defines the functionality to be performed.
39 */
40 protected DhcpCliFunction function = null;
41 protected DhcpCliOptions options = null;
42
43 /**
44 * Program arguments.
45 */
46 protected String [] args = null;
47
48 /**
49 * Return codes.
50 */
51 public static final int FAILURE = -1;
52 public static final int SUCCESS = 0;
53 public static final int EXISTS = 1;
54 public static final int ENOENT = 2;
55 public static final int WARNING = 3;
56 public static final int CRITICAL = 4;
57
58 /**
59 * Returns the manpage signature for the program.
60 * @return the manpage signature for the program.
61 */
62 public abstract String getManPage();
63
64 /**
65 * Uninitializes the program function.
66 */
67 protected void clearFunction() {
68 function = null;
69 }
70
71 /**
72 * Sets the function that the user has requested.
73 * @param function user requested function.
74 */
75 protected void setFunction(DhcpCliFunction function)
76 throws IllegalArgumentException {
77
78 if (this.function != null) {
79 Object [] args = new Object[2];
80 args[0] = DhcpCliOption.getOptionCharacter(
81 this.function.getFunctionFlag());
82 args[1] = DhcpCliOption.getOptionCharacter(
83 function.getFunctionFlag());
84 String msg = ResourceStrings.getString("multiple_functions");
85 MessageFormat form = new MessageFormat(msg);
86 throw new IllegalArgumentException(form.format(args));
87 }
88
89 this.function = function;
90 }
91
92 /**
93 * Checks to see if the user has permission to run the program.
94 * @return true if the user has permission to execute, false otherwise.
95 */
96 protected boolean isValidUser() {
97
98 // Must be root to run.
99 //
100 if (!System.getProperty("user.name").equals("root")) {
101
102 Object [] args = new Object[1];
103 args[0] = getManPage();
104 String msg = ResourceStrings.getString("user_not_allowed");
105 MessageFormat form = new MessageFormat(msg);
106 DhcpCliPrint.printErrMessage(form.format(args));
107
108 return false;
109 }
110 return true;
111
112 } // isValidUser
113
114 } // DhcpCliProgram