KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jivesoftware > util > WebManager


1 /**
2  * $RCSfile: WebManager.java,v $
3  * $Revision: 1.24 $
4  * $Date: 2005/07/10 22:07:59 $
5  *
6  * Copyright (C) 2004 Jive Software. All rights reserved.
7  *
8  * This software is published under the terms of the GNU Public License (GPL),
9  * a copy of which is included in this distribution.
10  */

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 JavaDoc;
24 import java.util.LinkedHashMap JavaDoc;
25 import java.util.Map JavaDoc;
26 import java.util.StringTokenizer JavaDoc;
27
28 /**
29  * A utility bean for Messenger admin console pages.
30  */

31 public class WebManager extends WebBean {
32
33     private Map JavaDoc breadcrumbMap = new LinkedHashMap JavaDoc();
34     private String JavaDoc title = "";
35     private String JavaDoc sidebar = "";
36
37     private int start = 0;
38     private int range = 15;
39
40     public WebManager() {
41     }
42
43     /**
44      * Returns the auth token redirects to the login page if an auth token is not found.
45      */

46     public AuthToken getAuthToken() {
47         return (AuthToken)session.getAttribute("jive.admin.authToken");
48     }
49
50     /**
51      * Returns <tt>true</tt> if the Messenger container is in setup mode, <tt>false</tt> otherwise.
52      */

53     public boolean isSetupMode() {
54         return getXMPPServer().isSetupMode();
55     }
56
57     /**
58      * Returns the XMPP server object -- can get many config items from here.
59      */

60     public XMPPServer getXMPPServer() {
61         final XMPPServer xmppServer = XMPPServer.getInstance();
62         if (xmppServer == null) {
63             // Show that the server is down
64
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     /**
103      * Returns the page user or <tt>null</tt> if one is not found.
104      */

105     public User getUser() {
106         User pageUser = null;
107         try {
108             pageUser = getUserManager().getUser(getAuthToken().getUsername());
109         }
110         catch (Exception JavaDoc ignored) {}
111         return pageUser;
112     }
113
114     /**
115      * Returns <tt>true</tt> if the server is in embedded mode, <tt>false</tt> otherwise.
116      */

117     public boolean isEmbedded() {
118         try {
119             ClassUtils.forName("org.jivesoftware.messenger.starter.ServerStarter");
120             return true;
121         }
122         catch (Exception JavaDoc ignored) {
123             return false;
124         }
125     }
126
127     /**
128      * Restarts the server then sleeps for 3 seconds.
129      */

130     public void restart() {
131         try {
132             getXMPPServer().restart();
133         }
134         catch (Exception JavaDoc e) {
135             Log.error(e);
136         }
137         sleep();
138     }
139
140     /**
141      * Stops the server then sleeps for 3 seconds.
142      */

143     public void stop() {
144         try {
145             getXMPPServer().stop();
146         }
147         catch (Exception JavaDoc 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 JavaDoc name, String JavaDoc url) {
173         breadcrumbMap.put(name, url);
174     }
175
176     public Map JavaDoc getBreadCrumbs() {
177         return breadcrumbMap;
178     }
179
180     public void setSidebar(String JavaDoc sidebar) {
181         this.sidebar = sidebar;
182     }
183
184     public String JavaDoc getSidebar() {
185         return sidebar;
186     }
187
188     public void setTitle(String JavaDoc title) {
189         this.title = title;
190     }
191
192     public String JavaDoc 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         // Sleep for a minute:
222
try {
223             Thread.sleep(3000L);
224         }
225         catch (Exception JavaDoc ignored) {
226         }
227     }
228
229     protected void showServerDown() {
230         try {
231             response.sendRedirect("error-serverdown.jsp");
232         }
233         catch (Exception JavaDoc ex) {
234             ex.printStackTrace();
235         }
236     }
237
238     /**
239      * Copies the contents at <CODE>src</CODE> to <CODE>dst</CODE>.
240      */

241     public static void copy(URL JavaDoc 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     /**
267      * Common code for copy routines. By convention, the streams are
268      * closed in the same method in which they were opened. Thus,
269      * this method does not close the streams when the copying is done.
270      */

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     /**
283      * Returns the number of rows per page for the specified page for the current logged user.
284      * The rows per page value is stored as a user property. The same property is being used for
285      * different pages. The encoding format is the following "pageName1=value,pageName2=value".
286      *
287      * @param pageName the name of the page to look up its stored value.
288      * @param defaultValue the default value to return if no user value was found.
289      * @return the number of rows per page for the specified page for the current logged user.
290      */

291     public int getRowsPerPage(String JavaDoc pageName, int defaultValue) {
292         return getPageProperty(pageName, "console.rows_per_page", defaultValue);
293     }
294
295     /**
296      * Sets the new number of rows per page for the specified page for the current logged user.
297      * The rows per page value is stored as a user property. The same property is being used for
298      * different pages. The encoding format is the following "pageName1=value,pageName2=value".
299      *
300      * @param pageName the name of the page to stored its new value.
301      * @param newValue the new rows per page value.
302      */

303     public void setRowsPerPage(String JavaDoc pageName, int newValue) {
304         setPageProperty(pageName, "console.rows_per_page", newValue);
305     }
306
307     /**
308      * Returns the number of seconds between each page refresh for the specified page for the
309      * current logged user. The value is stored as a user property. The same property is being
310      * used for different pages. The encoding format is the following
311      * "pageName1=value,pageName2=value".
312      *
313      * @param pageName the name of the page to look up its stored value.
314      * @param defaultValue the default value to return if no user value was found.
315      * @return the number of seconds between each page refresh for the specified page for
316      * the current logged user.
317      */

318     public int getRefreshValue(String JavaDoc pageName, int defaultValue) {
319         return getPageProperty(pageName, "console.refresh", defaultValue);
320     }
321
322     /**
323      * Sets the number of seconds between each page refresh for the specified page for the
324      * current logged user. The value is stored as a user property. The same property is being
325      * used for different pages. The encoding format is the following
326      * "pageName1=value,pageName2=value".
327      *
328      * @param pageName the name of the page to stored its new value.
329      * @param newValue the new number of seconds between each page refresh.
330      */

331     public void setRefreshValue(String JavaDoc pageName, int newValue) {
332         setPageProperty(pageName, "console.refresh", newValue);
333     }
334
335     private int getPageProperty(String JavaDoc pageName, String JavaDoc property, int defaultValue) {
336         User user = getUser();
337         if (user != null) {
338             String JavaDoc values = user.getProperties().get(property);
339             if (values != null) {
340                 StringTokenizer JavaDoc tokens = new StringTokenizer JavaDoc(values, ",=");
341                 while (tokens.hasMoreTokens()) {
342                     String JavaDoc page = tokens.nextToken().trim();
343                     String JavaDoc rows = tokens.nextToken().trim();
344                     if (pageName.equals(page)) {
345                         try {
346                             return Integer.parseInt(rows);
347                         }
348                         catch (NumberFormatException JavaDoc e) {
349                             return defaultValue;
350                         }
351                     }
352                 }
353             }
354         }
355         return defaultValue;
356     }
357
358     public void setPageProperty(String JavaDoc pageName, String JavaDoc property, int newValue) {
359         String JavaDoc toStore = pageName + "=" + newValue;
360         User user = getUser();
361         if (user != null) {
362             String JavaDoc values = user.getProperties().get(property);
363             if (values != null) {
364                 if (values.contains(toStore)) {
365                     // The new value for the page was already stored so do nothing
366
return;
367                 }
368                 else {
369                     if (values.contains(pageName)) {
370                         // Replace an old value for the page with the new value
371
int oldValue = getPageProperty(pageName, property, -1);
372                         String JavaDoc toRemove = pageName + "=" + oldValue;
373                         user.getProperties().put(property, values.replace(toRemove, toStore));
374                     }
375                     else {
376                         // Append the new page-value
377
user.getProperties().put(property, values + "," + toStore);
378                     }
379                 }
380             }
381             else if (values == null) {
382                 // Store the new page-value as a new user property
383
user.getProperties().put(property, toStore);
384             }
385         }
386     }
387 }
Popular Tags