001: /*
002: * BEGIN_HEADER - DO NOT EDIT
003: *
004: * The contents of this file are subject to the terms
005: * of the Common Development and Distribution License
006: * (the "License"). You may not use this file except
007: * in compliance with the License.
008: *
009: * You can obtain a copy of the license at
010: * https://open-esb.dev.java.net/public/CDDLv1.0.html.
011: * See the License for the specific language governing
012: * permissions and limitations under the License.
013: *
014: * When distributing Covered Code, include this CDDL
015: * HEADER in each file and include the License file at
016: * https://open-esb.dev.java.net/public/CDDLv1.0.html.
017: * If applicable add the following below this CDDL HEADER,
018: * with the fields enclosed by brackets "[]" replaced with
019: * your own identifying information: Portions Copyright
020: * [year] [name of copyright owner]
021: */
022:
023: /*
024: * @(#)SampleThread.java
025: * Copyright 2004-2007 Sun Microsystems, Inc. All Rights Reserved.
026: *
027: * END_HEADER - DO NOT EDIT
028: */
029: /**
030: * TestThread.java
031: *
032: * SUN PROPRIETARY/CONFIDENTIAL.
033: * This software is the proprietary information of Sun Microsystems, Inc.
034: * Use is subject to license terms.
035: *
036: * Created on February 6, 2005, 8:13 PM
037: */package com.sun.jbi.internal.security;
038:
039: import javax.security.auth.Subject;
040: import javax.security.auth.x500.X500Principal;
041:
042: public class SampleThread extends Thread {
043:
044: public void run() {
045: try {
046: modifySubject(getName());
047: checkMySubject(getName());
048: modifySubject(getName() + "-modified-" + getName());
049: checkMySubject(getName() + "-modified-" + getName());
050: } catch (Exception ex) {
051: ex.printStackTrace();
052: throw new IllegalStateException(ex.getMessage());
053: }
054:
055: }
056:
057: private void modifySubject(String tid) throws Exception {
058: createSubjectInTLS(tid);
059: sleep(3000);
060: }
061:
062: /**
063: * Update the X500 Principal in the Subject.
064: */
065: private void createSubjectInTLS(String tid) throws Exception {
066: boolean subjectLinked = true;
067: Subject subject = ThreadLocalContext.getLocalSubject();
068:
069: if (subject == null) {
070: subject = new Subject();
071: subjectLinked = false;
072: }
073:
074: java.util.Set pSet = subject.getPrincipals();
075: java.util.Iterator itr = pSet.iterator();
076:
077: // -- Clean the Principal Set
078: while (itr.hasNext()) {
079: Object obj = itr.next();
080: pSet.remove(obj);
081: obj = null;
082: }
083:
084: pSet.add(new X500Principal("CN=" + tid));
085:
086: if (!subjectLinked) {
087: ThreadLocalContext.setLocalSubject(subject);
088: }
089: }
090:
091: /**
092: * Checks if the Principal name is as expected.
093: *
094: * @throws Exception if the Prinicipal name is not as expected.
095: */
096: private void checkMySubject(String expectedSubject)
097: throws Exception {
098: Subject mySubject = ThreadLocalContext.getLocalSubject();
099: if (mySubject != null) {
100: java.util.Iterator itr = mySubject.getPrincipals()
101: .iterator();
102: if (itr.hasNext()) {
103: if (!expectedSubject
104: .equals(((X500Principal) itr.next()).getName())) {
105: throw new Exception("Thread " + getName()
106: + " expected Subject " + expectedSubject
107: + " got "
108: + ((X500Principal) itr.next()).getName());
109: }
110: ;
111: }
112: } else {
113: throw new Exception("Thread " + getName()
114: + " Subject is null :( ");
115: }
116: }
117:
118: }
|