1 /*
2 * Copyright (c) 1999, 2006, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
155 /**
156 * Creates a new <code>PrivateCredentialPermission</code>
157 * with the specified <code>name</code>. The <code>name</code>
158 * specifies both a Credential class and a <code>Principal</code> Set.
159 *
160 * <p>
161 *
162 * @param name the name specifying the Credential class and
163 * <code>Principal</code> Set. <p>
164 *
165 * @param actions the actions specifying that the Credential can be read.
166 *
167 * @throws IllegalArgumentException if <code>name</code> does not conform
168 * to the correct syntax or if <code>actions</code> is not "read".
169 */
170 public PrivateCredentialPermission(String name, String actions) {
171 super(name);
172
173 if (!"read".equalsIgnoreCase(actions))
174 throw new IllegalArgumentException
175 (ResourcesMgr.getString("actions can only be 'read'"));
176 init(name);
177 }
178
179 /**
180 * Returns the Class name of the Credential associated with this
181 * <code>PrivateCredentialPermission</code>.
182 *
183 * <p>
184 *
185 * @return the Class name of the Credential associated with this
186 * <code>PrivateCredentialPermission</code>.
187 */
188 public String getCredentialClass() {
189 return credentialClass;
190 }
191
192 /**
193 * Returns the <code>Principal</code> classes and names
194 * associated with this <code>PrivateCredentialPermission</code>.
195 * The information is returned as a two-dimensional array (array[x][y]).
327
328 if (name == null || name.trim().length() == 0) {
329 throw new IllegalArgumentException("invalid empty name");
330 }
331
332 ArrayList<CredOwner> pList = new ArrayList<CredOwner>();
333 StringTokenizer tokenizer = new StringTokenizer(name, " ", true);
334 String principalClass = null;
335 String principalName = null;
336
337 if (testing)
338 System.out.println("whole name = " + name);
339
340 // get the Credential Class
341 credentialClass = tokenizer.nextToken();
342 if (testing)
343 System.out.println("Credential Class = " + credentialClass);
344
345 if (tokenizer.hasMoreTokens() == false) {
346 MessageFormat form = new MessageFormat(ResourcesMgr.getString
347 ("permission name [name] syntax invalid: "));
348 Object[] source = {name};
349 throw new IllegalArgumentException
350 (form.format(source) + ResourcesMgr.getString
351 ("Credential Class not followed by a " +
352 "Principal Class and Name"));
353 }
354
355 while (tokenizer.hasMoreTokens()) {
356
357 // skip delimiter
358 tokenizer.nextToken();
359
360 // get the Principal Class
361 principalClass = tokenizer.nextToken();
362 if (testing)
363 System.out.println(" Principal Class = " + principalClass);
364
365 if (tokenizer.hasMoreTokens() == false) {
366 MessageFormat form = new MessageFormat(ResourcesMgr.getString
367 ("permission name [name] syntax invalid: "));
368 Object[] source = {name};
369 throw new IllegalArgumentException
370 (form.format(source) + ResourcesMgr.getString
371 ("Principal Class not followed by a Principal Name"));
372 }
373
374 // skip delimiter
375 tokenizer.nextToken();
376
377 // get the Principal Name
378 principalName = tokenizer.nextToken();
379
380 if (!principalName.startsWith("\"")) {
381 MessageFormat form = new MessageFormat(ResourcesMgr.getString
382 ("permission name [name] syntax invalid: "));
383 Object[] source = {name};
384 throw new IllegalArgumentException
385 (form.format(source) + ResourcesMgr.getString
386 ("Principal Name must be surrounded by quotes"));
387 }
388
389 if (!principalName.endsWith("\"")) {
390
391 // we have a name with spaces in it --
392 // keep parsing until we find the end quote,
393 // and keep the spaces in the name
394
395 while (tokenizer.hasMoreTokens()) {
396 principalName = principalName + tokenizer.nextToken();
397 if (principalName.endsWith("\""))
398 break;
399 }
400
401 if (!principalName.endsWith("\"")) {
402 MessageFormat form = new MessageFormat
403 (ResourcesMgr.getString
404 ("permission name [name] syntax invalid: "));
405 Object[] source = {name};
406 throw new IllegalArgumentException
407 (form.format(source) + ResourcesMgr.getString
408 ("Principal Name missing end quote"));
409 }
410 }
411
412 if (testing)
413 System.out.println("\tprincipalName = '" + principalName + "'");
414
415 principalName = principalName.substring
416 (1, principalName.length() - 1);
417
418 if (principalClass.equals("*") &&
419 !principalName.equals("*")) {
420 throw new IllegalArgumentException(ResourcesMgr.getString
421 ("PrivateCredentialPermission Principal Class " +
422 "can not be a wildcard (*) value if Principal Name " +
423 "is not a wildcard (*) value"));
424 }
425
426 if (testing)
427 System.out.println("\tprincipalName = '" + principalName + "'");
428
429 pList.add(new CredOwner(principalClass, principalName));
430 }
431
432 this.credOwners = new CredOwner[pList.size()];
433 pList.toArray(this.credOwners);
434 }
435
436 private boolean impliesCredentialClass(String thisC, String thatC) {
437
438 // this should never happen
439 if (thisC == null || thatC == null)
440 return false;
441
442 if (testing)
443 System.out.println("credential class comparison: " +
539 CredOwner that = (CredOwner)obj;
540
541 if (principalClass.equals("*") ||
542 principalClass.equals(that.principalClass)) {
543
544 if (principalName.equals("*") ||
545 principalName.equals(that.principalName)) {
546 return true;
547 }
548 }
549
550 /**
551 * XXX no code yet to support a.b.*
552 */
553
554 return false;
555 }
556
557 public String toString() {
558 MessageFormat form = new MessageFormat(ResourcesMgr.getString
559 ("CredOwner:\n\tPrincipal Class = class\n\t" +
560 "Principal Name = name"));
561 Object[] source = {principalClass, principalName};
562 return (form.format(source));
563 }
564 }
565 }
|
1 /*
2 * Copyright (c) 1999, 2010, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
155 /**
156 * Creates a new <code>PrivateCredentialPermission</code>
157 * with the specified <code>name</code>. The <code>name</code>
158 * specifies both a Credential class and a <code>Principal</code> Set.
159 *
160 * <p>
161 *
162 * @param name the name specifying the Credential class and
163 * <code>Principal</code> Set. <p>
164 *
165 * @param actions the actions specifying that the Credential can be read.
166 *
167 * @throws IllegalArgumentException if <code>name</code> does not conform
168 * to the correct syntax or if <code>actions</code> is not "read".
169 */
170 public PrivateCredentialPermission(String name, String actions) {
171 super(name);
172
173 if (!"read".equalsIgnoreCase(actions))
174 throw new IllegalArgumentException
175 (ResourcesMgr.getString("actions.can.only.be.read."));
176 init(name);
177 }
178
179 /**
180 * Returns the Class name of the Credential associated with this
181 * <code>PrivateCredentialPermission</code>.
182 *
183 * <p>
184 *
185 * @return the Class name of the Credential associated with this
186 * <code>PrivateCredentialPermission</code>.
187 */
188 public String getCredentialClass() {
189 return credentialClass;
190 }
191
192 /**
193 * Returns the <code>Principal</code> classes and names
194 * associated with this <code>PrivateCredentialPermission</code>.
195 * The information is returned as a two-dimensional array (array[x][y]).
327
328 if (name == null || name.trim().length() == 0) {
329 throw new IllegalArgumentException("invalid empty name");
330 }
331
332 ArrayList<CredOwner> pList = new ArrayList<CredOwner>();
333 StringTokenizer tokenizer = new StringTokenizer(name, " ", true);
334 String principalClass = null;
335 String principalName = null;
336
337 if (testing)
338 System.out.println("whole name = " + name);
339
340 // get the Credential Class
341 credentialClass = tokenizer.nextToken();
342 if (testing)
343 System.out.println("Credential Class = " + credentialClass);
344
345 if (tokenizer.hasMoreTokens() == false) {
346 MessageFormat form = new MessageFormat(ResourcesMgr.getString
347 ("permission.name.name.syntax.invalid."));
348 Object[] source = {name};
349 throw new IllegalArgumentException
350 (form.format(source) + ResourcesMgr.getString
351 ("Credential.Class.not.followed.by.a.Principal.Class.and.Name"));
352 }
353
354 while (tokenizer.hasMoreTokens()) {
355
356 // skip delimiter
357 tokenizer.nextToken();
358
359 // get the Principal Class
360 principalClass = tokenizer.nextToken();
361 if (testing)
362 System.out.println(" Principal Class = " + principalClass);
363
364 if (tokenizer.hasMoreTokens() == false) {
365 MessageFormat form = new MessageFormat(ResourcesMgr.getString
366 ("permission.name.name.syntax.invalid."));
367 Object[] source = {name};
368 throw new IllegalArgumentException
369 (form.format(source) + ResourcesMgr.getString
370 ("Principal.Class.not.followed.by.a.Principal.Name"));
371 }
372
373 // skip delimiter
374 tokenizer.nextToken();
375
376 // get the Principal Name
377 principalName = tokenizer.nextToken();
378
379 if (!principalName.startsWith("\"")) {
380 MessageFormat form = new MessageFormat(ResourcesMgr.getString
381 ("permission.name.name.syntax.invalid."));
382 Object[] source = {name};
383 throw new IllegalArgumentException
384 (form.format(source) + ResourcesMgr.getString
385 ("Principal.Name.must.be.surrounded.by.quotes"));
386 }
387
388 if (!principalName.endsWith("\"")) {
389
390 // we have a name with spaces in it --
391 // keep parsing until we find the end quote,
392 // and keep the spaces in the name
393
394 while (tokenizer.hasMoreTokens()) {
395 principalName = principalName + tokenizer.nextToken();
396 if (principalName.endsWith("\""))
397 break;
398 }
399
400 if (!principalName.endsWith("\"")) {
401 MessageFormat form = new MessageFormat
402 (ResourcesMgr.getString
403 ("permission.name.name.syntax.invalid."));
404 Object[] source = {name};
405 throw new IllegalArgumentException
406 (form.format(source) + ResourcesMgr.getString
407 ("Principal.Name.missing.end.quote"));
408 }
409 }
410
411 if (testing)
412 System.out.println("\tprincipalName = '" + principalName + "'");
413
414 principalName = principalName.substring
415 (1, principalName.length() - 1);
416
417 if (principalClass.equals("*") &&
418 !principalName.equals("*")) {
419 throw new IllegalArgumentException(ResourcesMgr.getString
420 ("PrivateCredentialPermission.Principal.Class.can.not.be.a.wildcard.value.if.Principal.Name.is.not.a.wildcard.value"));
421 }
422
423 if (testing)
424 System.out.println("\tprincipalName = '" + principalName + "'");
425
426 pList.add(new CredOwner(principalClass, principalName));
427 }
428
429 this.credOwners = new CredOwner[pList.size()];
430 pList.toArray(this.credOwners);
431 }
432
433 private boolean impliesCredentialClass(String thisC, String thatC) {
434
435 // this should never happen
436 if (thisC == null || thatC == null)
437 return false;
438
439 if (testing)
440 System.out.println("credential class comparison: " +
536 CredOwner that = (CredOwner)obj;
537
538 if (principalClass.equals("*") ||
539 principalClass.equals(that.principalClass)) {
540
541 if (principalName.equals("*") ||
542 principalName.equals(that.principalName)) {
543 return true;
544 }
545 }
546
547 /**
548 * XXX no code yet to support a.b.*
549 */
550
551 return false;
552 }
553
554 public String toString() {
555 MessageFormat form = new MessageFormat(ResourcesMgr.getString
556 ("CredOwner.Principal.Class.class.Principal.Name.name"));
557 Object[] source = {principalClass, principalName};
558 return (form.format(source));
559 }
560 }
561 }
|