/*
* 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.implementation.widget;
import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
import java.net.URL;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.tuscany.sca.assembly.Binding;
import org.apache.tuscany.sca.assembly.ComponentReference;
import org.apache.tuscany.sca.assembly.OptimizableBinding;
import org.apache.tuscany.sca.runtime.RuntimeComponent;
/**
* Servlet to handle requests for ..
*
* @version $Rev: 589538 $ $Date: 2007-10-29 08:15:47 +0000 (Mon, 29 Oct 2007) $
*/
public class WidgetComponentServlet extends HttpServlet {
private static final long serialVersionUID = -2996033426789721509L;
private transient RuntimeComponent component;
private transient URI servletURI;
public WidgetComponentServlet(RuntimeComponent component, String servletURI) {
this.component = component;
this.servletURI = URI.create(servletURI);
}
@Override
public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
ServletOutputStream os = response.getOutputStream();
os.println();
os.println("/* Apache Tuscany - SCA Web Widget */");
os.println();
writeSCAWidgetCode(os, request.getServletPath());
}
/**
*/
private void writeSCAWidgetCode(ServletOutputStream out, String path) throws IOException {
out.println();
out.println("/* Apache Tuscany SCA Widget header */");
out.println();
// remove the leading slash '/' character
path = path.substring(1);
for(ComponentReference reference : component.getReferences()) {
for(Binding binding : reference.getBindings()) {
String bindingProxyName = WidgetProxyHelper.getJavaScriptProxyFile(binding.getClass().getName());
if(bindingProxyName != null) {
writeJavaScriptBindingProxy(out,bindingProxyName);
}
}
}
writeJavaScriptReferenceFunction(out);
out.println();
out.println("/** End of Apache Tuscany SCA Widget */");
out.println();
}
/**
* Retrieve the binding proxy based on the bind name
* and embedded the javascript into this js
*/
private void writeJavaScriptBindingProxy(ServletOutputStream os, String bindingProxyName) throws IOException {
URL url = getClass().getClassLoader().getResource(bindingProxyName); //Thread.currentThread().getContextClassLoader().getResource(bindingProxyName);
InputStream is = url.openStream();
int i;
while ((i = is.read()) != -1) {
os.write(i);
}
os.println();
os.println();
}
private void writeJavaScriptReferenceFunction (ServletOutputStream os) throws IOException {
os.println("var referenceMap = new Object();");
for(ComponentReference reference : component.getReferences()) {
String referenceName = reference.getName();
Binding binding = reference.getBindings().get(0);
if (binding != null) {
String proxyClient = WidgetProxyHelper.getJavaScriptProxyClient(binding.getClass().getName());
if(proxyClient != null) {
//FIXME Eventually the binding URI should be initialized from the SCA domain
// for now point to the target binding model directly, assuming that it's available
// in the same node
String targetURI = null;
if (binding instanceof OptimizableBinding) {
Binding targetBinding = ((OptimizableBinding)binding).getTargetBinding();
if (targetBinding != null) {
targetURI = URI.create(targetBinding.getURI()).getPath();
}
}
if (targetURI == null) {
targetURI = URI.create(binding.getURI()).getPath();
if (!targetURI.startsWith("/")) {
targetURI = "/" + targetURI;
}
}
if(proxyClient.equals("JSONRpcClient")) {
os.println("referenceMap." + referenceName + " = new " + proxyClient + "(\"" + targetURI + "\").Service;");
} else {
os.println("referenceMap." + referenceName + " = new " + proxyClient + "(\"" + targetURI + "\");");
}
}
}
}
os.println("function Reference(name) {");
os.println(" return referenceMap[name];");
os.println("}");
}
}