modules/helpers/jeeGen/pom.xml | 6 modules/helpers/jeeGen/src/main/java/org/rhq/helpers/jeegen/JeeGen.java | 32 ++ modules/helpers/jeeGen/src/main/java/org/rhq/helpers/jeegen/ejb/EjbArchive.java | 142 ++++++---- modules/helpers/jeeGen/src/main/java/test/ejb/MessageDrivenBean.java | 109 +++++++ modules/helpers/jeeGen/src/main/resources/org/rhq/helpers/jeegen/ejb/v2/ejb-jar.xml.ftl | 30 ++ 5 files changed, 265 insertions(+), 54 deletions(-)
New commits: commit af24373f392f36b77988e5015a9754f506cbb236 Author: Ian Springer ian.springer@redhat.com Date: Fri Feb 10 10:52:00 2012 -0500
jeegen: add support for generating message-driven beans, as well as support for configurability via command line
diff --git a/modules/helpers/jeeGen/pom.xml b/modules/helpers/jeeGen/pom.xml index 67fdf05..df620a0 100644 --- a/modules/helpers/jeeGen/pom.xml +++ b/modules/helpers/jeeGen/pom.xml @@ -85,6 +85,12 @@ <version>3.0</version> </dependency>
+ <dependency> + <groupId>javax.jms</groupId> + <artifactId>jms-api</artifactId> + <version>1.1-rev-1</version> + </dependency> + </dependencies>
</project> diff --git a/modules/helpers/jeeGen/src/main/java/org/rhq/helpers/jeegen/JeeGen.java b/modules/helpers/jeeGen/src/main/java/org/rhq/helpers/jeegen/JeeGen.java index e1fdda9..d17001f 100644 --- a/modules/helpers/jeeGen/src/main/java/org/rhq/helpers/jeegen/JeeGen.java +++ b/modules/helpers/jeeGen/src/main/java/org/rhq/helpers/jeegen/JeeGen.java @@ -33,9 +33,39 @@ import org.rhq.helpers.jeegen.ejb.EjbArchive; public class JeeGen {
public static void main(String[] args) throws Exception { - EjbArchive ejbArchive = new EjbArchive(ShrinkWrap.create(JavaArchive.class), "2.1", 10, 10, 10); + if (args.length != 5) { + printUsageAndExit(); + } + + String ejbVersion = args[0]; + int entityBeanCount = 0; + int statelessSessionBeanCount = 0; + int statefulSessionBeanCount = 0; + int messageDrivenBeanCount = 0; + try { + entityBeanCount = Integer.parseInt(args[1]); + statelessSessionBeanCount = Integer.parseInt(args[2]); + statefulSessionBeanCount = Integer.parseInt(args[3]); + messageDrivenBeanCount = Integer.parseInt(args[4]); + } catch (NumberFormatException e) { + printUsageAndExit(); + } + + if (entityBeanCount < 0 || statelessSessionBeanCount < 0 || statefulSessionBeanCount < 0 || messageDrivenBeanCount < 0) { + printUsageAndExit(); + } + + EjbArchive ejbArchive = new EjbArchive(ShrinkWrap.create(JavaArchive.class), ejbVersion, entityBeanCount, + statelessSessionBeanCount, statefulSessionBeanCount, messageDrivenBeanCount); File ejbJarFile = new File("test-ejb.jar"); ejbArchive.as(ZipExporter.class).exportTo(ejbJarFile, true); }
+ private static void printUsageAndExit() { + System.err.println("Usage: " + JeeGen.class.getName() + + " EJB_VERSION ENTITY_BEAN_COUNT STATELESS_SESSION_BEAN_COUNT STATEFUL_SESSION_BEAN_COUNT MESSAGE_DRIVEN_BEAN_COUNT"); + System.err.println("Example: " + JeeGen.class.getName() + " 3.0 10 10 10 10"); + System.exit(1); + } + } diff --git a/modules/helpers/jeeGen/src/main/java/org/rhq/helpers/jeegen/ejb/EjbArchive.java b/modules/helpers/jeeGen/src/main/java/org/rhq/helpers/jeegen/ejb/EjbArchive.java index e53cdb5..1bfac72 100644 --- a/modules/helpers/jeeGen/src/main/java/org/rhq/helpers/jeegen/ejb/EjbArchive.java +++ b/modules/helpers/jeeGen/src/main/java/org/rhq/helpers/jeegen/ejb/EjbArchive.java @@ -53,65 +53,17 @@ import org.jboss.shrinkwrap.impl.base.path.BasicPath; */ public class EjbArchive implements JavaArchive {
- public static final String TEST_EJB_RESOURCE_PATH = "test/ejb"; + private static final String TEST_EJB_RESOURCE_PATH = "test/ejb";
private JavaArchive delegate;
- enum EjbVersionInfo { - v2_0("2.0", "<!DOCTYPE ejb-jar PUBLIC \"-//Sun Microsystems, Inc.//DTD Enterprise JavaBeans 2.0//EN\" \"http://java.sun.com/dtd/ejb-jar_2_0.dtd\">\n<ejb-jar>"), - v2_1("2.1", "<ejb-jar version="2.1"\n" + - " xmlns="http://java.sun.com/xml/ns/j2ee%5C%22%5Cn" + - " xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance%5C%22%5Cn" + - " xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/ejb-jar_2_1.xsd%5C%22%3E"), - v3_0("3.0", "<ejb-jar version="3.0"\n" + "" + - " xmlns="http://java.sun.com/xml/ns/javaee%5C%22%5Cn" + - " xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance%5C%22%5Cn" + - " xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/ejb-jar_3_0.xsd%5C%22%3E"), - v3_1("3.1", "<ejb-jar version="3.1"\n" + - " xmlns="http://java.sun.com/xml/ns/javaee%5C%22%5Cn" + - " xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance%5C%22%5Cn" + - " xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/ejb-jar_3_1.xsd%5C%22%3E"); - - private String version; - private String rootElement; - - EjbVersionInfo(String version, String rootElement) { - this.version = version; - this.rootElement = rootElement; - } - - private static final Map<String, EjbVersionInfo> VERSION_TO_VALUE_MAP = - new HashMap<String, EjbVersionInfo>(values().length); - static { - for (EjbVersionInfo value : values()) { - VERSION_TO_VALUE_MAP.put(value.getVersion(), value); - } - } - - public String getVersion() { - return version; - } - - public String getRootElement() { - return rootElement; - } - - public static EjbVersionInfo forVersion(String version) { - if (!VERSION_TO_VALUE_MAP.containsKey(version)) { - throw new IllegalArgumentException("Unsupported version: "" + version + - "". The following versions are supported: " + VERSION_TO_VALUE_MAP.keySet()); - } - return VERSION_TO_VALUE_MAP.get(version); - } - } - /** * Create a new JavaArchive with any type storage engine as backing. * * @param delegate The storage backing. */ public EjbArchive(JavaArchive delegate, String ejbVersion, int entityBeanCount, int statelessSessionBeanCount, - int statefulSessionBeanCount) { + int statefulSessionBeanCount, int messageDrivenBeanCount) { EjbVersionInfo ejbVersionInfo = EjbVersionInfo.forVersion(ejbVersion);
this.delegate = delegate; @@ -124,13 +76,15 @@ public class EjbArchive implements JavaArchive { try { Template template = config.getTemplate("v2/ejb-jar.xml.ftl");
- Map dataModel = new HashMap(); + Map<String, Object> dataModel = new HashMap<String, Object>(); dataModel.put("name", "Test"); dataModel.put("rootElement", ejbVersionInfo.getRootElement()); - dataModel.put("package", "test.ejb"); - dataModel.put("entityBeanCount", statelessSessionBeanCount); + dataModel.put("mdbElements", ejbVersionInfo.getMdbElements()); + dataModel.put("package", TEST_EJB_RESOURCE_PATH.replace('/', '.')); + dataModel.put("entityBeanCount", entityBeanCount); dataModel.put("statelessSessionBeanCount", statelessSessionBeanCount); dataModel.put("statefulSessionBeanCount", statefulSessionBeanCount); + dataModel.put("messageDrivenBeanCount", messageDrivenBeanCount);
byteArrayOutputStream = new ByteArrayOutputStream(); Writer out = new OutputStreamWriter(byteArrayOutputStream); @@ -158,6 +112,9 @@ public class EjbArchive implements JavaArchive { addAsResource(new ClassLoaderAsset(TEST_EJB_RESOURCE_PATH + "/StatefulSessionEJBHome.class"), TEST_EJB_RESOURCE_PATH + "/StatefulSessionEJBHome.class"); addAsResource(new ClassLoaderAsset(TEST_EJB_RESOURCE_PATH + "/StatefulSessionEJBObject.class"),TEST_EJB_RESOURCE_PATH + "/StatefulSessionEJBObject.class"); addAsResource(new ClassLoaderAsset(TEST_EJB_RESOURCE_PATH + "/StatefulSessionBean.class"), TEST_EJB_RESOURCE_PATH + "/StatefulSessionBean.class"); + + // add message-driven bean classes + addAsResource(new ClassLoaderAsset(TEST_EJB_RESOURCE_PATH + "/MessageDrivenBean.class"), TEST_EJB_RESOURCE_PATH + "/MessageDrivenBean.class"); }
@Override @@ -584,4 +541,83 @@ public class EjbArchive implements JavaArchive { public JavaArchive addAsResource(Package resourcePackage, String resourceName, ArchivePath target) throws IllegalArgumentException { return delegate.addAsResource(resourcePackage, resourceName, target); } + + private static final String v2_1_AND_3_x_MDB_ELEMENTS = + " <activation-config>\n" + + " <activation-config-property>\n" + + " <activation-config-property-name>messageSelector</activation-config-property-name>\n" + + " <activation-config-property-value>RECIPIENT='MDB'</activation-config-property-value>\n" + + " </activation-config-property>\n" + + " <activation-config-property>\n" + + " <activation-config-property-name>destinationType</activation-config-property-name>\n" + + " <activation-config-property-value>javax.jms.Queue</activation-config-property-value>\n" + + " </activation-config-property>\n" + + " <activation-config-property>\n" + + " <activation-config-property-name>destination</activation-config-property-name>\n" + + " <activation-config-property-value>jms/persistentQueue</activation-config-property-value>\n" + + " </activation-config-property>\n" + + " </activation-config>"; + + private enum EjbVersionInfo { + v2_0("2.0", "<!DOCTYPE ejb-jar PUBLIC \"-//Sun Microsystems, Inc.//DTD Enterprise JavaBeans 2.0//EN\"\n" + + " \"http://java.sun.com/dtd/ejb-jar_2_0.dtd\">\n<ejb-jar>", + " <message-selector>RECIPIENT='MDB'</message-selector>\n" + + " <message-driven-destination>\n" + + " <destination-type>javax.jms.Queue</destination-type>\n" + + " </message-driven-destination>"), + v2_1("2.1", "<ejb-jar version="2.1"\n" + + " xmlns="http://java.sun.com/xml/ns/j2ee%5C%22%5Cn" + + " xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance%5C%22%5Cn" + + " xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/ejb-jar_2_1.xsd%5C%22%3E", + v2_1_AND_3_x_MDB_ELEMENTS), + v3_0("3.0", "<ejb-jar version="3.0"\n" + "" + + " xmlns="http://java.sun.com/xml/ns/javaee%5C%22%5Cn" + + " xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance%5C%22%5Cn" + + " xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/ejb-jar_3_0.xsd%5C%22%3E", + v2_1_AND_3_x_MDB_ELEMENTS), + v3_1("3.1", "<ejb-jar version="3.1"\n" + + " xmlns="http://java.sun.com/xml/ns/javaee%5C%22%5Cn" + + " xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance%5C%22%5Cn" + + " xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/ejb-jar_3_1.xsd%5C%22%3E", + v2_1_AND_3_x_MDB_ELEMENTS); + + private String version; + private String rootElement; + private String mdbElements; + + EjbVersionInfo(String version, String rootElement, String mdbElements) { + this.version = version; + this.rootElement = rootElement; + this.mdbElements = mdbElements; + } + + private static final Map<String, EjbVersionInfo> VERSION_TO_VALUE_MAP = + new HashMap<String, EjbVersionInfo>(values().length); + static { + for (EjbVersionInfo value : values()) { + VERSION_TO_VALUE_MAP.put(value.getVersion(), value); + } + } + + public String getVersion() { + return version; + } + + public String getRootElement() { + return rootElement; + } + + public String getMdbElements() { + return mdbElements; + } + + public static EjbVersionInfo forVersion(String version) { + if (!VERSION_TO_VALUE_MAP.containsKey(version)) { + throw new IllegalArgumentException("Unsupported version: "" + version + + "". The following versions are supported: " + VERSION_TO_VALUE_MAP.keySet()); + } + return VERSION_TO_VALUE_MAP.get(version); + } + } + } diff --git a/modules/helpers/jeeGen/src/main/java/test/ejb/MessageDrivenBean.java b/modules/helpers/jeeGen/src/main/java/test/ejb/MessageDrivenBean.java new file mode 100644 index 0000000..780984f --- /dev/null +++ b/modules/helpers/jeeGen/src/main/java/test/ejb/MessageDrivenBean.java @@ -0,0 +1,109 @@ +package test.ejb; + +import javax.ejb.MessageDrivenContext; +import javax.jms.Destination; +import javax.jms.Message; +import javax.jms.MessageListener; +import javax.jms.Queue; +import javax.jms.QueueConnection; +import javax.jms.QueueConnectionFactory; +import javax.jms.QueueSender; +import javax.jms.QueueSession; +import javax.jms.Session; +import javax.naming.Context; +import javax.naming.InitialContext; + +/** + * Message-driven bean - impl. + */ +public class MessageDrivenBean implements javax.ejb.MessageDrivenBean, MessageListener { + + private MessageDrivenContext context; + + // Constructor, which is public and takes no arguments + public MessageDrivenBean() { + } + + /** + * Begin EJB-required methods. The following methods are called + * by the container, and never called by client code. + */ + + /** + * ejbCreate method, declared as public (but not final or + * static), with a return type of void, and with no arguments. + */ + public void ejbCreate() { + } + + public void setMessageDrivenContext(MessageDrivenContext context) { + // As with all enterprise beans, you must set the context in order to be + // able to use it at another time within the MDB methods + this.context = context; + } + + // life cycle Methods + + public void ejbRemove() { + } + + /** + * JMS MessageListener-required methods. The following + * methods are called by the container, and never called by + * client code. + */ + + // Receives the incoming Message and displays the text. + public void onMessage(Message message) { + // MDB does not carry state for an individual client + + try { + Context namingContext = new InitialContext(); + // 1. Retrieve the QueueConnectionFactory using a + // resource reference defined in the ejb-jar.xml file. + QueueConnectionFactory qcf = (QueueConnectionFactory) + namingContext.lookup("java:comp/env/jms/myQueueConnectionFactory"); + namingContext.close(); + + // 2. Create the queue connection + QueueConnection queueConnection = qcf.createQueueConnection(); + // 3. Create the session over the queue connection. + QueueSession queueSession = queueConnection.createQueueSession(false, Session.AUTO_ACKNOWLEDGE); + // 4. Create the sender to send messages over the session. + QueueSender queueSender = queueSession.createSender(null); + + // When the onMessage method is called, a message has been sent. + // You can retrieve attributes of the message using the Message object. + String txt = ("mdb rcv: " + message.getJMSMessageID()); + System.out.println(txt + " redel=" + + message.getJMSRedelivered() + " cnt=" + + message.getIntProperty("JMSXDeliveryCount")); + + // Create a new message using the createMessage method. + // To send it back to the originator of the other message, + // set the String property of "RECIPIENT" to "CLIENT." + // The client only looks for messages with string property CLIENT. + // Copy the original message ID into new message's Correlation ID for + // tracking purposes using the setJMSCorrelationID method. Finally, + // set the destination for the message using the getJMSReplyTo method + // on the previously received message. Send the message using the + // send method on the queue sender. + + // 5. Create a message using the createMessage method + Message returnMessage = queueSession.createMessage(); + // 6. Set properties of the message. + returnMessage.setStringProperty("RECIPIENT", "CLIENT"); + returnMessage.setIntProperty("count", message.getIntProperty("JMSXDeliveryCount")); + returnMessage.setJMSCorrelationID(message.getJMSMessageID()); + // 7. Retrieve the reply destination. + Destination destination = message.getJMSReplyTo(); + // 8. Send the message using the send method of the sender. + queueSender.send((Queue) destination, returnMessage); + System.out.println(txt + " snd: " + returnMessage.getJMSMessageID()); + // close the connection + queueConnection.close(); + } catch (Exception e) { + throw new RuntimeException("Failed to process message [" + message + "].", e); + } + } +} diff --git a/modules/helpers/jeeGen/src/main/resources/org/rhq/helpers/jeegen/ejb/v2/ejb-jar.xml.ftl b/modules/helpers/jeeGen/src/main/resources/org/rhq/helpers/jeegen/ejb/v2/ejb-jar.xml.ftl index efc5ceb..aa2ecae 100644 --- a/modules/helpers/jeeGen/src/main/resources/org/rhq/helpers/jeegen/ejb/v2/ejb-jar.xml.ftl +++ b/modules/helpers/jeeGen/src/main/resources/org/rhq/helpers/jeegen/ejb/v2/ejb-jar.xml.ftl @@ -76,5 +76,35 @@ ${rootElement} </session> </#list>
+<!-- Stateful Session Beans --> + +<#list 1..messageDrivenBeanCount as index> + <message-driven> + <description>a very simple message-driven bean</description> + <display-name>Simple Message-Driven Bean</display-name> + + <ejb-name>${name}MessageDrivenBean${index?c}</ejb-name> + <ejb-class>${package}.MessageDrivenBean</ejb-class> + <transaction-type>Container</transaction-type> + +${mdbElements} +<!-- + <resource-ref> + <description>description</description> + <res-ref-name>jms/myQueueConnectionFactory</res-ref-name> + <res-type>javax.jms.QueueConnectionFactory</res-type> + <res-auth>Application</res-auth> + <res-sharing-scope>Shareable</res-sharing-scope> + </resource-ref> +--> +<!-- + <resource-env-ref> + <resource-env-ref-name>jms/persistentQueue</resource-env-ref-name> + <resource-env-ref-type>javax.jms.Queue</resource-env-ref-type> + </resource-env-ref> +--> + </message-driven> +</#list> + </enterprise-beans> </ejb-jar>
rhq-commits@lists.fedorahosted.org