cmsobjectwrapper.java

来自「找了很久才找到到源代码」· Java 代码 · 共 906 行 · 第 1/3 页

JAVA
906
字号
/*
 * File   : $Source: /usr/local/cvs/opencms/src/org/opencms/file/wrapper/CmsObjectWrapper.java,v $
 * Date   : $Date: 2007-08-13 16:29:50 $
 * Version: $Revision: 1.10 $
 *
 * This library is part of OpenCms -
 * the Open Source Content Management System
 *
 * Copyright (c) 2002 - 2007 Alkacon Software GmbH (http://www.alkacon.com)
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * For further information about Alkacon Software GmbH, please see the
 * company website: http://www.alkacon.com
 *
 * For further information about OpenCms, please see the
 * project website: http://www.opencms.org
 * 
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

package org.opencms.file.wrapper;

import org.opencms.file.CmsFile;
import org.opencms.file.CmsObject;
import org.opencms.file.CmsProperty;
import org.opencms.file.CmsRequestContext;
import org.opencms.file.CmsResource;
import org.opencms.file.CmsResourceFilter;
import org.opencms.file.CmsUser;
import org.opencms.file.CmsResource.CmsResourceCopyMode;
import org.opencms.file.CmsResource.CmsResourceDeleteMode;
import org.opencms.file.types.CmsResourceTypeJsp;
import org.opencms.file.types.CmsResourceTypePlain;
import org.opencms.file.types.CmsResourceTypeXmlContent;
import org.opencms.file.types.CmsResourceTypeXmlPage;
import org.opencms.file.types.I_CmsResourceType;
import org.opencms.i18n.CmsEncoder;
import org.opencms.i18n.CmsLocaleManager;
import org.opencms.loader.CmsLoaderException;
import org.opencms.lock.CmsLock;
import org.opencms.main.CmsException;
import org.opencms.main.CmsIllegalArgumentException;
import org.opencms.main.CmsLog;
import org.opencms.main.OpenCms;
import org.opencms.util.CmsUUID;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;

import org.apache.commons.logging.Log;

/**
 * This class contains a subset of the methods of {@link CmsObject} and uses the
 * configured resource wrappers ({@link I_CmsResourceWrapper}) to change the view
 * to the existing resources in the VFS.<p>
 * 
 * Almost every method in this class iterates through the configured list of 
 * {@link I_CmsResourceWrapper} and calls the same method there. The first resource 
 * wrapper in the list which feels responsible for that action handles it and the 
 * iteration ends. So the resource wrappers should check in every method if it is 
 * responsible or not. Be careful if there are more than one resource wrapper for 
 * the same resource in the VFS, because the first in the list wins. If the iteration is
 * finished and no resource wrapper felt responsible the default action is to call the 
 * method in the {@link CmsObject}.<p> 
 * 
 * It is possible to create an unchanged access to the resource in the VFS by creating 
 * a new instance of the CmsObjectWrapper with an empty list of resource wrappers.<p>
 *
 * @author Peter Bonrad
 * 
 * @version $Revision: 1.10 $
 * 
 * @since 6.2.4
 */
public class CmsObjectWrapper {

    /** The name of the attribute in the {@link CmsRequestContext} where the current CmsObjectWrapper can be found. */
    public static final String ATTRIBUTE_NAME = "org.opencms.file.wrapper.CmsObjectWrapper";

    /** The log object for this class. */
    private static final Log LOG = CmsLog.getLog(CmsObjectWrapper.class);

    /** The initialized CmsObject. */
    private CmsObject m_cms;

    /** The list with the configured wrappers (entries of type {@link I_CmsResourceWrapper}). */
    private List m_wrappers;

    /**
     * Constructor with the CmsObject to wrap and the resource wrappers to use.<p>
     * 
     * @param cms the initialized CmsObject
     * @param wrappers the configured wrappers to use (entries of type {@link I_CmsResourceWrapper})
     */
    public CmsObjectWrapper(CmsObject cms, List wrappers) {

        m_cms = cms;
        m_wrappers = wrappers;
    }

    /**
     * Copies a resource.<p>
     * 
     * Iterates through all configured resource wrappers till the first returns <code>true</code>.<p>
     * 
     * @see I_CmsResourceWrapper#copyResource(CmsObject, String, String, CmsResource.CmsResourceCopyMode)
     * @see CmsObject#copyResource(String, String, CmsResource.CmsResourceCopyMode)
     * 
     * @param source the name of the resource to copy (full path)
     * @param destination the name of the copy destination (full path)
     * @param siblingMode indicates how to handle siblings during copy
     * 
     * @throws CmsException if something goes wrong
     * @throws CmsIllegalArgumentException if the <code>destination</code> argument is null or of length 0
     */
    public void copyResource(String source, String destination, CmsResourceCopyMode siblingMode)
    throws CmsException, CmsIllegalArgumentException {

        boolean exec = false;

        // iterate through all wrappers and call "copyResource" till one does not return null
        List wrappers = getWrappers();
        Iterator iter = wrappers.iterator();
        while (iter.hasNext()) {
            I_CmsResourceWrapper wrapper = (I_CmsResourceWrapper)iter.next();
            exec = wrapper.copyResource(m_cms, source, destination, siblingMode);
            if (exec) {
                break;
            }
        }

        // delegate the call to the CmsObject
        if (!exec) {
            m_cms.copyResource(source, destination, siblingMode);
        }

    }

