1 52 53 package com.go.teaservlet; 54 55 import java.lang.reflect.Method ; 56 import java.beans.*; 57 import java.util.*; 58 import javax.servlet.ServletContext ; 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 75 public class TeaServletAdmin implements Restartable { 76 private TeaServlet mTeaServlet; 77 private String [] mClusteredServers; 78 private TemplateDepot.TemplateLoadResult mTemplateLoadResult; 79 private List mServerStatus; 80 private AppAdminLinks[] mAdminLinks; 81 82 86 public TeaServletAdmin(TeaServlet teaServlet) { 87 mTeaServlet = teaServlet; 88 mServerStatus = new Vector(); 89 } 90 91 public String getStatus(Object 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 paramObj) { 114 115 try { 116 setTemplateLoadResult(mTeaServlet 117 .getApplicationDepot() 118 .loadTemplates(((Boolean )paramObj) 119 .booleanValue())); 120 return getTemplateLoadResult().isSuccessful(); 121 } 122 catch (Exception e) { 123 return false; 124 } 125 } 126 127 public ServletContext 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 initName = (String )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 context = getServletContext(); 153 Enumeration e = context.getAttributeNames(); 154 List list = new ArrayList(); 155 while (e.hasMoreElements()) { 156 String initName = (String )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 [] names = depot.getApplicationNames(); 175 String [] 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 188 public FunctionInfo[] getFunctions() { 189 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 .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 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 245 public String [] getRecompiledTemplateNames() { 246 if (mTemplateLoadResult != null) { 247 return mTemplateLoadResult.getReloadedTemplates(); 248 } 249 else { 250 return null; 251 } 252 } 253 254 257 public TemplateError[] getRecompiledTemplateErrors() { 258 if (mTemplateLoadResult != null) { 259 return mTemplateLoadResult.getTemplateLoadErrors(); 260 } 261 else { 262 return null; 263 } 264 } 265 266 public Class getTeaServletClass() { 267 return mTeaServlet.getClass(); 268 } 269 270 public String getTeaServletVersion() { 271 return com.go.teaservlet.PackageInfo.getImplementationVersion(); 272 } 273 274 public String getTeaVersion() { 275 return com.go.tea.PackageInfo.getImplementationVersion(); 276 } 277 278 public String [] getClusteredServers() { 279 return mClusteredServers; 280 } 281 282 protected void setClusteredServers(String [] 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 name, 302 int statusCode, String 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 mServerName; 317 private String mMessage; 318 private int mStatusCode; 319 320 public ServerStatus(String name, int statusCode, String message) { 321 mServerName = name; 322 mMessage = message; 323 mStatusCode = statusCode; 324 } 325 326 public String getServerName() { 327 return mServerName; 328 } 329 330 public String getMessage() { 331 return mMessage; 332 } 333 334 public int getStatusCode() { 335 return mStatusCode; 336 } 337 } 338 } 339 | Popular Tags |