1 19 20 package org.apache.cayenne.remote.hessian; 21 22 import java.lang.reflect.Constructor ; 23 import java.lang.reflect.Method ; 24 25 import org.apache.cayenne.CayenneRuntimeException; 26 import org.apache.cayenne.map.EntityResolver; 27 import org.apache.cayenne.util.Util; 28 29 import com.caucho.hessian.io.AbstractSerializerFactory; 30 import com.caucho.hessian.io.SerializerFactory; 31 32 38 public class HessianConfig { 39 40 52 public static SerializerFactory createFactory( 53 String [] factoryNames, 54 EntityResolver resolver) { 55 56 SerializerFactory factory = new SerializerFactory(); 57 58 if (factoryNames != null && factoryNames.length > 0) { 59 60 for (int i = 0; i < factoryNames.length; i++) { 61 62 try { 63 factory.addFactory(loadFactory(factoryNames[i], resolver)); 64 } 65 catch (Exception e) { 66 throw new CayenneRuntimeException("Error configuring factory class " 67 + factoryNames[i], e); 68 } 69 } 70 } 71 72 return factory; 73 } 74 75 static AbstractSerializerFactory loadFactory( 76 String factoryName, 77 EntityResolver resolver) throws Exception { 78 79 ClassLoader loader = Thread.currentThread().getContextClassLoader(); 80 Class factoryClass = Class.forName(factoryName, true, loader); 81 82 if (!AbstractSerializerFactory.class.isAssignableFrom(factoryClass)) { 83 throw new IllegalArgumentException (factoryClass 84 + " is not a AbstractSerializerFactory"); 85 } 86 87 Constructor c = factoryClass.getDeclaredConstructor(new Class [] {}); 88 if (!Util.isAccessible(c)) { 89 c.setAccessible(true); 90 } 91 92 AbstractSerializerFactory object = (AbstractSerializerFactory) c 93 .newInstance(null); 94 95 if (resolver != null) { 96 try { 97 98 Method setter = factoryClass.getDeclaredMethod( 99 "setEntityResolver", 100 new Class [] { 101 EntityResolver.class 102 }); 103 104 if (!Util.isAccessible(setter)) { 105 setter.setAccessible(true); 106 } 107 108 setter.invoke(object, new Object [] { 109 resolver 110 }); 111 } 112 catch (Exception e) { 113 } 115 } 116 117 return object; 118 } 119 120 } 121 | Popular Tags |