001/*
002 * This library is part of OpenCms -
003 * the Open Source Content Management System
004 *
005 * Copyright (c) Alkacon Software GmbH & Co. KG (http://www.alkacon.com)
006 *
007 * This library is free software; you can redistribute it and/or
008 * modify it under the terms of the GNU Lesser General Public
009 * License as published by the Free Software Foundation; either
010 * version 2.1 of the License, or (at your option) any later version.
011 *
012 * This library is distributed in the hope that it will be useful,
013 * but WITHOUT ANY WARRANTY; without even the implied warranty of
014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015 * Lesser General Public License for more details.
016 *
017 * For further information about Alkacon Software GmbH & Co. KG, please see the
018 * company website: http://www.alkacon.com
019 *
020 * For further information about OpenCms, please see the
021 * project website: http://www.opencms.org
022 *
023 * You should have received a copy of the GNU Lesser General Public
024 * License along with this library; if not, write to the Free Software
025 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
026 */
027
028package org.opencms.setup;
029
030import org.opencms.configuration.CmsParameterConfiguration;
031import org.opencms.main.CmsLog;
032
033import java.io.IOException;
034import java.util.ArrayList;
035import java.util.HashMap;
036import java.util.List;
037import java.util.Map;
038
039import org.apache.commons.logging.Log;
040
041/**
042 * Reads and manages the configuration file for the OpenCms auto-setup.<p>
043 * Note that each property set in the file is overwritten if the identical property is set as Java system property (what is used, e.g., by the setup wizard).
044 * Moreover, the property <code>setup.install.components</code> can be set via an accordingly named environment variable.
045 *
046 * @since 6.0.0
047 */
048public final class CmsAutoSetupProperties {
049
050    /** The log object for this class. */
051    public static final Log LOG = CmsLog.getLog(CmsAutoSetupProperties.class);
052
053    /** The DB connection string parameters property name. */
054    public static final String PROP_DB_CONNECTION_STRING_PARAMS = "db.constrparams";
055
056    /** The property key <code>db.connection.url</code> for providing the JDBC connection string,
057     * e.g., <code>jdbc:mysql://localhost:3306/</code> for the default MySQL installation.
058     */
059    public static final String PROP_DB_CONNECTION_URL = "db.connection.url";
060
061    /** The property key <code>db.create.db</code> for specifying if the database should be created during the setup.<P>
062     *  Set to <code>true</code> to create the database automatically, or <code>false</code> if it should not.<P>
063     *  NOTE: Automatic database creation is not supported for all DBMSs.
064     */
065    public static final String PROP_DB_CREATE_DB = "db.create.db";
066
067    /** The property key <code>db.create.pwd</code> for specifying the password for the database user that is used during the setup connection. */
068    public static final String PROP_DB_CREATE_PWD = "db.create.pwd";
069
070    /** The property key <code>db.create.tables</code> for specifying if the database tables in the OpenCms database should be created.<P>
071     *  Set to <code>true</code> to create the database automatically, or <code>false</code> if it should not.<P>
072     */
073    public static final String PROP_DB_CREATE_TABLES = "db.create.tables";
074
075    /** The property key <code>db.create.user</code> for specifying the name of the database user that is used during the setup connection.
076     *  NOTE: The user must have administration permissions. The user data is deleted, when the setup is finished.
077     * */
078    public static final String PROP_DB_CREATE_USER = "db.create.user";
079
080    /** The property key <code>db.default.tablespace</code> necessary dependent on the chosen DBMS. */
081    public static final String PROP_DB_DEFAULT_TABLESPACE = "db.default.tablespace";
082
083    /** The property key <code>db.dropDb</code> for specifying if an existing database should be dropped when the setup is configured to create a database with the same name.
084     *  Set to <code>true</code> if existing databases should be dropped when necessary, or to <code>false</code> if not.
085     */
086    public static final String PROP_DB_DROP_DB = "db.dropDb";
087
088    /** The property key <code>db.index.tablespace</code> necessary dependent on the chosen DBMS. */
089    public static final String PROP_DB_INDEX_TABLESPACE = "db.index.tablespace";
090
091    /** The property key <code>db.jdbc.driver</code> for specifying the fully qualified name of the Java class implementing the JDBC driver to use for the connection.<P>
092     * Hint: The names can be found in the <code>database.properties</code> file in the webapp' folders <code>setup/database/<db.product>/</code>.
093     * */
094    public static final String PROP_DB_JDBC_DRIVER = "db.jdbc.driver";
095
096    /** The property key <code>db.name</code> for specifying the name of the database used by OpenCms, e.g. choose "opencms". */
097    public static final String PROP_DB_NAME = "db.name";
098
099    /** The property key <code>db.product</code> for specifying the used DBMS. Values should match the folders under the OpenCms webapp's folder <code>setup/database/</code>. */
100    public static final String PROP_DB_PRODUCT = "db.product";
101
102    /** The property key <code>db.provider</code> for specifying the database provider. <P>
103     * Hint: The available providers are defined as constants in {@link org.opencms.setup.CmsSetupBean}. */
104    public static final String PROP_DB_PROVIDER = "db.provider";
105
106    /** The property key <code>db.template.db</code> necessary dependent of the chosen DBMS. */
107    public static final String PROP_DB_TEMPLATE_DB = "db.template.db";
108
109    /** The property key <code>db.temporary.tablespace</code> necessary dependent of the chosen DBMS.. */
110    public static final String PROP_DB_TEMPORARY_TABLESPACE = "db.temporary.tablespace";
111
112    /** The property key <code>db.worker.pwd</code> for providing the password of the database user that is used when running OpenCms after the setup.
113    *  CAUTION: For security reasons, the user should not have administration permissions.
114    *  The user data is stored in the <code>opencms.properties</code> file after the setup.
115    */
116    public static final String PROP_DB_WORKER_PWD = "db.worker.pwd";
117
118    /** The property key <code>db.worker.user</code> for providing the name of the database user that is used for the connection when running OpenCms after the setup.
119     *  CAUTION: For security reasons, the user should not have administration permissions.
120     *  The user data is stored in the <code>opencms.properties</code> file after the setup.
121    . */
122    public static final String PROP_DB_WORKER_USER = "db.worker.user";
123
124    /** The property key <code>server.ethernet.address</code>. Specify a valid MAC-Address. It is used internally by OpenCms. (If not given, the address is generated automatically.) */
125    public static final String PROP_SERVER_ETHERNET_ADDRESS = "server.ethernet.address";
126
127    /** The property key <code>server.name</code> for specifying the server's name. Special server names are of particular interest in a cluster installation.*/
128    public static final String PROP_SERVER_NAME = "server.name";
129
130    /** The property key <code>server.servlet.mapping</code> for specifying the name of the OpenCms servlet (by default opencms). */
131    public static final String PROP_SERVER_SERVLET_MAPPING = "server.servlet.mapping";
132
133    /** The property key <code>server.url</code> for specifying the server's URL. It is used, e.g., for the site configuration. */
134    public static final String PROP_SERVER_URL = "server.url";
135
136    /** The property key <code>setup.default.webapp</code>. Provide the default webapp in your servlet container (Default: ROOT). */
137    public static final String PROP_SETUP_DEFAULT_WEBAPP = "setup.default.webapp";
138
139    /** The property key <code>setup.install.components</code> to choose the components that should be installed during the setup.<P>
140     * The available components are configured in <code>setup/components.properties</code> in the webapp's folder.<P>
141     * NOTE: You can specify the components to install also as Java system property (highest priority) or via an environment variable (second choice).
142     * The value specified in the configuration file is only the third choice.
143     */
144    public static final String PROP_SETUP_INSTALL_COMPONENTS = "setup.install.components";
145
146    /** The property key <code>setup.show.progress</code> to specify if dots '.' should be printed to the real STDOUT while a setup is in progress. */
147    public static final String PROP_SETUP_SHOW_PROGRESS = "setup.show.progress";
148
149    /** The property key <code>setup.webapp.path</code> to specify the path of the OpenCms webapp to install, e.g., <code>/var/lib/tomcat7/webapps/opencms</code>. */
150    public static final String PROP_SETUP_WEBAPP_PATH = "setup.webapp.path";
151
152    /** The configuration from <code>opencms.properties</code>. */
153    private CmsParameterConfiguration m_configuration;
154
155    /** The connection String/URL. */
156    private String m_connectionUrl;
157
158    /** The db connection string parameters. */
159    private String m_conStrParams;
160
161    /** The create db flag. */
162    private boolean m_createDb;
163
164    /** The db user pwd for the setup. */
165    private String m_createPwd;
166
167    /** The create table flag. */
168    private boolean m_createTables;
169
170    /** The db user name for the setup. */
171    private String m_createUser;
172
173    /** The database name for OpenCms. */
174    private String m_dbName;
175
176    /** The database to use. */
177    private String m_dbProduct;
178
179    /** The db provider (sql or jpa). */
180    private String m_dbProvider;
181
182    /** The default table space for oracle DBs. */
183    private String m_defaultTablespace;
184
185    /** The drop db flag. */
186    private boolean m_dropDb;
187
188    /** The ethernet address. */
189    private String m_ethernetAddress;
190
191    /** The index table space for oracle DBs. */
192    private String m_indexTablespace;
193
194    /** The list of component IDs to install. */
195    private List<String> m_installComponents = new ArrayList<String>();
196
197    /** The name of the jdbc driver to use. */
198    private String m_jdbcDriver;
199
200    /** The server name. */
201    private String m_serverName;
202
203    /** The server URL. */
204    private String m_serverUrl;
205
206    /** The servlet mapping. */
207    private String m_servletMapping;
208
209    /** The name of the OpenCms webapp. */
210    private String m_setupDefaultWebappName;
211
212    /** The path to the webapp setup folder. */
213    private String m_setupWebappPath;
214
215    /** Indicates if dots '.' should be printed to the real STDOUT while a setup is in progress. */
216    private boolean m_showProgress;
217
218    /** The template DB for PostgreSQL. */
219    private String m_templateDb;
220
221    /** The temporary table space for oracle DBs. */
222    private String m_temporaryTablespace;
223
224    /** The servlet containers webapps folder. */
225    private String m_webappPath;
226
227    /** The db user name for production. */
228    private String m_workerPwd;
229
230    /** The db user pwd for production. */
231    private String m_workerUser;
232
233    /**
234     * Public constructor.<p>
235     *
236     * @param propertiesFile the path to the setup properties file
237     *
238     * @throws IOException if the property file could not be read
239     * @throws SecurityException if the environment variables could not be read
240     */
241    public CmsAutoSetupProperties(String propertiesFile)
242    throws IOException, SecurityException {
243
244        m_configuration = new CmsParameterConfiguration(propertiesFile);
245
246        m_setupWebappPath = addProperty(PROP_SETUP_WEBAPP_PATH);
247        m_setupDefaultWebappName = addProperty(PROP_SETUP_DEFAULT_WEBAPP);
248        m_dbProduct = addProperty(PROP_DB_PRODUCT);
249        m_dbName = addProperty(PROP_DB_NAME);
250        m_dbProvider = addProperty(PROP_DB_PROVIDER);
251        m_createUser = addProperty(PROP_DB_CREATE_USER);
252        m_createPwd = addProperty(PROP_DB_CREATE_PWD);
253        m_workerUser = addProperty(PROP_DB_WORKER_USER);
254        m_workerPwd = addProperty(PROP_DB_WORKER_PWD);
255        m_connectionUrl = addProperty(PROP_DB_CONNECTION_URL);
256        m_createDb = Boolean.valueOf(addProperty(PROP_DB_CREATE_DB)).booleanValue();
257        m_createTables = Boolean.valueOf(addProperty(PROP_DB_CREATE_TABLES)).booleanValue();
258        m_dropDb = Boolean.valueOf(addProperty(PROP_DB_DROP_DB)).booleanValue();
259        m_defaultTablespace = addProperty(PROP_DB_DEFAULT_TABLESPACE);
260        m_indexTablespace = addProperty(PROP_DB_INDEX_TABLESPACE);
261        m_jdbcDriver = addProperty(PROP_DB_JDBC_DRIVER);
262        m_templateDb = addProperty(PROP_DB_TEMPLATE_DB);
263        m_temporaryTablespace = addProperty(PROP_DB_TEMPORARY_TABLESPACE);
264        m_serverUrl = addProperty(PROP_SERVER_URL);
265        m_serverName = addProperty(PROP_SERVER_NAME);
266        m_ethernetAddress = addProperty(PROP_SERVER_ETHERNET_ADDRESS);
267        m_servletMapping = addProperty(PROP_SERVER_SERVLET_MAPPING);
268        m_showProgress = Boolean.valueOf(addProperty(PROP_SETUP_SHOW_PROGRESS)).booleanValue();
269        m_conStrParams = addProperty(PROP_DB_CONNECTION_STRING_PARAMS);
270
271        if (System.getProperty(PROP_SETUP_INSTALL_COMPONENTS) != null) {
272            m_configuration.put(PROP_SETUP_INSTALL_COMPONENTS, System.getProperty(PROP_SETUP_INSTALL_COMPONENTS));
273        } else if (System.getenv(PROP_SETUP_INSTALL_COMPONENTS) != null) {
274            m_configuration.put(PROP_SETUP_INSTALL_COMPONENTS, System.getenv(PROP_SETUP_INSTALL_COMPONENTS));
275        }
276        m_installComponents = m_configuration.getList(PROP_SETUP_INSTALL_COMPONENTS);
277    }
278
279    /**
280     * Returns the connectionUrl.<p>
281     *
282     * @return the connectionUrl
283     */
284    public String getConnectionUrl() {
285
286        return m_connectionUrl;
287    }
288
289    /**
290     * Returns the DB connection string parameters.<p>
291     *
292     * @return the DB connection string parameters
293     */
294    public String getConStrParams() {
295
296        return m_conStrParams;
297    }
298
299    /**
300     * Returns the createPwd.<p>
301     *
302     * @return the createPwd
303     */
304    public String getCreatePwd() {
305
306        return m_createPwd;
307    }
308
309    /**
310     * Returns the createUser.<p>
311     *
312     * @return the createUser
313     */
314    public String getCreateUser() {
315
316        return m_createUser;
317    }
318
319    /**
320     * @return the name of the db name used
321     */
322    public String getDbName() {
323
324        return m_dbName;
325    }
326
327    /**
328     * @return the name of the db product used
329     */
330    public String getDbProduct() {
331
332        return m_dbProduct;
333    }
334
335    /**
336     * Returns the dbProvider.<p>
337     *
338     * @return the dbProvider
339     */
340    public String getDbProvider() {
341
342        return m_dbProvider;
343    }
344
345    /**
346     * Returns the defaultTablespace.<p>
347     *
348     * @return the defaultTablespace
349     */
350    public String getDefaultTablespace() {
351
352        return m_defaultTablespace;
353    }
354
355    /**
356     * Returns the ethernetAddress.<p>
357     *
358     * @return the ethernetAddress
359     */
360    public String getEthernetAddress() {
361
362        return m_ethernetAddress;
363    }
364
365    /**
366     * Returns the indexTablespace.<p>
367     *
368     * @return the indexTablespace
369     */
370    public String getIndexTablespace() {
371
372        return m_indexTablespace;
373    }
374
375    /**
376     * Returns the installComponents.<p>
377     *
378     * @return the installComponents
379     */
380    public List<String> getInstallComponents() {
381
382        return m_installComponents;
383    }
384
385    /**
386     * Returns the jdbcDriver.<p>
387     *
388     * @return the jdbcDriver
389     */
390    public String getJdbcDriver() {
391
392        return m_jdbcDriver;
393    }
394
395    /**
396     * Returns the serveltMapping.<p>
397     *
398     * @return the serveltMapping
399     */
400    public String getServeltMapping() {
401
402        return m_servletMapping;
403    }
404
405    /**
406     * Returns the serverName.<p>
407     *
408     * @return the serverName
409     */
410    public String getServerName() {
411
412        return m_serverName;
413    }
414
415    /**
416     * Returns the serverUrl.<p>
417     *
418     * @return the serverUrl
419     */
420    public String getServerUrl() {
421
422        return m_serverUrl;
423    }
424
425    /**
426     * Returns the setup configuration object.<p>
427     *
428     * @return the setup configuration object
429     */
430    public CmsParameterConfiguration getSetupConfiguration() {
431
432        return m_configuration;
433    }
434
435    /**
436     * Returns the setupWebappName.<p>
437     *
438     * @return the setupWebappName
439     */
440    public String getSetupDefaultWebappName() {
441
442        return m_setupDefaultWebappName;
443    }
444
445    /**
446     * Returns the setupWebappPath.<p>
447     *
448     * @return the setupWebappPath
449     */
450    public String getSetupWebappPath() {
451
452        return m_setupWebappPath;
453    }
454
455    /**
456     * Returns the templateDb.<p>
457     *
458     * @return the templateDb
459     */
460    public String getTemplateDb() {
461
462        return m_templateDb;
463    }
464
465    /**
466     * Returns the temporaryTablespace.<p>
467     *
468     * @return the temporaryTablespace
469     */
470    public String getTemporaryTablespace() {
471
472        return m_temporaryTablespace;
473    }
474
475    /**
476     * Returns the webappPath.<p>
477     *
478     * @return the webappPath
479     */
480    public String getWebappPath() {
481
482        return m_webappPath;
483    }
484
485    /**
486     * Returns the workerPwd.<p>
487     *
488     * @return the workerPwd
489     */
490    public String getWorkerPwd() {
491
492        return m_workerPwd;
493    }
494
495    /**
496     * Returns the workerUser.<p>
497     *
498     * @return the workerUser
499     */
500    public String getWorkerUser() {
501
502        return m_workerUser;
503    }
504
505    /**
506     * Returns the createDb.<p>
507     *
508     * @return the createDb
509     */
510    public boolean isCreateDb() {
511
512        return m_createDb;
513    }
514
515    /**
516     * Returns the createTables.<p>
517     *
518     * @return the createTables
519     */
520    public boolean isCreateTables() {
521
522        return m_createTables;
523    }
524
525    /**
526     * Returns the dropDb.<p>
527     *
528     * @return the dropDb
529     */
530    public boolean isDropDb() {
531
532        return m_dropDb;
533    }
534
535    /**
536     * Indicates if dots '.' should be printed to the real STDOUT while a setup is in progress.<p>
537     *
538     * @return true if dots '.' should be printed to the real STDOUT while a setup is in progress
539     */
540    public boolean isShowProgress() {
541
542        return m_showProgress;
543    }
544
545    /**
546     * Converts and returns this object as map.<p>
547     *
548     * @return this object as map
549     */
550    public Map<String, String[]> toParameterMap() {
551
552        Map<String, String[]> result = new HashMap<String, String[]>();
553
554        result.put("dbCreateConStr", new String[] {getConnectionUrl()});
555        result.put("dbCreateConStrParams", new String[] {getConStrParams()});
556        result.put("dbName", new String[] {getDbName()});
557        result.put("dbProduct", new String[] {getDbProduct()});
558        result.put("dbProvider", new String[] {getDbProvider()});
559        result.put("dbName", new String[] {getDbName()});
560        result.put("db", new String[] {getDbName()});
561        result.put("createDb", new String[] {Boolean.toString(isCreateDb())});
562        result.put("createTables", new String[] {Boolean.toString(isCreateTables())});
563        result.put("jdbcDriver", new String[] {getJdbcDriver()});
564        result.put("templateDb", new String[] {getTemplateDb()});
565        result.put("dbCreateUser", new String[] {getCreateUser()});
566        result.put("dbCreatePwd", new String[] {getCreatePwd() == null ? "" : getCreatePwd()});
567        result.put("dbWorkUser", new String[] {getWorkerUser()});
568        result.put("dbWorkPwd", new String[] {getWorkerPwd() == null ? "" : getWorkerPwd()});
569        result.put("dbDefaultTablespace", new String[] {getDefaultTablespace()});
570        result.put("dbTemporaryTablespace", new String[] {getTemporaryTablespace()});
571        result.put("dbIndexTablespace", new String[] {getIndexTablespace()});
572        result.put("dropDb", new String[] {Boolean.toString(isDropDb())});
573        result.put("servletMapping", new String[] {getServeltMapping()});
574        result.put("submit", new String[] {Boolean.TRUE.toString()});
575
576        return result;
577    }
578
579    /**
580     * Adds and returns the property for the given key.<p>
581     *
582     * @param key the key to add the property
583     *
584     * @return the value of that property
585     */
586    private String addProperty(String key) {
587
588        if (System.getProperty(key) != null) {
589            m_configuration.put(key, System.getProperty(key));
590        }
591        return m_configuration.get(key);
592    }
593}