001: /**
002: * EasyBeans
003: * Copyright (C) 2006 Bull S.A.S.
004: * Contact: easybeans@ow2.org
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2.1 of the License, or any later version.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this library; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
019: * USA
020: *
021: * --------------------------------------------------------------------------
022: * $Id: MDBResourceRef.java 1970 2007-10-16 11:49:25Z benoitf $
023: * --------------------------------------------------------------------------
024: */package org.ow2.easybeans.tests.common.ejbs.mdb.containermanaged.resourceref;
025:
026: import static org.ow2.easybeans.tests.common.ejbs.entity.callbacklogger.CallbackType.ON_MESSAGE;
027: import static org.ow2.easybeans.tests.common.ejbs.entity.callbacklogger.OperationType.ANNOTATION_INJECTION_FIELD;
028: import static org.ow2.easybeans.tests.common.ejbs.entity.callbacklogger.OperationType.ANNOTATION_INJECTION_METHOD;
029: import static org.ow2.easybeans.tests.common.ejbs.entity.callbacklogger.OperationType.ANNOTATION_RESOURCES_DECLARATION;
030: import static org.ow2.easybeans.tests.common.ejbs.entity.callbacklogger.OperationType.ANNOTATION_RESOURCE_DECLARATION;
031: import static org.ow2.easybeans.tests.common.ejbs.entity.callbacklogger.OperationType.OVERRIDE_INJECTION_FIELD;
032: import static org.ow2.easybeans.tests.common.ejbs.entity.callbacklogger.OperationType.OVERRIDE_INJECTION_METHOD;
033: import static org.ow2.easybeans.tests.common.ejbs.entity.callbacklogger.OperationType.XML_INJECTION_FIELD;
034: import static org.ow2.easybeans.tests.common.ejbs.entity.callbacklogger.OperationType.XML_INJECTION_METHOD;
035: import static org.ow2.easybeans.tests.common.ejbs.entity.callbacklogger.OperationType.XML_RESOURCE_DECLARATION;
036: import static org.ow2.easybeans.tests.common.helper.ContextHelper.checkResource;
037:
038: import javax.annotation.Resource;
039: import javax.annotation.Resources;
040: import javax.ejb.ActivationConfigProperty;
041: import javax.ejb.MessageDriven;
042: import javax.ejb.MessageDrivenContext;
043: import javax.jms.Message;
044: import javax.jms.MessageListener;
045: import javax.sql.DataSource;
046:
047: import org.ow2.easybeans.tests.common.ejbs.stateless.containermanaged.callbacklogger.BaseInsertOperation;
048: import org.ow2.easybeans.tests.common.jms.JMSManager;
049: import org.ow2.util.log.Log;
050: import org.ow2.util.log.LogFactory;
051:
052: /**
053: * This bean is used to test the "resource" annotation on the bean class.
054: * Resources will be declared in the environment, but they will not be injected.
055: * @author Eduardo Studzinski Estima de Castro
056: * @author Gisele Pinheiro Souza
057: */
058: @MessageDriven(name="MDBResourceRef",messageListenerInterface=MessageListener.class,activationConfig={@ActivationConfigProperty(propertyName="destinationType",propertyValue="javax.jms.Queue"),@ActivationConfigProperty(propertyName="destination",propertyValue=JMSManager.DEFAULT_QUEUE),@ActivationConfigProperty(propertyName="messageSelector",propertyValue="TYPE = 'org.ow2.easybeans.tests." + "common.ejbs.mdb.containermanaged.resourceref.MDBResourceRef'")})
059: @Resources({@Resource(name="jdbc/ds00",type=javax.sql.DataSource.class)})
060: @Resource(name="jdbc/ds01",type=javax.sql.DataSource.class)
061: public class MDBResourceRef extends BaseInsertOperation {
062:
063: /**
064: * Logger.
065: */
066: private static Log logger = LogFactory.getLog(MDBResourceRef.class);
067:
068: /**
069: * Message type.
070: */
071: public static final String MESSAGE_TYPE = "org.ow2.easybeans.tests.common.ejbs.mdb.containermanaged.resourceref."
072: + "MDBResourceRef";
073:
074: /**
075: * Context.
076: */
077: @Resource
078: private MessageDrivenContext ctx;
079:
080: /**
081: * Field.
082: */
083: private DataSource dsMethodInjection;
084:
085: /**
086: * The value must be injected by the container.
087: */
088: @Resource(name="jdbc/dsFieldInjection",mappedName="jdbc_1")
089: private DataSource dsFieldInjection;
090:
091: /**
092: * The value must be injected by the container using the XML value.
093: */
094: private DataSource dsXMLFieldInjection;
095:
096: /**
097: * The value must be injected by the container using the XML value.
098: */
099: private DataSource dsXMLMethodInjection;
100:
101: /**
102: * The value must be injected by the container using the XML value (Override
103: * test).
104: */
105: @Resource(name="jdbc/dsOverrideField")
106: private DataSource dsOverrideFieldInjection;
107:
108: /**
109: * The value must be injected by the container using the XML value (Override
110: * test).
111: */
112: private DataSource dsOverrideMethodInjection;
113:
114: /**
115: * The value must be injected by the container.
116: * @param value information value
117: */
118: @SuppressWarnings("unused")
119: @Resource(name="jdbc/dsMethodInjection",mappedName="jdbc_1")
120: private void setMethodInjection(final DataSource value) {
121: dsMethodInjection = value;
122: }
123:
124: /**
125: * The value must be injected by the container.
126: * @param value information value
127: */
128: @SuppressWarnings("unused")
129: private void setXMLMethodInjection(final DataSource value) {
130: dsXMLMethodInjection = value;
131: }
132:
133: /**
134: * The value must be injected by the container.
135: * @param value information value
136: */
137: @SuppressWarnings("unused")
138: @Resource(name="jdbc/dsOverrideMethod")
139: private void setOverrideMethodInjection(final DataSource value) {
140: dsOverrideMethodInjection = value;
141: }
142:
143: /**
144: * Tests declaration and injection.
145: * @param message msg
146: */
147: public void onMessage(final Message message) {
148:
149: // Declaration using annotation.
150: try {
151: checkResource(ctx, "jdbc/dsDeclaration00");
152: super .log(MDBResourceRef.class, ON_MESSAGE,
153: MDBResourceRef.class,
154: ANNOTATION_RESOURCES_DECLARATION);
155: logger.debug("{0} is working properly.",
156: ANNOTATION_RESOURCES_DECLARATION.toString());
157: } catch (Exception e) {
158: logger.error("Error checking {0}: {1}",
159: ANNOTATION_RESOURCES_DECLARATION.toString(), e
160: .getMessage());
161: }
162: try {
163: checkResource(ctx, "jdbc/dsDeclaration01");
164: super .log(MDBResourceRef.class, ON_MESSAGE,
165: MDBResourceRef.class,
166: ANNOTATION_RESOURCE_DECLARATION);
167: logger.debug("{0} is working properly.",
168: ANNOTATION_RESOURCE_DECLARATION.toString());
169: } catch (Exception e) {
170: logger.error("Error checking {0}: {1}",
171: ANNOTATION_RESOURCE_DECLARATION.toString(), e
172: .getMessage());
173: }
174:
175: // Declaration using XML.
176: try {
177: checkResource(ctx, "jdbc/dsXMLDeclaration");
178: super .log(MDBResourceRef.class, ON_MESSAGE,
179: MDBResourceRef.class, XML_RESOURCE_DECLARATION);
180: logger.debug("{0} is working properly.",
181: XML_RESOURCE_DECLARATION.toString());
182: } catch (Exception e) {
183: logger
184: .error("Error checking {0}: {1}",
185: XML_RESOURCE_DECLARATION.toString(), e
186: .getMessage());
187: }
188:
189: // Injection in a field using annotation.
190: try {
191:
192: checkResource(ctx, dsFieldInjection,
193: "jdbc/dsFieldInjection");
194: super .log(MDBResourceRef.class, ON_MESSAGE,
195: MDBResourceRef.class, ANNOTATION_INJECTION_FIELD);
196: logger.debug("{0} is working properly.",
197: ANNOTATION_INJECTION_FIELD.toString());
198: } catch (Exception e) {
199: logger.error("Error checking {0}: {1}",
200: ANNOTATION_INJECTION_FIELD.toString(), e
201: .getMessage());
202: }
203:
204: // Injection in a method using annotation.
205: try {
206: checkResource(ctx, dsMethodInjection,
207: "jdbc/dsMethodInjection");
208: super .log(MDBResourceRef.class, ON_MESSAGE,
209: MDBResourceRef.class, ANNOTATION_INJECTION_METHOD);
210: logger.debug("{0} is working properly.",
211: ANNOTATION_INJECTION_METHOD.toString());
212: } catch (Exception e) {
213: logger.error("Error checking {0}: {1}",
214: ANNOTATION_INJECTION_METHOD.toString(), e
215: .getMessage());
216: }
217:
218: // Injection in a field using XML.
219: try {
220: checkResource(ctx, dsXMLFieldInjection,
221: "jdbc/dsXMLFieldInjection");
222: super .log(MDBResourceRef.class, ON_MESSAGE,
223: MDBResourceRef.class, XML_INJECTION_FIELD);
224: logger.debug("{0} is working properly.",
225: XML_INJECTION_FIELD.toString());
226: } catch (Exception e) {
227: logger.error("Error checking {0}: {1}", XML_INJECTION_FIELD
228: .toString(), e.getMessage());
229: }
230:
231: // Injection in a method using XML.
232: try {
233: checkResource(ctx, dsXMLMethodInjection,
234: "jdbc/dsXMLMethodInjection");
235: super .log(MDBResourceRef.class, ON_MESSAGE,
236: MDBResourceRef.class, XML_INJECTION_METHOD);
237: logger.debug("{0} is working properly.",
238: XML_INJECTION_METHOD.toString());
239: } catch (Exception e) {
240: logger.error("Error checking {0}: {1}",
241: XML_INJECTION_METHOD.toString(), e.getMessage());
242: }
243:
244: // Override an injection in a field with a XML.
245: try {
246: // Warning: the "jdbc/dsOverrideFieldInjection" is the injection
247: // declared using the XML.
248: checkResource(ctx, dsOverrideFieldInjection,
249: "jdbc/dsOverrideFieldInjection");
250: super .log(MDBResourceRef.class, ON_MESSAGE,
251: MDBResourceRef.class, OVERRIDE_INJECTION_FIELD);
252: logger.debug("{0} is working properly.",
253: OVERRIDE_INJECTION_FIELD.toString());
254: } catch (Exception e) {
255: logger
256: .error("Error checking {0}: {1}",
257: OVERRIDE_INJECTION_FIELD.toString(), e
258: .getMessage());
259: }
260:
261: // Override an injection in a method with a XML.
262: try {
263: // Warning: the "jdbc/dsOverrideMethodInjection" is the injection
264: // declared using the XML.
265: checkResource(ctx, dsOverrideMethodInjection,
266: "jdbc/dsOverrideMethodInjection");
267: super .log(MDBResourceRef.class, ON_MESSAGE,
268: MDBResourceRef.class, OVERRIDE_INJECTION_METHOD);
269: logger.debug("{0} is working properly.",
270: OVERRIDE_INJECTION_METHOD.toString());
271: } catch (Exception e) {
272: logger.error("Error checking {0}: {1}",
273: OVERRIDE_INJECTION_METHOD.toString(), e
274: .getMessage());
275: }
276: }
277:
278: }
|