KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > go > teaservlet > TeaServletAdmin


1 /* ====================================================================
2  * TeaServlet - Copyright (c) 1999-2000 Walt Disney Internet Group
3  * ====================================================================
4  * The Tea Software License, Version 1.1
5  *
6  * Copyright (c) 2000 Walt Disney Internet Group. All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright
13  * notice, this list of conditions and the following disclaimer.
14  *
15  * 2. Redistributions in binary form must reproduce the above copyright
16  * notice, this list of conditions and the following disclaimer in
17  * the documentation and/or other materials provided with the
18  * distribution.
19  *
20  * 3. The end-user documentation included with the redistribution,
21  * if any, must include the following acknowledgment:
22  * "This product includes software developed by the
23  * Walt Disney Internet Group (http://opensource.go.com/)."
24  * Alternately, this acknowledgment may appear in the software itself,
25  * if and wherever such third-party acknowledgments normally appear.
26  *
27  * 4. The names "Tea", "TeaServlet", "Kettle", "Trove" and "BeanDoc" must
28  * not be used to endorse or promote products derived from this
29  * software without prior written permission. For written
30  * permission, please contact opensource@dig.com.
31  *
32  * 5. Products derived from this software may not be called "Tea",
33  * "TeaServlet", "Kettle" or "Trove", nor may "Tea", "TeaServlet",
34  * "Kettle", "Trove" or "BeanDoc" appear in their name, without prior
35  * written permission of the Walt Disney Internet Group.
36  *
37  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
38  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
39  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
40  * DISCLAIMED. IN NO EVENT SHALL THE WALT DISNEY INTERNET GROUP OR ITS
41  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
42  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
43  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
44  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
45  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
46  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
47  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
48  * ====================================================================
49  *
50  * For more information about Tea, please see http://opensource.go.com/.
51  */

52
53 package com.go.teaservlet;
54
55 import java.lang.reflect.Method JavaDoc;
56 import java.beans.*;
57 import java.util.*;
58 import javax.servlet.ServletContext JavaDoc;
59 import com.go.teaservlet.util.NameValuePair;
60 import com.go.tea.runtime.TemplateLoader;
61 import com.go.trove.log.Log;
62 import com.go.trove.log.LogEvent;
63 import com.go.trove.util.BeanComparator;
64 import com.go.teaservlet.util.cluster.Restartable;
65 import com.go.teaservlet.util.cluster.ClusterHook;
66
67 /******************************************************************************
68  * The Admin object which contains all administrative information. This object
69  * is meant to be used by the Admin page of the TeaServlet.
70  *
71  * @author Reece Wilton, Brian S O'Neill, Jonathan Colwell
72  * @version
73  * <!--$$Revision:--> 27 <!-- $-->, <!--$$JustDate:--> 6/26/01 <!-- $-->
74  */

