1   /*
2    * Copyright 2003 - 2014 The eFaps Team
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   * Revision:        $Rev$
17   * Last Changed:    $Date$
18   * Last Changed By: $Author$
19   */
20  
21  
22  package org.efaps.admin;
23  
24  import java.io.File;
25  import java.net.URI;
26  import java.util.Collections;
27  import java.util.Map;
28  
29  /**
30   * Class used to set the configuration of an single instance of eFaps.
31   * It can only be instantiated once. If it was not instantiated before the
32   * first access a default instance of <code>AppConfigHandler</code> will be
33   * created.
34   *
35   * @author The eFaps Team
36   * @version $Id$
37   */
38  public final class AppConfigHandler
39  {
40      /**
41       * Enum used to have a String representation of the config key,
42       * that can be used by an webapp initialization parameter.
43       */
44      public enum Parameter
45      {
46          /**
47           * Deactivate the AccessCache.
48           */
49          ACCESSCACHE_DEACTIVATE("org.efaps.application.config.AccessCache.deactivate"),
50  
51          /**
52           * Deactivate the AccessCache.
53           */
54          QUERYCACHE_DEACTIVATE("org.efaps.application.config.QueryCache.deactivate"),
55  
56          /**
57           * The id of this system as needed by the GeneralInstance mechanism.
58           */
59          SYSTEMID("org.efaps.application.config.SystemID"),
60  
61          /**
62           * The id of this system as needed by the GeneralInstance mechanism.
63           */
64          TEMPFOLDER("org.efaps.application.config.TempFolder");
65  
66          /**
67           * Key of the enum instance.
68           */
69          private String key;
70  
71          /**
72           * @param _key key of the enum instance.
73           */
74          private Parameter(final String _key)
75          {
76              this.key = _key;
77          }
78  
79          /**
80           * Getter method for the instance variable {@link #key}.
81           *
82           * @return value of instance variable {@link #key}
83           */
84          public String getKey()
85          {
86              return this.key;
87          }
88      }
89  
90      /**
91       * Singleton instance.
92       */
93      private static AppConfigHandler HANDLER;
94  
95      /**
96       * Possibility to deactivate the AccessCache.
97       */
98      private final boolean accessCacheDeactivated;
99  
100     /**
101      * Possibility to deactivate the AccessCache.
102      */
103     private final boolean queryCacheDeactivated;
104 
105     /**
106      *  The id of this system as needed by the GeneralInstance mechanism.
107      */
108     private final int systemID;
109 
110     /**
111      * URI to the temp folder used by eFaps.
112      */
113     private final URI tmpURI;
114 
115     /**
116      * Singleton Constructor.
117      * @param _values values for the init
118      */
119     private AppConfigHandler(final Map<String, String> _values)
120     {
121         this.accessCacheDeactivated =  "true".equalsIgnoreCase(_values.get(Parameter.ACCESSCACHE_DEACTIVATE.getKey()));
122         this.queryCacheDeactivated =  "true".equalsIgnoreCase(_values.get(Parameter.QUERYCACHE_DEACTIVATE.getKey()));
123         if (_values.containsKey(Parameter.SYSTEMID.getKey())) {
124             this.systemID = Integer.parseInt(_values.get(Parameter.SYSTEMID.getKey()));
125         } else {
126             this.systemID = 0;
127         }
128         if (_values.containsKey(Parameter.TEMPFOLDER.getKey())) {
129             this.tmpURI = URI.create(_values.get(Parameter.TEMPFOLDER.getKey()));
130         } else {
131             this.tmpURI = null;
132         }
133     }
134 
135     /**
136      * Getter method for the instance variable {@link #accessCacheDeactivated}.
137      *
138      * @return value of instance variable {@link #accessCacheDeactivated}
139      */
140     public boolean isAccessCacheDeactivated()
141     {
142         return this.accessCacheDeactivated;
143     }
144 
145     /**
146      * Getter method for the instance variable {@link #queryCacheDeactivated}.
147      *
148      * @return value of instance variable {@link #queryCacheDeactivated}
149      */
150     public boolean isQueryCacheDeactivated()
151     {
152         return this.queryCacheDeactivated;
153     }
154 
155     /**
156      * Getter method for the instance variable {@link #systemID}.
157      *
158      * @return value of instance variable {@link #systemID}
159      */
160     public int getSystemID()
161     {
162         return this.systemID;
163     }
164 
165     /**
166      * @return if defined the tempfolder for eFaps, else null
167      */
168     public File getTempFolder()
169     {
170         File ret;
171         if (this.tmpURI == null) {
172             ret = null;
173         } else {
174             ret = new File(this.tmpURI);
175         }
176         return ret;
177     }
178 
179     /**
180      * Init the Handler. Can only be executed once.
181      * @param _values values for the init
182      */
183     public static void init(final Map<String, String> _values)
184     {
185         if (AppConfigHandler.HANDLER == null) {
186             AppConfigHandler.HANDLER = new AppConfigHandler(_values);
187         }
188     }
189 
190     /**
191      * Is the Handler initialized.
192      *
193      * @return true if already the Handler is set
194      */
195     public static boolean initialized()
196     {
197         return AppConfigHandler.HANDLER != null;
198     }
199 
200     /**
201      * @return the instance of the AppConfigHandler
202      */
203     public static AppConfigHandler get()
204     {
205         if (!AppConfigHandler.initialized()) {
206             AppConfigHandler.init(Collections.<String, String>emptyMap());
207         }
208         return AppConfigHandler.HANDLER;
209     }
210 }