xref: /titanic_41/usr/src/cmd/krb5/kadmin/gui/dataclasses/Flags.java (revision ac88567a7a5bb7f01cf22cf366bc9d6203e24d7a)
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) 1999-2000 by Sun Microsystems, Inc.
26  * All rights reserved.
27  */
28 
29 import java.util.ResourceBundle;
30 import java.util.MissingResourceException;
31 
32 /**
33  * The Flags class stores all flags that might pertain to a
34  * Principal.
35  */
36 
37 //XXX: Move this to a java.util.BitSet model later on.
38 public class Flags {
39 
40     private int flags = 0;
41 
42     private static int allOnes = 0xFFFF;
43 
44     public static final int DISALLOW_POSTDATED = 1;
45     public static final int DISALLOW_FORWARDABLE = 2;
46     public static final int DISALLOW_TGT_BASED = 4;
47     public static final int DISALLOW_RENEWABLE = 8;
48     public static final int DISALLOW_PROXIABLE = 16;
49     public static final int DISALLOW_DUP_SKEY = 32;
50     public static final int DISALLOW_ALL_TIX = 64;
51     public static final int REQUIRE_PRE_AUTH = 128;
52     public static final int REQUIRE_HW_AUTH = 256;
53     public static final int REQUIRES_PWCHANGE = 512;
54     public static final int DISALLOW_SVR = 4096;
55     // public static final int MASK = 65535 - 1024 - 2048 - 32678;
56 
57     private static int bitfields[] = {1, 2, 4, 8, 16, 32, 64, 128, 256, 512,
58   				      4096};
59 
60     private static String flagNames[] = {"Allow Postdated Tickets",
61 				         "Allow Forwardable Tickets",
62 				         "Allow TGT-Based Authentication",
63 				         "Allow Renewable Tickets",
64 				         "Allow Proxiable Tickets",
65 				         "Allow Duplicate Authentication",
66 				         "Disable Account",
67 				         "Require Preauthentication",
68 				         "Require Hardware Preauthentication",
69 				         "Require Password Change",
70 				         "Allow Service Tickets"};
71 
72     // For I18N
73     private static ResourceBundle rb =
74     ResourceBundle.getBundle("GuiResource" /* NOI18N */);
75 
76     /**
77      * Constructor for Flags. Sets all flags to false;
78      */
79     // Required since non-default constructor is used.
80     public Flags() {
81     }
82 
83     /**
84      * Constructor for Flags.
85      * @param flags an integer where the bit positions determined by the
86      * static masks determine the value of that flag.
87      */
88     public Flags(int flags) {
89         this.flags = flags;
90     }
91 
92     /**
93      * Call rb.getString(), but catch exception and return English
94      * key so that small spelling errors don't cripple the GUI
95      *
96      */
97     private static final String getString(String key) {
98         try {
99       	    String res = rb.getString(key);
100 	    return res;
101         } catch (MissingResourceException e) {
102 	    System.out.println("Missing resource "+key+", using English.");
103 	    return key;
104         }
105     }
106 
107     /**
108      * Returns a label for the flag corresponding to the given
109      * bitfield.
110      * @param bitfield an integer chosen from the static list of masks
111      *   in this class to indicate a particular flag.
112      * @return a String containing the label for the flag.
113      */
114     public static final String getLabel(int bitfield) {
115         int pos = getIndex(bitfield);
116         if (pos < 0)
117             return null;
118         else
119             return getString(flagNames[pos]);
120     }
121 
122     /**
123      * Returns the boolean value of the flag corresponding to the given
124      * bitfield.
125      * @param bitfield an integer chosen from the static list of masks
126      *   in this class to indicate a particular flag.
127      * @return the boolean value that the flag is currently set to.
128      */
129     public boolean getFlag(int bitfield) {
130         return !((flags & bitfield) == 0);
131     }
132 
133     /**
134      * Sets the current value of one or more flags.
135      * @param mask an integer mask that has all those bits set that
136      * correspond to flags that need to be set.
137      * @value the boolean value that the flags should be set to.
138      */
139     public void setFlags(int mask, boolean value) {
140         if (!value) {
141             mask ^= allOnes; // invert mask
142             flags &= mask;   // zero out
143         } else {
144             flags |= mask;
145         }
146     }
147 
148     /**
149      * Toggles the current value of one or more flags.
150      * @param mask an integermask that has all those bits set that
151      * correspond to flags that need to be toggled.
152      */
153     public void toggleFlags(int mask) {
154         flags ^= mask;
155     }
156 
157     /**
158      * Returns a string containing all of the flags labels and their
159      * corresponding boolean values.
160      */
161     public String toString() {
162 
163         StringBuffer sb = new StringBuffer();
164         char ch;
165 
166         ch = (!getFlag(DISALLOW_POSTDATED)? '+':'-');
167         sb.append('\t').append(ch).append(getString(flagNames[0])).append('\n');
168 
169         ch = (!getFlag(DISALLOW_FORWARDABLE)? '+':'-');
170         sb.append('\t').append(ch).append(getString(flagNames[1])).append('\n');
171 
172         ch = (!getFlag(DISALLOW_TGT_BASED)? '+':'-');
173         sb.append('\t').append(ch).append(getString(flagNames[2])).append('\n');
174 
175         ch = (!getFlag(DISALLOW_RENEWABLE)? '+':'-');
176         sb.append('\t').append(ch).append(getString(flagNames[3])).append('\n');
177 
178         ch = (!getFlag(DISALLOW_PROXIABLE)? '+':'-');
179         sb.append('\t').append(ch).append(getString(flagNames[4])).append('\n');
180 
181         ch = (!getFlag(DISALLOW_DUP_SKEY)? '+':'-');
182         sb.append('\t').append(ch).append(getString(flagNames[5])).append('\n');
183 
184         ch = (getFlag(DISALLOW_ALL_TIX)? '+':'-');
185         sb.append('\t').append(ch).append(getString(flagNames[6])).append('\n');
186 
187         ch = (getFlag(REQUIRE_PRE_AUTH)? '+':'-');
188         sb.append('\t').append(ch).append(getString(flagNames[7])).append('\n');
189 
190         ch = (getFlag(REQUIRE_HW_AUTH)? '+':'-');
191         sb.append('\t').append(ch).append(getString(flagNames[8])).append('\n');
192 
193         ch = (getFlag(REQUIRES_PWCHANGE)? '+':'-');
194         sb.append('\t').append(ch).append(getString(flagNames[9])).append('\n');
195 
196         ch = (!getFlag(DISALLOW_SVR)? '+':'-');
197         sb.append('\t').append(ch).append(getString(flagNames[10])).append(
198             '\n');
199 
200         return sb.toString();
201     }
202 
203     /**
204      * Converts a bitfield with one bit set in it to an index.
205      * The index can be used with bitfields or flagNames.
206      * @flagBitfield an integer that has exactly one bit set in it
207      * @return the index of the first bit that was found set when
208      *  scanning from the lsb.
209      */
210     // This is not always the position of the bit in the integer's
211     // internal representation.
212     private static int getIndex(int flagBitfield) {
213         for (int i = 0; i < flagNames.length; i++) {
214             if (flagBitfield == bitfields[i])
215     	        return i;
216         }
217 
218         return -1;
219     }
220 
221     /**
222      * Returns an integer with the bits indicating the status of each of
223      * the flags.
224      */
225     public int getBits() {
226         return flags;
227     }
228 
229 }
230