From a56ad24846d364b48f8cc1d78d94bc10cd9f0aff Mon Sep 17 00:00:00 2001 From: mcombellack Date: Sun, 12 Jul 2009 21:53:48 +0000 Subject: Initial commit of notification service that uses a JMS binding git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@793413 13f79535-47bb-0310-9956-ffa450edef68 --- .../notification-jms-contribution/pom.xml | 57 ++++++++++ .../main/resources/META-INF/sca-contribution.xml | 23 ++++ .../src/main/resources/notification-jms.composite | 42 +++++++ .../launchers/notification-jms-launcher/pom.xml | 121 ++++++++++++++++++++ .../src/main/java/scatours/LaunchJMSBroker.java | 38 +++++++ .../java/scatours/LaunchNotificationJMSNode.java | 57 ++++++++++ .../java/scatours/notification/Notification.java | 26 +++++ .../services/smsgateway-jms-service/pom.xml | 88 ++++++++++++++ .../main/java/scatours/smsgateway/SMSGateway.java | 31 +++++ .../java/scatours/smsgateway/SMSGatewayImpl.java | 126 +++++++++++++++++++++ .../smsgateway/SMSGatewayJMSServiceBootstrap.java | 40 +++++++ 11 files changed, 649 insertions(+) create mode 100644 sandbox/travelsample/contributions/notification-jms-contribution/pom.xml create mode 100644 sandbox/travelsample/contributions/notification-jms-contribution/src/main/resources/META-INF/sca-contribution.xml create mode 100644 sandbox/travelsample/contributions/notification-jms-contribution/src/main/resources/notification-jms.composite create mode 100644 sandbox/travelsample/launchers/notification-jms-launcher/pom.xml create mode 100644 sandbox/travelsample/launchers/notification-jms-launcher/src/main/java/scatours/LaunchJMSBroker.java create mode 100644 sandbox/travelsample/launchers/notification-jms-launcher/src/main/java/scatours/LaunchNotificationJMSNode.java create mode 100644 sandbox/travelsample/launchers/notification-jms-launcher/src/main/java/scatours/notification/Notification.java create mode 100644 sandbox/travelsample/services/smsgateway-jms-service/pom.xml create mode 100644 sandbox/travelsample/services/smsgateway-jms-service/src/main/java/scatours/smsgateway/SMSGateway.java create mode 100644 sandbox/travelsample/services/smsgateway-jms-service/src/main/java/scatours/smsgateway/SMSGatewayImpl.java create mode 100644 sandbox/travelsample/services/smsgateway-jms-service/src/main/java/scatours/smsgateway/SMSGatewayJMSServiceBootstrap.java (limited to 'sandbox') diff --git a/sandbox/travelsample/contributions/notification-jms-contribution/pom.xml b/sandbox/travelsample/contributions/notification-jms-contribution/pom.xml new file mode 100644 index 0000000000..bd76433d10 --- /dev/null +++ b/sandbox/travelsample/contributions/notification-jms-contribution/pom.xml @@ -0,0 +1,57 @@ + + + + 4.0.0 + + org.apache.tuscany.sca + tuscany-sca + 1.6-SNAPSHOT + + 1.0-SNAPSHOT + scatours-notification-jms + Apache Tuscany SCA Tours Notification Contribution Which Uses SMSGateway JMS Service + + + + org.apache.tuscany.sca + tuscany-host-embedded + 1.6-SNAPSHOT + + + + org.apache.tuscany.sca + tuscany-implementation-java-runtime + 1.6-SNAPSHOT + runtime + + + + junit + junit + 4.5 + test + + + + + + ${artifactId} + + diff --git a/sandbox/travelsample/contributions/notification-jms-contribution/src/main/resources/META-INF/sca-contribution.xml b/sandbox/travelsample/contributions/notification-jms-contribution/src/main/resources/META-INF/sca-contribution.xml new file mode 100644 index 0000000000..27cf45a116 --- /dev/null +++ b/sandbox/travelsample/contributions/notification-jms-contribution/src/main/resources/META-INF/sca-contribution.xml @@ -0,0 +1,23 @@ + + + + + + \ No newline at end of file diff --git a/sandbox/travelsample/contributions/notification-jms-contribution/src/main/resources/notification-jms.composite b/sandbox/travelsample/contributions/notification-jms-contribution/src/main/resources/notification-jms.composite new file mode 100644 index 0000000000..2f44ff99ab --- /dev/null +++ b/sandbox/travelsample/contributions/notification-jms-contribution/src/main/resources/notification-jms.composite @@ -0,0 +1,42 @@ + + + + + + + + + + + + + + + + + + + + + diff --git a/sandbox/travelsample/launchers/notification-jms-launcher/pom.xml b/sandbox/travelsample/launchers/notification-jms-launcher/pom.xml new file mode 100644 index 0000000000..66930213f6 --- /dev/null +++ b/sandbox/travelsample/launchers/notification-jms-launcher/pom.xml @@ -0,0 +1,121 @@ + + + + 4.0.0 + + org.apache.tuscany.sca + tuscany-sca + 1.6-SNAPSHOT + + 1.0-SNAPSHOT + scatours-notification-jms-launcher + Apache Tuscany SCA Tours Notification JMS Launcher + + + + org.apache.tuscany.sca + tuscany-node-api + 1.6-SNAPSHOT + + + + org.apache.tuscany.sca + tuscany-implementation-java-runtime + 1.6-SNAPSHOT + runtime + + + + org.apache.tuscany.sca + tuscany-implementation-node-runtime + 1.6-SNAPSHOT + test + + + + org.apache.tuscany.sca + tuscany-host-jms-asf + 1.6-SNAPSHOT + + + + junit + junit + 4.5 + test + + + + org.apache.activemq + activemq-all + 5.2.0 + + + org.apache.activemq + activemq-web-demo + + + + + + org.apache.ws.commons.axiom + axiom-api + 1.2.7 + + + xerces + xercesImpl + + + stax + stax-api + + + xml-apis + xml-apis + + + + + + org.apache.ws.commons.axiom + axiom-impl + 1.2.7 + runtime + + + stax + stax-api + + + + + + junit + junit + 4.5 + test + + + + + ${artifactId} + + diff --git a/sandbox/travelsample/launchers/notification-jms-launcher/src/main/java/scatours/LaunchJMSBroker.java b/sandbox/travelsample/launchers/notification-jms-launcher/src/main/java/scatours/LaunchJMSBroker.java new file mode 100644 index 0000000000..fee4c357a1 --- /dev/null +++ b/sandbox/travelsample/launchers/notification-jms-launcher/src/main/java/scatours/LaunchJMSBroker.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 LaunchJMSBroker { + + 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/sandbox/travelsample/launchers/notification-jms-launcher/src/main/java/scatours/LaunchNotificationJMSNode.java b/sandbox/travelsample/launchers/notification-jms-launcher/src/main/java/scatours/LaunchNotificationJMSNode.java new file mode 100644 index 0000000000..2715a56fba --- /dev/null +++ b/sandbox/travelsample/launchers/notification-jms-launcher/src/main/java/scatours/LaunchNotificationJMSNode.java @@ -0,0 +1,57 @@ +/* + * 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.tuscany.sca.node.SCAClient; +import org.apache.tuscany.sca.node.SCAContribution; +import org.apache.tuscany.sca.node.SCANode; +import org.apache.tuscany.sca.node.SCANodeFactory; + +import scatours.notification.Notification; + +public class LaunchNotificationJMSNode { + + public static void main(String[] args) throws Exception { + SCAContribution notificationContribution = + new SCAContribution("notification", + "../../contributions/notification-contribution/target/classes"); + SCAContribution notificationJMSContribution = + new SCAContribution("notification-jms", + "../../contributions/notification-jms-contribution/target/classes"); + + SCANode node = SCANodeFactory.newInstance().createSCANode( + "notification-jms.composite", notificationContribution, notificationJMSContribution); + + try { + node.start(); + + System.out.println("Quick notification test"); + Notification notification = ((SCAClient)node).getService( + Notification.class, "Notification"); + String accountID = "1234"; + String subject = "Holiday payment taken"; + String message = "Payment of £102.37 accepted..."; + notification.notify(accountID, subject, message); + System.out.println("Notification sent"); + } finally { + node.stop(); + } + } +} diff --git a/sandbox/travelsample/launchers/notification-jms-launcher/src/main/java/scatours/notification/Notification.java b/sandbox/travelsample/launchers/notification-jms-launcher/src/main/java/scatours/notification/Notification.java new file mode 100644 index 0000000000..79706fcc9d --- /dev/null +++ b/sandbox/travelsample/launchers/notification-jms-launcher/src/main/java/scatours/notification/Notification.java @@ -0,0 +1,26 @@ +/* + * 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.notification; + +public interface Notification { + + boolean notify(String accountID, String subject, String message); + +} diff --git a/sandbox/travelsample/services/smsgateway-jms-service/pom.xml b/sandbox/travelsample/services/smsgateway-jms-service/pom.xml new file mode 100644 index 0000000000..d85687e1fa --- /dev/null +++ b/sandbox/travelsample/services/smsgateway-jms-service/pom.xml @@ -0,0 +1,88 @@ + + + + 4.0.0 + + org.apache.tuscany.sca + tuscany-sca + 1.6-SNAPSHOT + + 1.0-SNAPSHOT + smsgateway-jms-service + Apache Tuscany SCA Tours SMS Gateway JMS Service + + + + org.apache.activemq + activemq-all + 5.2.0 + + + org.apache.activemq + activemq-web-demo + + + + + + org.apache.ws.commons.axiom + axiom-api + 1.2.7 + + + xerces + xercesImpl + + + stax + stax-api + + + xml-apis + xml-apis + + + + + + org.apache.ws.commons.axiom + axiom-impl + 1.2.7 + runtime + + + stax + stax-api + + + + + + junit + junit + 4.5 + test + + + + + ${artifactId} + + diff --git a/sandbox/travelsample/services/smsgateway-jms-service/src/main/java/scatours/smsgateway/SMSGateway.java b/sandbox/travelsample/services/smsgateway-jms-service/src/main/java/scatours/smsgateway/SMSGateway.java new file mode 100644 index 0000000000..a900dfecee --- /dev/null +++ b/sandbox/travelsample/services/smsgateway-jms-service/src/main/java/scatours/smsgateway/SMSGateway.java @@ -0,0 +1,31 @@ +/* + * 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/sandbox/travelsample/services/smsgateway-jms-service/src/main/java/scatours/smsgateway/SMSGatewayImpl.java b/sandbox/travelsample/services/smsgateway-jms-service/src/main/java/scatours/smsgateway/SMSGatewayImpl.java new file mode 100644 index 0000000000..adacac77f5 --- /dev/null +++ b/sandbox/travelsample/services/smsgateway-jms-service/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/sandbox/travelsample/services/smsgateway-jms-service/src/main/java/scatours/smsgateway/SMSGatewayJMSServiceBootstrap.java b/sandbox/travelsample/services/smsgateway-jms-service/src/main/java/scatours/smsgateway/SMSGatewayJMSServiceBootstrap.java new file mode 100644 index 0000000000..6fe08a44b5 --- /dev/null +++ b/sandbox/travelsample/services/smsgateway-jms-service/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"); + + 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(); + } +} -- cgit v1.2.3