001: package org.caramba.jmx;
002:
003: import org.caramba.CarambaServlet;
004: import org.caramba.config.CarambaConfig;
005: import org.caramba.config.DefaultCarambaConfigFactory;
006: import org.caramba.config.Interceptor;
007: import org.caramba.config.PageConfig;
008: import org.caramba.config.PanelConfig;
009: import org.caramba.plugin.PlugIn;
010: import org.springframework.beans.BeansException;
011: import org.springframework.context.ApplicationContext;
012: import org.springframework.context.ApplicationContextAware;
013: import org.springframework.jmx.export.annotation.ManagedAttribute;
014: import org.springframework.jmx.export.annotation.ManagedOperation;
015: import org.springframework.jmx.export.annotation.ManagedResource;
016: import org.springframework.web.context.WebApplicationContext;
017:
018: import javax.servlet.ServletException;
019: import java.util.Collection;
020: import java.util.Iterator;
021: import java.util.List;
022:
023: /**
024: * This JavaBean should be used to expose in a JMX server. Currently, it works only when it is exposed by
025: * {@link org.springframework.jmx.export.MBeanExporter}.
026: * It is also required that it is constructed by a {@link WebApplicationContext}. (if not, the bean cannot access the ServletContext,
027: * were the CarambaConfig is located)
028: *
029: * @author Pieter Degraeuwe
030: * Date: Oct 4, 2006
031: * Time: 4:51:55 PM
032: */
033: @ManagedResource(objectName="caramba:name=CarambaConfigManager",description="Manage Caramba Config",log=true,logFile="jmx.log",currencyTimeLimit=15,persistPolicy="OnUpdate",persistPeriod=200,persistLocation="foo",persistName="bar")
034: public class CarambaManager implements ApplicationContextAware {
035: private WebApplicationContext context;
036:
037: @ManagedOperation(description="Reloads the caramba configuration by re-initializing the CarambaServlet")
038: public void reloadConfig() throws ServletException {
039: CarambaConfig carambaConfig = getCarambaConfig();
040: CarambaServlet servlet = carambaConfig.getCarambaServlet();
041: servlet.init();
042: }
043:
044: @ManagedOperation(description="Shows all configered pages")
045: public String showConfiguredPages() throws ServletException {
046: StringBuffer buff = new StringBuffer();
047: PageConfig[] pageConfigs = getCarambaConfig().getPageConfigs();
048: buff.append("Page Configurations: ").append("\n\n");
049: for (int i = 0; i < pageConfigs.length; i++) {
050: PageConfig pageConfig = pageConfigs[i];
051: buff.append(pageConfig.getName()).append("\t").append(
052: pageConfig.getClassName()).append("\t").append(
053: pageConfig.getResource()).append("\n");
054: }
055: return buff.toString();
056: }
057:
058: @ManagedOperation(description="Shows all configered panels")
059: public String showConfiguredPanels() throws ServletException {
060: StringBuffer buff = new StringBuffer();
061: PanelConfig[] panelConfigs = getCarambaConfig()
062: .getPanelConfigs();
063: buff.append("Panel Configurations: ").append("\n\n");
064: for (int i = 0; i < panelConfigs.length; i++) {
065: PanelConfig panelConfig = panelConfigs[i];
066: buff.append(panelConfig.getName()).append("\t").append(
067: panelConfig.getClassName()).append("\t").append(
068: panelConfig.getResource()).append("\n");
069: }
070: return buff.toString();
071: }
072:
073: @ManagedOperation(description="Shows all configered interceptors")
074: public String showConfiguredInterceptors() throws ServletException {
075: StringBuffer buff = new StringBuffer();
076: List interceptors = getCarambaConfig().getInterceptors();
077: buff.append("Configured interceptors: ").append("\n\n");
078: for (int i = 0; i < interceptors.size(); i++) {
079: Interceptor interceptor = (Interceptor) interceptors.get(i);
080: buff.append(interceptor.getClass().getName()).append(" : ")
081: .append(interceptor).append("\n");
082: }
083: return buff.toString();
084: }
085:
086: @ManagedOperation(description="Shows all installed plugins")
087: public String showInstalledPlugIns() throws ServletException {
088: StringBuffer buff = new StringBuffer();
089: Collection plugins = getCarambaConfig().getPlugIns();
090: buff.append("Installed plugins: ").append("\n\n");
091: for (Iterator iterator = plugins.iterator(); iterator.hasNext();) {
092: PlugIn plugIn = (PlugIn) iterator.next();
093: buff.append(plugIn.getClass().getName()).append(" : ")
094: .append(plugIn).append("\n");
095: }
096: return buff.toString();
097: }
098:
099: @ManagedOperation(description="Shows all JavaScript libraries that are streamed via Caramba")
100: public String showStreamedJavaScriptLibraries()
101: throws ServletException {
102: StringBuffer buff = new StringBuffer();
103: String[] javascriptLibraries = getCarambaConfig()
104: .getJavaScriptLibraries();
105: buff.append("Streamed JavaScript libraries : ").append("\n\n");
106: for (int i = 0; i < javascriptLibraries.length; i++) {
107: String javascriptLibrary = javascriptLibraries[i];
108: buff.append(javascriptLibrary).append("\n");
109: }
110: return buff.toString();
111: }
112:
113: protected CarambaConfig getCarambaConfig() {
114: return CarambaConfig.getCarambaConfig(context
115: .getServletContext());
116: }
117:
118: @ManagedAttribute
119: public int getPageConfigCount() {
120: return getCarambaConfig().getPageConfigs().length;
121: }
122:
123: @ManagedAttribute
124: public int getPanelConfigCount() {
125: return getCarambaConfig().getPanelConfigs().length;
126: }
127:
128: @ManagedAttribute
129: public int getStreamedJavaScriptLibraryCount() {
130: return getCarambaConfig().getJavaScriptLibraries().length;
131: }
132:
133: @ManagedAttribute
134: public int getInterceptorCount() {
135: return getCarambaConfig().getInterceptors().size();
136: }
137:
138: @ManagedAttribute
139: public int getPluginCount() {
140: return getCarambaConfig().getPlugIns().size();
141: }
142:
143: @ManagedAttribute
144: public String getCarambaConfigFactoryClassName() {
145: String configFactoryClass = getCarambaConfig()
146: .getCarambaServlet().getServletConfig()
147: .getInitParameter(
148: CarambaServlet.CONFIG_FACTORY_CLASS_PARAM);
149: if (configFactoryClass == null) {
150: configFactoryClass = DefaultCarambaConfigFactory.class
151: .getName();
152: }
153: return configFactoryClass;
154: }
155:
156: public void setApplicationContext(
157: ApplicationContext applicationContext)
158: throws BeansException {
159: if (!(applicationContext instanceof WebApplicationContext)) {
160: throw new IllegalArgumentException(
161: "The CarambaManager can only be loaded by a WebApplicationContext BeanFactory implementation");
162: }
163: context = (WebApplicationContext) applicationContext;
164: }
165: }
|