File: InitCAParameters.java

package info (click to toggle)
gridengine 8.1.9%2Bdfsg-10
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 56,880 kB
  • sloc: ansic: 432,689; java: 87,068; cpp: 31,958; sh: 29,429; jsp: 7,757; perl: 6,336; xml: 5,828; makefile: 4,701; csh: 3,928; ruby: 2,221; tcl: 1,676; lisp: 669; yacc: 519; python: 503; lex: 361; javascript: 200
file content (364 lines) | stat: -rw-r--r-- 12,379 bytes parent folder | download | duplicates (9)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
/*___INFO__MARK_BEGIN__*/
/*************************************************************************
 *
 *  The Contents of this file are made available subject to the terms of
 *  the Sun Industry Standards Source License Version 1.2
 *
 *  Sun Microsystems Inc., March, 2001
 *
 *
 *  Sun Industry Standards Source License Version 1.2
 *  =================================================
 *  The contents of this file are subject to the Sun Industry Standards
 *  Source License Version 1.2 (the "License"); You may not use this file
 *  except in compliance with the License. You may obtain a copy of the
 *  License at http://gridengine.sunsource.net/Gridengine_SISSL_license.html
 *
 *  Software provided under this License is provided on an "AS IS" basis,
 *  WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING,
 *  WITHOUT LIMITATION, WARRANTIES THAT THE SOFTWARE IS FREE OF DEFECTS,
 *  MERCHANTABLE, FIT FOR A PARTICULAR PURPOSE, OR NON-INFRINGING.
 *  See the License for the specific provisions governing your rights and
 *  obligations concerning the Software.
 *
 *   The Initial Developer of the Original Code is: Sun Microsystems, Inc.
 *
 *   Copyright: 2001 by Sun Microsystems, Inc.
 *
 *   All Rights Reserved.
 *
 ************************************************************************/
/*___INFO__MARK_END__*/
package com.sun.grid.ca;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.MissingResourceException;
import javax.security.auth.callback.Callback;
import javax.security.auth.callback.CallbackHandler;
import javax.security.auth.callback.NameCallback;
import javax.security.auth.callback.TextOutputCallback;
import javax.security.auth.callback.UnsupportedCallbackException;

/**
 * This class hold the parameters for Certificate Authority
 */
public class InitCAParameters implements java.io.Serializable {
    
    /**
     *  Defines a parameter for the init ca command.
     */
    public static class ParamDef {
        
        private final static char [] FORBIDDEN_LETTERS = {
            '"', '\''
        };
        
        private String name;
        private Integer minLen;
        private Integer maxLen;
        
        private ParamDef(String name) {
            this.name = name;
            minLen = new Integer(RB.getString("initCAParam." + name + ".minLen"));
            maxLen = new Integer(RB.getString("initCAParam." + name + ".maxLen"));
        }
        
        /**
         * Get name of this parameter
         * @return name of this parameter
         */
        public String getName() {
            return name;
        }
        
        /**
         *  Get the localized name of this parameter
         *
         *  @return the localized name of this parameter
         */
        public String getLocalizedName() {
            return RB.getString("initCAParam." + name + ".name");
        }
        
        /**
         * Get the description of this parameter
         * @return the description
         */
        public String getDescription() {
            try {
                return RB.getString("initCAParam." + name + ".description");
            } catch(MissingResourceException ex) {
                return null;
            }
        }
        
        /**
         * Get the default value of this param
         * @return the default value of this param
         */
        public String getDefaultValue() {
            try {
                return RB.getString("initCAParam." + name + ".default");
            } catch(MissingResourceException ex) {
                return null;
            }
        }
        
        /**
         * Get the minimum number of character for this parameter
         * @return minimum number of character for this parameter
         */
        public Integer getMinLen() {
            return minLen;
        }
        
        /**
         * Get the maximum number of characters for this parameter
         * @return maximum number of characters for this parameter
         */
        public Integer getMaxLen() {
            return maxLen;
        }
        
        private NameCallback createCallback() {
            
            String description = getDescription();
            String prompt = null;
            if(description == null) {
                prompt = RB.getString("initCAParam.prompt", new Object[] { getLocalizedName() });
            } else {
                prompt = RB.getString("initCAParam.promptWithDescription",
                        new Object [] { getLocalizedName(), description });
                
            }
            String defaultValue = getDefaultValue();
            
            if(defaultValue == null) {
                return new NameCallback(prompt);
            } else {
                return new NameCallback(prompt, defaultValue);
            }
        }
        
        /**
         * Check the validity of a param value
         * @param value 
         * @throws com.sun.grid.ca.GridCAException 
         */
        public void check(String value) throws GridCAException {
            if(value == null) {
                throw RB.newGridCAException("initCAParam.empty", name);
            }
            if(value.length() < minLen.intValue()) {
                throw RB.newGridCAException("initCAParam.tooShort", new Object [] { getLocalizedName(), value, minLen });
            }
            if(value.length() > maxLen.intValue()) {
                throw RB.newGridCAException("initCAParam.tooLong", new Object [] { getLocalizedName(), value, maxLen });
            }
            
            for(int i = 0; i < FORBIDDEN_LETTERS.length; i++) {
                if(value.indexOf(FORBIDDEN_LETTERS[i]) >= 0) {
                    throw RB.newGridCAException("gridCAParam.forbiddenLetter",
                            new Object [] { getLocalizedName(), value, new Character(FORBIDDEN_LETTERS[i]) });
                }
            }
        }
        
        public boolean equals(Object obj) {
            return obj instanceof ParamDef &&
                    name.equals(((ParamDef)obj).name);
        }
        
