View Javadoc

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   //axis
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;     // Message key.
48      //private String type;        // Type of message: "i", "w" or "e".
49      private String errMessage;  // exception message passed in.
50      private String message;     // 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      	//this.type = type;
69      	this.errMessage = errMessage;
70      	loadProperties();
71      }
72  
73  /***
74   * Create a <code>MySpaceMessage</code>, setting the message and type.
75   */
76  /*
77      public MySpaceMessage (String key, String type){
78      	this.key = key;
79          this.type = type;
80          loadProperties();
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 // Get methods.
101 //
102 // The MySpaceMessage class has a get method for every member variable.
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     //public String getType(){
141    // 	return type;
142 //    }
143 
144 /***
145  * Produce a reasonable string representation of a MySpaceMessage.
146  */
147 /*
148    public String toString(){
149    	String returnString = "";
150 
151       if (type.equals("i"))
152       {  returnString = "!Info: " + key;
153       }
154       else if (type.equals("w"))
155       {  returnString = "!Warning: " + key;
156       }
157       else if (type.equals("e")) 
158       {  returnString = "!Error: " + key;
159       }
160       else
161       {  returnString = "!Unknown: " + key;
162       }
163 
164       return returnString;
165    }
166    */
167    /*
168    public static String readFromFile( File file )throws Exception {
169    
170    	FileReader fileReader = null;
171    	try{
172 		if (file == null || !file.exists()) {
173 			throw new IOException("File does not exist");
174 		}
175 		//open file to read from
176 		fileReader = new FileReader(file);
177 
178 		StringBuffer strbufFileContent = new StringBuffer(8192);
179 		char charFileText[] = new char[2048];
180 
181 		int iTotalCharactersRead = 0;
182 		int iCharactersRead = 0;
183 		    
184 		while((iCharactersRead = fileReader.read(charFileText,0,2048)) != -1){
185 			//add number of characters read to the total read
186 			iTotalCharactersRead += iCharactersRead;
187 
188 			//add the characters read to the buffer
189 			strbufFileContent.append(charFileText);
190 		}
191 
192 		//size buffer to fit data
193 		strbufFileContent.setLength(iTotalCharactersRead);
194 
195 		//return the data as a String
196 		return strbufFileContent.toString();
197    	}catch (Exception e) {
198    		throw e;
199    	}finally{
200    		try{
201    			if(fileReader != null){
202    				fileReader.close();
203    			}
204    		}catch(Exception e){
205    			throw e;
206    		}
207    	}
208 
209    }*/
210 }