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.impl.config.jndi;
17  
18  import javax.annotation.concurrent.NotThreadSafe;
19  
20  import de.smartics.properties.api.config.domain.Property;
21  import de.smartics.properties.api.config.domain.PropertyCollection;
22  import de.smartics.properties.api.config.domain.PropertyLocation;
23  import de.smartics.properties.api.config.domain.PropertyProvider;
24  import de.smartics.properties.api.config.domain.key.ConfigurationKey;
25  import de.smartics.properties.api.core.app.JndiContext;
26  import de.smartics.util.lang.Arg;
27  
28  /**
29   * Provides an adapter for properties in a JNDI context to match the
30   * {@link PropertyProvider} interface.
31   */
32  @NotThreadSafe
33  public final class JndiConfigurationPropertyProvider implements
34      PropertyProvider
35  {
36    // ********************************* Fields *********************************
37  
38    // --- constants ------------------------------------------------------------
39  
40    /**
41     * The class version identifier.
42     */
43    private static final long serialVersionUID = 1L;
44  
45    // --- members --------------------------------------------------------------
46  
47    /**
48     * The identifier of the configuration the provided properties are part of.
49     *
50     * @serial
51     */
52    private final ConfigurationKey<?> configurationKey;
53  
54    /**
55     * The context in JNDI to access properties for the given configuration key.
56     *
57     * @serial
58     */
59    private final JndiContext context;
60  
61    /**
62     * The manager to access properties in an JNDI store.
63     */
64    private final JndiPropertyStoreAccessor manager;
65  
66    // ****************************** Initializer *******************************
67  
68    // ****************************** Constructors ******************************
69  
70    /**
71     * Default constructor.
72     *
73     * @param configurationKey the identifier of the configuration the provided
74     *          properties are part of.
75     * @param context the context in JNDI to access properties for the given
76     *          configuration key.
77     * @throws NullPointerException if {@code configurationKey} or {@code manager}
78     *           is <code>null</code>.
79     */
80    public JndiConfigurationPropertyProvider(
81        final ConfigurationKey<?> configurationKey, final JndiContext context)
82      throws NullPointerException
83    {
84      this.configurationKey =
85          Arg.checkNotNull("configurationKey", configurationKey);
86      this.context = Arg.checkNotNull("manager", context);
87      this.manager = new JndiPropertyStoreAccessor(configurationKey, context);
88    }
89  
90    // ****************************** Inner Classes *****************************
91  
92    // ********************************* Methods ********************************
93  
94    // --- init -----------------------------------------------------------------
95  
96    // --- get&set --------------------------------------------------------------
97  
98    @Override
99    public ConfigurationKey<?> getConfigurationKey()
100   {
101     return configurationKey;
102   }
103 
104   @Override
105   public PropertyLocation getSourceId()
106   {
107     return new PropertyLocation(context.getSourceId());
108   }
109 
110   @Override
111   public boolean isLazy()
112   {
113     return true;
114   }
115 
116   // --- business -------------------------------------------------------------
117 
118   @Override
119   public Property getProperty(final String name) throws NullPointerException,
120     IllegalArgumentException
121   {
122     final Property property = manager.getPropertyFromStore(name);
123     return property;
124   }
125 
126   @Override
127   public PropertyCollection getProperties()
128   {
129     return manager.getPropertyCollectionFromStore();
130   }
131 
132   @Override
133   public boolean containsKey(final String name) throws NullPointerException
134   {
135     final Property property = manager.getPropertyFromStore(name);
136     return property.getValue() != null;
137   }
138 
139   // --- object basics --------------------------------------------------------
140 
141   /**
142    * Returns the string representation of the object.
143    *
144    * @return the string representation of the object.
145    */
146   @Override
147   public String toString()
148   {
149     final StringBuilder buffer = new StringBuilder();
150 
151     buffer.append(configurationKey).append(": ").append(manager);
152 
153     return buffer.toString();
154   }
155 }