KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > opencms > workplace > tools > CmsToolManager


1 /*
2  * File : $Source: /usr/local/cvs/opencms/src/org/opencms/workplace/tools/CmsToolManager.java,v $
3  * Date : $Date: 2006/03/28 13:10:02 $
4  * Version: $Revision: 1.44 $
5  *
6  * This library is part of OpenCms -
7  * the Open Source Content Mananagement System
8  *
9  * Copyright (c) 2005 Alkacon Software GmbH (http://www.alkacon.com)
10  *
11  * This library is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU Lesser General Public
13  * License as published by the Free Software Foundation; either
14  * version 2.1 of the License, or (at your option) any later version.
15  *
16  * This library is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19  * Lesser General Public License for more details.
20  *
21  * For further information about Alkacon Software GmbH, please see the
22  * company website: http://www.alkacon.com
23  *
24  * For further information about OpenCms, please see the
25  * project website: http://www.opencms.org
26  *
27  * You should have received a copy of the GNU Lesser General Public
28  * License along with this library; if not, write to the Free Software
29  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
30  */

31
32 package org.opencms.workplace.tools;
33
34 import org.opencms.file.CmsObject;
35 import org.opencms.file.CmsProperty;
36 import org.opencms.file.CmsResource;
37 import org.opencms.i18n.CmsEncoder;
38 import org.opencms.jsp.CmsJspActionElement;
39 import org.opencms.main.CmsException;
40 import org.opencms.main.CmsLog;
41 import org.opencms.util.CmsIdentifiableObjectContainer;
42 import org.opencms.util.CmsRequestUtil;
43 import org.opencms.util.CmsStringUtil;
44 import org.opencms.workplace.CmsDialog;
45 import org.opencms.workplace.CmsWorkplace;
46
47 import java.io.IOException JavaDoc;
48 import java.util.ArrayList JavaDoc;
49 import java.util.HashMap JavaDoc;
50 import java.util.Iterator JavaDoc;
51 import java.util.List JavaDoc;
52 import java.util.Map JavaDoc;
53
54 import javax.servlet.ServletException JavaDoc;
55
56 import org.apache.commons.logging.Log;
57
58 /**
59  * Manages the registered tools, actualizing its state every time the workplace is reinitialize.<p>
60  *
61  * Manages also the configuration settings for the administration view, and provides
62  * several tool related methods.<p>
63  *
64  * @author Michael Moossen
65  *
66  * @version $Revision: 1.44 $
67  *
68  * @since 6.0.0
69  */

