KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > ejb3 > security > SecurityActions


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.ejb3.security;
23
24 import java.lang.reflect.UndeclaredThrowableException JavaDoc;
25 import java.security.AccessController JavaDoc;
26 import java.security.PrivilegedAction JavaDoc;
27 import java.security.PrivilegedActionException JavaDoc;
28 import java.security.PrivilegedExceptionAction JavaDoc;
29 import javax.security.auth.Subject JavaDoc;
30 import javax.security.jacc.PolicyContext JavaDoc;
31 import javax.security.jacc.PolicyContextException JavaDoc;
32
33
34 /**
35  * A collection of privileged actions for this package
36  * <p/>
37  * Copy paste from package org.jboss.ejb.plugins and org.jboss.ejb.
38  * Did not want to rely on this from org.jboss.ejb.plugins package since this is
39  * packaged in jboss.jar which will not be part of the embedded distribution.
40  *
41  * @author Scott.Stark@jboss.org
42  * @author <a HREF="mailto:alex@jboss.org">Alexey Loubyansky</a>
43  * @author <a HREF="mailto:kabir.khan@jboss.org">Kabir Khan</a>
44  * @version $Revison: 1.1$
45  */

46 class SecurityActions
47 {
48    private static class SetContextID implements PrivilegedAction JavaDoc
49    {
50       String JavaDoc contextID;
51
52       SetContextID(String JavaDoc contextID)
53       {
54          this.contextID = contextID;
55       }
56
57       public Object JavaDoc run()
58       {
59          String JavaDoc previousID = PolicyContext.getContextID();
60          PolicyContext.setContextID(contextID);
61          return previousID;
62       }
63    }
64
65    interface PolicyContextActions
66    {
67       /**
68        * The JACC PolicyContext key for the current Subject
69        */

70       static final String JavaDoc SUBJECT_CONTEXT_KEY = "javax.security.auth.Subject.container";
71       PolicyContextActions PRIVILEGED = new PolicyContextActions()
72       {
73          private final PrivilegedExceptionAction JavaDoc exAction = new PrivilegedExceptionAction JavaDoc()
74          {
75             public Object JavaDoc run() throws Exception JavaDoc
76             {
77                return (Subject JavaDoc) PolicyContext.getContext(SUBJECT_CONTEXT_KEY);
78             }
79          };
80
81          public Subject JavaDoc getContextSubject()
82                  throws PolicyContextException JavaDoc
83          {
84             try
85             {
86                return (Subject JavaDoc) AccessController.doPrivileged(exAction);
87             }
88             catch (PrivilegedActionException JavaDoc e)
89             {
90                Exception JavaDoc ex = e.getException();
91                if (ex instanceof PolicyContextException JavaDoc)
92                   throw (PolicyContextException JavaDoc) ex;
93                else
94                   throw new UndeclaredThrowableException JavaDoc(ex);
95             }
96          }
97       };
98
99       PolicyContextActions NON_PRIVILEGED = new PolicyContextActions()
100       {
101          public Subject JavaDoc getContextSubject()
102                  throws PolicyContextException JavaDoc
103          {
104             return (Subject JavaDoc) PolicyContext.getContext(SUBJECT_CONTEXT_KEY);
105          }
106       };
107
108       Subject JavaDoc getContextSubject()
109               throws PolicyContextException JavaDoc;
110    }
111
112    static Subject JavaDoc getContextSubject()
113            throws PolicyContextException JavaDoc
114    {
115       if (System.getSecurityManager() == null)
116       {
117          return PolicyContextActions.NON_PRIVILEGED.getContextSubject();
118       }
119       else
120       {
121          return PolicyContextActions.PRIVILEGED.getContextSubject();
122       }
123    }
124
125    static String JavaDoc setContextID(String JavaDoc contextID)
126    {
127       PrivilegedAction JavaDoc action = new SetContextID(contextID);
128       String JavaDoc previousID = (String JavaDoc) AccessController.doPrivileged(action);
129       return previousID;
130    }
131 }
132
Popular Tags