-
Notifications
You must be signed in to change notification settings - Fork 460
/
Config.java
229 lines (200 loc) · 6.92 KB
/
Config.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
package com.dotmarketing.util;
import com.dotcms.util.AsciiArt;
import com.dotmarketing.db.DbConnectionFactory;
import org.apache.commons.configuration.PropertiesConfiguration;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.util.Date;
import java.util.Iterator;
import java.util.NoSuchElementException;
public class Config {
//Generated File Indicator
public static final String GENERATED_FILE ="dotGenerated_";
public static final String RENDITION_FILE ="dotRendition_";
public static int DB_VERSION=0;
//Object Config properties
public static javax.servlet.ServletContext CONTEXT = null;
public static String CONTEXT_PATH = null;
//PERMISSION CONSTANTS
public static final int PERMISSION_READ = 1;
public static final int PERMISSION_WRITE = 2;
public static final int PERMISSION_PUBLISH = 4;
//Config internal properties
private static int refreshInterval = 5; //In minutes, Default 5 can be overridden in the config file as config.refreshinterval int property
private static Date lastRefreshTime = new Date ();
private static boolean configInited = false;
private static PropertiesConfiguration props = null;
private static ClassLoader classLoader = null;
private static URL url = null;
//Config internal methods
public static void initializeConfig () {
classLoader = Thread.currentThread().getContextClassLoader();
_loadProperties();
}
private static void _loadProperties () {
AsciiArt.doArt();
if (classLoader == null) {
classLoader = Thread.currentThread().getContextClassLoader();
Logger.info(Config.class, "Initializing properties reader.");
}
if (url == null)
url = classLoader.getResource("dotmarketing-config.properties");
if (url != null) {
File f = new File(url.getPath());
Date lastModified = new Date(f.lastModified());
if (lastModified.after(lastRefreshTime) || props == null) {
try {
Logger.info(Config.class, "Loading dotCMS Properties...");
InputStream propsInputStream = new FileInputStream (f);
Config.props = new PropertiesConfiguration ();
props.load(new InputStreamReader(propsInputStream));
propsInputStream.close();
try {
int interval = props.getInt("config.refreshinterval");
refreshInterval = interval;
Logger.info(Config.class, "Assigned custom refresh interval: " + interval + " minutes.");
} catch (NoSuchElementException e) {
Logger.info(Config.class, "Assigned default refresh interval: " + refreshInterval + " minutes.");
}
Logger.info(Config.class, "dotCMS Properties Loaded");
} catch (Exception e) {
Logger.fatal(Config.class, "Exception loading properties", e);
props = null;
}
}
Config.lastRefreshTime = new Date ();
} else {
Logger.fatal(Config.class, "DotCMS Properties file (dotmarketing-config.properties) not found.");
}
}
private static void _refreshProperties () {
if(System.currentTimeMillis() > lastRefreshTime.getTime() + (refreshInterval * 60 * 1000) || props == null){
_loadProperties();
}
}
/**
* Returns a string property
* @param name The name of the property to locate.
* @param defValue Value to return if property is not found.
* @return The value of the property. If property is found more than once, all the occurrences will be concatenated (with a comma separating each element).
*/
public static String getStringProperty(String name,String defValue) {
_refreshProperties ();
if ( props == null ) {
return defValue;
}
String[] propsArr = props.getStringArray(name);
String property = new String ();
int i = 0;
if ((propsArr !=null) &&(propsArr.length>0)) {
for (String propItem : propsArr) {
if (i > 0)
property += ",";
property += propItem;
i++;
}
} else {
property=defValue;
}
return property;
}
/**
* @deprecated Use getStringProperty(String name, String default) and
* set an intelligent default
*/
@Deprecated
public static String getStringProperty (String name) {
_refreshProperties ();
String[] propsArr = props.getStringArray(name);
String property = new String ();
int i = 0;
for (String propItem : propsArr) {
if (i > 0)
property += ",";
property += propItem;
i++;
}
return property;
}
public static String[] getStringArrayProperty (String name) {
_refreshProperties ();
return props.getStringArray(name);
}
/**
* @deprecated Use getIntProperty(String name, int default) and
* set an intelligent default
*/
@Deprecated
public static int getIntProperty (String name) {
_refreshProperties ();
return props.getInt(name);
}
public static int getIntProperty (String name, int defaultVal) {
_refreshProperties ();
if ( props == null ) {
return defaultVal;
}
return props.getInt(name, defaultVal);
}
/**
* @deprecated Use getFloatProperty(String name, float default) and
* set an intelligent default
*/
@Deprecated
public static float getFloatProperty (String name) {
_refreshProperties ();
return props.getFloat(name);
}
public static float getFloatProperty (String name, float defaultVal) {
_refreshProperties ();
if ( props == null ) {
return defaultVal;
}
return props.getFloat(name, defaultVal);
}
/**
* @deprecated Use getBooleanProperty(String name, boolean default) and
* set an intelligent default
*/
@Deprecated
public static boolean getBooleanProperty (String name) {
_refreshProperties ();
return props.getBoolean(name);
}
public static boolean getBooleanProperty (String name, boolean defaultVal) {
_refreshProperties ();
if ( props == null ) {
return defaultVal;
}
return props.getBoolean(name, defaultVal);
}
@SuppressWarnings("unchecked")
public static Iterator<String> getKeys () {
_refreshProperties ();
return props.getKeys();
}
public static boolean containsProperty(String key) {
return props.containsKey(key);
}
// Spindle Config
public static void setMyApp(javax.servlet.ServletContext myApp) {
CONTEXT = myApp;
CONTEXT_PATH = myApp.getRealPath("/");
}
public static String getLimitOffsetQuery(int limit, int offset) {
String db = DbConnectionFactory.getDBType();
if (db.equals("PostgreSQL")){
return " limit " + limit + " offset " + offset;
}
else if (db.equals("MySQL")){
return " limit " + offset + " ," + limit;
}
return "";
}
public static void forceRefresh(){
lastRefreshTime = new Date(0);
}
}