001: package org.mockejb.test;
002:
003: import javax.ejb.*;
004: import javax.naming.*;
005: import java.util.*;
006:
007: import java.rmi.RemoteException;
008: import java.security.Principal;
009: import java.sql.*;
010:
011: import javax.sql.DataSource;
012:
013: import javax.jms.*;
014:
015: import org.apache.commons.logging.*;
016:
017: /**
018: * Sample EJB with methods to access other beans and JDBC.
019: *
020: * @ejb.bean type="Stateless" view-type="remote" name="SampleService"
021: * jndi-name="mockejb/SampleService"
022: *
023: * @ejb.ejb-ref ejb-name="SampleHelper" view-type="local" ref-name="ejb/sampleHelper"
024: * @ejb.resource-ref res-ref-name="jdbc/SampleDataSource" res-type="javax.sql.DataSource" res-auth="Container" jndi-name="jdbc/SampleDataSource"
025: *
026: */
027: public class SampleServiceBean extends BaseSessionBean {
028:
029: /**
030: * JNDI name used to access HelperBean. We assume that ejb-ref with
031: * this name is configured in the deployment descriptor if this bean
032: * runs in the app server.
033: */
034: public final static String HELPER_BEAN_JNDI_NAME = "java:comp/env/ejb/sampleHelper";
035:
036: // logger for this class
037: private static Log logger = LogFactory
038: .getLog(SampleServiceBean.class.getName());
039:
040: /**
041: * Simple method which returns an input parameter back to the caller.
042: *
043: * @ejb.interface-method
044: * @ejb:transaction type="Supports"
045: */
046: public String echoString(String input) {
047:
048: return input;
049:
050: }
051:
052: /**
053: * Calls a method of {@link SampleHelper}.
054: * Demonstrates the use of mock JNDI to lookup the beans.
055: *
056: * @return a string returned by SampleHelper
057: * @throws NamingException
058: * @throws CreateException
059: *
060: * @ejb.interface-method
061: * @ejb:transaction type="Supports"
062: */
063: public String invokeOtherBean() throws NamingException,
064: CreateException {
065:
066: Context ctx = new InitialContext();
067:
068: SampleHelperHome helperHome = (SampleHelperHome) ctx
069: .lookup(HELPER_BEAN_JNDI_NAME);
070:
071: SampleHelper helperBean = helperHome.create();
072:
073: return helperBean.dummyMethod("some value");
074: }
075:
076: /**
077: *
078: * @ejb.interface-method
079: * @ejb:transaction type="Supports"
080: */
081: public void invokeExternalService() throws NamingException,
082: CreateException, RemoteException {
083:
084: Context ctx = new InitialContext();
085:
086: ExternalServiceHome externalServiceHome = (ExternalServiceHome) ctx
087: .lookup(ExternalService.JNDI_NAME);
088:
089: ExternalService externalService = externalServiceHome.create();
090:
091: /*
092: * In this example we don't do anything with the the return value,
093: * in reality we should have some code that uses it.
094: */
095: externalService.sampleMethod();
096:
097: }
098:
099: /**
100: * Sends the message with the provided message text to the test topic
101: * @param message message text to send
102: * @throws NamingException
103: * @throws JMSException
104: *
105: * @ejb.interface-method
106: * @ejb:transaction type="Supports"
107: */
108: public void sendMessage(String message) throws NamingException,
109: JMSException {
110:
111: InitialContext ctx = new InitialContext();
112:
113: TopicConnectionFactory topicConnectionFactory = (TopicConnectionFactory) ctx
114: .lookup("SampleConnectionFactory");
115: TopicConnection topicConnection = topicConnectionFactory
116: .createTopicConnection();
117: TopicSession topicSession = topicConnection.createTopicSession(
118: false, Session.AUTO_ACKNOWLEDGE);
119:
120: Topic sampleTopic = (Topic) ctx.lookup("SampleTopic");
121: TopicPublisher publisher = topicSession
122: .createPublisher(sampleTopic);
123: // create the message
124: TextMessage textMessage = topicSession.createTextMessage();
125: textMessage.setText(message);
126:
127: // post the message to the topic
128: publisher.publish(textMessage);
129:
130: }
131:
132: /**
133: * Retrieves all values for the column from the provided table
134: * @param columnName column name to select from
135: * @param tableName table name to select from
136: * @return collection of retrieved values
137: *
138: * @ejb.interface-method
139: * @ejb:transaction type="Required"
140: */
141: public Collection selectFromTable(String tableName,
142: String columnName) throws NamingException, SQLException {
143:
144: Context ctx = new InitialContext();
145:
146: DataSource ds = (DataSource) ctx
147: .lookup("java:comp/env/jdbc/SampleDataSource");
148:
149: java.sql.Connection con = ds.getConnection();
150:
151: log("Connected.");
152:
153: Statement stmt = con.createStatement();
154: ResultSet rs = stmt.executeQuery("select " + columnName
155: + " from " + tableName);
156:
157: Collection collection = new ArrayList();
158: while (rs.next()) {
159: String s = rs.getString(1);
160: collection.add(s);
161: //log(s);
162: }
163:
164: con.close();
165:
166: return collection;
167: }
168:
169: /**
170: * Example of using SessionContext to rollback
171: * a database transaction
172: * @ejb.interface-method
173: * @ejb.transaction type="Required"
174: *
175: */
176: public void rollbackSampleTransaction() throws NamingException,
177: SQLException, CreateException {
178:
179: Context ctx = new InitialContext();
180:
181: SampleHelperHome helperBeanHome = (SampleHelperHome) ctx
182: .lookup(HELPER_BEAN_JNDI_NAME);
183: SampleHelper helperBean = helperBeanHome.create();
184:
185: helperBean.insertData();
186:
187: sessionCtx.setRollbackOnly();
188:
189: }
190:
191: /**
192: *
193: * @ejb.interface-method
194: * @ejb:transaction type="Supports"
195: */
196: public void throwSystemException() throws NamingException,
197: CreateException, RuntimeException {
198:
199: Context ctx = new InitialContext();
200: SampleHelperHome helperHome = (SampleHelperHome) ctx
201: .lookup(HELPER_BEAN_JNDI_NAME);
202: SampleHelper helperBean = helperHome.create();
203: helperBean.throwSystemException();
204: }
205:
206: /**
207: *
208: * @ejb.interface-method
209: * @ejb:transaction type="Supports"
210: */
211: public void throwAppException() throws Exception {
212: throw new Exception("Example of application exception");
213: }
214:
215: /**
216: * Returns the principal provided by the "getCallerPrincipal" method of
217: * the EJBContext for this bean.
218: *
219: * @return current principal
220: *
221: * @ejb.interface-method
222: */
223: public Principal getPrincipal() {
224:
225: return sessionCtx.getCallerPrincipal();
226:
227: }
228:
229: /**
230: * Returns the result of the EJBContext.isCallerInRole check.
231: * @param name role name
232: * @return true if the current user has the given role
233: *
234: * @ejb.interface-method
235: */
236: public boolean hasRole(String role) {
237:
238: return sessionCtx.isCallerInRole(role);
239:
240: }
241:
242: }
|