AdminConsole.java 16.2 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 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
 */

package org.jivesoftware.admin;

import org.jivesoftware.util.ClassUtils;
import org.jivesoftware.util.Log;
import org.jivesoftware.util.XMLProperties;

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

/**
 * <p>A model for admin tab and sidebar info. This class loads in xml definitions of the data and
 * produces an in-memory model. There is an internal class, {@link Item} which is the main part
 * of the model. Items hold info like name, id, url and description as well as an arbritrary number
 * of sub items. Based on this we can make a tree model of the data.</p>
 *
 * <p>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.</p>
 *
 * <p>Note: IDs in the XML file must be unique because an internal mapping is kept of IDs to
 * nodes.</p>
 */
public class AdminConsole {

    private static Collection items;
    private static Map idMap; // map of item ids -> item objs
Bill Lynch's avatar
Bill Lynch committed
41 42
    private static String appName;
    private static String logoImage;
Bill Lynch's avatar
Bill Lynch committed
43 44 45 46 47 48 49 50 51 52 53

    static {
        items = new ArrayList();
        idMap = new HashMap();
        load();
    }

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

Bill Lynch's avatar
Bill Lynch committed
54 55 56 57 58 59 60 61 62 63
    /**
     * Adds XML stream to the tabs/sidebar model.
     *
     * @param in the XML input stream.
     * @throws Exception if an error occurs when parsing the XML or adding it to the model.
     */
    public static void addXMLSource(InputStream in) throws Exception {
        addToModel(in);
    }

Bill Lynch's avatar
Bill Lynch committed
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
    /**
     * Returns the name of the application.
     */
    public static String getAppName() {
        return appName;
    }

    /**
     * Returns the URL (relative or absolute) of the main logo image for the admin console.
     * @return
     */
    public static String getLogoImage() {
        return logoImage;
    }

Bill Lynch's avatar
Bill Lynch committed
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201
    /**
     * Returns all items starting from the root. Getting the iterator from this collection returns
     * all root items (should be used as tabs in the admin tool).
     *
     * @return a collection of all items - the root items are returned by calling the
     *      <tt>iterator()</tt> method.
     */
    public static Collection getItems() {
        return items;
    }

    /**
     * Returns an item given its ID or <tt>null</tt> if it can't be found.
     *
     * @param id the ID of the item.
     * @return an item given its ID or <tt>null</tt> if it can't be found.
     */
    public static Item getItem(String id) {
        return (Item)idMap.get(id);
    }

    /**
     * Returns the root item given a child item. In other words, a lookup is done on the ID for
     * the corresponding item - that item is assumed to be a leaf and this method returns the
     * root ancestor of it.
     *
     * @param id the ID of the child item.
     * @return the root ancestor of the specified child item.
     */
    public static Item getRootByChildID(String id) {
        if (id == null) {
            return null;
        }
        Item child = getItem(id);
        Item root = null;
        if (child != null) {
            Item parent = child.getParent();
            root = parent;
            while (parent != null) {
                parent = parent.getParent();
                if (parent != null) {
                    root = parent;
                }
            }
        }
        return root;
    }

    /**
     * Returns <tt>true</tt> if the given item is a sub-menu item.
     *
     * @param item the item to test.
     * @return <tt>true</tt> if the given item is a sub-menu item, <tt>false</tt> otherwise.
     */
    public static boolean isSubMenItem(Item item) {
        int parentCount = 0;
        Item parent = item.getParent();
        while (parent != null) {
            parentCount++;
            parent = parent.getParent();
        }
        return parentCount >= 3;
    }

    /**
     * Returns the ID of the page ID associated with this sub page ID.
     * @param subPageID the subPageID to use to look up the page ID.
     * @return the associated pageID or <tt>null</tt> if it can't be found.
     */
    public static String lookupPageID(String subPageID) {
        String pageID = null;
        Item item = getItem(subPageID);
        if (item != null) {
            Item parent = item.getParent();
            while (parent.getId() == null) {
                parent = parent.getParent();
            }
            pageID = parent.getId();
        }
        return pageID;
    }

    /**
     * A simple class to model an item. Each item has attributes used by the admin console to
     * display it like ID, name, URL and description. Also, from each item you can get its parent
     * (because an Item goes in a tree structure) and any children items it has.
     */
    public static class Item {

        private String id;
        private String name;
        private String description;
        private String url;
        private boolean active;
        private Collection items;
        private Item parent;

        /**
         * Creates a new item given its main attributes.
         */
        public Item(String id, String name, String description, String url) {
            this.id = id;
            this.name = name;
            this.description = description;
            this.url = url;
            init();
        }

