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