AdminConsole.java 14.1 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 14 15
 */

package org.jivesoftware.admin;

import org.jivesoftware.util.ClassUtils;
import org.jivesoftware.util.Log;
16
import org.jivesoftware.wildfire.XMPPServer;
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 45 46 47 48 49 50
        load();
    }

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

Bill Lynch's avatar
Bill Lynch committed
51 52 53
    /**
     * Adds XML stream to the tabs/sidebar model.
     *
54
     * @param name the name.
Bill Lynch's avatar
Bill Lynch committed
55 56 57
     * @param in the XML input stream.
     * @throws Exception if an error occurs when parsing the XML or adding it to the model.
     */
58
    public static void addModel(String name, InputStream in) throws Exception {
Matt Tucker's avatar
Matt Tucker committed
59 60
        SAXReader saxReader = new SAXReader();
        Document doc = saxReader.read(in);
61
        addModel(name, (Element)doc.selectSingleNode("/adminconsole"));
Matt Tucker's avatar
Matt Tucker committed
62 63 64 65 66
    }

    /**
     * Adds an &lt;adminconsole&gt; Element to the tabs/sidebar model.
     *
67
     * @param name the name.
Matt Tucker's avatar
Matt Tucker committed
68 69 70
     * @param element the Element
     * @throws Exception if an error occurs.
     */
71 72 73 74 75 76 77 78 79 80 81 82
    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);
83
        rebuildModel();
Bill Lynch's avatar
Bill Lynch committed
84 85
    }

Bill Lynch's avatar
Bill Lynch committed
86 87 88 89
    /**
     * Returns the name of the application.
     */
    public static String getAppName() {
Matt Tucker's avatar
Matt Tucker committed
90
        Element appName = (Element)generatedModel.selectSingleNode("//adminconsole/global/appname");
91 92 93 94 95 96
        if (appName != null) {
            return appName.getText();
        }
        else {
            return null;
        }
Bill Lynch's avatar
Bill Lynch committed
97 98 99
    }

    /**
100 101 102
     * Returns the URL of the main logo image for the admin console.
     *
     * @return the logo image.
Bill Lynch's avatar
Bill Lynch committed
103 104
     */
    public static String getLogoImage() {
105
        Element globalLogoImage = (Element)generatedModel.selectSingleNode(
Matt Tucker's avatar
Matt Tucker committed
106
                "//adminconsole/global/logo-image");
107 108
        if (globalLogoImage != null) {
            return globalLogoImage.getText();
109
        }
110
        else {
Bill Lynch's avatar
Bill Lynch committed
111 112 113 114
            return null;
        }
    }

115 116 117 118 119 120 121
    /**
     * Returns the URL of the login image for the admin console.
     *
     * @return the login image.
     */
    public static String getLoginLogoImage() {
        Element globalLoginLogoImage = (Element)generatedModel.selectSingleNode(
Matt Tucker's avatar
Matt Tucker committed
122
                "//adminconsole/global/login-image");
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
        if (globalLoginLogoImage != null) {
            return globalLoginLogoImage.getText();
        }
        else {
            return null;
        }
    }

    /**
     * Returns the version string displayed in the admin console.
     *
     * @return the version string.
     */
    public static String getVersionString() {
        Element globalVersion = (Element)generatedModel.selectSingleNode(
Matt Tucker's avatar
Matt Tucker committed
138
                "//adminconsole/global/version");
139 140 141 142
        if (globalVersion != null) {
            return globalVersion.getText();
        }
        else {
143
            // Default to the Wildfire version if none has been provided via XML.
144
            XMPPServer xmppServer = XMPPServer.getInstance();
145
            return xmppServer.getServerInfo().getVersion().getVersionString();
146 147 148
        }
    }

Bill Lynch's avatar
Bill Lynch committed
149
    /**
150
     * Returns the model. The model should be considered read-only.
Bill Lynch's avatar
Bill Lynch committed
151
     *
152
     * @return the model.
Bill Lynch's avatar
Bill Lynch committed
153
     */
154 155
    public static Element getModel() {
        return generatedModel;
Bill Lynch's avatar
Bill Lynch committed
156 157 158
    }

    /**
159 160 161 162 163
     * 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
164
     */
165
    public static Element getElemnetByID(String id) {
Matt Tucker's avatar
Matt Tucker committed
166
        return (Element)generatedModel.selectSingleNode("//*[@id='" + id + "']");
Bill Lynch's avatar
Bill Lynch committed
167 168 169
    }

    private static void load() {
170
        // Load the core model as the admin-sidebar.xml file from the classpath.
Bill Lynch's avatar
Bill Lynch committed
171 172
        InputStream in = ClassUtils.getResourceAsStream("/admin-sidebar.xml");
        if (in == null) {
173
            Log.error("Failed to load admin-sidebar.xml file from Wildfire classes - admin "
Bill Lynch's avatar
Bill Lynch committed
174 175 176 177
                    + "console will not work correctly.");
            return;
        }
        try {
178 179 180
            SAXReader saxReader = new SAXReader();
            Document doc = saxReader.read(in);
            coreModel = (Element)doc.selectSingleNode("/adminconsole");
Bill Lynch's avatar
Bill Lynch committed
181 182 183 184 185 186 187 188 189 190 191 192 193 194 195
        }
        catch (Exception e) {
            Log.error("Failure when parsing main admin-sidebar.xml file", e);
        }
        try {
            in.close();
        }
        catch (Exception ignored) {}

        // 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
196
                    Enumeration e = classLoaders[i].getResources("/META-INF/admin-sidebar.xml");
Bill Lynch's avatar
Bill Lynch committed
197 198 199
                    while (e.hasMoreElements()) {
                        url = (URL)e.nextElement();
                        try {
200
                            in = url.openStream();
201
                            addModel("admin", in);
202 203 204 205
                        }
                        finally {
                            try { if (in != null) { in.close(); } }
                            catch (Exception ignored) {}
Bill Lynch's avatar
Bill Lynch committed
206 207 208 209 210 211 212 213 214 215 216 217
                        }
                    }
                }
            }
            catch (Exception e) {
                String msg = "Failed to load admin-sidebar.xml";
                if (url != null) {
                    msg += " from resource: " + url.toString();
                }
                Log.warn(msg, e);
            }
        }
