AdminConsole.java 19.4 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$
 *
Bill Lynch's avatar
Bill Lynch committed
6
 * Copyright (C) 2004 Jive Software. All rights reserved.
Bill Lynch's avatar
Bill Lynch committed
7
 *
Bill Lynch's avatar
Bill Lynch committed
8 9
 * This software is published under the terms of the GNU Public License (GPL),
 * a copy of which is included in this distribution.
Bill Lynch's avatar
Bill Lynch committed
10 11 12 13
 */

package org.jivesoftware.admin;

14
import org.jivesoftware.util.*;
15 16
import org.jivesoftware.openfire.XMPPServer;
import org.jivesoftware.openfire.container.PluginManager;
17 18
import org.dom4j.Document;
import org.dom4j.Element;
19
import org.dom4j.DocumentFactory;
Matt Tucker's avatar
Matt Tucker committed
20
import org.dom4j.io.SAXReader;
Bill Lynch's avatar
Bill Lynch committed
21 22 23 24 25 26

import java.util.*;
import java.io.InputStream;
import java.net.URL;

/**
27
 * A model for admin tab and sidebar info. This class loads in XML definitions of the
28
 * data and produces an in-memory model.<p>
Bill Lynch's avatar
Bill Lynch committed
29
 *
30 31 32 33 34
 * 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
35 36 37
 */
public class AdminConsole {

38
    private static Element coreModel;
39
    private static Map<String,Element> overrideModels;
40
    private static Element generatedModel;
Bill Lynch's avatar
Bill Lynch committed
41 42

    static {
Matt Tucker's avatar
Matt Tucker committed
43
        overrideModels = new LinkedHashMap<String,Element>();
Bill Lynch's avatar
Bill Lynch committed
44
        load();
45 46 47 48 49 50 51 52

        // The admin console model has special logic to include an informational
        // Enterprise tab when the Enterprise plugin is not installed. A property
        // controls whether to show that tab. Listen for the property value changing
        // and rebuild the model when that happens.
        PropertyEventDispatcher.addListener(new PropertyEventListener() {

            public void propertySet(String property, Map params) {
Matt Tucker's avatar
Matt Tucker committed
53 54 55
                if ("enterpriseInfoEnabled".equals(property)) {
                    rebuildModel();
                }
56 57 58 59 60 61 62 63
            }

            public void propertyDeleted(String property, Map params) {
                if ("enterpriseInfoEnabled".equals(property)) {
                    rebuildModel();
                }
            }

Matt Tucker's avatar
Matt Tucker committed
64 65 66 67
            public void xmlPropertySet(String property, Map params) {
                // Do nothing
            }

68
            public void xmlPropertyDeleted(String property, Map params) {
Matt Tucker's avatar
Matt Tucker committed
69
                // Do nothing    
70 71
            }
        });
Bill Lynch's avatar
Bill Lynch committed
72 73 74 75 76 77
    }

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

Bill Lynch's avatar
Bill Lynch committed
78 79 80
    /**
     * Adds XML stream to the tabs/sidebar model.
     *
81
     * @param name the name.
Bill Lynch's avatar
Bill Lynch committed
82 83 84
     * @param in the XML input stream.
     * @throws Exception if an error occurs when parsing the XML or adding it to the model.
     */
85
    public static void addModel(String name, InputStream in) throws Exception {
Matt Tucker's avatar
Matt Tucker committed
86 87
        SAXReader saxReader = new SAXReader();
        Document doc = saxReader.read(in);
88
        addModel(name, (Element)doc.selectSingleNode("/adminconsole"));
Matt Tucker's avatar
Matt Tucker committed
89 90 91 92 93
    }

    /**
     * Adds an &lt;adminconsole&gt; Element to the tabs/sidebar model.
     *
94
     * @param name the name.
Matt Tucker's avatar
Matt Tucker committed
95 96 97
     * @param element the Element
     * @throws Exception if an error occurs.
     */
98 99 100 101 102 103 104 105 106 107 108 109
    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);
110
        rebuildModel();
Bill Lynch's avatar
Bill Lynch committed
111 112
    }

Bill Lynch's avatar
Bill Lynch committed
113 114
    /**
     * Returns the name of the application.
115 116
     *
     * @return the name of the application.
Bill Lynch's avatar
Bill Lynch committed
117
     */
