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.core.services;
17  
18  
19  /**
20   * Provides means to change the default configuration name. Usually the
21   * extension configuration to use is determined by boot properties found on the
22   * classpath. This is correct for applications that only use one configuration.
23   * This is not true for administration applications that switch their
24   * configurations at runtime.
25   * <p>
26   * Switching is done by adding the configuration name to lookup to the
27   * thread-local context. This implementation is an easy to use wrapper.
28   * </p>
29   */
30  public final class ThreadLocalAdminRuntime implements AdminRuntime
31  {
32    // ********************************* Fields *********************************
33  
34    // --- constants ------------------------------------------------------------
35  
36    /**
37     * The thread-local storage.
38     */
39    private static final ThreadLocal<ThreadLocalAdminRuntime> LOCAL =
40        new ThreadLocal<ThreadLocalAdminRuntime>()
41        {
42          @Override
43          protected ThreadLocalAdminRuntime initialValue()
44          {
45            return new ThreadLocalAdminRuntime();
46          }
47        };
48  
49    // --- members --------------------------------------------------------------
50  
51    /**
52     * The name of the selected configuration. May be <code>null</code>, if no
53     * configuration is selected. In this case the configuration name found in the
54     * boot properties is used.
55     */
56    private volatile String configurationName;
57  
58    // ****************************** Initializer *******************************
59  
60    // ****************************** Constructors ******************************
61  
62    /**
63     * Default constructor.
64     */
65    public ThreadLocalAdminRuntime()
66    {
67    }
68  
69    // ****************************** Inner Classes *****************************
70  
71    // ********************************* Methods ********************************
72  
73    // --- init -----------------------------------------------------------------
74  
75    // --- get&set --------------------------------------------------------------
76  
77    @Override
78    public String getConfigurationName()
79    {
80      return configurationName;
81    }
82  
83    /**
84     * Sets the name of the selected configuration. May be <code>null</code>, if
85     * no configuration is selected. In this case the configuration name found in
86     * the boot properties is used.
87     *
88     * @param configurationName the name of the selected configuration.
89     */
90    public static void setConfigurationName(final String configurationName)
91    {
92      final ThreadLocalAdminRuntime instance = LOCAL.get();
93      instance.configurationName = configurationName;
94    }
95  
96    // --- business -------------------------------------------------------------
97  
98    /**
99     * Returns the thread-locally stored instance.
100    *
101    * @return the thread-locally stored instance.
102    */
103   public static AdminRuntime getInstance()
104   {
105     return LOCAL.get();
106   }
107 
108   /**
109    * Delegates to {@link ThreadLocal#remove()}.
110    */
111   public static void remove()
112   {
113     LOCAL.remove();
114   }
115 
116   // --- object basics --------------------------------------------------------
117 
118   @Override
119   public String toString()
120   {
121     final StringBuilder buffer = new StringBuilder(32);
122 
123     buffer.append(configurationName);
124 
125     return buffer.toString();
126   }
127 }