Porting restfull sample to 2.x

git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@920646 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
lresende 2010-03-09 03:49:29 +00:00
parent 1d3030d1a5
commit ef8d908668
9 changed files with 542 additions and 0 deletions

View file

@ -0,0 +1,83 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
* 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.
-->
<project>
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.apache.tuscany.sca</groupId>
<artifactId>tuscany-sca</artifactId>
<version>1.7-SNAPSHOT</version>
</parent>
<artifactId>sample-expertise-restfull</artifactId>
<name>Apache Tuscany SCA RESTFull Expertise Locator</name>
<dependencies>
<dependency>
<groupId>org.apache.tuscany.sca</groupId>
<artifactId>tuscany-sca-api</artifactId>
<version>2.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.apache.tuscany.sca</groupId>
<artifactId>tuscany-data-api</artifactId>
<version>2.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.apache.tuscany.sca</groupId>
<artifactId>tuscany-node-impl</artifactId>
<version>2.0-SNAPSHOT</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.apache.tuscany.sca</groupId>
<artifactId>tuscany-implementation-java-runtime</artifactId>
<version>2.0-SNAPSHOT</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.apache.tuscany.sca</groupId>
<artifactId>tuscany-binding-http-runtime</artifactId>
<version>2.0-SNAPSHOT</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.apache.tuscany.sca</groupId>
<artifactId>tuscany-host-jetty</artifactId>
<version>2.0-SNAPSHOT</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<finalName>${artifactId}</finalName>
</build>
</project>

View file

