Title: [715] trunk/assembly: Add loan broker demo (first implementation)

Diff

Modified: trunk/assembly/project.xml (714 => 715)

--- trunk/assembly/project.xml	2005-11-02 00:29:19 UTC (rev 714)
+++ trunk/assembly/project.xml	2005-11-02 00:30:31 UTC (rev 715)
@@ -20,7 +20,6 @@
       <version>${pom.currentVersion}</version>
       <properties>
         <servicemix.module>true</servicemix.module>
-        <lib>true</lib>
         <eclipse.dependency>true</eclipse.dependency>
       </properties>
     </dependency>
@@ -31,7 +30,6 @@
       <version>${pom.currentVersion}</version>
       <properties>
         <servicemix.module>true</servicemix.module>
-        <lib>true</lib>
         <eclipse.dependency>true</eclipse.dependency>
       </properties>
     </dependency>
@@ -42,7 +40,6 @@
       <version>${pom.currentVersion}</version>
       <properties>
         <servicemix.module>true</servicemix.module>
-        <optional>true</optional>
         <eclipse.dependency>true</eclipse.dependency>
       </properties>
     </dependency>
@@ -53,7 +50,6 @@
       <version>${pom.currentVersion}</version>
       <properties>
         <servicemix.module>true</servicemix.module>
-        <optional>true</optional>
         <eclipse.dependency>true</eclipse.dependency>
       </properties>
     </dependency>
@@ -64,7 +60,6 @@
       <version>${pom.currentVersion}</version>
       <properties>
         <servicemix.module>true</servicemix.module>
-        <optional>true</optional>
         <eclipse.dependency>true</eclipse.dependency>
       </properties>
     </dependency>
@@ -333,6 +328,16 @@
         <optional>true</optional>
       </properties>
     </dependency>
+    
+    <!-- Lingo -->
+    <dependency>
+      <groupId>lingo</groupId>
+      <artifactId>lingo</artifactId>
+      <version>${lingo_version}</version>
+      <properties>
+        <optional>true</optional>
+      </properties>
+    </dependency>
 
   </dependencies>
 

Added: trunk/assembly/src/release/examples/loan-broker/README.txt (714 => 715)

--- trunk/assembly/src/release/examples/loan-broker/README.txt	2005-11-02 00:29:19 UTC (rev 714)
+++ trunk/assembly/src/release/examples/loan-broker/README.txt	2005-11-02 00:30:31 UTC (rev 715)
@@ -0,0 +1,19 @@
+Welcome to the ServiceMixBPEL Example
+=============================================
+
+This example uses shows how to connect the JMS bindings to a BPEL service component provided by the PXE AsyncProcess example - see www.fivesight.com
+
+To start the servicemix server using the sample configuration, just run:
+
+../../bin/servicemix servicemix.xml
+
+This will start a component which sends a soap message to a JmsServiceComponent which forwards the request to the PXE BPEL engine and waits for a response.
+
+A simple JMS client is provided so that messages can sent and received from the JMSService.  The client is built
+and run from source using Ant, http://ant.apache.org. Just execute 'ant' from the current directory to
+run the JMS client.
+
+
+For more information on this example please see
+
+http://servicemix.org/BPEL+example
\ No newline at end of file

Added: trunk/assembly/src/release/examples/loan-broker/activemq-broker.xml (714 => 715)

--- trunk/assembly/src/release/examples/loan-broker/activemq-broker.xml	2005-11-02 00:29:19 UTC (rev 714)
+++ trunk/assembly/src/release/examples/loan-broker/activemq-broker.xml	2005-11-02 00:30:31 UTC (rev 715)
@@ -0,0 +1,20 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<beans xmlns="http://xbean.org/schemas/spring/1.0" 
+	   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
+	   xmlns:sm="http://servicemix.org/config/1.0"
+	   xmlns:lb="http://servicemix.org/demos/loan-broker"
+	   xsi:schemaLocation="http://xbean.org/schemas/spring/1.0 ../../conf/spring-beans.xsd 
+	                       http://servicemix.org/config/1.0 ../../conf/servicemix.xsd">
+	                       
+	<sm:container name="jbi"
+	              monitorInstallationDirectory="false"
+	              createMBeanServer="false"
+	              useMBeanServer="false">
+	</sm:container>                
+	                       
+	<!-- message broker -->
+	<bean id="broker" class="org.activemq.spring.BrokerFactoryBean">
+        <property name="config" value="classpath:activemq.xml" />
+	</bean>
+
+</beans>

