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