118
    public static synchronized String getAppName() {
Matt Tucker's avatar
Matt Tucker committed
119
        Element appName = (Element)generatedModel.selectSingleNode("//adminconsole/global/appname");
120
        if (appName != null) {
Matt Tucker's avatar
Matt Tucker committed
121 122
            String pluginName = appName.attributeValue("plugin");
            return getAdminText(appName.getText(), pluginName);
123 124 125 126
        }
        else {
            return null;
        }
Bill Lynch's avatar
Bill Lynch committed
127 128 129
    }

    /**
130 131 132
     * Returns the URL of the main logo image for the admin console.
     *
     * @return the logo image.
Bill Lynch's avatar
Bill Lynch committed
133
     */
134
    public static synchronized String getLogoImage() {
135
        Element globalLogoImage = (Element)generatedModel.selectSingleNode(
Matt Tucker's avatar
Matt Tucker committed
136
                "//adminconsole/global/logo-image");
137
        if (globalLogoImage != null) {
Matt Tucker's avatar
Matt Tucker committed
138 139
            String pluginName = globalLogoImage.attributeValue("plugin");
            return getAdminText(globalLogoImage.getText(), pluginName);
140
        }
141
        else {
Bill Lynch's avatar
Bill Lynch committed
142 143 144 145
            return null;
        }
    }

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

    /**
     * Returns the version string displayed in the admin console.
     *
     * @return the version string.
     */
168
    public static synchronized String getVersionString() {
169
        Element globalVersion = (Element)generatedModel.selectSingleNode(
Matt Tucker's avatar
Matt Tucker committed
170
                "//adminconsole/global/version");
171
        if (globalVersion != null) {
Matt Tucker's avatar
Matt Tucker committed
172 173
            String pluginName = globalVersion.attributeValue("plugin");
            return getAdminText(globalVersion.getText(), pluginName);
174 175
        }
        else {
176
            // Default to the Openfire version if none has been provided via XML.
177
            XMPPServer xmppServer = XMPPServer.getInstance();
178
            return xmppServer.getServerInfo().getVersion().getVersionString();
179 180 181
        }
    }

Bill Lynch's avatar
Bill Lynch committed
182
    /**
183
     * Returns the model. The model should be considered read-only.
Bill Lynch's avatar
Bill Lynch committed
184
     *
185
     * @return the model.
Bill Lynch's avatar
Bill Lynch committed
186
     */
187
    public static synchronized Element getModel() {
188
        return generatedModel;
Bill Lynch's avatar
Bill Lynch committed
189 190 191
    }

    /**
192 193 194 195 196
     * 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
197
     */
198
    public static synchronized Element getElemnetByID(String id) {
Matt Tucker's avatar
Matt Tucker committed
199
        return (Element)generatedModel.selectSingleNode("//*[@id='" + id + "']");
Bill Lynch's avatar
Bill Lynch committed
200 201
    }

Matt Tucker's avatar
Matt Tucker committed
202 203 204 205 206 207 208 209 210
    /**
     * 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,
211
     *      or <tt>null</tt> if the standard Openfire resource bundle should be used.
Matt Tucker's avatar
Matt Tucker committed
212 213 214 215 216 217 218 219 220 221 222 223 224 225
     * @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
226
    private static void load() {
227
        // Load the core model as the admin-sidebar.xml file from the classpath.
Bill Lynch's avatar
Bill Lynch committed
228 229
        InputStream in = ClassUtils.getResourceAsStream("/admin-sidebar.xml");
        if (in == null) {
230
            Log.error("Failed to load admin-sidebar.xml file from Openfire classes - admin "
Bill Lynch's avatar
Bill Lynch committed
231 232 233 234
                    + "console will not work correctly.");
            return;
        }
        try {
235 236 237
            SAXReader saxReader = new SAXReader();
            Document doc = saxReader.read(in);
            coreModel = (Element)doc.selectSingleNode("/adminconsole");
Bill Lynch's avatar
Bill Lynch committed
238 239 240 241 242 243 244
        }
        catch (Exception e) {
            Log.error("Failure when parsing main admin-sidebar.xml file", e);
        }
        try {
            in.close();
        }
245 246 247
        catch (Exception ignored) {
            // Ignore.
        }
Bill Lynch's avatar
Bill Lynch committed
248 249 250 251 252 253 254

        // Load other admin-sidebar.xml files from the classpath
        ClassLoader[] classLoaders = getClassLoaders();
        for (int i=0; i<classLoaders.length; i++) {
            URL url = null;
            try {
                if (classLoaders[i] != null) {
Bill Lynch's avatar
Bill Lynch committed
255
                    Enumeration e = classLoaders[i].getResources("/META-INF/admin-sidebar.xml");
Bill Lynch's avatar
Bill Lynch committed
256 257 258
                    while (e.hasMoreElements()) {
                        url = (URL)e.nextElement();
                        try {
259
                            in = url.openStream();
260
                            addModel("admin", in);
261 262 263
                        }
                        finally {
                            try { if (in != null) { in.close(); } }
264 265 266
                            catch (Exception ignored) {
                                // Ignore.
                            }
Bill Lynch's avatar
Bill Lynch committed
267 268 269 270 271 272 273 274 275 276 277 278
                        }
                    }
                }
            }
            catch (Exception e) {
                String msg = "Failed to load admin-sidebar.xml";
                if (url != null) {
                    msg += " from resource: " + url.toString();
                }
                Log.warn(msg, e);
            }
        }
279
        rebuildModel();
Bill Lynch's avatar
Bill Lynch committed
280 281
    }

282 283 284
    /**
     * Rebuilds the generated model.
     */