        /**
         * Creates a new item given its main attributes and the parent item (this helps set up
         * the tree structure).
         */
        public Item(String id, String name, String description, String url, Item parent) {
            this.id = id;
            this.name = name;
            this.description = description;
            this.url = url;
            this.parent = parent;
            init();
        }

        private void init() {
            items = Collections.synchronizedList(new ArrayList());
Bill Lynch's avatar
Bill Lynch committed
202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217
//            items = Collections.synchronizedList(new ArrayList(){
//                public boolean add(Object obj) {
//                    Item item = (Item)obj;
//                    if (contains(item)) {
//                        Item i = (Item)get(indexOf(item));
//                        i.setName(item.getName());
//                        i.setDescription(item.getDescription());
//                        i.setUrl(item.getUrl());
//                        return true;
//                    }
//                    else {
//                        super.add(item);
//                        return true;
//                    }
//                };
//            });
Bill Lynch's avatar
Bill Lynch committed
218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236
            if (id != null && !"".equals(id.trim())) {
                idMap.put(id, this);
            }
        }

        /**
         * Returns the ID of the item.
         */
        public String getId() {
            return id;
        }

        /**
         * Returns the name of the item - this is the display name.
         */
        public String getName() {
            return name;
        }

Bill Lynch's avatar
Bill Lynch committed
237 238 239 240 241 242 243
        /**
         * Sets the name.
         */
        void setName(String name) {
            this.name = name;
        }

Bill Lynch's avatar
Bill Lynch committed
244 245 246 247 248 249 250
        /**
         * Returns the description of the item.
         */
        public String getDescription() {
            return description;
        }

Bill Lynch's avatar
Bill Lynch committed
251 252 253 254 255 256 257
        /**
         * Sets the description.
         */
        void setDescription(String description) {
            this.description = description;
        }

Bill Lynch's avatar
Bill Lynch committed
258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335
        /**
         * Returns the URL for this item.
         */
        public String getUrl() {
            return url;
        }

        /**
         * Sets the URL for this item.
         */
        public void setUrl(String url) {
            this.url = url;
        }

        /**
         * Returns true if this items is active - in the admin console this would mean it's selected.
         */
        public boolean isActive() {
            return active;
        }

        /**
         * Sets the item as active - in the admin console this would mean it's selected.
         */
        public void setActive(boolean active) {
            this.active = active;
        }

        /**
         * Returns the parent item or <tt>null</tt> if this is a root item.
         */
        public Item getParent() {
            return parent;
        }

        /**
         * Sets the parent item.
         */
        public void setParent(Item parent) {
            this.parent = parent;
        }

        /**
         * Returns the items as a collection. Use the Collection API to get/set/remove items.
         */
        public Collection getItems() {
            return items;
        }

        public boolean equals(Object o) {
            if (this == o) {
                return true;
            }
            if (o == null) {
                return false;
            }
            if (!(o instanceof Item)) {
                return false;
            }
            Item i = (Item) o;
            if (!id.equals(i.id)) {
                return false;
            }
            return true;
        }

        /**
         * Returns the ID of the item.
         */
        public String toString() {
            return id;
        }
    }

