cmstoolmanager.java

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

JAVA
844
字号
/*
 * File   : $Source: /usr/local/cvs/opencms/src/org/opencms/workplace/tools/CmsToolManager.java,v $
 * Date   : $Date: 2007-08-13 16:29:53 $
 * Version: $Revision: 1.46 $
 *
 * 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.workplace.tools;

import org.opencms.file.CmsObject;
import org.opencms.file.CmsProperty;
import org.opencms.file.CmsResource;
import org.opencms.i18n.CmsEncoder;
import org.opencms.jsp.CmsJspActionElement;
import org.opencms.main.CmsException;
import org.opencms.main.CmsLog;
import org.opencms.util.CmsRequestUtil;
import org.opencms.util.CmsStringUtil;
import org.opencms.workplace.CmsDialog;
import org.opencms.workplace.CmsWorkplace;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;

import org.apache.commons.logging.Log;

/**
 * Manages the registered tools, actualizing its state every time the workplace is reinitialize.<p>
 * 
 * Manages also the configuration settings for the administration view, and provides
 * several tool related methods.<p>
 *
 * @author Michael Moossen  
 * 
 * @version $Revision: 1.46 $ 
 * 
 * @since 6.0.0 
 */
public class CmsToolManager {

    /**  Root location of the administration view. */
    public static final String ADMINVIEW_ROOT_LOCATION = CmsWorkplace.PATH_WORKPLACE + "views/admin";

    /**  Property definition name to look for. */
    public static final String HANDLERCLASS_PROPERTY = "admintoolhandler-class";

    /**  Navegation bar separator (html code). */
    public static final String NAVBAR_SEPARATOR = "\n&nbsp;&gt;&nbsp;\n";

    /**  Tool root separator. */
    public static final String ROOT_SEPARATOR = ":";

    /**  Key for the default tool root, if there is no configured root with this a key, a new one will be configured. */
    public static final String ROOTKEY_DEFAULT = "admin";

    /**  Tool path separator. */
    public static final String TOOLPATH_SEPARATOR = "/";

    /** Location of the default admin view jsp page. */
    public static final String VIEW_JSPPAGE_LOCATION = ADMINVIEW_ROOT_LOCATION + "/admin-main.jsp";

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

    /** List of all available roots. */
    private final CmsIdentifiableObjectContainer m_roots;

    /** List of all available tools. */
    private final CmsIdentifiableObjectContainer m_tools;

    /** List of all available urls and related tool paths. */
    private final CmsIdentifiableObjectContainer m_urls;

    /**
     * Default constructor.<p>
     */
    public CmsToolManager() {

        m_roots = new CmsIdentifiableObjectContainer(true, false);
        m_tools = new CmsIdentifiableObjectContainer(true, false);
        m_urls = new CmsIdentifiableObjectContainer(false, false);
    }

    /**
     * Returns the OpenCms link for the given tool path which requires no parameters.<p>
     * 
     * @param jsp the jsp action element
     * @param toolPath the tool path
     * 
     * @return the OpenCms link for the given tool path which requires parameters
     */
    public static String linkForToolPath(CmsJspActionElement jsp, String toolPath) {

        StringBuffer result = new StringBuffer();
        result.append(jsp.link(VIEW_JSPPAGE_LOCATION));
        result.append('?');
        result.append(CmsToolDialog.PARAM_PATH);
        result.append('=');
        result.append(CmsEncoder.encode(toolPath));
        return result.toString();
    }

    /**
     * Returns the OpenCms link for the given tool path which requires parameters.<p>
     * 
     * Please note: Don't overuse the parameter map because this will likely introduce issues 
     * with encoding. If possible, don't pass parameters at all, or only very simple parameters
     * with no special chars that can easily be parsed.<p>
     * 
     * @param jsp the jsp action element
     * @param toolPath the tool path
     * @param params the map of required tool parameters
     * 
     * @return the OpenCms link for the given tool path which requires parameters
     */
    public static String linkForToolPath(CmsJspActionElement jsp, String toolPath, Map params) {

        if (params == null) {
            // no parameters - take the shortcut
            return linkForToolPath(jsp, toolPath);
        }
        params.put(CmsToolDialog.PARAM_PATH, toolPath);
        return CmsRequestUtil.appendParameters(jsp.link(VIEW_JSPPAGE_LOCATION), params, true);
    }

    /**
     * Adds a new tool root to the tool manager.<p>
     * 
     * @param toolRoot the tool root to add
     */
    public void addToolRoot(CmsToolRootHandler toolRoot) {

        m_roots.addIdentifiableObject(toolRoot.getKey(), toolRoot);
    }

