View Javadoc
1   /**
2    * This file Copyright (c) 2013-2018 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 info.magnolia.i18nsystem.I18nKeyGenerator;
37  import info.magnolia.i18nsystem.I18nKeyGeneratorFactory;
38  import info.magnolia.i18nsystem.I18nParentable;
39  import info.magnolia.i18nsystem.I18nText;
40  import info.magnolia.i18nsystem.I18nable;
41  import info.magnolia.i18nsystem.I18nizer;
42  import info.magnolia.i18nsystem.LocaleProvider;
43  import info.magnolia.i18nsystem.TranslationService;
44  
45  import javax.inject.Inject;
46  
47  import com.thoughtworks.proxy.ProxyFactory;
48  import com.thoughtworks.proxy.factory.CglibProxyFactory;
49  import com.thoughtworks.proxy.toys.decorate.Decorating;
50  import com.thoughtworks.proxy.toys.decorate.Decorator;
51  import com.thoughtworks.proxy.toys.dispatch.Dispatching;
52  
53  import net.sf.cglib.proxy.Enhancer;
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 obj) {
75          if (obj == null) {
76              return null;
77          }
78          // Check if this has already been decorated to avoid redundancy in client code
79          if (isProxied(obj.getClass()) && obj instanceof I18nParentable) {
80              throw new IllegalStateException(obj + " has already been enhanced by " + getClass().getSimpleName());
81          }
82  
83          return decorateChild(obj, null);
84      }
85  
86      protected <P, C> C decorateChild(C child, P parent) {
87          log.debug("About to decorate {} (parent: {})", child, parent);
88          if (child == null) {
89              return null;
90          }
91          if (child instanceof I18nParentable) {
92              log.debug(" - {} is already an instance of I18nParentable - probably because we're in a list. Assuming it's also already decorated with i18n.", child);
93              return child;
94          }
95  
96          // Get the "real" type of the object to decorate; all we do is go up the class hierarchy until it's not a proxy'd class
97          Class<C> originalClass = (Class<C>) child.getClass();
98          while (isProxied(originalClass)) {
99              // As long as the class is a proxy, we can rely on the fact the superclass is still of type C, hence this dubious cast:
100             originalClass = (Class<C>) originalClass.getSuperclass();
101         }
102         final C parentedChild = injectParentableInterface(originalClass, child, parent);
103 
104         final C i18nd = decorateI18nTextMethods(parentedChild, originalClass, I18nParentable.class);
105         return interceptGetters(i18nd, originalClass);
106     }
107 
108     protected <T, P> T injectParentableInterface(Class<? extends T> ct, T object, P parent) {
109         final I18nParentableImpl<P> parentable = new I18nParentableImpl<P>(parent);
110         return Dispatching.proxy(ct, I18nParentable.class)
111                 .with(object, parentable)
112                 .build(proxyFactory);
113     }
114 
115     protected <T> T decorateI18nTextMethods(T obj, Class<T> proxyPrimaryType, Class... proxyMoreTypes) {
116         final I18nKeyGenerator<T> keyGen = I18nKeyGeneratorFactory.newKeyGeneratorFor(obj);
117         final I18nTextMethodDecorator<T> decorator = newI18nTextMethodDecorator(keyGen);
118 
119         return decorateI18nTextMethods(obj, proxyPrimaryType, proxyMoreTypes, decorator);
120     }
121 
122     protected <T> I18nTextMethodDecorator<T> newI18nTextMethodDecorator(I18nKeyGenerator<T> keyGen) {
123         return new I18nTextMethodDecorator<T>(translationService, localeProvider, keyGen);
124     }
125 
126     protected <T> T decorateI18nTextMethods(T obj, Class<T> proxyPrimaryType, Class[] proxyMoreTypes, Decorator<T> decorator) {
127         return Decorating.proxy(proxyPrimaryType, proxyMoreTypes)
128                 .with(obj)
129                 .visiting(new FilteringMethodDecorator<T>(decorator, new MethodsAnnotatedWith(I18nText.class)))
130                 .build(proxyFactory);
131     }
132 
133     protected <T> T interceptGetters(T o, Class<T> originalClass) {
134         final T proxy = Decorating.proxy(originalClass, new Class[] { I18nParentable.class })
135                 .with(o)
136                 .visiting(new FilteringMethodDecorator<T>(new ChildDecorator<T>(this), new ReturnsAnnotatedTypeArgument(I18nable.class)))
137                 .build(proxyFactory);
138         log.debug("Proxied {} (and returning {})", o, proxy);
139         return proxy;
140     }
141 
142     protected boolean isProxied(Class<?> aClass) {
143         // We still need to check with cglib's Enhancer#isEnhanced in case the object was proxied with something else than ProxyToys.
144         return proxyFactory.isProxyClass(aClass) || Enhancer.isEnhanced(aClass);
145     }
146 
147 }