    private static void load() {
        // Load the admin-sidebar.xml file from the jiveforums.jar file:
        InputStream in = ClassUtils.getResourceAsStream("/admin-sidebar.xml");
        if (in == null) {
336
            Log.error("Failed to load admin-sidebar.xml file from Jive Messenger classes - admin "
Bill Lynch's avatar
Bill Lynch committed
337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356
                    + "console will not work correctly.");
            return;
        }
        try {
            addToModel(in);
        }
        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
357
                    Enumeration e = classLoaders[i].getResources("/META-INF/admin-sidebar.xml");
Bill Lynch's avatar
Bill Lynch committed
358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380
                    while (e.hasMoreElements()) {
                        url = (URL)e.nextElement();
                        in = url.openStream();
                        addToModel(in);
                        try {
                            in.close();
                        }
                        catch (Exception ignored) {}
                    }
                }
            }
            catch (Exception e) {
                String msg = "Failed to load admin-sidebar.xml";
                if (url != null) {
                    msg += " from resource: " + url.toString();
                }
                Log.warn(msg, e);
            }
        }
    }

    private static void addToModel(InputStream in) throws Exception {
        // Build an XMLPropertiesTest object from the input stream:
Bill Lynch's avatar
Bill Lynch committed
381
        XMLProperties xml = new XMLProperties(in);
Bill Lynch's avatar
Bill Lynch committed
382 383 384 385 386 387 388
        // Set any global properties
        if (xml.getProperty("global.appname") != null) {
            appName = xml.getProperty("global.appname");
        }
        if (xml.getProperty("global.logo-image") != null) {
            logoImage = xml.getProperty("global.logo-image");
        }
Bill Lynch's avatar
Bill Lynch committed
389
        // Get all children of the 'tabs' element - should be 'tab' items:
Bill Lynch's avatar
Bill Lynch committed
390
        String[] tabs = xml.getChildrenProperties("tabs");
Bill Lynch's avatar
Bill Lynch committed
391 392
        for (int i=0; i<tabs.length; i++) {
            String propName = "tabs." + tabs[i];
Bill Lynch's avatar
Bill Lynch committed
393 394 395 396
            // Create a new top level item with data from the xml file:
            String id = xml.getProperty(propName + ".id");
            String name = xml.getProperty(propName + ".name");
            String description = xml.getProperty(propName + ".description");
Bill Lynch's avatar
Bill Lynch committed
397 398 399 400 401
            Item item = new Item(id, name, description, null);
            // Add that item to the item collection
            getItems().add(item);
            // Delve down into this item's sidebars - build up a model of these then add into
            // the item above.
Bill Lynch's avatar
Bill Lynch committed
402
            String[] sidebars = xml.getChildrenProperties(propName + ".sidebars");
Bill Lynch's avatar
Bill Lynch committed
403 404
            for (int j=0; j<sidebars.length; j++) {
                String sidebarName = propName + ".sidebars." + sidebars[j];
Bill Lynch's avatar
Bill Lynch committed
405
                name = xml.getProperty(sidebarName + ".name");
Bill Lynch's avatar
Bill Lynch committed
406 407 408 409
                // Create a new item, set its name
                Item subItem = new Item(null, name, null, null);
                // Now iterate down another level, get the items for this item - this will be the
                // specific links on the sidebar
Bill Lynch's avatar
Bill Lynch committed
410
                String[] subitems = xml.getChildrenProperties(sidebarName + ".items");
Bill Lynch's avatar
Bill Lynch committed
411 412 413
                for (int k=0; k<subitems.length; k++) {
                    String subitemName = sidebarName + ".items." + subitems[k];
                    // Get the id, name, descr and url attributes:
Bill Lynch's avatar
Bill Lynch committed
414 415 416 417
                    String subID = xml.getProperty(subitemName + ".id");
                    String subName = xml.getProperty(subitemName + ".name");
                    String subDescr = xml.getProperty(subitemName + ".description");
                    String subURL = xml.getProperty(subitemName + ".url");
Bill Lynch's avatar
Bill Lynch committed
418 419 420 421
                    // Build an item with this, add it to the subItem we made above
                    Item kItem = new Item(subID, subName, subDescr, subURL, subItem);
                    subItem.getItems().add(kItem);
                    // Build any sub-sub menus:
Bill Lynch's avatar
Bill Lynch committed
422
                    subAddtoModel(subitemName, xml, kItem);
Bill Lynch's avatar
Bill Lynch committed
423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471
                    // If this is the first item, set the root menu item's URL as this URL:
                    if (j==0 && k == 0) {
                        item.setUrl(subURL);
                    }
                }
                // Add the subItem to the item created above
                subItem.setParent(item);
                item.getItems().add(subItem);
            }
        }
    }

    private static void subAddtoModel(String path, XMLProperties props, Item parent) {
        String propName = path + ".subsidebars";
        String[] subsidebars = props.getChildrenProperties(propName);
        for (int i=0; i<subsidebars.length; i++) {
            String child = propName + "." + subsidebars[i];
            String[] subsidebarchildren = props.getChildrenProperties(child);
            for (int j=0; j<subsidebarchildren.length; j++) {
                String root = propName  + ".subsidebar" + i + "." + subsidebarchildren[j];
                String name = props.getProperty(root + ".name");
                Item header = new Item(null, name, null, null, parent);
                // Get the children of this
                String subPath = root + ".items";
                String[] allitems = props.getChildrenProperties(subPath);
                for (int k=0; k<allitems.length; k++) {
                    String subName = subPath + "." + allitems[k];
                    String itemID = props.getProperty(subName + ".id");
                    String itemName = props.getProperty(subName + ".name");
                    String itemDescr = props.getProperty(subName + ".description");
                    String itemURL = props.getProperty(subName + ".url");
                    Item si = new Item(itemID, itemName, itemDescr, itemURL, header);
                    header.getItems().add(si);
                }
                parent.getItems().add(header);
            }
        }
    }

    /**
     * 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;
    }
Bill Lynch's avatar
Bill Lynch committed
472 473 474 475 476 477

    private static void clear() {
        items = new ArrayList();
        idMap = new HashMap();
        load();
    }
Bill Lynch's avatar
Bill Lynch committed
478
}