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.configuration;
029
030import org.opencms.main.CmsLog;
031import org.opencms.main.OpenCms;
032import org.opencms.module.CmsModule;
033import org.opencms.module.CmsModuleManager;
034import org.opencms.module.CmsModuleXmlHandler;
035import org.opencms.module.CmsModuleXmlHandler.XmlWriteMode;
036
037import java.util.ArrayList;
038import java.util.Collections;
039import java.util.Iterator;
040import java.util.List;
041
042import org.apache.commons.digester3.Digester;
043
044import org.dom4j.Element;
045
046/**
047 * Modules configuration class.<p>
048 *
049 * @since 6.0.0
050 */
051public class CmsModuleConfiguration extends A_CmsXmlConfiguration {
052
053    /** The name of the DTD for this configuration. */
054    public static final String CONFIGURATION_DTD_NAME = "opencms-modules.dtd";
055
056    /** The name of the default XML file for this configuration. */
057    public static final String DEFAULT_XML_FILE_NAME = "opencms-modules.xml";
058
059    /** The node name for the modules top node. */
060    public static final String N_MODULES = "modules";
061
062    /** The module manager generated from the configuration. */
063    private CmsModuleManager m_moduleManager;
064
065    /** The configured list of module descriptions. */
066    private List<CmsModule> m_modules;
067
068    /**
069     * @see org.opencms.configuration.I_CmsXmlConfiguration#addXmlDigesterRules(org.apache.commons.digester3.Digester)
070     */
071    public void addXmlDigesterRules(Digester digester) {
072
073        // add finish rule
074        digester.addCallMethod("*/" + N_MODULES, "initializeFinished");
075
076        // add the module rules for the module digester
077        CmsModuleXmlHandler.addXmlDigesterRules(digester);
078    }
079
080    /**
081     * @see org.opencms.configuration.I_CmsXmlConfiguration#generateXml(org.dom4j.Element)
082     */
083    public Element generateXml(Element parent) {
084
085        List<CmsModule> modules;
086        if (OpenCms.getRunLevel() >= OpenCms.RUNLEVEL_3_SHELL_ACCESS) {
087            modules = new ArrayList<CmsModule>();
088            Iterator<String> names = OpenCms.getModuleManager().getModuleNames().iterator();
089            while (names.hasNext()) {
090                CmsModule module = OpenCms.getModuleManager().getModule(names.next());
091                if (module != null) {
092                    modules.add(module);
093                }
094            }
095            Collections.sort(modules);
096        } else {
097            // simple unit tests
098            modules = m_modules;
099        }
100
101        // generate modules node and sub nodes
102        Element modulesNode = parent.addElement(N_MODULES);
103
104        for (int i = 0; i < modules.size(); i++) {
105            // append all configured modules
106            CmsModule module = modules.get(i);
107            Element moduleNode = CmsModuleXmlHandler.generateXml(module, XmlWriteMode.config);
108            modulesNode.add(moduleNode);
109        }
110
111        // return the modules node
112        return modulesNode;
113    }
114
115    /**
116     * @see org.opencms.configuration.I_CmsXmlConfiguration#getDtdFilename()
117     */
118    public String getDtdFilename() {
119
120        return CONFIGURATION_DTD_NAME;
121    }
122
123    /**
124     * Returns the configured module manager.<p>
125     *
126     * @return the configured module manager
127     */
128    public CmsModuleManager getModuleManager() {
129
130        return m_moduleManager;
131    }
132
133    /**
134     * Will be called when configuration of this object is finished.<p>
135     */
136    public void initializeFinished() {
137
138        // create the module manager with the configured modules
139        m_moduleManager = new CmsModuleManager(m_modules);
140        if (CmsLog.INIT.isInfoEnabled()) {
141            CmsLog.INIT.info(Messages.get().getBundle().key(Messages.INIT_MODULE_CONFIG_FINISHED_0));
142        }
143    }
144
145    /**
146     * Adds a new module to the list of configured modules.<p>
147     *
148     * @param moduleHandler contains the imported module
149     */
150    public void setModule(CmsModuleXmlHandler moduleHandler) {
151
152        // add the module info to the list of configured modules
153        m_modules.add(moduleHandler.getModule());
154    }
155
156    /**
157     * @see org.opencms.configuration.A_CmsXmlConfiguration#initMembers()
158     */
159    @Override
160    protected void initMembers() {
161
162        setXmlFileName(DEFAULT_XML_FILE_NAME);
163        m_modules = new ArrayList<CmsModule>();
164        if (CmsLog.INIT.isInfoEnabled()) {
165            CmsLog.INIT.info(Messages.get().getBundle().key(Messages.INIT_MODULE_CONFIG_INIT_0));
166        }
167    }
168}