View Javadoc

1   /**
2    * This file Copyright (c) 2013 Magnolia International
3    * Ltd.  (http://www.magnolia-cms.com). All rights reserved.
4    *
5    *
6    * This file is dual-licensed under both the Magnolia
7    * Network Agreement and the GNU General Public License.
8    * You may elect to use one or the other of these licenses.
9    *
10   * This file is distributed in the hope that it will be
11   * useful, but AS-IS and WITHOUT ANY WARRANTY; without even the
12   * implied warranty of MERCHANTABILITY or FITNESS FOR A
13   * PARTICULAR PURPOSE, TITLE, or NONINFRINGEMENT.
14   * Redistribution, except as permitted by whichever of the GPL
15   * or MNA you select, is prohibited.
16   *
17   * 1. For the GPL license (GPL), you can redistribute and/or
18   * modify this file under the terms of the GNU General
19   * Public License, Version 3, as published by the Free Software
20   * Foundation.  You should have received a copy of the GNU
21   * General Public License, Version 3 along with this program;
22   * if not, write to the Free Software Foundation, Inc., 51
23   * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
24   *
25   * 2. For the Magnolia Network Agreement (MNA), this file
26   * and the accompanying materials are made available under the
27   * terms of the MNA which accompanies this distribution, and
28   * is available at http://www.magnolia-cms.com/mna.html
29   *
30   * Any modifications to this file must keep this entire header
31   * intact.
32   *
33   */
34  package info.magnolia.i18nsystem.proxytoys;
35  
36  import com.thoughtworks.proxy.ProxyFactory;
37  import com.thoughtworks.proxy.toys.decorate.Decorator;
38  import info.magnolia.i18nsystem.I18nable;
39  import info.magnolia.i18nsystem.I18nKeyGenerator;
40  import info.magnolia.i18nsystem.I18nKeyGeneratorFactory;
41  import info.magnolia.i18nsystem.I18nParentable;
42  import info.magnolia.i18nsystem.I18nText;
43  import info.magnolia.i18nsystem.I18nizer;
44  import info.magnolia.i18nsystem.LocaleProvider;
45  import info.magnolia.i18nsystem.TranslationService;
46  
47  import javax.inject.Inject;
48  
49  import net.sf.cglib.proxy.Enhancer;
50  
51  import com.thoughtworks.proxy.factory.CglibProxyFactory;
52  import com.thoughtworks.proxy.toys.decorate.Decorating;
53  import com.thoughtworks.proxy.toys.dispatch.Dispatching;
54  
55  /**
56   * A implementation of {@link I18nizer} based on <a href="http://proxytoys.org">ProxyToys</a>, using proxies to decorate
57   * the given objects.
58   */
59  public class ProxytoysI18nizer implements I18nizer {
60      private static final org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(ProxytoysI18nizer.class);
61  
62      private final TranslationService translationService;
63      private final LocaleProvider localeProvider;
64      private final ProxyFactory proxyFactory;
65  
66      @Inject
67      public ProxytoysI18nizer(TranslationService translationService, LocaleProvider localeProvider) {
68          this.translationService = translationService;
69          this.localeProvider = localeProvider;
70          this.proxyFactory = new CglibProxyFactory(false);
71      }
72  
73      @Override
74      public <C> C decorate(C child) {
75          if (child == null) {
76              return null;
77          }
78          // Check if this has already been decorated to avoid redundancy in client code
79          // TODO however, it could be that the given object was already processed by CGLib, for other purposes ... need to implement better check.
80          if (Enhancer.isEnhanced(child.getClass())) {
81              throw new IllegalStateException(child + " has already been enhanced (presumably by ProxytoysI18nizer)");
82          }
83  
84          return decorateChild(child, null);
85      }
86  
87      protected <P, C> C decorateChild(C child, P parent) {
88          log.debug("About to decorate {} (parent: {})", child, parent);
89          if (child == null) {
90              return null;
91          }
92          if (child instanceof I18nParentable) {
93              log.debug(" - " + child + " is already an instance of I18nParentable - probably because we're in a list. Assuming it's also already decorated with i18n.");
94              return child;
95          }
96  
97          final Class<C> originalClass = (Class<C>) child.getClass();
98          final C parentedChild = injectParentableInterface(originalClass, child, parent);
99  
100         final C i18nd = (C) decorateI18nTextMethods(parentedChild, (Class<Object>) originalClass, I18nParentable.class);
101         return interceptGetters(i18nd, originalClass);
102     }
103 
104     protected <T, P> T injectParentableInterface(Class<? extends T> ct, T object, P parent) {
105         final I18nParentableImpl<P> parentable = new I18nParentableImpl<P>(parent);
106         return Dispatching.proxy(ct, I18nParentable.class)
107                 .with(object, parentable)
108                 .build(proxyFactory);
109     }
110 
111     protected <T> T decorateI18nTextMethods(T obj, Class<T> proxyPrimaryType, Class... proxyMoreTypes) {
112         final I18nKeyGenerator<T> keyGen = I18nKeyGeneratorFactory.newKeyGeneratorFor(obj);
113         final I18nTextMethodDecorator<T> decorator = newI18nTextMethodDecorator(keyGen);
114 
115         return decorateI18nTextMethods(obj, proxyPrimaryType, proxyMoreTypes, decorator);
116     }
117 
118     protected <T> I18nTextMethodDecorator<T> newI18nTextMethodDecorator(I18nKeyGenerator<T> keyGen) {
119         return new I18nTextMethodDecorator<T>(translationService, localeProvider, keyGen);
120     }
121 
122     protected <T> T decorateI18nTextMethods(T obj, Class<T> proxyPrimaryType, Class[] proxyMoreTypes, Decorator<T> decorator) {
123         return Decorating.proxy(proxyPrimaryType, proxyMoreTypes)
124                 .with(obj)
125                 .visiting(new FilteringMethodDecorator<T>(decorator, new MethodsAnnotatedWith(I18nText.class)))
126                 .build(proxyFactory);
127     }
128 
129     protected <T> T interceptGetters(T o, Class<T> originalClass) {
130         final T proxy = Decorating.proxy(originalClass, new Class[] { I18nParentable.class })
131                 .with(o)
132                 .visiting(new FilteringMethodDecorator<T>(new ChildDecorator<T>(this), new ReturnsAnnotatedTypeArgument(I18nable.class)))
133                 .build(proxyFactory);
134         log.debug("Proxied {} (and returning {})", o, proxy);
135         return proxy;
136     }
137 
138 }