PageRenderTime 492ms CodeModel.GetById 29ms RepoModel.GetById 0ms app.codeStats 0ms

/src/main/java/xades4j/utils/XadesProfileCore.java

http://xades4j.googlecode.com/
Java | 226 lines | 173 code | 20 blank | 33 comment | 27 complexity | f65c59742394ba9ecb26c838ef24893a MD5 | raw file
Possible License(s): GPL-3.0, LGPL-3.0
  1. /*
  2. * XAdES4j - A Java library for generation and verification of XAdES signatures.
  3. * Copyright (C) 2010 Luis Goncalves.
  4. *
  5. * XAdES4j is free software; you can redistribute it and/or modify it under
  6. * the terms of the GNU Lesser General Public License as published by the Free
  7. * Software Foundation; either version 3 of the License, or any later version.
  8. *
  9. * XAdES4j is distributed in the hope that it will be useful, but WITHOUT
  10. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  11. * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
  12. * details.
  13. *
  14. * You should have received a copy of the GNU Lesser General Public License along
  15. * with XAdES4j. If not, see <http://www.gnu.org/licenses/>.
  16. */
  17. package xades4j.utils;
  18. import com.google.inject.Binder;
  19. import com.google.inject.Guice;
  20. import com.google.inject.Key;
  21. import com.google.inject.Module;
  22. import com.google.inject.TypeLiteral;
  23. import com.google.inject.multibindings.MapBinder;
  24. import com.google.inject.multibindings.Multibinder;
  25. import com.google.inject.util.Modules;
  26. import com.google.inject.util.Types;
  27. import java.lang.reflect.ParameterizedType;
  28. import java.lang.reflect.Type;
  29. import java.util.ArrayList;
  30. import java.util.Arrays;
  31. import java.util.Collection;
  32. /**
  33. * Helper class that implements the core logic of profile resolution based on a series
  34. * of dependencies. Profile resultion is based on Google's dependency container
  35. * (Guice).
  36. * @see xades4j.production.XadesSigningProfile
  37. * @see xades4j.verification.XadesVerificationProfile
  38. * @see xades4j.production.XadesFormatExtenderProfile
  39. * @author Lu?­s
  40. */
  41. public final class XadesProfileCore
  42. {
  43. private static interface BindingAction
  44. {
  45. void bind(Binder b);
  46. }
  47. /**/
  48. private final Collection<BindingAction> bindings;
  49. public XadesProfileCore()
  50. {
  51. this.bindings = new ArrayList<BindingAction>();
  52. }
  53. /**
  54. * @throws NullPointerException if {@code from} or {@code to} are null
  55. */
  56. public <T> void addBinding(final Class<T> from, final Class<? extends T> to)
  57. {
  58. if (null == from || null == to)
  59. throw new NullPointerException();
  60. this.bindings.add(new BindingAction()
  61. {
  62. @Override
  63. public void bind(Binder b)
  64. {
  65. b.bind(from).to(to);
  66. }
  67. });
  68. }
  69. /**
  70. * @throws NullPointerException if {@code from} or {@code to} are null
  71. */
  72. public <T> void addBinding(final Class<T> from, final T to)
  73. {
  74. if (null == from || null == to)
  75. throw new NullPointerException();
  76. this.bindings.add(new BindingAction()
  77. {
  78. @Override
  79. public void bind(Binder b)
  80. {
  81. b.bind(from).toInstance(to);
  82. }
  83. });
  84. }
  85. public void addGenericBinding(
  86. final Type genericClass,
  87. final Class to,
  88. final Type... genericClassParams)
  89. {
  90. if (ObjectUtils.anyNull(genericClass, genericClassParams, to))
  91. throw new NullPointerException();
  92. this.bindings.add(new BindingAction()
  93. {
  94. @Override
  95. public void bind(Binder b)
  96. {
  97. ParameterizedType pt = Types.newParameterizedType(genericClass, genericClassParams);
  98. Key k = Key.get(TypeLiteral.get(pt));
  99. b.bind(k).to(to);
  100. }
  101. });
  102. }
  103. public void addGenericBinding(
  104. final Type genericClass,
  105. final Object to,
  106. final Type... genericClassParams)
  107. {
  108. if (ObjectUtils.anyNull(genericClass, genericClassParams, to))
  109. throw new NullPointerException();
  110. this.bindings.add(new BindingAction()
  111. {
  112. @Override
  113. public void bind(Binder b)
  114. {
  115. ParameterizedType pt = Types.newParameterizedType(genericClass, genericClassParams);
  116. Key k = Key.get(TypeLiteral.get(pt));
  117. b.bind(k).toInstance(to);
  118. }
  119. });
  120. }
  121. public <T> void addMultibinding(final Class<T> from, final Class<? extends T> to)
  122. {
  123. if (null == from || null == to)
  124. throw new NullPointerException();
  125. this.bindings.add(new BindingAction()
  126. {
  127. @Override
  128. public void bind(Binder b)
  129. {
  130. Multibinder multibinder = Multibinder.newSetBinder(b, from);
  131. multibinder.addBinding().to(to);
  132. }
  133. });
  134. }
  135. public <T> void addMultibinding(final Class<T> from, final T to)
  136. {
  137. if (null == from || null == to)
  138. throw new NullPointerException();
  139. this.bindings.add(new BindingAction()
  140. {
  141. @Override
  142. public void bind(Binder b)
  143. {
  144. Multibinder multibinder = Multibinder.newSetBinder(b, from);
  145. multibinder.addBinding().toInstance(to);
  146. }
  147. });
  148. }
  149. public <T, TV extends T> void addMapBinding(final Class<T> valueClass, final Object key, final TV value)
  150. {
  151. if (null == key || null == value)
  152. throw new NullPointerException();
  153. this.bindings.add(new BindingAction()
  154. {
  155. @Override
  156. public void bind(Binder b)
  157. {
  158. MapBinder mapBinder = MapBinder.newMapBinder(b, key.getClass(), valueClass);
  159. mapBinder.addBinding(key).toInstance(value);
  160. }
  161. });
  162. }
  163. public <T> void addMapBinding(final Class<T> valueClass, final Object key, final Class<? extends T> to)
  164. {
  165. if (null == key || null == to)
  166. throw new NullPointerException();
  167. this.bindings.add(new BindingAction()
  168. {
  169. @Override
  170. public void bind(Binder b)
  171. {
  172. MapBinder mapBinder = MapBinder.newMapBinder(b, key.getClass(), valueClass);
  173. mapBinder.addBinding(key).to(to);
  174. }
  175. });
  176. }
  177. public <T> T getInstance(
  178. Class<T> clazz,
  179. Module[] overridableModules,
  180. Module[] sealedModules) throws XadesProfileResolutionException
  181. {
  182. Module userBindingsModule = new Module()
  183. {
  184. @Override
  185. public void configure(Binder b)
  186. {
  187. for (BindingAction ba : bindings)
  188. {
  189. ba.bind(b);
  190. }
  191. }
  192. };
  193. Module overridesModule = Modules.override(overridableModules).with(userBindingsModule);
  194. // Concat sealed modules with overrides module
  195. Module[] finalModules = Arrays.copyOf(sealedModules, sealedModules.length + 1);
  196. finalModules[finalModules.length - 1] = overridesModule;
  197. try
  198. {
  199. return Guice.createInjector(finalModules).getInstance(clazz);
  200. }
  201. catch (RuntimeException ex)
  202. {
  203. throw new XadesProfileResolutionException(ex.getMessage(), ex);
  204. }
  205. }
  206. }