diff options
Diffstat (limited to '')
8 files changed, 893 insertions, 0 deletions
diff --git a/sca-java-1.x/branches/sca-java-1.6/modules/binding-hessian/src/main/java/org/apache/tuscany/sca/binding/hessian/impl/HessianBinding.java b/sca-java-1.x/branches/sca-java-1.6/modules/binding-hessian/src/main/java/org/apache/tuscany/sca/binding/hessian/impl/HessianBinding.java new file mode 100644 index 0000000000..7f90d78424 --- /dev/null +++ b/sca-java-1.x/branches/sca-java-1.6/modules/binding-hessian/src/main/java/org/apache/tuscany/sca/binding/hessian/impl/HessianBinding.java @@ -0,0 +1,94 @@ +/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.tuscany.sca.binding.hessian.impl;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import org.apache.tuscany.sca.assembly.Binding;
+
+public class HessianBinding implements Binding {
+
+ // properties required to implement the Tuscany
+ // binding extension SPI
+ private String uri = null;
+ private String name = null;
+ private boolean unresolved = false;
+ private List<Object> extensions = new ArrayList<Object>();
+
+ public HessianBinding() {
+ super();
+ }
+
+ /**
+ * Returns the binding URI.
+ *
+ * @return the binding URI
+ */
+ public String getURI() {
+ return this.uri;
+ }
+
+ /**
+ * Sets the binding URI.
+ *
+ * @param uri the binding URI
+ */
+ public void setURI(String uri) {
+ this.uri = uri;
+ }
+
+ /**
+ * Returns the binding name.
+ *
+ * @return the binding name
+ */
+ public String getName() {
+ return this.name;
+ }
+
+ /**
+ * Sets the binding name.
+ *
+ * @param name the binding name
+ */
+ public void setName(String name) {
+ this.name = name;
+ }
+
+ public boolean isUnresolved() {
+ return this.unresolved;
+ }
+
+ public void setUnresolved(boolean unresolved) {
+ this.unresolved = unresolved;
+ }
+
+ public List<Object> getExtensions() {
+ return extensions;
+ }
+
+ /**
+ * Clone the binding
+ */
+ public Object clone() throws CloneNotSupportedException {
+ return super.clone();
+ }
+
+}
diff --git a/sca-java-1.x/branches/sca-java-1.6/modules/binding-hessian/src/main/java/org/apache/tuscany/sca/binding/hessian/impl/HessianBindingException.java b/sca-java-1.x/branches/sca-java-1.6/modules/binding-hessian/src/main/java/org/apache/tuscany/sca/binding/hessian/impl/HessianBindingException.java new file mode 100644 index 0000000000..456f0d11ba --- /dev/null +++ b/sca-java-1.x/branches/sca-java-1.6/modules/binding-hessian/src/main/java/org/apache/tuscany/sca/binding/hessian/impl/HessianBindingException.java @@ -0,0 +1,40 @@ +/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.tuscany.sca.binding.hessian.impl;
+
+public class HessianBindingException extends RuntimeException {
+ private static final long serialVersionUID = 1L;
+
+ public HessianBindingException() {
+ super();
+ }
+
+ public HessianBindingException(String arg0, Throwable arg1) {
+ super(arg0, arg1);
+ }
+
+ public HessianBindingException(String arg0) {
+ super(arg0);
+ }
+
+ public HessianBindingException(Throwable arg0) {
+ super(arg0);
+ }
+
+}
diff --git a/sca-java-1.x/branches/sca-java-1.6/modules/binding-hessian/src/main/java/org/apache/tuscany/sca/binding/hessian/impl/HessianBindingProcessor.java b/sca-java-1.x/branches/sca-java-1.6/modules/binding-hessian/src/main/java/org/apache/tuscany/sca/binding/hessian/impl/HessianBindingProcessor.java new file mode 100644 index 0000000000..f5564b926b --- /dev/null +++ b/sca-java-1.x/branches/sca-java-1.6/modules/binding-hessian/src/main/java/org/apache/tuscany/sca/binding/hessian/impl/HessianBindingProcessor.java @@ -0,0 +1,135 @@ +/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.tuscany.sca.binding.hessian.impl;
+
+import javax.xml.namespace.QName;
+import javax.xml.stream.XMLStreamException;
+import javax.xml.stream.XMLStreamReader;
+import javax.xml.stream.XMLStreamWriter;
+
+import org.apache.tuscany.sca.assembly.builder.impl.ProblemImpl;
+import org.apache.tuscany.sca.assembly.xml.PolicyAttachPointProcessor;
+import org.apache.tuscany.sca.contribution.ModelFactoryExtensionPoint;
+import org.apache.tuscany.sca.contribution.processor.StAXArtifactProcessor;
+import org.apache.tuscany.sca.contribution.resolver.ModelResolver;
+import org.apache.tuscany.sca.contribution.service.ContributionReadException;
+import org.apache.tuscany.sca.contribution.service.ContributionResolveException;
+import org.apache.tuscany.sca.contribution.service.ContributionWriteException;
+import org.apache.tuscany.sca.monitor.Monitor;
+import org.apache.tuscany.sca.monitor.Problem;
+import org.apache.tuscany.sca.monitor.Problem.Severity;
+import org.apache.tuscany.sca.policy.PolicyFactory;
+
+public class HessianBindingProcessor implements StAXArtifactProcessor<HessianBinding> {
+
+ private QName BINDING_HESSIAN = new QName("http://hessian", "binding.hessian");
+
+ private PolicyFactory policyFactory;
+ private PolicyAttachPointProcessor policyProcessor;
+ private Monitor monitor;
+
+ public HessianBindingProcessor(ModelFactoryExtensionPoint modelFactories, Monitor monitor) {
+ this.policyFactory = modelFactories.getFactory(PolicyFactory.class);
+ this.policyProcessor = new PolicyAttachPointProcessor(policyFactory);
+ this.monitor = monitor;
+ }
+
+ /**
+ * Report a error.
+ *
+ * @param problems
+ * @param message
+ * @param model
+ */
+ private void warning(String message, Object model, Object... messageParameters) {
+ if (monitor != null) {
+ Problem problem =
+ new ProblemImpl(this.getClass().getName(), "binding-hessian-validation-messages", Severity.WARNING,
+ model, message, (Object[])messageParameters);
+ monitor.problem(problem);
+ }
+ }
+
+ /**
+ * Report a error.
+ *
+ * @param problems
+ * @param message
+ * @param model
+ */
+ private void error(String message, Object model, Object... messageParameters) {
+ if (monitor != null) {
+ Problem problem =
+ new ProblemImpl(this.getClass().getName(), "binding-hessian-validation-messages", Severity.ERROR,
+ model, message, (Object[])messageParameters);
+ monitor.problem(problem);
+ }
+ }
+
+ public QName getArtifactType() {
+ return BINDING_HESSIAN;
+ }
+
+ public Class<HessianBinding> getModelType() {
+ return HessianBinding.class;
+ }
+
+ public HessianBinding read(XMLStreamReader reader) throws ContributionReadException, XMLStreamException {
+ HessianBinding hessianBinding = new HessianBinding();
+
+ // Read policies
+ policyProcessor.readPolicies(hessianBinding, reader);
+
+ // Read binding name
+ String name = reader.getAttributeValue(null, "name");
+ if (name != null) {
+ hessianBinding.setName(name);
+ }
+
+ // Read binding URI
+ String uri = reader.getAttributeValue(null, "uri");
+ if (uri != null) {
+ hessianBinding.setURI(uri);
+ }
+
+ return hessianBinding;
+ }
+
+ public void resolve(HessianBinding model, ModelResolver resolver) throws ContributionResolveException {
+ }
+
+ public void write(HessianBinding hessianBinding, XMLStreamWriter writer) throws ContributionWriteException,
+ XMLStreamException {
+ policyProcessor.writePolicyPrefixes(hessianBinding, writer);
+ writer.writeStartElement(BINDING_HESSIAN.getNamespaceURI(), BINDING_HESSIAN.getLocalPart());
+ policyProcessor.writePolicyAttributes(hessianBinding, writer);
+
+ if (hessianBinding.getName() != null) {
+ writer.writeAttribute("name", hessianBinding.getName());
+ }
+
+ if (hessianBinding.getURI() != null) {
+ writer.writeAttribute("uri", hessianBinding.getURI());
+ }
+
+ writer.writeEndElement();
+ }
+
+}
diff --git a/sca-java-1.x/branches/sca-java-1.6/modules/binding-hessian/src/main/java/org/apache/tuscany/sca/binding/hessian/provider/HessianBindingInvoker.java b/sca-java-1.x/branches/sca-java-1.6/modules/binding-hessian/src/main/java/org/apache/tuscany/sca/binding/hessian/provider/HessianBindingInvoker.java new file mode 100644 index 0000000000..9d6ede2dbc --- /dev/null +++ b/sca-java-1.x/branches/sca-java-1.6/modules/binding-hessian/src/main/java/org/apache/tuscany/sca/binding/hessian/provider/HessianBindingInvoker.java @@ -0,0 +1,73 @@ +/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.tuscany.sca.binding.hessian.provider;
+
+import java.lang.reflect.InvocationTargetException;
+import java.lang.reflect.Method;
+
+import org.apache.tuscany.sca.binding.hessian.impl.HessianBinding;
+import org.apache.tuscany.sca.interfacedef.Interface;
+import org.apache.tuscany.sca.interfacedef.java.JavaOperation;
+import org.apache.tuscany.sca.invocation.Invoker;
+import org.apache.tuscany.sca.invocation.Message;
+
+import com.caucho.hessian.client.HessianProxyFactory;
+
+public class HessianBindingInvoker implements Invoker {
+
+ private String serviceURL;
+ private Interface homeImpl;
+
+ HessianBindingInvoker(Interface homeImpl, HessianBinding binding) {
+ this.serviceURL = binding.getURI();
+ this.homeImpl = homeImpl;
+ }
+
+ public Message invoke(Message msg) {
+ try {
+ Method method = ((JavaOperation)msg.getOperation()).getJavaMethod();
+ Object[] args = msg.getBody();
+
+ HessianProxyFactory proxyFactory = new HessianProxyFactory();
+ Object service = proxyFactory.create(loadClass(homeImpl.toString()), serviceURL);
+ Object result = null;
+ result = method.invoke(service, args);
+ msg.setBody(result);
+ } catch (Throwable e) {
+ e.printStackTrace();
+ if (e instanceof InvocationTargetException)
+ e = ((InvocationTargetException)e).getTargetException();
+ msg.setFaultBody(e);
+ }
+ return msg;
+ }
+
+ private Class loadClass(String className) {
+ ClassLoader loader = Thread.currentThread().getContextClassLoader();
+ try {
+ if (loader != null)
+ return Class.forName(className, false, loader);
+ else
+ return Class.forName(className);
+ } catch (Exception e) {
+
+ }
+ return null;
+ }
+}
diff --git a/sca-java-1.x/branches/sca-java-1.6/modules/binding-hessian/src/main/java/org/apache/tuscany/sca/binding/hessian/provider/HessianBindingProviderFactory.java b/sca-java-1.x/branches/sca-java-1.6/modules/binding-hessian/src/main/java/org/apache/tuscany/sca/binding/hessian/provider/HessianBindingProviderFactory.java new file mode 100644 index 0000000000..77a5f6f675 --- /dev/null +++ b/sca-java-1.x/branches/sca-java-1.6/modules/binding-hessian/src/main/java/org/apache/tuscany/sca/binding/hessian/provider/HessianBindingProviderFactory.java @@ -0,0 +1,67 @@ +/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.tuscany.sca.binding.hessian.provider;
+
+import java.util.List;
+
+import org.apache.tuscany.sca.binding.hessian.impl.HessianBinding;
+import org.apache.tuscany.sca.contribution.ModelFactoryExtensionPoint;
+import org.apache.tuscany.sca.core.ExtensionPointRegistry;
+import org.apache.tuscany.sca.host.http.ServletHost;
+import org.apache.tuscany.sca.host.http.ServletHostExtensionPoint;
+import org.apache.tuscany.sca.invocation.MessageFactory;
+import org.apache.tuscany.sca.provider.BindingProviderFactory;
+import org.apache.tuscany.sca.provider.ReferenceBindingProvider;
+import org.apache.tuscany.sca.provider.ServiceBindingProvider;
+import org.apache.tuscany.sca.runtime.RuntimeComponent;
+import org.apache.tuscany.sca.runtime.RuntimeComponentReference;
+import org.apache.tuscany.sca.runtime.RuntimeComponentService;
+
+public class HessianBindingProviderFactory implements BindingProviderFactory<HessianBinding> {
+
+ private MessageFactory messageFactory;
+ private ServletHost servletHost;
+
+ public HessianBindingProviderFactory(ExtensionPointRegistry extensionPoints) {
+ ServletHostExtensionPoint servletHosts = extensionPoints.getExtensionPoint(ServletHostExtensionPoint.class);
+ List<ServletHost> hosts = servletHosts.getServletHosts();
+ if (!hosts.isEmpty()) {
+ this.servletHost = hosts.get(0);
+ }
+ ModelFactoryExtensionPoint modelFactories = extensionPoints.getExtensionPoint(ModelFactoryExtensionPoint.class);
+ messageFactory = modelFactories.getFactory(MessageFactory.class);
+ }
+
+ public ReferenceBindingProvider createReferenceBindingProvider(RuntimeComponent component,
+ RuntimeComponentReference reference,
+ HessianBinding binding) {
+ return new HessianReferenceBindingProvider(component, reference, binding);
+ }
+
+ public ServiceBindingProvider createServiceBindingProvider(RuntimeComponent component,
+ RuntimeComponentService service,
+ HessianBinding binding) {
+ return new HessianServiceBindingProvider(component, service, binding, messageFactory, servletHost);
+ }
+
+ public Class<HessianBinding> getModelType() {
+ return HessianBinding.class;
+ }
+}
diff --git a/sca-java-1.x/branches/sca-java-1.6/modules/binding-hessian/src/main/java/org/apache/tuscany/sca/binding/hessian/provider/HessianReferenceBindingProvider.java b/sca-java-1.x/branches/sca-java-1.6/modules/binding-hessian/src/main/java/org/apache/tuscany/sca/binding/hessian/provider/HessianReferenceBindingProvider.java new file mode 100644 index 0000000000..27711932d7 --- /dev/null +++ b/sca-java-1.x/branches/sca-java-1.6/modules/binding-hessian/src/main/java/org/apache/tuscany/sca/binding/hessian/provider/HessianReferenceBindingProvider.java @@ -0,0 +1,62 @@ +/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.tuscany.sca.binding.hessian.provider;
+
+import org.apache.tuscany.sca.binding.hessian.impl.HessianBinding;
+import org.apache.tuscany.sca.interfacedef.InterfaceContract;
+import org.apache.tuscany.sca.interfacedef.Operation;
+import org.apache.tuscany.sca.invocation.Invoker;
+import org.apache.tuscany.sca.provider.ReferenceBindingProvider;
+import org.apache.tuscany.sca.runtime.RuntimeComponent;
+import org.apache.tuscany.sca.runtime.RuntimeComponentReference;
+
+public class HessianReferenceBindingProvider implements ReferenceBindingProvider {
+
+ private RuntimeComponentReference reference;
+ private HessianBinding hessianBinding;
+
+ public HessianReferenceBindingProvider(RuntimeComponent component,
+ RuntimeComponentReference reference,
+ HessianBinding binding) {
+ this.reference = reference;
+ this.hessianBinding = binding;
+ }
+
+ public Invoker createInvoker(Operation operation) {
+ return new HessianBindingInvoker(getBindingInterfaceContract().getInterface(), hessianBinding);
+ }
+
+ public boolean supportsOneWayInvocation() {
+ return true;
+ }
+
+ public InterfaceContract getBindingInterfaceContract() {
+ return reference.getInterfaceContract();
+ }
+
+ public void start() {
+
+ }
+
+ public void stop() {
+
+ }
+
+}
diff --git a/sca-java-1.x/branches/sca-java-1.6/modules/binding-hessian/src/main/java/org/apache/tuscany/sca/binding/hessian/provider/HessianServiceBindingProvider.java b/sca-java-1.x/branches/sca-java-1.6/modules/binding-hessian/src/main/java/org/apache/tuscany/sca/binding/hessian/provider/HessianServiceBindingProvider.java new file mode 100644 index 0000000000..3e4922dc13 --- /dev/null +++ b/sca-java-1.x/branches/sca-java-1.6/modules/binding-hessian/src/main/java/org/apache/tuscany/sca/binding/hessian/provider/HessianServiceBindingProvider.java @@ -0,0 +1,87 @@ +/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.tuscany.sca.binding.hessian.provider;
+
+import java.util.logging.Logger;
+
+import javax.servlet.Servlet;
+
+import org.apache.tuscany.sca.binding.hessian.impl.HessianBinding;
+import org.apache.tuscany.sca.host.http.ServletHost;
+import org.apache.tuscany.sca.interfacedef.Interface;
+import org.apache.tuscany.sca.interfacedef.InterfaceContract;
+import org.apache.tuscany.sca.invocation.MessageFactory;
+import org.apache.tuscany.sca.provider.ServiceBindingProvider;
+import org.apache.tuscany.sca.runtime.RuntimeComponent;
+import org.apache.tuscany.sca.runtime.RuntimeComponentService;
+import org.apache.tuscany.sca.runtime.RuntimeWire;
+
+public class HessianServiceBindingProvider implements ServiceBindingProvider {
+ private static final Logger logger = Logger.getLogger(HessianServiceBindingProvider.class.getName());
+
+ private RuntimeComponent component;
+ private RuntimeComponentService service;
+ private HessianBinding hessianBinding;
+ private MessageFactory messageFactory;
+ private ServletHost servletHost;
+ private String servletMapping;
+
+ public HessianServiceBindingProvider(RuntimeComponent component,
+ RuntimeComponentService service,
+ HessianBinding binding,
+ MessageFactory messageFactory,
+ ServletHost servletHost) {
+ this.component = component;
+ this.service = service;
+ this.hessianBinding = binding;
+ this.messageFactory = messageFactory;
+ this.servletHost = servletHost;
+ }
+
+ public InterfaceContract getBindingInterfaceContract() {
+ return service.getInterfaceContract();
+ }
+
+ public boolean supportsOneWayInvocation() {
+ return true;
+ }
+
+ public void start() {
+ Servlet servlet = null;
+ Interface homeImpl = service.getInterfaceContract().getInterface();
+ RuntimeWire wire = service.getRuntimeWire(hessianBinding);
+ servlet = new HessianServiceListenerServlet(homeImpl, wire, messageFactory);
+
+ if (servlet == null) {
+ throw new IllegalStateException("No get or service method found on the service");
+ }
+
+ servletMapping = hessianBinding.getURI();
+ servletHost.addServletMapping(servletMapping, servlet);
+
+ // Save the actual binding URI in the binding
+ hessianBinding.setURI(servletHost.getURLMapping(hessianBinding.getURI()).toString());
+ }
+
+ public void stop() {
+ servletHost.removeServletMapping(servletMapping);
+ }
+
+}
diff --git a/sca-java-1.x/branches/sca-java-1.6/modules/binding-hessian/src/main/java/org/apache/tuscany/sca/binding/hessian/provider/HessianServiceListenerServlet.java b/sca-java-1.x/branches/sca-java-1.6/modules/binding-hessian/src/main/java/org/apache/tuscany/sca/binding/hessian/provider/HessianServiceListenerServlet.java new file mode 100644 index 0000000000..de9b4927c6 --- /dev/null +++ b/sca-java-1.x/branches/sca-java-1.6/modules/binding-hessian/src/main/java/org/apache/tuscany/sca/binding/hessian/provider/HessianServiceListenerServlet.java @@ -0,0 +1,335 @@ +/* + * Copyright (c) 2001-2008 Caucho Technology, Inc. All rights reserved. + * + * The Apache Software License, Version 1.1 + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * + * 3. The end-user documentation included with the redistribution, if + * any, must include the following acknowlegement: + * "This product includes software developed by the + * Caucho Technology (http://www.caucho.com/)." + * Alternately, this acknowlegement may appear in the software itself, + * if and wherever such third-party acknowlegements normally appear. + * + * 4. The names "Hessian", "Resin", and "Caucho" must not be used to + * endorse or promote products derived from this software without prior + * written permission. For written permission, please contact + * info@caucho.com. + * + * 5. Products derived from this software may not be called "Resin" + * nor may "Resin" appear in their names without prior written + * permission of Caucho Technology. + * + * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED + * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL CAUCHO TECHNOLOGY OR ITS CONTRIBUTORS + * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, + * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT + * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR + * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE + * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN + * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + * @author Scott Ferguson + */ + +package org.apache.tuscany.sca.binding.hessian.provider; + +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; +import java.io.PrintWriter; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import java.util.Hashtable; +import java.util.List; +import java.util.Map; +import java.util.logging.Logger; + +import javax.servlet.GenericServlet; +import javax.servlet.ServletException; +import javax.servlet.ServletRequest; +import javax.servlet.ServletResponse; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +import org.apache.tuscany.sca.interfacedef.Interface; +import org.apache.tuscany.sca.interfacedef.Operation; +import org.apache.tuscany.sca.interfacedef.java.JavaOperation; +import org.apache.tuscany.sca.invocation.Message; +import org.apache.tuscany.sca.invocation.MessageFactory; +import org.apache.tuscany.sca.runtime.RuntimeWire; + +import com.caucho.hessian.io.AbstractHessianInput; +import com.caucho.hessian.io.AbstractHessianOutput; +import com.caucho.hessian.io.Hessian2Input; +import com.caucho.hessian.io.Hessian2Output; +import com.caucho.hessian.io.HessianOutput; +import com.caucho.hessian.io.SerializerFactory; +import com.caucho.services.server.ServiceContext; + +/** + * Servlet for serving Hessian services. + */ +public class HessianServiceListenerServlet extends GenericServlet { + + private static final Logger logger = Logger.getLogger(HessianServiceListenerServlet.class.getName()); + + private Interface homeImpl; + private RuntimeWire wire; + private MessageFactory messageFactory; + + private SerializerFactory _serializerFactory; + + private Map<String, JavaOperation> _operationMap = new Hashtable<String, JavaOperation>(); + + public HessianServiceListenerServlet(Interface homeImpl, RuntimeWire wire, MessageFactory messageFactory) { + this.homeImpl = homeImpl; + this.wire = wire; + this.messageFactory = messageFactory; + + List<Operation> operations = homeImpl.getOperations(); + for (int i = 0; i < operations.size(); i++) { + JavaOperation op = (JavaOperation)operations.get(i); + Method method = op.getJavaMethod(); + if (_operationMap.get(method.getName()) == null) + _operationMap.put(method.getName(), op); + + Class[] param = method.getParameterTypes(); + String mangledName = method.getName() + "__" + param.length; + _operationMap.put(mangledName, op); + _operationMap.put(mangleName(method, false), op); + } + } + + public String getServletInfo() { + return "Tuscany Binding Hessian Servlet"; + } + + /** + * Sets the serializer factory. + */ + public void setSerializerFactory(SerializerFactory factory) { + _serializerFactory = factory; + } + + /** + * Gets the serializer factory. + */ + public SerializerFactory getSerializerFactory() { + if (_serializerFactory == null) + _serializerFactory = new SerializerFactory(); + + return _serializerFactory; + } + + /** + * Sets the serializer send collection java type. + */ + public void setSendCollectionType(boolean sendType) { + getSerializerFactory().setSendCollectionType(sendType); + } + + /** + * Execute a request. The path-info of the request selects the bean. Once + * the bean's selected, it will be applied. + */ + public void service(ServletRequest request, ServletResponse response) throws IOException, ServletException { + HttpServletRequest req = (HttpServletRequest)request; + HttpServletResponse res = (HttpServletResponse)response; + + if (!req.getMethod().equals("POST")) { + res.setStatus(500, "Hessian Requires POST"); + PrintWriter out = res.getWriter(); + res.setContentType("text/html"); + out.println("<h1>Hessian Requires POST</h1>"); + return; + } + + String serviceId = req.getPathInfo(); + String objectId = req.getParameter("id"); + if (objectId == null) + objectId = req.getParameter("ejbid"); + + ServiceContext.begin(req, serviceId, objectId); + + try { + InputStream is = request.getInputStream(); + OutputStream os = response.getOutputStream(); + + Hessian2Input in = new Hessian2Input(is); + AbstractHessianOutput out; + + SerializerFactory serializerFactory = getSerializerFactory(); + + in.setSerializerFactory(serializerFactory); + + int code = in.read(); + + if (code != 'c') { + // XXX: deflate + throw new IOException("expected 'c' in hessian input at " + code); + } + + int major = in.read(); + int minor = in.read(); + + if (major >= 2) + out = new Hessian2Output(os); + else + out = new HessianOutput(os); + + out.setSerializerFactory(serializerFactory); + + invoke(in, out); + + out.close(); + } catch (RuntimeException e) { + throw e; + } catch (ServletException e) { + throw e; + } catch (Throwable e) { + throw new ServletException(e); + } finally { + ServiceContext.end(); + } + } + + private String mangleName(Method method, boolean isFull) { + StringBuffer sb = new StringBuffer(); + sb.append(method.getName()); + Class[] params = method.getParameterTypes(); + for (int i = 0; i < params.length; i++) { + sb.append('_'); + sb.append(mangleClass(params[i], isFull)); + } + return sb.toString(); + } + + private String mangleClass(Class cl, boolean isFull) { + String name = cl.getName(); + if (name.equals("boolean") || name.equals("java.lang.Boolean")) + return "boolean"; + else if (name.equals("int") || name.equals("java.lang.Integer") + || name.equals("short") + || name.equals("java.lang.Short") + || name.equals("byte") + || name.equals("java.lang.Byte")) + return "int"; + else if (name.equals("long") || name.equals("java.lang.Long")) + return "long"; + else if (name.equals("float") || name.equals("java.lang.Float") + || name.equals("double") + || name.equals("java.lang.Double")) + return "double"; + else if (name.equals("java.lang.String") || name.equals("com.caucho.util.CharBuffer") + || name.equals("char") + || name.equals("java.lang.Character") + || name.equals("java.io.Reader")) + return "string"; + else if (name.equals("java.util.Date") || name.equals("com.caucho.util.QDate")) + return "date"; + else if (InputStream.class.isAssignableFrom(cl) || name.equals("[B")) + return "binary"; + else if (cl.isArray()) { + return "[" + mangleClass(cl.getComponentType(), isFull); + } else if (name.equals("org.w3c.dom.Node") || name.equals("org.w3c.dom.Element") + || name.equals("org.w3c.dom.Document")) + return "xml"; + else if (isFull) + return name; + else { + int p = name.lastIndexOf('.'); + if (p > 0) + return name.substring(p + 1); + else + return name; + } + } + + public void invoke(AbstractHessianInput in, AbstractHessianOutput out) throws Exception { + ServiceContext context = ServiceContext.getContext(); + + // backward compatibility for some frameworks that don't read + // the call type first + in.skipOptionalCall(); + + String header; + while ((header = in.readHeader()) != null) { + Object value = in.readObject(); + context.addHeader(header, value); + } + + String methodName = in.readMethod(); + Method method = null; + JavaOperation operation = _operationMap.get(methodName); + if (operation != null) + method = operation.getJavaMethod(); + + if (method != null) { + } else if ("_hessian_getAttribute".equals(methodName)) { + String attrName = in.readString(); + in.completeCall(); + String value = null; + if ("java.api.class".equals(attrName)) + value = homeImpl.toString(); + else if ("java.home.class".equals(attrName)) + value = homeImpl.toString(); + else if ("java.object.class".equals(attrName)) + value = homeImpl.toString(); + out.startReply(); + out.writeObject(value); + out.completeReply(); + return; + } else if (method == null) { + out.startReply(); + out.writeFault("NoSuchMethodException", "The service has no method named: " + in.getMethod(), null); + out.completeReply(); + return; + } + + Class[] args = method.getParameterTypes(); + Object[] values = new Object[args.length]; + + for (int i = 0; i < args.length; i++) { + values[i] = in.readObject(args[i]); + } + + Object result = null; + try { + Message msg = messageFactory.createMessage(); + msg.setOperation(operation); + msg.setBody(values); + result = wire.invoke(operation, msg); + } catch (Throwable e) { + while (e instanceof InvocationTargetException) + e = ((InvocationTargetException)e).getTargetException(); + out.startReply(); + out.writeFault("ServiceException", e.getMessage(), e); + out.completeReply(); + return; + } + + // The complete call needs to be after the invoke to handle a + // trailing InputStream + in.completeCall(); + out.startReply(); + out.writeObject(result); + out.completeReply(); + out.close(); + } + +} |