KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > soa > encoding > HessianEncoding


1 /*
2  * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  *
23  * Free Software Foundation, Inc.
24  * 59 Temple Place, Suite 330
25  * Boston, MA 02111-1307 USA
26  *
27  * @author Emil Ong
28  */

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 JavaDoc;
45 import java.io.InputStream JavaDoc;
46 import java.io.OutputStream JavaDoc;
47 import java.util.logging.Level JavaDoc;
48 import java.util.logging.Logger JavaDoc;
49
50 /**
51  * Invokes a service based on a Hessian-encoded request.
52  */

53 public class HessianEncoding implements ServiceEncoding {
54   protected static Logger JavaDoc log
55     = Logger.getLogger(HessianEncoding.class.getName());
56   private static final L10N L = new L10N(HessianEncoding.class);
57
58   private Object JavaDoc _serviceImpl;
59   
60   private HessianSkeleton _skeleton;
61
62   private SerializerFactory _serializerFactory;
63
64   /**
65    * Sets the service class.
66    */

67   public void setService(Object JavaDoc serviceImpl)
68   {
69     _serviceImpl = serviceImpl;
70   }
71
72   /**
73    * Sets the serializer factory.
74    */

75   public void setSerializerFactory(SerializerFactory factory)
76   {
77     _serializerFactory = factory;
78   }
79
80   /**
81    * Gets the serializer factory.
82    */

83   public SerializerFactory getSerializerFactory()
84   {
85     if (_serializerFactory == null)
86       _serializerFactory = new SerializerFactory();
87
88     return _serializerFactory;
89   }
90
91   /**
92    * Sets the serializer send collection java type.
93    */

94   public void setSendCollectionType(boolean sendType)
95   {
96     getSerializerFactory().setSendCollectionType(sendType);
97   }
98
99   @PostConstruct
100   public void init()
101     throws Exception JavaDoc
102   {
103     if (_serviceImpl == null)
104       _serviceImpl = this;
105
106     Class JavaDoc 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 JavaDoc findWebServiceEndpointInterface(Class JavaDoc implClass)
122     throws ClassNotFoundException JavaDoc
123   {
124     if (implClass.isAnnotationPresent(javax.jws.WebService.class)) {
125       WebService webServiceAnnotation =
126         (WebService) implClass.getAnnotation(javax.jws.WebService.class);
127
128       String JavaDoc endpoint = webServiceAnnotation.endpointInterface();
129       if (endpoint != null && ! "".equals(endpoint))
130         return CauchoSystem.loadClass(endpoint);
131     }
132
133     return null;
134   }
135
136   private Class JavaDoc findRemoteAPI(Class JavaDoc implClass)
137   {
138     if (implClass == null || implClass.equals(GenericService.class))
139       return null;
140     
141     Class JavaDoc []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 JavaDoc is, OutputStream JavaDoc 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         // XXX: deflate
163
throw new IOException JavaDoc(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 JavaDoc("skeleton is null!");
179
180       _skeleton.invoke(in, out);
181
182       out.close();
183     } catch (IOException JavaDoc e) {
184       log.log(Level.INFO, L.l("Unable to process request: "), e);
185     } catch (Throwable JavaDoc e) {
186       log.log(Level.INFO, L.l("Unable to process request: "), e);
187     }
188   }
189 }
190
Popular Tags