~ubuntu-branches/ubuntu/utopic/gridengine/utopic

« back to all changes in this revision

Viewing changes to source/libs/juti/java/com/sun/grid/ca/InitCAParameters.java

  • Committer: Bazaar Package Importer
  • Author(s): Mark Hymers
  • Date: 2008-06-25 22:36:13 UTC
  • Revision ID: james.westby@ubuntu.com-20080625223613-tvd9xlhuoct9kyhm
Tags: upstream-6.2~beta2
ImportĀ upstreamĀ versionĀ 6.2~beta2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*___INFO__MARK_BEGIN__*/
 
2
/*************************************************************************
 
3
 *
 
4
 *  The Contents of this file are made available subject to the terms of
 
5
 *  the Sun Industry Standards Source License Version 1.2
 
6
 *
 
7
 *  Sun Microsystems Inc., March, 2001
 
8
 *
 
9
 *
 
10
 *  Sun Industry Standards Source License Version 1.2
 
11
 *  =================================================
 
12
 *  The contents of this file are subject to the Sun Industry Standards
 
13
 *  Source License Version 1.2 (the "License"); You may not use this file
 
14
 *  except in compliance with the License. You may obtain a copy of the
 
15
 *  License at http://gridengine.sunsource.net/Gridengine_SISSL_license.html
 
16
 *
 
17
 *  Software provided under this License is provided on an "AS IS" basis,
 
18
 *  WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING,
 
19
 *  WITHOUT LIMITATION, WARRANTIES THAT THE SOFTWARE IS FREE OF DEFECTS,
 
20
 *  MERCHANTABLE, FIT FOR A PARTICULAR PURPOSE, OR NON-INFRINGING.
 
21
 *  See the License for the specific provisions governing your rights and
 
22
 *  obligations concerning the Software.
 
23
 *
 
24
 *   The Initial Developer of the Original Code is: Sun Microsystems, Inc.
 
25
 *
 
26
 *   Copyright: 2001 by Sun Microsystems, Inc.
 
27
 *
 
28
 *   All Rights Reserved.
 
29
 *
 
30
 ************************************************************************/
 
31
/*___INFO__MARK_END__*/
 
32
package com.sun.grid.ca;
 
33
 
 
34
import java.io.IOException;
 
35
import java.util.HashMap;
 
36
import java.util.Map;
 
37
import java.util.MissingResourceException;
 
38
import javax.security.auth.callback.Callback;
 
39
import javax.security.auth.callback.CallbackHandler;
 
40
import javax.security.auth.callback.NameCallback;
 
41
import javax.security.auth.callback.TextOutputCallback;
 
42
import javax.security.auth.callback.UnsupportedCallbackException;
 
43
 
 
44
/**
 
45
 * This class hold the parameters for Certificate Authority
 
46
 */
 