70 public class CmsToolManager {
71
72     /** Root location of the administration view. */
73     public static final String JavaDoc ADMINVIEW_ROOT_LOCATION = CmsWorkplace.PATH_WORKPLACE + "views/admin";
74
75     /** Property definition name to look for. */
76     public static final String JavaDoc HANDLERCLASS_PROPERTY = "admintoolhandler-class";
77
78     /** Navegation bar separator (html code). */
79     public static final String JavaDoc NAVBAR_SEPARATOR = "\n&nbsp;&gt;&nbsp;\n";
80
81     /** Tool root separator. */
82     public static final String JavaDoc ROOT_SEPARATOR = ":";
83
84     /** Key for the default tool root, if there is no configured root with this a key, a new one will be configured. */
85     public static final String JavaDoc ROOTKEY_DEFAULT = "admin";
86
87     /** Tool path separator. */
88     public static final String JavaDoc TOOLPATH_SEPARATOR = "/";
89
90     /** Location of the default admin view jsp page. */
91     public static final String JavaDoc VIEW_JSPPAGE_LOCATION = ADMINVIEW_ROOT_LOCATION + "/admin-main.jsp";
92
93     /** The static log object for this class. */
94     private static final Log LOG = CmsLog.getLog(CmsToolManager.class);
95
96     /** List of all available roots. */
97     private final CmsIdentifiableObjectContainer m_roots;
98
99     /** List of all available tools. */
100     private final CmsIdentifiableObjectContainer m_tools;
101
102     /** List of all available urls and related tool paths. */
103     private final CmsIdentifiableObjectContainer m_urls;
104
105     /**
106      * Default constructor.<p>
107      */

108     public CmsToolManager() {
109
110         m_roots = new CmsIdentifiableObjectContainer(true, false);
111         m_tools = new CmsIdentifiableObjectContainer(true, false);
112         m_urls = new CmsIdentifiableObjectContainer(false, false);
113     }
114
115     /**
116      * Returns the OpenCms link for the given tool path which requires no parameters.<p>
117      *
118      * @param jsp the jsp action element
119      * @param toolPath the tool path
120      *
121      * @return the OpenCms link for the given tool path which requires parameters
122      */

123     public static String JavaDoc linkForToolPath(CmsJspActionElement jsp, String JavaDoc toolPath) {
124
125         StringBuffer JavaDoc result = new StringBuffer JavaDoc();
126         result.append(jsp.link(VIEW_JSPPAGE_LOCATION));
127         result.append('?');
128         result.append(CmsToolDialog.PARAM_PATH);
129         result.append('=');
130         result.append(CmsEncoder.encode(toolPath));
131         return result.toString();
132     }
133
134     /**
135      * Returns the OpenCms link for the given tool path which requires parameters.<p>
136      *
137      * Please note: Don't overuse the parameter map because this will likely introduce issues
138      * with encoding. If possible, don't pass parameters at all, or only very simple parameters
139      * with no special chars that can easily be parsed.<p>
140      *
141      * @param jsp the jsp action element
142      * @param toolPath the tool path
143      * @param params the map of required tool parameters
144      *
145      * @return the OpenCms link for the given tool path which requires parameters
146      */

147     public static String JavaDoc linkForToolPath(CmsJspActionElement jsp, String JavaDoc toolPath, Map JavaDoc params) {
148
149         if (params == null) {
150             // no parameters - take the shortcut
151
return linkForToolPath(jsp, toolPath);
152         }
153         params.put(CmsToolDialog.PARAM_PATH, toolPath);
154         return CmsRequestUtil.appendParameters(jsp.link(VIEW_JSPPAGE_LOCATION), params, true);
155     }
156
157     /**
158      * Adds a new tool root to the tool manager.<p>
159      *
160      * @param toolRoot the tool root to add
161      */

162     public void addToolRoot(CmsToolRootHandler toolRoot) {
163
164         m_roots.addIdentifiableObject(toolRoot.getKey(), toolRoot);
165     }
166
167     /**
168      * Called by the <code>{@link org.opencms.workplace.CmsWorkplaceManager#initialize(CmsObject)}</code> method.<p>
169      *
170      * @param cms the admin cms context
171      */

172     public void configure(CmsObject cms) {
173
174         if (CmsLog.INIT.isInfoEnabled()) {
175             CmsLog.INIT.info(Messages.get().getBundle().key(Messages.INIT_TOOLMANAGER_CREATED_0));
176         }
177         if (m_roots.getObject(ROOTKEY_DEFAULT) == null) {
178             CmsToolRootHandler defToolRoot = new CmsToolRootHandler();
179             defToolRoot.setKey(ROOTKEY_DEFAULT);
180             defToolRoot.setUri(CmsWorkplace.PATH_WORKPLACE + "admin/");
181             defToolRoot.setName("${key." + Messages.GUI_ADMIN_VIEW_ROOT_NAME_0 + "}");
182             defToolRoot.setHelpText("${key." + Messages.GUI_ADMIN_VIEW_ROOT_HELP_0 + "}");
183             addToolRoot(defToolRoot);
184         }
185         m_tools.clear();
186         m_urls.clear();
187         Iterator JavaDoc it = getToolRoots().iterator();
188         while (it.hasNext()) {
189             CmsToolRootHandler toolRoot = (CmsToolRootHandler)it.next();
190             if (!cms.existsResource(toolRoot.getUri())) {
191                 if (CmsLog.INIT.isInfoEnabled()) {
192                     CmsLog.INIT.info(Messages.get().getBundle().key(
193                         Messages.INIT_TOOLMANAGER_ROOT_SKIPPED_2,
194                         toolRoot.getKey(),
195                         toolRoot.getUri()));
196                 }
197                 continue;
198             }
199             try {
200                 toolRoot.setup(cms, null, toolRoot.getUri());
201                 configureToolRoot(cms, toolRoot);
202                 // log info
203
if (CmsLog.INIT.isInfoEnabled()) {
204                     CmsLog.INIT.info(Messages.get().getBundle().key(
205                         Messages.INIT_TOOLMANAGER_SETUP_1,
206                         toolRoot.getKey()));
207                 }
208             } catch (CmsException e) {
209                 // log failure
210
if (CmsLog.INIT.isWarnEnabled()) {
211                     CmsLog.INIT.warn(Messages.get().getBundle().key(
212                         Messages.INIT_TOOLMANAGER_SETUP_ERROR_1,
213                         toolRoot.getKey()), e);
214                 }
215             }
216         }
217     }
218
219     /**
220      * Returns the navegation bar html code for the given tool path.<p>
221      *
222      * @param toolPath the path
223      * @param wp the jsp page
224      *
225      * @return the html code
226      */

227     public String JavaDoc generateNavBar(String JavaDoc toolPath, CmsWorkplace wp) {
228
229         if (toolPath.equals(getBaseToolPath(wp))) {
230             return "<div class='pathbar'>&nbsp;</div>\n";
231         }
232         CmsTool adminTool = resolveAdminTool(getCurrentRoot(wp).getKey(), toolPath);
233         String JavaDoc html = A_CmsHtmlIconButton.defaultButtonHtml(
234             wp.getJsp(),
235             CmsHtmlIconButtonStyleEnum.SMALL_ICON_TEXT,
236             "nav" + adminTool.getId(),
237             adminTool.getHandler().getShortName(),
238             null,
239             false,
240             null,
241             null,
242             null);
243         String JavaDoc parent = toolPath;
244         while (!parent.equals(getBaseToolPath(wp))) {
245             parent = getParent(wp, parent);
246             adminTool = resolveAdminTool(getCurrentRoot(wp).getKey(), parent);
247
248             String JavaDoc id = "nav" + adminTool.getId();
249             String JavaDoc link = linkForToolPath(wp.getJsp(), parent, adminTool.getHandler().getParameters(wp));
250             String JavaDoc onClic = "openPage('" + link + "');";
251             String JavaDoc buttonHtml = A_CmsHtmlIconButton.defaultButtonHtml(
252                 wp.getJsp(),
253                 CmsHtmlIconButtonStyleEnum.SMALL_ICON_TEXT,
254                 id,
255                 adminTool.getHandler().getName(),
256                 adminTool.getHandler().getHelpText(),
257                 true,
258                 null,
259                 null,
260                 onClic);
261             html = buttonHtml + NAVBAR_SEPARATOR + html;
262         }
263
264         return CmsToolMacroResolver.resolveMacros("<div class='pathbar'>\n" + html + "&nbsp;</div>\n", wp);
265     }
266
267     /**
268      * Returns the base tool path for the active user.<p>
269      *
270      * @param wp the workplace object
271      *
272      * @return the base tool path for the active user
273      */

274     public String JavaDoc getBaseToolPath(CmsWorkplace wp) {
275
276         CmsToolUserData userData = getUserData(wp);
277         String JavaDoc path = TOOLPATH_SEPARATOR;
278         if (userData != null) {
279             path = userData.getBaseTool(getCurrentRoot(wp).getKey());
280         }
281         return path;
282     }
283
284     /**
285      * Returns the current user's root handler.<p>
286      *
287      * @param wp the workplace context
288      *
289      * @return the current user's root handler
290      */

291     public CmsToolRootHandler getCurrentRoot(CmsWorkplace wp) {
292
293         CmsToolUserData userData = getUserData(wp);
294         String JavaDoc root = ROOTKEY_DEFAULT;
295         if (userData != null && m_roots.getObject(userData.getRootKey()) != null) {
296             root = userData.getRootKey();
297         }
298         return (CmsToolRootHandler)m_roots.getObject(root);
299     }
300
301     /**
302      * Returns the current tool.<p>
303      *
304      * @param wp the workplace object
305      *
306      * @return the current tool
307      */

308     public CmsTool getCurrentTool(CmsWorkplace wp) {
309
310         return resolveAdminTool(getCurrentRoot(wp).getKey(), getCurrentToolPath(wp));
311     }
312
313     /**
314      * Returns the current tool path.<p>
315      *
316      * @param wp the workplace object
317      *
318      * @return the current tool path
319      */

320     public String JavaDoc getCurrentToolPath(CmsWorkplace wp) {
321
322         CmsToolUserData userData = getUserData(wp);
323         String JavaDoc path = getBaseToolPath(wp);
324         if (userData != null) {
325             path = userData.getCurrentToolPath(getCurrentRoot(wp).getKey());
326         }
327         return path;
328     }
329
330     /**
331      * Returns the path to the parent of the tool identified by the given tool path.<p>
332      *
333      * The parent of the root is the same root.<p>
334      *
335      * @param wp the workplace object
336      * @param toolPath the abstract tool path
337      *
338      * @return his parent
339      */

340     public String JavaDoc getParent(CmsWorkplace wp, String JavaDoc toolPath) {
341
342         if (toolPath.equals(getBaseToolPath(wp))) {
343             return toolPath;
344         }
345         int pos = toolPath.lastIndexOf(TOOLPATH_SEPARATOR);
346         return pos <= 0 ? TOOLPATH_SEPARATOR : toolPath.substring(0, pos);
347     }
348
349     /**
350      * Returns a list with all registered tools.<p>
351      *
352      * @return list if <code>{@link CmsTool}</code>
353      */

354     public List JavaDoc getToolHandlers() {
355
356         return m_tools.elementList();
357     }
358
359     /**
360      * Returns a list of tool roots.<p>
361      *
362      * @return a list of {@link CmsToolRootHandler} objects
363      */

364     public List JavaDoc getToolRoots() {
365
366         return m_roots.elementList();
367     }
368
369     /**
370      * Returns a list of all tools in the given path.<p>
371      *
372      * @param wp the workplace context
373      * @param baseTool the path
374      * @param includeSubtools if the tools in subfolders should be also returned
375      *
376      * @return a list of {@link CmsTool} objects
377      */

378     public List JavaDoc getToolsForPath(CmsWorkplace wp, String JavaDoc baseTool, boolean includeSubtools) {
379
380         List JavaDoc toolList = new ArrayList JavaDoc();
381         String JavaDoc rootKey = getCurrentRoot(wp).getKey();
382         Iterator JavaDoc itTools = m_tools.elementList().iterator();
383         while (itTools.hasNext()) {
384             CmsTool tool = (CmsTool)itTools.next();
385             String JavaDoc path = tool.getHandler().getPath();
386             if (resolveAdminTool(rootKey, path) != tool) {
387                 continue;
388             }
389             if (path.equals(TOOLPATH_SEPARATOR)) {
390                 continue;
391             }
392             // leave out everything above the base
393
if (!path.startsWith(baseTool)) {
394                 continue;
395             }
396             // filter for path
397
if (baseTool.equals(TOOLPATH_SEPARATOR) || path.startsWith(baseTool + TOOLPATH_SEPARATOR)) {
398                 // filter sub tree
399
if (includeSubtools || path.indexOf(TOOLPATH_SEPARATOR, baseTool.length() + 1) < 0) {
400                     toolList.add(tool);
401                 }
402             }
403         }
404         return toolList;
405     }
406
407     /**
408      * Returns the <code>{@link CmsToolUserData}</code> object for a given user.<p>
409      *
410      * @param wp the workplace object
411      *
412      * @return the current user data
413      */

414     public CmsToolUserData getUserData(CmsWorkplace wp) {
415
416         CmsToolUserData userData = wp.getSettings().getToolUserData();
417         if (userData == null) {
418             userData = new CmsToolUserData();
419             userData.setRootKey(ROOTKEY_DEFAULT);
420             Iterator JavaDoc it = getToolRoots().iterator();
421             while (it.hasNext()) {
422                 CmsToolRootHandler root = (CmsToolRootHandler)it.next();
423                 userData.setCurrentToolPath(root.getKey(), TOOLPATH_SEPARATOR);
424                 userData.setBaseTool(root.getKey(), TOOLPATH_SEPARATOR);
425             }
426             wp.getSettings().setToolUserData(userData);
427         }
428         return userData;
429     }
430
431     /**
432      * Returns <code>true</code> if there is at least one tool registered using the given url.<p>
433      *
434      * @param url the url of the tool
435      *
436      * @return <code>true</code> if there is at least one tool registered using the given url
437      */

438     public boolean hasToolPathForUrl(String JavaDoc url) {
439
440         List JavaDoc toolPaths = (List JavaDoc)m_urls.getObject(url);
441         return (toolPaths != null && !toolPaths.isEmpty());
442     }
443
444     /**
445      * This method initializes the tool manager for the current user.<p>
446      *
447      * @param wp the jsp page comming from
448      */

449     public synchronized void initParams(CmsToolDialog wp) {
450
451         setCurrentRoot(wp, wp.getParamRoot());
452         setCurrentToolPath(wp, wp.getParamPath());
453         setBaseToolPath(wp, wp.getParamBase());
454
455         // if the current tool path is not under the current root, set the current root as the current tool
456
if (!getCurrentToolPath(wp).startsWith(getBaseToolPath(wp))) {
457             setCurrentToolPath(wp, getBaseToolPath(wp));
458         }
459         wp.setParamPath(getCurrentToolPath(wp));
460         wp.setParamBase(getBaseToolPath(wp));
461         wp.setParamRoot(getCurrentRoot(wp).getKey());
462     }
463
464     /**
465      * Redirects to the given page with the given parameters.<p>
466      *
467      * @param wp the workplace object
468      * @param pagePath the path to the page to redirect to
469      * @param params the parameters to send
470      *
471      * @throws IOException in case of errors during forwarding
472      * @throws ServletException in case of errors during forwarding
473      */

474     public void jspForwardPage(CmsWorkplace wp, String JavaDoc pagePath, Map JavaDoc params) throws IOException JavaDoc, ServletException JavaDoc {
475
476         Map JavaDoc newParams = new HashMap JavaDoc();
477         // add query parameters to the parameter map if required
478
if (pagePath.indexOf("?") > 0) {
479             String JavaDoc query = pagePath.substring(pagePath.indexOf("?"));
480             pagePath = pagePath.substring(0, pagePath.indexOf("?"));
481             Map JavaDoc reqParameters = CmsRequestUtil.createParameterMap(query);
482             newParams.putAll(reqParameters);
483         }
484         if (params != null) {
485             newParams.putAll(params);
486         }
487
488         // put close link if not set
489
if (!newParams.containsKey(CmsDialog.PARAM_CLOSELINK)) {
490             Map JavaDoc argMap = resolveAdminTool(getCurrentRoot(wp).getKey(), getCurrentToolPath(wp)).getHandler().getParameters(
491                 wp);
492             newParams.put(CmsDialog.PARAM_CLOSELINK, linkForToolPath(wp.getJsp(), getCurrentToolPath(wp), argMap));
493         }
494         wp.setForwarded(true);
495         // forward to the requested page uri
496
CmsRequestUtil.forwardRequest(
497             wp.getJsp().link(pagePath),
498             CmsRequestUtil.createParameterMap(newParams),
499             wp.getJsp().getRequest(),
500             wp.getJsp().getResponse());
501     }
502
503     /**
504      * Redirects to the given tool with the given parameters.<p>
505      *
506      * @param wp the workplace object
507      * @param toolPath the path to the tool to redirect to
508      * @param params the parameters to send
509      *
510      * @throws IOException in case of errors during forwarding
511      * @throws ServletException in case of errors during forwarding
512      */

513     public void jspForwardTool(CmsWorkplace wp, String JavaDoc toolPath, Map JavaDoc params) throws IOException JavaDoc, ServletException JavaDoc {
514
515         Map JavaDoc newParams;
516         if (params == null) {
517             newParams = new HashMap JavaDoc();
518         } else {
519             newParams = new HashMap JavaDoc(params);
520         }
521         // update path param
522
newParams.put(CmsToolDialog.PARAM_PATH, toolPath);
523         jspForwardPage(wp, VIEW_JSPPAGE_LOCATION, newParams);
524     }
525
526     /**
527      * Returns the admin tool corresponding to the given abstract path.<p>
528      *
529      * @param rootKey the tool root
530      * @param toolPath the path
531      *
532      * @return the corresponding tool, or <code>null</code> if not found
533      */

534     public CmsTool resolveAdminTool(String JavaDoc rootKey, String JavaDoc toolPath) {
535
536         return (CmsTool)m_tools.getObject(rootKey + ROOT_SEPARATOR + toolPath);
537     }
538
539     /**
540      * Sets the base tool path.<p>
541      *
542      * @param wp the workplace object
543      * @param baseToolPath the base tool path to set
544      */

545     public void setBaseToolPath(CmsWorkplace wp, String JavaDoc baseToolPath) {
546
547         // use last used base if param empty
548
if (CmsStringUtil.isEmpty(baseToolPath) || baseToolPath.trim().equals("null")) {
549             baseToolPath = getBaseToolPath(wp);
550         }
551         baseToolPath = repairPath(wp, baseToolPath);
552         // set it
553
CmsToolUserData userData = getUserData(wp);
554         userData.setBaseTool(userData.getRootKey(), baseToolPath);
555     }
556
557     /**
558      * Sets the current user's root key.<p>
559      *
560      * @param wp the workplace context
561      * @param key the current user's root key to set
562      */

563     public void setCurrentRoot(CmsWorkplace wp, String JavaDoc key) {
564
565         // use last used root if param empty
566
if (CmsStringUtil.isEmpty(key) || key.trim().equals("null")) {
567             key = getCurrentRoot(wp).getKey();
568         }
569         // set it
570
CmsToolUserData userData = getUserData(wp);
571         userData.setRootKey(key);
572     }
573
574     /**
575      * Sets the current tool path.<p>
576      *
577      * @param wp the workplace object
578      * @param currentToolPath the current tool path to set
579      */

580     public void setCurrentToolPath(CmsWorkplace wp, String JavaDoc currentToolPath) {
581
582         // use last used path if param empty
583
if (CmsStringUtil.isEmptyOrWhitespaceOnly(currentToolPath) || currentToolPath.trim().equals("null")) {
584             currentToolPath = getCurrentToolPath(wp);
585         }
586         currentToolPath = repairPath(wp, currentToolPath);
587         // use it
588
CmsToolUserData userData = getUserData(wp);
589         userData.setCurrentToolPath(userData.getRootKey(), currentToolPath);
590     }
591
592     /**
593      * Configures a whole tool root with all its tools.<p>
594      *
595      * @param toolRoot the tool root to configure
596      *
597      * @throws CmsException if something goes wrong
598      */

599     private void configureToolRoot(CmsObject cms, CmsToolRootHandler toolRoot) throws CmsException {
600
601         List JavaDoc handlers = new ArrayList JavaDoc();
602
603         // add tool root handler
604
handlers.add(toolRoot);
605
606         // look in every file under the root uri for valid
607
// admin tools and register them
608
List JavaDoc resources = cms.readResourcesWithProperty(toolRoot.getUri(), HANDLERCLASS_PROPERTY);
609         Iterator JavaDoc itRes = resources.iterator();
610         while (itRes.hasNext()) {
611             CmsResource res = (CmsResource)itRes.next();
612             CmsProperty prop = cms.readPropertyObject(res.getRootPath(), HANDLERCLASS_PROPERTY, false);
613             if (!prop.isNullProperty()) {
614                 try {
615                     // instantiate the handler
616
Class JavaDoc handlerClass = Class.forName(prop.getValue());
617                     I_CmsToolHandler handler = (I_CmsToolHandler)handlerClass.newInstance();
618
619                     if (!handler.setup(cms, toolRoot, res.getRootPath())) {
620                         // log failure
621
if (CmsLog.INIT.isWarnEnabled()) {
622                             CmsLog.INIT.warn(Messages.get().getBundle().key(
623                                 Messages.INIT_TOOLMANAGER_TOOL_SETUP_ERROR_1,
624                                 res.getRootPath()));
625                         }
626                     }
627
628                     // keep for later use
629
handlers.add(handler);
630                     // log success
631
if (CmsLog.INIT.isDebugEnabled()) {
632                         if (!handler.getLink().equals(VIEW_JSPPAGE_LOCATION)) {
633                             CmsLog.INIT.debug(Messages.get().getBundle().key(
634                                 Messages.INIT_TOOLMANAGER_NEWTOOL_FOUND_2,
635                                 handler.getPath(),
636                                 handler.getLink()));
637                         } else {
638                             CmsLog.INIT.debug(Messages.get().getBundle().key(
639                                 Messages.INIT_TOOLMANAGER_NEWTOOL_FOUND_2,
640                                 handler.getPath(),
641                                 res.getRootPath()));
642                         }
643                     }
644                 } catch (Exception JavaDoc e) {
645                     // log failure
646
if (CmsLog.INIT.isWarnEnabled()) {
647                         CmsLog.INIT.warn(Messages.get().getBundle().key(
648                             Messages.INIT_TOOLMANAGER_TOOL_SETUP_ERROR_1,
649                             res.getRootPath()), e);
650                     }
651                 }
652             }
653         }
654         registerHandlerList(cms, toolRoot, 1, handlers);
655     }
656
657     /**
658      * Registers a new tool at a given install point for the given tool root.<p>
659      *
660      * @param cms the cms context object
661      * @param toolRoot the tool root
662      * @param handler the handler to install
663      */

664     private void registerAdminTool(CmsObject cms, CmsToolRootHandler toolRoot, I_CmsToolHandler handler) {
665
666         String JavaDoc link = handler.getLink();
667         if (link.indexOf("?") > 0) {
668             link = link.substring(0, link.indexOf("?"));
669         }
670         // check visibility
671
if (!cms.existsResource(link)) {
672             return;
673         }
674
675         //validate path
676
if (!validatePath(toolRoot.getKey(), handler.getPath(), false)) {
677             // log failure
678
if (CmsLog.INIT.isWarnEnabled()) {
679                 CmsLog.INIT.warn(Messages.get().getBundle().key(
680                     Messages.INIT_TOOLMANAGER_INCONSISTENT_PATH_2,
681                     handler.getPath(),
682                     handler.getLink()));
683             }
684             return;
685         }
686
687         String JavaDoc id = "tool" + m_tools.elementList().size();
688         CmsTool tool = new CmsTool(id, handler);
689
690         try {
691             // try to find problems in custom tools
692
handler.isEnabled(cms);
693             handler.isVisible(cms);
694         } catch (Throwable JavaDoc ex) {
695             String JavaDoc message = Messages.get().getBundle().key(
696                 Messages.INIT_TOOLMANAGER_INSTALL_ERROR_2,
697                 handler.getPath(),
698                 handler.getLink());
699             if (CmsLog.INIT.isWarnEnabled()) {
700                 CmsLog.INIT.warn(message);
701             } else if (CmsLog.INIT.isDebugEnabled()) {
702                 CmsLog.INIT.debug(message, ex);
703             }
704             return;
705         }
706
707         try {
708             // try to register, can fail if path is already used by another tool
709
m_tools.addIdentifiableObject(toolRoot.getKey() + ROOT_SEPARATOR + handler.getPath(), tool);
710             // just for fast association of links with tools
711
m_urls.addIdentifiableObject(link, handler.getPath());
712         } catch (Throwable JavaDoc ex) {
713             CmsLog.INIT.warn(Messages.get().getBundle().key(
714                 Messages.INIT_TOOLMANAGER_DUPLICATED_ERROR_3,
715                 handler.getPath(),
716                 handler.getLink(),
717                 resolveAdminTool(toolRoot.getKey(), handler.getPath()).getHandler().getLink()));
718         }
719     }
720
721     /**
722      * Registers all tool handlers recursively for a given tool root.<p>
723      *
724      * @param cms the cms context object
725      * @param toolRoot the tool root
726      * @param len the recursion level
727      * @param handlers the list of handlers to register
728      */

729     private void registerHandlerList(CmsObject cms, CmsToolRootHandler toolRoot, int len, List JavaDoc handlers) {
730
731         boolean found = false;
732         Iterator JavaDoc it = handlers.iterator();
733         while (it.hasNext()) {
734             I_CmsToolHandler handler = (I_CmsToolHandler)it.next();
735             int myLen = CmsStringUtil.splitAsArray(handler.getPath(), TOOLPATH_SEPARATOR).length;
736             if ((len == myLen && !handler.getPath().equals(TOOLPATH_SEPARATOR))
737                 || (len == 1 && handler.getPath().equals(TOOLPATH_SEPARATOR))) {
738                 found = true;
739                 registerAdminTool(cms, toolRoot, handler);
740             }
741         }
742         if (found) {
743             registerHandlerList(cms, toolRoot, len + 1, handlers);
744         }
745
746     }
747
748     /**
749      * Given a string a valid and visible tool path is computed.<p>
750      *
751      * @param wp the workplace object
752      * @param path the path to repair
753      *
754      * @return a valida and visible tool path
755      */

756     private String JavaDoc repairPath(CmsWorkplace wp, String JavaDoc path) {
757
758         String JavaDoc rootKey = getCurrentRoot(wp).getKey();
759         // navigate until to reach a valid path
760
while (!validatePath(rootKey, path, true)) {
761             // log failure
762
LOG.warn(Messages.get().getBundle().key(Messages.LOG_MISSING_ADMIN_TOOL_1, path));
763             // try parent
764
path = getParent(wp, path);
765         }
766         // navigate until to reach a valid tool
767
while (resolveAdminTool(rootKey, path) == null) {
768             // log failure
769
LOG.warn(Messages.get().getBundle().key(Messages.LOG_MISSING_ADMIN_TOOL_1, path));
770             // try parent
771
path = getParent(wp, path);
772         }
773
774         // navegate until to reach an enabled path
775
CmsTool aTool = resolveAdminTool(rootKey, path);
776         while (!aTool.getHandler().isEnabled(wp.getCms())) {
777             if (aTool.getHandler().getLink().equals(VIEW_JSPPAGE_LOCATION)) {
778                 // just grouping
779
break;
780             }
781             path = getParent(wp, path);
782             aTool = resolveAdminTool(rootKey, path);
783         }
784
785         return path;
786     }
787
788     /**
789      * Tests if the full tool path is available.<p>
790      *
791      * @param rootKey the root tool
792      * @param toolPath the path
793      * @param full if <code>true</code> the whole path is checked, if not the last part is not checked (for new tools)
794      *
795      * @return if valid or not
796      */

797     private boolean validatePath(String JavaDoc rootKey, String JavaDoc toolPath, boolean full) {
798
799         if (toolPath.equals(TOOLPATH_SEPARATOR)) {
800             return true;
801         }
802         if (!toolPath.startsWith(TOOLPATH_SEPARATOR)) {
803             return false;
804         }
805         List JavaDoc groups = CmsStringUtil.splitAsList(toolPath, TOOLPATH_SEPARATOR);
806         Iterator JavaDoc itGroups = groups.iterator();
807         String JavaDoc subpath = "";
808         while (itGroups.hasNext()) {
809             String JavaDoc group = (String JavaDoc)itGroups.next();
810             if (subpath.length() != TOOLPATH_SEPARATOR.length()) {
811                 subpath += TOOLPATH_SEPARATOR + group;
812             } else {
813                 subpath += group;
814             }
815             if (itGroups.hasNext() || full) {
816                 try {
817                     // just check if the tool is available
818
resolveAdminTool(rootKey, subpath).toString();
819                 } catch (Exception JavaDoc e) {
820                     return false;
821                 }
822             }
823         }
824         return true;
825     }
826 }
Popular Tags