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