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, 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.configuration;
029
030import org.opencms.db.CmsLoginMessage;
031import org.opencms.main.CmsLog;
032import org.opencms.main.OpenCms;
033
034import org.apache.commons.digester3.Digester;
035
036import org.dom4j.Element;
037
038/**
039 * Class to read and write the OpenCms site configuration.<p>
040 */
041public class CmsVariablesConfiguration extends A_CmsXmlConfiguration {
042
043    /** The node name for the login message. */
044    public static final String N_LOGINMESSAGE = "loginmessage";
045
046    /** The node name for the login message. */
047    public static final String N_BEFORELOGINMESSAGE = "beforeloginmessage";
048
049    /** The node name for the variables element. */
050    public static final String N_VARIABLES = "variables";
051
052    /** The node name for the login message enabled flag. */
053    public static final String N_ENABLED = "enabled";
054
055    /** The node name for the login message text. */
056    public static final String N_MESSAGE = "message";
057
058    /** The node name for the login message start time. */
059    public static final String N_TIMESTART = "timeStart";
060
061    /** The node name for the login message end time. */
062    public static final String N_TIMEEND = "timeEnd";
063
064    /** The node name for the login message login forbidden flag. */
065    public static final String N_LOGINFORBIDDEN = "loginForbidden";
066
067    /** The name of the DTD for this configuration. */
068    public static final String CONFIGURATION_DTD_NAME = "opencms-variables.dtd";
069
070    /** The name of the default XML file for this configuration. */
071    public static final String DEFAULT_XML_FILE_NAME = "opencms-variables.xml";
072
073    /** The configured login message. */
074    private CmsLoginMessage m_loginMessage;
075
076    /** The configured before login message. */
077    private CmsLoginMessage m_beforeLoginMessage;
078
079    /**
080     * @see org.opencms.configuration.I_CmsXmlConfiguration#addXmlDigesterRules(org.apache.commons.digester3.Digester)
081     */
082    public void addXmlDigesterRules(Digester digester) {
083
084        // add login message creation rules
085        digester.addObjectCreate("*/" + N_LOGINMESSAGE, CmsLoginMessage.class);
086        digester.addBeanPropertySetter("*/" + N_LOGINMESSAGE + "/" + N_ENABLED);
087        digester.addBeanPropertySetter("*/" + N_LOGINMESSAGE + "/" + N_MESSAGE);
088        digester.addBeanPropertySetter("*/" + N_LOGINMESSAGE + "/" + N_LOGINFORBIDDEN);
089        digester.addBeanPropertySetter("*/" + N_LOGINMESSAGE + "/" + N_TIMESTART);
090        digester.addBeanPropertySetter("*/" + N_LOGINMESSAGE + "/" + N_TIMEEND);
091        digester.addSetNext("*/" + N_LOGINMESSAGE, "setLoginMessage");
092
093        // add before login message creation rules
094        digester.addObjectCreate("*/" + N_BEFORELOGINMESSAGE, CmsLoginMessage.class);
095        digester.addBeanPropertySetter("*/" + N_BEFORELOGINMESSAGE + "/" + N_ENABLED);
096        digester.addBeanPropertySetter("*/" + N_BEFORELOGINMESSAGE + "/" + N_MESSAGE);
097        digester.addSetNext("*/" + N_BEFORELOGINMESSAGE, "setBeforeLoginMessage");
098    }
099
100    /**
101     * @see org.opencms.configuration.I_CmsXmlConfiguration#generateXml(org.dom4j.Element)
102     */
103    public Element generateXml(Element parent) {
104
105        // create <sites> node
106        Element variablesElement = parent.addElement(N_VARIABLES);
107        if (OpenCms.getRunLevel() >= OpenCms.RUNLEVEL_3_SHELL_ACCESS) {
108            // initialized OpenCms instance is available, use latest values
109            m_loginMessage = OpenCms.getLoginManager().getLoginMessage();
110            m_beforeLoginMessage = OpenCms.getLoginManager().getBeforeLoginMessage();
111        }
112        // login message
113        if (m_loginMessage != null) {
114            Element messageElement = variablesElement.addElement(N_LOGINMESSAGE);
115            messageElement.addElement(N_ENABLED).addText(String.valueOf(m_loginMessage.isEnabled()));
116            messageElement.addElement(N_MESSAGE).addCDATA(m_loginMessage.getMessage());
117            messageElement.addElement(N_LOGINFORBIDDEN).addText(String.valueOf(m_loginMessage.isLoginForbidden()));
118            if (m_loginMessage.getTimeStart() != CmsLoginMessage.DEFAULT_TIME_START) {
119                messageElement.addElement(N_TIMESTART).addText(String.valueOf(m_loginMessage.getTimeStart()));
120            }
121            if (m_loginMessage.getTimeEnd() != CmsLoginMessage.DEFAULT_TIME_END) {
122                messageElement.addElement(N_TIMEEND).addText(String.valueOf(m_loginMessage.getTimeEnd()));
123            }
124        }
125        // before login message
126        if (m_beforeLoginMessage != null) {
127            Element messageElement = variablesElement.addElement(N_BEFORELOGINMESSAGE);
128            messageElement.addElement(N_ENABLED).addText(String.valueOf(m_beforeLoginMessage.isEnabled()));
129            messageElement.addElement(N_MESSAGE).addCDATA(m_beforeLoginMessage.getMessage());
130        }
131
132        return variablesElement;
133    }
134
135    /**
136     * Returns the login message.<p>
137     *
138     * @return before login message
139     */
140    public CmsLoginMessage getBeforeLoginMessage() {
141
142        return m_beforeLoginMessage;
143    }
144
145    /**
146     * @see org.opencms.configuration.I_CmsXmlConfiguration#getDtdFilename()
147     */
148    public String getDtdFilename() {
149
150        return CONFIGURATION_DTD_NAME;
151    }
152
153    /**
154     * Returns the login message.<p>
155     *
156     * @return the login message
157     */
158    public CmsLoginMessage getLoginMessage() {
159
160        return m_loginMessage;
161    }
162
163    /**
164     * Adds the before login message from the configuration.<p>
165     *
166     * @param message the login message to add
167     */
168    public void setBeforeLoginMessage(CmsLoginMessage message) {
169
170        m_beforeLoginMessage = message;
171        if (CmsLog.INIT.isInfoEnabled()) {
172            CmsLog.INIT.info(
173                Messages.get().getBundle().key(
174                    Messages.INIT_LOGINMESSAGE_3,
175                    Boolean.valueOf(message.isEnabled()),
176                    Boolean.valueOf(message.isLoginForbidden()),
177                    message.getMessage()));
178        }
179    }
180
181    /**
182     * Adds the login message from the configuration.<p>
183     *
184     * @param message the login message to add
185     */
186    public void setLoginMessage(CmsLoginMessage message) {
187
188        m_loginMessage = message;
189        if (CmsLog.INIT.isInfoEnabled()) {
190            CmsLog.INIT.info(
191                Messages.get().getBundle().key(
192                    Messages.INIT_LOGINMESSAGE_3,
193                    Boolean.valueOf(message.isEnabled()),
194                    Boolean.valueOf(message.isLoginForbidden()),
195                    message.getMessage()));
196        }
197    }
198
199    /**
200     * @see org.opencms.configuration.A_CmsXmlConfiguration#initMembers()
201     */
202    @Override
203    protected void initMembers() {
204
205        setXmlFileName(DEFAULT_XML_FILE_NAME);
206    }
207}