    /**
     * Creates a new resource of the given resource type with empty content and no properties.<p>
     * 
     * @see #createResource(String, int, byte[], List)
     * 
     * @param resourcename the name of the resource to create (full path)
     * @param type the type of the resource to create
     * 
     * @return the created resource
     * 
     * @throws CmsException if something goes wrong
     * @throws CmsIllegalArgumentException if the given <code>resourcename</code> is null or of length 0
     */
    public CmsResource createResource(String resourcename, int type) throws CmsException, CmsIllegalArgumentException {

        return createResource(resourcename, type, new byte[0], Collections.EMPTY_LIST);
    }

    /**
     * Creates a new resource of the given resource type with the provided content and properties.<p>
     * 
     * Iterates through all configured resource wrappers till the first returns not <code>null</code>.<p>
     * 
     * @see I_CmsResourceWrapper#createResource(CmsObject, String, int, byte[], List)
     * @see CmsObject#createResource(String, int, byte[], List)
     * 
     * @param resourcename the name of the resource to create (full path)
     * @param type the type of the resource to create
     * @param content the contents for the new resource
     * @param properties the properties for the new resource
     * 
     * @return the created resource
     * 
     * @throws CmsException if something goes wrong
     * @throws CmsIllegalArgumentException if the <code>resourcename</code> argument is null or of length 0
     */
    public CmsResource createResource(String resourcename, int type, byte[] content, List properties)
    throws CmsException, CmsIllegalArgumentException {

        CmsResource res = null;

        // iterate through all wrappers and call "createResource" till one does not return null
        List wrappers = getWrappers();
        Iterator iter = wrappers.iterator();
        while (iter.hasNext()) {
            I_CmsResourceWrapper wrapper = (I_CmsResourceWrapper)iter.next();
            res = wrapper.createResource(m_cms, resourcename, type, content, properties);
            if (res != null) {
                break;
            }
        }

        // delegate the call to the CmsObject
        if (res == null) {
            res = m_cms.createResource(resourcename, type, content, properties);
        }

        return res;
    }

    /**
     * Deletes a resource given its name.<p>
     * 
     * Iterates through all configured resource wrappers till the first returns <code>true</code>.<p>
     * 
     * @see I_CmsResourceWrapper#deleteResource(CmsObject, String, CmsResource.CmsResourceDeleteMode)
     * @see CmsObject#deleteResource(String, CmsResource.CmsResourceDeleteMode)
     * 
     * @param resourcename the name of the resource to delete (full path)
     * @param siblingMode indicates how to handle siblings of the deleted resource
     *
     * @throws CmsException if something goes wrong
     */
    public void deleteResource(String resourcename, CmsResourceDeleteMode siblingMode) throws CmsException {

        boolean exec = false;

        // iterate through all wrappers and call "deleteResource" till one does not return false
        List wrappers = getWrappers();
        Iterator iter = wrappers.iterator();
        while (iter.hasNext()) {
            I_CmsResourceWrapper wrapper = (I_CmsResourceWrapper)iter.next();
            exec = wrapper.deleteResource(m_cms, resourcename, siblingMode);
            if (exec) {
                break;
            }
        }

        // delegate the call to the CmsObject
        if (!exec) {
            m_cms.deleteResource(resourcename, siblingMode);
        }
    }

    /**
     * Checks the availability of a resource in the VFS,
     * using the {@link CmsResourceFilter#DEFAULT} filter.<p>
     * 
     * Here it will be first checked if the resource exists in the VFS by calling 
     * {@link org.opencms.file.CmsObject#existsResource(String)}. Only if it doesn't exist
     * in the VFS the method {@link I_CmsResourceWrapper#readResource(CmsObject, String, CmsResourceFilter)}
     * in the configured resource wrappers are called till the first does not throw an exception or returns 
     * <code>null</code>.<p>
     *
     * @param resourcename the name of the resource to check (full path)
     * 
     * @return <code>true</code> if the resource is available
     */
    public boolean existsResource(String resourcename) {

        // first try to find the resource
        boolean ret = m_cms.existsResource(resourcename);

        // if not exists, ask the resource type wrappers
        if (!ret) {

            List wrappers = getWrappers();
            Iterator iter = wrappers.iterator();
            while (iter.hasNext()) {
                I_CmsResourceWrapper wrapper = (I_CmsResourceWrapper)iter.next();
                try {
                    CmsResource res = wrapper.readResource(m_cms, resourcename, CmsResourceFilter.DEFAULT);
                    if (res != null) {
                        ret = true;
                        break;
                    }
                } catch (CmsException ex) {
                    // noop
                }
            }

        }

        return ret;
    }

    /**
     * Returns the lock state for a specified resource.<p>
     * 
     * Iterates through all configured resource wrappers till the first returns not <code>null</code>.<p>
     * 
     * @see I_CmsResourceWrapper#getLock(CmsObject, CmsResource)
     * @see CmsObject#getLock(CmsResource)
     * 
     * @param resource the resource to return the lock state for
     * 
     * @return the lock state for the specified resource
     * 
     * @throws CmsException if something goes wrong
     */
    public CmsLock getLock(CmsResource resource) throws CmsException {

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?