KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > resource > security > GetPrincipalInfoAction


1 /*
2 * JBoss, Home of Professional Open Source
3 * Copyright 2005, JBoss Inc., and individual contributors as indicated
4 * by the @authors tag. See the copyright.txt in the distribution for a
5 * full listing of individual contributors.
6 *
7 * This is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU Lesser General Public License as
9 * published by the Free Software Foundation; either version 2.1 of
10 * the License, or (at your option) any later version.
11 *
12 * This software is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this software; if not, write to the Free
19 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21 */

22 package org.jboss.resource.security;
23
24 import java.security.AccessController JavaDoc;
25 import java.security.PrivilegedAction JavaDoc;
26 import java.security.Principal JavaDoc;
27 import java.io.UnsupportedEncodingException JavaDoc;
28
29 import org.jboss.security.SecurityAssociation;
30 import org.jboss.security.RunAsIdentity;
31
32 /** PrivilegedActions used by this package
33  *
34  * @author Scott.Stark@jboss.org
35  * @version $Revision: 37459 $
36  */

37 class GetPrincipalInfoAction
38 {
39    /* Obtain the password credential by trying char[], byte[],
40     and toString()
41     */

42    private static char[] getPassword()
43    {
44       Object JavaDoc credential = SecurityAssociation.getCredential();
45       char[] password = null;
46        if( credential instanceof char[] )
47        {
48           password = (char[]) credential;
49        }
50        else if( credential instanceof byte[] )
51        {
52           try
53           {
54              String JavaDoc tmp = new String JavaDoc((byte[]) credential, "UTF-8");
55              password = tmp.toCharArray();
56           }
57           catch (UnsupportedEncodingException JavaDoc e)
58           {
59              throw new SecurityException JavaDoc(e.getMessage());
60           }
61        }
62        else if( credential != null )
63        {
64           String JavaDoc tmp = credential.toString();
65           password = tmp.toCharArray();
66        }
67       return password;
68    }
69
70    interface PrincipalActions
71    {
72       PrincipalActions PRIVILEGED = new PrincipalActions()
73       {
74          private final PrivilegedAction JavaDoc peekAction = new PrivilegedAction JavaDoc()
75          {
76             public Object JavaDoc run()
77             {
78                return SecurityAssociation.peekRunAsIdentity();
79             }
80          };
81
82          private final PrivilegedAction JavaDoc getPrincipalAction = new PrivilegedAction JavaDoc()
83          {
84             public Object JavaDoc run()
85             {
86                return SecurityAssociation.getPrincipal();
87             }
88          };
89
90          private final PrivilegedAction JavaDoc getCredentialAction = new PrivilegedAction JavaDoc()
91          {
92             public Object JavaDoc run()
93             {
94                return getPassword();
95             }
96          };
97
98          public RunAsIdentity peek()
99          {
100             return (RunAsIdentity)AccessController.doPrivileged(peekAction);
101          }
102
103          public Principal JavaDoc getPrincipal()
104          {
105             return (Principal JavaDoc)AccessController.doPrivileged(getPrincipalAction);
106          }
107
108          public char[] getCredential()
109          {
110             return (char[]) AccessController.doPrivileged(getCredentialAction);
111          }
112       };
113
114       PrincipalActions NON_PRIVILEGED = new PrincipalActions()
115       {
116          public RunAsIdentity peek()
117          {
118             return SecurityAssociation.peekRunAsIdentity();
119          }
120
121          public Principal JavaDoc getPrincipal()
122          {
123             return SecurityAssociation.getPrincipal();
124          }
125
126          public char[] getCredential()
127          {
128             return getPassword();
129          }
130       };
131
132       Principal JavaDoc getPrincipal();
133       char[] getCredential();
134       RunAsIdentity peek();
135    }
136
137    static Principal JavaDoc getPrincipal()
138    {
139       Principal JavaDoc principal;
140       if(System.getSecurityManager() == null)
141       {
142          principal = PrincipalActions.NON_PRIVILEGED.getPrincipal();
143       }
144       else
145       {
146          principal = PrincipalActions.PRIVILEGED.getPrincipal();
147       }
148       return principal;
149    }
150    static char[] getCredential()
151    {
152       char[] credential;
153       if(System.getSecurityManager() == null)
154       {
155          credential = PrincipalActions.NON_PRIVILEGED.getCredential();
156       }
157       else
158       {
159          credential = PrincipalActions.PRIVILEGED.getCredential();
160       }
161       return credential;
162    }
163    static RunAsIdentity peekRunAsIdentity()
164    {
165       if(System.getSecurityManager() == null)
166       {
167          return PrincipalActions.NON_PRIVILEGED.peek();
168       }
169       else
170       {
171          return PrincipalActions.PRIVILEGED.peek();
172       }
173    }
174
175 }
176
Popular Tags