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.core.registry;
17  
18  import java.io.Serializable;
19  import java.util.HashMap;
20  import java.util.Map;
21  import java.util.Map.Entry;
22  import java.util.concurrent.locks.Lock;
23  import java.util.concurrent.locks.ReentrantReadWriteLock;
24  
25  import javax.annotation.CheckForNull;
26  import javax.annotation.concurrent.ThreadSafe;
27  
28  import org.slf4j.Logger;
29  import org.slf4j.LoggerFactory;
30  
31  import de.smartics.properties.api.core.domain.ConfigException;
32  import de.smartics.properties.api.core.domain.PropertiesContext;
33  import de.smartics.properties.api.core.domain.PropertyDescriptor;
34  import de.smartics.properties.spi.core.context.DeclarationConfigParser;
35  import de.smartics.properties.spi.core.util.ClassLoaderUtils;
36  import de.smartics.util.lang.Arg;
37  import de.smartics.util.lang.classpath.ClassPathContext;
38  
39  /**
40   * Manages configurations for property descriptors. The configurations are
41   * backed by <code>META-INF/property/config.xml</code> files. This registry
42   * serves as a cache.
43   */
44  @ThreadSafe
45  public final class ConfigurationRegistry implements Serializable
46  {
47    // ********************************* Fields *********************************
48  
49    // --- constants ------------------------------------------------------------
50  
51    /**
52     * The class version identifier.
53     */
54    private static final long serialVersionUID = 1L;
55  
56    /**
57     * Reference to the logger for this class.
58     */
59    private static final Logger LOG = LoggerFactory
60        .getLogger(ConfigurationRegistry.class);
61  
62    // --- members --------------------------------------------------------------
63  
64    /**
65     * The lock for synchronized access to the context cache.
66     *
67     * @serial
68     */
69    private final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
70  
71    /**
72     * The read lock for synchronized access to the context cache.
73     *
74     * @serial
75     */
76    private final Lock readLock = lock.readLock();
77  
78    /**
79     * The write lock for synchronized access to the context cache.
80     *
81     * @serial
82     */
83    private final Lock writeLock = lock.writeLock();
84  
85    /**
86     * Maps the properties context configuration information keyed by their
87     * archive path.
88     */
89    private final Map<String, PropertiesContext> contexts =
90        new HashMap<String, PropertiesContext>();
91  
92    // ****************************** Initializer *******************************
93  
94    // ****************************** Constructors ******************************
95  
96    /**
97     * Default constructor.
98     */
99    public ConfigurationRegistry()
100   {
101   }
102 
103   // ****************************** Inner Classes *****************************
104 
105   // ********************************* Methods ********************************
106 
107   // --- init -----------------------------------------------------------------
108 
109   // --- get&set --------------------------------------------------------------
110 
111   // --- business -------------------------------------------------------------
112 
113   /**
114    * Returns the properties context for the given descriptor.
115    *
116    * @param descriptor the descriptor whose properties context is requested.
117    * @return the context for the property provided by the descriptor. May return
118    *         <code>null</code> if the {@code descriptor} is unknown.
119    * @throws NullPointerException if {@code descriptor} is <code>null</code>.
120    */
121   @CheckForNull
122   public PropertiesContext get(final PropertyDescriptor descriptor)
123     throws NullPointerException
124   {
125     Arg.checkNotNull("descriptor", descriptor);
126 
127     final Class<?> declaringType = descriptor.getDeclaringType();
128     return get(declaringType);
129   }
130 
131   /**
132    * Returns the properties context for the declaring type of a property set.
133    *
134    * @param declaringType the declaring type of a property set.
135    * @return the context for the declared properties. May return
136    *         <code>null</code> if the {@code declaringType} is unknown.
137    * @throws NullPointerException if {@code declaringType} is <code>null</code>.
138    */
139   @CheckForNull
140   public PropertiesContext get(final Class<?> declaringType)
141     throws NullPointerException
142   {
143     Arg.checkNotNull("declaringType", declaringType);
144 
145     final ClassLoader loader = declaringType.getClassLoader();
146     if (loader == null)
147     {
148       LOG.debug("Declaring type '{}' not registered as a property set.",
149           declaringType.getName());
150       return PropertiesContext.createEmptyContext();
151     }
152 
153     try
154     {
155       final PropertiesContext context = load(declaringType);
156       return context;
157     }
158     catch (final ConfigException e)
159     {
160       LOG.debug("Cannot load properties context for declaring type '"
161                 + declaringType.getName() + "'.", e);
162       return PropertiesContext.createEmptyContext();
163     }
164   }
165 
166   private PropertiesContext load(final Class<?> declaringType)
167     throws ConfigException
168   {
169     final String archivePath = ClassLoaderUtils.calcArchivePath(declaringType);
170     final ClassPathContext cpContext =
171         new ClassPathContext(declaringType.getClassLoader(), archivePath);
172 
173     readLock.lock();
174     try
175     {
176       PropertiesContext context = contexts.get(archivePath);
177       if (context == null)
178       {
179         readLock.unlock();
180         writeLock.lock();
181         try
182         {
183           context = contexts.get(archivePath);
184           if (context == null)
185           {
186             final DeclarationConfigParser parser =
187                 new DeclarationConfigParser();
188             context = parser.parse(cpContext);
189 
190             contexts.put(archivePath, context);
191           }
192         }
193         finally
194         {
195           readLock.lock();
196           writeLock.unlock();
197         }
198       }
199       return context;
200     }
201     finally
202     {
203       readLock.unlock();
204     }
205   }
206 
207   // --- object basics --------------------------------------------------------
208 
209   /**
210    * Returns the string representation of the object.
211    *
212    * @return the string representation of the object.
213    */
214   @Override
215   public String toString()
216   {
217     final StringBuilder buffer = new StringBuilder();
218 
219     for (final Entry<String, PropertiesContext> entry : contexts.entrySet())
220     {
221       buffer.append(entry.getKey()).append('=')
222           .append(entry.getValue().getHomePageUrl());
223     }
224 
225     return buffer.toString();
226   }
227 }