Main Page | Packages | Class Hierarchy | Alphabetical List | Class List | File List | Class Members | Related Pages

AbstractObjectManager.java

00001 /*
00002  * AbstractObjectManager.java
00003  *
00004  * Created on 16 September 2003, 11:23
00005  */
00006 
00007 package Sct;
00008 import java.io.*;
00009 import java.lang.reflect.*;
00010 
00017 public abstract class AbstractObjectManager implements ObjectManager {
00018     
00023     public Streamable readObject(IStream s, String name)  throws IOException{
00024         String className = readClassName(s);
00025         return readObject(s, name, className);
00026     }
00027     
00031     public Streamable readObject(IStream s, String name, String className) throws IOException{
00032         Streamable ob = createObject(s, className);        
00033         return ob;
00034     }
00035     
00040     public void writeObject(OStream s, String name, Streamable object, boolean writeClassName) throws IOException{
00041         if (writeClassName) writeClassName(s,  object.getClassName());
00042         object.write(s, this);
00043     }
00044     
00045     
00050     protected Streamable createObject(IStream s, String className) throws IOException {
00051         ClassLoader cl = this.getClass().getClassLoader();
00052         try {                                    
00053             Class c = cl.loadClass(className);
00054                                        
00055             try {
00056                 Class[] argTypes = {IStream.class, ObjectManager.class};
00057                 Object[] args = {s, this};
00058                 Method m = c.getMethod("read", argTypes);
00059                 if (!m.isAccessible()) {
00060                     try {
00061                         m.setAccessible(true);
00062                     } catch (SecurityException se) {
00063                         InvalidClassException ie = new InvalidClassException(className, "No accessible static method for class: " + className);
00064                         ie.initCause(se);
00065                         throw ie;
00066                     }
00067                 }
00068                 return (Streamable)m.invoke(null, args);
00069             
00070             } catch (NoSuchMethodException e) {
00071                 InvalidClassException ie = new InvalidClassException(className, "No appropriate constructor or static method for class: " + className);
00072                 ie.initCause(e);
00073                 throw ie;
00074             } 
00075             catch (IllegalAccessException e) {
00076                 InvalidClassException ie = new InvalidClassException(className, "No appropriate static method for class: " + className);
00077                 ie.initCause(e);
00078                 throw ie;
00079             }
00080             catch (IllegalArgumentException e) {
00081                 throw new Error("Internal bug creating object", e);                
00082             }            
00083             catch (NullPointerException e) {
00084                 InvalidClassException ie = new InvalidClassException(className, "Method is not static for class: " + className);
00085                 ie.initCause(e);
00086                 throw ie;                
00087             }            
00088             catch (InvocationTargetException e) {
00089                 if (e.getCause() instanceof IOException) throw (IOException)e.getCause();
00090                 if (e.getCause() instanceof RuntimeException) throw (RuntimeException)e.getCause();                
00091                 throw new Error("Bug creating: " + className + " - threw unexpected exception", e.getCause());            
00092             }                       
00093             
00094         } catch (ClassNotFoundException cnfe) { 
00095             InvalidClassException ie = new InvalidClassException(className, "Couldn't find class: " + className);
00096             ie.initCause(cnfe);
00097             throw ie;
00098         }
00099     }
00100     
00107     protected String readClassName(IStream in) throws IOException {
00108         String className = in.readString("ClassName");
00109         if (className.startsWith("SctClassName:")) {
00110             return Name.convertFromC(className.substring(13));
00111         } else {
00112             throw new InvalidClassException(className, "Stream does not contain a className");
00113         }
00114     }
00115     
00120     protected void writeClassName(OStream out, String className) throws IOException {
00121         out.writeString("ClassName", "SctClassName:"+ Name.convertToC(className));
00122     }
00123    
00124     
00125 }

Generated on Thu Jul 15 09:55:38 2004 for SCT DAQ/DCS Software - Java by doxygen 1.3.5