Added: trunk/assembly/src/release/examples/loan-broker/build.xml (714 => 715)

--- trunk/assembly/src/release/examples/loan-broker/build.xml	2005-11-02 00:29:19 UTC (rev 714)
+++ trunk/assembly/src/release/examples/loan-broker/build.xml	2005-11-02 00:30:31 UTC (rev 715)
@@ -0,0 +1,47 @@
+<project name="jms-binding" default="run" basedir=".">
+
+  <property name="class.dir" value="classes"/>
+
+  <target name="clean">
+    <delete dir="target" quiet="true"/>
+    <delete dir="${class.dir}" quiet="true"/>
+  </target>
+
+  <target name="init">
+    <mkdir dir="${class.dir}"/>
+    <mkdir dir="src/ddl"/>
+
+    <path id="javac.classpath">
+      <pathelement path="${class.dir}"/>
+      <pathelement path="../../conf"/>
+      <fileset dir="../../lib">
+        <include name="**/*.jar"/>
+      </fileset>
+      <fileset dir="../..">
+        <include name="*.jar"/>
+      </fileset>
+    </path>
+  </target>
+
+  <target name="compile" depends="init" description="Compile all Java">
+    <javac srcdir="src" destdir="${class.dir}" debug="on">
+      <classpath refid="javac.classpath"/>
+    </javac>
+  </target>
+
+  <target name="run" depends="compile" description="Runs the example client">
+    <echo>Running exsample client</echo>
+    <java classname="JMSClient" fork="yes" maxmemory="100M">
+      <classpath refid="javac.classpath"/>
+    </java>
+  </target>
+	
+  <target name="run-loop" depends="compile" description="Runs the example client">
+    <echo>Running exsample client</echo>
+    <java classname="JMSClient" fork="yes" maxmemory="100M">
+      <classpath refid="javac.classpath"/>
+      <arg value="100000"/>
+    </java>
+  </target>
+	
+</project>

Added: trunk/assembly/src/release/examples/loan-broker/servicemix-alone.xml (714 => 715)

--- trunk/assembly/src/release/examples/loan-broker/servicemix-alone.xml	2005-11-02 00:29:19 UTC (rev 714)
+++ trunk/assembly/src/release/examples/loan-broker/servicemix-alone.xml	2005-11-02 00:30:31 UTC (rev 715)
@@ -0,0 +1,88 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<beans xmlns="http://xbean.org/schemas/spring/1.0" 
+	   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
+	   xmlns:sm="http://servicemix.org/config/1.0"
+	   xmlns:lb="http://servicemix.org/demos/loan-broker"
+	   xsi:schemaLocation="http://xbean.org/schemas/spring/1.0 ../../conf/spring-beans.xsd 
+	                       http://servicemix.org/config/1.0 ../../conf/servicemix.xsd">
+	                       
+	<sm:container id="jbi"
+	              monitorInstallationDirectory="true"
+	              createMBeanServer="true"
+	              useMBeanServer="true"
+                dumpStats="true" 
+                statsInterval="10">
+		<sm:activationSpecs>
+		
+		  <!-- In/out binding -->
+		  <sm:activationSpec componentName="loanBrokerJmsBinding"
+		                     destinationService="lb:loan-broker">
+		    <sm:component>
+		      <bean class="org.servicemix.components.jms.JmsServiceComponent">
+		        <property name="template">
+		          <bean class="org.springframework.jms.core.JmsTemplate">
+		            <property name="connectionFactory" ref="jmsFactory" />
+					<property name="defaultDestinationName" value="demo.org.servicemix.source" />
+					<property name="pubSubDomain" value="false" />
+		          </bean>
+		        </property>
+		      </bean>
+		    </sm:component>
+		  </sm:activationSpec>
+		
+		  <!-- Loan broker component -->
+		  <sm:activationSpec componentName="loanBroker" 
+                             service="lb:loan-broker">
+		    <sm:component>
+		      <bean class="loanbroker.LoanBroker" />
+		    </sm:component>
+		  </sm:activationSpec>
+		  
+		  <!-- Credit Agency -->
+		  <sm:activationSpec componentName="creditAgency"
+		                     service="lb:credit-agency">
+		    <sm:component>
+		      <bean class="loanbroker.CreditAgency" />
+		    </sm:component>
+		  </sm:activationSpec>
+		  
+		  <!-- Lender Gateway -->
+		  <sm:activationSpec componentName="lenderGateway"
+		                     service="lb:lender-gateway">
+		    <sm:component>
+		      <bean class="loanbroker.LenderGateway" />
+		    </sm:component>
+		  </sm:activationSpec>
+		  
+		  <!-- Banks -->
+		  <sm:activationSpec componentName="bank1" service="lb:bank1">
+		    <sm:component><bean class="loanbroker.Bank" /></sm:component>
+		  </sm:activationSpec>
+		  <sm:activationSpec componentName="bank2" service="lb:bank2">
+		    <sm:component><bean class="loanbroker.Bank" /></sm:component>
+		  </sm:activationSpec>
+		  <sm:activationSpec componentName="bank3" service="lb:bank3">
+		    <sm:component><bean class="loanbroker.Bank" /></sm:component>
+		  </sm:activationSpec>
+		  <sm:activationSpec componentName="bank4" service="lb:bank4">
+		    <sm:component><bean class="loanbroker.Bank" /></sm:component>
+		  </sm:activationSpec>
+		  <sm:activationSpec componentName="bank5" service="lb:bank5">
+		    <sm:component><bean class="loanbroker.Bank" /></sm:component>
+		  </sm:activationSpec>
+		  
+		</sm:activationSpecs>
+	</sm:container>                
+	                       
+	<bean id="transactionContextManager" class="org.jencks.factory.TransactionContextManagerFactoryBean"/>
+	<bean id="transactionManager" class="org.jencks.factory.GeronimoTransactionManagerFactoryBean" />
+
+	<bean id="jmsFactory" class="org.activemq.pool.PooledConnectionFactory">
+		<property name="connectionFactory">
+			<bean class="org.activemq.ActiveMQConnectionFactory">
+				<property name="brokerURL" value="reliable:(tcp://localhost:61616)" />
+			</bean>
+		</property>
+	</bean>
+
+</beans>