75 public class TeaServletAdmin implements Restartable {
76     private TeaServlet mTeaServlet;
77     private String JavaDoc[] mClusteredServers;
78     private TemplateDepot.TemplateLoadResult mTemplateLoadResult;
79     private List mServerStatus;
80     private AppAdminLinks[] mAdminLinks;
81
82     /**
83      * Initializes the Admin object for the specific TeaServlet instance.
84      * @param teaServlet the TeaServlet to administer
85      */

86     public TeaServletAdmin(TeaServlet teaServlet) {
87         mTeaServlet = teaServlet;
88         mServerStatus = new Vector();
89     }
90
91     public String JavaDoc getStatus(Object JavaDoc paramObj) {
92         TemplateDepot.TemplateLoadResult result = getTemplateLoadResult();
93         if (result.isSuccessful()) {
94             int len = result.getReloadedTemplates().length;
95             if (len == 1) {
96                 return len + " Template reloaded successfully.";
97             }
98             else {
99                 return len + " Templates reloaded successfully.";
100             }
101         }
102         else {
103             int len = result.getTemplateLoadErrors().length;
104             if (len == 1) {
105                 return len + "Error encountered while reloading templates.";
106             }
107             else {
108                 return len + " Errors encountered while reloading templates.";
109             }
110         }
111     }
112
113     public boolean restart(Object JavaDoc paramObj) {
114         
115         try {
116             setTemplateLoadResult(mTeaServlet
117                                   .getApplicationDepot()
118                                   .loadTemplates(((Boolean JavaDoc)paramObj)
119                                                  .booleanValue()));
120             return getTemplateLoadResult().isSuccessful();
121         }
122         catch (Exception JavaDoc e) {
123             return false;
124         }
125     }
126
127     public ServletContext JavaDoc getServletContext() {
128         return mTeaServlet.getServletConfig().getServletContext();
129     }
130     
131     public void setTemplateLoadResult(TemplateDepot.TemplateLoadResult result)
132     {
133         mTemplateLoadResult = result;
134     }
135
136     protected TemplateDepot.TemplateLoadResult getTemplateLoadResult() {
137         return mTemplateLoadResult;
138     }
139
140     public NameValuePair[] getInitParameters() {
141         Enumeration e = mTeaServlet.getInitParameterNames();
142         List list = new ArrayList();
143         while (e.hasMoreElements()) {
144             String JavaDoc initName = (String JavaDoc)e.nextElement();
145             list.add(new NameValuePair
146                      (initName, mTeaServlet.getInitParameter(initName)));
147         }
148         return (NameValuePair[])list.toArray(new NameValuePair[list.size()]);
149     }
150
151     public NameValuePair[] getAttributes() {
152         ServletContext JavaDoc context = getServletContext();
153         Enumeration e = context.getAttributeNames();
154         List list = new ArrayList();
155         while (e.hasMoreElements()) {
156             String JavaDoc initName = (String JavaDoc)e.nextElement();
157             list.add(new NameValuePair
158                      (initName, context.getAttribute(initName)));
159         }
160         return (NameValuePair[])list.toArray(new NameValuePair[list.size()]);
161     }
162
163     public Log getLog() {
164         return mTeaServlet.getLog();
165     }
166
167     public LogEvent[] getLogEvents() {
168         return mTeaServlet.getLogEvents();
169     }
170
171     public ApplicationInfo[] getApplications() {
172         ApplicationDepot depot = mTeaServlet.getApplicationDepot();
173         Application[] apps = depot.getApplications();
174         String JavaDoc[] names = depot.getApplicationNames();
175         String JavaDoc[] prefixes = depot.getContextPrefixNames();
176
177         ApplicationInfo[] infos = new ApplicationInfo[apps.length];
178         for (int i=0; i<apps.length; i++) {
179             infos[i] = new ApplicationInfo(names[i], apps[i], prefixes[i]);
180         }
181
182         return infos;
183     }
184
185     /**
186      * Returns information about all functions available to the templates.
187      */

188     public FunctionInfo[] getFunctions() {
189         // TODO: make this a little more useful by showing more function
190
// details.
191

192         ApplicationInfo[] AppInf = getApplications();
193         
194         FunctionInfo[] funcArray = null;
195         
196         try {
197             MethodDescriptor[] methods = Introspector
198                 .getBeanInfo(HttpContext.class)
199                 .getMethodDescriptors();
200             List funcList = new Vector(50);
201             
202             for (int j = -1; j < AppInf.length;j++) {
203                 if (j >= 0) {
204                     methods = AppInf[j].getContextFunctions();
205                 }
206                 for (int i=0; i<methods.length; i++) {
207                     MethodDescriptor m = methods[i];
208                     if (m.getMethod().getDeclaringClass() != Object JavaDoc.class &&
209                         !m.getMethod().getName().equals("print") &&
210                         !m.getMethod().getName().equals("toString")) {
211                         
212                         if (j >= 0) {
213                             funcList.add(new FunctionInfo(m, AppInf[j]));
214                         }
215                         else {
216                             funcList.add(new FunctionInfo(m, null));
217                         }
218                     }
219                 }
220             }
221             
222             funcArray = (FunctionInfo[])funcList.toArray
223                 (new FunctionInfo[funcList.size()]);
224             Arrays.sort(funcArray);
225         }
226         catch (Exception JavaDoc ie) {
227             ie.printStackTrace();
228         }
229         
230         return funcArray;
231     }
232
233     public TemplateLoader.Template[] getTemplates() {
234         TemplateLoader.Template[] templates =
235             mTeaServlet.getApplicationDepot().getTemplateDepot().getLoadedTemplates();
236         Comparator c = BeanComparator.forClass(TemplateLoader.Template.class)
237             .orderBy("name");
238         Arrays.sort(templates, c);
239         return templates;
240     }
241
242     /**
243      * Returns null if no template compilation operation was requested.
244      */

245     public String JavaDoc[] getRecompiledTemplateNames() {
246         if (mTemplateLoadResult != null) {
247             return mTemplateLoadResult.getReloadedTemplates();
248         }
249         else {
250             return null;
251         }
252     }
253
254     /**
255      * Returns null if no template compilation operation was requested.
256      */

257     public TemplateError[] getRecompiledTemplateErrors() {
258         if (mTemplateLoadResult != null) {
259             return mTemplateLoadResult.getTemplateLoadErrors();
260         }
261         else {
262             return null;
263         }
264     }
265
266     public Class JavaDoc getTeaServletClass() {
267         return mTeaServlet.getClass();
268     }
269
270     public String JavaDoc getTeaServletVersion() {
271         return com.go.teaservlet.PackageInfo.getImplementationVersion();
272     }
273
274     public String JavaDoc getTeaVersion() {
275         return com.go.tea.PackageInfo.getImplementationVersion();
276     }
277
278     public String JavaDoc[] getClusteredServers() {
279         return mClusteredServers;
280     }
281
282     protected void setClusteredServers(String JavaDoc[] serverNames) {
283         mClusteredServers = serverNames;
284     }
285
286     public ServerStatus[] getReloadStatusOfServers() {
287         ServerStatus[] statusArray =
288             (ServerStatus[])mServerStatus.toArray(new ServerStatus[0]);
289
290         Comparator c = BeanComparator.forClass(ServerStatus.class)
291             .orderBy("statusCode").reverse()
292             .orderBy("serverName");
293         Arrays.sort(statusArray, c);
294         return statusArray;
295     }
296
297     protected void clearServerReloadStatus() {
298         mServerStatus.clear();
299     }
300
301     protected void setServerReloadStatus(String JavaDoc name,
302                                       int statusCode, String JavaDoc message) {
303         mServerStatus.add(new ServerStatus(name, statusCode, message));
304     }
305
306     public AppAdminLinks[] getAdminLinks() {
307         return mAdminLinks;
308     }
309
310     protected void setAdminLinks(AppAdminLinks[] links) {
311
312         mAdminLinks = links;
313     }
314
315     public class ServerStatus {
316         private String JavaDoc mServerName;
317         private String JavaDoc mMessage;
318         private int mStatusCode;
319
320         public ServerStatus(String JavaDoc name, int statusCode, String JavaDoc message) {
321             mServerName = name;
322             mMessage = message;
323             mStatusCode = statusCode;
324         }
325
326         public String JavaDoc getServerName() {
327             return mServerName;
328         }
329
330         public String JavaDoc getMessage() {
331             return mMessage;
332         }
333
334         public int getStatusCode() {
335             return mStatusCode;
336         }
337     }
338 }
339
Popular Tags