@ -0,0 +1,64 @@
/*
* 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.expertise;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
public class Expert implements Serializable {
private static final long serialVersionUID = 2026687637313310397L;
private String id;
private String name;
private String location;
private List<String> expertises = new ArrayList<String>();
public Expert() {
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getLocation() {
return location;
}
public void setLocation(String location) {
this.location = location;
}
public List<String> getExpertise() {
return this.expertises;
}
}

View file

@ -0,0 +1,37 @@
/*
* 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.expertise.launch;
import org.apache.tuscany.sca.node.Node;
import org.apache.tuscany.sca.node.NodeFactory;
public class Launch {
public static void main(String[] args) throws Exception {
System.out.println("Starting ...");
Node node = NodeFactory.newInstance().createNode("rest.composite", new String[] {"target/classes"});
node.start();
System.out.println("rest.composite ready for big business !!!");
System.in.read();
System.out.println("Stopping ...");
node.stop();
System.out.println();
}
}

View file

@ -0,0 +1,37 @@
/*
* 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.expertise.restfull;
import java.util.List;
import org.apache.tuscany.expertise.Expert;
import org.oasisopen.sca.annotation.Remotable;
@Remotable
public interface ExpertRegistry {
void addExpert(Expert expert);
void removeExpert(Expert expert);
List<Expert> getExperts();
List<Expert> getExpertsByExpertise(String expertise);
}

View file

@ -0,0 +1,92 @@
/*
* 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.expertise.restfull;
import java.util.ArrayList;
import java.util.List;
import org.apache.tuscany.expertise.Expert;
import org.oasisopen.sca.annotation.Init;
import org.oasisopen.sca.annotation.Service;
@Service(ExpertRegistry.class)
public class ExpertRegistryImpl implements ExpertRegistry {
private List<Expert> experts = new ArrayList<Expert>();
public ExpertRegistryImpl() {
}
@Init
public void init() {
Expert expert;
expert = new Expert();
expert.setId("1");
expert.setName("John Smith");
expert.setLocation("CA");
expert.getExpertise().add("SOA");
expert.getExpertise().add("SCA");
expert.getExpertise().add("WAS");
experts.add(expert);
expert = new Expert();
expert.setId("2");
expert.setName("Ken Johnson");
expert.setLocation("NY");
expert.getExpertise().add("Search");
experts.add(expert);
expert = new Expert();
expert.setId("3");
expert.setName("Mark Smith");
expert.setLocation("CA");
expert.getExpertise().add("SCA");
expert.getExpertise().add("WASCE");
experts.add(expert);
}
public void addExpert(Expert expert) {
experts.add(expert);
}
public void removeExpert(Expert expert) {
experts.remove(expert);
}
public List<Expert> getExperts() {
return experts;
}
public List<Expert> getExpertsByExpertise(String expertise) {
List<Expert> expertsByExpertise = new ArrayList<Expert>();
for (Expert expert : experts) {
if (expert.getExpertise().contains(expertise)) {
expertsByExpertise.add(expert);
}
}
return expertsByExpertise;
}
}

View file

@ -0,0 +1,100 @@
/*
* 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.expertise.restfull;
import java.io.IOException;
import java.net.URLDecoder;
import java.util.List;
import javax.servlet.Servlet;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.tuscany.expertise.Expert;
import org.oasisopen.sca.annotation.Reference;
import org.oasisopen.sca.annotation.Scope;
import org.oasisopen.sca.annotation.Service;
@Service(Servlet.class)
@Scope("COMPOSITE")
public class RestfulExpertiseLocator extends HttpServlet implements Servlet {
private static final long serialVersionUID = 4021701701436888369L;
@Reference
protected ExpertRegistry experts;
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String path = URLDecoder.decode(getRequestPath(request), "UTF-8");
String expertise = path.substring(1);
if (expertise != null && expertise.length() > 0 ) {
printExperts(response.getOutputStream(), experts.getExpertsByExpertise(expertise));
} else {
printExperts(response.getOutputStream(), experts.getExperts());
}
}
private void printExperts(ServletOutputStream out, List<Expert> experts) throws IOException {
out.print("<html><body>");
out.print("<h2>Expertise Locator</h2>");
if (experts.size() > 0) {
for (Expert expert: experts) {
out.print("<b>Location:</b> " + expert.getLocation() + " - " + expert.getName() +" ==> ");
for (String expertise : expert.getExpertise()) {
out.print(expertise + ";");
}
out.print("<br/>");
}
} else {
out.println("No experts found");
}
out.print("</body></html>");
}
/**
*
* @param request
* @return
*/
private static String getRequestPath(HttpServletRequest request) {
// Get the request path
String contextPath = request.getContextPath();
String servletPath = request.getServletPath();
String requestURI = request.getRequestURI();
int contextPathLength = request.getContextPath().length();
int servletPathLenght = servletPath.contains(contextPath) ? servletPath.length() - contextPath.length() : servletPath.length();
String requestPath = requestURI.substring(contextPathLength + servletPathLenght);
return requestPath;
}
}

View file

@ -0,0 +1,24 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
* 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.
-->
<contribution xmlns="http://docs.oasis-open.org/ns/opencsa/sca/200912"
xmlns:sample="http://sample">
<deployable composite="sample:rest"/>
</contribution>

View file

@ -0,0 +1,34 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
* 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.
-->
<composite xmlns="http://docs.oasis-open.org/ns/opencsa/sca/200912" xmlns:s="http://sample" xmlns:t="http://tuscany.apache.org/xmlns/sca/1.1" name="rest" targetNamespace="http://sample">
<component name="ExpertRegistryComponent">
<implementation.java class="org.apache.tuscany.expertise.restfull.ExpertRegistryImpl"/>
<service name="ExpertRegistry">
<interface.java interface="org.apache.tuscany.expertise.restfull.ExpertRegistry"/>
</service>
</component>
<component name="RestExpertiseLocatorComponent">
<implementation.java class="org.apache.tuscany.expertise.restfull.RestfulExpertiseLocator"/>
<service name="Servlet">
<t:binding.http uri="http://localhost:8200/experts"/>
</service>
<reference name="experts" target="ExpertRegistryComponent"/>
</component>
</composite>

View file

