KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jonas > jtests > beans > secured > BaseCommon


1 /*
2  * JOnAS: Java(TM) Open Application Server
3  * Copyright (C) 1999 Bull S.A.
4  * Contact: jonas-team@objectweb.org
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19  * USA
20  *
21  * --------------------------------------------------------------------------
22  * $Id: BaseCommon.java,v 1.14 2004/12/17 15:09:45 joaninh Exp $
23  * --------------------------------------------------------------------------
24  */

25
26 // BaseCommon.java
27

28 package org.objectweb.jonas.jtests.beans.secured;
29
30 import java.io.Serializable JavaDoc;
31 import java.rmi.RemoteException JavaDoc;
32 import java.security.Principal JavaDoc;
33 import java.util.Hashtable JavaDoc;
34
35 import javax.ejb.EJBContext JavaDoc;
36 import javax.ejb.EJBException JavaDoc;
37 import javax.ejb.TimedObject JavaDoc;
38 import javax.ejb.Timer JavaDoc;
39 import javax.ejb.TimerService JavaDoc;
40 import javax.naming.Context JavaDoc;
41 import javax.naming.InitialContext JavaDoc;
42 import javax.rmi.PortableRemoteObject JavaDoc;
43
44 import org.objectweb.util.monolog.api.BasicLevel;
45 import org.objectweb.util.monolog.api.Logger;
46
47 public abstract class BaseCommon implements TimedObject JavaDoc {
48
49     static Logger logger = null;
50     static protected int timercount = 0;
51
52     public abstract EJBContext JavaDoc getEJBContext();
53
54     /**
55      * Name of the principal 1
56      */

57     protected static String JavaDoc PRINCIPAL1_NAME = "principal1";
58
59
60     /**
61      * Name of the role 1
62      */

63     protected static String JavaDoc ROLE1_NAME = "role1";
64
65     // ------------------------------------------------------------------
66
// Base implementation
67
// ------------------------------------------------------------------
68

69     public String JavaDoc getPrincipalName() throws RemoteException JavaDoc {
70
71         Principal JavaDoc callerPrincipal = getEJBContext().getCallerPrincipal();
72         logger.log(BasicLevel.DEBUG, "principal = "+callerPrincipal.getName());
73         return callerPrincipal.getName();
74     }
75
76     public String JavaDoc localGetPrincipalName() {
77         Principal JavaDoc callerPrincipal = getEJBContext().getCallerPrincipal();
78         logger.log(BasicLevel.DEBUG, "principal = "+callerPrincipal.getName());
79         return callerPrincipal.getName();
80     }
81
82     /*
83      * Access to the Derived bean
84      */

85
86     public String JavaDoc getPrincipalNameOfAnotherBean() throws RemoteException JavaDoc {
87         logger.log(BasicLevel.DEBUG, "");
88         try {
89             Context JavaDoc it = new InitialContext JavaDoc();
90             DerivedHome h = (DerivedHome) PortableRemoteObject.narrow(it.lookup("securedDerivedSFHome"),
91                                                                       DerivedHome.class);
92             Derived other = h.create();
93             return other.getPrincipalName();
94         } catch (RemoteException JavaDoc e) {
95             throw e;
96         } catch (Exception JavaDoc e) {
97             e.printStackTrace();
98             return null;
99         }
100     }
101
102     /**
103      * This method is in excluded list in DD
104      * @throws RemoteException if access failed
105      */

106     public void excludedMethod() throws RemoteException JavaDoc {
107         logger.log(BasicLevel.DEBUG, "");
108     }
109         
110     /**
111      * Access to a local interface of a bean
112      * we must catch access denied exception
113      */

114     public boolean callAnotherMethod() throws RemoteException JavaDoc {
115         logger.log(BasicLevel.DEBUG, "");
116         try {
117         Context JavaDoc it = new InitialContext JavaDoc();
118         DerivedLocalHome h = (DerivedLocalHome)it.lookup("java:comp/env/ejb/derivedlocal");
119         DerivedLocal other = h.create();
120         other.anotherMethod();
121         } catch (EJBException JavaDoc e) {
122         return false;
123         }catch (Exception JavaDoc e) {
124         e.printStackTrace();
125             return false;
126     }
127         return true;
128     }
129
130     public boolean isCallerInRole(String JavaDoc role) throws RemoteException JavaDoc {
131         logger.log(BasicLevel.DEBUG, "isCallerInRole "+role);
132         return getEJBContext().isCallerInRole(role);
133     }
134
135     public boolean localIsCallerInRole(String JavaDoc role) {
136         logger.log(BasicLevel.DEBUG, "isCallerInRole "+role);
137         return getEJBContext().isCallerInRole(role);
138     }
139
140     public void simpleMethod() throws RemoteException JavaDoc {
141         logger.log(BasicLevel.DEBUG, "");
142     }
143
144     public void complexMethod(Hashtable JavaDoc h, Object JavaDoc[] o) throws RemoteException JavaDoc {
145         logger.log(BasicLevel.DEBUG, "");
146     }
147  
148     public int getTimerCount() throws RemoteException JavaDoc {
149         logger.log(BasicLevel.DEBUG, "");
150         return timercount;
151     }
152
153     public int setTimer(int dur, int period, int action) throws RemoteException JavaDoc {
154         logger.log(BasicLevel.DEBUG, "");
155         TimerService JavaDoc timerservice = getEJBContext().getTimerService();
156         if (period > 0) {
157             timerservice.createTimer(dur * 1000, period * 1000, new Integer JavaDoc(action));
158         } else {
159             timerservice.createTimer(dur * 1000, new Integer JavaDoc(action));
160         }
161         return action;
162     }
163
164     // -----------------------------------------------------------
165
// TimedObject implementation
166
// -----------------------------------------------------------
167

168     /**
169      * A timer is expired.
170      */

171     public void ejbTimeout(Timer JavaDoc timer) {
172         logger.log(BasicLevel.DEBUG, "");
173         Serializable JavaDoc sz = timer.getInfo();
174         if (!(sz instanceof Integer JavaDoc)) {
175             logger.log(BasicLevel.ERROR, "Bad Info");
176             return;
177         }
178         int action = ((Integer JavaDoc)sz).intValue();
179         boolean ok = true;
180         switch (action) {
181             case 0:
182                 try {
183                     simpleMethod();
184                 } catch (RemoteException JavaDoc e) {
185                     logger.log(BasicLevel.ERROR, "Can't call a simple Method");
186                     return;
187                 }
188                 break;
189             case 1:
190                 try {
191                     ok = callBeanNoRunAsWithRole1();
192                 } catch (RemoteException JavaDoc e) {
193                     logger.log(BasicLevel.ERROR, "run as role1 failed");
194                     return;
195                 }
196                 break;
197             case 2:
198                 try {
199                     ok = callBeanNoRunAsWithRole2();
200                 } catch (RemoteException JavaDoc e) {
201                     logger.log(BasicLevel.ERROR, "run as role2 failed");
202                     return;
203                 }
204                 break;
205         }
206         if (ok) {
207             timercount++;
208         }
209     }
210
211     // ------------------------------------------------------------------
212
// Methods for runAs test
213
// ------------------------------------------------------------------
214

215
216     // Execute a method without runAs on the derived bean
217
public boolean callBeanNoRunAsWithRole1() throws RemoteException JavaDoc {
218         logger.log(BasicLevel.DEBUG, "");
219         try {
220             Context JavaDoc ictx = new InitialContext JavaDoc();
221             DerivedLocalHome h = (DerivedLocalHome) ictx.lookup("java:comp/env/ejb/derivednorunaslocal");
222             DerivedLocal other = h.create();
223             other.noRunAsWithRole1();
224             if (!(getPrincipalName().equals(other.localGetPrincipalName()))) {
225                 throw new Exception JavaDoc("Principal of the other bean must be the same");
226             }
227             if (!other.localIsCallerInRole("role1")) {
228                 throw new Exception JavaDoc("Role of the other bean must be role1");
229             }
230         } catch (EJBException JavaDoc e) {
231             return false;
232         }catch (Exception JavaDoc e) {
233             e.printStackTrace();
234             return false;
235         }
236         return true;
237     }
238
239     // Execute a method with runAs on the derived bean
240
public boolean callBeanNoRunAsWithRole2() throws RemoteException JavaDoc {
241         logger.log(BasicLevel.DEBUG, "");
242         try {
243             Context JavaDoc ictx = new InitialContext JavaDoc();
244             DerivedLocalHome h = (DerivedLocalHome) ictx.lookup("java:comp/env/ejb/derivednorunaslocal");
245             DerivedLocal other = h.create();
246             other.noRunAsWithRole2();
247             // run as is executed with principal = (runas role) = role1
248
if (!("role1".equals(other.localGetPrincipalName()))) {
249                 throw new Exception JavaDoc("Principal of the other bean must be the same");
250             }
251             // run as is executed with role role1
252
if (!other.localIsCallerInRole("role1")) {
253                 throw new Exception JavaDoc("Role of the other bean must be role2");
254             }
255         } catch (EJBException JavaDoc e) {
256             return false;
257         } catch (Exception JavaDoc e) {
258             e.printStackTrace();
259             return false;
260         }
261         return true;
262     }
263
264
265     // Execute a method without being in runAs mode on a runAs derived bean
266
public boolean callBeanRunAsWithRole1() throws RemoteException JavaDoc {
267         logger.log(BasicLevel.DEBUG, "");
268         try {
269             Context JavaDoc ictx = new InitialContext JavaDoc();
270             DerivedLocalHome h = (DerivedLocalHome)ictx.lookup("java:comp/env/ejb/derivedrunaslocal");
271             try {
272                 DerivedLocal other = h.create();
273             } catch (EJBException JavaDoc e) {
274                 // Can't create a bean which require role2 with role1
275
// It's ok
276
return true;
277             }
278         } catch (Exception JavaDoc e) {
279             e.printStackTrace();
280             return false;
281         }
282         // Must not work
283
return false;
284     }
285
286
287     // Execute a method without being in runAs mode on a runAs derived bean
288
public boolean callBeanRunAsWithRole2() throws RemoteException JavaDoc {
289         logger.log(BasicLevel.DEBUG, "");
290         try {
291             Context JavaDoc ictx = new InitialContext JavaDoc();
292             DerivedLocalHome h = (DerivedLocalHome) ictx.lookup("java:comp/env/ejb/derivedrunas2local");
293             try {
294                 DerivedLocal other = h.create();
295                 other.runAsWithRole2();
296                 if (!(getPrincipalName().equals(other.localGetPrincipalName()))) {
297                     throw new Exception JavaDoc("Principal of the other bean must be the same");
298                 }
299                 if (!other.localIsCallerInRole("role2")) {
300                     throw new Exception JavaDoc("Role of the other bean must be role2");
301                 }
302             } catch (EJBException JavaDoc e) {
303                 // Can't call bean which need role2 as we run with role1 (run-as role)
304
return true;
305             }
306         } catch (Exception JavaDoc e) {
307             e.printStackTrace();
308             return false;
309         }
310         return false;
311     }
312
313    
314 }
315
Popular Tags