001: /* Copyright 2004, 2005, 2006 Acegi Technology Pty Limited
002: *
003: * Licensed under the Apache License, Version 2.0 (the "License");
004: * you may not use this file except in compliance with the License.
005: * You may obtain a copy of the License at
006: *
007: * http://www.apache.org/licenses/LICENSE-2.0
008: *
009: * Unless required by applicable law or agreed to in writing, software
010: * distributed under the License is distributed on an "AS IS" BASIS,
011: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012: * See the License for the specific language governing permissions and
013: * limitations under the License.
014: */
015:
016: package sample.contact;
017:
018: import org.acegisecurity.Authentication;
019:
020: import org.acegisecurity.context.SecurityContextHolder;
021:
022: import org.acegisecurity.providers.UsernamePasswordAuthenticationToken;
023:
024: import org.springframework.beans.factory.ListableBeanFactory;
025:
026: import org.springframework.context.support.FileSystemXmlApplicationContext;
027:
028: import org.springframework.util.StopWatch;
029:
030: import java.lang.reflect.InvocationTargetException;
031: import java.lang.reflect.Method;
032:
033: import java.util.Iterator;
034: import java.util.List;
035: import java.util.Map;
036:
037: /**
038: * Demonstrates accessing the {@link ContactManager} via remoting protocols.<P>Based on Spring's JPetStore sample,
039: * written by Juergen Hoeller.</p>
040: *
041: * @author Ben Alex
042: */
043: public class ClientApplication {
044: //~ Instance fields ================================================================================================
045:
046: private final ListableBeanFactory beanFactory;
047:
048: //~ Constructors ===================================================================================================
049:
050: public ClientApplication(ListableBeanFactory beanFactory) {
051: this .beanFactory = beanFactory;
052: }
053:
054: //~ Methods ========================================================================================================
055:
056: public void invokeContactManager(Authentication authentication,
057: int nrOfCalls) {
058: StopWatch stopWatch = new StopWatch(nrOfCalls
059: + " ContactManager call(s)");
060: Map contactServices = this .beanFactory.getBeansOfType(
061: ContactManager.class, true, true);
062:
063: SecurityContextHolder.getContext().setAuthentication(
064: authentication);
065:
066: for (Iterator it = contactServices.keySet().iterator(); it
067: .hasNext();) {
068: String beanName = (String) it.next();
069:
070: Object object = this .beanFactory.getBean("&" + beanName);
071:
072: try {
073: System.out
074: .println("Trying to find setUsername(String) method on: "
075: + object.getClass().getName());
076:
077: Method method = object.getClass().getMethod(
078: "setUsername", new Class[] { String.class });
079: System.out
080: .println("Found; Trying to setUsername(String) to "
081: + authentication.getPrincipal());
082: method.invoke(object, new Object[] { authentication
083: .getPrincipal() });
084: } catch (NoSuchMethodException ignored) {
085: System.out
086: .println("This client proxy factory does not have a setUsername(String) method");
087: } catch (IllegalAccessException ignored) {
088: ignored.printStackTrace();
089: } catch (InvocationTargetException ignored) {
090: ignored.printStackTrace();
091: }
092:
093: try {
094: System.out
095: .println("Trying to find setPassword(String) method on: "
096: + object.getClass().getName());
097:
098: Method method = object.getClass().getMethod(
099: "setPassword", new Class[] { String.class });
100: method.invoke(object, new Object[] { authentication
101: .getCredentials() });
102: System.out
103: .println("Found; Trying to setPassword(String) to "
104: + authentication.getCredentials());
105: } catch (NoSuchMethodException ignored) {
106: System.out
107: .println("This client proxy factory does not have a setPassword(String) method");
108: } catch (IllegalAccessException ignored) {
109: } catch (InvocationTargetException ignored) {
110: }
111:
112: ContactManager remoteContactManager = (ContactManager) contactServices
113: .get(beanName);
114: System.out.println("Calling ContactManager '" + beanName
115: + "'");
116:
117: stopWatch.start(beanName);
118:
119: List contacts = null;
120:
121: for (int i = 0; i < nrOfCalls; i++) {
122: contacts = remoteContactManager.getAll();
123: }
124:
125: stopWatch.stop();
126:
127: if (contacts.size() != 0) {
128: Iterator listIterator = contacts.iterator();
129:
130: while (listIterator.hasNext()) {
131: Contact contact = (Contact) listIterator.next();
132: System.out
133: .println("Contact: " + contact.toString());
134: }
135: } else {
136: System.out
137: .println("No contacts found which this user has permission to");
138: }
139:
140: System.out.println();
141: System.out.println(stopWatch.prettyPrint());
142: }
143:
144: SecurityContextHolder.clearContext();
145: }
146:
147: public static void main(String[] args) {
148: String username = System.getProperty("username", "");
149: String password = System.getProperty("password", "");
150: String nrOfCallsString = System.getProperty("nrOfCalls", "");
151:
152: if ("".equals(username) || "".equals(password)) {
153: System.out
154: .println("You need to specify the user ID to use, the password to use, and optionally a number of calls "
155: + "using the username, password, and nrOfCalls system properties respectively. eg for user marissa, "
156: + "use: -Dusername=marissa -Dpassword=koala' for a single call per service and "
157: + "use: -Dusername=marissa -Dpassword=koala -DnrOfCalls=10 for ten calls per service.");
158: System.exit(-1);
159: } else {
160: int nrOfCalls = 1;
161:
162: if (!"".equals(nrOfCallsString)) {
163: nrOfCalls = Integer.parseInt(nrOfCallsString);
164: }
165:
166: ListableBeanFactory beanFactory = new FileSystemXmlApplicationContext(
167: "clientContext.xml");
168: ClientApplication client = new ClientApplication(
169: beanFactory);
170:
171: client.invokeContactManager(
172: new UsernamePasswordAuthenticationToken(username,
173: password), nrOfCalls);
174: System.exit(0);
175: }
176: }
177: }
|