001: /*
002: * JOnAS: Java(TM) Open Application Server
003: * Copyright (C) 1999 Bull S.A.
004: * Contact: jonas-team@objectweb.org
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2.1 of the License, or any later version.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this library; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
019: * USA
020: *
021: * --------------------------------------------------------------------------
022: * $Id: BaseCommon.java 5996 2004-12-17 15:09:45Z joaninh $
023: * --------------------------------------------------------------------------
024: */
025:
026: // BaseCommon.java
027: package org.objectweb.jonas.jtests.beans.secured;
028:
029: import java.io.Serializable;
030: import java.rmi.RemoteException;
031: import java.security.Principal;
032: import java.util.Hashtable;
033:
034: import javax.ejb.EJBContext;
035: import javax.ejb.EJBException;
036: import javax.ejb.TimedObject;
037: import javax.ejb.Timer;
038: import javax.ejb.TimerService;
039: import javax.naming.Context;
040: import javax.naming.InitialContext;
041: import javax.rmi.PortableRemoteObject;
042:
043: import org.objectweb.util.monolog.api.BasicLevel;
044: import org.objectweb.util.monolog.api.Logger;
045:
046: public abstract class BaseCommon implements TimedObject {
047:
048: static Logger logger = null;
049: static protected int timercount = 0;
050:
051: public abstract EJBContext getEJBContext();
052:
053: /**
054: * Name of the principal 1
055: */
056: protected static String PRINCIPAL1_NAME = "principal1";
057:
058: /**
059: * Name of the role 1
060: */
061: protected static String ROLE1_NAME = "role1";
062:
063: // ------------------------------------------------------------------
064: // Base implementation
065: // ------------------------------------------------------------------
066:
067: public String getPrincipalName() throws RemoteException {
068:
069: Principal callerPrincipal = getEJBContext()
070: .getCallerPrincipal();
071: logger.log(BasicLevel.DEBUG, "principal = "
072: + callerPrincipal.getName());
073: return callerPrincipal.getName();
074: }
075:
076: public String localGetPrincipalName() {
077: Principal callerPrincipal = getEJBContext()
078: .getCallerPrincipal();
079: logger.log(BasicLevel.DEBUG, "principal = "
080: + callerPrincipal.getName());
081: return callerPrincipal.getName();
082: }
083:
084: /*
085: * Access to the Derived bean
086: */
087:
088: public String getPrincipalNameOfAnotherBean()
089: throws RemoteException {
090: logger.log(BasicLevel.DEBUG, "");
091: try {
092: Context it = new InitialContext();
093: DerivedHome h = (DerivedHome) PortableRemoteObject.narrow(
094: it.lookup("securedDerivedSFHome"),
095: DerivedHome.class);
096: Derived other = h.create();
097: return other.getPrincipalName();
098: } catch (RemoteException e) {
099: throw e;
100: } catch (Exception e) {
101: e.printStackTrace();
102: return null;
103: }
104: }
105:
106: /**
107: * This method is in excluded list in DD
108: * @throws RemoteException if access failed
109: */
110: public void excludedMethod() throws RemoteException {
111: logger.log(BasicLevel.DEBUG, "");
112: }
113:
114: /**
115: * Access to a local interface of a bean
116: * we must catch access denied exception
117: */
118: public boolean callAnotherMethod() throws RemoteException {
119: logger.log(BasicLevel.DEBUG, "");
120: try {
121: Context it = new InitialContext();
122: DerivedLocalHome h = (DerivedLocalHome) it
123: .lookup("java:comp/env/ejb/derivedlocal");
124: DerivedLocal other = h.create();
125: other.anotherMethod();
126: } catch (EJBException e) {
127: return false;
128: } catch (Exception e) {
129: e.printStackTrace();
130: return false;
131: }
132: return true;
133: }
134:
135: public boolean isCallerInRole(String role) throws RemoteException {
136: logger.log(BasicLevel.DEBUG, "isCallerInRole " + role);
137: return getEJBContext().isCallerInRole(role);
138: }
139:
140: public boolean localIsCallerInRole(String role) {
141: logger.log(BasicLevel.DEBUG, "isCallerInRole " + role);
142: return getEJBContext().isCallerInRole(role);
143: }
144:
145: public void simpleMethod() throws RemoteException {
146: logger.log(BasicLevel.DEBUG, "");
147: }
148:
149: public void complexMethod(Hashtable h, Object[] o)
150: throws RemoteException {
151: logger.log(BasicLevel.DEBUG, "");
152: }
153:
154: public int getTimerCount() throws RemoteException {
155: logger.log(BasicLevel.DEBUG, "");
156: return timercount;
157: }
158:
159: public int setTimer(int dur, int period, int action)
160: throws RemoteException {
161: logger.log(BasicLevel.DEBUG, "");
162: TimerService timerservice = getEJBContext().getTimerService();
163: if (period > 0) {
164: timerservice.createTimer(dur * 1000, period * 1000,
165: new Integer(action));
166: } else {
167: timerservice.createTimer(dur * 1000, new Integer(action));
168: }
169: return action;
170: }
171:
172: // -----------------------------------------------------------
173: // TimedObject implementation
174: // -----------------------------------------------------------
175:
176: /**
177: * A timer is expired.
178: */
179: public void ejbTimeout(Timer timer) {
180: logger.log(BasicLevel.DEBUG, "");
181: Serializable sz = timer.getInfo();
182: if (!(sz instanceof Integer)) {
183: logger.log(BasicLevel.ERROR, "Bad Info");
184: return;
185: }
186: int action = ((Integer) sz).intValue();
187: boolean ok = true;
188: switch (action) {
189: case 0:
190: try {
191: simpleMethod();
192: } catch (RemoteException e) {
193: logger.log(BasicLevel.ERROR,
194: "Can't call a simple Method");
195: return;
196: }
197: break;
198: case 1:
199: try {
200: ok = callBeanNoRunAsWithRole1();
201: } catch (RemoteException e) {
202: logger.log(BasicLevel.ERROR, "run as role1 failed");
203: return;
204: }
205: break;
206: case 2:
207: try {
208: ok = callBeanNoRunAsWithRole2();
209: } catch (RemoteException e) {
210: logger.log(BasicLevel.ERROR, "run as role2 failed");
211: return;
212: }
213: break;
214: }
215: if (ok) {
216: timercount++;
217: }
218: }
219:
220: // ------------------------------------------------------------------
221: // Methods for runAs test
222: // ------------------------------------------------------------------
223:
224: // Execute a method without runAs on the derived bean
225: public boolean callBeanNoRunAsWithRole1() throws RemoteException {
226: logger.log(BasicLevel.DEBUG, "");
227: try {
228: Context ictx = new InitialContext();
229: DerivedLocalHome h = (DerivedLocalHome) ictx
230: .lookup("java:comp/env/ejb/derivednorunaslocal");
231: DerivedLocal other = h.create();
232: other.noRunAsWithRole1();
233: if (!(getPrincipalName().equals(other
234: .localGetPrincipalName()))) {
235: throw new Exception(
236: "Principal of the other bean must be the same");
237: }
238: if (!other.localIsCallerInRole("role1")) {
239: throw new Exception(
240: "Role of the other bean must be role1");
241: }
242: } catch (EJBException e) {
243: return false;
244: } catch (Exception e) {
245: e.printStackTrace();
246: return false;
247: }
248: return true;
249: }
250:
251: // Execute a method with runAs on the derived bean
252: public boolean callBeanNoRunAsWithRole2() throws RemoteException {
253: logger.log(BasicLevel.DEBUG, "");
254: try {
255: Context ictx = new InitialContext();
256: DerivedLocalHome h = (DerivedLocalHome) ictx
257: .lookup("java:comp/env/ejb/derivednorunaslocal");
258: DerivedLocal other = h.create();
259: other.noRunAsWithRole2();
260: // run as is executed with principal = (runas role) = role1
261: if (!("role1".equals(other.localGetPrincipalName()))) {
262: throw new Exception(
263: "Principal of the other bean must be the same");
264: }
265: // run as is executed with role role1
266: if (!other.localIsCallerInRole("role1")) {
267: throw new Exception(
268: "Role of the other bean must be role2");
269: }
270: } catch (EJBException e) {
271: return false;
272: } catch (Exception e) {
273: e.printStackTrace();
274: return false;
275: }
276: return true;
277: }
278:
279: // Execute a method without being in runAs mode on a runAs derived bean
280: public boolean callBeanRunAsWithRole1() throws RemoteException {
281: logger.log(BasicLevel.DEBUG, "");
282: try {
283: Context ictx = new InitialContext();
284: DerivedLocalHome h = (DerivedLocalHome) ictx
285: .lookup("java:comp/env/ejb/derivedrunaslocal");
286: try {
287: DerivedLocal other = h.create();
288: } catch (EJBException e) {
289: // Can't create a bean which require role2 with role1
290: // It's ok
291: return true;
292: }
293: } catch (Exception e) {
294: e.printStackTrace();
295: return false;
296: }
297: // Must not work
298: return false;
299: }
300:
301: // Execute a method without being in runAs mode on a runAs derived bean
302: public boolean callBeanRunAsWithRole2() throws RemoteException {
303: logger.log(BasicLevel.DEBUG, "");
304: try {
305: Context ictx = new InitialContext();
306: DerivedLocalHome h = (DerivedLocalHome) ictx
307: .lookup("java:comp/env/ejb/derivedrunas2local");
308: try {
309: DerivedLocal other = h.create();
310: other.runAsWithRole2();
311: if (!(getPrincipalName().equals(other
312: .localGetPrincipalName()))) {
313: throw new Exception(
314: "Principal of the other bean must be the same");
315: }
316: if (!other.localIsCallerInRole("role2")) {
317: throw new Exception(
318: "Role of the other bean must be role2");
319: }
320: } catch (EJBException e) {
321: // Can't call bean which need role2 as we run with role1 (run-as role)
322: return true;
323: }
324: } catch (Exception e) {
325: e.printStackTrace();
326: return false;
327: }
328: return false;
329: }
330:
331: }
|