[jboss-cvs] JBossAS SVN: r98868 - in projects/ejb-book/trunk: ch08-messagedestinationlink and 22 other directories.

jboss-cvs-commits at lists.jboss.org jboss-cvs-commits at lists.jboss.org
Tue Dec 22 15:56:21 EST 2009


Author: ALRubinger
Date: 2009-12-22 15:56:20 -0500 (Tue, 22 Dec 2009)
New Revision: 98868

Added:
   projects/ejb-book/trunk/ch08-messagedestinationlink/
   projects/ejb-book/trunk/ch08-messagedestinationlink/pom.xml
   projects/ejb-book/trunk/ch08-messagedestinationlink/src/
   projects/ejb-book/trunk/ch08-messagedestinationlink/src/main/
   projects/ejb-book/trunk/ch08-messagedestinationlink/src/main/java/
   projects/ejb-book/trunk/ch08-messagedestinationlink/src/main/java/org/
   projects/ejb-book/trunk/ch08-messagedestinationlink/src/main/java/org/jboss/
   projects/ejb-book/trunk/ch08-messagedestinationlink/src/main/java/org/jboss/ejb3/
   projects/ejb-book/trunk/ch08-messagedestinationlink/src/main/java/org/jboss/ejb3/examples/
   projects/ejb-book/trunk/ch08-messagedestinationlink/src/main/java/org/jboss/ejb3/examples/ch08/
   projects/ejb-book/trunk/ch08-messagedestinationlink/src/main/java/org/jboss/ejb3/examples/ch08/messagedestinationlink/
   projects/ejb-book/trunk/ch08-messagedestinationlink/src/main/java/org/jboss/ejb3/examples/ch08/messagedestinationlink/api/
   projects/ejb-book/trunk/ch08-messagedestinationlink/src/main/java/org/jboss/ejb3/examples/ch08/messagedestinationlink/api/MessageDestinationLinkConstants.java
   projects/ejb-book/trunk/ch08-messagedestinationlink/src/main/java/org/jboss/ejb3/examples/ch08/messagedestinationlink/mdb/
   projects/ejb-book/trunk/ch08-messagedestinationlink/src/main/java/org/jboss/ejb3/examples/ch08/messagedestinationlink/mdb/MessageDestinationLinkMdb.java
   projects/ejb-book/trunk/ch08-messagedestinationlink/src/main/java/org/jboss/ejb3/examples/ch08/messagedestinationlink/slsb/
   projects/ejb-book/trunk/ch08-messagedestinationlink/src/main/java/org/jboss/ejb3/examples/ch08/messagedestinationlink/slsb/MessageSendingBean.java
   projects/ejb-book/trunk/ch08-messagedestinationlink/src/main/java/org/jboss/ejb3/examples/ch08/messagedestinationlink/slsb/MessageSendingBusiness.java
   projects/ejb-book/trunk/ch08-messagedestinationlink/src/main/resources/
   projects/ejb-book/trunk/ch08-messagedestinationlink/src/main/resources/META-INF/
   projects/ejb-book/trunk/ch08-messagedestinationlink/src/main/resources/META-INF/ejb-jar.xml
   projects/ejb-book/trunk/ch08-messagedestinationlink/src/test/
   projects/ejb-book/trunk/ch08-messagedestinationlink/src/test/java/
   projects/ejb-book/trunk/ch08-messagedestinationlink/src/test/java/org/
   projects/ejb-book/trunk/ch08-messagedestinationlink/src/test/java/org/jboss/
   projects/ejb-book/trunk/ch08-messagedestinationlink/src/test/java/org/jboss/ejb3/
   projects/ejb-book/trunk/ch08-messagedestinationlink/src/test/java/org/jboss/ejb3/examples/
   projects/ejb-book/trunk/ch08-messagedestinationlink/src/test/java/org/jboss/ejb3/examples/ch08/
   projects/ejb-book/trunk/ch08-messagedestinationlink/src/test/java/org/jboss/ejb3/examples/ch08/messagedestinationlink/
   projects/ejb-book/trunk/ch08-messagedestinationlink/src/test/java/org/jboss/ejb3/examples/ch08/messagedestinationlink/MessageDestinationLinkIntegrationTest.java
   projects/ejb-book/trunk/ch08-messagedestinationlink/src/test/java/org/jboss/ejb3/examples/ch08/messagedestinationlink/SecurityActions.java
Modified:
   projects/ejb-book/trunk/pom.xml
Log:
[EJBBOOK-13] Create the setup for MDB message destination examples (incomplete)


Property changes on: projects/ejb-book/trunk/ch08-messagedestinationlink
___________________________________________________________________
Name: svn:ignore
   + target
eclipse-target
target-eclipse
.settings
.classpath
.project


