Source Code Cross Referenced for HsqlDBServiceImpl.java in  » J2EE » JOnAS-4.8.6 » org » objectweb » jonas » db » hsqldb » 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 » org.objectweb.jonas.db.hsqldb 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /**
002:         * JOnAS: Java(TM) Open Application Server
003:         * Copyright (C) 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:         * --------------------------------------------------------------------------
022:         * $Id: HsqlDBServiceImpl.java 9372 2006-08-07 09:34:09Z benoitf $
023:         * --------------------------------------------------------------------------
024:         */package org.objectweb.jonas.db.hsqldb;
025:
026:        import java.io.File;
027:        import java.sql.Connection;
028:        import java.sql.DriverManager;
029:        import java.sql.ResultSet;
030:        import java.sql.Statement;
031:        import java.util.Iterator;
032:        import java.util.List;
033:
034:        import org.hsqldb.Server;
035:        import org.hsqldb.ServerConstants;
036:        import org.objectweb.jonas.common.JProp;
037:        import org.objectweb.jonas.db.AbsDBServiceImpl;
038:        import org.objectweb.jonas.db.AbsDBServiceImplMBean;
039:        import org.objectweb.jonas.db.DBService;
040:        import org.objectweb.jonas.db.User;
041:        import org.objectweb.jonas.service.ServiceException;
042:        import org.objectweb.util.monolog.api.BasicLevel;
043:
044:        /**
045:         * Embeds the HSQL database in JOnAS
046:         * @author Florent Benoit
047:         */
048:        public class HsqlDBServiceImpl extends AbsDBServiceImpl implements 
049:                DBService, AbsDBServiceImplMBean {
050:
051:            /**
052:             * List of users
053:             */
054:            private List users = null;
055:
056:            /**
057:             * Name of database
058:             */
059:            private String databaseName = null;
060:
061:            /**
062:             * Default port number
063:             */
064:            private static final String DEFAULT_PORT = "9001";
065:
066:            /**
067:             * Sleep value
068:             */
069:            private static final int SLEEP_VALUE = 100;
070:
071:            /**
072:             * Max retry number
073:             */
074:            private static final int MAX_RETRY_NB = 20;
075:
076:            /**
077:             * port number used
078:             */
079:            private String portNumber = null;
080:
081:            /**
082:             * HsqlDB server
083:             */
084:            private Server server = null;
085:
086:            /**
087:             * Create a database with the specified arguments.
088:             * @param users user/password (separated by a ":")
089:             * @param databaseName name of the database
090:             * @param portNumber port number of the database
091:             */
092:
093:            protected void initServer(List users, String databaseName,
094:                    String portNumber) {
095:                this .users = users;
096:                if (portNumber != null) {
097:                    this .portNumber = portNumber;
098:                } else {
099:                    this .portNumber = DEFAULT_PORT;
100:                }
101:                this .databaseName = databaseName;
102:                String jBase = JProp.getJonasBase();
103:
104:                server = new Server();
105:                // Remove all traces if level != DEBUG
106:                if (!getLogger().isLoggable(BasicLevel.DEBUG)) {
107:                    server.setLogWriter(null);
108:                    server.setErrWriter(null);
109:                    server.setSilent(true);
110:                    server.setTrace(false);
111:                    server.setLogWriter(null);
112:                } else {
113:                    // Enable all traces : verbose mode (as user needs DEBUG)
114:                    server.setSilent(false);
115:                    server.setTrace(true);
116:                }
117:
118:                String baseDir = jBase + File.separator + "work"
119:                        + File.separator + "hsqldb" + File.separator
120:                        + databaseName;
121:                String pString = "";
122:                if (portNumber != null) {
123:                    pString = ";port=" + portNumber;
124:                }
125:                String serverProps = "database.0=" + baseDir + ";dbname.0="
126:                        + databaseName + pString;
127:                server.putPropertiesFromString(serverProps);
128:
129:            }
130:
131:            /**
132:             * Start the service.
133:             * @throws ServiceException if the startup failed.
134:             */
135:            protected void doStart() throws ServiceException {
136:                super .doStart();
137:                if (getLogger().isLoggable(BasicLevel.INFO)) {
138:                    getLogger().log(
139:                            BasicLevel.INFO,
140:                            "Starting " + server.getProductName() + " "
141:                                    + server.getProductVersion() + " on port "
142:                                    + portNumber);
143:                }
144:                if (getLogger().isLoggable(BasicLevel.DEBUG)) {
145:                    getLogger().log(BasicLevel.DEBUG,
146:                            "serverState=" + server.getState());
147:                }
148:                server.start();
149:
150:                // Wait the start
151:                int retryNb = 0;
152:                while (server.getState() != ServerConstants.SERVER_STATE_ONLINE) {
153:                    try {
154:                        Thread.sleep(SLEEP_VALUE);
155:                    } catch (InterruptedException ie) {
156:                        getLogger().log(BasicLevel.ERROR,
157:                                "Can't wait that the service is online", ie);
158:                    }
159:                    // Error if server state is "SHUTDOWN" during a long period
160:                    // Maybe strange but 'SHUTDOWN' state seems to be an intermediate state during startup
161:                    retryNb++;
162:                    if (server.getState() == ServerConstants.SERVER_STATE_SHUTDOWN
163:                            && retryNb >= MAX_RETRY_NB) {
164:                        Throwable t = server.getServerError();
165:                        throw new ServiceException("The server was shutdown :",
166:                                t);
167:                    }
168:                    if (getLogger().isLoggable(BasicLevel.DEBUG)) {
169:                        getLogger().log(
170:                                BasicLevel.DEBUG,
171:                                "retry=" + retryNb + ", serverState="
172:                                        + server.getState());
173:                    }
174:                }
175:
176:                getLogger().log(BasicLevel.INFO,
177:                        server.getProductName() + " started.");
178:                Connection conn = null;
179:                Statement st = null;
180:                try {
181:
182:                    Class.forName("org.hsqldb.jdbcDriver");
183:                    conn = DriverManager.getConnection(
184:                            "jdbc:hsqldb:hsql://localhost:" + portNumber + "/"
185:                                    + databaseName, "sa", "");
186:                    st = conn.createStatement();
187:                } catch (Exception e) {
188:                    throw new ServiceException("Cannot access to HSQL", e);
189:                }
190:
191:                // Drop users before recreating it
192:                User user = null;
193:                String userName = null;
194:                String password = null;
195:                ResultSet rs = null;
196:                for (Iterator it = users.iterator(); it.hasNext();) {
197:                    user = (User) it.next();
198:                    try {
199:                        password = user.getPassword();
200:                        userName = user.getUserName();
201:                        getLogger()
202:                                .log(
203:                                        BasicLevel.INFO,
204:                                        "Dropping and adding user '" + userName
205:                                                + "' with password '"
206:                                                + password + "'.");
207:                        try {
208:                            rs = st.executeQuery("DROP USER " + userName);
209:                        } catch (Exception ee) {
210:                            if (getLogger().isLoggable(BasicLevel.DEBUG)) {
211:                                getLogger().log(
212:                                        BasicLevel.DEBUG,
213:                                        "User '" + userName
214:                                                + "' doesn't exists", ee);
215:                            }
216:                        }
217:                        rs = st.executeQuery("Create USER " + userName
218:                                + " PASSWORD " + password + " ADMIN");
219:                        rs.close();
220:                    } catch (Exception e) {
221:                        getLogger().log(BasicLevel.ERROR,
222:                                "Error while creating/adding user", e);
223:                    }
224:
225:                }
226:
227:                try {
228:                    st.close();
229:                } catch (Exception e) {
230:                    if (getLogger().isLoggable(BasicLevel.DEBUG)) {
231:                        getLogger().log(BasicLevel.DEBUG,
232:                                "Error while closing statement object", e);
233:                    }
234:                }
235:
236:            }
237:
238:            /**
239:             * Stop the service.
240:             * @throws ServiceException if the stop failed.
241:             */
242:            protected void doStop() throws ServiceException {
243:                super.doStop();
244:
245:                server.shutdown();
246:            }
247:
248:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.