KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > tutorial > entity > security > client > Client


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 package org.jboss.tutorial.entity.security.client;
23
24 import org.jboss.tutorial.entity.security.bean.AllEntity;
25 import org.jboss.tutorial.entity.security.bean.SomeEntity;
26 import org.jboss.tutorial.entity.security.bean.StarEntity;
27 import org.jboss.tutorial.entity.security.bean.Stateless;
28 import org.jboss.security.SecurityAssociation;
29 import org.jboss.security.SimplePrincipal;
30
31 import javax.naming.Context JavaDoc;
32 import javax.naming.InitialContext JavaDoc;
33 import java.util.Properties JavaDoc;
34
35 /**
36  *
37  * @author <a HREF="mailto:kabir.khan@jboss.org">Kabir Khan</a>
38  * @version $Revision: 45433 $
39  */

40 public class Client
41 {
42
43    public static void main(String JavaDoc[] args) throws Exception JavaDoc
44    {
45       testAllEntity();
46       testSomeEntity();
47       testStarEntity();
48    }
49
50    public static InitialContext JavaDoc getInitialContext(String JavaDoc username, String JavaDoc password) throws Exception JavaDoc
51    {
52       Properties JavaDoc env = new Properties JavaDoc();
53       env.setProperty(Context.SECURITY_PRINCIPAL, username);
54       env.setProperty(Context.SECURITY_CREDENTIALS, password);
55       env.setProperty(Context.INITIAL_CONTEXT_FACTORY, "org.jboss.security.jndi.JndiLoginInitialContextFactory");
56       return new InitialContext JavaDoc(env);
57
58    }
59
60    public static void testAllEntity()throws Exception JavaDoc
61    {
62       InitialContext JavaDoc ctx = getInitialContext("somebody", "password");
63       Stateless stateless = (Stateless)ctx.lookup("StatelessBean/remote");
64
65       System.out.println("Good role");
66       System.out.println("Inserting...");
67       AllEntity e = stateless.insertAllEntity();
68       System.out.println("Reading...");
69       e = stateless.readAllEntity(e.id);
70       e.val += "y";
71       System.out.println("Updating...");
72       stateless.updateAllEntity(e);
73       System.out.println("Deleting...");
74       stateless.deleteAllEntity(e);
75       System.out.println("Inserting...");
76       e = stateless.insertAllEntity();
77
78       System.out.println("Bad role");
79       getInitialContext("rolefail", "password");
80
81       AllEntity ae2 = null;
82       try
83       {
84          System.out.println("Inserting...");
85          ae2 = stateless.insertAllEntity();
86          throw new RuntimeException JavaDoc("security exception should have been thrown!");
87       }
88       catch(Exception JavaDoc ex)
89       {
90          System.out.println("Expected failure: " + ex.getMessage());
91       }
92
93       try
94       {
95          System.out.println("Reading...");
96          ae2 = stateless.readAllEntity(e.id);
97          throw new RuntimeException JavaDoc("security exception should have been thrown!");
98       }
99       catch(Exception JavaDoc ex)
100       {
101          System.out.println("Expected failure: " + ex.getMessage());
102       }
103
104       try
105       {
106          e.val += "y";
107          stateless.updateAllEntity(e);
108          throw new RuntimeException JavaDoc("security exception should have been thrown!");
109       }
110       catch(Exception JavaDoc ex)
111       {
112          System.out.println("Expected failure: " + ex.getMessage());
113       }
114
115       try
116       {
117          stateless.deleteAllEntity(e);
118          throw new RuntimeException JavaDoc("security exception should have been thrown!");
119       }
120       catch(Exception JavaDoc ex)
121       {
122          System.out.println("Expected failure: " + ex.getMessage());
123       }
124
125       try
126       {
127          e = stateless.insertAllEntity();
128          throw new RuntimeException JavaDoc("security exception should have been thrown!");
129       }
130       catch(Exception JavaDoc ex)
131       {
132          System.out.println("Expected failure: " + ex.getMessage());
133       }
134    }
135
136
137    public static void testStarEntity()throws Exception JavaDoc
138    {
139       InitialContext JavaDoc ctx = getInitialContext("somebody", "password");
140       Stateless stateless = (Stateless)ctx.lookup("StatelessBean/remote");
141
142       System.out.println("Good role");
143       System.out.println("Inserting...");
144       StarEntity e = stateless.insertStarEntity();
145       System.out.println("Reading...");
146       e = stateless.readStarEntity(e.id);
147       e.val += "y";
148       System.out.println("Updating...");
149       stateless.updateStarEntity(e);
150       System.out.println("Deleting...");
151       stateless.deleteStarEntity(e);
152       System.out.println("Inserting...");
153       e = stateless.insertStarEntity();
154
155       System.out.println("Bad role");
156       getInitialContext("rolefail", "password");
157
158       StarEntity ae2 = null;
159       try
160       {
161          System.out.println("Inserting...");
162          ae2 = stateless.insertStarEntity();
163          throw new RuntimeException JavaDoc("security exception should have been thrown!");
164       }
165       catch(Exception JavaDoc ex)
166       {
167          System.out.println("Expected failure: " + ex.getMessage());
168       }
169
170       try
171       {
172          System.out.println("Reading...");
173          ae2 = stateless.readStarEntity(e.id);
174          throw new RuntimeException JavaDoc("security exception should have been thrown!");
175       }
176       catch(Exception JavaDoc ex)
177       {
178          System.out.println("Expected failure: " + ex.getMessage());
179       }
180
181       try
182       {
183          e.val += "y";
184          stateless.updateStarEntity(e);
185          throw new RuntimeException JavaDoc("security exception should have been thrown!");
186       }
187       catch(Exception JavaDoc ex)
188       {
189          System.out.println("Expected failure: " + ex.getMessage());
190       }
191
192       try
193       {
194          stateless.deleteStarEntity(e);
195          throw new RuntimeException JavaDoc("security exception should have been thrown!");
196       }
197       catch(Exception JavaDoc ex)
198       {
199          System.out.println("Expected failure: " + ex.getMessage());
200       }
201
202       try
203       {
204          e = stateless.insertStarEntity();
205          throw new RuntimeException JavaDoc("security exception should have been thrown!");
206       }
207       catch(Exception JavaDoc ex)
208       {
209          System.out.println("Expected failure: " + ex.getMessage());
210       }
211    }
212
213    public static void testSomeEntity()throws Exception JavaDoc
214    {
215       InitialContext JavaDoc ctx = getInitialContext("somebody", "password");
216       Stateless stateless = (Stateless)ctx.lookup("StatelessBean/remote");
217
218       System.out.println("Good role");
219       System.out.println("Inserting...");
220       SomeEntity e = stateless.insertSomeEntity();
221
222       try
223       {
224          System.out.println("Reading...");
225          e = stateless.readSomeEntity(e.id);
226          throw new RuntimeException JavaDoc("security exception should have been thrown!");
227       }
228       catch(Exception JavaDoc ex)
229       {
230          System.out.println("Expected failure: " + ex.getMessage());
231       }
232
233       try
234       {
235          e.val += "y";
236          System.out.println("Updating...");
237          stateless.updateSomeEntity(e);
238          throw new RuntimeException JavaDoc("security exception should have been thrown!");
239       }
240       catch(Exception JavaDoc ex)
241       {
242          System.out.println("Expected failure: " + ex.getMessage());
243       }
244
245
246       System.out.println("Deleting...");
247       stateless.deleteSomeEntity(e);
248       System.out.println("Inserting...");
249       e = stateless.insertSomeEntity();
250
251       System.out.println("Bad role");
252       getInitialContext("rolefail", "password");
253
254       SomeEntity ae2 = null;
255       try
256       {
257          System.out.println("Inserting...");
258          ae2 = stateless.insertSomeEntity();
259          throw new RuntimeException JavaDoc("security exception should have been thrown!");
260       }
261       catch(Exception JavaDoc ex)
262       {
263          System.out.println("Expected failure: " + ex.getMessage());
264       }
265
266       try
267       {
268          stateless.deleteSomeEntity(e);
269          throw new RuntimeException JavaDoc("security exception should have been thrown!");
270       }
271       catch(Exception JavaDoc ex)
272       {
273          System.out.println("Expected failure: " + ex.getMessage());
274       }
275
276       try
277       {
278          e = stateless.insertSomeEntity();
279          throw new RuntimeException JavaDoc("security exception should have been thrown!");
280       }
281       catch(Exception JavaDoc ex)
282       {
283          System.out.println("Expected failure: " + ex.getMessage());
284       }
285    }
286 }
287
Popular Tags