001: /**
002: * $RCSfile$
003: * $Revision: 8311 $
004: * $Date: 2007-05-14 12:51:36 -0700 (Mon, 14 May 2007) $
005: *
006: * Copyright (C) 2004 Jive Software. All rights reserved.
007: *
008: * This software is published under the terms of the GNU Public License (GPL),
009: * a copy of which is included in this distribution.
010: */package org.jivesoftware.util;
011:
012: import org.jivesoftware.openfire.*;
013: import org.jivesoftware.openfire.auth.AuthToken;
014: import org.jivesoftware.openfire.group.GroupManager;
015: import org.jivesoftware.openfire.muc.MultiUserChatServer;
016: import org.jivesoftware.openfire.roster.RosterManager;
017: import org.jivesoftware.openfire.user.User;
018: import org.jivesoftware.openfire.user.UserManager;
019: import org.jivesoftware.util.cache.Cache;
020: import org.jivesoftware.util.cache.CacheFactory;
021:
022: import java.io.*;
023: import java.net.URL;
024: import java.util.Arrays;
025: import java.util.Comparator;
026: import java.util.StringTokenizer;
027:
028: /**
029: * A utility bean for Openfire admin console pages.
030: */
031: public class WebManager extends WebBean {
032:
033: private int start = 0;
034: private int range = 15;
035:
036: public WebManager() {
037: }
038:
039: /**
040: * Returns the auth token redirects to the login page if an auth token is not found.
041: */
042: public AuthToken getAuthToken() {
043: return (AuthToken) session.getAttribute("jive.admin.authToken");
044: }
045:
046: /**
047: * Returns <tt>true</tt> if the Openfire container is in setup mode, <tt>false</tt> otherwise.
048: */
049: public boolean isSetupMode() {
050: return getXMPPServer().isSetupMode();
051: }
052:
053: /**
054: * Returns the XMPP server object -- can get many config items from here.
055: */
056: public XMPPServer getXMPPServer() {
057: final XMPPServer xmppServer = XMPPServer.getInstance();
058: if (xmppServer == null) {
059: // Show that the server is down
060: showServerDown();
061: return null;
062: }
063: return xmppServer;
064: }
065:
066: public UserManager getUserManager() {
067: return getXMPPServer().getUserManager();
068: }
069:
070: public GroupManager getGroupManager() {
071: return GroupManager.getInstance();
072: }
073:
074: public RosterManager getRosterManager() {
075: return getXMPPServer().getRosterManager();
076: }
077:
078: public PrivateStorage getPrivateStore() {
079: return getXMPPServer().getPrivateStorage();
080: }
081:
082: public PresenceManager getPresenceManager() {
083: return getXMPPServer().getPresenceManager();
084: }
085:
086: public SessionManager getSessionManager() {
087: return getXMPPServer().getSessionManager();
088: }
089:
090: public MultiUserChatServer getMultiUserChatServer() {
091: return getXMPPServer().getMultiUserChatServer();
092: }
093:
094: public XMPPServerInfo getServerInfo() {
095: return getXMPPServer().getServerInfo();
096: }
097:
098: /**
099: * Returns the page user or <tt>null</tt> if one is not found.
100: */
101: public User getUser() {
102: User pageUser = null;
103: try {
104: pageUser = getUserManager().getUser(
105: getAuthToken().getUsername());
106: } catch (Exception ignored) {
107: // Ignore.
108: }
109: return pageUser;
110: }
111:
112: /**
113: * Returns <tt>true</tt> if the server is in embedded mode, <tt>false</tt> otherwise.
114: */
115: public boolean isEmbedded() {
116: try {
117: ClassUtils
118: .forName("org.jivesoftware.openfire.starter.ServerStarter");
119: return true;
120: } catch (Exception ignored) {
121: return false;
122: }
123: }
124:
125: /**
126: * Restarts the server then sleeps for 3 seconds.
127: */
128: public void restart() {
129: try {
130: getXMPPServer().restart();
131: } catch (Exception e) {
132: Log.error(e);
133: }
134: sleep();
135: }
136:
137: /**
138: * Stops the server then sleeps for 3 seconds.
139: */
140: public void stop() {
141: try {
142: getXMPPServer().stop();
143: } catch (Exception e) {
144: Log.error(e);
145: }
146: sleep();
147: }
148:
149: public WebManager getManager() {
150: return this ;
151: }
152:
153: public void validateService() {
154: if (getPresenceManager() == null || getXMPPServer() == null) {
155: showServerDown();
156: }
157: }
158:
159: public boolean isServerRunning() {
160: return !(getPresenceManager() == null || getXMPPServer() == null);
161: }
162:
163: public void setStart(int start) {
164: this .start = start;
165: }
166:
167: public int getStart() {
168: return start;
169: }
170:
171: public void setRange(int range) {
172: this .range = range;
173: }
174:
175: public int getRange() {
176: return range;
177: }
178:
179: public int getCurrentPage() {
180: return (start / range) + 1;
181: }
182:
183: private void sleep() {
184: // Sleep for a minute:
185: try {
186: Thread.sleep(3000L);
187: } catch (Exception ignored) {
188: // Ignore.
189: }
190: }
191:
192: protected void showServerDown() {
193: try {
194: response.sendRedirect("error-serverdown.jsp");
195: } catch (Exception ex) {
196: ex.printStackTrace();
197: }
198: }
199:
200: /**
201: * Copies the contents at <CODE>src</CODE> to <CODE>dst</CODE>.
202: */
203: public static void copy(URL src, File dst) throws IOException {
204: InputStream in = null;
205: OutputStream out = null;
206: try {
207: in = src.openStream();
208: out = new FileOutputStream(dst);
209: dst.mkdirs();
210: copy(in, out);
211: } finally {
212: try {
213: if (in != null) {
214: in.close();
215: }
216: } catch (IOException e) {
217: // Ignore.
218: }
219: try {
220: if (out != null) {
221: out.close();
222: }
223: } catch (IOException e) {
224: // Ignore.
225: }
226: }
227: }
228:
229: /**
230: * Common code for copy routines. By convention, the streams are
231: * closed in the same method in which they were opened. Thus,
232: * this method does not close the streams when the copying is done.
233: */
234: private static void copy(InputStream in, OutputStream out)
235: throws IOException {
236: byte[] buffer = new byte[4096];
237: while (true) {
238: int bytesRead = in.read(buffer);
239: if (bytesRead < 0) {
240: break;
241: }
242: out.write(buffer, 0, bytesRead);
243: }
244: }
245:
246: /**
247: * Returns the number of rows per page for the specified page for the current logged user.
248: * The rows per page value is stored as a user property. The same property is being used for
249: * different pages. The encoding format is the following "pageName1=value,pageName2=value".
250: *
251: * @param pageName the name of the page to look up its stored value.
252: * @param defaultValue the default value to return if no user value was found.
253: * @return the number of rows per page for the specified page for the current logged user.
254: */
255: public int getRowsPerPage(String pageName, int defaultValue) {
256: return getPageProperty(pageName, "console.rows_per_page",
257: defaultValue);
258: }
259:
260: /**
261: * Sets the new number of rows per page for the specified page for the current logged user.
262: * The rows per page value is stored as a user property. The same property is being used for
263: * different pages. The encoding format is the following "pageName1=value,pageName2=value".
264: *
265: * @param pageName the name of the page to stored its new value.
266: * @param newValue the new rows per page value.
267: */
268: public void setRowsPerPage(String pageName, int newValue) {
269: setPageProperty(pageName, "console.rows_per_page", newValue);
270: }
271:
272: /**
273: * Returns the number of seconds between each page refresh for the specified page for the
274: * current logged user. The value is stored as a user property. The same property is being
275: * used for different pages. The encoding format is the following
276: * "pageName1=value,pageName2=value".
277: *
278: * @param pageName the name of the page to look up its stored value.
279: * @param defaultValue the default value to return if no user value was found.
280: * @return the number of seconds between each page refresh for the specified page for
281: * the current logged user.
282: */
283: public int getRefreshValue(String pageName, int defaultValue) {
284: return getPageProperty(pageName, "console.refresh",
285: defaultValue);
286: }
287:
288: /**
289: * Sets the number of seconds between each page refresh for the specified page for the
290: * current logged user. The value is stored as a user property. The same property is being
291: * used for different pages. The encoding format is the following
292: * "pageName1=value,pageName2=value".
293: *
294: * @param pageName the name of the page to stored its new value.
295: * @param newValue the new number of seconds between each page refresh.
296: */
297: public void setRefreshValue(String pageName, int newValue) {
298: setPageProperty(pageName, "console.refresh", newValue);
299: }
300:
301: public int getPageProperty(String pageName, String property,
302: int defaultValue) {
303: User user = getUser();
304: if (user != null) {
305: String values = user.getProperties().get(property);
306: if (values != null) {
307: StringTokenizer tokens = new StringTokenizer(values,
308: ",=");
309: while (tokens.hasMoreTokens()) {
310: String page = tokens.nextToken().trim();
311: String rows = tokens.nextToken().trim();
312: if (pageName.equals(page)) {
313: try {
314: return Integer.parseInt(rows);
315: } catch (NumberFormatException e) {
316: return defaultValue;
317: }
318: }
319: }
320: }
321: }
322: return defaultValue;
323: }
324:
325: public void setPageProperty(String pageName, String property,
326: int newValue) {
327: String toStore = pageName + "=" + newValue;
328: User user = getUser();
329: if (user != null) {
330: String values = user.getProperties().get(property);
331: if (values != null) {
332: if (values.contains(toStore)) {
333: // The new value for the page was already stored so do nothing
334: return;
335: } else {
336: if (values.contains(pageName)) {
337: // Replace an old value for the page with the new value
338: int oldValue = getPageProperty(pageName,
339: property, -1);
340: String toRemove = pageName + "=" + oldValue;
341: user.getProperties().put(property,
342: values.replace(toRemove, toStore));
343: } else {
344: // Append the new page-value
345: user.getProperties().put(property,
346: values + "," + toStore);
347: }
348: }
349: } else {
350: // Store the new page-value as a new user property
351: user.getProperties().put(property, toStore);
352: }
353: }
354: }
355:
356: public Cache[] getCaches() {
357: Cache[] caches = CacheFactory.getAllCaches();
358: Arrays.sort(caches, new Comparator<Cache>() {
359: public int compare(Cache cache1, Cache cache2) {
360: return cache1.getName().toLowerCase().compareTo(
361: cache2.getName().toLowerCase());
362: }
363: });
364: return caches;
365: }
366: }
|