1 package org.astrogrid.mySpace.mySpaceStatus;
2
3 import java.io.IOException;
4 import java.io.FileNotFoundException;
5 import java.util.Properties;
6
7 import java.io.FileInputStream;
8
9
10 import org.apache.axis.AxisProperties;
11
12 import org.apache.log4j.Logger;
13
14 /***
15 * The <code>MySpaceMessage</code> class holds messages from the MySpace
16 * system which are intended for eventual delivery to the User.
17 *
18 * <p>
19 * The <code>MySpaceMessage</code> class is intended for use with the
20 * <code>MySpaceStatus</code> class, which stores and returns
21 * <code>MySpaceMessage</code> objects. Each <code>MySpaceMessage</code>
22 * object comprises two components: a message and a type. The message
23 * is a human-readable string set by the MySpace system and ultimately
24 * intended for delivery to the User. The type indicates the type of
25 * event to which the message refers, coded as follows:
26 * </p>
27 * <ul>
28 * <li><code>"i"</code> - information (that is, nothing is amiss),</li>
29 * <li><code>"w"</code> - warning,</li>
30 * <li><code>"e"</code> - error.</li>
31 * </ul>
32 * <p>
33 * The class has a single constructor to which the message and type are
34 * passed as arguments. There are get methods for both the message and
35 * type and a <code>toString</code> method to produce a reasonable
36 * representation.
37 *
38 * @author A C Davenhall (Edinburgh)
39 * @edited C L QIN
40 * @version Iteration 2.
41 */
42
43 public class MySpaceMessage{
44
45 private static Logger logger = Logger.getLogger(MySpaceMessage.class);
46 private boolean DEBUG = true;
47 private String key;
48
49 private String errMessage;
50 private String message;
51 private String catalinaHome = AxisProperties.getProperty("catalina.home");
52 private String messageFilePath = catalinaHome+"/conf/astrogrid/mySpace/" +"statuscodes.lis";
53 private Properties p = new Properties();
54
55 /***
56 * Default constructor
57 */
58 public MySpaceMessage (String key){
59 this.key = key;
60 loadProperties();
61 }
62
63 /***
64 * For debugging purpers, this constructor pass error message
65 */
66 public MySpaceMessage ( String key, String errMessage ){
67 this.key = key;
68
69 this.errMessage = errMessage;
70 loadProperties();
71 }
72
73 /***
74 * Create a <code>MySpaceMessage</code>, setting the message and type.
75 */
76
77
78
79
80
81
82
83 /***
84 * This method reads properties from local property file.
85 */
86 private void loadProperties(){
87 try{
88 FileInputStream istream = new FileInputStream( messageFilePath );
89 p.load(istream);
90 istream.close();
91 }catch( FileNotFoundException fne ){
92 if (DEBUG) logger.error("Can't find property file for reading MySpaceMessage: "+fne.toString());
93 }
94 catch(IOException ioe){
95 if (DEBUG) logger.error("Unhandled IOException MySpaceMessage.readFromFile: "+ioe.toString());
96 }
97 }
98
99
100
101
102
103
104 /***
105 * Return the message string associated with the <code>MySpaceMessage</code>.
106 */
107
108 public String getMessage(){
109 if (DEBUG) logger.debug("MySpaceMessage.getMessage, CATALINAHOME: " +catalinaHome);
110 try{
111 message = p.getProperty(key);
112 }catch (Exception e){
113 if (DEBUG) logger.error("Error Reading Properties File: " +e);
114 message = e.toString();
115 return message;
116 }
117 return message;
118 }
119
120
121 /***
122 * This method is called when there is an exception thrown.
123 */
124 public String getMessage(String errMessage){
125 if (DEBUG) logger.debug("MySpaceMessage.getMessage(errMessage)");
126 try{
127 message = p.getProperty(key) + "Error!" +errMessage;
128 }catch (Exception e){
129 if (DEBUG) logger.error("ERROR Reading Properties File: " +e);
130 message = errMessage + e.toString();
131 return message;
132 }
133 return message;
134 }
135
136
137 /***
138 * Return the type associated with the <code>MySpaceMessage</code>.
139 */
140
141
142
143
144 /***
145 * Produce a reasonable string representation of a MySpaceMessage.
146 */
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210 }