KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > jca > securedejb > CallerIdentityBean


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.jca.securedejb;
23
24 import java.rmi.RemoteException JavaDoc;
25 import java.sql.Connection JavaDoc;
26 import java.sql.SQLException JavaDoc;
27 import java.security.Principal JavaDoc;
28 import javax.ejb.SessionBean JavaDoc;
29 import javax.ejb.SessionContext JavaDoc;
30 import javax.ejb.EJBException JavaDoc;
31 import javax.naming.InitialContext JavaDoc;
32 import javax.naming.directory.DirContext JavaDoc;
33 import javax.sql.DataSource JavaDoc;
34
35 import org.jboss.logging.Logger;
36 import org.jboss.test.jca.fs.DirContextFactory;
37
38 /** An ejb for testing the ejb caller identity propagation
39  *
40  * @author Scott.Stark@jboss.org
41  * @version $Revision: 37406 $
42  */

43 public class CallerIdentityBean implements SessionBean JavaDoc
44 {
45    static Logger log = Logger.getLogger(CallerIdentityBean.class);
46    private SessionContext JavaDoc ctx;
47
48    public void ejbCreate()
49    {
50    }
51    public void ejbActivate()
52    {
53    }
54    public void ejbPassivate() throws RemoteException JavaDoc
55    {
56    }
57    public void ejbRemove() throws RemoteException JavaDoc
58    {
59    }
60    public void setSessionContext(SessionContext JavaDoc ctx) throws RemoteException JavaDoc
61    {
62       this.ctx = ctx;
63    }
64    public void unsetSessionContext() throws RemoteException JavaDoc
65    {
66       this.ctx = null;
67    }
68
69    public void useCallerForAuth()
70    {
71       try
72       {
73          Principal JavaDoc caller = ctx.getCallerPrincipal();
74          String JavaDoc name0 = caller.getName();
75          boolean isCallerIdentityUser = ctx.isCallerInRole("CallerIdentityUser");
76          boolean isUseCallerForAuth = ctx.isCallerInRole("UseCallerForAuth");
77          log.info("useCallerForAuth#0, caller="+caller
78             +", isCallerIdentityUser="+isCallerIdentityUser
79             +", isUseCallerForAuth="+isUseCallerForAuth);
80          InitialContext JavaDoc enc = new InitialContext JavaDoc();
81          DataSource JavaDoc ds = (DataSource JavaDoc) enc.lookup("java:comp/env/jdbc/CallerIdentityDS");
82          testConnection(ds);
83          caller = ctx.getCallerPrincipal();
84          String JavaDoc name1 = caller.getName();
85          isCallerIdentityUser = ctx.isCallerInRole("CallerIdentityUser");
86          isUseCallerForAuth = ctx.isCallerInRole("UseCallerForAuth");
87          log.info("useCallerForAuth#1, caller="+caller
88             +", isCallerIdentityUser="+isCallerIdentityUser
89             +", isUseCallerForAuth="+isUseCallerForAuth);
90          if( name0.equals(name1) == false )
91             throw new EJBException JavaDoc(name0+" != "+name1);
92          if( isCallerIdentityUser == false || isUseCallerForAuth == false )
93             throw new EJBException JavaDoc("Lost CallerIdentityUser, UseCallerForAuth roles");
94       }
95       catch(Exception JavaDoc e)
96       {
97          throw new EJBException JavaDoc(e);
98       }
99    }
100
101    public void useConfiguredForAuth()
102    {
103       try
104       {
105          Principal JavaDoc caller = ctx.getCallerPrincipal();
106          String JavaDoc name0 = caller.getName();
107          boolean isCallerIdentityUser = ctx.isCallerInRole("CallerIdentityUser");
108          boolean isUseConfiguredForAuth = ctx.isCallerInRole("UseConfiguredForAuth");
109          log.info("useConfiguredForAuth#0, caller="+caller
110             +", isCallerIdentityUser="+isCallerIdentityUser
111             +", isUseConfiguredForAuth="+isUseConfiguredForAuth);
112          InitialContext JavaDoc enc = new InitialContext JavaDoc();
113          DataSource JavaDoc ds = (DataSource JavaDoc) enc.lookup("java:comp/env/jdbc/ConfiguredIdentityDS");
114          testConnection(ds);
115          caller = ctx.getCallerPrincipal();
116          String JavaDoc name1 = caller.getName();
117          isCallerIdentityUser = ctx.isCallerInRole("CallerIdentityUser");
118          isUseConfiguredForAuth = ctx.isCallerInRole("UseConfiguredForAuth");
119          log.info("useConfiguredForAuth#1, caller="+caller
120             +", isCallerIdentityUser="+isCallerIdentityUser
121             +", isUseConfiguredForAuth="+isUseConfiguredForAuth);
122          if( name0.equals(name1) == false )
123             throw new EJBException JavaDoc(name0+" != "+name1);
124          if( isCallerIdentityUser == false || isUseConfiguredForAuth == false )
125             throw new EJBException JavaDoc("Lost CallerIdentityUser, UseConfiguredForAuth roles");
126
127          // Access the connection again
128
ds = (DataSource JavaDoc) enc.lookup("java:comp/env/jdbc/ConfiguredIdentityDS");
129          for(int n = 0; n < 1000; n ++)
130          {
131             testConnection(ds);
132          }
133          caller = ctx.getCallerPrincipal();
134          String JavaDoc name2 = caller.getName();
135          isCallerIdentityUser = ctx.isCallerInRole("CallerIdentityUser");
136          isUseConfiguredForAuth = ctx.isCallerInRole("UseConfiguredForAuth");
137          log.info("useRunAsForAuthDS#2, caller="+caller
138             +", isCallerIdentityUser="+isCallerIdentityUser
139             +", isUseConfiguredForAuth="+isUseConfiguredForAuth);
140          if( name0.equals(name2) == false )
141             throw new EJBException JavaDoc(name0+" != "+name2);
142          if( isCallerIdentityUser == false || isUseConfiguredForAuth == false )
143             throw new EJBException JavaDoc("Lost CallerIdentityUser, UseConfiguredForAuth roles");
144       }
145       catch(Exception JavaDoc e)
146       {
147          throw new EJBException JavaDoc(e);
148       }
149    }
150
151    public void useRunAsForAuthDS()
152    {
153       try
154       {
155          Principal JavaDoc caller = ctx.getCallerPrincipal();
156          String JavaDoc name0 = caller.getName();
157          boolean isCallerIdentityUser = ctx.isCallerInRole("CallerIdentityUser");
158          boolean isUseConfiguredForAuth = ctx.isCallerInRole("UseConfiguredForAuth");
159          log.info("useRunAsForAuthDS#0, caller="+caller
160             +", isCallerIdentityUser="+isCallerIdentityUser
161             +", isUseConfiguredForAuth="+isUseConfiguredForAuth);
162          InitialContext JavaDoc enc = new InitialContext JavaDoc();
163          DataSource JavaDoc ds = (DataSource JavaDoc) enc.lookup("java:comp/env/jdbc/RunAsIdentityDS");
164          testConnection(ds);
165          caller = ctx.getCallerPrincipal();
166          String JavaDoc name1 = caller.getName();
167          isCallerIdentityUser = ctx.isCallerInRole("CallerIdentityUser");
168          isUseConfiguredForAuth = ctx.isCallerInRole("UseConfiguredForAuth");
169          log.info("useRunAsForAuthDS#1, caller="+caller
170             +", isCallerIdentityUser="+isCallerIdentityUser
171             +", isUseConfiguredForAuth="+isUseConfiguredForAuth);
172          if( name0.equals(name1) == false )
173             throw new EJBException JavaDoc(name0+" != "+name1);
174          if( isCallerIdentityUser == false || isUseConfiguredForAuth == false )
175             throw new EJBException JavaDoc("Lost CallerIdentityUser, UseConfiguredForAuth roles");
176
177          // Access the connection again
178
ds = (DataSource JavaDoc) enc.lookup("java:comp/env/jdbc/RunAsIdentityDS");
179          for(int n = 0; n < 1000; n ++)
180          {
181             testConnection(ds);
182          }
183          caller = ctx.getCallerPrincipal();
184          String JavaDoc name2 = caller.getName();
185          isCallerIdentityUser = ctx.isCallerInRole("CallerIdentityUser");
186          isUseConfiguredForAuth = ctx.isCallerInRole("UseConfiguredForAuth");
187          log.info("useRunAsForAuthDS#2, caller="+caller
188             +", isCallerIdentityUser="+isCallerIdentityUser
189             +", isUseConfiguredForAuth="+isUseConfiguredForAuth);
190          if( name0.equals(name2) == false )
191             throw new EJBException JavaDoc(name0+" != "+name2);
192          if( isCallerIdentityUser == false || isUseConfiguredForAuth == false )
193             throw new EJBException JavaDoc("Lost CallerIdentityUser, UseConfiguredForAuth roles");
194       }
195       catch(Exception JavaDoc e)
196       {
197          throw new EJBException JavaDoc(e);
198       }
199    }
200
201    public void useRunAsForAuthFS()
202    {
203       try
204       {
205          Principal JavaDoc caller = ctx.getCallerPrincipal();
206          String JavaDoc name0 = caller.getName();
207          boolean isCallerIdentityUser = ctx.isCallerInRole("CallerIdentityUser");
208          boolean isUseConfiguredForAuth = ctx.isCallerInRole("UseConfiguredForAuth");
209          log.info("useRunAsForAuthFS#0, caller="+caller
210             +", isCallerIdentityUser="+isCallerIdentityUser
211             +", isUseConfiguredForAuth="+isUseConfiguredForAuth);
212          InitialContext JavaDoc enc = new InitialContext JavaDoc();
213          DirContextFactory dcf = (DirContextFactory) enc.lookup("java:comp/env/jndi/RunAsIdentityFS");
214          DirContext JavaDoc dc = dcf.getConnection();
215          caller = ctx.getCallerPrincipal();
216          dc.close();
217          String JavaDoc name1 = caller.getName();
218          isCallerIdentityUser = ctx.isCallerInRole("CallerIdentityUser");
219          isUseConfiguredForAuth = ctx.isCallerInRole("UseConfiguredForAuth");
220          log.info("useRunAsForAuthFS#1, caller="+caller
221             +", isCallerIdentityUser="+isCallerIdentityUser
222             +", isUseConfiguredForAuth="+isUseConfiguredForAuth);
223          if( name0.equals(name1) == false )
224             throw new EJBException JavaDoc(name0+" != "+name1);
225          if( isCallerIdentityUser == false || isUseConfiguredForAuth == false )
226             throw new EJBException JavaDoc("Lost CallerIdentityUser, UseConfiguredForAuth roles");
227       }
228       catch(Exception JavaDoc e)
229       {
230          throw new EJBException JavaDoc(e);
231       }
232    }
233
234    private void testConnection(DataSource JavaDoc ds) throws SQLException JavaDoc
235    {
236       Connection JavaDoc conn = ds.getConnection();
237       conn.close();
238    }
239 }
240
Popular Tags