AdminConsole.java 20.7 KB
Newer Older
Bill Lynch's avatar
Bill Lynch committed
1 2
/**
 * $RCSfile$
Bill Lynch's avatar
Bill Lynch committed
3
 * $Revision$
Bill Lynch's avatar
Bill Lynch committed
4 5
 * $Date$
 *
6
 * Copyright (C) 2004-2008 Jive Software. All rights reserved.
Bill Lynch's avatar
Bill Lynch committed
7
 *
8 9 10 11 12 13 14 15 16 17 18
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
Bill Lynch's avatar
Bill Lynch committed
19 20 21 22 23 24
 */

package org.jivesoftware.admin;

import java.io.InputStream;
import java.net.URL;
25 26 27 28
import java.util.Enumeration;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;
Bill Lynch's avatar
Bill Lynch committed
29

30 31 32 33 34 35 36 37 38 39 40 41 42
import org.dom4j.Document;
import org.dom4j.DocumentFactory;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import org.jivesoftware.openfire.XMPPServer;
import org.jivesoftware.openfire.clearspace.ClearspaceManager;
import org.jivesoftware.util.ClassUtils;
import org.jivesoftware.util.LocaleUtils;
import org.jivesoftware.util.PropertyEventDispatcher;
import org.jivesoftware.util.PropertyEventListener;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Bill Lynch's avatar
Bill Lynch committed
43
/**
44
 * A model for admin tab and sidebar info. This class loads in XML definitions of the
45
 * data and produces an in-memory model.<p>
Bill Lynch's avatar
Bill Lynch committed
46
 *
47 48 49 50 51
 * This class loads its data from the <tt>admin-sidebar.xml</tt> file which is assumed
 * to be in the main application jar file. In addition, it will load files from
 * <tt>META-INF/admin-sidebar.xml</tt> if they're found. This allows developers to
 * extend the functionality of the admin console to provide more options. See the main
 * <tt>admin-sidebar.xml</tt> file for documentation of its format.
Bill Lynch's avatar
Bill Lynch committed
52 53 54
 */
public class AdminConsole {

55 56
	private static final Logger Log = LoggerFactory.getLogger(AdminConsole.class);

57
    private static Element coreModel;
58
    private static Map<String,Element> overrideModels;
59
    private static Element generatedModel;
Bill Lynch's avatar
Bill Lynch committed
60 61

    static {
Matt Tucker's avatar
Matt Tucker committed
62
        overrideModels = new LinkedHashMap<String,Element>();
Bill Lynch's avatar
Bill Lynch committed
63
        load();
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
        
        // Detect when a new auth provider class is set to ClearspaceAuthProvider
        // then rebuild the model to add the Clearspace tab
        // This is to add the tab after Openfire setup
        PropertyEventListener propListener = new PropertyEventListener() {
            public void propertySet(String property, Map params) {
                if ("provider.auth.className".equals(property)) {
                    String value = (String) params.get("value");
                    if ("org.jivesoftware.openfire.clearspace.ClearspaceAuthProvider".equals(value)) {
                        rebuildModel();
                    }
                }
            }

            public void propertyDeleted(String property, Map params) {
                //Ignore
            }
            public void xmlPropertySet(String property, Map params) {
                //Ignore
            }
            public void xmlPropertyDeleted(String property, Map params) {
                //Ignore
            }
        };
        PropertyEventDispatcher.addListener(propListener);
Bill Lynch's avatar
Bill Lynch committed
89 90 91 92
    }

    /** Not instantiatable */
    private AdminConsole() {
93

Bill Lynch's avatar
Bill Lynch committed
94 95
    }

Bill Lynch's avatar
Bill Lynch committed
96 97 98
    /**
     * Adds XML stream to the tabs/sidebar model.
     *
99
     * @param name the name.
Bill Lynch's avatar
Bill Lynch committed
100 101 102
     * @param in the XML input stream.
     * @throws Exception if an error occurs when parsing the XML or adding it to the model.
     */
103
    public static void addModel(String name, InputStream in) throws Exception {
Matt Tucker's avatar
Matt Tucker committed
104 105
        SAXReader saxReader = new SAXReader();
        Document doc = saxReader.read(in);
106
        addModel(name, (Element)doc.selectSingleNode("/adminconsole"));
Matt Tucker's avatar
Matt Tucker committed
107 108 109 110 111
    }

