⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 cmstoolmanager.java

📁 一个cms内容管理平台
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
    public CmsToolUserData getUserData(CmsWorkplace wp) {

        CmsToolUserData userData = wp.getSettings().getToolUserData();
        if (userData == null) {
            userData = new CmsToolUserData();
            userData.setRootKey(ROOTKEY_DEFAULT);
            Iterator it = getToolRoots().iterator();
            while (it.hasNext()) {
                CmsToolRootHandler root = (CmsToolRootHandler)it.next();
                userData.setCurrentToolPath(root.getKey(), TOOLPATH_SEPARATOR);
                userData.setBaseTool(root.getKey(), TOOLPATH_SEPARATOR);
            }
            wp.getSettings().setToolUserData(userData);
        }
        return userData;
    }

    /**
     * Returns <code>true</code> if there is at least one tool registered using the given url.<p>
     * 
     * @param url the url of the tool
     * 
     * @return <code>true</code> if there is at least one tool registered using the given url
     */
    public boolean hasToolPathForUrl(String url) {

        List toolPaths = (List)m_urls.getObject(url);
        return (toolPaths != null && !toolPaths.isEmpty());
    }

    /**
     * This method initializes the tool manager for the current user.<p>
     * 
     * @param wp the jsp page comming from
     */
    public synchronized void initParams(CmsToolDialog wp) {

        setCurrentRoot(wp, wp.getParamRoot());
        setCurrentToolPath(wp, wp.getParamPath());
        setBaseToolPath(wp, wp.getParamBase());

        // if the current tool path is not under the current root, set the current root as the current tool
        if (!getCurrentToolPath(wp).startsWith(getBaseToolPath(wp))) {
            setCurrentToolPath(wp, getBaseToolPath(wp));
        }
        wp.setParamPath(getCurrentToolPath(wp));
        wp.setParamBase(getBaseToolPath(wp));
        wp.setParamRoot(getCurrentRoot(wp).getKey());
    }

    /**
     * Redirects to the given page with the given parameters.<p>
     * 
     * @param wp the workplace object
     * @param pagePath the path to the page to redirect to
     * @param params the parameters to send
     * 
     * @throws IOException in case of errors during forwarding
     * @throws ServletException in case of errors during forwarding
     */
    public void jspForwardPage(CmsWorkplace wp, String pagePath, Map params) throws IOException, ServletException {

        Map newParams = new HashMap();
        // add query parameters to the parameter map if required
        if (pagePath.indexOf("?") > 0) {
            String query = pagePath.substring(pagePath.indexOf("?"));
            pagePath = pagePath.substring(0, pagePath.indexOf("?"));
            Map reqParameters = CmsRequestUtil.createParameterMap(query);
            newParams.putAll(reqParameters);
        }
        if (params != null) {
            newParams.putAll(params);
        }

        // put close link if not set
        if (!newParams.containsKey(CmsDialog.PARAM_CLOSELINK)) {
            Map argMap = resolveAdminTool(getCurrentRoot(wp).getKey(), getCurrentToolPath(wp)).getHandler().getParameters(
                wp);
            newParams.put(CmsDialog.PARAM_CLOSELINK, linkForToolPath(wp.getJsp(), getCurrentToolPath(wp), argMap));
        }
        wp.setForwarded(true);
        // forward to the requested page uri
        CmsRequestUtil.forwardRequest(
            wp.getJsp().link(pagePath),
            CmsRequestUtil.createParameterMap(newParams),
            wp.getJsp().getRequest(),
            wp.getJsp().getResponse());
    }

    /**
     * Redirects to the given tool with the given parameters.<p>
     * 
     * @param wp the workplace object
     * @param toolPath the path to the tool to redirect to
     * @param params the parameters to send
     * 
     * @throws IOException in case of errors during forwarding
     * @throws ServletException in case of errors during forwarding
     */
    public void jspForwardTool(CmsWorkplace wp, String toolPath, Map params) throws IOException, ServletException {

        Map newParams;
        if (params == null) {
            newParams = new HashMap();
        } else {
            newParams = new HashMap(params);
        }
        // update path param
        newParams.put(CmsToolDialog.PARAM_PATH, toolPath);
        jspForwardPage(wp, VIEW_JSPPAGE_LOCATION, newParams);
    }

    /**
     * Returns the admin tool corresponding to the given abstract path.<p>
     * 
     * @param rootKey the tool root
     * @param toolPath the path
     * 
     * @return the corresponding tool, or <code>null</code> if not found
     */
    public CmsTool resolveAdminTool(String rootKey, String toolPath) {

        return (CmsTool)m_tools.getObject(rootKey + ROOT_SEPARATOR + toolPath);
    }

    /**
     * Sets the base tool path.<p>
     * 
     * @param wp the workplace object
     * @param baseToolPath the base tool path to set
     */
    public void setBaseToolPath(CmsWorkplace wp, String baseToolPath) {

        // use last used base if param empty
        if (CmsStringUtil.isEmpty(baseToolPath) || baseToolPath.trim().equals("null")) {
            baseToolPath = getBaseToolPath(wp);
        }
        baseToolPath = repairPath(wp, baseToolPath);
        // set it
        CmsToolUserData userData = getUserData(wp);
        userData.setBaseTool(userData.getRootKey(), baseToolPath);
    }

    /**
     * Sets the current user's root key.<p>
     * 
     * @param wp the workplace context
     * @param key the current user's root key to set
     */
    public void setCurrentRoot(CmsWorkplace wp, String key) {

        // use last used root if param empty
        if (CmsStringUtil.isEmpty(key) || key.trim().equals("null")) {
            key = getCurrentRoot(wp).getKey();
        }
        // set it
        CmsToolUserData userData = getUserData(wp);
        userData.setRootKey(key);
    }

    /**
     * Sets the current tool path.<p>
     * 
     * @param wp the workplace object
     * @param currentToolPath the current tool path to set
     */
    public void setCurrentToolPath(CmsWorkplace wp, String currentToolPath) {

        // use last used path if param empty
        if (CmsStringUtil.isEmptyOrWhitespaceOnly(currentToolPath) || currentToolPath.trim().equals("null")) {
            currentToolPath = getCurrentToolPath(wp);
        }
        currentToolPath = repairPath(wp, currentToolPath);
        // use it
        CmsToolUserData userData = getUserData(wp);
        userData.setCurrentToolPath(userData.getRootKey(), currentToolPath);
    }

    /**
     * Configures a whole tool root with all its tools.<p>
     * 
     * @param toolRoot the tool root to configure
     * 
     * @throws CmsException if something goes wrong
     */
    private void configureToolRoot(CmsObject cms, CmsToolRootHandler toolRoot) throws CmsException {

        List handlers = new ArrayList();

        // add tool root handler 
        handlers.add(toolRoot);

        // look in every file under the root uri for valid
        // admin tools and register them
        List resources = cms.readResourcesWithProperty(toolRoot.getUri(), HANDLERCLASS_PROPERTY);
        Iterator itRes = resources.iterator();
        while (itRes.hasNext()) {
            CmsResource res = (CmsResource)itRes.next();
            CmsProperty prop = cms.readPropertyObject(res.getRootPath(), HANDLERCLASS_PROPERTY, false);
            if (!prop.isNullProperty()) {
                try {
                    // instantiate the handler
                    Class handlerClass = Class.forName(prop.getValue());
                    I_CmsToolHandler handler = (I_CmsToolHandler)handlerClass.newInstance();

                    if (!handler.setup(cms, toolRoot, res.getRootPath())) {
                        // log failure
                        if (CmsLog.INIT.isWarnEnabled()) {
                            CmsLog.INIT.warn(Messages.get().getBundle().key(
                                Messages.INIT_TOOLMANAGER_TOOL_SETUP_ERROR_1,
                                res.getRootPath()));
                        }
                    }

                    // keep for later use
                    handlers.add(handler);
                    // log success
                    if (CmsLog.INIT.isDebugEnabled()) {
                        if (!handler.getLink().equals(VIEW_JSPPAGE_LOCATION)) {
                            CmsLog.INIT.debug(Messages.get().getBundle().key(
                                Messages.INIT_TOOLMANAGER_NEWTOOL_FOUND_2,
                                handler.getPath(),
                                handler.getLink()));
                        } else {
                            CmsLog.INIT.debug(Messages.get().getBundle().key(
                                Messages.INIT_TOOLMANAGER_NEWTOOL_FOUND_2,
                                handler.getPath(),
                                res.getRootPath()));
                        }
                    }
                } catch (Exception e) {
                    // log failure
                    if (CmsLog.INIT.isWarnEnabled()) {
                        CmsLog.INIT.warn(Messages.get().getBundle().key(
                            Messages.INIT_TOOLMANAGER_TOOL_SETUP_ERROR_1,
                            res.getRootPath()), e);
                    }
                }
            }
        }
        registerHandlerList(cms, toolRoot, 1, handlers);
    }

    /**
     * Registers a new tool at a given install point for the given tool root.<p>
     * 
     * @param cms the cms context object
     * @param toolRoot the tool root
     * @param handler the handler to install
     */
    private void registerAdminTool(CmsObject cms, CmsToolRootHandler toolRoot, I_CmsToolHandler handler) {

        String link = handler.getLink();
        if (link.indexOf("?") > 0) {
            link = link.substring(0, link.indexOf("?"));
        }
        // check visibility
        if (!cms.existsResource(link)) {
            return;
        }

        //validate path
        if (!validatePath(toolRoot.getKey(), handler.getPath(), false)) {
            // log failure
            if (CmsLog.INIT.isWarnEnabled()) {
                CmsLog.INIT.warn(Messages.get().getBundle().key(
                    Messages.INIT_TOOLMANAGER_INCONSISTENT_PATH_2,
                    handler.getPath(),
                    handler.getLink()));
            }
            return;
        }

        String id = "tool" + m_tools.elementList().size();
        CmsTool tool = new CmsTool(id, handler);

        try {
            // try to find problems in custom tools
            handler.isEnabled(cms);
            handler.isVisible(cms);
        } catch (Throwable ex) {
            String message = Messages.get().getBundle().key(
                Messages.INIT_TOOLMANAGER_INSTALL_ERROR_2,
                handler.getPath(),
                handler.getLink());
            if (CmsLog.INIT.isWarnEnabled()) {
                CmsLog.INIT.warn(message);
            } else if (CmsLog.INIT.isDebugEnabled()) {
                CmsLog.INIT.debug(message, ex);
            }
            return;
        }

        try {
            // try to register, can fail if path is already used by another tool
            m_tools.addIdentifiableObject(toolRoot.getKey() + ROOT_SEPARATOR + handler.getPath(), tool);
            // just for fast association of links with tools
            m_urls.addIdentifiableObject(link, handler.getPath());
        } catch (Throwable ex) {
            CmsLog.INIT.warn(Messages.get().getBundle().key(
                Messages.INIT_TOOLMANAGER_DUPLICATED_ERROR_3,
                handler.getPath(),
                handler.getLink(),
                resolveAdminTool(toolRoot.getKey(), handler.getPath()).getHandler().getLink()));
        }
    }

    /**
     * Registers all tool handlers recursively for a given tool root.<p> 
     * 
     * @param cms the cms context object
     * @param toolRoot the tool root
     * @param len the recursion level
     * @param handlers the list of handlers to register
     */
    private void registerHandlerList(CmsObject cms, CmsToolRootHandler toolRoot, int len, List handlers) {

        boolean found = false;
        Iterator it = handlers.iterator();
        while (it.hasNext()) {
            I_CmsToolHandler handler = (I_CmsToolHandler)it.next();
            int myLen = CmsStringUtil.splitAsArray(handler.getPath(), TOOLPATH_SEPARATOR).length;
            if ((len == myLen && !handler.getPath().equals(TOOLPATH_SEPARATOR))
                || (len == 1 && handler.getPath().equals(TOOLPATH_SEPARATOR))) {
                found = true;
                registerAdminTool(cms, toolRoot, handler);
            }
        }
        if (found) {
            registerHandlerList(cms, toolRoot, len + 1, handlers);
        }

    }

    /**
     * Given a string a valid and visible tool path is computed.<p>
     * 
     * @param wp the workplace object
     * @param path the path to repair
     * 
     * @return a valida and visible tool path
     */
    private String repairPath(CmsWorkplace wp, String path) {

        String rootKey = getCurrentRoot(wp).getKey();
        // navigate until to reach a valid path
        while (!validatePath(rootKey, path, true)) {
            // log failure
            LOG.warn(Messages.get().getBundle().key(Messages.LOG_MISSING_ADMIN_TOOL_1, path));
            // try parent
            path = getParent(wp, path);
        }
        // navigate until to reach a valid tool
        while (resolveAdminTool(rootKey, path) == null) {
            // log failure
            LOG.warn(Messages.get().getBundle().key(Messages.LOG_MISSING_ADMIN_TOOL_1, path));
            // try parent
            path = getParent(wp, path);
        }

        // navegate until to reach an enabled path
        CmsTool aTool = resolveAdminTool(rootKey, path);
        while (!aTool.getHandler().isEnabled(wp.getCms())) {
            if (aTool.getHandler().getLink().equals(VIEW_JSPPAGE_LOCATION)) {
                // just grouping
                break;
            }
            path = getParent(wp, path);
            aTool = resolveAdminTool(rootKey, path);
        }

        return path;
    }

    /**
     * Tests if the full tool path is available.<p>
     * 
     * @param rootKey the root tool
     * @param toolPath the path
     * @param full if <code>true</code> the whole path is checked, if not the last part is not checked (for new tools)
     * 
     * @return if valid or not
     */
    private boolean validatePath(String rootKey, String toolPath, boolean full) {

        if (toolPath.equals(TOOLPATH_SEPARATOR)) {
            return true;
        }
        if (!toolPath.startsWith(TOOLPATH_SEPARATOR)) {
            return false;
        }
        List groups = CmsStringUtil.splitAsList(toolPath, TOOLPATH_SEPARATOR);
        Iterator itGroups = groups.iterator();
        String subpath = "";
        while (itGroups.hasNext()) {
            String group = (String)itGroups.next();
            if (subpath.length() != TOOLPATH_SEPARATOR.length()) {
                subpath += TOOLPATH_SEPARATOR + group;
            } else {
                subpath += group;
            }
            if (itGroups.hasNext() || full) {
                try {
                    // just check if the tool is available
                    resolveAdminTool(rootKey, subpath).toString();
                } catch (Exception e) {
                    return false;
                }
            }
        }
        return true;
    }
}

⌨️ 快捷键说明

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