1 11 12 package org.jivesoftware.admin; 13 14 import org.jivesoftware.util.ClassUtils; 15 import org.jivesoftware.util.Log; 16 import org.jivesoftware.messenger.XMPPServer; 17 import org.dom4j.Document; 18 import org.dom4j.Element; 19 import org.dom4j.DocumentFactory; 20 import org.dom4j.io.SAXReader; 21 22 import java.util.*; 23 import java.io.InputStream ; 24 import java.net.URL ; 25 26 36 public class AdminConsole { 37 38 private static Element coreModel; 39 private static Map<String ,Element> overrideModels; 40 private static Element generatedModel; 41 42 static { 43 init(); 44 } 45 46 private static void init() { 47 overrideModels = new LinkedHashMap<String ,Element>(); 48 load(); 49 } 50 51 52 private AdminConsole() { 53 } 54 55 62 public static void addModel(String name, InputStream in) throws Exception { 63 SAXReader saxReader = new SAXReader(); 64 Document doc = saxReader.read(in); 65 addModel(name, (Element)doc.selectSingleNode("/adminconsole")); 66 } 67 68 75 public static void addModel(String name, Element element) throws Exception { 76 overrideModels.put(name, element); 77 rebuildModel(); 78 } 79 80 85 public static void removeModel(String name) { 86 overrideModels.remove(name); 87 rebuildModel(); 88 } 89 90 93 public static String getAppName() { 94 Element appName = (Element)generatedModel.selectSingleNode("//adminconsole/global/appname"); 95 if (appName != null) { 96 return appName.getText(); 97 } 98 else { 99 return null; 100 } 101 } 102 103 108 public static String getLogoImage() { 109 Element globalLogoImage = (Element)generatedModel.selectSingleNode( 110 "//adminconsole/global/logo-image"); 111 if (globalLogoImage != null) { 112 return globalLogoImage.getText(); 113 } 114 else { 115 return null; 116 } 117 } 118 119 124 public static String getLoginLogoImage() { 125 Element globalLoginLogoImage = (Element)generatedModel.selectSingleNode( 126 "//adminconsole/global/login-image"); 127 if (globalLoginLogoImage != null) { 128 return globalLoginLogoImage.getText(); 129 } 130 else { 131 return null; 132 } 133 } 134 135 140 public static String getVersionString() { 141 Element globalVersion = (Element)generatedModel.selectSingleNode( 142 "//adminconsole/global/version"); 143 if (globalVersion != null) { 144 return globalVersion.getText(); 145 } 146 else { 147 XMPPServer xmppServer = XMPPServer.getInstance(); 149 return xmppServer.getServerInfo().getVersion().getVersionString(); 150 } 151 } 152 153 158 public static Element getModel() { 159 return generatedModel; 160 } 161 162 169 public static Element getElemnetByID(String id) { 170 return (Element)generatedModel.selectSingleNode("//*[@id='" + id + "']"); 171 } 172 173 private static void load() { 174 InputStream in = ClassUtils.getResourceAsStream("/admin-sidebar.xml"); 176 if (in == null) { 177 Log.error("Failed to load admin-sidebar.xml file from Jive Messenger classes - admin " 178 + "console will not work correctly."); 179 return; 180 } 181 try { 182 SAXReader saxReader = new SAXReader(); 183 Document doc = saxReader.read(in); 184 coreModel = (Element)doc.selectSingleNode("/adminconsole"); 185 } 186 catch (Exception e) { 187 Log.error("Failure when parsing main admin-sidebar.xml file", e); 188 } 189 try { 190 in.close(); 191 } 192 catch (Exception ignored) {} 193 194 ClassLoader [] classLoaders = getClassLoaders(); 196 for (int i=0; i<classLoaders.length; i++) { 197 URL url = null; 198 try { 199 if (classLoaders[i] != null) { 200 Enumeration e = classLoaders[i].getResources("/META-INF/admin-sidebar.xml"); 201 while (e.hasMoreElements()) { 202 url = (URL )e.nextElement(); 203 try { 204 in = url.openStream(); 205 addModel("admin", in); 206 } 207 finally { 208 try { if (in != null) { in.close(); } } 209 catch (Exception ignored) {} 210 } 211 } 212 } 213 } 214 catch (Exception e) { 215 String msg = "Failed to load admin-sidebar.xml"; 216 if (url != null) { 217 msg += " from resource: " + url.toString(); 218 } 219 Log.warn(msg, e); 220 } 221 } 222 rebuildModel(); 223 } 224 225 228 private static void rebuildModel() { 229 Document doc = DocumentFactory.getInstance().createDocument(); 230 generatedModel = coreModel.createCopy(); 231 doc.add(generatedModel); 232 233 for (Element element : overrideModels.values()) { 235 Element appName = (Element)element.selectSingleNode("//adminconsole/global/appname"); 237 if (appName != null) { 238 Element existingAppName = (Element)generatedModel.selectSingleNode( 239 "//adminconsole/global/appname"); 240 existingAppName.setText(appName.getText()); 241 } 242 Element appLogoImage = (Element)element.selectSingleNode("//adminconsole/global/logo-image"); 243 if (appLogoImage != null) { 244 Element existingLogoImage = (Element)generatedModel.selectSingleNode( 245 "//adminconsole/global/logo-image"); 246 existingLogoImage.setText(appLogoImage.getText()); 247 } 248 Element appLoginImage = (Element)element.selectSingleNode("//adminconsole/global/login-image"); 249 if (appLoginImage != null) { 250 Element existingLoginImage = (Element)generatedModel.selectSingleNode( 251 "//adminconsole/global/login-image"); 252 existingLoginImage.setText(appLoginImage.getText()); 253 } 254 Element appVersion = (Element)element.selectSingleNode("//adminconsole/global/version"); 255 if (appVersion != null) { 256 Element existingVersion = (Element)generatedModel.selectSingleNode( 257 "//adminconsole/global/version"); 258 if (existingVersion != null) { 259 existingVersion.setText(appVersion.getText()); 260 } 261 else { 262 ((Element)generatedModel.selectSingleNode( 263 "//adminconsole/global")).add(appVersion.createCopy()); 264 } 265 } 266 for (Iterator i=element.selectNodes("//tab").iterator(); i.hasNext(); ) { 268 Element tab = (Element)i.next(); 269 String id = tab.attributeValue("id"); 270 Element existingTab = getElemnetByID(id); 271 if (existingTab == null) { 273 if (tab.attributeValue("url") == null) { 276 Element firstItem = (Element)tab.selectSingleNode( 277 "//item[@url]"); 278 if (firstItem != null) { 279 tab.addAttribute("url", firstItem.attributeValue("url")); 280 } 281 } 282 generatedModel.add(tab.createCopy()); 283 } 284 else { 288 overrideTab(existingTab, tab); 289 } 290 } 291 } 292 } 293 294 private static void overrideTab(Element tab, Element overrideTab) { 295 if (overrideTab.attributeValue("name") != null) { 297 tab.addAttribute("name", overrideTab.attributeValue("name")); 298 } 299 if (overrideTab.attributeValue("url") != null) { 300 tab.addAttribute("url", overrideTab.attributeValue("url")); 301 } 302 if (overrideTab.attributeValue("description") != null) { 303 tab.addAttribute("description", overrideTab.attributeValue("description")); 304 } 305 for (Iterator i=overrideTab.elementIterator(); i.hasNext(); ) { 307 Element sidebar = (Element)i.next(); 308 String id = sidebar.attributeValue("id"); 309 Element existingSidebar = getElemnetByID(id); 310 if (existingSidebar == null) { 312 tab.add(sidebar.createCopy()); 313 } 314 else { 318 overrideSidebar(existingSidebar, sidebar); 319 } 320 } 321 } 322 323 private static void overrideSidebar(Element sidebar, Element overrideSidebar) { 324 if (overrideSidebar.attributeValue("name") != null) { 326 sidebar.addAttribute("name", overrideSidebar.attributeValue("name")); 327 } 328 for (Iterator i=overrideSidebar.elementIterator(); i.hasNext(); ) { 330 Element entry = (Element)i.next(); 331 String id = entry.attributeValue("id"); 332 Element existingEntry = getElemnetByID(id); 333 if (existingEntry == null) { 335 sidebar.add(entry.createCopy()); 336 } 337 else { 341 overrideEntry(existingEntry, entry); 342 } 343 } 344 } 345 346 private static void overrideEntry(Element entry, Element overrideEntry) { 347 if (overrideEntry.attributeValue("name") != null) { 349 entry.addAttribute("name", overrideEntry.attributeValue("name")); 350 } 351 if (overrideEntry.attributeValue("url") != null) { 352 entry.addAttribute("url", overrideEntry.attributeValue("url")); 353 } 354 if (overrideEntry.attributeValue("description") != null) { 355 entry.addAttribute("description", overrideEntry.attributeValue("description")); 356 } 357 for (Iterator i=overrideEntry.elementIterator(); i.hasNext(); ) { 359 Element sidebar = (Element)i.next(); 360 String id = sidebar.attributeValue("id"); 361 Element existingSidebar = getElemnetByID(id); 362 if (existingSidebar == null) { 364 entry.add(sidebar.createCopy()); 365 } 366 else { 370 overrideSidebar(existingSidebar, sidebar); 371 } 372 } 373 } 374 375 378 private static ClassLoader [] getClassLoaders() { 379 ClassLoader [] classLoaders = new ClassLoader [3]; 380 classLoaders[0] = AdminConsole.class.getClass().getClassLoader(); 381 classLoaders[1] = Thread.currentThread().getContextClassLoader(); 382 classLoaders[2] = ClassLoader.getSystemClassLoader(); 383 return classLoaders; 384 } 385 } | Popular Tags |