218
        rebuildModel();
Bill Lynch's avatar
Bill Lynch committed
219 220
    }

221 222 223 224 225 226 227 228 229
    /**
     * Rebuilds the generated model.
     */
    private static void rebuildModel() {
        Document doc = DocumentFactory.getInstance().createDocument();
        generatedModel = coreModel.createCopy();
        doc.add(generatedModel);

        // Add in all overrides.
230
        for (Element element : overrideModels.values()) {
231
            // See if global settings are overriden.
Matt Tucker's avatar
Matt Tucker committed
232
            Element appName = (Element)element.selectSingleNode("//adminconsole/global/appname");
233 234
            if (appName != null) {
                Element existingAppName = (Element)generatedModel.selectSingleNode(
Matt Tucker's avatar
Matt Tucker committed
235
                        "//adminconsole/global/appname");
236 237
                existingAppName.setText(appName.getText());
            }
Matt Tucker's avatar
Matt Tucker committed
238
            Element appLogoImage = (Element)element.selectSingleNode("//adminconsole/global/logo-image");
239 240
            if (appLogoImage != null) {
                Element existingLogoImage = (Element)generatedModel.selectSingleNode(
Matt Tucker's avatar
Matt Tucker committed
241
                        "//adminconsole/global/logo-image");
242 243
                existingLogoImage.setText(appLogoImage.getText());
            }
Matt Tucker's avatar
Matt Tucker committed
244 245
            Element appLoginImage = (Element)element.selectSingleNode("//adminconsole/global/login-image");
            if (appLoginImage != null) {
246
                Element existingLoginImage = (Element)generatedModel.selectSingleNode(
Matt Tucker's avatar
Matt Tucker committed
247
                        "//adminconsole/global/login-image");
248 249
                existingLoginImage.setText(appLoginImage.getText());
            }
Matt Tucker's avatar
Matt Tucker committed
250 251
            Element appVersion = (Element)element.selectSingleNode("//adminconsole/global/version");
            if (appVersion != null) {
252
                Element existingVersion = (Element)generatedModel.selectSingleNode(
Matt Tucker's avatar
Matt Tucker committed
253
                        "//adminconsole/global/version");
Matt Tucker's avatar
Matt Tucker committed
254 255 256 257 258 259 260
                if (existingVersion != null) {
                    existingVersion.setText(appVersion.getText());
                }
                else {
                    ((Element)generatedModel.selectSingleNode(
                            "//adminconsole/global")).add(appVersion.createCopy());
                }
261
            }
262 263 264 265 266 267 268
            // 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) {
269 270 271
                    // 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
272 273 274 275 276
                        Element firstItem = (Element)tab.selectSingleNode(
                                "//item[@url]");
                        if (firstItem != null) {
                            tab.addAttribute("url", firstItem.attributeValue("url"));
                        }
277
                    }
278 279 280 281 282 283 284 285 286
                    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
287
        }
288
    }
289

290 291
    private static void overrideTab(Element tab, Element overrideTab) {
        // Override name, url, description.
Matt Tucker's avatar
Matt Tucker committed
292 293 294 295 296 297 298 299 300
        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"));
        }
301 302 303 304 305 306 307 308 309 310 311 312 313 314
        // 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
315 316 317 318
            }
        }
    }

319 320
    private static void overrideSidebar(Element sidebar, Element overrideSidebar) {
        // Override name.
Matt Tucker's avatar
Matt Tucker committed
321 322 323
        if (overrideSidebar.attributeValue("name") != null) {
            sidebar.addAttribute("name", overrideSidebar.attributeValue("name"));
        }
324 325 326
        // Override entries.
        for (Iterator i=overrideSidebar.elementIterator(); i.hasNext(); ) {
            Element entry = (Element)i.next();
327
            String id = entry.attributeValue("id");
328 329 330 331 332
            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
333
            // More complex case -- an entry with the same id already exists.
334 335 336 337 338 339 340
            // In this case, we have to overrite only the difference between
            // the two elements.
            else {
                overrideEntry(existingEntry, entry);
            }
        }
    }
341

342 343
    private static void overrideEntry(Element entry, Element overrideEntry) {
        // Override name.
Matt Tucker's avatar
Matt Tucker committed
344 345 346 347 348 349 350 351 352
        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"));
        }
353 354 355 356 357 358 359 360 361 362 363 364 365 366
        // 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);
367 368
            }
        }
Bill Lynch's avatar
Bill Lynch committed
369 370 371 372 373 374 375 376 377 378 379 380
    }

    /**
     * Returns an array of class loaders to load resources from.
     */
    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;
    }
381
}