Added: trunk/assembly/src/release/examples/loan-broker/servicemix.xml (714 => 715)

--- trunk/assembly/src/release/examples/loan-broker/servicemix.xml	2005-11-02 00:29:19 UTC (rev 714)
+++ trunk/assembly/src/release/examples/loan-broker/servicemix.xml	2005-11-02 00:30:31 UTC (rev 715)
@@ -0,0 +1,92 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<beans xmlns="http://xbean.org/schemas/spring/1.0" 
+	   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
+	   xmlns:sm="http://servicemix.org/config/1.0"
+	   xmlns:lb="http://servicemix.org/demos/loan-broker"
+	   xsi:schemaLocation="http://xbean.org/schemas/spring/1.0 ../../conf/spring-beans.xsd 
+	                       http://servicemix.org/config/1.0 ../../conf/servicemix.xsd">
+	                       
+	<sm:container name="jbi"
+	              monitorInstallationDirectory="false"
+	              createMBeanServer="true"
+	              useMBeanServer="true">
+		<sm:activationSpecs>
+		
+		  <!-- In/out binding -->
+		  <sm:activationSpec componentName="loanBrokerJmsBinding"
+		                     destinationService="lb:loan-broker">
+		    <sm:component>
+		      <bean class="org.servicemix.components.jms.JmsServiceComponent"
+		            depends-on="broker">
+		        <property name="template">
+		          <bean class="org.springframework.jms.core.JmsTemplate">
+		            <property name="connectionFactory" ref="jmsFactory" />
+					<property name="defaultDestinationName" value="demo.org.servicemix.source" />
+					<property name="pubSubDomain" value="false" />
+		          </bean>
+		        </property>
+		      </bean>
+		    </sm:component>
+		  </sm:activationSpec>
+		
+		  <!-- Loan broker component -->
+		  <sm:activationSpec componentName="loanBroker" 
+                             service="lb:loan-broker">
+		    <sm:component>
+		      <bean class="loanbroker.LoanBroker" />
+		    </sm:component>
+		  </sm:activationSpec>
+		  
+		  <!-- Credit Agency -->
+		  <sm:activationSpec componentName="creditAgency"
+		                     service="lb:credit-agency">
+		    <sm:component>
+		      <bean class="loanbroker.CreditAgency" />
+		    </sm:component>
+		  </sm:activationSpec>
+		  
+		  <!-- Lender Gateway -->
+		  <sm:activationSpec componentName="lenderGateway"
+		                     service="lb:lender-gateway">
+		    <sm:component>
+		      <bean class="loanbroker.LenderGateway" />
+		    </sm:component>
+		  </sm:activationSpec>
+		  
+		  <!-- Banks -->
+		  <sm:activationSpec componentName="bank1" service="lb:bank1">
+		    <sm:component><bean class="loanbroker.Bank" /></sm:component>
+		  </sm:activationSpec>
+		  <sm:activationSpec componentName="bank2" service="lb:bank2">
+		    <sm:component><bean class="loanbroker.Bank" /></sm:component>
+		  </sm:activationSpec>
+		  <sm:activationSpec componentName="bank3" service="lb:bank3">
+		    <sm:component><bean class="loanbroker.Bank" /></sm:component>
+		  </sm:activationSpec>
+		  <sm:activationSpec componentName="bank4" service="lb:bank4">
+		    <sm:component><bean class="loanbroker.Bank" /></sm:component>
+		  </sm:activationSpec>
+		  <sm:activationSpec componentName="bank5" service="lb:bank5">
+		    <sm:component><bean class="loanbroker.Bank" /></sm:component>
+		  </sm:activationSpec>
+		  
+		</sm:activationSpecs>
+	</sm:container>                
+	                       
+	<!-- message broker -->
+	<bean id="broker" class="org.activemq.spring.BrokerFactoryBean">
+        <property name="config" value="classpath:activemq.xml" />
+	</bean>
+
+	<bean id="transactionContextManager" class="org.jencks.factory.TransactionContextManagerFactoryBean"/>
+	<bean id="transactionManager" class="org.jencks.factory.GeronimoTransactionManagerFactoryBean" />
+
+	<bean id="jmsFactory" class="org.activemq.pool.PooledConnectionFactory">
+		<property name="connectionFactory">
+			<bean class="org.activemq.ActiveMQConnectionFactory">
+				<property name="brokerURL" value="tcp://localhost:61616" />
+			</bean>
+		</property>
+	</bean>
+
+</beans>

