View Javadoc

1   package com.explosion.expfmodules.monitoring;
2   
3   import java.awt.BorderLayout;
4   import java.awt.GridBagConstraints;
5   import java.awt.Insets;
6   import java.awt.event.ActionEvent;
7   import java.awt.event.ActionListener;
8   import java.util.Enumeration;
9   import java.util.List;
10  import java.util.Vector;
11  
12  import javax.swing.BorderFactory;
13  import javax.swing.JButton;
14  import javax.swing.JLabel;
15  import javax.swing.JPanel;
16  import javax.swing.JScrollPane;
17  import javax.swing.JTabbedPane;
18  import javax.swing.JTable;
19  import javax.swing.JViewport;
20  import javax.swing.table.DefaultTableModel;
21  
22  import org.apache.log4j.Appender;
23  import org.apache.log4j.LogManager;
24  import org.apache.log4j.Logger;
25  
26  import com.explosion.expf.Application;
27  import com.explosion.expf.Closeable;
28  import com.explosion.expf.ExpComponent;
29  import com.explosion.utilities.exception.ExceptionManagerFactory;
30  import com.explosion.utilities.process.ProcessMonitor;
31  import com.explosion.utilities.process.ProcessMonitoringStatusBar;
32  import com.explosion.utilities.process.threads.ProcessThread;
33  import com.explosion.utilities.process.threads.Updateable;
34  
35  
36  /* =============================================================================
37   *       
38   *     Copyright 2004 Stephen Cowx
39   *
40   *     Licensed under the Apache License, Version 2.0 (the "License");
41   *     you may not use this file except in compliance with the License.
42   *     You may obtain a copy of the License at
43   *
44   *     http://www.apache.org/licenses/LICENSE-2.0
45   *
46   *     Unless required by applicable law or agreed to in writing, software
47   *     distributed under the License is distributed on an "AS IS" BASIS,
48   *     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
49   *     See the License for the specific language governing permissions and
50   *     limitations under the License.
51   * 
52   * =============================================================================
53   */
54  
55  /***
56   * @author Stephen
57   * Created on Apr 25, 2004
58   */
59  public class ResourceManagementPanel extends JPanel implements Updateable, ExpComponent, Closeable
60  {
61  
62      private static Logger log = LogManager.getLogger(ResourceManagementPanel.class);
63      private ProcessMonitoringStatusBar statusBar;
64  
65      private JLabel systemFreeResult = new JLabel("");
66      private JLabel systemMaxResult = new JLabel("");
67      private JLabel systemTotalResult = new JLabel("");
68      private JPanel processPanel = new JPanel();
69      private JPanel memoryStatsPanel = new JPanel();
70      private JScrollPane logPane;
71      private JButton garbageCollectButton = new JButton("Run GC");
72      
73      private JTabbedPane tabs = new JTabbedPane();
74      private TableAppender appender = null;
75      private JTable processesTable;
76      private  Vector columns;
77  
78      private SystemMonitoringProcess process;
79  
80      public ResourceManagementPanel()
81      {
82          log.debug("Initialising ResourceManagementPanel");
83  
84          /* Listener */
85          garbageCollectButton.addActionListener(new ActionListener(){
86              public void actionPerformed(ActionEvent e)
87              {
88                  Runtime.getRuntime().gc();
89              }
90          });
91  
92          columns = new Vector();
93          columns.add("Status");
94          columns.add("Status text");
95          columns.add("Percent Complete");
96          
97          /* status bar */
98          statusBar = new ProcessMonitoringStatusBar(Application.getApplicationFrame());
99          statusBar.setProgressBarEnabled(false);
100         
101         /* Panel */
102         JPanel memoryPanel = createMemoryInfoPanel();
103         logPane = createLogPane();
104         createProcessesPanel();
105         
106         tabs.add("Statistics", memoryPanel);
107         tabs.add("Log", logPane);
108 
109         this.setLayout(new BorderLayout());
110         this.add(tabs, BorderLayout.CENTER);
111         
112     }
113 
114     /***
115      * Create the memory information panel
116      * 
117      * @return
118      */
119     private JPanel createMemoryInfoPanel()
120     {
121         /* Memory watcher */
122         JPanel memoryPanel = new JPanel();
123         
124         memoryStatsPanel.setBorder(BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(), "Memory Information"));
125         memoryStatsPanel.setLayout(new java.awt.GridBagLayout());
126         memoryStatsPanel.add(new JLabel("Free memory:"), 	new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.BOTH, new Insets(1, 1, 1, 1), 0, 0));
127         memoryStatsPanel.add(systemFreeResult, 				new GridBagConstraints(1, 0, 1, 1, 1.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.BOTH, new Insets(1, 1, 1, 1), 0, 0));
128         memoryStatsPanel.add(new JLabel("Total memory:"), 	new GridBagConstraints(0, 1, 1, 1, 0.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.BOTH, new Insets(1, 1, 1, 1), 0, 0));
129         memoryStatsPanel.add(systemTotalResult,			 	new GridBagConstraints(1, 1, 1, 1, 1.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.BOTH, new Insets(1, 1, 1, 1), 0, 0));
130         memoryStatsPanel.add(new JLabel("Max memory:"), 	new GridBagConstraints(0, 2, 1, 1, 0.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.BOTH, new Insets(1, 1, 1, 1), 0, 0));
131         memoryStatsPanel.add(systemMaxResult, 				new GridBagConstraints(1, 2, 1, 1, 1.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.BOTH, new Insets(1, 1, 1, 1), 0, 0));
132         memoryStatsPanel.add(garbageCollectButton, 			new GridBagConstraints(0, 3, 2, 1, 0.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(1, 1, 1, 1), 0, 0));
133         
134         memoryPanel.setLayout(new java.awt.GridBagLayout());
135         memoryPanel.add(memoryStatsPanel, 				new GridBagConstraints(0, 0, 1, 1, 1.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.BOTH, new Insets(1, 1, 1, 1), 0, 0));
136         memoryPanel.add(processPanel, 					new GridBagConstraints(0, 1, 1, 1, 1.0, 1.0, GridBagConstraints.WEST, GridBagConstraints.BOTH, new Insets(1, 1, 1, 1), 0, 0));
137                 
138         processPanel.setBorder(BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(), "Running processes"));
139         
140         return memoryPanel;
141     }
142 
143     /***
144      * Create the logging pane
145      * 
146      * @return
147      */
148     private JScrollPane createLogPane()
149     {
150         logPane = new JScrollPane();
151         appender = new TableAppender(logPane);
152         return logPane;
153     }
154     
155     private void createProcessesPanel()
156     {
157         processPanel.setLayout(new BorderLayout());
158         JScrollPane processesScroller = new JScrollPane();
159         processesTable = new JTable();
160         processesTable.setModel(new DefaultTableModel(new Vector(), columns));
161         processesScroller.getViewport().add(processesTable);
162         processesScroller.getViewport().setScrollMode(JViewport.BLIT_SCROLL_MODE);
163         processPanel.add(processesScroller, BorderLayout.CENTER);
164     }
165     
166     /***
167      * Loggers come and go, we need to keep this up to date
168      * @param appender
169      */
170     private void updateLoggingConfig(Appender appender)
171     {
172         LogManager.getRootLogger().addAppender(appender);
173         LogManager.getRootLogger().setAdditivity(false);
174         Enumeration ener = LogManager.getCurrentLoggers();
175         while (ener.hasMoreElements())
176         {
177             Logger logger = ((Logger) ener.nextElement());
178             logger.addAppender(appender);
179             logger.setAdditivity(false);
180             
181         }        
182     }
183 
184     public void startMonitoring()
185     {
186         try
187         {
188             log.debug("startMonitoring");
189             process = new SystemMonitoringProcess(this);
190             statusBar.setProcessToMonitor(process);
191             statusBar.startProcess();
192 
193         } catch (Exception e)
194         {
195             ExceptionManagerFactory.getExceptionManager().manageException(e, "Exception caught while initialising SystemMonitoringProcess");
196         }
197     }
198 
199     public void stopMonitoring()
200     {
201         log.debug("stopMonitoring");
202         try {
203 			statusBar.stopNoConfirm();
204 		} catch (InterruptedException e) {
205 			log.error("Interrupted",e);
206 		}
207         LogManager.getRootLogger().removeAppender(appender);
208     }
209 
210     public void update(Object object)
211     {
212         updateLoggingConfig(this.appender);
213         
214         systemFreeResult.setText(": " + getIt(Runtime.getRuntime().freeMemory()) + " mb");
215         systemMaxResult.setText(": " + getIt(Runtime.getRuntime().maxMemory()) + " mb");
216         systemTotalResult.setText(": " + getIt(Runtime.getRuntime().totalMemory()) + " mb");
217         
218         while(((DefaultTableModel)processesTable.getModel()).getRowCount() > 0)
219             ((DefaultTableModel)processesTable.getModel()).removeRow(0);
220         
221         List list = ProcessMonitor.getUserProcessThreads();
222         restart:
223         for (int i=0; i<list.size(); i++)
224         {
225             Vector row = new Vector();
226             ProcessThread p;
227             try
228             {
229                 p = (ProcessThread) list.get(i);
230             }
231             catch (IndexOutOfBoundsException e)
232             {
233                 //process have been removed from the list now, we need to restart from scratch
234                 continue restart;
235             }
236             row.add(Integer.toString(p.getStatus()));
237             row.add(p.getProcess().getStatusText());
238             row.add(p.getProcess().getPercentComplete() + " %");
239             ((DefaultTableModel)processesTable.getModel()).addRow(row);
240         }
241         
242         
243     }
244 
245     private String getIt(long num)
246     {
247         return Double.toString(((double) num / 1024) / 1024);
248     }
249 
250     public void applyPreferences()
251     {
252        boolean selected = false;
253        if (tabs.getSelectedIndex() > 0)
254            selected = true;
255        
256        tabs.remove(logPane); 
257        createLogPane();  
258        tabs.add("Log", logPane);
259        if (selected)
260            tabs.setSelectedIndex(1);
261     }
262 
263     public String getCloseCommand()
264     {
265         return ResourceManagementModule.MENU_CLOSE_RESOURCEMONITOR;
266     }
267 }