KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > easybeans > examples > security > ClientSecurity


1 /**
2  * EasyBeans
3  * Copyright (C) 2006 Bull S.A.S.
4  * Contact: easybeans@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: ClientSecurity.java 1151 2006-10-11 13:37:06Z benoitf $
23  * --------------------------------------------------------------------------
24  */

25
26 package org.objectweb.easybeans.examples.security;
27
28 import java.util.Hashtable JavaDoc;
29
30 import javax.ejb.EJBAccessException JavaDoc;
31 import javax.naming.Context JavaDoc;
32 import javax.naming.InitialContext JavaDoc;
33 import javax.naming.NamingException JavaDoc;
34
35 /**
36  * Simple client of the stateless.
37  * @author Florent Benoit
38  */

39 public final class ClientSecurity {
40
41     /**
42      * Default InitialContextFactory to use.
43      */

44     private static final String JavaDoc DEFAULT_INITIAL_CONTEXT_FACTORY = "org.objectweb.carol.jndi.spi.MultiOrbInitialContextFactory";
45
46     /**
47      * Utility class.
48      */

49     private ClientSecurity() {
50
51     }
52
53     /**
54      * Main method.
55      * @param args the arguments (not required)
56      * @throws Exception if exception is found.
57      */

58     public static void main(final String JavaDoc[] args) throws Exception JavaDoc {
59         Context JavaDoc ictx = getInitialContext();
60
61         // JNDI name was specified as mappedName attribute in the stateless bean
62
StatelessRemote statelessBean = (StatelessRemote) ictx.lookup("securityBean");
63
64         // Not authenticated but method can be called
65
System.out.println("Calling methods that everybody can call...");
66         statelessBean.allRolesAllowed();
67
68         // Takes a new 'admin' identity to call restricted methods.
69
System.out.println("Call a bean with run-as in order to have 'admin' role...");
70         statelessBean.callRunAsBean();
71
72         // Call methods that is denied for all
73
try {
74             statelessBean.deniedForAll();
75             System.out.println("Access granted which is not expected");
76         } catch (EJBAccessException JavaDoc e) {
77             System.out.println("Access denied as expected (method is denied)");
78         }
79
80     }
81
82     /**
83      * @return Returns the InitialContext.
84      * @throws NamingException If the Context cannot be created.
85      */

86     private static Context JavaDoc getInitialContext() throws NamingException JavaDoc {
87
88         // if user don't use jclient/client container
89
// we can specify the InitialContextFactory to use
90
// But this is *not recommended*.
91
Hashtable JavaDoc<String JavaDoc, Object JavaDoc> env = new Hashtable JavaDoc<String JavaDoc, Object JavaDoc>();
92         env.put(Context.INITIAL_CONTEXT_FACTORY, getInitialContextFactory());
93
94         // Usually a simple new InitialContext() without any parameters is sufficent.
95
// return new InitialContext();
96

97         return new InitialContext JavaDoc(env);
98     }
99
100     /**
101      * Returns a configurable InitialContextFactory classname.<br/>
102      * Can be configured with the <code>easybeans.client.initial-context-factory</code> System property.
103      * @return Returns a configurable InitialContextFactory classname.
104      */

105     private static String JavaDoc getInitialContextFactory() {
106         String JavaDoc prop = System.getProperty("easybeans.client.initial-context-factory");
107         // If not found, use the default
108
if (prop == null) {
109             prop = DEFAULT_INITIAL_CONTEXT_FACTORY;
110         }
111         return prop;
112     }
113
114
115 }
116
Popular Tags