ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/i-scream/projects/cms/source/server/uk/org/iscream/cms/server/client/WebFeeder.java
(Generate patch)

Comparing projects/cms/source/server/uk/org/iscream/cms/server/client/WebFeeder.java (file contents):
Revision 1.2 by tdb, Thu Mar 8 20:57:35 2001 UTC vs.
Revision 1.8 by tdb, Thu Mar 15 03:26:43 2001 UTC

# Line 1 | Line 1
1   //---PACKAGE DECLARATION---
2 < package uk.ac.ukc.iscream.client;
2 > package uk.org.iscream.client;
3  
4   //---IMPORTS---
5 < import uk.ac.ukc.iscream.componentmanager.*;
6 < import uk.ac.ukc.iscream.core.*;
7 < import uk.ac.ukc.iscream.util.*;
5 > import uk.org.iscream.componentmanager.*;
6 > import uk.org.iscream.core.*;
7 > import uk.org.iscream.util.*;
8   import java.io.*;
9  
10   /**
# Line 39 | Line 39 | public class WebFeeder {
39  
40      private WebFeeder() {
41          // do something, or nothing.. but must be private
42 +        // need to clean up all the alerts
43 +        // don't need to cleanup latest data
44      }
45  
46   //---PUBLIC METHODS---
# Line 53 | Line 55 | public class WebFeeder {
55      // that to a file.
56      
57      public void receiveXMLPacket(XMLPacket packet) {
58 +        String packetType = packet.getParam("packet.attributes.type");
59 +        if(packetType == null || !packetType.equals("data")) {
60 +            // bail out, without warning
61 +            // this is probably a heartbeat or similar
62 +            return;
63 +        }
64          // get config proxy
65          ConfigurationProxy cp = ConfigurationProxy.getInstance();
66          // get file locations
# Line 68 | Line 76 | public class WebFeeder {
76          }
77          // get raw data
78          String data = packet.printAll();
79 <        String hostname = packet.getParam("packet.attributes.host_name");
79 >        String hostname = packet.getParam("packet.attributes.machine_name");
80          // set paths
81          String destDir = rootPath+"/"+latestSubDir+"/"+hostname;
82          String destFile = destDir+"/"+latestFileName;
83 <        // write data
83 >        // try to create directory
84          File outDir = new File(destDir);
85 <        if(outDir.mkdirs()) {
86 <            File outFile = new File(destFile);
87 <            if(outFile.canWrite()) {
88 <                PrintWriter out;
89 <                try {
90 <                    out = new PrintWriter(new FileWriter(outFile));
83 <                    out.println(data);
84 <                    out.close();
85 <                } catch (IOException e) {
86 <                    _logger.write(this.toString(), Logger.ERROR, "Failed to write file: "+e);
87 <                }
85 >        if(!outDir.exists()) {
86 >            if(!outDir.mkdirs()) {
87 >                // didn't exist, and we couldn't make it
88 >                _logger.write(this.toString(), Logger.ERROR, "Failed to create directory: "+outDir.getPath());
89 >                // bail out
90 >                return;
91              }
92 <            else {
93 <                _logger.write(this.toString(), Logger.ERROR, "File not readable: "+outFile.getPath());
92 >        }
93 >        // directory has been made, check file exists
94 >        File outFile = new File(destFile);
95 >        if(!outFile.exists()) {
96 >            try {
97 >                outFile.createNewFile();
98 >            } catch (IOException e) {
99 >                _logger.write(this.toString(), Logger.ERROR, "Failed to create file: "+e);
100 >                // bail out
101 >                return;
102              }
103          }
104 +        // file should now exist
105 +        if(outFile.canWrite()) {
106 +            PrintWriter out;
107 +            try {
108 +                out = new PrintWriter(new FileWriter(outFile));
109 +                out.println(data);
110 +                out.close();
111 +            } catch (IOException e) {
112 +                _logger.write(this.toString(), Logger.ERROR, "Failed to write file: "+e);
113 +            }
114 +        }
115          else {
116 <            _logger.write(this.toString(), Logger.ERROR, "Failed to create directory: "+outDir.getPath());
116 >            _logger.write(this.toString(), Logger.ERROR, "File not writeable: "+outFile.getPath());
117          }
118      }
119      
120      public void receiveAlert(Alert alert) {
121 <        // process and save
121 >        // get config proxy
122 >        ConfigurationProxy cp = ConfigurationProxy.getInstance();
123 >        // get file locations
124 >        String rootPath, alertSubDir, alertFileName;
125 >        try {
126 >            rootPath = cp.getProperty("WebFeeder", "WebFeeder.rootPath");
127 >            alertSubDir = cp.getProperty("WebFeeder", "WebFeeder.alertSubDir");
128 >            alertFileName = cp.getProperty("WebFeeder", "WebFeeder.alertFileName");
129 >        } catch (PropertyNotFoundException e) {
130 >            _logger.write(this.toString(), Logger.ERROR, "Failed to get config for Alert Data: "+e);
131 >            // bail out
132 >            return;
133 >        }
134 >        // get raw data
135 >        String data = alert.printAll();
136 >        String hostname = alert.getSource();
137 >        // set paths
138 >        String destDir = rootPath+"/"+alertSubDir+"/"+hostname;
139 >        String destFile = destDir+"/"+alertFileName+"."+String.valueOf(alert.getInitialAlertTime());
140 >        // try to create directory
141 >        File outDir = new File(destDir);
142 >        if(!outDir.exists()) {
143 >            if(!outDir.mkdirs()) {
144 >                // didn't exist, and we couldn't make it
145 >                _logger.write(this.toString(), Logger.ERROR, "Failed to create directory: "+outDir.getPath());
146 >                // bail out
147 >                return;
148 >            }
149 >        }
150 >        // directory has been made, check file exists
151 >        File outFile = new File(destFile);
152 >        if(!outFile.exists()) {
153 >            try {
154 >                outFile.createNewFile();
155 >            } catch (IOException e) {
156 >                _logger.write(this.toString(), Logger.ERROR, "Failed to create file: "+e);
157 >                // bail out
158 >                return;
159 >            }
160 >        }
161 >        // file should now exist
162 >        if(outFile.canWrite()) {
163 >            PrintWriter out;
164 >            try {
165 >                out = new PrintWriter(new FileWriter(outFile));
166 >                out.println(data);
167 >                out.close();
168 >            } catch (IOException e) {
169 >                _logger.write(this.toString(), Logger.ERROR, "Failed to write file: "+e);
170 >            }
171 >        }
172 >        else {
173 >            _logger.write(this.toString(), Logger.ERROR, "File not writeable: "+outFile.getPath());
174 >        }
175      }
176      
177      /**
178       * Overrides the {@link java.lang.Object#toString() Object.toString()}
179       * method to provide clean logging (every class should have this).
180       *
181 <     * This uses the uk.ac.ukc.iscream.util.FormatName class
181 >     * This uses the uk.org.iscream.util.FormatName class
182       * to format the toString()
183       *
184       * @return the name of this class and its CVS revision

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines