KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > proxy > SecurityInterceptor


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.proxy;
23
24 import java.security.Principal JavaDoc;
25 import java.security.PrivilegedAction JavaDoc;
26 import java.security.AccessController JavaDoc;
27
28 import org.jboss.invocation.Invocation;
29 import org.jboss.security.SecurityAssociation;
30
31 /**
32 * The client-side proxy for an EJB Home object.
33 *
34 * @author <a HREF="mailto:marc.fleury@jboss.org">Marc Fleury</a>
35 * @version $Revision: 37459 $
36 */

37 public class SecurityInterceptor
38    extends Interceptor
39 {
40    /** Serial Version Identifier. @since 1.4.2.1 */
41    private static final long serialVersionUID = -4206940878404525061L;
42
43    /**
44    * No-argument constructor for externalization.
45    */

46    public SecurityInterceptor()
47    {
48    }
49
50    // Public --------------------------------------------------------
51

52    public Object JavaDoc invoke(Invocation invocation)
53       throws Throwable JavaDoc
54    {
55       // Get Principal and credentials
56
SecurityActions sa = SecurityActions.UTIL.getSecurityActions();
57
58       Principal JavaDoc principal = sa.getPrincipal();
59       if (principal != null)
60       {
61          invocation.setPrincipal(principal);
62       }
63
64       Object JavaDoc credential = sa.getCredential();
65       if (credential != null)
66       {
67          invocation.setCredential(credential);
68       }
69
70       return getNext().invoke(invocation);
71    }
72
73    interface SecurityActions
74    {
75       class UTIL
76       {
77          static SecurityActions getSecurityActions()
78          {
79             return System.getSecurityManager() == null ? NON_PRIVILEGED : PRIVILEGED;
80          }
81       }
82
83       SecurityActions NON_PRIVILEGED = new SecurityActions()
84       {
85          public Principal JavaDoc getPrincipal()
86          {
87             return SecurityAssociation.getPrincipal();
88          }
89
90          public Object JavaDoc getCredential()
91          {
92             return SecurityAssociation.getCredential();
93          }
94       };
95
96       SecurityActions PRIVILEGED = new SecurityActions()
97       {
98          private final PrivilegedAction JavaDoc getPrincipalAction = new PrivilegedAction JavaDoc()
99          {
100             public Object JavaDoc run()
101             {
102                return SecurityAssociation.getPrincipal();
103             }
104          };
105
106          private final PrivilegedAction JavaDoc getCredentialAction = new PrivilegedAction JavaDoc()
107          {
108             public Object JavaDoc run()
109             {
110                return SecurityAssociation.getCredential();
111             }
112          };
113
114          public Principal JavaDoc getPrincipal()
115          {
116             return (Principal JavaDoc)AccessController.doPrivileged(getPrincipalAction);
117          }
118
119          public Object JavaDoc getCredential()
120          {
121             return AccessController.doPrivileged(getCredentialAction);
122          }
123       };
124
125       Principal JavaDoc getPrincipal();
126
127       Object JavaDoc getCredential();
128    }
129 }
130
Popular Tags