47
public class InitCAParameters implements java.io.Serializable {
 
48
    
 
49
    /**
 
50
     *  Defines a parameter for the init ca command.
 
51
     */
 
52
    public static class ParamDef {
 
53
        
 
54
        private final static char [] FORBIDDEN_LETTERS = {
 
55
            '"', '\''
 
56
        };
 
57
        
 
58
        private String name;
 
59
        private Integer minLen;
 
60
        private Integer maxLen;
 
61
        
 
62
        private ParamDef(String name) {
 
63
            this.name = name;
 
64
            minLen = new Integer(RB.getString("initCAParam." + name + ".minLen"));
 
65
            maxLen = new Integer(RB.getString("initCAParam." + name + ".maxLen"));
 
66
        }
 
67
        
 
68
        /**
 
69
         * Get name of this parameter
 
70
         * @return name of this parameter
 
71
         */
 
72
        public String getName() {
 
73
            return name;
 
74
        }
 
75
        
 
76
        /**
 
77
         *  Get the localized name of this parameter
 
78
         *
 
79
         *  @return the localized name of this parameter
 
80
         */
 
81
        public String getLocalizedName() {
 
82
            return RB.getString("initCAParam." + name + ".name");
 
83
        }
 
84
        
 
85
        /**
 
86
         * Get the description of this parameter
 
87
         * @return the description
 
88
         */
 
89
        public String getDescription() {
 
90
            try {
 
91
                return RB.getString("initCAParam." + name + ".description");
 
92
            } catch(MissingResourceException ex) {
 
93
                return null;
 
94
            }
 
95
        }
 
96
        
 
97
        /**
 
98
         * Get the default value of this param
 
99
         * @return the default value of this param
 
100
         */
 
101
        public String getDefaultValue() {
 
102
            try {
 
103
                return RB.getString("initCAParam." + name + ".default");
 
104
            } catch(MissingResourceException ex) {
 
105
                return null;
 
106
            }
 
107
        }
 
108
        
 
109
        /**
 
110
         * Get the minimum number of character for this parameter
 
111
         * @return minimum number of character for this parameter
 
112
         */
 
113
        public Integer getMinLen() {
 
114
            return minLen;
 
115
        }
 
116
        
 
117
        /**
 
118
         * Get the maximum number of characters for this parameter
 
119
         * @return maximum number of characters for this parameter
 
120
         */
 
121
        public Integer getMaxLen() {
 
122
            return maxLen;
 
123
        }
 
124
        
 
125
        private NameCallback createCallback() {
 
126
            
 
127
            String description = getDescription();
 
128
            String prompt = null;
 
129
            if(description == null) {
 
130
                prompt = RB.getString("initCAParam.prompt", new Object[] { getLocalizedName() });
 
131
            } else {
 
132
                prompt = RB.getString("initCAParam.promptWithDescription",
 
133
                        new Object [] { getLocalizedName(), description });
 
134
                
 
135
            }
 
136
            String defaultValue = getDefaultValue();
 
137
            
 
138
            if(defaultValue == null) {
 
139
                return new NameCallback(prompt);
 
140
            } else {
 
141
                return new NameCallback(prompt, defaultValue);
 
142
            }
 
143
        }
 
144
        
 
145
        /**
 
146
         * Check the validity of a param value
 
147
         * @param value 
 
148
         * @throws com.sun.grid.ca.GridCAException 
 
149
         */
 
150
        public void check(String value) throws GridCAException {
 
151
            if(value == null) {
 
152
                throw RB.newGridCAException("initCAParam.empty", name);
 
153
            }
 
154
            if(value.length() < minLen.intValue()) {
 
155
                throw RB.newGridCAException("initCAParam.tooShort", new Object [] { getLocalizedName(), value, minLen });
 
156
            }
 
157
            if(value.length() > maxLen.intValue()) {
 
158
                throw RB.newGridCAException("initCAParam.tooLong", new Object [] { getLocalizedName(), value, maxLen });
 
159
            }
 
160
            
 
161
            for(int i = 0; i < FORBIDDEN_LETTERS.length; i++) {
 
162
                if(value.indexOf(FORBIDDEN_LETTERS[i]) >= 0) {
 
163
                    throw RB.newGridCAException("gridCAParam.forbiddenLetter",
 
164
                            new Object [] { getLocalizedName(), value, new Character(FORBIDDEN_LETTERS[i]) });
 
165
                }
 
166
            }
 
167
        }
 
168
        
 
169
        public boolean equals(Object obj) {
 
170
            return obj instanceof ParamDef &&
 
171
                    name.equals(((ParamDef)obj).name);
 
172
        }
 
173
        
 
174
        public int hashCode() {
 
175
            return name.hashCode();
 
176
        }
 
177
        
 
178
    }
 
179
    
 
180
    /** Definition of the country parameter */
 
181
    public final static ParamDef COUNTRY = new ParamDef("country");
 
182
    
 
183
    /** Definition of the state parameter */
 
184
    public final static ParamDef STATE = new ParamDef("state");
 
185
    
 
186
    /** Definition of the location parameter */
 
187
    public final static ParamDef LOCATION = new ParamDef("location");
 
188
    
 
189
    /** Definition of the organisation parameter */
 
190
    public final static ParamDef ORG = new ParamDef("organisation");
 
191
    
 
192
    /** Definition of the organisation unit parameter */
 
193
    public final static ParamDef ORG_UNIT = new ParamDef("organisationUnit");
 
194
    
 
195
    /** Definition of the admin email address parameter */
 
196
    public final static ParamDef ADMIN_EMAIL = new ParamDef("adminEmailAddress");
 
197
    
 
198
    private final static ParamDef [] PARAMS = {
 
199
        COUNTRY, STATE, LOCATION, ORG, ORG_UNIT, ADMIN_EMAIL
 
200
    };
 
201
    
 
202
    private Map values = new HashMap();
 
203
    
 
204
    public InitCAParameters() {
 
205
        
 
206
    }
 
207
    
 
208
    /**
 
209
     * Query the values for a init ca command.
 
210
     *
 
211
     * @param callbackHandler   the callback handler which is used to query the values
 
212
     * @throws java.io.IOException if the callback handler throws a IOException
 
213
     * @throws javax.security.auth.callback.UnsupportedCallbackException if the the callback handler does not 
 
214
     *            support <code>NameCallback</code> or <code>TextOutputCallback</code>
 
215
     * @return the <code>InitCAParameters</code> object with the queried values
 
216
     */
 
217
    public static InitCAParameters queryNewInstance(CallbackHandler callbackHandler) throws IOException, UnsupportedCallbackException {
 
218
        
 
219
        InitCAParameters params = new InitCAParameters();
 
220
        
 
221
        Callback [] cb = new Callback[1];
 
222
        Callback [] ecb = new Callback[1];
 
223
        for(int i = 0; i < PARAMS.length; i++) {
 
224
            cb[0] = PARAMS[i].createCallback();
 
225
           do {
 
226
                callbackHandler.handle(cb);
 
227
                try {
 
228
                    params.setValue(PARAMS[i], ((NameCallback)cb[0]).getName());
 
229
                    break;
 
230
                } catch (GridCAException ex) {
 
231
                    ecb[0] = new TextOutputCallback(TextOutputCallback.ERROR, ex.getLocalizedMessage());
 
232
                    callbackHandler.handle(ecb);
 
233
                }
 
234
           } while(true);
 
235
           
 
236
            
 
237
        }
 
238
        return params;
 
239
    }
 
240
    
 
241
    
 
242
    private String getValue(ParamDef param) {
 
243
        return (String)values.get(param);
 
244
    }
 
245
    
 
246
    private void setValue(ParamDef param, String value) throws GridCAException {
 
247
        param.check(value);
 
248
        values.put(param, value);
 
249
    }
 
250
    
 
251
    /**
 
252
     * Get the country of the ca.
 
253
     * @return the country of the ca
 
254
     */
 
255
    public String getCountry() {
 
256
        return getValue(COUNTRY);
 
257
    }
 
258
    
 
259
    /**
 
260
     * Set the country of the ca (e.g. Germany)
 
261
     * @param country country of the ca (exact two characters)
 
262
     * @throws com.sun.grid.ca.GridCAException if <code>country</code> is not valid
 
263
     */
 
264
    public void setCountry(String country) throws GridCAException {
 
265
        setValue(COUNTRY, country);
 
266
    }
 
267
    
 
268
    /**
 
269
     * Get the state of the ca.
 
270
     * @return the state of the ca
 
271
     */
 
272
    public String getState() {
 
273
        return getValue(STATE);
 
274
    }
 
275
    
 
276
    /**
 
277
     * Set the state of the ca (e.g. Bayern)
 
278
     * @param state state of the ca (at least one char)
 
279
     * @throws com.sun.grid.ca.GridCAException if <code>state</code> is not valid
 
280
     */
 
281
    public void setState(String state) throws GridCAException {
 
282
        setValue(STATE, state);
 
283
    }
 
284
    
 
285
    /**
 
286
     * Get the location of the ca.
 
287
     * @return the location of the ca
 
288
     */
 
289
    public String getLocation() {
 
290
        return getValue(LOCATION);
 
291
    }
 
292
    
 
293
    /**
 
294
     * Set the location of the ca (e.g. city)
 
295
     * @param location location of the ca (at least one char)
 
296
     * @throws com.sun.grid.ca.GridCAException if <code>location</code> is not valid
 
297
     */
 
298
    public void setLocation(String location) throws GridCAException {
 
299
        setValue(LOCATION, location);
 
300
    }
 
301
    
 
302
    /**
 
303
     * Get the organization of the ca.
 
304
     * @return the organization of the ca
 
305
     */
 
306
    public String getOrganization() {
 
307
        return getValue(ORG);
 
308
    }
 
309
    
 
310
    /**
 
311
     * Set the organization of the ca.
 
312
     * @param organization organization of the ca (at least one char)
 
313
     * @throws com.sun.grid.ca.GridCAException if <code>organization</code> is not valid
 
314
     */
 
315
    public void setOrganization(String organization) throws GridCAException {
 
316
        setValue(ORG, organization);
 
317
    }
 
318
    
 
319
    /**
 
320
     * Get the organization unit of the ca.
 
321
     * @return the organization unit of the ca
 
322
     */
 
323
    public String getOrganizationUnit() {
 
324
        return getValue(ORG_UNIT);
 
325
    }
 
326
    
 
327
    /**
 
328
     * Set the organization unit of the ca.
 
329
     * @param organizationUnit organization unit of the ca (at least one char)
 
330
     * @throws com.sun.grid.ca.GridCAException if <code>organizationUnit</code> is not valid
 
331
     */
 
332
    public void setOrganizationUnit(String organizationUnit) throws GridCAException {
 
333
        setValue(ORG_UNIT, organizationUnit);
 
334
    }
 
335
    
 
336
    /**
 
337
     * Get the email address of the ca adminstrator
 
338
     * @return the email address of the ca adminstrator
 
339
     */
 
340
    public String getAdminEmailAddress() {
 
341
        return getValue(ADMIN_EMAIL);
 
342
    }
 
343
    
 
344
    /**
 
345
     * Set the email address of the ca adminstrator
 
346
     * @param adminEmailAddress email address of the ca adminstrator (at least one char)
 
347
     * @throws com.sun.grid.ca.GridCAException if <code>adminEmailAddress</code> is not valid
 
348
     */
 
349
    public void setAdminEmailAddress(String adminEmailAddress) throws GridCAException {
 
350
        setValue(ADMIN_EMAIL, adminEmailAddress);
 
351
    }
 
352
    
 
353
    /**
 
354
     * Validate the <code>InitCAParameters</code>
 
355
     * @throws com.sun.grid.ca.GridCAException  if the parameters are not valid
 
356
     */
 
357
    public void validate() throws GridCAException {
 
358
        for(int i = 0; i < PARAMS.length; i++) {
 
359
            PARAMS[i].check(getValue(PARAMS[i]));
 
360
        }
 
361
    }
 
362
    
 
363
    
 
364
}