1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package sample.contact;
17
18 import org.acegisecurity.Authentication;
19 import org.acegisecurity.context.SecurityContextHolder;
20 import org.acegisecurity.context.SecurityContextImpl;
21 import org.acegisecurity.providers.UsernamePasswordAuthenticationToken;
22
23 import org.springframework.beans.factory.ListableBeanFactory;
24
25 import org.springframework.context.support.FileSystemXmlApplicationContext;
26
27 import org.springframework.util.StopWatch;
28
29 import java.lang.reflect.InvocationTargetException;
30 import java.lang.reflect.Method;
31
32 import java.util.Iterator;
33 import java.util.List;
34 import java.util.Map;
35
36
37 /***
38 * Demonstrates accessing the {@link ContactManager} via remoting protocols.
39 *
40 * <P>
41 * Based on Spring's JPetStore sample, written by Juergen Hoeller.
42 * </p>
43 *
44 * @author Ben Alex
45 */
46 public class ClientApplication {
47
48
49 private final ListableBeanFactory beanFactory;
50
51
52
53 public ClientApplication(ListableBeanFactory beanFactory) {
54 this.beanFactory = beanFactory;
55 }
56
57
58
59 public void invokeContactManager(Authentication authentication,
60 int nrOfCalls) {
61 StopWatch stopWatch = new StopWatch(nrOfCalls
62 + " ContactManager call(s)");
63 Map contactServices = this.beanFactory.getBeansOfType(ContactManager.class,
64 true, true);
65
66 SecurityContextHolder.getContext().setAuthentication(authentication);
67
68 for (Iterator it = contactServices.keySet().iterator(); it.hasNext();) {
69 String beanName = (String) it.next();
70
71 Object object = this.beanFactory.getBean("&" + beanName);
72
73 try {
74 System.out.println(
75 "Trying to find setUsername(String) method on: "
76 + object.getClass().getName());
77
78 Method method = object.getClass().getMethod("setUsername",
79 new Class[] {String.class});
80 System.out.println("Found; Trying to setUsername(String) to "
81 + authentication.getPrincipal());
82 method.invoke(object,
83 new Object[] {authentication.getPrincipal()});
84 } catch (NoSuchMethodException ignored) {
85 System.out.println(
86 "This client proxy factory does not have a setUsername(String) method");
87 } catch (IllegalAccessException ignored) {
88 ignored.printStackTrace();
89 } catch (InvocationTargetException ignored) {
90 ignored.printStackTrace();
91 }
92
93 try {
94 System.out.println(
95 "Trying to find setPassword(String) method on: "
96 + object.getClass().getName());
97
98 Method method = object.getClass().getMethod("setPassword",
99 new Class[] {String.class});
100 method.invoke(object,
101 new Object[] {authentication.getCredentials()});
102 System.out.println("Found; Trying to setPassword(String) to "
103 + authentication.getCredentials());
104 } catch (NoSuchMethodException ignored) {
105 System.out.println(
106 "This client proxy factory does not have a setPassword(String) method");
107 } catch (IllegalAccessException ignored) {}
108 catch (InvocationTargetException ignored) {}
109
110 ContactManager remoteContactManager = (ContactManager) contactServices
111 .get(beanName);
112 System.out.println("Calling ContactManager '" + beanName + "'");
113
114 stopWatch.start(beanName);
115
116 List contacts = null;
117
118 for (int i = 0; i < nrOfCalls; i++) {
119 contacts = remoteContactManager.getAll();
120 }
121
122 stopWatch.stop();
123
124 if (contacts.size() != 0) {
125 Iterator listIterator = contacts.iterator();
126
127 while (listIterator.hasNext()) {
128 Contact contact = (Contact) listIterator.next();
129 System.out.println("Contact: " + contact.toString());
130 }
131 } else {
132 System.out.println(
133 "No contacts found which this user has permission to");
134 }
135
136 System.out.println();
137 System.out.println(stopWatch.prettyPrint());
138 }
139
140 SecurityContextHolder.setContext(new SecurityContextImpl());
141 }
142
143 public static void main(String[] args) {
144 String username = System.getProperty("username", "");
145 String password = System.getProperty("password", "");
146 String nrOfCallsString = System.getProperty("nrOfCalls", "");
147
148 if ("".equals(username) || "".equals(password)) {
149 System.out.println(
150 "You need to specify the user ID to use, the password to use, and optionally a number of calls "
151 + "using the username, password, and nrOfCalls system properties respectively. eg for user marissa, "
152 + "use: -Dusername=marissa -Dpassword=koala' for a single call per service and "
153 + "use: -Dusername=marissa -Dpassword=koala -DnrOfCalls=10 for ten calls per service.");
154 System.exit(-1);
155 } else {
156 int nrOfCalls = 1;
157
158 if (!"".equals(nrOfCallsString)) {
159 nrOfCalls = Integer.parseInt(nrOfCallsString);
160 }
161
162 ListableBeanFactory beanFactory = new FileSystemXmlApplicationContext(
163 "clientContext.xml");
164 ClientApplication client = new ClientApplication(beanFactory);
165
166 client.invokeContactManager(new UsernamePasswordAuthenticationToken(
167 username, password), nrOfCalls);
168 System.exit(0);
169 }
170 }
171 }