Added: trunk/assembly/src/release/examples/loan-broker/src/JMSClient.java (714 => 715)

--- trunk/assembly/src/release/examples/loan-broker/src/JMSClient.java	2005-11-02 00:29:19 UTC (rev 714)
+++ trunk/assembly/src/release/examples/loan-broker/src/JMSClient.java	2005-11-02 00:30:31 UTC (rev 715)
@@ -0,0 +1,113 @@
+/** 
+ * 
+ * Copyright 2005 RAJD Consultanct Ltd
+ * 
+ * Licensed 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. 
+ * 
+ **/
+import edu.emory.mathcs.backport.java.util.concurrent.CountDownLatch;
+
+import org.activemq.ActiveMQConnectionFactory;
+import org.activemq.message.ActiveMQQueue;
+import org.apache.geronimo.connector.work.GeronimoWorkManager;
+import org.apache.geronimo.transaction.ExtendedTransactionManager;
+import org.apache.geronimo.transaction.context.TransactionContextManager;
+import org.jencks.factory.TransactionContextManagerFactoryBean;
+import org.jencks.factory.TransactionManagerFactoryBean;
+import org.jencks.factory.WorkManagerFactoryBean;
+import org.logicblaze.lingo.jms.Requestor;
+import org.logicblaze.lingo.jms.impl.MultiplexingRequestor;
+
+import javax.jms.ConnectionFactory;
+import javax.jms.Destination;
+import javax.jms.Message;
+import javax.resource.spi.work.Work;
+
+/**
+ * @version $Revision: 622 $
+ */
+public class JMSClient implements Work {
+    
+    private static ConnectionFactory factory;
+    private static CountDownLatch latch;
+    private static Requestor requestor;
+    
+    /**
+     * main ...
+     * 
+     * @param args
+     * @throws Exception
+     */
+    public static void main(String[] args) throws Exception {
+        System.out.println("Connecting to JMS server.");
+        factory = new ActiveMQConnectionFactory("tcp://localhost:61616");
+        Destination inQueue = new ActiveMQQueue("demo.org.servicemix.source");
+        Destination outQueue = new ActiveMQQueue("demo.org.servicemix.output");
+        requestor = MultiplexingRequestor.newInstance(factory, inQueue, outQueue); 
+        
+        if (args.length == 0) {
+            new JMSClient().run();
+        } else {
+            GeronimoWorkManager wm = createWorkManager(30);
+            int nb = Integer.parseInt(args[0]);
+            latch = new CountDownLatch(nb);
+            for (int i = 0; i < nb; i++) {
+                wm.scheduleWork(new JMSClient());
+            }
+            latch.await();
+            wm.doStop();
+        }
+        System.out.println("Closing.");
+        requestor.close();
+    }
+    
+    protected static GeronimoWorkManager createWorkManager(int poolSize) throws Exception {
+        TransactionManagerFactoryBean tmfb = new TransactionManagerFactoryBean();
+        tmfb.afterPropertiesSet();
+        TransactionContextManagerFactoryBean tcmfb = new TransactionContextManagerFactoryBean();
+        tcmfb.setTransactionManager((ExtendedTransactionManager) tmfb.getObject());
+        tcmfb.afterPropertiesSet();
+        WorkManagerFactoryBean wmfb = new WorkManagerFactoryBean();
+        wmfb.setTransactionContextManager((TransactionContextManager) tcmfb.getObject());
+        wmfb.setThreadPoolSize(poolSize);
+        wmfb.afterPropertiesSet();
+        GeronimoWorkManager wm = wmfb.getWorkManager();
+        return wm;
+    }
+
+    public void run() {
+        try {
+            System.out.println("Sending request.");
+            Message out = requestor.getSession().createMapMessage();
+            out.setStringProperty("ssn", "012-24532-53254");
+            out.setDoubleProperty("amount", Math.random() * 100000);
+            out.setIntProperty("duration", (int) Math.random() * 48);
+            Message in = requestor.request(null, out);
+            if (in == null) {
+                System.out.println("Response timed out.");
+            }
+            else {
+                System.out.println("Response was: " + in.getDoubleProperty("rate") + " from " + in.getStringProperty("bank"));
+            }
+        } catch (Exception e) {
+            e.printStackTrace();
+        } finally {
+            if (latch != null) {
+                latch.countDown();
+            }
+        }
+    }
+
+    public void release() {
+    }
+}

