001/*
002 * (C) Copyright 2006-2015 Nuxeo SA (http://nuxeo.com/) and contributors.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 *
016 * Contributors:
017 *     bstefanescu, jcarsique
018 */
019
020package org.nuxeo.common;
021
022import java.io.File;
023import java.net.URL;
024import java.util.Properties;
025
026import org.apache.commons.lang.StringUtils;
027import org.apache.commons.lang.builder.ToStringBuilder;
028import org.apache.commons.logging.Log;
029import org.apache.commons.logging.LogFactory;
030
031/**
032 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a>
033 */
034public class Environment {
035
036    private static Log logger = LogFactory.getLog(Environment.class);
037
038    /**
039     * Constants that identifies possible hosts for the framework.
040     */
041    public static final String JBOSS_HOST = "JBoss";
042
043    // Jetty or GF3 embedded
044    public static final String NXSERVER_HOST = "NXServer";
045
046    public static final String TOMCAT_HOST = "Tomcat";
047
048    public static final String NUXEO_HOME_DIR = "nuxeo.home.dir";
049
050    /**
051     * @since 5.6
052     */
053    public static final String NUXEO_HOME = "nuxeo.home";
054
055    /**
056     * @since 5.4.2
057     */
058    public static final String NUXEO_RUNTIME_HOME = "nuxeo.runtime.home";
059
060    public static final String NUXEO_DATA_DIR = "nuxeo.data.dir";
061
062    /**
063     * @since 5.9.4
064     */
065    public static final String DEFAULT_DATA_DIR = "data";
066
067    public static final String NUXEO_LOG_DIR = "nuxeo.log.dir";
068
069    /**
070     * @since 5.9.4
071     */
072    public static final String DEFAULT_LOG_DIR = "log";
073
074    public static final String NUXEO_PID_DIR = "nuxeo.pid.dir";
075
076    public static final String NUXEO_TMP_DIR = "nuxeo.tmp.dir";
077
078    /**
079     * @since 5.9.4
080     */
081    public static final String DEFAULT_TMP_DIR = "tmp";
082
083    public static final String NUXEO_CONFIG_DIR = "nuxeo.config.dir";
084
085    /**
086     * @since 5.9.4
087     */
088    public static final String DEFAULT_CONFIG_DIR = "config";
089
090    public static final String NUXEO_WEB_DIR = "nuxeo.web.dir";
091
092    /**
093     * @since 5.9.4
094     */
095    public static final String DEFAULT_WEB_DIR = "web";
096
097    /**
098     * @since 5.9.4
099     */
100    public static final String NUXEO_MP_DIR = "nuxeo.mp.dir";
101
102    /**
103     * @since 5.9.4
104     */
105    public static final String DEFAULT_MP_DIR = "packages";
106
107    /**
108     * @since 5.6
109     */
110    public static final String NUXEO_CONTEXT_PATH = "org.nuxeo.ecm.contextPath";
111
112    /**
113     * The home directory.
114     *
115     * @deprecated never defined; use {@link #NUXEO_HOME_DIR}
116     */
117    @Deprecated
118    public static final String HOME_DIR = "org.nuxeo.app.home";
119
120    /**
121     * The web root.
122     *
123     * @deprecated never defined; use {@link #NUXEO_WEB_DIR}
124     */
125    @Deprecated
126    public static final String WEB_DIR = "org.nuxeo.app.web";
127
128    /**
129     * The config directory.
130     *
131     * @deprecated never defined; use {@link #NUXEO_CONFIG_DIR}
132     */
133    @Deprecated
134    public static final String CONFIG_DIR = "org.nuxeo.app.config";
135
136    /**
137     * The data directory.
138     *
139     * @deprecated never defined; use {@link #NUXEO_DATA_DIR}
140     */
141    @Deprecated
142    public static final String DATA_DIR = "org.nuxeo.app.data";
143
144    /**
145     * The log directory.
146     *
147     * @deprecated never defined; use {@link #NUXEO_LOG_DIR}
148     */
149    @Deprecated
150    public static final String LOG_DIR = "org.nuxeo.app.log";
151
152    /**
153     * The application layout (optional): directory containing nuxeo runtime osgi bundles.
154     */
155    public static final String BUNDLES_DIR = "nuxeo.osgi.app.bundles";
156
157    public static final String BUNDLES = "nuxeo.osgi.bundles";
158
159    private static volatile Environment DEFAULT;
160
161    protected final File home;
162
163    protected File data;
164
165    protected File log;
166
167    protected File config;
168
169    protected File web;
170
171    protected File temp;
172
173    protected final Properties properties;
174
175    protected String[] args;
176
177    protected boolean isAppServer;
178
179    protected String hostAppName;
180
181    protected String hostAppVersion;
182
183    protected Iterable<URL> configProvider;
184
185    // Handy parameter to distinguish from (Runtime)home
186    private File serverHome = null;
187
188    // Handy parameter to distinguish from (Server)home
189    private File runtimeHome;
190
191    public static final String SERVER_STATUS_KEY = "server.status.key";
192
193    public static final String DISTRIBUTION_NAME = "org.nuxeo.distribution.name";
194
195    public static final String DISTRIBUTION_VERSION = "org.nuxeo.distribution.version";
196
197    /**
198     * @since 7.10
199     */
200    public static final String DISTRIBUTION_SERVER = "org.nuxeo.distribution.server";
201
202    /**
203     * @since 7.10
204     */
205    public static final String DISTRIBUTION_DATE = "org.nuxeo.distribution.date";
206
207    /**
208     * @since 7.10
209     */
210    public static final String DISTRIBUTION_PACKAGE = "org.nuxeo.distribution.package";
211
212    /**
213     * @since 7.10
214     */
215    public static final String PRODUCT_NAME = "org.nuxeo.ecm.product.name";
216
217    /**
218     * @since 7.10
219     */
220    public static final String PRODUCT_VERSION = "org.nuxeo.ecm.product.version";
221
222    // proxy
223    /**
224     * @since 6.0
225     */
226    public static final String NUXEO_HTTP_PROXY_HOST = "nuxeo.http.proxy.host";
227
228    /**
229     * @since 6.0
230     */
231    public static final String NUXEO_HTTP_PROXY_PORT = "nuxeo.http.proxy.port";
232
233    /**
234     * @since 6.0
235     */
236    public static final String NUXEO_HTTP_PROXY_LOGIN = "nuxeo.http.proxy.login";
237
238    /**
239     * @since 6.0
240     */
241    public static final String NUXEO_HTTP_PROXY_PASSWORD = "nuxeo.http.proxy.password";
242
243    /**
244     * @since 7.4
245     */
246    public static final String CRYPT_ALGO = "server.crypt.algorithm";
247
248    /**
249     * @since 7.4
250     */
251    public static final String CRYPT_KEY = "server.crypt.secretkey";
252
253    /**
254     * @since 7.4
255     */
256    public static final String CRYPT_KEYALIAS = "server.crypt.keyalias";
257
258    /**
259     * @since 7.4
260     */
261    public static final String CRYPT_KEYSTORE_PATH = "server.crypt.keystore.path";
262
263    /**
264     * @since 7.4
265     */
266    public static final String CRYPT_KEYSTORE_PASS = "server.crypt.keystore.pass";
267
268    /**
269     * @since 7.4
270     */
271    public static final String JAVA_DEFAULT_KEYSTORE = "javax.net.ssl.keyStore";
272
273    /**
274     * @since 7.4
275     */
276    public static final String JAVA_DEFAULT_KEYSTORE_PASS = "javax.net.ssl.keyStorePassword";
277
278    public Environment(File home) {
279        this(home, null);
280    }
281
282    public Environment(File home, Properties properties) {
283        this.home = home;
284        this.properties = new Properties();
285        if (properties != null) {
286            loadProperties(properties);
287        }
288        this.properties.put(HOME_DIR, this.home.getAbsolutePath());
289    }
290
291    public static synchronized void setDefault(Environment env) {
292        DEFAULT = env;
293    }
294
295    public static Environment getDefault() {
296        if (DEFAULT == null) {
297            tryInitEnvironment();
298        }
299        return DEFAULT;
300    }
301
302    private static synchronized void tryInitEnvironment() {
303        String homeDir = System.getProperty(NUXEO_HOME);
304        if (homeDir != null) {
305            File home = new File(homeDir);
306            if (home.isDirectory()) {
307                DEFAULT = new Environment(home);
308            }
309        }
310    }
311
312    public File getHome() {
313        return home;
314    }
315
316    public boolean isApplicationServer() {
317        return isAppServer;
318    }
319
320    public void setIsApplicationServer(boolean isAppServer) {
321        this.isAppServer = isAppServer;
322    }
323
324    public String getHostApplicationName() {
325        return hostAppName;
326    }
327
328    public String getHostApplicationVersion() {
329        return hostAppVersion;
330    }
331
332    public void setHostApplicationName(String name) {
333        hostAppName = name;
334    }
335
336    public void setHostApplicationVersion(String version) {
337        hostAppVersion = version;
338    }
339
340    public File getTemp() {
341        if (temp == null) {
342            temp = new File(home, DEFAULT_TMP_DIR);
343        }
344        return temp;
345    }
346
347    public void setTemp(File temp) {
348        this.temp = temp;
349        properties.put(NUXEO_TMP_DIR, temp.getAbsolutePath());
350    }
351
352    public File getConfig() {
353        if (config == null) {
354            config = new File(home, DEFAULT_CONFIG_DIR);
355        }
356        return config;
357    }
358
359    public void setConfig(File config) {
360        this.config = config;
361        properties.put(NUXEO_CONFIG_DIR, config.getAbsolutePath());
362    }
363
364    public File getLog() {
365        if (log == null) {
366            log = new File(home, DEFAULT_LOG_DIR);
367        }
368        return log;
369    }
370
371    public void setLog(File log) {
372        this.log = log;
373        properties.put(NUXEO_LOG_DIR, log.getAbsolutePath());
374    }
375
376    public File getData() {
377        if (data == null) {
378            data = new File(home, DEFAULT_DATA_DIR);
379        }
380        return data;
381    }
382
383    public void setData(File data) {
384        this.data = data;
385        properties.put(NUXEO_DATA_DIR, data.getAbsolutePath());
386    }
387
388    public File getWeb() {
389        if (web == null) {
390            web = new File(home, DEFAULT_WEB_DIR);
391        }
392        return web;
393    }
394
395    public void setWeb(File web) {
396        this.web = web;
397        properties.put(NUXEO_WEB_DIR, web.getAbsolutePath());
398    }
399
400    /**
401     * @since 5.4.2
402     */
403    public File getRuntimeHome() {
404        if (runtimeHome == null) {
405            initRuntimeHome();
406        }
407        return runtimeHome;
408    }
409
410    /**
411     * @since 5.4.2
412     */
413    public void setRuntimeHome(File runtimeHome) {
414        this.runtimeHome = runtimeHome;
415        properties.put(NUXEO_RUNTIME_HOME, runtimeHome.getAbsolutePath());
416    }
417
418    public String[] getCommandLineArguments() {
419        return args;
420    }
421
422    public void setCommandLineArguments(String[] args) {
423        this.args = args;
424    }
425
426    public String getProperty(String key) {
427        return properties.getProperty(key);
428    }
429
430    public String getProperty(String key, String defaultValue) {
431        String val = properties.getProperty(key);
432        return val == null ? defaultValue : val;
433    }
434
435    public void setProperty(String key, String value) {
436        properties.put(key, value);
437    }
438
439    public Properties getProperties() {
440        return properties;
441    }
442
443    public void loadProperties(Properties props) {
444        properties.putAll(props);
445    }
446
447    public boolean isJBoss() {
448        return JBOSS_HOST.equals(hostAppName);
449    }
450
451    public boolean isJetty() {
452        return NXSERVER_HOST.equals(hostAppName);
453    }
454
455    public boolean isTomcat() {
456        return TOMCAT_HOST.equals(hostAppName);
457    }
458
459    /**
460     * Initialization with System properties to avoid issues due to home set with runtime home instead of server home.
461     *
462     * @since 5.4.1
463     */
464    public void init() {
465        String dataDir = System.getProperty(NUXEO_DATA_DIR);
466        String configDir = System.getProperty(NUXEO_CONFIG_DIR);
467        String logDir = System.getProperty(NUXEO_LOG_DIR);
468        String tmpDir = System.getProperty(NUXEO_TMP_DIR);
469        String mpDir = System.getProperty(NUXEO_MP_DIR);
470
471        initServerHome();
472        initRuntimeHome();
473        if (StringUtils.isNotEmpty(dataDir)) {
474            setData(new File(dataDir));
475        }
476        if (StringUtils.isNotEmpty(configDir)) {
477            setConfig(new File(configDir));
478        }
479        if (StringUtils.isNotEmpty(logDir)) {
480            setLog(new File(logDir));
481        }
482        if (StringUtils.isNotEmpty(tmpDir)) {
483            setTemp(new File(tmpDir));
484        }
485        if (StringUtils.isNotEmpty(mpDir)) {
486            properties.put(NUXEO_MP_DIR, mpDir);
487        }
488    }
489
490    private void initRuntimeHome() {
491        String runtimeDir = System.getProperty(NUXEO_RUNTIME_HOME);
492        if (runtimeDir != null && !runtimeDir.isEmpty()) {
493            runtimeHome = new File(runtimeDir);
494        } else {
495            runtimeHome = home;
496        }
497    }
498
499    /**
500     * This method always returns the server home (or null if {@link #NUXEO_HOME_DIR} is not set), whereas
501     * {@link #getHome()} may return runtime home.
502     *
503     * @since 5.4.2
504     * @return Server home
505     */
506    public File getServerHome() {
507        if (serverHome == null) {
508            initServerHome();
509        }
510        return serverHome;
511    }
512
513    /**
514     * @since 5.4.2
515     */
516    public void setServerHome(File serverHome) {
517        this.serverHome = serverHome;
518        properties.put(NUXEO_HOME_DIR, serverHome.getAbsolutePath());
519    }
520
521    private void initServerHome() {
522        String homeDir = System.getProperty(NUXEO_HOME, System.getProperty(NUXEO_HOME_DIR));
523        if (homeDir != null && !homeDir.isEmpty()) {
524            serverHome = new File(homeDir);
525        } else {
526            logger.warn(String.format("Could not get %s neither %s system properties, will use %s", NUXEO_HOME,
527                    NUXEO_HOME_DIR, home));
528            serverHome = home;
529        }
530        logger.debug(this);
531    }
532
533    public void setConfigurationProvider(Iterable<URL> configProvider) {
534        this.configProvider = configProvider;
535    }
536
537    public Iterable<URL> getConfigurationProvider() {
538        return configProvider;
539    }
540
541    @Override
542    public String toString() {
543        return ToStringBuilder.reflectionToString(this);
544    }
545}