001: /**
002: * EasyBeans
003: * Copyright (C) 2006 Bull S.A.S.
004: * Contact: easybeans@ow2.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: ClientSecurity.java 1970 2007-10-16 11:49:25Z benoitf $
023: * --------------------------------------------------------------------------
024: */package org.ow2.easybeans.examples.security;
025:
026: import java.util.Hashtable;
027:
028: import javax.ejb.EJBAccessException;
029: import javax.naming.Context;
030: import javax.naming.InitialContext;
031: import javax.naming.NamingException;
032:
033: /**
034: * Simple client of the stateless.
035: * @author Florent Benoit
036: */
037: public final class ClientSecurity {
038:
039: /**
040: * Default InitialContextFactory to use.
041: */
042: private static final String DEFAULT_INITIAL_CONTEXT_FACTORY = "org.objectweb.carol.jndi.spi.MultiOrbInitialContextFactory";
043:
044: /**
045: * Utility class.
046: */
047: private ClientSecurity() {
048:
049: }
050:
051: /**
052: * Main method.
053: * @param args the arguments (not required)
054: * @throws Exception if exception is found.
055: */
056: public static void main(final String[] args) throws Exception {
057: Context ictx = getInitialContext();
058:
059: // JNDI name was specified as mappedName attribute in the stateless bean
060: StatelessRemote statelessBean = (StatelessRemote) ictx
061: .lookup("securityBean");
062:
063: // Not authenticated but method can be called
064: System.out
065: .println("Calling methods that everybody can call...");
066: statelessBean.allRolesAllowed();
067:
068: // Takes a new 'admin' identity to call restricted methods.
069: System.out
070: .println("Call a bean with run-as in order to have 'admin' role...");
071: statelessBean.callRunAsBean();
072:
073: // Call methods that is denied for all
074: try {
075: statelessBean.deniedForAll();
076: System.out.println("Access granted which is not expected");
077: } catch (EJBAccessException e) {
078: System.out
079: .println("Access denied as expected (method is denied)");
080: }
081:
082: }
083:
084: /**
085: * @return Returns the InitialContext.
086: * @throws NamingException If the Context cannot be created.
087: */
088: private static Context getInitialContext() throws NamingException {
089:
090: // if user don't use jclient/client container
091: // we can specify the InitialContextFactory to use
092: // But this is *not recommended*.
093: Hashtable<String, Object> env = new Hashtable<String, Object>();
094: env.put(Context.INITIAL_CONTEXT_FACTORY,
095: getInitialContextFactory());
096:
097: // Usually a simple new InitialContext() without any parameters is sufficent.
098: // return new InitialContext();
099:
100: return new InitialContext(env);
101: }
102:
103: /**
104: * Returns a configurable InitialContextFactory classname.<br/>
105: * Can be configured with the <code>easybeans.client.initial-context-factory</code> System property.
106: * @return Returns a configurable InitialContextFactory classname.
107: */
108: private static String getInitialContextFactory() {
109: String prop = System
110: .getProperty("easybeans.client.initial-context-factory");
111: // If not found, use the default
112: if (prop == null) {
113: prop = DEFAULT_INITIAL_CONTEXT_FACTORY;
114: }
115: return prop;
116: }
117:
118: }
|