View Javadoc

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.core.domain.PropertyDescriptorRegistry;
22  
23  /**
24   * Property value resolver implementation using commons-jexl.
25   *
26   * @impl the implementation is thread safe since it only provides read access to
27   *       the wrapped configuration. If the configuration is accessed by other
28   *       threads, it is not.
29   */
30  @ThreadSafe
31  public final class SimplePropertyValueResolver implements PropertyValueResolver
32  {
33    // ********************************* Fields *********************************
34  
35    // --- constants ------------------------------------------------------------
36  
37    // --- members --------------------------------------------------------------
38  
39    /**
40     * The execution context for the Jexl engine providing access to other
41     * properties.
42     *
43     * @serial
44     */
45    private final ResolveContext context;
46  
47    // ****************************** Initializer *******************************
48  
49    // ****************************** Constructors ******************************
50  
51    /**
52     * Default constructor.
53     *
54     * @param registry the registry to resolve property descriptors.
55     * @param config the configuration to resolve place holders in property
56     *          values.
57     */
58    public SimplePropertyValueResolver(final PropertyDescriptorRegistry registry,
59        final ConfigurationProperties config)
60    {
61      this.context = new ConfigurationPropertiesResolveContext(registry, config);
62    }
63  
64    // ****************************** Inner Classes *****************************
65  
66    // ********************************* Methods ********************************
67  
68    // --- init -----------------------------------------------------------------
69  
70    // --- get&set --------------------------------------------------------------
71  
72    // --- business -------------------------------------------------------------
73  
74    /**
75     * {@inheritDoc}
76     *
77     * @see de.smartics.properties.spi.config.resolve.PropertyValueResolver#resolve(java.lang.String)
78     */
79    @Override
80    public String resolve(final String expression)
81      throws ResolveConfigurationException
82    {
83      try
84      {
85        final Resolver resolver = new Resolver(context);
86        final String resolved = resolver.resolve(expression);
87  
88        return resolved;
89      }
90      catch (final RuntimeException e)
91      {
92        throw new ResolveConfigurationException(e, context.getKey(), expression);
93      }
94    }
95  
96    // --- object basics --------------------------------------------------------
97  
98  }