Source Code Cross Referenced for SampleServiceBean.java in  » Testing » MockEJB » org » mockejb » test » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Testing » MockEJB » org.mockejb.test 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


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:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.