01: /*
02: * JBoss, Home of Professional Open Source.
03: * Copyright 2006, Red Hat Middleware LLC, and individual contributors
04: * as indicated by the @author tags. See the copyright.txt file in the
05: * distribution for a full listing of individual contributors.
06: *
07: * This is free software; you can redistribute it and/or modify it
08: * under the terms of the GNU Lesser General Public License as
09: * 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;
25: import java.util.Set;
26: import java.util.Iterator;
27: import javax.ejb.SessionBean;
28: import javax.ejb.SessionContext;
29: import javax.naming.InitialContext;
30: import javax.security.auth.Subject;
31: import org.apache.log4j.Logger;
32:
33: /** Test return of a custom principal from getCallerPrincipal.
34: *
35: * @author Scott.Stark@jboss.org
36: * @version $Revision: 57211 $
37: */
38: public class CustomPrincipalBean implements SessionBean {
39: private static Logger log = Logger
40: .getLogger(CustomPrincipalBean.class);
41:
42: private SessionContext ctx;
43:
44: public void ejbCreate() {
45: }
46:
47: public void ejbActivate() {
48: }
49:
50: public void ejbPassivate() {
51: }
52:
53: public void ejbRemove() {
54: }
55:
56: public void setSessionContext(SessionContext ctx) {
57: this .ctx = ctx;
58: }
59:
60: public boolean validateCallerPrincipal(Class type) {
61: ClassLoader typeLoader = type.getClassLoader();
62: log.info("validateCallerPrincipal, type=" + type + ", loader="
63: + typeLoader);
64: Principal caller = ctx.getCallerPrincipal();
65: log.info("caller=" + caller + ", class=" + caller.getClass());
66: boolean isType = true;
67: if (caller.getClass().isAssignableFrom(type) == false) {
68: log.error("type of caller is not: " + type);
69: isType = false;
70: }
71:
72: try {
73: InitialContext ctx = new InitialContext();
74: Subject s = (Subject) ctx
75: .lookup("java:comp/env/security/subject");
76: Set principals = s.getPrincipals();
77: Iterator iter = principals.iterator();
78: while (iter.hasNext()) {
79: Object p = iter.next();
80: ClassLoader pLoader = p.getClass().getClassLoader();
81: log
82: .info("type=" + p.getClass() + ", loader="
83: + pLoader);
84: }
85: Set customPrincipals = s.getPrincipals(type);
86: caller = (Principal) customPrincipals.iterator().next();
87: log.info("Subject caller=" + caller + ", class="
88: + caller.getClass());
89: if (caller.getClass().isAssignableFrom(type) == true) {
90: log.info("type of caller is: " + type);
91: isType = true;
92: }
93: } catch (Exception e) {
94: log.error("Failed to lookup security mgr", e);
95: }
96: return isType;
97: }
98:
99: }
|