Home >> All |
Source code: Freenet/MessageFactory.java
1 package Freenet; 2 3 import java.lang.reflect.*; 4 import java.io.*; 5 import java.util.Hashtable.*; 6 import Freenet.support.*; 7 8 /* 9 This code is part of the Java Adaptive Network Client by Ian Clarke. 10 It is distributed under the GNU Public Licence (GPL) version 2. See 11 http://www.gnu.org/ for further details of the GPL. 12 */ 13 14 /** 15 * MessageFactory.java 16 * 17 * This object takes a RawMessage and converts it to the appropriate 18 * subclass of the Message class depending on the message. 19 * 20 * @author Brandon Wiley (blanu@uts.cc.utexas.edu) 21 * @author Ian Clarke (I.Clarke@strs.co.uk) 22 **/ 23 24 public class MessageFactory 25 { 26 public static Message toMessage(RawMessage r) throws InvalidMessageException 27 { 28 try 29 { 30 Class c=Loader.load(r.messageType); 31 Class[] carr={Freenet.RawMessage.class}; 32 Object[] oarr={r}; 33 Constructor con=c.getConstructor(carr); 34 Message m=(Message)con.newInstance(oarr); 35 return m; 36 37 } catch(InvocationTargetException e) { 38 if (e.getTargetException() instanceof InvalidMessageException) 39 throw (InvalidMessageException) e.getTargetException(); 40 else { 41 Logger.log("MessageFactory.java","Message constructor threw exception:",Logger.ERROR); 42 e.getTargetException().printStackTrace(Logger.Lout); 43 throw new InvalidMessageException("Message constructor threw unkown exception."); 44 } 45 } 46 catch(ClassNotFoundException e) { 47 Logger.log("MessageFactory.java","Unknown messagetype - "+e,Logger.NORMAL); 48 throw new InvalidMessageException("Message type not recognized"); 49 } 50 catch(Exception e) 51 { 52 e.printStackTrace(Logger.Lout); 53 throw new InvalidMessageException("Unknown Error"); 54 } 55 56 } 57 } 58 59 60 61 62 63 64