View Javadoc

1   package com.explosion.expfmodules.monitoring;
2   
3   /*
4    * =============================================================================
5    * 0823935689
6    * Copyright 2004 Stephen Cowx
7    * 
8    * Licensed under the Apache License, Version 2.0 (the "License"); you may not
9    * use this file except in compliance with the License. You may obtain a copy of
10   * the License at
11   * 
12   * http://www.apache.org/licenses/LICENSE-2.0
13   * 
14   * Unless required by applicable law or agreed to in writing, software
15   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
16   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
17   * License for the specific language governing permissions and limitations under
18   * the License.
19   * 
20   * =============================================================================
21   */
22  
23  import java.awt.event.ActionEvent;
24  import java.awt.event.KeyEvent;
25  import java.util.HashMap;
26  import java.util.Map;
27  import java.util.Properties;
28  import java.util.Vector;
29  import java.util.prefs.Preferences;
30  
31  import javax.swing.JCheckBoxMenuItem;
32  import javax.swing.JInternalFrame;
33  import javax.swing.JPanel;
34  import javax.swing.KeyStroke;
35  
36  import org.apache.log4j.LogManager;
37  import org.apache.log4j.Logger;
38  
39  import com.explosion.expf.Application;
40  import com.explosion.expf.ExpActionListener;
41  import com.explosion.expf.ExpConstants;
42  import com.explosion.expf.ExpFrame;
43  import com.explosion.expf.ExpModuleManager;
44  import com.explosion.expf.menusandtools.menu.ExpMenu;
45  import com.explosion.expf.menusandtools.menu.ExpMenuBar;
46  import com.explosion.expf.menusandtools.menu.segmented.ExpMenuSegment;
47  import com.explosion.expf.preferences.SystemPreferences;
48  import com.explosion.utilities.exception.ExceptionManagerFactory;
49  import com.explosion.utilities.preferences.Preference;
50  
51  /***
52   * @author Stephen Cowx Date created:@08-Feb-2003
53   */
54  public class ResourceManagementModule implements ExpModuleManager
55  {
56  
57      private static Logger log = LogManager.getLogger(ResourceManagementModule.class);
58  
59      public static final String SYSTEM_RESOURCE_LOGMAX = "maxlogginglength";
60      public static final String SYSTEM_RESOURCE_LOGPATTERN = "loggingpattern";
61      public static final String SYSTEM_RESOURCE_HEIGHT = "SYSTEM_RESOURCE_HEIGHT";
62      public static final String SYSTEM_RESOURCE_WIDTH = "SYSTEM_RESOURCE_HEIGHT";
63      public static final String MENU_CLOSE_RESOURCEMONITOR = "MENU_CLOSE_RESOURCEMONITOR";
64      public static final String MENU_CONFIGURE_RESOURCES = "MENU_CONFIGURE_RESOURCES";
65  
66      private Vector preferences = new Vector();
67      private static ResourceManagementModule instance = null;
68      private HashMap preferenceHash = new HashMap();
69      private JCheckBoxMenuItem menuItem;
70  
71      public ResourceManagementModule()
72      {
73          instance = this;
74      }
75  
76      public static ResourceManagementModule instance()
77      {
78          return instance;
79      }
80  
81      public void initialiseGui() throws Exception
82      {
83          log.debug("Initialising gui of Resource Monitor Module");
84          ExpMenuBar menuBar = ((ExpFrame) Application.getApplicationFrame()).getExpMenuBar();
85          ExpMenu menuConfigure = (ExpMenu) menuBar.getMenuHelper().getMenu(ExpConstants.MENU_TOOLS);
86  
87          /* Configure menu */
88          ExpMenuSegment firstSegment = menuConfigure.getFirstSegment();
89          menuItem = menuBar.getMenuHelper().createCheckBoxMenuItem("Monitor", 'r', MENU_CONFIGURE_RESOURCES, KeyStroke.getKeyStroke(KeyEvent.VK_R, KeyEvent.CTRL_MASK), 1);
90          firstSegment.addElement(menuItem);
91          
92          Application.addToGlobalCookie(MENU_CONFIGURE_RESOURCES, 1);
93      }
94  
95      public String getVersion()
96      {
97          return "1.0";
98      }
99  
100     public String getName()
101     {
102         return "Monitor";
103     }
104 
105     public String getDescription()
106     {
107         return "Monitoring tools for application resources.";
108     }
109 
110     public void initialiseCore(Properties properties)
111     {
112         try
113         {
114             String appPrefix = (String) SystemPreferences.getPreference(ExpConstants.EXPF_APP_PREFIX).getValue();
115 
116             Preferences prefs = Preferences.userRoot().node(appPrefix + "/resource");
117 
118             Preference height = new Preference(SYSTEM_RESOURCE_HEIGHT, Preference.PROPERTY_TYPE_INT, new Integer(400), prefs);
119             height.setLongName("Last height");
120             height.setDescription("This is the saved window height.");
121             height.setEditable(false);
122             height.setVisible(false);
123             preferences.addElement(height);
124             preferenceHash.put(SYSTEM_RESOURCE_HEIGHT, height);
125 
126             Preference width = new Preference(SYSTEM_RESOURCE_WIDTH, Preference.PROPERTY_TYPE_INT, new Integer(600), prefs);
127             width.setLongName("Last width");
128             width.setDescription("This is the saved window width.");
129             width.setEditable(false);
130             width.setVisible(false);
131             preferences.addElement(width);
132             preferenceHash.put(SYSTEM_RESOURCE_WIDTH, width);
133             
134             Preference maxLoggingLength = new Preference(SYSTEM_RESOURCE_LOGMAX, Preference.PROPERTY_TYPE_INT, new Integer(50000), prefs);
135             maxLoggingLength.setLongName("Log window max output");
136             maxLoggingLength.setDescription("This is the maximum number of logging events captured in the logging window before the evens will be lost off the top of the stack. -1 indicates that there is no limit.");
137             maxLoggingLength.setEditable(true);
138             preferences.addElement(maxLoggingLength);
139             preferenceHash.put(SYSTEM_RESOURCE_LOGMAX, maxLoggingLength);
140             
141             Preference logPattern = new Preference(SYSTEM_RESOURCE_LOGPATTERN, Preference.PROPERTY_TYPE_TEXT, "%r [%t] %-5p %c - %m%n", prefs);
142             logPattern.setLongName("Log pattern");
143             logPattern.setDescription("This is the pattern which will be applied to each log message.");
144             preferences.addElement(logPattern);
145             preferenceHash.put(SYSTEM_RESOURCE_LOGPATTERN, logPattern);
146 
147         } catch (Exception e)
148         {
149             ExceptionManagerFactory.getExceptionManager().manageException(e, "Exception caught while initialising System Resource Manager preferences.");
150         }
151     }
152 
153     public Vector getPreferences()
154     {
155         return preferences;
156     }
157 
158     public Preference getPreference(String preferenceName)
159     {
160         return (Preference) instance.preferenceHash.get(preferenceName);
161     }
162 
163     public JPanel getPreferencesEditor()
164     {
165         return null;
166     }
167 
168     public ExpActionListener getGlobalListener()
169     {
170         return new ResourceManagementGlobalListener(menuItem);
171     }
172 
173 }
174 
175 class ResourceManagementGlobalListener implements ExpActionListener
176 {
177 
178     ResourceManagementPanel panel;
179     private JInternalFrame monitorInternalFrame = null;
180     private static Logger log = LogManager.getLogger(ResourceManagementGlobalListener.class);
181     private HashMap map;
182     private JCheckBoxMenuItem menuItem;
183 
184     public ResourceManagementGlobalListener(JCheckBoxMenuItem menuItem)
185     {
186         log.debug("Initialising ResourceManagementGlobalListener");
187         this.menuItem=menuItem;
188     }
189 
190     /***
191      * @returns a map;
192      */
193     public Map getListensFor()
194     {
195         if (map == null)
196         {
197             map = new HashMap();
198             map.put(ResourceManagementModule.MENU_CONFIGURE_RESOURCES, ResourceManagementModule.MENU_CONFIGURE_RESOURCES);
199         }
200         return map;
201     }
202 
203     /***
204      *  
205      */
206     public void actionPerformed(ActionEvent e)
207     {
208         try
209         {
210         	if (menuItem.isSelected())
211         	{
212         		menuItem.setText("Stop monitoring");
213         		panel = new ResourceManagementPanel();
214         		panel.startMonitoring();
215         		
216         		Preference heightOfIt = ResourceManagementModule.instance().getPreference(ResourceManagementModule.SYSTEM_RESOURCE_HEIGHT);
217         		Preference widthOfIt = ResourceManagementModule.instance().getPreference(ResourceManagementModule.SYSTEM_RESOURCE_WIDTH);
218         		
219         		((ExpFrame) Application.getApplicationFrame()).createBottomSplit(panel);
220         		
221         		//ExpInternalFrame frame = ((ExpFrame) Application.getApplicationFrame()).createDocumentFrame(panel, new Dimension(((Integer) widthOfIt.getValue()).intValue(), ((Integer) heightOfIt.getValue()).intValue()), "Resources",false);
222         		// frame.addSizePersistence(heightOfIt, widthOfIt);
223         		// frame.centreInParent();
224         	}
225         	else
226         	{
227         		panel.stopMonitoring();
228 				
229         		((ExpFrame) Application.getApplicationFrame()).removeBottomSplit();
230         		panel = null;
231         		menuItem.setText("Monitor");
232         	}
233 
234         } catch (Exception e1)
235         {
236             ExceptionManagerFactory.getExceptionManager().manageException(e1, "Exception caught while responding to event.");
237         }
238     }
239 }
240 
241 class ResourceManagementLocalListener implements ExpActionListener
242 {
243 
244     private static Logger log = LogManager.getLogger(ResourceManagementLocalListener.class);
245     private HashMap map;
246     private ResourceManagementPanel panel;
247 
248     /***
249      * Constructor for TextEditorLocalListener.
250      */
251     public ResourceManagementLocalListener(ResourceManagementPanel panel)
252     {
253         log.debug("Initialising ResourceManagementLocalListener");
254         this.panel = panel;
255         map = new HashMap();
256         map.put(ResourceManagementModule.MENU_CLOSE_RESOURCEMONITOR, ResourceManagementModule.MENU_CLOSE_RESOURCEMONITOR);
257 
258         /*
259          * Register this listener with the CompoundListener so that it listens
260          * for local events
261          */
262         ((ExpFrame) Application.getApplicationFrame()).getListener().addLocalActionListener(this, panel);
263     }
264 
265     /***
266      * @see package
267      *      com.explosion.expf.Interfaces.ExpActionListener#getListensFor()
268      */
269     public Map getListensFor()
270     {
271         return map;
272     }
273 
274     /***
275      * @see java.awt.event.ActionListener#actionPerformed(ActionEvent)
276      */
277     public void actionPerformed(ActionEvent e)
278     {
279         try
280         {
281             if (e.getActionCommand().equals(ResourceManagementModule.MENU_CLOSE_RESOURCEMONITOR))
282             {
283                 panel.stopMonitoring();
284                 ((ExpFrame) Application.getApplicationFrame()).closeFrameWithComponent(panel, ExpFrame.DOC_FRAME_LAYER);
285                 panel = null;
286             }
287         } catch (Exception ex)
288         {
289             com.explosion.utilities.exception.ExceptionManagerFactory.getExceptionManager().manageException(ex, "Exception caught while responding to SimpleProcess Event.");
290         }
291 
292     }
293 
294 }