View Javadoc
1   /**
2    * This file Copyright (c) 2011-2015 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.objectfactory.guice;
35  
36  import java.io.Serializable;
37  import java.lang.annotation.Annotation;
38  import javax.inject.Named;
39  
40  import com.google.inject.Key;
41  import info.magnolia.init.MagnoliaConfigurationProperties;
42  import info.magnolia.objectfactory.NoSuchComponentException;
43  
44  /**
45   * Guice configuration module which exposes Magnolia properties.
46   */
47  public class GuicePropertyConfigurer extends AbstractGuiceComponentConfigurer {
48  
49      @Override
50      protected void configure() {
51  
52          // If we have a parent and it has a MagnoliaConfigurationProperties component expose all its properties
53          if (parentComponentProvider != null) {
54              try{
55                  MagnoliaConfigurationProperties configurationProperties = parentComponentProvider.getComponent(MagnoliaConfigurationProperties.class);
56                  installProperties(configurationProperties);
57              }
58              catch(NoSuchComponentException e){
59                  // happens if the MagnoliaConfigurationProperties isn't registered
60                  // this is the case in tests
61              }
62          }
63      }
64  
65      private void installProperties(MagnoliaConfigurationProperties configurationProperties) {
66  
67          for (final String key : configurationProperties.getKeys()) {
68  
69              /*
70                 Unfortunately there's a trade off here. We CAN register these as providers, then properties can change
71                 and we can get the changed values using a provider. But then we dont get conversion to primitives such as
72                 boolean and int.
73              */
74  
75  /*
76              binder().bind(Key.get(String.class, new NamedImpl(propertyName))).toProvider(new Provider<String>() {
77                  @Override
78                  public String get() {
79                      return SystemProperty.getProperty(configurationProperties.getProperty(key));
80                  }
81              });
82  */
83              binder().bind(Key.get(String.class, new NamedImpl(key))).toInstance(configurationProperties.getProperty(key));
84          }
85      }
86  
87      /**
88       * Represents an instantiated @Named annotation.
89       */
90      public static class NamedImpl implements Named, Serializable {
91  
92          private final String value;
93  
94          public NamedImpl(String value) {
95              this.value = value;
96          }
97  
98          @Override
99          public String value() {
100             return this.value;
101         }
102 
103         @Override
104         public int hashCode() {
105             // This is specified in java.lang.Annotation.
106             return (127 * "value".hashCode()) ^ value.hashCode();
107         }
108 
109         @Override
110         public boolean equals(Object o) {
111             if (!(o instanceof Named)) {
112                 return false;
113             }
114 
115             Named other = (Named) o;
116             return value.equals(other.value());
117         }
118 
119         @Override
120         public String toString() {
121             return "@" + Named.class.getName() + "(value=" + value + ")";
122         }
123 
124         @Override
125         public Class<? extends Annotation> annotationType() {
126             return Named.class;
127         }
128     }
129 }