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 java.util.HashMap;
19  import java.util.Map;
20  
21  import javax.naming.NameClassPair;
22  import javax.naming.NamingEnumeration;
23  import javax.naming.NamingException;
24  
25  import de.smartics.properties.api.config.domain.Property;
26  import de.smartics.properties.api.config.domain.PropertyCollection;
27  import de.smartics.properties.api.config.domain.PropertyStoreAccessor;
28  import de.smartics.properties.api.config.domain.key.ConfigurationKey;
29  import de.smartics.properties.api.core.app.JndiContext;
30  import de.smartics.properties.spi.config.support.NativePropertyCollection;
31  
32  /**
33   * Provides direct access to the properties in the property store.
34   */
35  final class JndiPropertyStoreAccessor implements PropertyStoreAccessor
36  {
37    // ********************************* Fields *********************************
38  
39    // --- constants ------------------------------------------------------------
40  
41    /**
42     * The class version identifier.
43     * <p>
44     * The value of this constant is {@value}.
45     * </p>
46     */
47    private static final long serialVersionUID = 1L;
48  
49    // --- members --------------------------------------------------------------
50  
51    /**
52     * The key that identifies the configuration.
53     */
54    private final ConfigurationKey<?> key;
55  
56    /**
57     * The reference to the configuration within the JNDI context.
58     */
59    private final JndiContext context;
60  
61    // ****************************** Initializer *******************************
62  
63    // ****************************** Constructors ******************************
64  
65    JndiPropertyStoreAccessor(final ConfigurationKey<?> key,
66        final JndiContext context)
67    {
68      this.key = key;
69      this.context = context;
70    }
71  
72    // ****************************** Inner Classes *****************************
73  
74    // ********************************* Methods ********************************
75  
76    // --- init -----------------------------------------------------------------
77  
78    // --- get&set --------------------------------------------------------------
79  
80    // --- business -------------------------------------------------------------
81  
82    @Override
83    public Property getPropertyFromStore(final String name)
84      throws JndiPropertyStoreException
85    {
86      try
87      {
88        final String value = context.lookup(name);
89        final Property property =
90            new Property(context.getSourceId(), name, value);
91        return property;
92      }
93      catch (final NamingException e)
94      {
95        throw new JndiPropertyStoreException(new JndiPropertyStoreMessageBean(
96            JndiPropertyStoreCode.CANNOT_GET_PROPERTY, e, key, name));
97      }
98    }
99  
100   @Override
101   public Property setPropertyToStore(final String name, final String value)
102     throws JndiPropertyStoreException
103   {
104     try
105     {
106       final Property oldProperty = getPropertyFromStore(name);
107       context.set(name, value);
108       return oldProperty;
109     }
110     catch (final NamingException e)
111     {
112       throw new JndiPropertyStoreException(new JndiPropertyStoreMessageBean(
113           JndiPropertyStoreCode.CANNOT_SET_PROPERTY, e, key, name));
114     }
115   }
116 
117   @Override
118   public Property deletePropertyInStore(final String name)
119     throws JndiPropertyStoreException
120   {
121     try
122     {
123       final Property oldProperty = getPropertyFromStore(name);
124       context.set(name, null);
125       return oldProperty;
126     }
127     catch (final NamingException e)
128     {
129       throw new JndiPropertyStoreException(new JndiPropertyStoreMessageBean(
130           JndiPropertyStoreCode.CANNOT_DELETE_PROPERTY, e, key, name));
131     }
132   }
133 
134   @Override
135   public PropertyCollection getPropertyCollectionFromStore()
136     throws JndiPropertyStoreException
137   {
138     try
139     {
140       final Map<String, Property> properties = new HashMap<String, Property>();
141       final NamingEnumeration<NameClassPair> list = context.list();
142       while (list.hasMore())
143       {
144         final NameClassPair pair = list.next();
145         final String name = pair.getName();
146         final Property property = getPropertyFromStore(name);
147         properties.put(name, property);
148       }
149 
150       return new NativePropertyCollection(properties);
151     }
152     catch (final NamingException e)
153     {
154       throw new JndiPropertyStoreException(
155           JndiPropertyStoreMessageBean.collection(e, key));
156     }
157   }
158 
159   // --- object basics --------------------------------------------------------
160 
161   @Override
162   public String toString()
163   {
164     return context.getSourceId();
165   }
166 }