Source Code Cross Referenced for Naming.java in  » Database-ORM » MMBase » org » mmbase » module » database » 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 » Database ORM » MMBase » org.mmbase.module.database 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:
003:        This software is OSI Certified Open Source Software.
004:        OSI Certified is a certification mark of the Open Source Initiative.
005:
006:        The license (Mozilla version 1.0) can be read at the MMBase site.
007:        See http://www.MMBase.org/license
008:
009:         */
010:        package org.mmbase.module.database;
011:
012:        import java.sql.Connection;
013:        import java.sql.SQLException;
014:        import javax.sql.DataSource;
015:        import javax.sql.PooledConnection;
016:        import javax.sql.ConnectionPoolDataSource;
017:        import javax.naming.InitialContext;
018:        import javax.naming.Context;
019:
020:        import org.mmbase.module.*;
021:        import org.mmbase.util.logging.*;
022:
023:        /**
024:         * This class is used to retrieve a connection, which is provided by naming resources.
025:         * With the usage of naming resource, it is possible to configure the database resource
026:         * inside the application server and let the application server do the pooling. Since
027:         * this is a J2EE concept, this class provides support for usage of this.
028:         *
029:         * @author Eduard Witteveen
030:         * @version $Id: Naming.java,v 1.6 2007/06/19 13:59:30 michiel Exp $
031:         * @deprecated Datasource can be configured in mmbaseroot.xml
032:         */
033:        public class Naming extends ProcessorModule implements  JDBCInterface {
034:            private static Logger log = Logging.getLoggerInstance(Naming.class);
035:            private static String PROPERTY_CONTEXT_NAME = "context";
036:            private static String PROPERTY_DATASOURCE_NAME = "datasource";
037:            private Object datasource = null;
038:
039:            /** our own multi-connection implementation.. arrgg....*/
040:            private class NamingMultiConnection extends
041:                    MultiConnectionImplementation {
042:                /** constructor to set the connection which has to be retrieved */
043:                NamingMultiConnection(Connection con) {
044:                    super (null, con);
045:                    // this should take care of everything (we hope)
046:                    state = CON_BUSY;
047:                }
048:
049:                /** override the close, since the MultiConnection want to tell the pool that it is closed */
050:                public void close() throws SQLException {
051:                    con.close();
052:                }
053:
054:                /** claim what? */
055:                public void claim() {
056:                }
057:
058:                /** release what? */
059:                public void release() {
060:                }
061:
062:                /** the usage of what? */
063:                public int getUsage() {
064:                    return 0;
065:                }
066:
067:                /** the start time of what? */
068:                public int getStartTime() {
069:                    return 0;
070:                }
071:
072:                /** the start time of what? */
073:                public long getStartTimeMillis() {
074:                    return 0;
075:                }
076:            }
077:
078:            public Naming(String name) {
079:                super (name);
080:            }
081:
082:            /**
083:             * Init this module. Will check if properties are available and try to get a datasource, to
084:             * test if we can use it.
085:             */
086:            public void init() {
087:                String context = getInitParameter(PROPERTY_CONTEXT_NAME);
088:                if (context == null)
089:                    throw new RuntimeException("the property '"
090:                            + PROPERTY_CONTEXT_NAME + "' was not set");
091:                String source = getInitParameter(PROPERTY_DATASOURCE_NAME);
092:                if (source == null)
093:                    throw new RuntimeException("the property '"
094:                            + PROPERTY_CONTEXT_NAME + "' was not set");
095:
096:                // do the naming stuff..
097:                try {
098:                    Context initCtx = new InitialContext();
099:                    Context envCtx = (Context) initCtx.lookup(context);
100:                    datasource = envCtx.lookup(source);
101:                    if (datasource == null) {
102:                        String msg = "datasource was null for context:"
103:                                + context + " with source:" + source;
104:                        log.error(msg);
105:                        throw new RuntimeException(msg);
106:                    }
107:                    if (datasource instanceof  ConnectionPoolDataSource) {
108:                        ConnectionPoolDataSource ds = (ConnectionPoolDataSource) datasource;
109:                        log.info("Using the interface:"
110:                                + ConnectionPoolDataSource.class.getName()
111:                                + "(implemented by:" + ds.getClass().getName()
112:                                + " to get new database connections(time out: "
113:                                + ds.getLoginTimeout() + " seconds).");
114:                    } else if (datasource instanceof  DataSource) {
115:                        log.info("Using the interface:"
116:                                + DataSource.class.getName()
117:                                + "(implemented by:"
118:                                + datasource.getClass().getName()
119:                                + " to get new database connections.");
120:                    } else {
121:                        String msg = "Dont know how to retrieve a connection from datasource:"
122:                                + datasource.getClass().getName();
123:                        log.error(msg);
124:                        throw new RuntimeException(msg);
125:                    }
126:
127:                    // try to get an connection, so we can see if it all works..
128:                    Connection con = getConnection();
129:                    if (con == null) {
130:                        String msg = "Test run of retrieving a test-run failed.";
131:                        log.error(msg);
132:                        throw new RuntimeException(msg);
133:                    }
134:                    // closing a connection is very important!
135:                    con.close();
136:
137:                } catch (javax.naming.NamingException ne) {
138:                    String msg = "The following error occured while trying to initalise the datasource for context:'"
139:                            + context
140:                            + "' datasource:'"
141:                            + source
142:                            + "' :\n"
143:                            + Logging.stackTrace(ne);
144:                    log.error(msg);
145:                    throw new RuntimeException(msg);
146:                } catch (java.sql.SQLException se) {
147:                    String msg = "The following error occured while trying to retrieve a connection from the datasource for context:'"
148:                            + context
149:                            + "' datasource:'"
150:                            + source
151:                            + "' :\n"
152:                            + Logging.stackTrace(se);
153:                    log.error(msg);
154:                    throw new RuntimeException(msg);
155:                }
156:            }
157:
158:            /**
159:             * is a reload the same as an init?
160:             */
161:            public void reload() {
162:                init();
163:            }
164:
165:            /**
166:             * retrieves an connection to the database, depending on the class which is used as datasource
167:             * @return Connection A connection to the database
168:             */
169:            private Connection getConnection() throws java.sql.SQLException {
170:                if (datasource == null) {
171:                    log
172:                            .error("Getting connection before init of jdbc module. Trying to reinitalize the database layer.");
173:                    init();
174:                }
175:
176:                if (datasource instanceof  ConnectionPoolDataSource) {
177:                    ConnectionPoolDataSource ds = (ConnectionPoolDataSource) datasource;
178:                    PooledConnection pc = ds.getPooledConnection();
179:                    return pc.getConnection();
180:                } else if (datasource instanceof  DataSource) {
181:                    DataSource ds = (DataSource) datasource;
182:                    return ds.getConnection();
183:                } else {
184:                    String msg = "Dont know how to retrieve a connection from datasource:"
185:                            + (datasource != null ? datasource : datasource
186:                                    .getClass().getName());
187:                    log.error(msg);
188:                    throw new RuntimeException(msg);
189:                }
190:            }
191:
192:            public MultiConnection getConnection(String url, String name,
193:                    String password) throws SQLException {
194:                return new NamingMultiConnection(getConnection());
195:            }
196:
197:            public MultiConnection getConnection(String url)
198:                    throws SQLException {
199:                return new NamingMultiConnection(getConnection());
200:            }
201:
202:            public Connection getDirectConnection(String url)
203:                    throws SQLException {
204:                return getConnection();
205:            }
206:
207:            public Connection getDirectConnection(String url, String name,
208:                    String password) throws SQLException {
209:                return getConnection();
210:            }
211:
212:            // below all the things we dont use..
213:            public void unload() {
214:            }
215:
216:            public void shutdown() {
217:            }
218:
219:            public String makeUrl() {
220:                return null;
221:            }
222:
223:            public String makeUrl(String dbm) {
224:                return null;
225:            }
226:
227:            public String makeUrl(String host, String dbm) {
228:                return null;
229:            }
230:
231:            public String makeUrl(String host, int port, String dbm) {
232:                return null;
233:            }
234:
235:            public String getUser() {
236:                return null;
237:            }
238:
239:            public String getPassword() {
240:                return null;
241:            }
242:
243:            public String getDatabaseName() {
244:                return null;
245:            }
246:
247:            public void checkTime() {
248:            }
249:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.