Source Code Cross Referenced for EnvBean.java in  » J2EE » JOnAS-4.8.6 » sampleappli » 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 » J2EE » JOnAS 4.8.6 » sampleappli 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /**
002:         * JOnAS: Java(TM) Open Application Server
003:         * Copyright (C) 1999-2004 Bull S.A.
004:         * Contact: jonas-team@objectweb.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:         * Initial developer(s): ____________________________________.
022:         * Contributor(s): ______________________________________.
023:         *
024:         * --------------------------------------------------------------------------
025:         * $Id: EnvBean.java 4618 2004-04-19 06:39:30Z benoitf $
026:         * --------------------------------------------------------------------------
027:         */package sampleappli;
028:
029:        import java.rmi.RemoteException;
030:        import java.sql.Connection;
031:        import java.sql.SQLException;
032:        import java.sql.Statement;
033:        import java.io.File;
034:        import javax.ejb.CreateException;
035:        import javax.ejb.EJBException;
036:        import javax.ejb.RemoveException;
037:        import javax.ejb.EJBObject;
038:        import javax.ejb.SessionBean;
039:        import javax.ejb.SessionContext;
040:        import javax.naming.Context;
041:        import javax.naming.InitialContext;
042:        import javax.naming.NamingException;
043:        import javax.sql.DataSource;
044:
045:        /**
046:         * EnvBean is a stateless session bean use to create and clean a correct
047:         * environement for the running of the StockClient it creates/remove the
048:         * StockTable and the order file
049:         * @author JOnAS team
050:         */
051:        public class EnvBean implements  SessionBean {
052:
053:            SessionContext ejbContext;
054:
055:            DataSource dataSource = null;
056:
057:            Connection conn = null;
058:
059:            Statement stmt;
060:
061:            String stocktablename = null;
062:
063:            String orderfilename = null;
064:
065:            // ------------------------------------------------------------------
066:            // SessionBean implementation
067:            // ------------------------------------------------------------------
068:
069:            /**
070:             * Set the associated session context. The container calls this method after
071:             * the instance creation. The enterprise Bean instance should store the
072:             * reference to the context object in an instance variable. This method is
073:             * called with no transaction context.
074:             * @param sessionContext A SessionContext interface for the instance.
075:             * @throws EJBException Thrown by the method to indicate a failure caused by
076:             *         a system-level error.
077:             */
078:            public void setSessionContext(SessionContext ctx) {
079:                ejbContext = ctx;
080:                if (dataSource == null) {
081:                    // Finds DataSource from JNDI
082:                    Context initialContext = null;
083:                    try {
084:                        initialContext = new InitialContext();
085:                        dataSource = (DataSource) initialContext
086:                                .lookup("java:comp/env/jdbc/myDS");
087:                    } catch (Exception e) {
088:                        System.err.println("    new InitialContext() : " + e);
089:                        throw new EJBException("Cannot get JNDI InitialContext");
090:                    }
091:                    // Find the stocktablename
092:                    try {
093:                        stocktablename = (String) initialContext
094:                                .lookup("java:comp/env/stocktablename");
095:                    } catch (Exception e) {
096:                        System.err.println("cannot lookup environment " + e);
097:                        throw new EJBException("cannot lookup environment");
098:                    }
099:                    // Find the orderfilename
100:                    try {
101:                        orderfilename = (String) initialContext
102:                                .lookup("java:comp/env/orderfilename");
103:                    } catch (Exception e) {
104:                        System.err.println("cannot lookup environment " + e);
105:                        throw new EJBException("cannot lookup environment");
106:                    }
107:                }
108:            }
109:
110:            /**
111:             * A container invokes this method before it ends the life of the session
112:             * object. This happens as a result of a client's invoking a remove
113:             * operation, or when a container decides to terminate the session object
114:             * after a timeout. This method is called with no transaction context.
115:             * @throws EJBException Thrown by the method to indicate a failure caused by
116:             *         a system-level error.
117:             */
118:            public void ejbRemove() {
119:            }
120:
121:            /**
122:             * The Session bean must define 1 or more ejbCreate methods.
123:             * @throws CreateException Failure to create a session EJB object.
124:             */
125:            public void ejbCreate() throws CreateException {
126:            }
127:
128:            /**
129:             * A container invokes this method on an instance before the instance
130:             * becomes disassociated with a specific EJB object.
131:             */
132:            public void ejbPassivate() {
133:            }
134:
135:            /**
136:             * A container invokes this method when the instance is taken out of the
137:             * pool of available instances to become associated with a specific EJB
138:             * object.
139:             */
140:            public void ejbActivate() {
141:            }
142:
143:            // ------------------------------------------------------------------
144:            // Env implementation
145:            // ------------------------------------------------------------------
146:
147:            /**
148:             * init
149:             */
150:            public void init() {
151:                createTableStock();
152:                createOrderFile();
153:            }
154:
155:            /**
156:             * clean
157:             */
158:            public void clean() {
159:                dropTableStock();
160:                removeOrderFile();
161:            }
162:
163:            // ------------------------------------------------------------------
164:            // Env internal methods
165:            // ------------------------------------------------------------------
166:            public void dropTableStock() {
167:                // drop table
168:                try {
169:                    conn = dataSource.getConnection();
170:                    stmt = conn.createStatement();
171:                    stmt.execute("DROP TABLE " + stocktablename);
172:                    stmt.close();
173:                    conn.close();
174:                } catch (Exception e) {
175:                    System.err.println("Exception in dropTable : \n"
176:                            + stocktablename + " " + e);
177:                }
178:            }
179:
180:            public void createTableStock() {
181:                // get connection
182:                try {
183:                    conn = dataSource.getConnection();
184:
185:                } catch (Exception e) {
186:                    System.err.println("Cannot get Connection: \n" + e);
187:                    throw new EJBException("Cannot get Connection");
188:                }
189:                try {
190:                    stmt = conn.createStatement();
191:                    stmt.execute("DROP TABLE " + stocktablename);
192:                    stmt.close();
193:                } catch (Exception e) {
194:                }
195:                // create table
196:                try {
197:                    stmt = conn.createStatement();
198:                    stmt
199:                            .execute("create table "
200:                                    + stocktablename
201:                                    + "(ID varchar(5) not null primary key, QUANTITY integer)");
202:                    stmt.execute("insert into " + stocktablename
203:                            + " values ('00000', 10)");
204:                    stmt.execute("insert into " + stocktablename
205:                            + " values ('00001', 20)");
206:                    stmt.execute("insert into " + stocktablename
207:                            + " values ('00002', 10)");
208:                    stmt.execute("insert into " + stocktablename
209:                            + " values ('00003', 20)");
210:                    stmt.execute("insert into " + stocktablename
211:                            + " values ('00004', 10)");
212:                    stmt.close();
213:                    conn.close();
214:                } catch (SQLException e) {
215:                    System.err.println("Exception in createTable : " + e);
216:                    throw new EJBException("Exception in createTable");
217:                }
218:            }
219:
220:            public void createOrderFile() {
221:                try {
222:                    File f = new File(System.getProperty("java.io.tmpdir")
223:                            + File.separator + System.getProperty("user.name")
224:                            + "_" + orderfilename);
225:                    if (!f.createNewFile()) {
226:                        f.delete();
227:                        f.createNewFile();
228:                    }
229:                } catch (Exception ex) {
230:                    System.err.println("Exception on createOrderFile: " + ex);
231:                    throw new EJBException("Exception in createOrderFile");
232:                }
233:
234:            }
235:
236:            public void removeOrderFile() {
237:                try {
238:                    File f = new File(System.getProperty("java.io.tmpdir")
239:                            + File.separator + System.getProperty("user.name")
240:                            + "_" + orderfilename);
241:                    f.delete();
242:                } catch (Exception ex) {
243:                    System.err.println("Exception on removeOrderFile: " + ex);
244:                    throw new EJBException("Exception in removeOrderFile");
245:                }
246:            }
247:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.