@ -0,0 +1,71 @@
<?xml version="1.0" encoding="UTF-8"?>
<notation:Diagram xmi:version="2.0" xmlns:xmi="http://www.omg.org/XMI" xmlns:notation="http://www.eclipse.org/gmf/runtime/1.0.2/notation" xmlns:sca="http://www.osoa.org/xmlns/sca/1.0" xmi:id="_RmagEZT2Ed6D4MBlxOyxqQ" type="Sca" measurementUnit="Pixel">
<children xmi:type="notation:Node" xmi:id="_R56q4JT2Ed6D4MBlxOyxqQ" type="2001">
<children xmi:type="notation:Node" xmi:id="_R59uMJT2Ed6D4MBlxOyxqQ" type="5008"/>
<children xmi:type="notation:Node" xmi:id="_R5_jYJT2Ed6D4MBlxOyxqQ" type="7001">
<styles xmi:type="notation:SortingStyle" xmi:id="_R5_jYZT2Ed6D4MBlxOyxqQ"/>
<styles xmi:type="notation:FilteringStyle" xmi:id="_R5_jYpT2Ed6D4MBlxOyxqQ"/>
</children>
<children xmi:type="notation:Node" xmi:id="_R6AKcJT2Ed6D4MBlxOyxqQ" type="7002">
<styles xmi:type="notation:SortingStyle" xmi:id="_R6AKcZT2Ed6D4MBlxOyxqQ"/>
<styles xmi:type="notation:FilteringStyle" xmi:id="_R6AKcpT2Ed6D4MBlxOyxqQ"/>
</children>
<children xmi:type="notation:Node" xmi:id="_R6BYkJT2Ed6D4MBlxOyxqQ" type="7003">
<styles xmi:type="notation:SortingStyle" xmi:id="_R6BYkZT2Ed6D4MBlxOyxqQ"/>
<styles xmi:type="notation:FilteringStyle" xmi:id="_R6BYkpT2Ed6D4MBlxOyxqQ"/>
</children>
<children xmi:type="notation:Node" xmi:id="_R6CmsJT2Ed6D4MBlxOyxqQ" type="7004">
<children xmi:type="notation:Node" xmi:id="_R7gmUJT2Ed6D4MBlxOyxqQ" type="3012">
<children xmi:type="notation:Node" xmi:id="_R7hNYJT2Ed6D4MBlxOyxqQ" type="5007"/>
<children xmi:type="notation:Node" xmi:id="_R7h0cJT2Ed6D4MBlxOyxqQ" type="7009">
<styles xmi:type="notation:SortingStyle" xmi:id="_R7h0cZT2Ed6D4MBlxOyxqQ"/>
<styles xmi:type="notation:FilteringStyle" xmi:id="_R7h0cpT2Ed6D4MBlxOyxqQ"/>
</children>
<children xmi:type="notation:Node" xmi:id="_R7jpoJT2Ed6D4MBlxOyxqQ" type="7010">
<styles xmi:type="notation:SortingStyle" xmi:id="_R7jpoZT2Ed6D4MBlxOyxqQ"/>
<styles xmi:type="notation:FilteringStyle" xmi:id="_R7jpopT2Ed6D4MBlxOyxqQ"/>
</children>
<children xmi:type="notation:Node" xmi:id="_R7k3wJT2Ed6D4MBlxOyxqQ" type="7011">
<styles xmi:type="notation:SortingStyle" xmi:id="_R7k3wZT2Ed6D4MBlxOyxqQ"/>
<styles xmi:type="notation:FilteringStyle" xmi:id="_R7k3wpT2Ed6D4MBlxOyxqQ"/>
</children>
<children xmi:type="notation:Node" xmi:id="_R7le0JT2Ed6D4MBlxOyxqQ" type="7012">
<styles xmi:type="notation:SortingStyle" xmi:id="_R7le0ZT2Ed6D4MBlxOyxqQ"/>
<styles xmi:type="notation:FilteringStyle" xmi:id="_R7le0pT2Ed6D4MBlxOyxqQ"/>
</children>
<styles xmi:type="notation:ShapeStyle" xmi:id="_R7gmUZT2Ed6D4MBlxOyxqQ" fontName="Lucida Grande"/>
<element xmi:type="sca:Component" href="rest.composite#//@composite/@component.0"/>
<layoutConstraint xmi:type="notation:Bounds" xmi:id="_R7gmUpT2Ed6D4MBlxOyxqQ" x="249" y="1"/>
</children>
<children xmi:type="notation:Node" xmi:id="_R7ms8JT2Ed6D4MBlxOyxqQ" type="3012">
<children xmi:type="notation:Node" xmi:id="_R7nUAJT2Ed6D4MBlxOyxqQ" type="5007"/>
<children xmi:type="notation:Node" xmi:id="_R7nUAZT2Ed6D4MBlxOyxqQ" type="7009">
<styles xmi:type="notation:SortingStyle" xmi:id="_R7nUApT2Ed6D4MBlxOyxqQ"/>
<styles xmi:type="notation:FilteringStyle" xmi:id="_R7nUA5T2Ed6D4MBlxOyxqQ"/>
</children>
<children xmi:type="notation:Node" xmi:id="_R7n7EJT2Ed6D4MBlxOyxqQ" type="7010">
<styles xmi:type="notation:SortingStyle" xmi:id="_R7n7EZT2Ed6D4MBlxOyxqQ"/>
<styles xmi:type="notation:FilteringStyle" xmi:id="_R7n7EpT2Ed6D4MBlxOyxqQ"/>
</children>
<children xmi:type="notation:Node" xmi:id="_R7n7E5T2Ed6D4MBlxOyxqQ" type="7011">
<styles xmi:type="notation:SortingStyle" xmi:id="_R7n7FJT2Ed6D4MBlxOyxqQ"/>
<styles xmi:type="notation:FilteringStyle" xmi:id="_R7n7FZT2Ed6D4MBlxOyxqQ"/>
</children>
<children xmi:type="notation:Node" xmi:id="_R7oiIJT2Ed6D4MBlxOyxqQ" type="7012">
<styles xmi:type="notation:SortingStyle" xmi:id="_R7oiIZT2Ed6D4MBlxOyxqQ"/>
<styles xmi:type="notation:FilteringStyle" xmi:id="_R7oiIpT2Ed6D4MBlxOyxqQ"/>
</children>
<styles xmi:type="notation:ShapeStyle" xmi:id="_R7ms8ZT2Ed6D4MBlxOyxqQ" fontName="Lucida Grande"/>
<element xmi:type="sca:Component" href="rest.composite#//@composite/@component.1"/>
<layoutConstraint xmi:type="notation:Bounds" xmi:id="_R7ms8pT2Ed6D4MBlxOyxqQ" x="33" y="1"/>
</children>
<styles xmi:type="notation:SortingStyle" xmi:id="_R6CmsZT2Ed6D4MBlxOyxqQ"/>
<styles xmi:type="notation:FilteringStyle" xmi:id="_R6CmspT2Ed6D4MBlxOyxqQ"/>
</children>
<styles xmi:type="notation:ShapeStyle" xmi:id="_R56q4ZT2Ed6D4MBlxOyxqQ" fontName="Lucida Grande"/>
<element xmi:type="sca:Composite" href="rest.composite#//@composite"/>
<layoutConstraint xmi:type="notation:Bounds" xmi:id="_R56q4pT2Ed6D4MBlxOyxqQ" width="559" height="226"/>
</children>
<styles xmi:type="notation:DiagramStyle" xmi:id="_RmbHIJT2Ed6D4MBlxOyxqQ"/>
<element xmi:type="sca:DocumentRoot" href="rest.composite#/"/>
</notation:Diagram>