summaryrefslogtreecommitdiffstats
path: root/sca-java-1.x/trunk/tutorials/travelsample/services/smsgateway-jms
diff options
context:
space:
mode:
Diffstat (limited to 'sca-java-1.x/trunk/tutorials/travelsample/services/smsgateway-jms')
-rw-r--r--sca-java-1.x/trunk/tutorials/travelsample/services/smsgateway-jms/build.xml47
-rw-r--r--sca-java-1.x/trunk/tutorials/travelsample/services/smsgateway-jms/pom.xml104
-rw-r--r--sca-java-1.x/trunk/tutorials/travelsample/services/smsgateway-jms/src/main/java/scatours/JMSBrokerLauncher.java38
-rw-r--r--sca-java-1.x/trunk/tutorials/travelsample/services/smsgateway-jms/src/main/java/scatours/smsgateway/SMSGateway.java30
-rw-r--r--sca-java-1.x/trunk/tutorials/travelsample/services/smsgateway-jms/src/main/java/scatours/smsgateway/SMSGatewayImpl.java126
-rw-r--r--sca-java-1.x/trunk/tutorials/travelsample/services/smsgateway-jms/src/main/java/scatours/smsgateway/SMSGatewayJMSServiceBootstrap.java40
6 files changed, 385 insertions, 0 deletions
diff --git a/sca-java-1.x/trunk/tutorials/travelsample/services/smsgateway-jms/build.xml b/sca-java-1.x/trunk/tutorials/travelsample/services/smsgateway-jms/build.xml
new file mode 100644
index 0000000000..fe7e66111e
--- /dev/null
+++ b/sca-java-1.x/trunk/tutorials/travelsample/services/smsgateway-jms/build.xml
@@ -0,0 +1,47 @@
+<!--
+ * 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 name="scatours-service-smsgateway-jms" default="compile">
+ <import file="../../antdefs.xml"/>
+
+ <!-- Before invoking the "run" target, the JMS broker needs to be
+ started and running in a different process. -->
+ <target name="run-broker">
+ <java classname="scatours.JMSBrokerLauncher" fork="true">
+ <classpath>
+ <pathelement location="target/${ant.project.name}.jar"/>
+ <!-- The following is used to bring in the ActiveMQ runtime. -->
+ <pathelement location="${env.TUSCANY_HOME}/lib/tuscany-sca-manifest.jar"/>
+ </classpath>
+ </java>
+ </target>
+
+ <!-- The "run" target creates JMS request and response queues for the
+ SMS gateway service. These queues are used by the "run" target of
+ the notification-jms launcher. -->
+ <target name="run">
+ <java classname="scatours.smsgateway.SMSGatewayJMSServiceBootstrap" fork="true">
+ <classpath>
+ <pathelement location="target/${ant.project.name}.jar"/>
+ <!-- The following is used to bring in the ActiveMQ runtime. -->
+ <pathelement location="${env.TUSCANY_HOME}/lib/tuscany-sca-manifest.jar"/>
+ </classpath>
+ </java>
+ </target>
+</project>
diff --git a/sca-java-1.x/trunk/tutorials/travelsample/services/smsgateway-jms/pom.xml b/sca-java-1.x/trunk/tutorials/travelsample/services/smsgateway-jms/pom.xml
new file mode 100644
index 0000000000..8202d4d141
--- /dev/null
+++ b/sca-java-1.x/trunk/tutorials/travelsample/services/smsgateway-jms/pom.xml
@@ -0,0 +1,104 @@
+<?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>scatours</artifactId>
+ <version>1.0-SNAPSHOT</version>
+ <relativePath>../../pom.xml</relativePath>
+ </parent><version>1.0-SNAPSHOT</version>
+ <artifactId>scatours-service-smsgateway-jms</artifactId>
+ <name>Apache Tuscany SCA Tours SMS Gateway JMS Service</name>
+
+ <dependencies>
+ <dependency>
+ <groupId>org.apache.activemq</groupId>
+ <artifactId>activemq-all</artifactId>
+ <version>5.2.0</version>
+ <exclusions>
+ <exclusion>
+ <groupId>org.apache.activemq</groupId>
+ <artifactId>activemq-web-demo</artifactId>
+ </exclusion>
+ </exclusions>
+ </dependency>
+
+ <dependency>
+ <groupId>org.apache.ws.commons.axiom</groupId>
+ <artifactId>axiom-api</artifactId>
+ <version>1.2.7</version>
+ <exclusions>
+ <exclusion>
+ <groupId>xerces</groupId>
+ <artifactId>xercesImpl</artifactId>
+ </exclusion>
+ <exclusion>
+ <groupId>stax</groupId>
+ <artifactId>stax-api</artifactId>
+ </exclusion>
+ <exclusion>
+ <groupId>xml-apis</groupId>
+ <artifactId>xml-apis</artifactId>
+ </exclusion>
+ </exclusions>
+ </dependency>
+
+ <dependency>
+ <groupId>org.apache.ws.commons.axiom</groupId>
+ <artifactId>axiom-impl</artifactId>
+ <version>1.2.7</version>
+ <scope>runtime</scope>
+ <exclusions>
+ <exclusion>
+ <groupId>stax</groupId>
+ <artifactId>stax-api</artifactId>
+ </exclusion>
+ </exclusions>
+ </dependency>
+
+ <dependency>
+ <groupId>junit</groupId>
+ <artifactId>junit</artifactId>
+ <version>4.5</version>
+ <scope>test</scope>
+ </dependency>
+ </dependencies>
+
+ <build>
+ <finalName>${artifactId}</finalName>
+ <plugins>
+ <plugin>
+ <groupId>org.apache.maven.plugins</groupId>
+ <artifactId>maven-jar-plugin</artifactId>
+ <version>2.2</version>
+ <configuration>
+ <archive>
+ <manifest>
+ <addClasspath>true</addClasspath>
+ <classpathPrefix>../lib/</classpathPrefix>
+ <mainClass>scatours.smsgateway.SMSGatewayJMSServiceBootstrap</mainClass>
+ </manifest>
+ </archive>
+ </configuration>
+ </plugin>
+ </plugins>
+ </build>
+</project>
diff --git a/sca-java-1.x/trunk/tutorials/travelsample/services/smsgateway-jms/src/main/java/scatours/JMSBrokerLauncher.java b/sca-java-1.x/trunk/tutorials/travelsample/services/smsgateway-jms/src/main/java/scatours/JMSBrokerLauncher.java
new file mode 100644
index 0000000000..929eb78431
--- /dev/null
+++ b/sca-java-1.x/trunk/tutorials/travelsample/services/smsgateway-jms/src/main/java/scatours/JMSBrokerLauncher.java
@@ -0,0 +1,38 @@
+/*
+ * 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 scatours;
+
+import org.apache.activemq.broker.BrokerService;
+
+public class JMSBrokerLauncher {
+
+ public static void main(String[] args) throws Exception {
+ final BrokerService jmsBroker = new BrokerService();
+ jmsBroker.setPersistent(false);
+ jmsBroker.setUseJmx(false);
+ jmsBroker.addConnector("tcp://localhost:61619");
+ jmsBroker.start();
+
+ System.out.println("JMS Message Broker started");
+ System.out.println("Press enter to shutdown.");
+ System.in.read();
+
+ jmsBroker.stop();
+ }
+}
diff --git a/sca-java-1.x/trunk/tutorials/travelsample/services/smsgateway-jms/src/main/java/scatours/smsgateway/SMSGateway.java b/sca-java-1.x/trunk/tutorials/travelsample/services/smsgateway-jms/src/main/java/scatours/smsgateway/SMSGateway.java
new file mode 100644
index 0000000000..e6e866fa1d
--- /dev/null
+++ b/sca-java-1.x/trunk/tutorials/travelsample/services/smsgateway-jms/src/main/java/scatours/smsgateway/SMSGateway.java
@@ -0,0 +1,30 @@
+/*
+ * 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 scatours.smsgateway;
+
+import java.rmi.Remote;
+import java.rmi.RemoteException;
+
+/**
+ * A gateway to send SMS messages.
+ */
+public interface SMSGateway extends Remote {
+ boolean sendSMS(String fromNumber, String toNumber, String text) throws RemoteException;
+}
diff --git a/sca-java-1.x/trunk/tutorials/travelsample/services/smsgateway-jms/src/main/java/scatours/smsgateway/SMSGatewayImpl.java b/sca-java-1.x/trunk/tutorials/travelsample/services/smsgateway-jms/src/main/java/scatours/smsgateway/SMSGatewayImpl.java
new file mode 100644
index 0000000000..c35aa61513
--- /dev/null
+++ b/sca-java-1.x/trunk/tutorials/travelsample/services/smsgateway-jms/src/main/java/scatours/smsgateway/SMSGatewayImpl.java
@@ -0,0 +1,126 @@
+/*
+ * 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 scatours.smsgateway;
+
+import java.io.ByteArrayInputStream;
+
+import javax.jms.BytesMessage;
+import javax.jms.Destination;
+import javax.jms.JMSException;
+import javax.jms.Message;
+import javax.jms.MessageConsumer;
+import javax.jms.MessageProducer;
+import javax.jms.Session;
+import javax.jms.TextMessage;
+import javax.xml.namespace.QName;
+import javax.xml.stream.XMLStreamException;
+
+import org.apache.axiom.om.OMAbstractFactory;
+import org.apache.axiom.om.OMElement;
+import org.apache.axiom.om.OMFactory;
+import org.apache.axiom.om.OMText;
+import org.apache.axiom.om.impl.builder.StAXOMBuilder;
+
+public class SMSGatewayImpl implements SMSGateway {
+
+ private final Session session;
+ private final MessageConsumer consumer;
+ private final MessageProducer producer;
+
+ public SMSGatewayImpl(Session session) throws JMSException {
+ this.session = session;
+ Destination requestDest = session.createQueue("SMSRequestQueue");
+ consumer = session.createConsumer(requestDest);
+
+ Destination responseDest = session.createQueue("SMSResponseQueue");
+ producer = session.createProducer(responseDest);
+ }
+
+ public void start() throws JMSException, XMLStreamException {
+ while (true) {
+ Message message = consumer.receive();
+ handleRequest(message);
+ }
+ }
+
+ private void handleRequest(Message inMessage) throws XMLStreamException, JMSException {
+ OMElement xml = parseXMLPayload(inMessage);
+
+ String fromNumber = getStringArg(xml, 0);
+ String toNumber = getStringArg(xml, 1);
+ String text = getStringArg(xml, 2);
+
+ boolean result = sendSMS(fromNumber, toNumber, text);
+
+ String responseXML = createXMLResponse(result);
+
+ TextMessage outMessage = createResponseMessage(inMessage.getJMSMessageID(), responseXML);
+ producer.send(outMessage);
+ }
+
+ private OMElement parseXMLPayload(Message inMessage) throws JMSException, XMLStreamException {
+ final byte[] msgData;
+ if (inMessage instanceof TextMessage) {
+ msgData = ((TextMessage)inMessage).getText().getBytes();
+ } else if (inMessage instanceof BytesMessage) {
+ BytesMessage bytesMessage = (BytesMessage)inMessage;
+ msgData = new byte[(int)bytesMessage.getBodyLength()];
+ bytesMessage.readBytes(msgData);
+ } else {
+ throw new JMSException("Unsupported JMS message type of " + inMessage.getClass().getName());
+ }
+ ByteArrayInputStream in = new ByteArrayInputStream(msgData);
+ StAXOMBuilder builder = new StAXOMBuilder(in);
+ OMElement doc = builder.getDocumentElement();
+ return doc;
+ }
+
+ private String getStringArg(OMElement doc, int i) {
+ QName argQName = new QName("arg" + i);
+ OMElement arg = doc.getFirstChildWithName(argQName);
+ if (arg == null) {
+ return null;
+ }
+ return arg.getText();
+ }
+
+ public boolean sendSMS(String fromNumber, String toNumber, String text) {
+ System.out.println("From: " + fromNumber);
+ System.out.println("To: " + toNumber);
+ System.out.println(text);
+ return true;
+ }
+
+ private String createXMLResponse(boolean result) {
+ OMFactory omFactory = OMAbstractFactory.getOMFactory();
+ OMElement response = omFactory.createOMElement("sendSMS", "http://smsgateway.scatours/", "ns2");
+ OMElement returnValue = omFactory.createOMElement("return", null);
+ OMText returnV = omFactory.createOMText(Boolean.toString(result));
+ returnValue.addChild(returnV);
+ response.addChild(returnValue);
+ return response.toString();
+ }
+
+ private TextMessage createResponseMessage(String correlationID, String responseXML) throws JMSException {
+ TextMessage outMessage = session.createTextMessage("sendSMS");
+ outMessage.setText(responseXML);
+ outMessage.setJMSCorrelationID(correlationID);
+ return outMessage;
+ }
+}
diff --git a/sca-java-1.x/trunk/tutorials/travelsample/services/smsgateway-jms/src/main/java/scatours/smsgateway/SMSGatewayJMSServiceBootstrap.java b/sca-java-1.x/trunk/tutorials/travelsample/services/smsgateway-jms/src/main/java/scatours/smsgateway/SMSGatewayJMSServiceBootstrap.java
new file mode 100644
index 0000000000..b4effa6f82
--- /dev/null
+++ b/sca-java-1.x/trunk/tutorials/travelsample/services/smsgateway-jms/src/main/java/scatours/smsgateway/SMSGatewayJMSServiceBootstrap.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 scatours.smsgateway;
+
+import javax.jms.Connection;
+import javax.jms.Session;
+
+import org.apache.activemq.ActiveMQConnectionFactory;
+
+public class SMSGatewayJMSServiceBootstrap {
+
+ public static void main(String[] args) throws Exception {
+ System.out.println("Publishing SMS Gateway Service as a JMS service: tcp://localhost:61619");
+ System.out.println("Press Ctrl^C to terminate...");
+
+ ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory("tcp://localhost:61619");
+ Connection connection = connectionFactory.createConnection();
+ connection.start();
+ Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
+
+ SMSGatewayImpl impl = new SMSGatewayImpl(session);
+ impl.start();
+ }
+}