KickJava   Java API By Example, From Geeks To Geeks.

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


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.lang.reflect.Method JavaDoc;
25 import java.lang.reflect.InvocationTargetException JavaDoc;
26 import java.rmi.RemoteException JavaDoc;
27 import java.security.Principal JavaDoc;
28 import javax.ejb.CreateException JavaDoc;
29 import javax.ejb.EJBException JavaDoc;
30 import javax.ejb.EntityBean JavaDoc;
31 import javax.ejb.EntityContext JavaDoc;
32 import javax.naming.InitialContext JavaDoc;
33 import javax.naming.NamingException JavaDoc;
34 import javax.security.auth.Subject JavaDoc;
35
36 /** A BMP entity bean that creates beans on the fly with
37 a key equal to that passed to findByPrimaryKey. Obviously
38 not a real entity bean. It is used to test Principal propagation
39 using the echo method.
40
41 @author Scott.Stark@jboss.org
42 @version $Revision: 58115 $
43 */

44 public class EntityBeanImpl implements EntityBean JavaDoc
45 {
46    org.jboss.logging.Logger log = org.jboss.logging.Logger.getLogger(getClass());
47    
48     private String JavaDoc key;
49     private EntityContext JavaDoc context;
50
51     public void ejbActivate()
52     {
53         log.debug("EntityBean.ejbActivate() called");
54     }
55
56     public void ejbPassivate()
57     {
58         log.debug("EntityBean.ejbPassivate() called");
59     }
60
61     public void ejbRemove()
62     {
63         log.debug("EntityBean.ejbRemove() called");
64     }
65     public void ejbLoad()
66     {
67         log.debug("EntityBean.ejbLoad() called");
68         key = (String JavaDoc) context.getPrimaryKey();
69     }
70     public void ejbStore()
71     {
72         log.debug("EntityBean.ejbStore() called");
73     }
74
75     public void setEntityContext(EntityContext JavaDoc context)
76     {
77         this.context = context;
78     }
79     public void unsetEntityContext()
80     {
81         this.context = null;
82     }
83
84     public String JavaDoc ejbCreate(String JavaDoc key)
85     throws CreateException JavaDoc
86    {
87       this.key = key;
88       return key;
89    }
90    public void ejbPostCreate(String JavaDoc key)
91    {
92    }
93
94     public String JavaDoc echo(String JavaDoc arg)
95     {
96         log.debug("EntityBean.echo, arg="+arg);
97         Principal JavaDoc p = context.getCallerPrincipal();
98         boolean isInternalRole = context.isCallerInRole("InternalRole");
99         log.debug("EntityBean.echo, callerPrincipal="+p);
100         log.debug("EntityBean.echo, isCallerInRole('InternalRole')="+isInternalRole);
101         // Check the java:comp/env/security/security-domain
102
try
103         {
104            InitialContext JavaDoc ctx = new InitialContext JavaDoc();
105            Object JavaDoc securityMgr = ctx.lookup("java:comp/env/security/security-domain");
106            log.debug("Checking java:comp/env/security/security-domain");
107            if( securityMgr == null )
108               throw new EJBException JavaDoc("Failed to find security mgr under: java:comp/env/security/security-domain");
109            log.debug("Found SecurityManager: "+securityMgr);
110            Subject JavaDoc activeSubject = (Subject JavaDoc) ctx.lookup("java:comp/env/security/subject");
111            log.debug("ActiveSubject: "+activeSubject);
112         }
113         catch(NamingException JavaDoc e)
114         {
115            log.debug("failed", e);
116            throw new EJBException JavaDoc("Naming exception: "+e.toString(true));
117         }
118         return p.getName();
119     }
120
121     public String JavaDoc ejbFindByPrimaryKey(String JavaDoc key)
122     {
123         log.debug("EntityBean.ejbFindByPrimaryKey, key="+key);
124         return key;
125     }
126 }
127
Popular Tags