Source Code Cross Referenced for ConnectionHelper.java in  » J2EE » Jaffa » helpers » 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 » Jaffa » helpers 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        package helpers;
002:
003:        import java.net.URL;
004:        import java.sql.Connection;
005:        import java.sql.DriverManager;
006:        import java.sql.SQLException;
007:        import java.util.HashMap;
008:        import java.util.Iterator;
009:        import java.util.Map;
010:        import javax.xml.bind.JAXBContext;
011:        import javax.xml.bind.Unmarshaller;
012:        import org.jaffa.config.Config;
013:        import org.jaffa.persistence.engines.jdbcengine.configservice.initdomain.Database;
014:        import org.jaffa.persistence.engines.jdbcengine.configservice.initdomain.Init;
015:        import org.jaffa.persistence.engines.jdbcengine.configservice.initdomain.Param;
016:        import org.jaffa.util.URLHelper;
017:        import org.jaffa.util.XmlHelper;
018:
019:        /*
020:         * ConnectionHelper.java
021:         *
022:         * Created on March 1, 2004, 11:55 AM
023:         */
024:
025:        /**
026:         *
027:         * @author  PaulE
028:         */
029:        public class ConnectionHelper {
030:
031:            static final String ENGINE = "engine";
032:            static final String URL = "url";
033:            static final String DRIVER_CLASS = "driverClass";
034:            static final String USER = "user";
035:            static final String PASSWORD = "password";
036:            static final String MAXIMUM_CONNECTIONS = "maximumConnections";
037:
038:            public static Connection getConnection() throws Exception {
039:                Map info = ConnectionHelper.getDatabaseInfo();
040:                return ConnectionHelper.getConnection((String) info
041:                        .get(DRIVER_CLASS), (String) info.get(URL),
042:                        (String) info.get(USER), (String) info.get(PASSWORD));
043:            }
044:
045:            private static Map getDatabaseInfo() throws Exception {
046:                URL initUrl = URLHelper.newExtendedURL((String) Config
047:                        .getProperty(Config.PROP_JDBC_ENGINE_INIT));
048:
049:                // create a JAXBContext capable of handling classes generated into the package
050:                JAXBContext jc = JAXBContext
051:                        .newInstance("org.jaffa.persistence.engines.jdbcengine.configservice.initdomain");
052:
053:                // create an Unmarshaller
054:                Unmarshaller u = jc.createUnmarshaller();
055:
056:                // enable validation
057:                u.setValidating(true);
058:
059:                // unmarshal a document into a tree of Java content objects composed of classes from the package.
060:                Init init = (Init) u.unmarshal(XmlHelper
061:                        .stripDoctypeDeclaration(initUrl));
062:                Database database = null;
063:                for (Iterator i = init.getDatabase().iterator(); i.hasNext();) {
064:                    database = (Database) i.next();
065:                    if (database.getName().equals("default"))
066:                        break;
067:                }
068:
069:                if (database == null)
070:                    throw new Exception(
071:                            "The 'default' database has not been defined in init.xml ");
072:
073:                Map info = new HashMap();
074:                info.put(ENGINE, database.getEngine());
075:                for (Iterator i = database.getConnectionFactory().getParam()
076:                        .iterator(); i.hasNext();) {
077:                    Param param = (Param) i.next();
078:                    if (URL.equals(param.getName()))
079:                        info.put(URL, param.getValue());
080:                    else if (DRIVER_CLASS.equals(param.getName()))
081:                        info.put(DRIVER_CLASS, param.getValue());
082:                    else if (USER.equals(param.getName()))
083:                        info.put(USER, param.getValue());
084:                    else if (PASSWORD.equals(param.getName()))
085:                        info.put(PASSWORD, param.getValue());
086:                    else if (MAXIMUM_CONNECTIONS.equals(param.getName()))
087:                        info.put(MAXIMUM_CONNECTIONS, param.getValue());
088:                }
089:                return info;
090:            }
091:
092:            private static Connection getConnection(String driverClass,
093:                    String url, String user, String password)
094:                    throws ClassNotFoundException, SQLException {
095:                Class.forName(driverClass);
096:                Connection connection = DriverManager.getConnection(url, user,
097:                        password);
098:                connection.setAutoCommit(false);
099:                return connection;
100:            }
101:
102:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.