added validation method to perform minimal check that message class implementation...
[vamsas.git] / src / uk / ac / vamsas / client / picking / Message.java
1 package uk.ac.vamsas.client.picking;\r
2 \r
3 /**\r
4  * Abstract base class for all message types supported by the picking API.\r
5  */\r
6 public abstract class Message\r
7 {\r
8         protected String message;\r
9         \r
10         /**\r
11          * Constructs a new message.\r
12          */\r
13         protected Message()\r
14         {\r
15 \r
16         }\r
17         \r
18         /**\r
19          * Returns the raw message content as a string.\r
20          * @return the raw message content as a string\r
21          */\r
22         public String getRawMessage()\r
23                 { return message; }\r
24         /**\r
25          * compare the on-wire message content of the given message to this.\r
26          * @param msg\r
27          * @return true if message content is equal\r
28          */\r
29         public boolean equals(Message msg)\r
30         {\r
31           return message.equals(msg.getRawMessage());\r
32         }\r
33         /**\r
34          * Test consistence of a Message class implementation. This method throws an error if\r
35          * the message object cannot be parsed into another instance of the same object by\r
36          * invoking the MessageImpl(String this.getRawMessage()) constructor or that\r
37          * newinstance.getRawMessage != this.getRawMessage \r
38          */\r
39   public void validate() {\r
40     try {\r
41       java.lang.reflect.Constructor msgcons = this.getClass().getConstructor(new Class[] { String.class });\r
42       if (msgcons==null)\r
43       {\r
44         throw new Exception("No "+this.getClass().getName()+"(String rawmessage) constructor.");\r
45       }\r
46       Message instance = (Message) msgcons.newInstance(new Object[] { this.getRawMessage() });\r
47       if (!instance.getRawMessage().equals(getRawMessage()))\r
48       {\r
49         throw new Error("Raw Message Content does not match :\nInitial Message:"+getRawMessage()+"\nParsed and regnerated as :\n"+instance.getRawMessage()+"\n");\r
50       }\r
51     } catch (Exception e)\r
52     {\r
53       throw new Error("Message implementation broken for "+this.getClass(),e);\r
54     }\r
55   }\r
56 \r
57 }