You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tomee.apache.org by jl...@apache.org on 2007/01/14 11:29:51 UTC

svn commit: r496059 - in /incubator/openejb/trunk/openejb3/examples/resource-injection: ./ src/main/java/org/apache/openejb/examples/injection/ src/main/resources/ src/main/resources/META-INF/ src/test/java/org/apache/openejb/examples/injection/

Author: jlaskowski
Date: Sun Jan 14 02:29:51 2007
New Revision: 496059

URL: http://svn.apache.org/viewvc?view=rev&rev=496059
Log:
OPENEJB-356 Example: @Resource for Setter Injection

Submitted by: Raj Saini

Added:
    incubator/openejb/trunk/openejb3/examples/resource-injection/src/main/java/org/apache/openejb/examples/injection/Invoice.java   (with props)
    incubator/openejb/trunk/openejb3/examples/resource-injection/src/main/java/org/apache/openejb/examples/injection/InvoiceBean.java   (with props)
    incubator/openejb/trunk/openejb3/examples/resource-injection/src/main/resources/
    incubator/openejb/trunk/openejb3/examples/resource-injection/src/main/resources/META-INF/
    incubator/openejb/trunk/openejb3/examples/resource-injection/src/main/resources/META-INF/ejb-jar.xml   (with props)
    incubator/openejb/trunk/openejb3/examples/resource-injection/src/test/java/org/apache/openejb/examples/injection/InvoiceBeanTest.java   (with props)
Modified:
    incubator/openejb/trunk/openejb3/examples/resource-injection/pom.xml

Modified: incubator/openejb/trunk/openejb3/examples/resource-injection/pom.xml
URL: http://svn.apache.org/viewvc/incubator/openejb/trunk/openejb3/examples/resource-injection/pom.xml?view=diff&rev=496059&r1=496058&r2=496059
==============================================================================
--- incubator/openejb/trunk/openejb3/examples/resource-injection/pom.xml (original)
+++ incubator/openejb/trunk/openejb3/examples/resource-injection/pom.xml Sun Jan 14 02:29:51 2007
@@ -7,9 +7,9 @@
   The ASF licenses this file to You under the Apache License, Version 2.0
   (the "License"); you may not use this file except in compliance with
   the License.  You may obtain a copy of the License at
-  
-  http://www.apache.org/licenses/LICENSE-2.0
-  
+
+     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.

Added: incubator/openejb/trunk/openejb3/examples/resource-injection/src/main/java/org/apache/openejb/examples/injection/Invoice.java
URL: http://svn.apache.org/viewvc/incubator/openejb/trunk/openejb3/examples/resource-injection/src/main/java/org/apache/openejb/examples/injection/Invoice.java?view=auto&rev=496059
==============================================================================
--- incubator/openejb/trunk/openejb3/examples/resource-injection/src/main/java/org/apache/openejb/examples/injection/Invoice.java (added)
+++ incubator/openejb/trunk/openejb3/examples/resource-injection/src/main/java/org/apache/openejb/examples/injection/Invoice.java Sun Jan 14 02:29:51 2007
@@ -0,0 +1,28 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.openejb.examples.injection;
+
+/**
+ * @version $Rev$ $Date$
+ */
+public interface Invoice {
+
+    void addLineItem(LineItem item) throws TooManyItemsException;
+
+    int getMaxLineItems();
+
+}

Propchange: incubator/openejb/trunk/openejb3/examples/resource-injection/src/main/java/org/apache/openejb/examples/injection/Invoice.java
------------------------------------------------------------------------------
    svn:keywords = Date Rev Author Id Revision HeadURL

Added: incubator/openejb/trunk/openejb3/examples/resource-injection/src/main/java/org/apache/openejb/examples/injection/InvoiceBean.java
URL: http://svn.apache.org/viewvc/incubator/openejb/trunk/openejb3/examples/resource-injection/src/main/java/org/apache/openejb/examples/injection/InvoiceBean.java?view=auto&rev=496059
==============================================================================
--- incubator/openejb/trunk/openejb3/examples/resource-injection/src/main/java/org/apache/openejb/examples/injection/InvoiceBean.java (added)
+++ incubator/openejb/trunk/openejb3/examples/resource-injection/src/main/java/org/apache/openejb/examples/injection/InvoiceBean.java Sun Jan 14 02:29:51 2007
@@ -0,0 +1,80 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.openejb.examples.injection;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import javax.annotation.Resource;
+import javax.ejb.Remote;
+
+/**
+ * This example demostrates the use of the injection of environment entries
+ * using <b>Resource</b> annotation.
+ * 
+ * "EJB Core Contracts and Requirements" specification section 16.4.1.1.
+ * 
+ * @version $Rev$ $Date$
+ */
+
+@Remote
+public class InvoiceBean implements Invoice {
+
+    int maxLineItems;
+
+    private List<LineItem> items = new ArrayList<LineItem>();
+
+    private int itemCount;
+
+    /**
+     * Injects the <b>maxLineItems</b> simple environment entry through bean
+     * method.
+     * 
+     * The JavaBeans property name (not the method name) is used as the default
+     * JNDI name. By default, the JavaBeans propery name is combined with the
+     * name of the class in which the annotation is used and is used directly as
+     * the name in the bean's naming context. JNDI name for this entry would
+     * be
+     * java:comp/env/org.apache.openejb.examples.resource.InvoiceBean/maxLineItems
+     * 
+     * Refer "EJB Core Contracts and Requirements" specification section 16.2.2.
+     * 
+     * @param maxLineItems
+     */
+    @Resource
+    public void setMaxLineItems(int maxLineItems) {
+        this.maxLineItems = maxLineItems;
+    }
+
+    public void addLineItem(LineItem item) throws TooManyItemsException {
+        if (item == null) {
+            throw new IllegalArgumentException("Line item must not be null");
+        }
+
+        if (itemCount <= maxLineItems) {
+            items.add(item);
+            itemCount++;
+        } else {
+            throw new TooManyItemsException("Number of items exceeded the maximum limit");
+        }
+    }
+
+    public int getMaxLineItems() {
+        return this.maxLineItems;
+    }
+
+}

