EMMA Coverage Report (generated Tue Feb 12 22:23:49 ICT 2008)
[all classes][net.sourceforge.hiveremoting.caucho]

COVERAGE SUMMARY FOR SOURCE FILE [CauchoRemotingServlet.java]

nameclass, %method, %block, %line, %
CauchoRemotingServlet.java0%   (0/1)0%   (0/11)0%   (0/218)0%   (0/58)

COVERAGE BREAKDOWN BY CLASS AND METHOD

nameclass, %method, %block, %line, %
     
class CauchoRemotingServlet0%   (0/1)0%   (0/11)0%   (0/218)0%   (0/58)
<static initializer> 0%   (0/1)0%   (0/4)0%   (0/1)
CauchoRemotingServlet (): void 0%   (0/1)0%   (0/12)0%   (0/4)
checkInit (HttpServletRequest): void 0%   (0/1)0%   (0/15)0%   (0/7)
getRegistry (HttpServletRequest): Registry 0%   (0/1)0%   (0/3)0%   (0/1)
getServletInfo (): String 0%   (0/1)0%   (0/2)0%   (0/1)
init (ServletConfig): void 0%   (0/1)0%   (0/4)0%   (0/2)
initBurlapService (PublishServiceContribution, String): void 0%   (0/1)0%   (0/12)0%   (0/2)
initHessianService (PublishServiceContribution, String): void 0%   (0/1)0%   (0/12)0%   (0/2)
initServices (HttpServletRequest): void 0%   (0/1)0%   (0/67)0%   (0/18)
sendError (HttpServletResponse, String): void 0%   (0/1)0%   (0/35)0%   (0/6)
service (ServletRequest, ServletResponse): void 0%   (0/1)0%   (0/52)0%   (0/14)

1//  Copyright 2004-2007 Jean-Francois Poilpret
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14 
15package net.sourceforge.hiveremoting.caucho;
16 
17import java.io.IOException;
18import java.io.PrintWriter;
19import java.util.HashMap;
20import java.util.Iterator;
21import java.util.List;
22import java.util.Map;
23 
24import javax.servlet.GenericServlet;
25import javax.servlet.ServletConfig;
26import javax.servlet.ServletException;
27import javax.servlet.ServletRequest;
28import javax.servlet.ServletResponse;
29import javax.servlet.http.HttpServletRequest;
30import javax.servlet.http.HttpServletResponse;
31 
32import org.apache.commons.logging.Log;
33import org.apache.commons.logging.LogFactory;
34import org.apache.hivemind.Registry;
35import org.apache.hivemind.servlet.HiveMindFilter;
36 
37import com.caucho.hessian.io.SerializerFactory;
38 
39/**
40 * Servlet class that "publishes" HiveMind services as remote services,
41 * accessible through Caucho Hessian or Burlap protocol.
42 * <p>
43 * All services to be published must be defined in <code>RemoteServices</code>
44 * configuration point.
45 * @author Jean-Francois Poilpret
46 */
47public class CauchoRemotingServlet extends GenericServlet
48{
49        static private final Log        _logger = LogFactory.getLog(CauchoRemotingServlet.class);
50        
51        @Override public String getServletInfo()
52        {
53                return "HiveRemoting Caucho Servlet";
54        }
55        
56        @Override public void init(ServletConfig config)
57                throws ServletException
58        {
59                super.init(config);
60        }
61        
62        //CSOFF: IllegalCatchCheck
63        protected void        checkInit(HttpServletRequest request)
64        {
65                if (!_init)
66                {
67                        try
68                        {
69                                initServices(request);
70                        }
71                        catch (RuntimeException e)
72                        {
73                                _logger.fatal("checkInit", e);
74                                throw e;
75                        }
76                }
77        }
78        //CSON: IllegalCatchCheck
79        
80        synchronized protected void initServices(HttpServletRequest request)
81        {
82                if (!_init)
83                {
84                        // Get HiveMind Registry
85                        Registry registry = getRegistry(request);
86 
87                        // Initialize special Serialization for Hessian/Burlap
88                        _factory = (SerializerFactory) registry.getService(
89                                        "hiveremoting.caucho.CauchoSerializerFactory", SerializerFactory.class);
90 
91                        // Initialize Hessian/Burlap services
92                        List services = registry.getConfiguration("hiveremoting.caucho.RemoteServices");
93                        Iterator i = services.iterator();
94                        _services = new HashMap<String, RemoteServiceInvoker>();
95                        while (i.hasNext())
96                        {
97                                PublishServiceContribution contrib =
98                                                                                (PublishServiceContribution) i.next();
99                                String urlPath = contrib.getUrlPath();
100                                if (!urlPath.startsWith("/"))
101                                {
102                                        urlPath = "/" + urlPath;
103                                }
104                                if (contrib.getProtocol() == Protocol.Hessian)
105                                {
106                                        initHessianService(contrib, urlPath);
107                                }
108                                else
109                                {
110                                        initBurlapService(contrib, urlPath);
111                                }
112                        }
113                        _init = true;
114                }
115        }
116        
117        /**
118         * Retrieve the HiveMind registry. Uses HiveMindFilter by default.
119         * 
120         * @param request current http request
121         * @return the HiveMind registry
122         */
123        protected Registry getRegistry(HttpServletRequest request)
124        {
125                return HiveMindFilter.getRegistry(request);
126        }
127 
128        protected void        initHessianService(PublishServiceContribution contrib, String urlPath)
129        {
130                _services.put(        urlPath,
131                                                new HessianRemoteServiceInvoker(contrib, _factory));
132        }
133        
134        protected void        initBurlapService(PublishServiceContribution contrib, String urlPath)
135        {
136                _services.put(        urlPath,
137                                                new BurlapRemoteServiceInvoker(contrib, _factory));
138        }
139        
140        protected void        sendError(HttpServletResponse res, String msg)
141                throws IOException, ServletException
142        {
143                _logger.warn("sendError '" + msg + "'");
144                res.sendError(HttpServletResponse.SC_OK, msg);
145                PrintWriter out = res.getWriter();
146                res.setContentType("text/html");
147                out.println("<h1>" + msg + "</h1>");
148        }
149        
150        @Override public void service(ServletRequest request, ServletResponse response)
151                throws IOException, ServletException
152        {
153                HttpServletRequest req = (HttpServletRequest) request;
154                HttpServletResponse res = (HttpServletResponse) response;
155                
156                if (!req.getMethod().equals("POST"))
157                {
158                        sendError(res, "Hessian/Burlap remoting protocols require POST");
159                        return;
160                }
161 
162                // Make sure that the skeletons are initialized                
163                checkInit(req);
164 
165                // Find out the service that was called
166                String urlPath = req.getPathInfo();
167                // Does it exist?
168                RemoteServiceInvoker invoker =  _services.get(urlPath);
169                if (invoker == null)
170                {
171                        sendError(res, "Service '" + urlPath + "' does not exist");
172                }
173                else
174                {
175                        res.setContentType("text/xml");
176                        invoker.invoke(req, res);
177                }
178        }
179        
180        protected boolean                                                        _init = false;        
181        protected Map<String, RemoteServiceInvoker>        _services = null;
182        protected SerializerFactory                                        _factory = null;
183}

[all classes][net.sourceforge.hiveremoting.caucho]
EMMA 2.0.5312 (C) Vladimir Roubtsov