1 11 12 package org.jivesoftware.util; 13 14 import org.jivesoftware.messenger.*; 15 import org.jivesoftware.messenger.auth.AuthToken; 16 import org.jivesoftware.messenger.group.GroupManager; 17 import org.jivesoftware.messenger.muc.MultiUserChatServer; 18 import org.jivesoftware.messenger.roster.RosterManager; 19 import org.jivesoftware.messenger.user.User; 20 import org.jivesoftware.messenger.user.UserManager; 21 22 import java.io.*; 23 import java.net.URL ; 24 import java.util.LinkedHashMap ; 25 import java.util.Map ; 26 import java.util.StringTokenizer ; 27 28 31 public class WebManager extends WebBean { 32 33 private Map breadcrumbMap = new LinkedHashMap (); 34 private String title = ""; 35 private String sidebar = ""; 36 37 private int start = 0; 38 private int range = 15; 39 40 public WebManager() { 41 } 42 43 46 public AuthToken getAuthToken() { 47 return (AuthToken)session.getAttribute("jive.admin.authToken"); 48 } 49 50 53 public boolean isSetupMode() { 54 return getXMPPServer().isSetupMode(); 55 } 56 57 60 public XMPPServer getXMPPServer() { 61 final XMPPServer xmppServer = XMPPServer.getInstance(); 62 if (xmppServer == null) { 63 showServerDown(); 65 return null; 66 } 67 return xmppServer; 68 } 69 70 public UserManager getUserManager() { 71 return getXMPPServer().getUserManager(); 72 } 73 74 public GroupManager getGroupManager() { 75 return GroupManager.getInstance(); 76 } 77 78 public RosterManager getRosterManager() { 79 return getXMPPServer().getRosterManager(); 80 } 81 82 public PrivateStorage getPrivateStore() { 83 return getXMPPServer().getPrivateStorage(); 84 } 85 86 public PresenceManager getPresenceManager() { 87 return getXMPPServer().getPresenceManager(); 88 } 89 90 public SessionManager getSessionManager() { 91 return getXMPPServer().getSessionManager(); 92 } 93 94 public MultiUserChatServer getMultiUserChatServer() { 95 return getXMPPServer().getMultiUserChatServer(); 96 } 97 98 public XMPPServerInfo getServerInfo() { 99 return getXMPPServer().getServerInfo(); 100 } 101 102 105 public User getUser() { 106 User pageUser = null; 107 try { 108 pageUser = getUserManager().getUser(getAuthToken().getUsername()); 109 } 110 catch (Exception ignored) {} 111 return pageUser; 112 } 113 114 117 public boolean isEmbedded() { 118 try { 119 ClassUtils.forName("org.jivesoftware.messenger.starter.ServerStarter"); 120 return true; 121 } 122 catch (Exception ignored) { 123 return false; 124 } 125 } 126 127 130 public void restart() { 131 try { 132 getXMPPServer().restart(); 133 } 134 catch (Exception e) { 135 Log.error(e); 136 } 137 sleep(); 138 } 139 140 143 public void stop() { 144 try { 145 getXMPPServer().stop(); 146 } 147 catch (Exception e) { 148 Log.error(e); 149 } 150 sleep(); 151 } 152 153 public WebManager getManager() { 154 return this; 155 } 156 157 public void validateService() { 158 if (getPresenceManager() == null || 159 getXMPPServer() == null) { 160 showServerDown(); 161 } 162 } 163 164 public boolean isServerRunning() { 165 if (getPresenceManager() == null || 166 getXMPPServer() == null) { 167 return false; 168 } 169 return true; 170 } 171 172 public void addBreadCrumb(String name, String url) { 173 breadcrumbMap.put(name, url); 174 } 175 176 public Map getBreadCrumbs() { 177 return breadcrumbMap; 178 } 179 180 public void setSidebar(String sidebar) { 181 this.sidebar = sidebar; 182 } 183 184 public String getSidebar() { 185 return sidebar; 186 } 187 188 public void setTitle(String title) { 189 this.title = title; 190 } 191 192 public String getTitle() { 193 return title; 194 } 195 196 public int getBreadcrumbSize() { 197 return getBreadCrumbs().size(); 198 } 199 200 public void setStart(int start) { 201 this.start = start; 202 } 203 204 public int getStart() { 205 return start; 206 } 207 208 public void setRange(int range) { 209 this.range = range; 210 } 211 212 public int getRange() { 213 return range; 214 } 215 216 public int getCurrentPage() { 217 return (start / range) + 1; 218 } 219 220 private void sleep() { 221 try { 223 Thread.sleep(3000L); 224 } 225 catch (Exception ignored) { 226 } 227 } 228 229 protected void showServerDown() { 230 try { 231 response.sendRedirect("error-serverdown.jsp"); 232 } 233 catch (Exception ex) { 234 ex.printStackTrace(); 235 } 236 } 237 238 241 public static void copy(URL src, File dst) throws IOException { 242 InputStream in = null; 243 OutputStream out = null; 244 try { 245 in = src.openStream(); 246 out = new FileOutputStream(dst); 247 dst.mkdirs(); 248 copy(in, out); 249 } 250 finally { 251 try { 252 if (in != null) { 253 in.close(); 254 } 255 } 256 catch (IOException e) { } 257 try { 258 if (out != null) { 259 out.close(); 260 } 261 } 262 catch (IOException e) { } 263 } 264 } 265 266 271 private static void copy(InputStream in, OutputStream out) throws IOException { 272 byte[] buffer = new byte[4096]; 273 while (true) { 274 int bytesRead = in.read(buffer); 275 if (bytesRead < 0) { 276 break; 277 } 278 out.write(buffer, 0, bytesRead); 279 } 280 } 281 282 291 public int getRowsPerPage(String pageName, int defaultValue) { 292 return getPageProperty(pageName, "console.rows_per_page", defaultValue); 293 } 294 295 303 public void setRowsPerPage(String pageName, int newValue) { 304 setPageProperty(pageName, "console.rows_per_page", newValue); 305 } 306 307 318 public int getRefreshValue(String pageName, int defaultValue) { 319 return getPageProperty(pageName, "console.refresh", defaultValue); 320 } 321 322 331 public void setRefreshValue(String pageName, int newValue) { 332 setPageProperty(pageName, "console.refresh", newValue); 333 } 334 335 private int getPageProperty(String pageName, String property, int defaultValue) { 336 User user = getUser(); 337 if (user != null) { 338 String values = user.getProperties().get(property); 339 if (values != null) { 340 StringTokenizer tokens = new StringTokenizer (values, ",="); 341 while (tokens.hasMoreTokens()) { 342 String page = tokens.nextToken().trim(); 343 String rows = tokens.nextToken().trim(); 344 if (pageName.equals(page)) { 345 try { 346 return Integer.parseInt(rows); 347 } 348 catch (NumberFormatException e) { 349 return defaultValue; 350 } 351 } 352 } 353 } 354 } 355 return defaultValue; 356 } 357 358 public void setPageProperty(String pageName, String property, int newValue) { 359 String toStore = pageName + "=" + newValue; 360 User user = getUser(); 361 if (user != null) { 362 String values = user.getProperties().get(property); 363 if (values != null) { 364 if (values.contains(toStore)) { 365 return; 367 } 368 else { 369 if (values.contains(pageName)) { 370 int oldValue = getPageProperty(pageName, property, -1); 372 String toRemove = pageName + "=" + oldValue; 373 user.getProperties().put(property, values.replace(toRemove, toStore)); 374 } 375 else { 376 user.getProperties().put(property, values + "," + toStore); 378 } 379 } 380 } 381 else if (values == null) { 382 user.getProperties().put(property, toStore); 384 } 385 } 386 } 387 } | Popular Tags |