285
    private static synchronized void rebuildModel() {
286 287 288 289 290
        Document doc = DocumentFactory.getInstance().createDocument();
        generatedModel = coreModel.createCopy();
        doc.add(generatedModel);

        // Add in all overrides.
291
        for (Element element : overrideModels.values()) {
292
            // See if global settings are overriden.
Matt Tucker's avatar
Matt Tucker committed
293
            Element appName = (Element)element.selectSingleNode("//adminconsole/global/appname");
294 295
            if (appName != null) {
                Element existingAppName = (Element)generatedModel.selectSingleNode(
Matt Tucker's avatar
Matt Tucker committed
296
                        "//adminconsole/global/appname");
297
                existingAppName.setText(appName.getText());
Matt Tucker's avatar
Matt Tucker committed
298 299 300
                if (appName.attributeValue("plugin") != null) {
                    existingAppName.addAttribute("plugin", appName.attributeValue("plugin"));
                }
301
            }
Matt Tucker's avatar
Matt Tucker committed
302
            Element appLogoImage = (Element)element.selectSingleNode("//adminconsole/global/logo-image");
303 304
            if (appLogoImage != null) {
                Element existingLogoImage = (Element)generatedModel.selectSingleNode(
Matt Tucker's avatar
Matt Tucker committed
305
                        "//adminconsole/global/logo-image");
306
                existingLogoImage.setText(appLogoImage.getText());
Matt Tucker's avatar
Matt Tucker committed
307 308 309
                if (appLogoImage.attributeValue("plugin") != null) {
                    existingLogoImage.addAttribute("plugin", appLogoImage.attributeValue("plugin"));
                }
310
            }
Matt Tucker's avatar
Matt Tucker committed
311 312
            Element appLoginImage = (Element)element.selectSingleNode("//adminconsole/global/login-image");
            if (appLoginImage != null) {
313
                Element existingLoginImage = (Element)generatedModel.selectSingleNode(
Matt Tucker's avatar
Matt Tucker committed
314
                        "//adminconsole/global/login-image");
315
                existingLoginImage.setText(appLoginImage.getText());
Matt Tucker's avatar
Matt Tucker committed
316 317 318
                if (appLoginImage.attributeValue("plugin") != null) {
                    existingLoginImage.addAttribute("plugin", appLoginImage.attributeValue("plugin"));
                }
319
            }
Matt Tucker's avatar
Matt Tucker committed
320 321
            Element appVersion = (Element)element.selectSingleNode("//adminconsole/global/version");
            if (appVersion != null) {
322
                Element existingVersion = (Element)generatedModel.selectSingleNode(
Matt Tucker's avatar
Matt Tucker committed
323
                        "//adminconsole/global/version");
Matt Tucker's avatar
Matt Tucker committed
324 325
                if (existingVersion != null) {
                    existingVersion.setText(appVersion.getText());
Matt Tucker's avatar
Matt Tucker committed
326 327 328
                    if (appVersion.attributeValue("plugin") != null) {
                        existingVersion.addAttribute("plugin", appVersion.attributeValue("plugin"));
                    }
Matt Tucker's avatar
Matt Tucker committed
329 330 331 332 333
                }
                else {
                    ((Element)generatedModel.selectSingleNode(
                            "//adminconsole/global")).add(appVersion.createCopy());
                }
334
            }
335 336 337 338 339 340 341
            // Tabs
            for (Iterator i=element.selectNodes("//tab").iterator(); i.hasNext(); ) {
                Element tab = (Element)i.next();
                String id = tab.attributeValue("id");
                Element existingTab = getElemnetByID(id);
                // Simple case, there is no existing tab with the same id.
                if (existingTab == null) {
342 343 344
                    // 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) {
Matt Tucker's avatar
Matt Tucker committed
345 346 347 348 349
                        Element firstItem = (Element)tab.selectSingleNode(
                                "//item[@url]");
                        if (firstItem != null) {
                            tab.addAttribute("url", firstItem.attributeValue("url"));
                        }
350
                    }
351 352 353 354 355 356 357 358 359
                    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
360
        }
361

362
        // Special case: show an informational tab about Openfire Enterprise if Enterprise
363
        // is not installed and if the user has not chosen to hide tab.
364 365 366 367 368
        PluginManager pluginManager = XMPPServer.getInstance().getPluginManager();
        boolean pluginExists = pluginManager != null && pluginManager.isPluginDownloaded(
                "enterprise.jar");
        if (!pluginExists && JiveGlobals.getBooleanProperty("enterpriseInfoEnabled", true)) {
            Element enterprise = generatedModel.addElement("tab");
369 370 371 372 373 374
            enterprise.addAttribute("id", "tab-enterprise");
            enterprise.addAttribute("name", "Enterprise");
            enterprise.addAttribute("url", "enterprise-info.jsp");
            enterprise.addAttribute("description", "Click for Enterprise information.");
            Element sidebar = enterprise.addElement("sidebar");
            sidebar.addAttribute("id", "sidebar-enterprise-info");
375
            sidebar.addAttribute("name", "Openfire Enterprise");
376 377 378 379
            Element item = sidebar.addElement("item");
            item.addAttribute("id", "enterprise-info");
            item.addAttribute("name", "Try Enterprise");
            item.addAttribute("url", "enterprise-info.jsp");
380
            item.addAttribute("description", "Openfire Enterprise overview inforation");
381
        }
382
    }
383

384 385
    private static void overrideTab(Element tab, Element overrideTab) {
        // Override name, url, description.
Matt Tucker's avatar
Matt Tucker committed
386 387 388 389 390 391 392 393 394
        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
395 396 397
        if (overrideTab.attributeValue("plugin") != null) {
            tab.addAttribute("plugin", overrideTab.attributeValue("plugin"));
        }
398 399 400 401 402 403 404 405 406 407 408 409 410 411
        // 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
412 413 414 415
            }
        }
    }

