KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > security > ejb > CustomPrincipalBean


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.test.security.ejb;
23
24 import java.security.Principal JavaDoc;
25 import java.util.Set JavaDoc;
26 import java.util.Iterator JavaDoc;
27 import javax.ejb.SessionBean JavaDoc;
28 import javax.ejb.SessionContext JavaDoc;
29 import javax.naming.InitialContext JavaDoc;
30 import javax.security.auth.Subject JavaDoc;
31 import org.jboss.logging.Logger;
32
33 /** Test return of a custom principal from getCallerPrincipal.
34  *
35  * @author Scott.Stark@jboss.org
36  * @version $Revision: 58115 $
37  */

38 public class CustomPrincipalBean implements SessionBean JavaDoc
39 {
40    private static Logger log = Logger.getLogger(CustomPrincipalBean.class);
41
42    private SessionContext JavaDoc ctx;
43
44    public void ejbCreate()
45    {
46    }
47
48    public void ejbActivate()
49    {
50    }
51
52    public void ejbPassivate()
53    {
54    }
55
56    public void ejbRemove()
57    {
58    }
59
60    public void setSessionContext(SessionContext JavaDoc ctx)
61    {
62       this.ctx = ctx;
63    }
64
65    public boolean validateCallerPrincipal(Class JavaDoc type)
66    {
67       ClassLoader JavaDoc typeLoader = type.getClassLoader();
68       log.info("validateCallerPrincipal, type="+type+", loader="+typeLoader);
69       Principal JavaDoc caller = ctx.getCallerPrincipal();
70       log.info("caller="+caller+", class="+caller.getClass());
71       boolean isType = true;
72       if( caller.getClass().isAssignableFrom(type) == false )
73       {
74          log.error("type of caller is not: "+type);
75          isType = false;
76       }
77
78       try
79       {
80          InitialContext JavaDoc ctx = new InitialContext JavaDoc();
81          Subject JavaDoc s = (Subject JavaDoc) ctx.lookup("java:comp/env/security/subject");
82          Set JavaDoc principals = s.getPrincipals();
83          Iterator JavaDoc iter = principals.iterator();
84          while( iter.hasNext() )
85          {
86             Object JavaDoc p = iter.next();
87             ClassLoader JavaDoc pLoader = p.getClass().getClassLoader();
88             log.info("type="+p.getClass()+", loader="+pLoader);
89          }
90          Set JavaDoc customPrincipals = s.getPrincipals(type);
91          caller = (Principal JavaDoc) customPrincipals.iterator().next();
92          log.info("Subject caller="+caller+", class="+caller.getClass());
93          if( caller.getClass().isAssignableFrom(type) == true )
94          {
95             log.info("type of caller is: "+type);
96             isType = true;
97          }
98       }
99       catch(Exception JavaDoc e)
100       {
101          log.error("Failed to lookup security mgr", e);
102       }
103       return isType;
104    }
105
106 }
107
Popular Tags