1 /* 2 * Copyright 2012-2013 smartics, Kronseder & Reiner GmbH 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 package de.smartics.properties.spi.config.resolve; 17 18 import javax.annotation.concurrent.ThreadSafe; 19 20 import de.smartics.properties.api.config.domain.ConfigurationProperties; 21 import de.smartics.properties.api.config.domain.Property; 22 import de.smartics.properties.api.config.domain.UnknownPropertyException; 23 import de.smartics.properties.api.config.domain.key.ConfigurationKey; 24 import de.smartics.properties.api.core.domain.PropertyDescriptor; 25 import de.smartics.properties.api.core.domain.PropertyDescriptorRegistry; 26 import de.smartics.properties.api.core.domain.PropertyExpression; 27 import de.smartics.properties.api.core.domain.PropertyValidationException; 28 29 /** 30 * Simple interface on {@link ConfigurationProperties}. 31 * 32 * @impl the implementation is thread safe since it only provides read access to 33 * the wrapped configuration. If the configuration is accessed by other 34 * threads, it is not. 35 */ 36 @ThreadSafe 37 public final class ConfigurationPropertiesResolveContext implements 38 ResolveContext 39 { 40 // ********************************* Fields ********************************* 41 42 // --- constants ------------------------------------------------------------ 43 44 /** 45 * The class version identifier. 46 */ 47 private static final long serialVersionUID = 1L; 48 49 // --- members -------------------------------------------------------------- 50 51 /** 52 * The registry to resolve property descriptors. 53 * 54 * @serial 55 */ 56 private final PropertyDescriptorRegistry registry; 57 58 /** 59 * The configuration to adapt. 60 * 61 * @serial 62 */ 63 private final ConfigurationProperties config; 64 65 // ****************************** Initializer ******************************* 66 67 // ****************************** Constructors ****************************** 68 69 /** 70 * Default constructor. 71 * 72 * @param registry the registry to resolve property descriptors. 73 * @param config the configuration to adapt. 74 */ 75 public ConfigurationPropertiesResolveContext( 76 final PropertyDescriptorRegistry registry, 77 final ConfigurationProperties config) 78 { 79 this.registry = registry; 80 this.config = config; 81 } 82 83 // ****************************** Inner Classes ***************************** 84 85 // ********************************* Methods ******************************** 86 87 // --- init ----------------------------------------------------------------- 88 89 // --- get&set -------------------------------------------------------------- 90 91 /** 92 * {@inheritDoc} 93 * 94 * @see de.smartics.properties.spi.config.resolve.ResolveContext#getKey() 95 */ 96 @Override 97 public ConfigurationKey getKey() 98 { 99 return config.getKey(); 100 } 101 102 // --- business ------------------------------------------------------------- 103 104 /** 105 * {@inheritDoc} 106 * 107 * @see de.smartics.properties.spi.config.resolve.ResolveContext#get(java.lang.String) 108 */ 109 @Override 110 public String get(final String key) throws IllegalArgumentException, 111 UnknownPropertyException, PropertyValidationException 112 { 113 final Property property = config.getProperty(key); 114 if (property == null) 115 { 116 throw new UnknownPropertyException(getKey(), key); 117 } 118 String value = property.getValue(); 119 120 if (value == null) 121 { 122 final PropertyDescriptor descriptor = registry.get(key); 123 final PropertyExpression expression = descriptor.getDefaultExpression(); 124 value = (expression != null ? expression.getExpression() : null); 125 } 126 127 return value; 128 } 129 // --- object basics -------------------------------------------------------- 130 131 }