    /**
     * Adds an &lt;adminconsole&gt; Element to the tabs/sidebar model.
     *
112
     * @param name the name.
Matt Tucker's avatar
Matt Tucker committed
113 114 115
     * @param element the Element
     * @throws Exception if an error occurs.
     */
116 117 118 119 120 121 122 123 124 125 126 127
    public static void addModel(String name, Element element) throws Exception {
        overrideModels.put(name, element);
        rebuildModel();
    }

    /**
     * Removes an &lt;adminconsole&gt; Element from the tabs/sidebar model.
     *
     * @param name the name.
     */
    public static void removeModel(String name) {
        overrideModels.remove(name);
128
        rebuildModel();
Bill Lynch's avatar
Bill Lynch committed
129 130
    }

Bill Lynch's avatar
Bill Lynch committed
131 132
    /**
     * Returns the name of the application.
133 134
     *
     * @return the name of the application.
Bill Lynch's avatar
Bill Lynch committed
135
     */
136
    public static synchronized String getAppName() {
Matt Tucker's avatar
Matt Tucker committed
137
        Element appName = (Element)generatedModel.selectSingleNode("//adminconsole/global/appname");
138
        if (appName != null) {
Matt Tucker's avatar
Matt Tucker committed
139 140
            String pluginName = appName.attributeValue("plugin");
            return getAdminText(appName.getText(), pluginName);
141 142 143 144
        }
        else {
            return null;
        }
Bill Lynch's avatar
Bill Lynch committed
145 146 147
    }

    /**
148 149 150
     * Returns the URL of the main logo image for the admin console.
     *
     * @return the logo image.
Bill Lynch's avatar
Bill Lynch committed
151
     */
152
    public static synchronized String getLogoImage() {
153
        Element globalLogoImage = (Element)generatedModel.selectSingleNode(
Matt Tucker's avatar
Matt Tucker committed
154
                "//adminconsole/global/logo-image");
155
        if (globalLogoImage != null) {
Matt Tucker's avatar
Matt Tucker committed
156 157
            String pluginName = globalLogoImage.attributeValue("plugin");
            return getAdminText(globalLogoImage.getText(), pluginName);
158
        }
159
        else {
Bill Lynch's avatar
Bill Lynch committed
160 161 162 163
            return null;
        }
    }

164 165 166 167 168
    /**
     * Returns the URL of the login image for the admin console.
     *
     * @return the login image.
     */
169
    public static synchronized String getLoginLogoImage() {
170
        Element globalLoginLogoImage = (Element)generatedModel.selectSingleNode(
Matt Tucker's avatar
Matt Tucker committed
171
                "//adminconsole/global/login-image");
172
        if (globalLoginLogoImage != null) {
Matt Tucker's avatar
Matt Tucker committed
173 174
            String pluginName = globalLoginLogoImage.attributeValue("plugin");
            return getAdminText(globalLoginLogoImage.getText(), pluginName);
175 176 177 178 179 180 181 182 183 184 185
        }
        else {
            return null;
        }
    }

    /**
     * Returns the version string displayed in the admin console.
     *
     * @return the version string.
     */
186
    public static synchronized String getVersionString() {
187
        Element globalVersion = (Element)generatedModel.selectSingleNode(
Matt Tucker's avatar
Matt Tucker committed
188
                "//adminconsole/global/version");
189
        if (globalVersion != null) {
Matt Tucker's avatar
Matt Tucker committed
190 191
            String pluginName = globalVersion.attributeValue("plugin");
            return getAdminText(globalVersion.getText(), pluginName);
192 193
        }
        else {
194
            // Default to the Openfire version if none has been provided via XML.
195
            XMPPServer xmppServer = XMPPServer.getInstance();
196
            return xmppServer.getServerInfo().getVersion().getVersionString();
197 198 199
        }
    }

Bill Lynch's avatar
Bill Lynch committed
200
    /**
201
     * Returns the model. The model should be considered read-only.
Bill Lynch's avatar
Bill Lynch committed
202
     *
203
     * @return the model.
Bill Lynch's avatar
Bill Lynch committed
204
     */
205
    public static synchronized Element getModel() {
206
        return generatedModel;
Bill Lynch's avatar
Bill Lynch committed
207 208 209
    }

