001: /*
002: * Copyright (c) 2001-2004 Caucho Technology, Inc. All rights reserved.
003: *
004: * The Apache Software License, Version 1.1
005: *
006: * Redistribution and use in source and binary forms, with or without
007: * modification, are permitted provided that the following conditions
008: * are met:
009: *
010: * 1. Redistributions of source code must retain the above copyright
011: * notice, this list of conditions and the following disclaimer.
012: *
013: * 2. Redistributions in binary form must reproduce the above copyright
014: * notice, this list of conditions and the following disclaimer in
015: * the documentation and/or other materials provided with the
016: * distribution.
017: *
018: * 3. The end-user documentation included with the redistribution, if
019: * any, must include the following acknowlegement:
020: * "This product includes software developed by the
021: * Caucho Technology (http://www.caucho.com/)."
022: * Alternately, this acknowlegement may appear in the software itself,
023: * if and wherever such third-party acknowlegements normally appear.
024: *
025: * 4. The names "Burlap", "Resin", and "Caucho" must not be used to
026: * endorse or promote products derived from this software without prior
027: * written permission. For written permission, please contact
028: * info@caucho.com.
029: *
030: * 5. Products derived from this software may not be called "Resin"
031: * nor may "Resin" appear in their names without prior written
032: * permission of Caucho Technology.
033: *
034: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
035: * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
036: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
037: * DISCLAIMED. IN NO EVENT SHALL CAUCHO TECHNOLOGY OR ITS CONTRIBUTORS
038: * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
039: * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
040: * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
041: * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
042: * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
043: * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
044: * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
045: *
046: * @author Scott Ferguson
047: */
048:
049: package com.caucho.burlap.server;
050:
051: import com.caucho.burlap.io.BurlapInput;
052: import com.caucho.burlap.io.BurlapOutput;
053: import com.caucho.services.server.AbstractSkeleton;
054: import com.caucho.services.server.ServiceContext;
055:
056: import java.lang.reflect.InvocationTargetException;
057: import java.lang.reflect.Method;
058: import java.util.logging.*;
059:
060: /**
061: * Proxy class for Burlap services.
062: */
063: public class BurlapSkeleton extends AbstractSkeleton {
064: private static final Logger log = Logger
065: .getLogger(BurlapSkeleton.class.getName());
066:
067: private Object _service;
068:
069: /**
070: * Create a new burlap skeleton.
071: *
072: * @param service the underlying service object.
073: * @param apiClass the API interface
074: */
075: public BurlapSkeleton(Object service, Class apiClass) {
076: super (apiClass);
077:
078: _service = service;
079: }
080:
081: /**
082: * Create a new burlap skeleton.
083: *
084: * @param service the underlying service object.
085: * @param apiClass the API interface
086: */
087: public BurlapSkeleton(Class apiClass) {
088: super (apiClass);
089: }
090:
091: /**
092: * Invoke the object with the request from the input stream.
093: *
094: * @param in the Burlap input stream
095: * @param out the Burlap output stream
096: */
097: public void invoke(BurlapInput in, BurlapOutput out)
098: throws Exception {
099: invoke(_service, in, out);
100: }
101:
102: /**
103: * Invoke the object with the request from the input stream.
104: *
105: * @param in the Burlap input stream
106: * @param out the Burlap output stream
107: */
108: public void invoke(Object service, BurlapInput in, BurlapOutput out)
109: throws Exception {
110: in.readCall();
111:
112: ServiceContext context = ServiceContext.getContext();
113:
114: String header;
115: while ((header = in.readHeader()) != null) {
116: Object value = in.readObject();
117:
118: context.addHeader(header, value);
119: }
120:
121: String methodName = in.readMethod();
122: Method method = getMethod(methodName);
123:
124: if (log.isLoggable(Level.FINE))
125: log.fine(this + " invoking " + methodName + " (" + method
126: + ")");
127:
128: if (method != null) {
129: } else if ("_burlap_getAttribute".equals(in.getMethod())) {
130: String attrName = in.readString();
131: in.completeCall();
132:
133: String value = null;
134:
135: if ("java.api.class".equals(attrName))
136: value = getAPIClassName();
137: else if ("java.home.class".equals(attrName))
138: value = getHomeClassName();
139: else if ("java.object.class".equals(attrName))
140: value = getObjectClassName();
141:
142: out.startReply();
143:
144: out.writeObject(value);
145:
146: out.completeReply();
147: return;
148: } else if (method == null) {
149: out.startReply();
150: out.writeFault("NoSuchMethodException",
151: "The service has no method named: "
152: + in.getMethod(), null);
153: out.completeReply();
154: return;
155: }
156:
157: Class[] args = method.getParameterTypes();
158: Object[] values = new Object[args.length];
159:
160: for (int i = 0; i < args.length; i++)
161: values[i] = in.readObject(args[i]);
162:
163: in.completeCall();
164:
165: Object result = null;
166:
167: try {
168: result = method.invoke(service, values);
169: } catch (Throwable e) {
170: log.log(Level.FINE, service + "." + method.getName()
171: + "() failed with exception:\n" + e.toString(), e);
172:
173: if (e instanceof InvocationTargetException
174: && e.getCause() instanceof Exception)
175: e = ((InvocationTargetException) e)
176: .getTargetException();
177: out.startReply();
178: out.writeFault("ServiceException", e.getMessage(), e);
179: out.completeReply();
180: return;
181: }
182:
183: out.startReply();
184:
185: out.writeObject(result);
186:
187: out.completeReply();
188: }
189: }
|