416 417
    private static void overrideSidebar(Element sidebar, Element overrideSidebar) {
        // Override name.
Matt Tucker's avatar
Matt Tucker committed
418 419 420
        if (overrideSidebar.attributeValue("name") != null) {
            sidebar.addAttribute("name", overrideSidebar.attributeValue("name"));
        }
Matt Tucker's avatar
Matt Tucker committed
421 422 423
        if (overrideSidebar.attributeValue("plugin") != null) {
            sidebar.addAttribute("plugin", overrideSidebar.attributeValue("plugin"));
        }
424 425 426
        // Override entries.
        for (Iterator i=overrideSidebar.elementIterator(); i.hasNext(); ) {
            Element entry = (Element)i.next();
427
            String id = entry.attributeValue("id");
428 429 430 431 432
            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
433
            // More complex case -- an entry with the same id already exists.
434 435 436 437 438 439 440
            // In this case, we have to overrite only the difference between
            // the two elements.
            else {
                overrideEntry(existingEntry, entry);
            }
        }
    }
441

442 443
    private static void overrideEntry(Element entry, Element overrideEntry) {
        // Override name.
Matt Tucker's avatar
Matt Tucker committed
444 445 446 447 448 449 450 451 452
        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
453 454 455
        if (overrideEntry.attributeValue("plugin") != null) {
            entry.addAttribute("plugin", overrideEntry.attributeValue("plugin"));
        }
456 457 458 459 460 461 462 463 464 465 466 467 468 469
        // 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);
470 471
            }
        }
Bill Lynch's avatar
Bill Lynch committed
472 473 474 475
    }

    /**
     * Returns an array of class loaders to load resources from.
476 477
     *
     * @return an array of class loaders to load resources from.
Bill Lynch's avatar
Bill Lynch committed
478 479 480 481 482 483 484 485
     */
    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;
    }
486
}