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.file.types.CmsResourceTypeUnknown;
031import org.opencms.file.types.I_CmsResourceType;
032import org.opencms.main.CmsLog;
033
034import org.apache.commons.digester3.AbstractObjectCreationFactory;
035import org.apache.commons.logging.Log;
036
037import org.xml.sax.Attributes;
038
039/**
040 * Factory to create resource type instances from the XML configuration.<p>
041 *
042 * This is required because the default digester implementation will cause an exception in case
043 * a resource type class is missing. However, a missing class is common if a module with a new resource type
044 * class is imported. In this case, the resource type class is changes to <code>{@link org.opencms.file.types.CmsResourceTypeUnknown}</code>,
045 * so that the import of the resources can proceed.<p>
046 *
047 * @since 6.0.2
048 */
049public class CmsDigesterResourceTypeCreationFactory extends AbstractObjectCreationFactory<I_CmsResourceType> {
050
051    /** The log object of this class. */
052    private static final Log LOG = CmsLog.getLog(CmsDigesterResourceTypeCreationFactory.class);
053
054    /**
055     * Default constructor for the resource type configuration factory.<p>
056     */
057    public CmsDigesterResourceTypeCreationFactory() {
058
059        super();
060    }
061
062    /**
063     * @see org.apache.commons.digester3.ObjectCreationFactory#createObject(org.xml.sax.Attributes)
064     */
065    @Override
066    public I_CmsResourceType createObject(Attributes attributes) throws Exception {
067
068        // get the class name attribute
069        String className = attributes.getValue(I_CmsXmlConfiguration.A_CLASS);
070        // create the class instance
071        I_CmsResourceType type;
072        try {
073            if (className != null) {
074                className = className.trim();
075            }
076            type = (I_CmsResourceType)Class.forName(className).newInstance();
077        } catch (Exception e) {
078            // resource type is unknown, use dummy class to import the module resources
079            type = new CmsResourceTypeUnknown();
080            // write an error to the log
081            LOG.error(
082                Messages.get().getBundle().key(
083                    Messages.ERR_UNKNOWN_RESTYPE_CLASS_2,
084                    className,
085                    type.getClass().getName()),
086                e);
087        }
088        return type;
089    }
090}