1
/*___INFO__MARK_BEGIN__*/
2
/*************************************************************************
4
* The Contents of this file are made available subject to the terms of
5
* the Sun Industry Standards Source License Version 1.2
7
* Sun Microsystems Inc., March, 2001
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
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.
24
* The Initial Developer of the Original Code is: Sun Microsystems, Inc.
26
* Copyright: 2001 by Sun Microsystems, Inc.
28
* All Rights Reserved.
30
************************************************************************/
31
/*___INFO__MARK_END__*/
32
package com.sun.grid.ca;
34
import java.io.IOException;
35
import java.util.HashMap;
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;
45
* This class hold the parameters for Certificate Authority
47
public class InitCAParameters implements java.io.Serializable {
50
* Defines a parameter for the init ca command.
52
public static class ParamDef {
54
private final static char [] FORBIDDEN_LETTERS = {
59
private Integer minLen;
60
private Integer maxLen;
62
private ParamDef(String name) {
64
minLen = new Integer(RB.getString("initCAParam." + name + ".minLen"));
65
maxLen = new Integer(RB.getString("initCAParam." + name + ".maxLen"));
69
* Get name of this parameter
70
* @return name of this parameter
72
public String getName() {
77
* Get the localized name of this parameter
79
* @return the localized name of this parameter
81
public String getLocalizedName() {
82
return RB.getString("initCAParam." + name + ".name");
86
* Get the description of this parameter
87
* @return the description
89
public String getDescription() {
91
return RB.getString("initCAParam." + name + ".description");
92
} catch(MissingResourceException ex) {
98
* Get the default value of this param
99
* @return the default value of this param
101
public String getDefaultValue() {
103
return RB.getString("initCAParam." + name + ".default");
104
} catch(MissingResourceException ex) {
110
* Get the minimum number of character for this parameter
111
* @return minimum number of character for this parameter
113
public Integer getMinLen() {
118
* Get the maximum number of characters for this parameter
119
* @return maximum number of characters for this parameter
121
public Integer getMaxLen() {
125
private NameCallback createCallback() {
127
String description = getDescription();
128
String prompt = null;
129
if(description == null) {
130
prompt = RB.getString("initCAParam.prompt", new Object[] { getLocalizedName() });
132
prompt = RB.getString("initCAParam.promptWithDescription",
133
new Object [] { getLocalizedName(), description });
136
String defaultValue = getDefaultValue();
138
if(defaultValue == null) {
139
return new NameCallback(prompt);
141
return new NameCallback(prompt, defaultValue);
146
* Check the validity of a param value
148
* @throws com.sun.grid.ca.GridCAException
150
public void check(String value) throws GridCAException {
152
throw RB.newGridCAException("initCAParam.empty", name);
154
if(value.length() < minLen.intValue()) {
155
throw RB.newGridCAException("initCAParam.tooShort", new Object [] { getLocalizedName(), value, minLen });
157
if(value.length() > maxLen.intValue()) {
158
throw RB.newGridCAException("initCAParam.tooLong", new Object [] { getLocalizedName(), value, maxLen });
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]) });
169
public boolean equals(Object obj) {
170
return obj instanceof ParamDef &&
171
name.equals(((ParamDef)obj).name);
174
public int hashCode() {
175
return name.hashCode();
180
/** Definition of the country parameter */
181
public final static ParamDef COUNTRY = new ParamDef("country");
183
/** Definition of the state parameter */
184
public final static ParamDef STATE = new ParamDef("state");
186
/** Definition of the location parameter */
187
public final static ParamDef LOCATION = new ParamDef("location");
189
/** Definition of the organisation parameter */
190
public final static ParamDef ORG = new ParamDef("organisation");
192
/** Definition of the organisation unit parameter */
193
public final static ParamDef ORG_UNIT = new ParamDef("organisationUnit");
195
/** Definition of the admin email address parameter */
196
public final static ParamDef ADMIN_EMAIL = new ParamDef("adminEmailAddress");
198
private final static ParamDef [] PARAMS = {
199
COUNTRY, STATE, LOCATION, ORG, ORG_UNIT, ADMIN_EMAIL
202
private Map values = new HashMap();
204
public InitCAParameters() {
209
* Query the values for a init ca command.
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
217
public static InitCAParameters queryNewInstance(CallbackHandler callbackHandler) throws IOException, UnsupportedCallbackException {
219
InitCAParameters params = new InitCAParameters();
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();
226
callbackHandler.handle(cb);
228
params.setValue(PARAMS[i], ((NameCallback)cb[0]).getName());
230
} catch (GridCAException ex) {
231
ecb[0] = new TextOutputCallback(TextOutputCallback.ERROR, ex.getLocalizedMessage());
232
callbackHandler.handle(ecb);
242
private String getValue(ParamDef param) {
243
return (String)values.get(param);
246
private void setValue(ParamDef param, String value) throws GridCAException {
248
values.put(param, value);
252
* Get the country of the ca.
253
* @return the country of the ca
255
public String getCountry() {
256
return getValue(COUNTRY);
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
264
public void setCountry(String country) throws GridCAException {
265
setValue(COUNTRY, country);
269
* Get the state of the ca.
270
* @return the state of the ca
272
public String getState() {
273
return getValue(STATE);
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
281
public void setState(String state) throws GridCAException {
282
setValue(STATE, state);
286
* Get the location of the ca.
287
* @return the location of the ca
289
public String getLocation() {
290
return getValue(LOCATION);
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
298
public void setLocation(String location) throws GridCAException {
299
setValue(LOCATION, location);
303
* Get the organization of the ca.
304
* @return the organization of the ca
306
public String getOrganization() {
307
return getValue(ORG);
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
315
public void setOrganization(String organization) throws GridCAException {
316
setValue(ORG, organization);
320
* Get the organization unit of the ca.
321
* @return the organization unit of the ca
323
public String getOrganizationUnit() {
324
return getValue(ORG_UNIT);
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
332
public void setOrganizationUnit(String organizationUnit) throws GridCAException {
333
setValue(ORG_UNIT, organizationUnit);
337
* Get the email address of the ca adminstrator
338
* @return the email address of the ca adminstrator
340
public String getAdminEmailAddress() {
341
return getValue(ADMIN_EMAIL);
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
349
public void setAdminEmailAddress(String adminEmailAddress) throws GridCAException {
350
setValue(ADMIN_EMAIL, adminEmailAddress);
354
* Validate the <code>InitCAParameters</code>
355
* @throws com.sun.grid.ca.GridCAException if the parameters are not valid
357
public void validate() throws GridCAException {
358
for(int i = 0; i < PARAMS.length; i++) {
359
PARAMS[i].check(getValue(PARAMS[i]));