package org.mockejb.test;
import javax.ejb.*;
import javax.naming.*;
import java.util.*;
import java.rmi.RemoteException;
import java.security.Principal;
import java.sql.*;
import javax.sql.DataSource;
import javax.jms.*;
import org.apache.commons.logging.*;
public class SampleServiceBean extends BaseSessionBean {
public final static String HELPER_BEAN_JNDI_NAME = "java:comp/env/ejb/sampleHelper";
private static Log logger = LogFactory.getLog( SampleServiceBean.class.getName() );
public String echoString( String input ) {
return input;
}
public String invokeOtherBean() throws NamingException, CreateException {
Context ctx = new InitialContext();
SampleHelperHome helperHome =
(SampleHelperHome)ctx.lookup( HELPER_BEAN_JNDI_NAME );
SampleHelper helperBean = helperHome.create();
return helperBean.dummyMethod( "some value");
}
public void invokeExternalService() throws NamingException, CreateException, RemoteException {
Context ctx = new InitialContext();
ExternalServiceHome externalServiceHome =
(ExternalServiceHome)ctx.lookup( ExternalService.JNDI_NAME );
ExternalService externalService = externalServiceHome.create();
externalService.sampleMethod();
}
public void sendMessage( String message ) throws NamingException, JMSException{
InitialContext ctx = new InitialContext();
TopicConnectionFactory topicConnectionFactory =
(TopicConnectionFactory) ctx.lookup("SampleConnectionFactory");
TopicConnection topicConnection = topicConnectionFactory.createTopicConnection();
TopicSession topicSession = topicConnection.createTopicSession(false,
Session.AUTO_ACKNOWLEDGE);
Topic sampleTopic =
(Topic) ctx.lookup("SampleTopic");
TopicPublisher publisher = topicSession.createPublisher( sampleTopic );
TextMessage textMessage = topicSession.createTextMessage();
textMessage.setText( message );
publisher.publish( textMessage );
}
public Collection selectFromTable( String tableName, String columnName )
throws NamingException, SQLException {
Context ctx = new InitialContext();
DataSource ds = (DataSource)ctx.lookup("java:comp/env/jdbc/SampleDataSource");
java.sql.Connection con = ds.getConnection();
log("Connected.");
Statement stmt =con.createStatement();
ResultSet rs = stmt.executeQuery("select "+columnName+" from "+tableName);
Collection collection = new ArrayList();
while (rs.next()) {
String s = rs.getString(1);
collection.add(s);
}
con.close();
return collection;
}
public void rollbackSampleTransaction() throws NamingException,
SQLException, CreateException {
Context ctx = new InitialContext();
SampleHelperHome helperBeanHome =
(SampleHelperHome)ctx.lookup( HELPER_BEAN_JNDI_NAME );
SampleHelper helperBean = helperBeanHome.create();
helperBean.insertData();
sessionCtx.setRollbackOnly();
}
public void throwSystemException() throws NamingException, CreateException, RuntimeException {
Context ctx = new InitialContext();
SampleHelperHome helperHome =
(SampleHelperHome)ctx.lookup( HELPER_BEAN_JNDI_NAME );
SampleHelper helperBean = helperHome.create();
helperBean.throwSystemException();
}
public void throwAppException() throws Exception {
throw new Exception("Example of application exception");
}
public Principal getPrincipal(){
return sessionCtx.getCallerPrincipal();
}
public boolean hasRole( String role ){
return sessionCtx.isCallerInRole( role );
}
}