1 29 30 package com.caucho.soa.encoding; 31 32 import com.caucho.hessian.io.AbstractHessianOutput; 33 import com.caucho.hessian.io.Hessian2Input; 34 import com.caucho.hessian.io.Hessian2Output; 35 import com.caucho.hessian.io.HessianOutput; 36 import com.caucho.hessian.io.SerializerFactory; 37 import com.caucho.hessian.server.HessianSkeleton; 38 import com.caucho.server.util.CauchoSystem; 39 import com.caucho.services.server.GenericService; 40 import com.caucho.util.L10N; 41 42 import javax.annotation.PostConstruct; 43 import javax.jws.WebService; 44 import java.io.IOException ; 45 import java.io.InputStream ; 46 import java.io.OutputStream ; 47 import java.util.logging.Level ; 48 import java.util.logging.Logger ; 49 50 53 public class HessianEncoding implements ServiceEncoding { 54 protected static Logger log 55 = Logger.getLogger(HessianEncoding.class.getName()); 56 private static final L10N L = new L10N(HessianEncoding.class); 57 58 private Object _serviceImpl; 59 60 private HessianSkeleton _skeleton; 61 62 private SerializerFactory _serializerFactory; 63 64 67 public void setService(Object serviceImpl) 68 { 69 _serviceImpl = serviceImpl; 70 } 71 72 75 public void setSerializerFactory(SerializerFactory factory) 76 { 77 _serializerFactory = factory; 78 } 79 80 83 public SerializerFactory getSerializerFactory() 84 { 85 if (_serializerFactory == null) 86 _serializerFactory = new SerializerFactory(); 87 88 return _serializerFactory; 89 } 90 91 94 public void setSendCollectionType(boolean sendType) 95 { 96 getSerializerFactory().setSendCollectionType(sendType); 97 } 98 99 @PostConstruct 100 public void init() 101 throws Exception 102 { 103 if (_serviceImpl == null) 104 _serviceImpl = this; 105 106 Class api = null; 107 108 if (_serviceImpl != null) { 109 api = findWebServiceEndpointInterface(_serviceImpl.getClass()); 110 111 if (api == null) 112 api = findRemoteAPI(_serviceImpl.getClass()); 113 114 if (api == null) 115 api = _serviceImpl.getClass(); 116 } 117 118 _skeleton = new HessianSkeleton(_serviceImpl, api); 119 } 120 121 private Class findWebServiceEndpointInterface(Class implClass) 122 throws ClassNotFoundException 123 { 124 if (implClass.isAnnotationPresent(javax.jws.WebService.class)) { 125 WebService webServiceAnnotation = 126 (WebService) implClass.getAnnotation(javax.jws.WebService.class); 127 128 String endpoint = webServiceAnnotation.endpointInterface(); 129 if (endpoint != null && ! "".equals(endpoint)) 130 return CauchoSystem.loadClass(endpoint); 131 } 132 133 return null; 134 } 135 136 private Class findRemoteAPI(Class implClass) 137 { 138 if (implClass == null || implClass.equals(GenericService.class)) 139 return null; 140 141 Class []interfaces = implClass.getInterfaces(); 142 143 if (interfaces.length == 1) 144 return interfaces[0]; 145 146 return findRemoteAPI(implClass.getSuperclass()); 147 } 148 149 public void invoke(InputStream is, OutputStream os) 150 { 151 try { 152 Hessian2Input in = new Hessian2Input(is); 153 AbstractHessianOutput out; 154 155 SerializerFactory serializerFactory = getSerializerFactory(); 156 157 in.setSerializerFactory(serializerFactory); 158 159 int code = in.read(); 160 161 if (code != 'c') { 162 throw new IOException (L.l("expected 'c' in hessian input at {0}", 164 code)); 165 } 166 167 int major = in.read(); 168 int minor = in.read(); 169 170 if (major >= 2) 171 out = new Hessian2Output(os); 172 else 173 out = new HessianOutput(os); 174 175 out.setSerializerFactory(serializerFactory); 176 177 if (_skeleton == null) 178 throw new Exception ("skeleton is null!"); 179 180 _skeleton.invoke(in, out); 181 182 out.close(); 183 } catch (IOException e) { 184 log.log(Level.INFO, L.l("Unable to process request: "), e); 185 } catch (Throwable e) { 186 log.log(Level.INFO, L.l("Unable to process request: "), e); 187 } 188 } 189 } 190 | Popular Tags |