    /**
210 211 212 213 214
     * Convenience method to select an element from the model by its ID. If an
     * element with a matching ID is not found, <tt>null</tt> will be returned.
     *
     * @param id the ID.
     * @return the element.
Bill Lynch's avatar
Bill Lynch committed
215
     */
216
    public static synchronized Element getElemnetByID(String id) {
Matt Tucker's avatar
Matt Tucker committed
217
        return (Element)generatedModel.selectSingleNode("//*[@id='" + id + "']");
Bill Lynch's avatar
Bill Lynch committed
218 219
    }

Matt Tucker's avatar
Matt Tucker committed
220 221 222 223 224 225 226 227 228
    /**
     * Returns a text element for the admin console, applying the appropriate locale.
     * Internationalization logic will only be applied if the String is specially encoded
     * in the format "${key.name}". If it is, the String is pulled from the resource bundle.
     * If the pluginName is not <tt>null</tt>, the plugin's resource bundle will be used
     * to look up the key.
     *
     * @param string the String.
     * @param pluginName the name of the plugin that the i18n String can be found in,
229
     *      or <tt>null</tt> if the standard Openfire resource bundle should be used.
Matt Tucker's avatar
Matt Tucker committed
230 231 232 233 234 235 236 237 238 239 240 241 242 243
     * @return the string, or if the string is encoded as an i18n key, the value from
     *      the appropriate resource bundle.
     */
    public static String getAdminText(String string, String pluginName) {
        if (string == null) {
            return null;
        }
        // Look for the key symbol:
        if (string.indexOf("${") == 0 && string.indexOf("}") == string.length()-1) {
            return LocaleUtils.getLocalizedString(string.substring(2, string.length()-1), pluginName);
        }
        return string;
    }

Bill Lynch's avatar
Bill Lynch committed
244
    private static void load() {
245
        // Load the core model as the admin-sidebar.xml file from the classpath.
Bill Lynch's avatar
Bill Lynch committed
246 247
        InputStream in = ClassUtils.getResourceAsStream("/admin-sidebar.xml");
        if (in == null) {
248
            Log.error("Failed to load admin-sidebar.xml file from Openfire classes - admin "
Bill Lynch's avatar
Bill Lynch committed
249 250 251 252
                    + "console will not work correctly.");
            return;
        }
        try {
253 254 255
            SAXReader saxReader = new SAXReader();
            Document doc = saxReader.read(in);
            coreModel = (Element)doc.selectSingleNode("/adminconsole");
Bill Lynch's avatar
Bill Lynch committed
256 257 258 259 260 261 262
        }
        catch (Exception e) {
            Log.error("Failure when parsing main admin-sidebar.xml file", e);
        }
        try {
            in.close();
        }
263 264 265
        catch (Exception ignored) {
            // Ignore.
        }
Bill Lynch's avatar
Bill Lynch committed
266 267 268

        // Load other admin-sidebar.xml files from the classpath
        ClassLoader[] classLoaders = getClassLoaders();
269
        for (ClassLoader classLoader : classLoaders) {
Bill Lynch's avatar
Bill Lynch committed
270 271
            URL url = null;
            try {
272 273
                if (classLoader != null) {
                    Enumeration e = classLoader.getResources("/META-INF/admin-sidebar.xml");
Bill Lynch's avatar
Bill Lynch committed
274
                    while (e.hasMoreElements()) {
275
                        url = (URL) e.nextElement();
Bill Lynch's avatar
Bill Lynch committed
276
                        try {
277
                            in = url.openStream();
278
                            addModel("admin", in);
279 280
                        }
                        finally {
281 282 283 284 285
                            try {
                                if (in != null) {
                                    in.close();
                                }
                            }
286 287 288
                            catch (Exception ignored) {
                                // Ignore.
                            }
Bill Lynch's avatar
Bill Lynch committed
289 290 291 292 293 294 295 296 297 298 299 300
                        }
                    }
                }
            }
            catch (Exception e) {
                String msg = "Failed to load admin-sidebar.xml";
                if (url != null) {
                    msg += " from resource: " + url.toString();
                }
                Log.warn(msg, e);
            }
        }
301
        rebuildModel();
Bill Lynch's avatar
Bill Lynch committed
302 303
    }

304 305 306
    /**
     * Rebuilds the generated model.
     */
307
    private static synchronized void rebuildModel() {
308 309 310 311 312
        Document doc = DocumentFactory.getInstance().createDocument();
        generatedModel = coreModel.createCopy();
        doc.add(generatedModel);

        // Add in all overrides.
313
        for (Element element : overrideModels.values()) {
314
            // See if global settings are overriden.
Matt Tucker's avatar
Matt Tucker committed
315
            Element appName = (Element)element.selectSingleNode("//adminconsole/global/appname");
316 317
            if (appName != null) {
                Element existingAppName = (Element)generatedModel.selectSingleNode(
Matt Tucker's avatar
Matt Tucker committed
318
                        "//adminconsole/global/appname");
319
                existingAppName.setText(appName.getText());
Matt Tucker's avatar
Matt Tucker committed
320 321 322
                if (appName.attributeValue("plugin") != null) {
                    existingAppName.addAttribute("plugin", appName.attributeValue("plugin"));
                }
323
            }
Matt Tucker's avatar
Matt Tucker committed
324
            Element appLogoImage = (Element)element.selectSingleNode("//adminconsole/global/logo-image");
325 326
            if (appLogoImage != null) {
                Element existingLogoImage = (Element)generatedModel.selectSingleNode(
Matt Tucker's avatar
Matt Tucker committed
327
                        "//adminconsole/global/logo-image");
328
                existingLogoImage.setText(appLogoImage.getText());
Matt Tucker's avatar
Matt Tucker committed
329 330 331
                if (appLogoImage.attributeValue("plugin") != null) {
                    existingLogoImage.addAttribute("plugin", appLogoImage.attributeValue("plugin"));
                }
332
            }
Matt Tucker's avatar
Matt Tucker committed
333 334
            Element appLoginImage = (Element)element.selectSingleNode("//adminconsole/global/login-image");
            if (appLoginImage != null) {
335
                Element existingLoginImage = (Element)generatedModel.selectSingleNode(
Matt Tucker's avatar
Matt Tucker committed
336
                        "//adminconsole/global/login-image");
337
                existingLoginImage.setText(appLoginImage.getText());
Matt Tucker's avatar
Matt Tucker committed
338 339 340
                if (appLoginImage.attributeValue("plugin") != null) {
                    existingLoginImage.addAttribute("plugin", appLoginImage.attributeValue("plugin"));
                }
341
            }
Matt Tucker's avatar
Matt Tucker committed
342 343
            Element appVersion = (Element)element.selectSingleNode("//adminconsole/global/version");
            if (appVersion != null) {
344
                Element existingVersion = (Element)generatedModel.selectSingleNode(
Matt Tucker's avatar
Matt Tucker committed
345
                        "//adminconsole/global/version");
Matt Tucker's avatar
Matt Tucker committed
346 347
                if (existingVersion != null) {
                    existingVersion.setText(appVersion.getText());
Matt Tucker's avatar
Matt Tucker committed
348 349 350
                    if (appVersion.attributeValue("plugin") != null) {
                        existingVersion.addAttribute("plugin", appVersion.attributeValue("plugin"));
                    }
Matt Tucker's avatar
Matt Tucker committed
351 352 353 354 355
                }
                else {
                    ((Element)generatedModel.selectSingleNode(
                            "//adminconsole/global")).add(appVersion.createCopy());
                }
356
            }
357
            // Tabs
358 359
            for (Object o : element.selectNodes("//tab")) {
                Element tab = (Element) o;
360 361 362 363
                String id = tab.attributeValue("id");
                Element existingTab = getElemnetByID(id);
                // Simple case, there is no existing tab with the same id.
                if (existingTab == null) {
364 365 366
                    // Make sure that the URL on the tab is set. If not, default to the
                    // url of the first item.
                    if (tab.attributeValue("url") == null) {
367
                        Element firstItem = (Element) tab.selectSingleNode(
Matt Tucker's avatar
Matt Tucker committed
368 369 370 371
                                "//item[@url]");
                        if (firstItem != null) {
                            tab.addAttribute("url", firstItem.attributeValue("url"));
                        }
372
                    }
373 374 375 376 377 378 379 380 381
                    generatedModel.add(tab.createCopy());
                }
                // More complex case -- a tab with the same id already exists.
                // In this case, we have to overrite only the difference between
                // the two elements.
                else {
                    overrideTab(existingTab, tab);
                }
            }
Bill Lynch's avatar
Bill Lynch committed
382
        }
383 384 385 386 387 388 389

        // Special case: show a link to Clearspace admin console if it is integrated with
        // Openfire.
        if (ClearspaceManager.isEnabled()) {
            Element clearspace = generatedModel.addElement("tab");
            clearspace.addAttribute("id", "tab-clearspace");
            clearspace.addAttribute("name", LocaleUtils.getLocalizedString("tab.tab-clearspace"));
390
            clearspace.addAttribute("url", "clearspace-status.jsp");
391 392
            clearspace.addAttribute("description", LocaleUtils.getLocalizedString("tab.tab-clearspace.descr"));
            Element sidebar = clearspace.addElement("sidebar");
393 394
            sidebar.addAttribute("id", "sidebar-clearspace");
            sidebar.addAttribute("name", LocaleUtils.getLocalizedString("sidebar.sidebar-clearspace"));
395

396 397 398 399 400 401 402 403 404 405 406 407
            Element statusItem = sidebar.addElement("item");
            statusItem.addAttribute("id", "clearspace-status");
            statusItem.addAttribute("name", LocaleUtils.getLocalizedString("sidebar.clearspace-status"));
            statusItem.addAttribute("url", "clearspace-status.jsp");
            statusItem.addAttribute("description", LocaleUtils.getLocalizedString("sidebar.clearspace-status.descr"));

            Element adminItem = sidebar.addElement("item");
            adminItem.addAttribute("id", "clearspace-admin");
            adminItem.addAttribute("name", LocaleUtils.getLocalizedString("sidebar.clearspace-admin"));
            adminItem.addAttribute("url", "clearspace-admin.jsp");
            adminItem.addAttribute("description", LocaleUtils.getLocalizedString("sidebar.clearspace-admin.descr"));

408
        }
409
    }
410

411 412
    private static void overrideTab(Element tab, Element overrideTab) {
        // Override name, url, description.
Matt Tucker's avatar
Matt Tucker committed
413 414 415 416 417 418 419 420 421
        if (overrideTab.attributeValue("name") != null) {
            tab.addAttribute("name", overrideTab.attributeValue("name"));
        }
        if (overrideTab.attributeValue("url") != null) {
            tab.addAttribute("url", overrideTab.attributeValue("url"));
        }
        if (overrideTab.attributeValue("description") != null) {
            tab.addAttribute("description", overrideTab.attributeValue("description"));
        }
Matt Tucker's avatar
Matt Tucker committed
422 423 424
        if (overrideTab.attributeValue("plugin") != null) {
            tab.addAttribute("plugin", overrideTab.attributeValue("plugin"));
        }
425 426 427 428 429 430 431 432 433 434 435 436 437 438
        // Override sidebar items.
        for (Iterator i=overrideTab.elementIterator(); i.hasNext(); ) {
            Element sidebar = (Element)i.next();
            String id = sidebar.attributeValue("id");
            Element existingSidebar = getElemnetByID(id);
            // Simple case, there is no existing sidebar with the same id.
            if (existingSidebar == null) {
                tab.add(sidebar.createCopy());
            }
            // More complex case -- a sidebar with the same id already exists.
            // In this case, we have to overrite only the difference between
            // the two elements.
            else {
                overrideSidebar(existingSidebar, sidebar);
Bill Lynch's avatar
Bill Lynch committed
439 440 441 442
            }
        }
    }

443 444
    private static void overrideSidebar(Element sidebar, Element overrideSidebar) {
        // Override name.
Matt Tucker's avatar
Matt Tucker committed
445 446 447
        if (overrideSidebar.attributeValue("name") != null) {
            sidebar.addAttribute("name", overrideSidebar.attributeValue("name"));
        }
Matt Tucker's avatar
Matt Tucker committed
448 449 450
        if (overrideSidebar.attributeValue("plugin") != null) {
            sidebar.addAttribute("plugin", overrideSidebar.attributeValue("plugin"));
        }
451 452 453
        // Override entries.
        for (Iterator i=overrideSidebar.elementIterator(); i.hasNext(); ) {
            Element entry = (Element)i.next();
454
            String id = entry.attributeValue("id");
455 456 457 458 459
            Element existingEntry = getElemnetByID(id);
            // Simple case, there is no existing sidebar with the same id.
            if (existingEntry == null) {
                sidebar.add(entry.createCopy());
            }
Matt Tucker's avatar
Matt Tucker committed
460
            // More complex case -- an entry with the same id already exists.
461 462 463 464 465 466 467
            // In this case, we have to overrite only the difference between
            // the two elements.
            else {
                overrideEntry(existingEntry, entry);
            }
        }
    }
468

469 470
    private static void overrideEntry(Element entry, Element overrideEntry) {
        // Override name.
Matt Tucker's avatar
Matt Tucker committed
471 472 473 474 475 476 477 478 479
        if (overrideEntry.attributeValue("name") != null) {
            entry.addAttribute("name", overrideEntry.attributeValue("name"));
        }
        if (overrideEntry.attributeValue("url") != null) {
            entry.addAttribute("url", overrideEntry.attributeValue("url"));
        }
        if (overrideEntry.attributeValue("description") != null) {
            entry.addAttribute("description", overrideEntry.attributeValue("description"));
        }
Matt Tucker's avatar
Matt Tucker committed
480 481 482
        if (overrideEntry.attributeValue("plugin") != null) {
            entry.addAttribute("plugin", overrideEntry.attributeValue("plugin"));
        }
483 484 485 486 487 488 489 490 491 492 493 494 495 496
        // Override any sidebars contained in the entry.
        for (Iterator i=overrideEntry.elementIterator(); i.hasNext(); ) {
            Element sidebar = (Element)i.next();
            String id = sidebar.attributeValue("id");
            Element existingSidebar = getElemnetByID(id);
            // Simple case, there is no existing sidebar with the same id.
            if (existingSidebar == null) {
                entry.add(sidebar.createCopy());
            }
            // More complex case -- a sidebar with the same id already exists.
            // In this case, we have to overrite only the difference between
            // the two elements.
            else {
                overrideSidebar(existingSidebar, sidebar);
497 498
            }
        }
Bill Lynch's avatar
Bill Lynch committed
499 500 501 502
    }

    /**
     * Returns an array of class loaders to load resources from.
503 504
     *
     * @return an array of class loaders to load resources from.
Bill Lynch's avatar
Bill Lynch committed
505 506 507 508 509 510 511 512
     */
    private static ClassLoader[] getClassLoaders() {
        ClassLoader[] classLoaders = new ClassLoader[3];
        classLoaders[0] = AdminConsole.class.getClass().getClassLoader();
        classLoaders[1] = Thread.currentThread().getContextClassLoader();
        classLoaders[2] = ClassLoader.getSystemClassLoader();
        return classLoaders;
    }
513
}