Added: projects/ejb-book/trunk/ch08-messagedestinationlink/pom.xml
===================================================================
--- projects/ejb-book/trunk/ch08-messagedestinationlink/pom.xml	                        (rev 0)
+++ projects/ejb-book/trunk/ch08-messagedestinationlink/pom.xml	2009-12-22 20:56:20 UTC (rev 98868)
@@ -0,0 +1,209 @@
+<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
+
+  <!-- Parent Information -->
+  <parent>
+    <groupId>org.jboss.ejb3.examples</groupId>
+    <artifactId>jboss-ejb3-examples-build</artifactId>
+    <version>0.0.1-SNAPSHOT</version>
+    <relativePath>../pom.xml</relativePath>
+  </parent>
+
+  <!-- Model Version -->
+  <modelVersion>4.0.0</modelVersion>
+
+  <!-- Artifact Information -->
+  <artifactId>jboss-ejb3-examples-ch08-messagedestinationlink</artifactId>
+  <name>JBoss EJB 3.x Examples - Chapter 8: MessageDestinationLink EJBs</name>
+  <description>Example to accompany O'Reilly "Enterprise Java Beans 6th Edition" Chapter 8</description>
+
+  <!-- Build -->
+  <build>
+  </build>
+
+
+  <!-- Properties -->
+  <properties>
+
+    <!-- JBOSS_HOME (We'll unpack into here -->
+    <JBOSS_HOME>${project.build.directory}/jboss-${version.org.jboss.jbossas}</JBOSS_HOME>
+
+    <!-- Versioning -->
+
+  </properties>
+
+  <!-- Dependencies -->
+  <dependencies>
+
+    <dependency>
+      <groupId>org.jboss.ejb3</groupId>
+      <artifactId>jboss-ejb3-api</artifactId>
+      <!-- Must exclude jboss-metadata to prevent class leaking onto the appCL which breaks booting AS?  
+      In an isolated CL context this is not a problem EJBBOOK-12
+      -->
+      <exclusions>
+        <exclusion>
+          <groupId>jboss.jbossws</groupId>
+          <artifactId>jboss-jaxrpc</artifactId>
+        </exclusion>
+      </exclusions>
+    </dependency>
+
+    <dependency>
+      <groupId>org.jboss.ejb3</groupId>
+      <artifactId>jboss-ejb3-ext-api</artifactId>
+      <!-- TODO: Resolve this need for extra configuration -->
+      <!-- Must exclude jboss-metadata to prevent class leaking onto the appCL which breaks booting AS?  
+      In an isolated CL context this is not a problem EJBBOOK-12
+      -->
+      <exclusions>
+        <exclusion>
+          <groupId>org.jboss.metadata</groupId>
+          <artifactId>jboss-metadata</artifactId>
+        </exclusion>
+        <exclusion>
+          <groupId>org.jboss.javaee</groupId>
+          <artifactId>jboss-ejb-api</artifactId>
+        </exclusion>
+      </exclusions>
+    </dependency>
+    
+    <dependency>
+      <groupId>org.jboss.javaee</groupId>
+      <artifactId>jboss-jms-api</artifactId>
+    </dependency>
+
+    <dependency>
+      <groupId>junit</groupId>
+      <artifactId>junit</artifactId>
+    </dependency>
+
+    <dependency>
+      <groupId>org.jboss.logging</groupId>
+      <artifactId>jboss-logging-log4j</artifactId>
+    </dependency>
+
+    <dependency>
+      <groupId>org.jboss.logging</groupId>
+      <artifactId>jboss-logging-spi</artifactId>
+    </dependency>
+
+    <!-- 
+    
+    Test Dependencies
+     -->
+
+    <dependency>
+      <groupId>org.jboss.embedded</groupId>
+      <artifactId>jboss-embedded-api</artifactId>
+    </dependency>
+    <!-- TODO: Remove the dependency on the full AS depchain; it pollutes the CP. EJBBOOK-12
+-->
+    <dependency>
+      <groupId>org.jboss.embedded</groupId>
+      <artifactId>jboss-embedded-depchain</artifactId>
+      <scope>test</scope>
+      <version>${version.org.jboss.embedded}</version>
+      <type>pom</type>
+    </dependency>
+    
+    <dependency>
+      <groupId>org.jboss.shrinkwrap</groupId>
+      <artifactId>shrinkwrap-impl-base</artifactId>
+    </dependency>
+
+  </dependencies>
+
+  <profiles>
+
+    <profile>
+
+      <!-- Declare the "Integration Test" Profile -->
+      <id>it</id>
+
+      <build>
+
+        <plugins>
+
+          <!-- Surefire -->
+          <plugin>
+            <artifactId>maven-surefire-plugin</artifactId>
+            <executions>
+              <execution>
+                <id>integration-test</id>
+                <phase>integration-test</phase>
+                <goals>
+                  <goal>test</goal>
+                </goals>
+                <configuration>
+                  <additionalClasspathElements>
+                    <additionalClasspathElement>${JBOSS_HOME}/bin/run.jar</additionalClasspathElement>
+                  </additionalClasspathElements>
+                  <redirectTestOutputToFile>true</redirectTestOutputToFile>
+                  <trimStackTrace>false</trimStackTrace>
+                  <printSummary>true</printSummary>
+                  <includes>
+                    <include>**/*IntegrationTest.java</include>
+                  </includes>
+                  <forkMode>always</forkMode>
+                  <argLine>-Xmx512m -XX:MaxPermSize=256m -Djava.net.preferIPv4Stack=true -Djava.endorsed.dirs=${JBOSS_HOME}/lib/endorsed -Djboss.home=${JBOSS_HOME}</argLine>
+                </configuration>
+              </execution>
+            </executions>
+          </plugin>
+
+          <plugin>
+            <groupId>org.apache.maven.plugins</groupId>
+            <artifactId>maven-dependency-plugin</artifactId>
+            <executions>
+              <!-- Get AS and put into "target" -->
+              <execution>
+                <id>unpack</id>
+                <phase>pre-integration-test</phase> <!-- So run before testing -->
+                <goals>
+                  <goal>unpack</goal>
+                </goals>
+                <configuration>
+                  <artifactItems>
+                    <artifactItem>
+                      <groupId>org.jboss.jbossas</groupId>
+                      <artifactId>jboss-as-distribution</artifactId>
+                      <version>${version.org.jboss.jbossas}</version>
+                      <type>zip</type>
+                      <overWrite>false</overWrite>
+                      <outputDirectory>${project.build.directory}</outputDirectory>
+                    </artifactItem>
+                  </artifactItems>
+                </configuration>
+              </execution>
+              <!-- Other deps, not part of AS, into "target/deps" -->
+              <execution>
+                <id>copy</id>
+                <phase>pre-integration-test</phase>
+                <goals>
+                  <goal>copy</goal>
+                </goals>
+                <configuration>
+                  <artifactItems>
+                    <artifactItem>
+                      <groupId>org.jboss.embedded</groupId>
+                      <artifactId>jboss-embedded-core</artifactId>
+                      <version>${version.org.jboss.embedded}</version>
+                      <overWrite>false</overWrite>
+                      <outputDirectory>${project.build.directory}/deps</outputDirectory>
+                    </artifactItem>
+                  </artifactItems>
+                </configuration>
+              </execution>
+            </executions>
+          </plugin>
+
+      </plugins>
+
+      </build>
+
+    </profile>
+
+  </profiles>
+
+</project>


Property changes on: projects/ejb-book/trunk/ch08-messagedestinationlink/pom.xml
___________________________________________________________________
Name: svn:executable
   + *

Added: projects/ejb-book/trunk/ch08-messagedestinationlink/src/main/java/org/jboss/ejb3/examples/ch08/messagedestinationlink/api/MessageDestinationLinkConstants.java
===================================================================
--- projects/ejb-book/trunk/ch08-messagedestinationlink/src/main/java/org/jboss/ejb3/examples/ch08/messagedestinationlink/api/MessageDestinationLinkConstants.java	                        (rev 0)
+++ projects/ejb-book/trunk/ch08-messagedestinationlink/src/main/java/org/jboss/ejb3/examples/ch08/messagedestinationlink/api/MessageDestinationLinkConstants.java	2009-12-22 20:56:20 UTC (rev 98868)
@@ -0,0 +1,52 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2009, Red Hat Middleware LLC, and individual contributors
+ * as indicated by the @author tags. See the copyright.txt file in the
+ * distribution for a full listing of individual contributors.
+  *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+package org.jboss.ejb3.examples.ch08.messagedestinationlink.api;
+
+/**
+ * Contains constants used in referring to resources shared
+ * by clients of the MessageDestinationLink EJBs.
+ *
+ * @author <a href="mailto:andrew.rubinger at jboss.org">ALR</a>
+ * @version $Revision: $
+ */
+public interface MessageDestinationLinkConstants
+{
+   //-------------------------------------------------------------------------------------||
+   // Contracts --------------------------------------------------------------------------||
+   //-------------------------------------------------------------------------------------||
+
+   /**
+    * JNDI Name of the JMS Connection Factory
+    */
+   String JNDI_NAME_CONNECTION_FACTORY = "ConnectionFactory";
+
+   /**
+    * Name of the Message Destination Link Ref
+    */
+   String NAME_MESSAGE_DESTINATION_LINK_REF = "jms/MessageDestinationLinkQueue"; // To match XML message-destination-ref-name
+
+   /**
+    * The type of destination used by StatusUpdate MDB implementations
+    */
+   String TYPE_DESTINATION = "javax.jms.Queue";
+
+}

Added: projects/ejb-book/trunk/ch08-messagedestinationlink/src/main/java/org/jboss/ejb3/examples/ch08/messagedestinationlink/mdb/MessageDestinationLinkMdb.java
===================================================================
--- projects/ejb-book/trunk/ch08-messagedestinationlink/src/main/java/org/jboss/ejb3/examples/ch08/messagedestinationlink/mdb/MessageDestinationLinkMdb.java	                        (rev 0)
+++ projects/ejb-book/trunk/ch08-messagedestinationlink/src/main/java/org/jboss/ejb3/examples/ch08/messagedestinationlink/mdb/MessageDestinationLinkMdb.java	2009-12-22 20:56:20 UTC (rev 98868)
@@ -0,0 +1,112 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2009, Red Hat Middleware LLC, and individual contributors
+ * as indicated by the @author tags. See the copyright.txt file in the
+ * distribution for a full listing of individual contributors.
+  *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+package org.jboss.ejb3.examples.ch08.messagedestinationlink.mdb;
+
+import java.util.concurrent.CountDownLatch;
+import java.util.logging.Logger;
+
+import javax.ejb.MessageDriven;
+import javax.jms.JMSException;
+import javax.jms.Message;
+import javax.jms.MessageListener;
+import javax.jms.TextMessage;
+
+/**
+ * MDB which is linked to a SLSB via the deployment descriptor; obtains messages
+ * and caches them as a class member for obtaining from a test later
+ *
+ * @author <a href="mailto:andrew.rubinger at jboss.org">ALR</a>
+ * @version $Revision: $
+ */
+ at MessageDriven(name = MessageDestinationLinkMdb.NAME_EJB)
+public class MessageDestinationLinkMdb implements MessageListener
+{
+
+   //-------------------------------------------------------------------------------------||
+   // Class Members ----------------------------------------------------------------------||
+   //-------------------------------------------------------------------------------------||
+
+   /**
+    * Logger
+    */
+   private static final Logger log = Logger.getLogger(MessageDestinationLinkMdb.class.getName());
+
+   /**
+    * Shared latch, so tests can wait until the MDB is processed.  In POJO
+    * testing this is wholly unnecessary as we've got a single-threaded environment, but 
+    * when testing in an EJB Container running in the *same* JVM as the test, the test 
+    * can use this to wait until the MDB has been invoked, strengthening the integrity
+    * of the test.  It's not recommended to put this piece into a production EJB; instead
+    * test an extension of your EJB which adds this (and only this) support.
+    */
+   public static CountDownLatch LATCH = new CountDownLatch(1);
+
+   /**
+    * Name of this EJB, to match the ejb-name used in the deployment descriptor
+    */
+   static final String NAME_EJB = "MessageDestinationLinkMdb";
+
+   /**
+    * Last message received; never use this in production (as many Threads/instances
+    * may concurrently have access).  Here we do so safely within the confines
+    * of our test environment.
+    */
+   public static String LAST_MESSAGE;
+
+   //-------------------------------------------------------------------------------------||
+   // Required Implementations -----------------------------------------------------------||
+   //-------------------------------------------------------------------------------------||
+
+   /**
+    * {@inheritDoc}
+    * @see javax.jms.MessageListener#onMessage(javax.jms.Message)
+    */
+   @Override
+   public void onMessage(final Message message)
+   {
+      // Cast
+      if (message instanceof TextMessage)
+      {
+         throw new IllegalArgumentException("Expecting message of type " + TextMessage.class.getName() + "; got: "
+               + message);
+      }
+      final TextMessage txtMessage = (TextMessage) message;
+      final String contents;
+      try
+      {
+         contents = txtMessage.getText();
+      }
+      catch (final JMSException e)
+      {
+         throw new RuntimeException("Could not get contents of message: " + txtMessage, e);
+      }
+
+      // Set the last contents received
+      log.info("Received message with contents: " + contents);
+      LAST_MESSAGE = contents;
+
+      // Count down the latch so that the test knows we're here
+      log.info("Counting down the latch...");
+      LATCH.countDown();
+   }
+
+}

Added: projects/ejb-book/trunk/ch08-messagedestinationlink/src/main/java/org/jboss/ejb3/examples/ch08/messagedestinationlink/slsb/MessageSendingBean.java
===================================================================
--- projects/ejb-book/trunk/ch08-messagedestinationlink/src/main/java/org/jboss/ejb3/examples/ch08/messagedestinationlink/slsb/MessageSendingBean.java	                        (rev 0)
+++ projects/ejb-book/trunk/ch08-messagedestinationlink/src/main/java/org/jboss/ejb3/examples/ch08/messagedestinationlink/slsb/MessageSendingBean.java	2009-12-22 20:56:20 UTC (rev 98868)
@@ -0,0 +1,154 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2009, Red Hat Middleware LLC, and individual contributors
+ * as indicated by the @author tags. See the copyright.txt file in the
+ * distribution for a full listing of individual contributors.
+  *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+package org.jboss.ejb3.examples.ch08.messagedestinationlink.slsb;
+
+import java.util.logging.Logger;
+
+import javax.annotation.Resource;
+import javax.ejb.Local;
+import javax.ejb.Stateless;
+import javax.jms.JMSException;
+import javax.jms.Queue;
+import javax.jms.QueueConnection;
+import javax.jms.QueueConnectionFactory;
+import javax.jms.QueueSender;
+import javax.jms.QueueSession;
+import javax.jms.TextMessage;
+
+import org.jboss.ejb3.annotation.LocalBinding;
+import org.jboss.ejb3.examples.ch08.messagedestinationlink.api.MessageDestinationLinkConstants;
+import org.jboss.ejb3.examples.ch08.messagedestinationlink.mdb.MessageDestinationLinkMdb;
+
+/**
+ * Stateless Sesssion Bean which sends a JMS {@link TextMessage} to
+ * the {@link MessageDestinationLinkMdb} by way of a "message-destination-link"
+ * as configured in the deployment descriptor.
+ *
+ * @author <a href="mailto:andrew.rubinger at jboss.org">ALR</a>
+ * @version $Revision: $
+ */
+ at Stateless(name = MessageSendingBusiness.NAME_EJB)
+ at LocalBinding(jndiBinding = MessageSendingBusiness.NAME_JNDI)
+ at Local(MessageSendingBusiness.class)
+public class MessageSendingBean implements MessageSendingBusiness
+{
+
+   //-------------------------------------------------------------------------------------||
+   // Class Members ----------------------------------------------------------------------||
+   //-------------------------------------------------------------------------------------||
+
+   /**
+    * Logger
+    */
+   private static final Logger log = Logger.getLogger(MessageSendingBean.class.getName());
+
+   //-------------------------------------------------------------------------------------||
+   // Instance Members -------------------------------------------------------------------||
+   //-------------------------------------------------------------------------------------||
+
+   /**
+    * Queue we'll send messages to; logical name as wired from the message-destination-link
+    */
+   @Resource(name = MessageDestinationLinkConstants.NAME_MESSAGE_DESTINATION_LINK_REF)
+   // Name to match message-destination-ref-name
+   private Queue queue;
+
+   /**
+    * Connection factory for making new Queue connections
+    */
+   @Resource(mappedName = MessageDestinationLinkConstants.JNDI_NAME_CONNECTION_FACTORY)
+   private QueueConnectionFactory connectionFactory;
+
+   //-------------------------------------------------------------------------------------||
+   // Required Implementations -----------------------------------------------------------||
+   //-------------------------------------------------------------------------------------||
+
+   /**
+    * {@inheritDoc}
+    * @see org.jboss.ejb3.examples.ch08.messagedestinationlink.slsb.MessageSendingBusiness#sendMessage(java.lang.String)
+    */
+   @Override
+   public void sendMessage(final String contents)
+   {
+      // Precondition checks
+      if (contents == null || contents.length() == 0)
+      {
+         throw new IllegalArgumentException("contents must be specified");
+      }
+
+      // Create a connection
+      final QueueConnection connection;
+      try
+      {
+         connection = connectionFactory.createQueueConnection();
+      }
+      catch (final JMSException jmse)
+      {
+         throw new RuntimeException("Could not create new connection", jmse);
+      }
+
+      // Create a session
+      final QueueSession session;
+      try
+      {
+         session = connection.createQueueSession(false, QueueSession.AUTO_ACKNOWLEDGE);
+      }
+      catch (final JMSException jmse)
+      {
+         throw new RuntimeException("Could not create new session", jmse);
+      }
+
+      // Create a sender
+      final QueueSender sender;
+      try
+      {
+         sender = session.createSender(queue);
+      }
+      catch (final JMSException jmse)
+      {
+         throw new RuntimeException("Could not create new sender", jmse);
+      }
+
+      // Create a message
+      final TextMessage message;
+      try
+      {
+         message = session.createTextMessage(contents);
+      }
+      catch (final JMSException jmse)
+      {
+         throw new RuntimeException("Could not create new message", jmse);
+      }
+
+      // Send
+      try
+      {
+         sender.send(message);
+         log.info("Sent to MDB: " + message);
+      }
+      catch (final JMSException jmse)
+      {
+         throw new RuntimeException("Could not send message", jmse);
+      }
+
+   }
+}

Added: projects/ejb-book/trunk/ch08-messagedestinationlink/src/main/java/org/jboss/ejb3/examples/ch08/messagedestinationlink/slsb/MessageSendingBusiness.java
===================================================================
--- projects/ejb-book/trunk/ch08-messagedestinationlink/src/main/java/org/jboss/ejb3/examples/ch08/messagedestinationlink/slsb/MessageSendingBusiness.java	                        (rev 0)
+++ projects/ejb-book/trunk/ch08-messagedestinationlink/src/main/java/org/jboss/ejb3/examples/ch08/messagedestinationlink/slsb/MessageSendingBusiness.java	2009-12-22 20:56:20 UTC (rev 98868)
@@ -0,0 +1,61 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2009, Red Hat Middleware LLC, and individual contributors
+ * as indicated by the @author tags. See the copyright.txt file in the
+ * distribution for a full listing of individual contributors.
+  *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+package org.jboss.ejb3.examples.ch08.messagedestinationlink.slsb;
+
+import javax.jms.TextMessage;
+
+/**
+ * Business interface to send a message to a message
+ * destination link (to be picked up by an MDB as configured
+ * down the chain) 
+ *
+ * @author <a href="mailto:andrew.rubinger at jboss.org">ALR</a>
+ * @version $Revision: $
+ */
+public interface MessageSendingBusiness
+{
+   //-------------------------------------------------------------------------------------||
+   // Constants --------------------------------------------------------------------------||
+   //-------------------------------------------------------------------------------------||
+
+   /**
+    * EJB Name
+    */
+   String NAME_EJB = "MessageSendingEJB";
+
+   /**
+    * Location to which we'll bind in JNDI
+    */
+   String NAME_JNDI = NAME_EJB + "/local";
+
+   //-------------------------------------------------------------------------------------||
+   // Contracts --------------------------------------------------------------------------||
+   //-------------------------------------------------------------------------------------||
+
+   /**
+    * Sends a {@link TextMessage} with the specified contents to the
+    * message destination link as configured by ejb-jar.xml
+    * 
+    * @throws IllegalArgumentException If the contents are not specified
+    */
+   void sendMessage(String contents) throws IllegalArgumentException;
+}

Added: projects/ejb-book/trunk/ch08-messagedestinationlink/src/main/resources/META-INF/ejb-jar.xml
===================================================================
--- projects/ejb-book/trunk/ch08-messagedestinationlink/src/main/resources/META-INF/ejb-jar.xml	                        (rev 0)
+++ projects/ejb-book/trunk/ch08-messagedestinationlink/src/main/resources/META-INF/ejb-jar.xml	2009-12-22 20:56:20 UTC (rev 98868)
@@ -0,0 +1,59 @@
+<ejb-jar xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+  xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
+                  http://java.sun.com/xml/ns/javaee/ejb-jar_3_0.xsd"
+  version="3.0">
+  <!--
+    TODO https://jira.jboss.org/jira/browse/EJBBOOK-6 Update the Schema
+    to 3.1
+  -->
+
+  <enterprise-beans>
+
+    <!--
+      In this section we'll configure our MessageSendingEJB to use a
+      message-destination-link
+    -->
+    <session>
+
+      <!--
+        This will match the value of @Stateless.name upon our bean
+        implementation class
+      -->
+      <ejb-name>MessageSendingEJB</ejb-name>
+
+      <!--
+        Declare the Message Destination Reference which will be used in
+        the bean implementation class as a logical reference to which
+        we'll send messages
+      -->
+      <message-destination-ref>
+        <message-destination-ref-name>jms/MessageDestinationLinkQueue</message-destination-ref-name>
+        <message-destination-type>javax.jms.Queue</message-destination-type>
+        <message-destination-usage>Produces</message-destination-usage>
+        <message-destination-link>OurLinkName</message-destination-link>
+      </message-destination-ref>
+
+    </session>
+
+    <!--
+      Now we'll configure our MDB to receive messages from the logical
+      name defined by the message-destination-link
+    -->
+    <message-driven>
+      <ejb-name>MessageDestinationLinkMdb</ejb-name>
+      <message-destination-link>OurLinkName</message-destination-link>
+    </message-driven>
+
+  </enterprise-beans>
+
+  <!--
+    Declare the Message Destination which is used as a logical name for
+    linking
+  -->
+  <assembly-descriptor>
+    <message-destination>
+      <message-destination-name>OurLinkName</message-destination-name>
+    </message-destination>
+  </assembly-descriptor>
+
+</ejb-jar>
\ No newline at end of file

Added: projects/ejb-book/trunk/ch08-messagedestinationlink/src/test/java/org/jboss/ejb3/examples/ch08/messagedestinationlink/MessageDestinationLinkIntegrationTest.java
===================================================================
--- projects/ejb-book/trunk/ch08-messagedestinationlink/src/test/java/org/jboss/ejb3/examples/ch08/messagedestinationlink/MessageDestinationLinkIntegrationTest.java	                        (rev 0)
+++ projects/ejb-book/trunk/ch08-messagedestinationlink/src/test/java/org/jboss/ejb3/examples/ch08/messagedestinationlink/MessageDestinationLinkIntegrationTest.java	2009-12-22 20:56:20 UTC (rev 98868)
@@ -0,0 +1,295 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2009, Red Hat Middleware LLC, and individual contributors
+ * as indicated by the @author tags. See the copyright.txt file in the
+ * distribution for a full listing of individual contributors.
+  *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+package org.jboss.ejb3.examples.ch08.messagedestinationlink;
+
+import java.io.File;
+import java.net.MalformedURLException;
+import java.net.URL;
+import java.util.HashSet;
+import java.util.Set;
+import java.util.concurrent.TimeUnit;
+import java.util.logging.Logger;
+
+import javax.naming.Context;
+import javax.naming.InitialContext;
+
+import junit.framework.TestCase;
+
+import org.jboss.bootstrap.api.lifecycle.LifecycleState;
+import org.jboss.ejb3.examples.ch08.messagedestinationlink.api.MessageDestinationLinkConstants;
+import org.jboss.ejb3.examples.ch08.messagedestinationlink.mdb.MessageDestinationLinkMdb;
+import org.jboss.ejb3.examples.ch08.messagedestinationlink.slsb.MessageSendingBean;
+import org.jboss.ejb3.examples.ch08.messagedestinationlink.slsb.MessageSendingBusiness;
+import org.jboss.embedded.api.DeploymentException;
+import org.jboss.embedded.api.server.JBossASEmbeddedServer;
+import org.jboss.embedded.api.server.JBossASEmbeddedServerFactory;
+import org.jboss.embedded.api.server.JBossHomeClassLoader;
+import org.jboss.shrinkwrap.api.Archives;
+import org.jboss.shrinkwrap.api.spec.JavaArchive;
+import org.junit.After;
+import org.junit.AfterClass;
+import org.junit.Before;
+import org.junit.BeforeClass;
+import org.junit.Test;
+
+/**
+ * Ensures that a SLSB wired to an MDB by way of a logical mapping
+ * (message-destination-link) is working as expected
+ *
+ * @author <a href="mailto:andrew.rubinger at jboss.org">ALR</a>
+ * @version $Revision: $
+ */
+public class MessageDestinationLinkIntegrationTest
+{
+
+   //-------------------------------------------------------------------------------------||
+   // Class Members ----------------------------------------------------------------------||
+   //-------------------------------------------------------------------------------------||
+
+   /**
+    * Logger
+    */
+   private static final Logger log = Logger.getLogger(MessageDestinationLinkIntegrationTest.class.getName());
+
+   /**
+    * The server instance
+    */
+   private static JBossASEmbeddedServer server;
+
+   /**
+    * The CL of the test as originally loaded
+    */
+   private static ClassLoader originalClassLoader;
+
+   /**
+    * Name of the archive we'll deploy into the server for testing
+    */
+   private static final String NAME_MDB_ARCHIVE = "messageDestinationLink.jar";
+
+   /**
+    * Name of the system property for JBOSS_HOME
+    */
+   private static final String NAME_SYSPROP_JBOSS_HOME = "jboss.home";
+
+   /**
+    * Location of the EJB deployment descriptor
+    */
+   private static final String NAME_EJB_JAR = "META-INF/ejb-jar.xml";
+
+   /**
+    * The JNDI Context
+    */
+   private static Context NAMING_CONTEXT;
+
+   //-------------------------------------------------------------------------------------||
+   // Instance Members -------------------------------------------------------------------||
+   //-------------------------------------------------------------------------------------||
+
+   /**
+    * Archive containing our EJBs
+    */
+   private JavaArchive testArchive;
+
+   //-------------------------------------------------------------------------------------||
+   // Lifecycle --------------------------------------------------------------------------||
+   //-------------------------------------------------------------------------------------||
+
+   /**
+    * Creates and starts a new JBossAS Server Embedded within this JVM, setting a JNDI context as well
+    */
+   @BeforeClass
+   public static void createAndStartJBossASAndSetNamingContext() throws Exception
+   {
+      // Get JBOSS_HOME
+      final URL jbossHome = getJBossHome();
+
+      // Get additional binaries which need CL visibility (ie. jboss-embedded-core,
+      // which is placed under "target/deps" by the build).  These
+      // binaries are not presently available under $JBOSS_HOME
+      final Set<URL> additionalUrls = new HashSet<URL>();
+      final URL source = MessageDestinationLinkIntegrationTest.class.getProtectionDomain().getCodeSource()
+            .getLocation();
+      final URL target = new URL(source, "..");
+      final URL additionalDeps = new URL(target, "deps");
+      final File deps = new File(additionalDeps.toURI());
+      TestCase.assertTrue("Dependencies location does not exist: " + deps, deps.exists());
+      TestCase.assertTrue("Dependencies location is not a directory: " + deps, deps.isDirectory());
+      for (final File child : deps.listFiles())
+      {
+         additionalUrls.add(child.toURI().toURL());
+         log.info("Booting with: " + child);
+      }
+
+      // Make the new ClassLoader
+      originalClassLoader = SecurityActions.getThreadContextClassLoader();
+      final ClassLoader jbossHomeClassLoader = JBossHomeClassLoader.newInstance(jbossHome, additionalUrls
+            .toArray(new URL[]
+            {}), originalClassLoader);
+
+      // Make Server
+      server = JBossASEmbeddedServerFactory.createServer(jbossHomeClassLoader);
+      log.info("Created: " + server);
+
+      // Start
+      log.info("Starting Server: " + server);
+
+      // Set TCCL
+      Thread.currentThread().setContextClassLoader(jbossHomeClassLoader);
+
+      // Start the Server
+      server.start();
+
+      // Set Naming Context
+      NAMING_CONTEXT = new InitialContext();
+   }
+
+   /**
+    * Stops the Application Server
+    */
+   @AfterClass
+   public static void stopJBossAS() throws Exception
+   {
+      if (server != null && server.getState().equals(LifecycleState.STARTED))
+      {
+         try
+         {
+            server.shutdown();
+         }
+         finally
+         {
+            // Reset the TCCL 
+            Thread.currentThread().setContextClassLoader(originalClassLoader);
+         }
+      }
+   }
+
+   /**
+    * Creates and deploys the test archive
+    * @throws DeploymentException
+    */
+   @Before
+   public void createAndDeployArchive() throws DeploymentException
+   {
+      // Package up the EJBs and a Deployment Descriptor
+      final JavaArchive archive = Archives.create(NAME_MDB_ARCHIVE, JavaArchive.class).addClasses(
+            MessageDestinationLinkConstants.class, MessageDestinationLinkMdb.class, MessageSendingBusiness.class,
+            MessageSendingBean.class).addResource(NAME_EJB_JAR);
+
+      // Deploy the archive
+      log.info("Deploying archive: " + archive.toString(true));
+      server.deploy(archive);
+      testArchive = archive;
+   }
+
+   /**
+    * Undeploys the test archive
+    */
+   @After
+   public void undeployArchive() throws DeploymentException
+   {
+      if (testArchive != null)
+      {
+         server.undeploy(testArchive);
+      }
+   }
+
+   //-------------------------------------------------------------------------------------||
+   // Tests ------------------------------------------------------------------------------||
+   //-------------------------------------------------------------------------------------||
+
+   /**
+    * Ensures that invoking an SLSB which sends messages to
+    * a logical name (message-destination-link) works correctly
+    * where the target MDB receives the message in question
+    */
+   @Test
+   public void testMessageLinkingFromSlsbToMdb() throws Exception
+   {
+      // Obtain the EJB
+      final MessageSendingBusiness bean = (MessageSendingBusiness) NAMING_CONTEXT
+            .lookup(MessageSendingBusiness.NAME_JNDI);
+
+      // Send a message
+      final String message = "Testing Message Linking";
+      bean.sendMessage(message);
+
+      // Wait for the MDB to process, as it's doing so in another Thread.
+      // This is *only* possible when we test MDBs in the same JVM as the test.
+      final boolean processed;
+      try
+      {
+         log.info("Waiting on the MDB...");
+         processed = MessageDestinationLinkMdb.LATCH.await(10, TimeUnit.SECONDS);
+      }
+      catch (final InterruptedException e)
+      {
+         // Clear the flag and rethrow; some error in setup is in play
+         Thread.interrupted();
+         throw new RuntimeException(
+               "Thread was interrupted while waiting for MDB processing; should not happen in this test");
+      }
+
+      // Ensure the MDB processed the message
+      if (!processed)
+      {
+         TestCase.fail("The MDB did not process the message in the allotted time.");
+      }
+      log.info("MDB signaled it's done processing, so we can resume");
+
+      // Ensure the contents are as expected
+      final String roundtrip = MessageDestinationLinkMdb.LAST_MESSAGE;
+      TestCase.assertEquals("Last message sent was not as expected", message, roundtrip);
+   }
+
+   //-------------------------------------------------------------------------------------||
+   // Internal Helper Methods ------------------------------------------------------------||
+   //-------------------------------------------------------------------------------------||
+
+   /**
+    * Obtains $JBOSS_HOME from the system property
+    * 
+    * @return
+    */
+   private static URL getJBossHome()
+   {
+      final String sysProp = NAME_SYSPROP_JBOSS_HOME;
+      final String jbossHomeString = SecurityActions.getSystemProperty(sysProp);
+      if (jbossHomeString == null)
+      {
+         throw new IllegalStateException("System property \"" + sysProp + "\" must be present in the environment");
+      }
+      final File jbossHomeFile = new File(jbossHomeString);
+      if (!jbossHomeFile.exists())
+      {
+         throw new IllegalStateException("JBOSS_HOME does not exist: " + jbossHomeFile.getAbsolutePath());
+      }
+      try
+      {
+         return jbossHomeFile.toURI().toURL();
+      }
+      catch (final MalformedURLException murle)
+      {
+         throw new RuntimeException("Could not get JBOSS_HOME", murle);
+      }
+   }
+
+}

Added: projects/ejb-book/trunk/ch08-messagedestinationlink/src/test/java/org/jboss/ejb3/examples/ch08/messagedestinationlink/SecurityActions.java
===================================================================
--- projects/ejb-book/trunk/ch08-messagedestinationlink/src/test/java/org/jboss/ejb3/examples/ch08/messagedestinationlink/SecurityActions.java	                        (rev 0)
+++ projects/ejb-book/trunk/ch08-messagedestinationlink/src/test/java/org/jboss/ejb3/examples/ch08/messagedestinationlink/SecurityActions.java	2009-12-22 20:56:20 UTC (rev 98868)
@@ -0,0 +1,154 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2009, Red Hat Middleware LLC, and individual contributors
+ * as indicated by the @author tags. See the copyright.txt file in the
+ * distribution for a full listing of individual contributors.
+  *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+package org.jboss.ejb3.examples.ch08.messagedestinationlink;
+
+import java.security.AccessController;
+import java.security.PrivilegedAction;
+
+/**
+ * Protected security actions not to leak outside this package
+ *
+ * @author <a href="mailto:andrew.rubinger at jboss.org">ALR</a>
+ * @version $Revision: $
+ */
+class SecurityActions
+{
+
+   //-------------------------------------------------------------------------------||
+   // Constructor ------------------------------------------------------------------||
+   //-------------------------------------------------------------------------------||
+
+   /**
+    * No external instanciation
+    */
+   private SecurityActions()
+   {
+
+   }
+
+   //-------------------------------------------------------------------------------||
+   // Utility Methods --------------------------------------------------------------||
+   //-------------------------------------------------------------------------------||
+
+   /**
+    * Obtains the Thread Context ClassLoader
+    */
+   static ClassLoader getThreadContextClassLoader()
+   {
+      return AccessController.doPrivileged(GetTcclAction.INSTANCE);
+   }
+
+   /**
+    * Sets the specified CL upon the current Thread's Context 
+    * 
+    * @param cl
+    * @throws IllegalArgumentException If the CL was null
+    */
+   static void setThreadContextClassLoader(final ClassLoader cl) throws IllegalArgumentException
+   {
+      if (cl == null)
+      {
+         throw new IllegalArgumentException("ClassLoader was null");
+      }
+
+      AccessController.doPrivileged(new PrivilegedAction<Void>()
+      {
+         public Void run()
+         {
+            Thread.currentThread().setContextClassLoader(cl);
+            return null;
+         };
+      });
+   }
+
+   /**
+    * Obtains the system property with the specified key
+    * 
+    * @param key
+    * @return
+    * @throws IllegalArgumentException If the key is null
+    */
+   static String getSystemProperty(final String key) throws IllegalArgumentException
+   {
+      // Precondition check
+      if (key == null)
+      {
+         throw new IllegalArgumentException("key was null");
+      }
+
+      // Get sysprop
+      return AccessController.doPrivileged(new GetSystemPropertyAction(key));
+   }
+
+   //-------------------------------------------------------------------------------||
+   // Inner Classes ----------------------------------------------------------------||
+   //-------------------------------------------------------------------------------||
+
+   /**
+    * {@link PrivilegedAction} action to obtain the TCCL
+    */
+   private enum GetTcclAction implements PrivilegedAction<ClassLoader> {
+      INSTANCE;
+
+      @Override
+      public ClassLoader run()
+      {
+         return Thread.currentThread().getContextClassLoader();
+      }
+   }
+
+   /**
+    * {@link PrivilegedAction} to access a system property
+    * 
+    *
+    * @author <a href="mailto:andrew.rubinger at jboss.org">ALR</a>
+    * @version $Revision: $
+    */
+   private static class GetSystemPropertyAction implements PrivilegedAction<String>
+   {
+
+      /**
+       * Name of the sysprop to get
+       */
+      private String sysPropName;
+
+      /**
+       * Creates a new instance capable of obtaining the specified system property by name
+       * @param sysPropName
+       */
+      public GetSystemPropertyAction(final String sysPropName)
+      {
+         this.sysPropName = sysPropName;
+      }
+
+      /**
+       * {@inheritDoc}
+       * @see java.security.PrivilegedAction#run()
+       */
+      @Override
+      public String run()
+      {
+         return System.getProperty(sysPropName);
+      }
+   }
+
+}

Modified: projects/ejb-book/trunk/pom.xml
===================================================================
--- projects/ejb-book/trunk/pom.xml	2009-12-22 20:46:29 UTC (rev 98867)
+++ projects/ejb-book/trunk/pom.xml	2009-12-22 20:56:20 UTC (rev 98868)
@@ -20,6 +20,7 @@
     <module>ch06-filetransfer</module>
     <module>ch07-rsscache</module>
     <module>ch08-statusupdate</module>
+    <module>ch08-messagedestinationlink</module>
   </modules>
 
 </project>




More information about the jboss-cvs-commits mailing list