    /**
     * Called by the <code>{@link org.opencms.workplace.CmsWorkplaceManager#initialize(CmsObject)}</code> method.<p>
     * 
     * @param cms the admin cms context
     */
    public void configure(CmsObject cms) {

        if (CmsLog.INIT.isInfoEnabled()) {
            CmsLog.INIT.info(Messages.get().getBundle().key(Messages.INIT_TOOLMANAGER_CREATED_0));
        }
        if (m_roots.getObject(ROOTKEY_DEFAULT) == null) {
            CmsToolRootHandler defToolRoot = new CmsToolRootHandler();
            defToolRoot.setKey(ROOTKEY_DEFAULT);
            defToolRoot.setUri(CmsWorkplace.PATH_WORKPLACE + "admin/");
            defToolRoot.setName("${key." + Messages.GUI_ADMIN_VIEW_ROOT_NAME_0 + "}");
            defToolRoot.setHelpText("${key." + Messages.GUI_ADMIN_VIEW_ROOT_HELP_0 + "}");
            addToolRoot(defToolRoot);
        }
        m_tools.clear();
        m_urls.clear();
        Iterator it = getToolRoots().iterator();
        while (it.hasNext()) {
            CmsToolRootHandler toolRoot = (CmsToolRootHandler)it.next();
            if (!cms.existsResource(toolRoot.getUri())) {
                if (CmsLog.INIT.isInfoEnabled()) {
                    CmsLog.INIT.info(Messages.get().getBundle().key(
                        Messages.INIT_TOOLMANAGER_ROOT_SKIPPED_2,
                        toolRoot.getKey(),
                        toolRoot.getUri()));
                }
                continue;
            }
            try {
                toolRoot.setup(cms, null, toolRoot.getUri());
                configureToolRoot(cms, toolRoot);
                // log info
                if (CmsLog.INIT.isInfoEnabled()) {
                    CmsLog.INIT.info(Messages.get().getBundle().key(
                        Messages.INIT_TOOLMANAGER_SETUP_1,
                        toolRoot.getKey()));
                }
            } catch (CmsException e) {
                // log failure
                if (CmsLog.INIT.isWarnEnabled()) {
                    CmsLog.INIT.warn(Messages.get().getBundle().key(
                        Messages.INIT_TOOLMANAGER_SETUP_ERROR_1,
                        toolRoot.getKey()), e);
                }
            }
        }
    }

    /**
     * Returns the navegation bar html code for the given tool path.<p>
     * 
     * @param toolPath the path
     * @param wp the jsp page
     * 
     * @return the html code
     */
    public String generateNavBar(String toolPath, CmsWorkplace wp) {

        if (toolPath.equals(getBaseToolPath(wp))) {
            return "<div class='pathbar'>&nbsp;</div>\n";
        }
        CmsTool adminTool = resolveAdminTool(getCurrentRoot(wp).getKey(), toolPath);
        String html = A_CmsHtmlIconButton.defaultButtonHtml(CmsHtmlIconButtonStyleEnum.SMALL_ICON_TEXT, "nav"
            + adminTool.getId(), adminTool.getHandler().getName(), null, false, null, null, null);
        String parent = toolPath;
        while (!parent.equals(getBaseToolPath(wp))) {
            parent = getParent(wp, parent);
            adminTool = resolveAdminTool(getCurrentRoot(wp).getKey(), parent);

            String id = "nav" + adminTool.getId();
            String link = linkForToolPath(wp.getJsp(), parent, adminTool.getHandler().getParameters(wp));
            String onClic = "openPage('" + link + "');";
            String buttonHtml = A_CmsHtmlIconButton.defaultButtonHtml(
                CmsHtmlIconButtonStyleEnum.SMALL_ICON_TEXT,
                id,
                adminTool.getHandler().getName(),
                adminTool.getHandler().getHelpText(),
                true,
                null,
                null,
                onClic);
            html = buttonHtml + NAVBAR_SEPARATOR + html;
        }
        html = CmsToolMacroResolver.resolveMacros(html, wp);
        html = CmsEncoder.decode(html);
        html = CmsToolMacroResolver.resolveMacros(html, wp);
        html = "<div class='pathbar'>\n" + html + "&nbsp;</div>\n";
        return html;
    }

    /**
     * Returns the base tool path for the active user.<p>
     * 
     * @param wp the workplace object
     * 
     * @return the base tool path for the active user
     */
    public String getBaseToolPath(CmsWorkplace wp) {

        CmsToolUserData userData = getUserData(wp);
        String path = TOOLPATH_SEPARATOR;
        if (userData != null) {
            path = userData.getBaseTool(getCurrentRoot(wp).getKey());
        }
        return path;
    }

    /**
     * Returns the current user's root handler.<p>
     * 
     * @param wp the workplace context
     * 
     * @return the current user's root handler

⌨️ 快捷键说明

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