KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > security > clientlogin > BeanB


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
23 /*
24  * JBoss, Home of Professional Open Source
25  *
26  * Distributable under LGPL license.
27  * See terms of license at gnu.org.
28  */

29
30 package org.jboss.test.security.clientlogin;
31
32 import java.security.Principal JavaDoc;
33 import java.rmi.RemoteException JavaDoc;
34 import javax.ejb.SessionBean JavaDoc;
35 import javax.ejb.SessionContext JavaDoc;
36 import javax.security.auth.login.LoginContext JavaDoc;
37 import javax.naming.InitialContext JavaDoc;
38
39 import org.jboss.security.auth.callback.UsernamePasswordHandler;
40
41 /**
42  An IClientLogin session bean that calls a BeanC with changes in the
43  caller indentity using ClientLogin module.
44
45  @author Scott.Stark@jboss.org
46  @version $Revision: 41293 $
47  */

48 public class BeanB implements SessionBean JavaDoc
49 {
50    private SessionContext JavaDoc context;
51
52    public void ejbCreate()
53    {
54    }
55
56    public void ejbActivate()
57    {
58    }
59
60    public void ejbPassivate()
61    {
62    }
63
64    public void ejbRemove()
65    {
66    }
67
68    public void setSessionContext(SessionContext JavaDoc context)
69    {
70       this.context = context;
71    }
72
73    public Principal JavaDoc callBeanAsClientLoginUser() throws RemoteException JavaDoc
74    {
75       Principal JavaDoc caller = context.getCallerPrincipal();
76       String JavaDoc inputName = caller.getName();
77       try
78       {
79          UsernamePasswordHandler handler = new UsernamePasswordHandler("clientLoginB1", "B1");
80          LoginContext JavaDoc lc = new LoginContext JavaDoc("client-login", handler);
81          lc.login();
82          InitialContext JavaDoc ctx = new InitialContext JavaDoc();
83          IClientLoginHome home = (IClientLoginHome) ctx.lookup("java:comp/env/TargetBean");
84          IClientLogin bean = home.create();
85          Principal JavaDoc callerB1 = bean.callTarget();
86          if( callerB1.getName().equals("clientLoginB1") == false )
87             throw new RemoteException JavaDoc("callBeanAsClientLoginUser#1 != clientLoginB1");
88          lc.logout();
89
90          handler = new UsernamePasswordHandler("clientLoginB2", "B2");
91          lc = new LoginContext JavaDoc("client-login", handler);
92          lc.login();
93          Principal JavaDoc callerB2 = bean.callTarget();
94          if( callerB2.getName().equals("clientLoginB2") == false )
95             throw new RemoteException JavaDoc("callBeanAsClientLoginUser#2 != clientLoginB2");
96          lc.logout();
97
98          // Make sure the caller principal is the same
99
String JavaDoc inputName2 = context.getCallerPrincipal().getName();
100          if( inputName.equals(inputName2) == false )
101             throw new RemoteException JavaDoc("CallerPrincipal changed after logout");
102       }
103       catch(Exception JavaDoc e)
104       {
105          if(e instanceof RemoteException JavaDoc )
106             throw (RemoteException JavaDoc) e;
107          throw new RemoteException JavaDoc("callBeanAsClientLoginUser", e);
108       }
109       return caller;
110    }
111    public Principal JavaDoc callTarget() throws RemoteException JavaDoc
112    {
113       return null;
114    }
115
116 }
117
Popular Tags