AdminConsole.java 17.9 KB
Newer Older
Bill Lynch's avatar
Bill Lynch committed
1
/**
2
 * Copyright (C) 2004-2008 Jive Software. All rights reserved.
Bill Lynch's avatar
Bill Lynch committed
3
 *
4 5 6 7 8 9 10 11 12 13 14
 * 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
15 16 17 18 19 20
 */

package org.jivesoftware.admin;

import java.io.InputStream;
import java.net.URL;
21 22 23 24
import java.util.Enumeration;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;
Bill Lynch's avatar
Bill Lynch committed
25

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

50 51
	private static final Logger Log = LoggerFactory.getLogger(AdminConsole.class);

52
    private static Element coreModel;
53
    private static Map<String,Element> overrideModels;
54
    private static Element generatedModel;
Bill Lynch's avatar
Bill Lynch committed
55 56

    static {
57
        overrideModels = new LinkedHashMap<>();
Bill Lynch's avatar
Bill Lynch committed
58 59 60 61 62
        load();
    }

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

Bill Lynch's avatar
Bill Lynch committed
64 65
    }

Bill Lynch's avatar
Bill Lynch committed
66 67 68
    /**
     * Adds XML stream to the tabs/sidebar model.
     *
69
     * @param name the name.
Bill Lynch's avatar
Bill Lynch committed
70 71 72
     * @param in the XML input stream.
     * @throws Exception if an error occurs when parsing the XML or adding it to the model.
     */
73
    public static void addModel(String name, InputStream in) throws Exception {
Matt Tucker's avatar
Matt Tucker committed
74 75
        SAXReader saxReader = new SAXReader();
        Document doc = saxReader.read(in);
76
        addModel(name, (Element)doc.selectSingleNode("/adminconsole"));
Matt Tucker's avatar
Matt Tucker committed
77 78 79 80 81
    }

    /**
     * Adds an &lt;adminconsole&gt; Element to the tabs/sidebar model.
     *
82
     * @param name the name.
Matt Tucker's avatar
Matt Tucker committed
83 84 85
     * @param element the Element
     * @throws Exception if an error occurs.
     */
86
    public static synchronized void addModel(String name, Element element) throws Exception {
87 88 89 90 91 92 93 94 95
        overrideModels.put(name, element);
        rebuildModel();
    }

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

Bill Lynch's avatar
Bill Lynch committed
101 102
    /**
     * Returns the name of the application.
103 104
     *
     * @return the name of the application.
Bill Lynch's avatar
Bill Lynch committed
105
     */
106
    public static synchronized String getAppName() {
Matt Tucker's avatar
Matt Tucker committed
107
        Element appName = (Element)generatedModel.selectSingleNode("//adminconsole/global/appname");
108
        if (appName != null) {
Matt Tucker's avatar
Matt Tucker committed
109 110
            String pluginName = appName.attributeValue("plugin");
            return getAdminText(appName.getText(), pluginName);
111 112 113 114
        }
        else {
            return null;
        }
Bill Lynch's avatar
Bill Lynch committed
115 116 117
    }

    /**
118 119 120
     * Returns the URL of the main logo image for the admin console.
     *
     * @return the logo image.
Bill Lynch's avatar
Bill Lynch committed
121
     */
122
    public static synchronized String getLogoImage() {
123
        Element globalLogoImage = (Element)generatedModel.selectSingleNode(
Matt Tucker's avatar
Matt Tucker committed
124
                "//adminconsole/global/logo-image");
125
        if (globalLogoImage != null) {
Matt Tucker's avatar
Matt Tucker committed
126 127
            String pluginName = globalLogoImage.attributeValue("plugin");
            return getAdminText(globalLogoImage.getText(), pluginName);
128
        }
129
        else {
Bill Lynch's avatar
Bill Lynch committed
130 131 132 133
            return null;
        }
    }

134 135 136 137 138
    /**
     * Returns the URL of the login image for the admin console.
     *
     * @return the login image.
     */
139
    public static synchronized String getLoginLogoImage() {
140
        Element globalLoginLogoImage = (Element)generatedModel.selectSingleNode(
Matt Tucker's avatar
Matt Tucker committed
141
                "//adminconsole/global/login-image");
142
        if (globalLoginLogoImage != null) {
Matt Tucker's avatar
Matt Tucker committed
143 144
            String pluginName = globalLoginLogoImage.attributeValue("plugin");
            return getAdminText(globalLoginLogoImage.getText(), pluginName);
145 146 147 148 149 150 151 152 153 154 155
        }
        else {
            return null;
        }
    }

    /**
     * Returns the version string displayed in the admin console.
     *
     * @return the version string.
     */
156
    public static synchronized String getVersionString() {
157
        Element globalVersion = (Element)generatedModel.selectSingleNode(
Matt Tucker's avatar
Matt Tucker committed
158
                "//adminconsole/global/version");
159
        if (globalVersion != null) {
Matt Tucker's avatar
Matt Tucker committed
160 161
            String pluginName = globalVersion.attributeValue("plugin");
            return getAdminText(globalVersion.getText(), pluginName);
162 163
        }
        else {
164
            // Default to the Openfire version if none has been provided via XML.
165
            XMPPServer xmppServer = XMPPServer.getInstance();
166
            return xmppServer.getServerInfo().getVersion().getVersionString();
167 168 169
        }
    }