        public int hashCode() {
            return name.hashCode();
        }
        
    }
    
    /** Definition of the country parameter */
    public final static ParamDef COUNTRY = new ParamDef("country");
    
    /** Definition of the state parameter */
    public final static ParamDef STATE = new ParamDef("state");
    
    /** Definition of the location parameter */
    public final static ParamDef LOCATION = new ParamDef("location");
    
    /** Definition of the organisation parameter */
    public final static ParamDef ORG = new ParamDef("organisation");
    
    /** Definition of the organisation unit parameter */
    public final static ParamDef ORG_UNIT = new ParamDef("organisationUnit");
    
    /** Definition of the admin email address parameter */
    public final static ParamDef ADMIN_EMAIL = new ParamDef("adminEmailAddress");
    
    private final static ParamDef [] PARAMS = {
        COUNTRY, STATE, LOCATION, ORG, ORG_UNIT, ADMIN_EMAIL
    };
    
    private Map values = new HashMap();
    
    public InitCAParameters() {
        
    }
    
    /**
     * Query the values for a init ca command.
     *
     * @param callbackHandler   the callback handler which is used to query the values
     * @throws java.io.IOException if the callback handler throws a IOException
     * @throws javax.security.auth.callback.UnsupportedCallbackException if the the callback handler does not 
     *            support <code>NameCallback</code> or <code>TextOutputCallback</code>
     * @return the <code>InitCAParameters</code> object with the queried values
     */
    public static InitCAParameters queryNewInstance(CallbackHandler callbackHandler) throws IOException, UnsupportedCallbackException {
        
        InitCAParameters params = new InitCAParameters();
        
        Callback [] cb = new Callback[1];
        Callback [] ecb = new Callback[1];
        for(int i = 0; i < PARAMS.length; i++) {
            cb[0] = PARAMS[i].createCallback();
           do {
                callbackHandler.handle(cb);
                try {
                    params.setValue(PARAMS[i], ((NameCallback)cb[0]).getName());
                    break;
                } catch (GridCAException ex) {
                    ecb[0] = new TextOutputCallback(TextOutputCallback.ERROR, ex.getLocalizedMessage());
                    callbackHandler.handle(ecb);
                }
           } while(true);
           
            
        }
        return params;
    }
    
    
    private String getValue(ParamDef param) {
        return (String)values.get(param);
    }
    
    private void setValue(ParamDef param, String value) throws GridCAException {
        param.check(value);
        values.put(param, value);
    }
    
    /**
     * Get the country of the ca.
     * @return the country of the ca
     */
    public String getCountry() {
        return getValue(COUNTRY);
    }
    
    /**
     * Set the country of the ca (e.g. Germany)
     * @param country country of the ca (exact two characters)
     * @throws com.sun.grid.ca.GridCAException if <code>country</code> is not valid
     */
    public void setCountry(String country) throws GridCAException {
        setValue(COUNTRY, country);
    }
    
    /**
     * Get the state of the ca.
     * @return the state of the ca
     */
    public String getState() {
        return getValue(STATE);
    }
    
    /**
     * Set the state of the ca (e.g. Bayern)
     * @param state state of the ca (at least one char)
     * @throws com.sun.grid.ca.GridCAException if <code>state</code> is not valid
     */
    public void setState(String state) throws GridCAException {
        setValue(STATE, state);
    }
    
    /**
     * Get the location of the ca.
     * @return the location of the ca
     */
    public String getLocation() {
        return getValue(LOCATION);
    }
    
    /**
     * Set the location of the ca (e.g. city)
     * @param location location of the ca (at least one char)
     * @throws com.sun.grid.ca.GridCAException if <code>location</code> is not valid
     */
    public void setLocation(String location) throws GridCAException {
        setValue(LOCATION, location);
    }
    
    /**
     * Get the organization of the ca.
     * @return the organization of the ca
     */
    public String getOrganization() {
        return getValue(ORG);
    }
    
    /**
     * Set the organization of the ca.
     * @param organization organization of the ca (at least one char)
     * @throws com.sun.grid.ca.GridCAException if <code>organization</code> is not valid
     */
    public void setOrganization(String organization) throws GridCAException {
        setValue(ORG, organization);
    }
    
    /**
     * Get the organization unit of the ca.
     * @return the organization unit of the ca
     */
    public String getOrganizationUnit() {
        return getValue(ORG_UNIT);
    }
    
    /**
     * Set the organization unit of the ca.
     * @param organizationUnit organization unit of the ca (at least one char)
     * @throws com.sun.grid.ca.GridCAException if <code>organizationUnit</code> is not valid
     */
    public void setOrganizationUnit(String organizationUnit) throws GridCAException {
        setValue(ORG_UNIT, organizationUnit);
    }
    
    /**
     * Get the email address of the ca adminstrator
     * @return the email address of the ca adminstrator
     */
    public String getAdminEmailAddress() {
        return getValue(ADMIN_EMAIL);
    }
    
    /**
     * Set the email address of the ca adminstrator
     * @param adminEmailAddress email address of the ca adminstrator (at least one char)
     * @throws com.sun.grid.ca.GridCAException if <code>adminEmailAddress</code> is not valid
     */
    public void setAdminEmailAddress(String adminEmailAddress) throws GridCAException {
        setValue(ADMIN_EMAIL, adminEmailAddress);
    }
    
    /**
     * Validate the <code>InitCAParameters</code>
     * @throws com.sun.grid.ca.GridCAException  if the parameters are not valid
     */
    public void validate() throws GridCAException {
        for(int i = 0; i < PARAMS.length; i++) {
            PARAMS[i].check(getValue(PARAMS[i]));
        }
    }
    
    
}