Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: org/altara/mars/plugin/csvlog/CSVLogPlugin.java


1   /* MARS CSV Logger Plugin
2      Copyright (C) 2002 Leapfrog Research & Development, LLC
3   
4     This program is free software; you can redistribute it and/or
5     modify it under the terms of the GNU General Public License
6     as published by the Free Software Foundation; either version 2
7     of the License, or (at your option) any later version.
8   
9     This program is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12    GNU General Public License for more details.
13    
14    You should have received a copy of the GNU General Public License
15    along with this program; if not, it is available at 
16    http:///www.gnu.org/copyleft/gpl.html, or by writing to the
17    Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18    Boston, MA  02111-1307, USA.
19  */
20  
21  package org.altara.mars.plugin.csvlog;
22  
23  import org.jdom.*;
24  import org.altara.util.*;
25  import org.altara.mars.*;
26  import org.altara.mars.engine.*;
27  import org.altara.mars.plugin.*;
28  import org.altara.mars.swingui.*;
29  import java.util.*;
30  import java.text.*;
31  import java.net.*;
32  import java.io.*;
33  import java.awt.*;
34  import java.awt.event.*;
35  import javax.swing.*;
36  import javax.swing.event.*;
37  
38  public class CSVLogPlugin implements Plugin, ProbeListener {
39  
40      private boolean enabled;
41      private String logfile;
42  
43      private SimpleDateFormat format;
44  
45      private PrintWriter writer;
46  
47      public CSVLogPlugin() {
48          format = new SimpleDateFormat("yyyy/MM/dd, HH:mm:ss.SSS");
49          enabled = false;
50          logfile = "";
51          writer = null;
52      }
53  
54      public String getElementName() {
55          return "csvlog";
56      }
57  
58      public String getDisplayName() {
59          return "CSV Logger";
60      }
61  
62      public void probeRun(ProbeEvent pe) {
63          if (writer != null) {
64              Service service = pe.getService();
65              Status status = pe.getNewStatus();
66              Host host = service.getHost();
67              String stamp = format.format(new Date());
68              writer.println( stamp+", "+
69                              host.getName()+", "+
70                              service.getName()+", "+
71                              host.getAddress().getHostName()+", "+
72                              service.getPort()+", "+
73                              service.getSvcType()+", "+
74                              status.toString()+", "+
75                              status.getResponseTime());
76          }
77      }
78  
79      private void startWriter() throws IOException {
80          writer = new PrintWriter(new FileWriter(logfile,true),true);
81          enabled = true;
82      }
83  
84      private void stopWriter() {
85          if (writer != null) writer.close();
86          writer = null;
87          enabled = false;
88      }
89  
90    /* ------------------------------------------------------
91      XML config serialization
92    ------------------------------------------------------ */
93  
94      public Element getConfig() {
95          Element out = new Element(getElementName(),MarsModel.NAMESPACE);
96          out.setAttribute("enabled",String.valueOf(enabled));
97          out.setAttribute("logfile",logfile);
98          return out;
99      }
100 
101     public void setConfig(Element in) throws InvalidDocumentException,
102             IOException {
103          boolean enabled;
104         String logfile;
105 
106     String enabledStr = in.getAttributeValue("enabled");
107     if (enabledStr == null) 
108       throw new InvalidDocumentException("Missing CSV enabled");
109     enabled = Boolean.valueOf(enabledStr).booleanValue();
110 
111         logfile = in.getAttributeValue("logfile");
112         if (logfile == null)
113             throw new InvalidDocumentException("Missing CSV logfile");
114 
115         this.logfile = logfile;
116         if (enabled) startWriter();
117     }
118 
119   /* ------------------------------------------------------
120     Editor
121   ------------------------------------------------------ */
122 
123     public Editor getEditor() {
124         return new CSVLogEditor();
125     }
126 
127     private class CSVLogEditor extends JPanel implements Editor {
128 
129     private JCheckBox enabledBox;
130         private JTextField logfileField;
131         private JButton chooseBtn;
132 
133         private CSVLogEditor() {
134             // create editable fields
135             enabledBox = new JCheckBox("Enabled",enabled);
136             logfileField = new JTextField(logfile,30);
137 
138             // create chooser button
139             chooseBtn = new JButton(new AbstractAction("Choose...") {
140                 public void actionPerformed(ActionEvent ae) {
141                     selectLogfile();
142                 }
143             });
144 
145             // set field enabled states
146             logfileField.setEnabled(enabled);
147             chooseBtn.setEnabled(enabled);
148 
149             // set up an action listener on the checkbox
150       enabledBox.addActionListener(new ActionListener() {
151         public void actionPerformed(ActionEvent ae) {
152                     logfileField.setEnabled(enabledBox.isSelected());
153                     chooseBtn.setEnabled(enabledBox.isSelected());
154         }
155       });
156 
157             // set up the layout manager
158       setLayout(new GridBagLayout());
159       GridBagConstraints c = new GridBagConstraints();
160       c.anchor = c.NORTHWEST; c.fill = c.BOTH;
161       c.weightx = 1.0; c.weighty = 0.0;
162       c.insets = new Insets(4,4,6,4);
163   
164       // lay out the panel
165       c.gridx = 1; c.gridy = 0; c.gridwidth = 1; c.gridheight = 1;
166       add(enabledBox,c);
167       c.gridx = 0; c.gridy = 1; c.gridwidth = 1; c.gridheight = 1;
168       add(new JLabel("Log file"),c);
169       c.gridx = 1; c.gridy = 1; c.gridwidth = 1; c.gridheight = 1;
170       add(logfileField,c);
171       c.anchor = c.SOUTHEAST; c.fill = c.NONE;
172       c.gridx = 1; c.gridy = 2; c.gridwidth = 1; c.gridheight = 1;
173             add(chooseBtn,c);
174         }
175 
176         public void commit() throws IOException {
177             logfile = logfileField.getText();
178             if (enabledBox.isSelected()) { 
179                 startWriter();
180             } else {
181                 stopWriter();
182             }
183         }
184 
185     public String getEditorTitle() {
186       return "Configure CSV Logger";
187     }
188       private void selectLogfile() {
189         // create a file chooser
190         JFileChooser chooser = new JFileChooser();
191         // ask for a file to open
192         int option = chooser.showSaveDialog(this);
193         if (option == JFileChooser.APPROVE_OPTION) {
194           // get pathname and stick it in the field
195           logfileField.setText(
196                     chooser.getSelectedFile().getAbsolutePath());
197         } 
198         }
199   }
200 }