KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > ejb3 > test > security > EntityBean


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.test.security;
23
24 import java.security.Principal JavaDoc;
25
26 import javax.annotation.Resource;
27 import javax.ejb.EJBException JavaDoc;
28 import javax.ejb.EntityContext JavaDoc;
29 import javax.ejb.SessionContext JavaDoc;
30 import javax.naming.InitialContext JavaDoc;
31 import javax.naming.NamingException JavaDoc;
32 import javax.persistence.Entity;
33 import javax.persistence.GeneratedValue; import javax.persistence.GenerationType;
34 import javax.persistence.Id;
35 import javax.persistence.Table;
36 import javax.security.auth.Subject JavaDoc;
37
38 /** A BMP entity bean that creates beans on the fly with
39 a key equal to that passed to findByPrimaryKey. Obviously
40 not a real entity bean. It is used to test Principal propagation
41 using the echo method.
42
43 @author Scott.Stark@jboss.org
44 @version $Revision: 58110 $
45 */

46 @Entity
47 @Table(name = "ENTITY_BEAN")
48 public class EntityBean
49 {
50    org.jboss.logging.Logger log = org.jboss.logging.Logger.getLogger(getClass());
51    
52     private Long JavaDoc key;
53    
54     @Id @GeneratedValue(strategy=GenerationType.IDENTITY)
55     public Long JavaDoc getKey()
56     {
57        return key;
58     }
59     
60     public void setKey(Long JavaDoc key)
61     {
62        this.key = key;
63     }
64
65     public String JavaDoc echo(String JavaDoc arg)
66     {
67         log.debug("EntityBean.echo, arg="+arg);
68         // Check the java:comp/env/security/security-domain
69
try
70         {
71            InitialContext JavaDoc ctx = new InitialContext JavaDoc();
72            Object JavaDoc securityMgr = ctx.lookup("java:comp/env/security/security-domain");
73            log.debug("Checking java:comp/env/security/security-domain");
74            if( securityMgr == null )
75               throw new EJBException JavaDoc("Failed to find security mgr under: java:comp/env/security/security-domain");
76            log.debug("Found SecurityManager: "+securityMgr);
77            Subject JavaDoc activeSubject = (Subject JavaDoc) ctx.lookup("java:comp/env/security/subject");
78            log.debug("ActiveSubject: "+activeSubject);
79         }
80         catch(NamingException JavaDoc e)
81         {
82            log.debug("failed", e);
83            throw new EJBException JavaDoc("Naming exception: "+e.toString(true));
84         }
85         return null;
86     }
87 }
88
Popular Tags