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.xml.containerpage;
029
030import org.opencms.file.CmsObject;
031import org.opencms.file.CmsPropertyDefinition;
032import org.opencms.file.CmsResource;
033import org.opencms.main.CmsException;
034import org.opencms.main.CmsIllegalStateException;
035import org.opencms.main.CmsLog;
036import org.opencms.main.OpenCms;
037import org.opencms.util.CmsStringUtil;
038
039import org.apache.commons.logging.Log;
040
041/**
042 * Helper class for locating configuration files by looking up their location in properties of another resource.<p>
043 *
044 * @since 8.0.0
045 */
046public class CmsConfigurationFileFinder {
047
048    /** The logger for this class. */
049    private static final Log LOG = CmsLog.getLog(CmsConfigurationFileFinder.class);
050
051    /** The name of the property which should contain the configuration file path. */
052    private String m_propertyName;
053
054    /**
055     * Creates a new configuration file finder which expects the location of configuration files to be stored in the
056     * property with the given name.<p>
057     *
058     * @param propertyName the name of the property which should contain the configuration file path
059     */
060    public CmsConfigurationFileFinder(String propertyName) {
061
062        m_propertyName = propertyName;
063    }
064
065    /**
066     * Returns the configuration file to use.<p>
067     *
068     * @param cms the current cms context
069     * @param containerPageUri the container page uri
070     *
071     * @return the configuration file to use, or <code>null</code> if not found
072     */
073    public CmsResource getConfigurationFile(CmsObject cms, String containerPageUri) {
074
075        String cfgPath = null;
076        try {
077            // get the resource type configuration file from the vfs tree
078            cfgPath = cms.readPropertyObject(containerPageUri, m_propertyName, true).getValue();
079        } catch (CmsException e) {
080            // should never happen
081            LOG.error(e.getLocalizedMessage(), e);
082        }
083
084        if (CmsStringUtil.isEmptyOrWhitespaceOnly(cfgPath)) {
085            // if not found try at the template
086            try {
087                // retrieve the template uri
088                String templateUri = cms.readPropertyObject(
089                    containerPageUri,
090                    CmsPropertyDefinition.PROPERTY_TEMPLATE,
091                    true).getValue();
092                if (CmsStringUtil.isNotEmptyOrWhitespaceOnly(templateUri)) {
093                    // get the resource type configuration file from the template itself
094                    cfgPath = cms.readPropertyObject(templateUri, m_propertyName, true).getValue();
095                }
096            } catch (CmsException e) {
097                // should never happen
098                LOG.error(e.getLocalizedMessage(), e);
099            }
100        }
101
102        if (CmsStringUtil.isEmptyOrWhitespaceOnly(cfgPath)) {
103            // configuration could not be found
104            LOG.warn(Messages.get().getBundle().key(Messages.ERR_CONFIG_NOT_SET_2, containerPageUri, m_propertyName));
105            return null;
106        }
107
108        try {
109            // read configuration file
110            return cms.readResource(cfgPath);
111        } catch (Exception e1) {
112            try {
113                CmsResource baseResource = cms.readResource(containerPageUri);
114                String baseRootPath = baseResource.getRootPath();
115                String siteRoot = OpenCms.getSiteManager().getSiteRoot(baseRootPath);
116                String rootCfgPath = CmsStringUtil.joinPaths(siteRoot, cfgPath);
117                return cms.readResource(rootCfgPath);
118            } catch (Exception e2) {
119                throw new CmsIllegalStateException(
120                    Messages.get().container(
121                        Messages.ERR_CONFIG_NOT_FOUND_3,
122                        containerPageUri,
123                        m_propertyName,
124                        cfgPath));
125            }
126        }
127    }
128
129}