ClientApplication.java 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. /* Copyright 2004, 2005 Acegi Technology Pty Limited
  2. *
  3. * Licensed under the Apache License, Version 2.0 (the "License");
  4. * you may not use this file except in compliance with the License.
  5. * You may obtain a copy of the License at
  6. *
  7. * http://www.apache.org/licenses/LICENSE-2.0
  8. *
  9. * Unless required by applicable law or agreed to in writing, software
  10. * distributed under the License is distributed on an "AS IS" BASIS,
  11. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. * See the License for the specific language governing permissions and
  13. * limitations under the License.
  14. */
  15. package sample.contact;
  16. import org.acegisecurity.Authentication;
  17. import org.acegisecurity.context.SecurityContextHolder;
  18. import org.acegisecurity.context.SecurityContextImpl;
  19. import org.acegisecurity.providers.UsernamePasswordAuthenticationToken;
  20. import org.springframework.beans.factory.ListableBeanFactory;
  21. import org.springframework.context.support.FileSystemXmlApplicationContext;
  22. import org.springframework.util.StopWatch;
  23. import java.lang.reflect.InvocationTargetException;
  24. import java.lang.reflect.Method;
  25. import java.util.Iterator;
  26. import java.util.List;
  27. import java.util.Map;
  28. /**
  29. * Demonstrates accessing the {@link ContactManager} via remoting protocols.
  30. *
  31. * <P>
  32. * Based on Spring's JPetStore sample, written by Juergen Hoeller.
  33. * </p>
  34. *
  35. * @author Ben Alex
  36. */
  37. public class ClientApplication {
  38. //~ Instance fields ========================================================
  39. private final ListableBeanFactory beanFactory;
  40. //~ Constructors ===========================================================
  41. public ClientApplication(ListableBeanFactory beanFactory) {
  42. this.beanFactory = beanFactory;
  43. }
  44. //~ Methods ================================================================
  45. public void invokeContactManager(Authentication authentication,
  46. int nrOfCalls) {
  47. StopWatch stopWatch = new StopWatch(nrOfCalls
  48. + " ContactManager call(s)");
  49. Map contactServices = this.beanFactory.getBeansOfType(ContactManager.class,
  50. true, true);
  51. SecurityContextHolder.getContext().setAuthentication(authentication);
  52. for (Iterator it = contactServices.keySet().iterator(); it.hasNext();) {
  53. String beanName = (String) it.next();
  54. Object object = this.beanFactory.getBean("&" + beanName);
  55. try {
  56. System.out.println(
  57. "Trying to find setUsername(String) method on: "
  58. + object.getClass().getName());
  59. Method method = object.getClass().getMethod("setUsername",
  60. new Class[] {String.class});
  61. System.out.println("Found; Trying to setUsername(String) to "
  62. + authentication.getPrincipal());
  63. method.invoke(object,
  64. new Object[] {authentication.getPrincipal()});
  65. } catch (NoSuchMethodException ignored) {
  66. System.out.println(
  67. "This client proxy factory does not have a setUsername(String) method");
  68. } catch (IllegalAccessException ignored) {
  69. ignored.printStackTrace();
  70. } catch (InvocationTargetException ignored) {
  71. ignored.printStackTrace();
  72. }
  73. try {
  74. System.out.println(
  75. "Trying to find setPassword(String) method on: "
  76. + object.getClass().getName());
  77. Method method = object.getClass().getMethod("setPassword",
  78. new Class[] {String.class});
  79. method.invoke(object,
  80. new Object[] {authentication.getCredentials()});
  81. System.out.println("Found; Trying to setPassword(String) to "
  82. + authentication.getCredentials());
  83. } catch (NoSuchMethodException ignored) {
  84. System.out.println(
  85. "This client proxy factory does not have a setPassword(String) method");
  86. } catch (IllegalAccessException ignored) {}
  87. catch (InvocationTargetException ignored) {}
  88. ContactManager remoteContactManager = (ContactManager) contactServices
  89. .get(beanName);
  90. System.out.println("Calling ContactManager '" + beanName + "'");
  91. stopWatch.start(beanName);
  92. List contacts = null;
  93. for (int i = 0; i < nrOfCalls; i++) {
  94. contacts = remoteContactManager.getAll();
  95. }
  96. stopWatch.stop();
  97. if (contacts.size() != 0) {
  98. Iterator listIterator = contacts.iterator();
  99. while (listIterator.hasNext()) {
  100. Contact contact = (Contact) listIterator.next();
  101. System.out.println("Contact: " + contact.toString());
  102. }
  103. } else {
  104. System.out.println(
  105. "No contacts found which this user has permission to");
  106. }
  107. System.out.println();
  108. System.out.println(stopWatch.prettyPrint());
  109. }
  110. SecurityContextHolder.clearContext();
  111. }
  112. public static void main(String[] args) {
  113. String username = System.getProperty("username", "");
  114. String password = System.getProperty("password", "");
  115. String nrOfCallsString = System.getProperty("nrOfCalls", "");
  116. if ("".equals(username) || "".equals(password)) {
  117. System.out.println(
  118. "You need to specify the user ID to use, the password to use, and optionally a number of calls "
  119. + "using the username, password, and nrOfCalls system properties respectively. eg for user marissa, "
  120. + "use: -Dusername=marissa -Dpassword=koala' for a single call per service and "
  121. + "use: -Dusername=marissa -Dpassword=koala -DnrOfCalls=10 for ten calls per service.");
  122. System.exit(-1);
  123. } else {
  124. int nrOfCalls = 1;
  125. if (!"".equals(nrOfCallsString)) {
  126. nrOfCalls = Integer.parseInt(nrOfCallsString);
  127. }
  128. ListableBeanFactory beanFactory = new FileSystemXmlApplicationContext(
  129. "clientContext.xml");
  130. ClientApplication client = new ClientApplication(beanFactory);
  131. client.invokeContactManager(new UsernamePasswordAuthenticationToken(
  132. username, password), nrOfCalls);
  133. System.exit(0);
  134. }
  135. }
  136. }