Added: trunk/assembly/src/release/examples/loan-broker/src/loanbroker/Bank.java (714 => 715)

--- trunk/assembly/src/release/examples/loan-broker/src/loanbroker/Bank.java	2005-11-02 00:29:19 UTC (rev 714)
+++ trunk/assembly/src/release/examples/loan-broker/src/loanbroker/Bank.java	2005-11-02 00:30:31 UTC (rev 715)
@@ -0,0 +1,45 @@
+/** 
+ * 
+ * Copyright 2005 LogicBlaze, Inc. http://www.logicblaze.com
+ * 
+ * Licensed 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 loanbroker;
+
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+import org.servicemix.components.util.TransformComponentSupport;
+
+import javax.jbi.messaging.MessageExchange;
+import javax.jbi.messaging.MessagingException;
+import javax.jbi.messaging.NormalizedMessage;
+
+public class Bank extends TransformComponentSupport {
+
+    private static final Log log = LogFactory.getLog(Bank.class); 
+
+    protected boolean transform(MessageExchange exchange, NormalizedMessage in, NormalizedMessage out) throws MessagingException {
+        log.info("Receiving bank request");
+        double rate = Math.random() * 10;
+        out.setProperty(Constants.PROPERTY_RATE, new Double(rate));
+        // Sleep some time
+        try {
+            Thread.sleep((int) (Math.random() * 10) * 100);
+        } catch (InterruptedException e) {
+            // Discard
+        }
+        return true;
+    }
+
+}

Added: trunk/assembly/src/release/examples/loan-broker/src/loanbroker/Constants.java (714 => 715)

--- trunk/assembly/src/release/examples/loan-broker/src/loanbroker/Constants.java	2005-11-02 00:29:19 UTC (rev 714)
+++ trunk/assembly/src/release/examples/loan-broker/src/loanbroker/Constants.java	2005-11-02 00:30:31 UTC (rev 715)
@@ -0,0 +1,38 @@
+/** 
+ * 
+ * Copyright 2005 LogicBlaze, Inc. http://www.logicblaze.com
+ * 
+ * Licensed 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 loanbroker;
+
+public interface Constants {
+
+    String LOANBROKER_NS = "http://servicemix.org/demos/loan-broker";
+    
+    String LOANBROKER_SERVICE = "loan-broker";
+    String CREDITAGENCY_SERVICE = "credit-agency";
+    String LENDERGATEWAY_SERVICE = "lender-gateway";
+    
+    String PROPERTY_SSN = "ssn";
+    String PROPERTY_AMOUNT = "amount";
+    String PROPERTY_DURATION = "duration";
+    String PROPERTY_SCORE = "score";
+    String PROPERTY_HISTORYLENGTH = "hlength";
+    String PROPERTY_RECIPIENTS = "recipients";
+    String PROPERTY_CORRELATIONID = "correlationId";
+    String PROPERTY_RATE = "rate";
+    String PROPERTY_BANK = "bank";
+
+}

Added: trunk/assembly/src/release/examples/loan-broker/src/loanbroker/CreditAgency.java (714 => 715)

--- trunk/assembly/src/release/examples/loan-broker/src/loanbroker/CreditAgency.java	2005-11-02 00:29:19 UTC (rev 714)
+++ trunk/assembly/src/release/examples/loan-broker/src/loanbroker/CreditAgency.java	2005-11-02 00:30:31 UTC (rev 715)
@@ -0,0 +1,41 @@
+/** 
+ * 
+ * Copyright 2005 LogicBlaze, Inc. http://www.logicblaze.com
+ * 
+ * Licensed 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 loanbroker;
+
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+import org.servicemix.components.util.TransformComponentSupport;
+
+import javax.jbi.messaging.MessageExchange;
+import javax.jbi.messaging.MessagingException;
+import javax.jbi.messaging.NormalizedMessage;
+
+public class CreditAgency extends TransformComponentSupport {
+
+    private static final Log log = LogFactory.getLog(CreditAgency.class); 
+
+    protected boolean transform(MessageExchange exchange, NormalizedMessage in, NormalizedMessage out) throws MessagingException {
+        log.info("Receiving credit agency request");
+        int score = (int) (Math.random() * 600 + 300);
+        int hlength = (int) (Math.random() * 19 + 1);
+        out.setProperty(Constants.PROPERTY_SCORE, new Integer(score));
+        out.setProperty(Constants.PROPERTY_HISTORYLENGTH, new Integer(hlength));
+        return true;
+    }
+
+}

Added: trunk/assembly/src/release/examples/loan-broker/src/loanbroker/LenderGateway.java (714 => 715)

--- trunk/assembly/src/release/examples/loan-broker/src/loanbroker/LenderGateway.java	2005-11-02 00:29:19 UTC (rev 714)
+++ trunk/assembly/src/release/examples/loan-broker/src/loanbroker/LenderGateway.java	2005-11-02 00:30:31 UTC (rev 715)
@@ -0,0 +1,53 @@
+/** 
+ * 
+ * Copyright 2005 LogicBlaze, Inc. http://www.logicblaze.com
+ * 
+ * Licensed 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 loanbroker;
+
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+import org.servicemix.components.util.TransformComponentSupport;
+
+import javax.jbi.messaging.MessageExchange;
+import javax.jbi.messaging.MessagingException;
+import javax.jbi.messaging.NormalizedMessage;
+import javax.xml.namespace.QName;
+
+public class LenderGateway extends TransformComponentSupport {
+
+    private static final Log log = LogFactory.getLog(LenderGateway.class); 
+
+    protected boolean transform(MessageExchange exchange, NormalizedMessage in, NormalizedMessage out) throws MessagingException {
+        log.info("Receiving lender gateway request");
+        double amount = ((Double) in.getProperty(Constants.PROPERTY_AMOUNT)).doubleValue();
+        int score = ((Integer) in.getProperty(Constants.PROPERTY_SCORE)).intValue();
+        int hlength = ((Integer) in.getProperty(Constants.PROPERTY_HISTORYLENGTH)).intValue();
+        QName[] recipients;
+        if (amount >= 75000.0 && score >= 600 && hlength >= 8) {
+            recipients = new QName[] { new QName(Constants.LOANBROKER_NS, "bank1"), 
+                                       new QName(Constants.LOANBROKER_NS, "bank2") };
+        } else
+        if (amount >= 10000.0 && amount < 75000.0 && score >= 400 && hlength >= 3) {
+            recipients = new QName[] { new QName(Constants.LOANBROKER_NS, "bank3"), 
+                                       new QName(Constants.LOANBROKER_NS, "bank4") };
+        } else {
+            recipients = new QName[] { new QName(Constants.LOANBROKER_NS, "bank5") };
+        }
+        out.setProperty(Constants.PROPERTY_RECIPIENTS, recipients);
+        return true;
+    }
+
+}

Added: trunk/assembly/src/release/examples/loan-broker/src/loanbroker/LoanBroker.java (714 => 715)

--- trunk/assembly/src/release/examples/loan-broker/src/loanbroker/LoanBroker.java	2005-11-02 00:29:19 UTC (rev 714)
+++ trunk/assembly/src/release/examples/loan-broker/src/loanbroker/LoanBroker.java	2005-11-02 00:30:31 UTC (rev 715)
@@ -0,0 +1,191 @@
+/** 
+ * 
+ * Copyright 2005 LogicBlaze, Inc. http://www.logicblaze.com
+ * 
+ * Licensed 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 loanbroker;
+
+import EDU.oswego.cs.dl.util.concurrent.ConcurrentHashMap;
+
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+import org.servicemix.MessageExchangeListener;
+import org.servicemix.components.util.ComponentSupport;
+
+import javax.jbi.messaging.ExchangeStatus;
+import javax.jbi.messaging.InOut;
+import javax.jbi.messaging.MessageExchange;
+import javax.jbi.messaging.MessagingException;
+import javax.jbi.messaging.NormalizedMessage;
+import javax.jbi.messaging.MessageExchange.Role;
+import javax.jbi.servicedesc.ServiceEndpoint;
+import javax.xml.namespace.QName;
+
+import java.util.ArrayList;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Map;
+
+public class LoanBroker extends ComponentSupport implements MessageExchangeListener {
+    
+    private static final Log log = LogFactory.getLog(LoanBroker.class); 
+
+    public LoanBroker() {
+        super(new QName(Constants.LOANBROKER_NS, Constants.LOANBROKER_SERVICE), "input");
+    }
+    
+    public void onMessageExchange(MessageExchange exchange) throws MessagingException {
+        // Provider role
+        if (exchange.getRole() == Role.PROVIDER) {
+            processInputRequest(exchange);
+        // Consumer role
+        } else {
+            ServiceEndpoint ep = exchange.getEndpoint();
+            // Credit agency response
+            if (ep.getServiceName().getLocalPart().equals(Constants.CREDITAGENCY_SERVICE)) {
+                processCreditAgencyResponse(exchange);
+            } else if (ep.getServiceName().getLocalPart().equals(Constants.LENDERGATEWAY_SERVICE)) {
+                processLenderGatewayResponse(exchange);
+            } else {
+                processLoanQuote(exchange);
+            }
+        }
+    }
+
+    private void processLoanQuote(MessageExchange exchange) throws MessagingException {
+        log.info("Receiving loan quote");
+        // Get aggregation
+        String id = (String) getProperty(exchange, Constants.PROPERTY_CORRELATIONID);
+        Aggregation ag = (Aggregation) aggregations.get(id);
+        // Get info from quote
+        LoanQuote q = new LoanQuote();
+        q.bank = exchange.getEndpoint().getServiceName().getLocalPart();
+        q.rate = (Double) getOutProperty(exchange, Constants.PROPERTY_RATE);
+        done(exchange);
+        // Check if all quotes are received
+        synchronized (ag) {
+            ag.quotes.add(q);
+            if (ag.quotes.size() == ag.numbers) {
+                LoanQuote best = null;
+                for (Iterator iter = ag.quotes.iterator(); iter.hasNext();) {
+                    q = (LoanQuote) iter.next();
+                    if (best == null || q.rate.doubleValue() < best.rate.doubleValue()) {
+                        best = q;
+                    }
+                }
+                NormalizedMessage response = ag.request.createMessage();
+                response.setProperty(Constants.PROPERTY_RATE, best.rate);
+                response.setProperty(Constants.PROPERTY_BANK, best.bank);
+                ag.request.setMessage(response, "out");
+                send(ag.request);
+                aggregations.remove(id);
+            }
+        }
+    }
+
+    private void processLenderGatewayResponse(MessageExchange exchange) throws MessagingException {
+        log.info("Receiving lender gateway response");
+        // Get aggregation
+        String id = (String) getProperty(exchange, Constants.PROPERTY_CORRELATIONID);
+        Aggregation ag = (Aggregation) aggregations.get(id);
+        QName[] recipients = (QName[]) getOutProperty(exchange, Constants.PROPERTY_RECIPIENTS);
+        ag.numbers = recipients.length;
+        for (int i = 0; i < recipients.length; i++) {
+            InOut inout = createInOutExchange(recipients[i], null, null);
+            inout.setProperty(Constants.PROPERTY_CORRELATIONID, id);
+            NormalizedMessage msg = inout.createMessage();
+            msg.setProperty(Constants.PROPERTY_SSN, ag.ssn);
+            msg.setProperty(Constants.PROPERTY_AMOUNT, ag.amount);
+            msg.setProperty(Constants.PROPERTY_DURATION, ag.duration);
+            msg.setProperty(Constants.PROPERTY_SCORE, ag.score);
+            msg.setProperty(Constants.PROPERTY_HISTORYLENGTH, ag.hlength);
+            inout.setInMessage(msg);
+            send(inout);
+        }
+        done(exchange);
+    }
+
+    private void processCreditAgencyResponse(MessageExchange exchange) throws MessagingException {
+        log.info("Receiving credit agency response");
+        // Get aggregation
+        String id = (String) getProperty(exchange, Constants.PROPERTY_CORRELATIONID);
+        Aggregation ag = (Aggregation) aggregations.get(id);
+        // Fill with infos
+        ag.score  = (Integer) getOutProperty(exchange, Constants.PROPERTY_SCORE);
+        ag.hlength = (Integer) getOutProperty(exchange, Constants.PROPERTY_HISTORYLENGTH);
+        // Send to lender gateway
+        InOut inout = createInOutExchange(new QName(Constants.LOANBROKER_NS, Constants.LENDERGATEWAY_SERVICE), null, null);
+        inout.setProperty(Constants.PROPERTY_CORRELATIONID, id);
+        NormalizedMessage msg = inout.createMessage();
+        msg.setProperty(Constants.PROPERTY_SCORE, ag.score);
+        msg.setProperty(Constants.PROPERTY_HISTORYLENGTH, ag.hlength);
+        msg.setProperty(Constants.PROPERTY_AMOUNT, ag.amount);
+        inout.setInMessage(msg);
+        send(inout);
+        done(exchange);
+    }
+
+    private void processInputRequest(MessageExchange exchange) throws MessagingException {
+        if (exchange.getStatus() == ExchangeStatus.ACTIVE) {
+            log.info("Receiving loan request");
+            // Create aggregation
+            String id = exchange.getExchangeId();
+            Aggregation ag = new Aggregation();
+            ag.request = exchange;
+            ag.ssn = (String) getInProperty(exchange, Constants.PROPERTY_SSN);
+            ag.amount = (Double) getInProperty(exchange, Constants.PROPERTY_AMOUNT);
+            ag.duration = (Integer) getInProperty(exchange, Constants.PROPERTY_DURATION);
+            aggregations.put(id, ag);
+            
+            InOut inout = createInOutExchange(new QName(Constants.LOANBROKER_NS, Constants.CREDITAGENCY_SERVICE), null, null);
+            inout.setProperty(Constants.PROPERTY_CORRELATIONID, id);
+            NormalizedMessage msg = inout.createMessage();
+            msg.setProperty(Constants.PROPERTY_SSN, ag.ssn);
+            inout.setInMessage(msg);
+            send(inout);
+        }
+    }
+    
+    protected Object getProperty(MessageExchange me, String name) {
+        return me.getProperty(name);
+    }
+    
+    protected Object getInProperty(MessageExchange me, String name) {
+        return me.getMessage("in").getProperty(name);
+    }
+    
+    protected Object getOutProperty(MessageExchange me, String name) {
+        return me.getMessage("out").getProperty(name);
+    }
+    
+    private Map aggregations = new ConcurrentHashMap();
+    
+    public static class Aggregation {
+        public MessageExchange request;
+        public int numbers;
+        public String ssn;
+        public Double amount;
+        public Integer duration;
+        public Integer score;
+        public Integer hlength;
+        public List quotes = new ArrayList();
+    }
+    
+    public static class LoanQuote {
+        public String bank;
+        public Double rate;
+    }
+
+}

Reply via email to