Propchange: incubator/openejb/trunk/openejb3/examples/resource-injection/src/main/java/org/apache/openejb/examples/injection/InvoiceBean.java
------------------------------------------------------------------------------
    svn:keywords = Date Rev Author Id Revision HeadURL

Added: incubator/openejb/trunk/openejb3/examples/resource-injection/src/main/resources/META-INF/ejb-jar.xml
URL: http://svn.apache.org/viewvc/incubator/openejb/trunk/openejb3/examples/resource-injection/src/main/resources/META-INF/ejb-jar.xml?view=auto&rev=496059
==============================================================================
--- incubator/openejb/trunk/openejb3/examples/resource-injection/src/main/resources/META-INF/ejb-jar.xml (added)
+++ incubator/openejb/trunk/openejb3/examples/resource-injection/src/main/resources/META-INF/ejb-jar.xml Sun Jan 14 02:29:51 2007
@@ -0,0 +1,37 @@
+<?xml version="1.0" encoding="US-ASCII"?>
+<!--
+ 
+ Licensed to the Apache Software Foundation (ASF) under one or more
+ contributor license agreements.  See the NOTICE file distributed with
+ this work for additional information regarding copyright ownership.
+ The ASF licenses this file to You under the Apache License, Version 2.0
+ (the "License"); you may not use this file except in compliance with
+ the License.  You may obtain a copy of the License at
+
+     http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing, software
+ distributed under the License is distributed on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ See the License for the specific language governing permissions and
+ limitations under the License.
+-->
+
+<!-- $Rev$ $Date$ -->
+
+<ejb-jar xmlns="http://java.sun.com/xml/ns/javaee" version="3.0" metadata-complete="true">
+  <enterprise-beans>
+    <session>
+      <ejb-name>InvoiceBean</ejb-name>
+      <ejb-class>org.apache.openejb.examples.injection.InvoiceBean</ejb-class>
+      <session-type>Stateful</session-type>
+      <transaction-type>Container</transaction-type>
+      <env-entry>
+        <description>The maximum number of line items per invoice.</description>
+        <env-entry-name>org.apache.openejb.examples.injection.InvoiceBean/maxLineItems</env-entry-name>
+        <env-entry-type>java.lang.Integer</env-entry-type>
+        <env-entry-value>15</env-entry-value>
+      </env-entry>
+    </session>
+  </enterprise-beans>
+</ejb-jar>

Propchange: incubator/openejb/trunk/openejb3/examples/resource-injection/src/main/resources/META-INF/ejb-jar.xml
------------------------------------------------------------------------------
    svn:keywords = Date Rev Author Id Revision HeadURL

Added: incubator/openejb/trunk/openejb3/examples/resource-injection/src/test/java/org/apache/openejb/examples/injection/InvoiceBeanTest.java
URL: http://svn.apache.org/viewvc/incubator/openejb/trunk/openejb3/examples/resource-injection/src/test/java/org/apache/openejb/examples/injection/InvoiceBeanTest.java?view=auto&rev=496059
==============================================================================
--- incubator/openejb/trunk/openejb3/examples/resource-injection/src/test/java/org/apache/openejb/examples/injection/InvoiceBeanTest.java (added)
+++ incubator/openejb/trunk/openejb3/examples/resource-injection/src/test/java/org/apache/openejb/examples/injection/InvoiceBeanTest.java Sun Jan 14 02:29:51 2007
@@ -0,0 +1,61 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.openejb.examples.injection;
+
+import java.util.Properties;
+
+import javax.naming.Context;
+import javax.naming.InitialContext;
+
+import junit.framework.TestCase;
+
+/**
+ * @version $Rev$ $Date$
+ */
+public class InvoiceBeanTest extends TestCase {
+
+    private InitialContext initialContext;
+
+    protected void setUp() throws Exception {
+        Properties properties = new Properties();
+        properties.setProperty(Context.INITIAL_CONTEXT_FACTORY, "org.apache.openejb.client.LocalInitialContextFactory");
+        properties.setProperty("openejb.deployments.classpath.include", ".*resource-injection.*");
+
+        initialContext = new InitialContext(properties);
+    }
+
+    public void testAddLineItem() throws Exception {
+        Invoice order = (Invoice) initialContext.lookup("InvoiceBeanBusinessRemote");
+        assertNotNull(order);
+        LineItem item = new LineItem("ABC-1", "Test Item");
+
+        try {
+            order.addLineItem(item);
+        } catch (TooManyItemsException tmie) {
+            fail("Test failed due to: " + tmie.getMessage());
+        }
+    }
+
+    public void testGetMaxLineItems() throws Exception {
+        Invoice order = (Invoice) initialContext.lookup("InvoiceBeanBusinessRemote");
+        assertNotNull(order);
+
+        int maxLineItems = order.getMaxLineItems();
+
+        assertEquals(15, maxLineItems);
+    }
+}

Propchange: incubator/openejb/trunk/openejb3/examples/resource-injection/src/test/java/org/apache/openejb/examples/injection/InvoiceBeanTest.java
------------------------------------------------------------------------------
    svn:keywords = Date Rev Author Id Revision HeadURL