Bill Lynch's avatar
Bill Lynch committed
170
    /**
171
     * Returns the model. The model should be considered read-only.
Bill Lynch's avatar
Bill Lynch committed
172
     *
173
     * @return the model.
Bill Lynch's avatar
Bill Lynch committed
174
     */
175
    public static synchronized Element getModel() {
176
        return generatedModel;
Bill Lynch's avatar
Bill Lynch committed
177 178 179
    }

    /**
180 181 182 183 184
     * 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
185
     */
186
    public static synchronized Element getElemnetByID(String id) {
Matt Tucker's avatar
Matt Tucker committed
187
        return (Element)generatedModel.selectSingleNode("//*[@id='" + id + "']");
Bill Lynch's avatar
Bill Lynch committed
188 189
    }

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

        // Load other admin-sidebar.xml files from the classpath
        ClassLoader[] classLoaders = getClassLoaders();
239
        for (ClassLoader classLoader : classLoaders) {
Bill Lynch's avatar
Bill Lynch committed
240 241
            URL url = null;
            try {
242 243
                if (classLoader != null) {
                    Enumeration e = classLoader.getResources("/META-INF/admin-sidebar.xml");
Bill Lynch's avatar
Bill Lynch committed
244
                    while (e.hasMoreElements()) {
245
                        url = (URL) e.nextElement();
Bill Lynch's avatar
Bill Lynch committed
246
                        try {
247
                            in = url.openStream();
248
                            addModel("admin", in);
249 250
                        }
                        finally {
251 252 253 254 255
                            try {
                                if (in != null) {
                                    in.close();
                                }
                            }
256 257 258
                            catch (Exception ignored) {
                                // Ignore.
                            }
Bill Lynch's avatar
Bill Lynch committed
259 260 261 262 263 264 265 266 267 268 269 270
                        }
                    }
                }
            }
            catch (Exception e) {
                String msg = "Failed to load admin-sidebar.xml";
                if (url != null) {
                    msg += " from resource: " + url.toString();
                }
                Log.warn(msg, e);
            }
        }
271
        rebuildModel();
Bill Lynch's avatar
Bill Lynch committed
272 273
    }

274 275 276
    /**
     * Rebuilds the generated model.
     */
277
    private static synchronized void rebuildModel() {
278 279 280 281 282
        Document doc = DocumentFactory.getInstance().createDocument();
        generatedModel = coreModel.createCopy();
        doc.add(generatedModel);

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

355 356
    private static void overrideTab(Element tab, Element overrideTab) {
        // Override name, url, description.
Matt Tucker's avatar
Matt Tucker committed
357 358 359 360 361 362 363 364 365
        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
366 367 368
        if (overrideTab.attributeValue("plugin") != null) {
            tab.addAttribute("plugin", overrideTab.attributeValue("plugin"));
        }
369 370 371 372 373 374 375 376 377 378 379 380 381 382
        // 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
383 384 385 386
            }
        }
    }

387 388
    private static void overrideSidebar(Element sidebar, Element overrideSidebar) {
        // Override name.
Matt Tucker's avatar
Matt Tucker committed
389 390 391
        if (overrideSidebar.attributeValue("name") != null) {
            sidebar.addAttribute("name", overrideSidebar.attributeValue("name"));
        }
Matt Tucker's avatar
Matt Tucker committed
392 393 394
        if (overrideSidebar.attributeValue("plugin") != null) {
            sidebar.addAttribute("plugin", overrideSidebar.attributeValue("plugin"));
        }
395 396 397
        // Override entries.
        for (Iterator i=overrideSidebar.elementIterator(); i.hasNext(); ) {
            Element entry = (Element)i.next();
398
            String id = entry.attributeValue("id");
399 400 401 402 403
            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
404
            // More complex case -- an entry with the same id already exists.
405 406 407 408 409 410 411
            // In this case, we have to overrite only the difference between
            // the two elements.
            else {
                overrideEntry(existingEntry, entry);
            }
        }
    }
412

413 414
    private static void overrideEntry(Element entry, Element overrideEntry) {
        // Override name.
Matt Tucker's avatar
Matt Tucker committed
415 416 417 418 419 420 421 422 423
        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
424 425 426
        if (overrideEntry.attributeValue("plugin") != null) {
            entry.addAttribute("plugin", overrideEntry.attributeValue("plugin"));
        }
427 428 429 430 431 432 433 434 435 436 437 438 439 440
        // 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);
441 442
            }
        }
Bill Lynch's avatar
Bill Lynch committed
443 444 445 446
    }

    /**
     * Returns an array of class loaders to load resources from.
447 448
     *
     * @return an array of class loaders to load resources from.
Bill Lynch's avatar
Bill Lynch committed
449 450 451 452 453 454 455 456
     */
    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;
    }
457
}