Source Code Cross Referenced for PKCache.java in  » Database-ORM » ProjectJulp » org » julp » 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 » ProjectJulp » org.julp 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        // FIXME CONCURRENT ACCESS TO DRIVER, URL ETC...??
002:        package org.julp;
003:
004:        import java.util.*;
005:        import java.sql.Connection;
006:
007:        ;
008:        /**
009:         * Retrieves and caches PrimaryKeys from DatabaseMetaData or user input
010:         */
011:
012:        public class PKCache implements  java.io.Serializable, Cloneable {
013:
014:            private PKCache() {
015:                if (Boolean.getBoolean("debug-julp")
016:                        || Boolean.getBoolean("debug-" + getClass().getName())) {
017:                    setDebug(true);
018:                }
019:            }
020:
021:            private static PKCache instance = null;
022:            /* Key for each table */
023:            protected Map pkCache = new HashMap();
024:            protected boolean debug = false;
025:            private static final String DOT = ".";
026:
027:            public static PKCache getInstance() {
028:                if (instance == null) {
029:                    instance = new PKCache();
030:                }
031:                return instance;
032:            }
033:
034:            /*
035:             *This method allows to set columns for a table PrimaryKey without Database connection, etc.
036:             *
037:             * Make sure to use format:  catalog + DOT + schema + DOT + table + DOT + column
038:             *   or schema + DOT + table + DOT + column
039:             */
040:            public synchronized void setPrimaryKey(String catalog,
041:                    String schema, String table, List pk) {
042:                String tableId = null;
043:                if (catalog != null && catalog.trim().length() != 0) {
044:                    tableId = catalog + DOT + schema + DOT + table;
045:                } else if (schema != null && schema.trim().length() != 0) {
046:                    tableId = schema + DOT + table;
047:                } else if (table != null && table.trim().length() != 0) {
048:                    tableId = table;
049:                } else {
050:                    throw new IllegalArgumentException(
051:                            "setPrimaryKey(): missing all table identifiers");
052:                }
053:                if (pk == null || pk.size() == 0) {
054:                    throw new IllegalArgumentException(
055:                            "setPrimaryKey(): PK list is empty");
056:                }
057:                if (debug)
058:                    System.out.println("julp ============= "
059:                            + new java.util.Date() + " " + this .getClass()
060:                            + "::" + this  + "::setPrimaryKey()::tableId: "
061:                            + tableId + " pk: " + pk);
062:                pkCache.put(tableId, pk);
063:            }
064:
065:            public synchronized List getPrimaryKey(String catalog,
066:                    String schema, String table) {
067:                if (debug)
068:                    System.out.println("julp ============= "
069:                            + new java.util.Date() + " " + this .getClass()
070:                            + "::" + this  + "::getPrimaryKey()::catalog: "
071:                            + catalog + "::schema: " + schema + "::table: "
072:                            + table);
073:                String tableId = null;
074:                //Build table id
075:                if (catalog != null && catalog.trim().length() != 0) {
076:                    tableId = catalog.trim() + DOT + schema.trim() + DOT
077:                            + table.trim();
078:                } else if (schema != null && schema.trim().length() != 0) {
079:                    tableId = schema.trim() + DOT + table.trim();
080:                } else if (table != null && table.trim().length() != 0) {
081:                    tableId = table.trim();
082:                } else {
083:                    throw new IllegalArgumentException(
084:                            "getPrimaryKey(): All table identifiers are missing");
085:                }
086:                if (this .pkCache.containsKey(tableId)) {
087:                    return (List) this .pkCache.get(tableId); //it's already cached
088:                } else {
089:                    return null;
090:                }
091:            }
092:
093:            public synchronized List getPrimaryKey(Connection connection,
094:                    String catalog, String schema, String table) {
095:                if (debug)
096:                    System.out.println("julp ============= "
097:                            + new java.util.Date() + " " + this .getClass()
098:                            + "::" + this  + "::getPrimaryKey()::catalog: "
099:                            + catalog + "::schema: " + schema + "::table: "
100:                            + table);
101:                String tableId = null;
102:                //Build table id
103:                if (catalog != null && catalog.trim().length() != 0) {
104:                    tableId = catalog.trim() + DOT + schema.trim() + DOT
105:                            + table.trim();
106:                } else if (schema != null && schema.trim().length() != 0) {
107:                    tableId = schema.trim() + DOT + table.trim();
108:                } else if (table != null && table.trim().length() != 0) {
109:                    tableId = table.trim();
110:                } else {
111:                    throw new IllegalArgumentException(
112:                            "getPrimaryKey(): All table identifiers are missing");
113:                }
114:                List pkFields = (List) this .pkCache.get(tableId);
115:                if (debug)
116:                    System.out.println("julp ============= "
117:                            + new java.util.Date() + " " + this .getClass()
118:                            + "::" + this  + "::getPrimaryKey()::tableId: "
119:                            + tableId + " pkFields: " + pkFields);
120:                if (pkFields != null) {
121:                    return pkFields; //it's already cached
122:                } else {
123:                    pkFields = new ArrayList();
124:                }
125:                java.sql.ResultSet pkInfo = null;
126:                try {
127:                    java.sql.DatabaseMetaData dbmd = connection.getMetaData();
128:                    if (debug)
129:                        System.out.println("julp ============= "
130:                                + new java.util.Date() + " " + this .getClass()
131:                                + "::" + this 
132:                                + "::getPrimaryKey()::DatabaseMetaData: "
133:                                + dbmd.getDatabaseProductName() + "::catalog: "
134:                                + catalog + "::schema: " + schema + "::table: "
135:                                + table);
136:                    pkInfo = dbmd.getPrimaryKeys(catalog, schema, table);
137:                    if (debug)
138:                        System.out.println("julp ============= "
139:                                + new java.util.Date() + " " + this .getClass()
140:                                + "::" + this  + "::getPrimaryKey()::pkInfo: "
141:                                + pkInfo);
142:                    while (pkInfo.next()) {
143:                        if (debug)
144:                            System.out.println("julp ============= "
145:                                    + new java.util.Date() + " "
146:                                    + this .getClass() + "::" + this 
147:                                    + "::getPrimaryKey()::pkInfo.next()");
148:                        String columnName = pkInfo.getString(4);
149:                        pkFields.add(tableId + DOT + columnName);
150:                    }
151:                    if (debug)
152:                        System.out.println("julp ============= "
153:                                + new java.util.Date() + " " + this .getClass()
154:                                + "::" + this  + "::getPrimaryKey()::pkFields: "
155:                                + pkFields);
156:                    if (pkFields.isEmpty()) {
157:                        throw new java.sql.SQLException(
158:                                "Cannot retrieve PrimaryKey info from DatabaseMetaData");
159:                    }
160:                } catch (java.sql.SQLException sqle) {
161:                    throw new RuntimeException(sqle);
162:                } catch (Exception e) {
163:                    throw new RuntimeException(e);
164:                } finally {
165:                    try {
166:                        if (pkInfo != null)
167:                            pkInfo.close();
168:                    } catch (java.sql.SQLException sqle) {
169:                        sqle.printStackTrace();
170:                        throw new RuntimeException(sqle);
171:                    }
172:                }
173:                pkCache.put(tableId, pkFields);
174:                return pkFields;
175:            }
176:
177:            public boolean isDebug() {
178:                return debug;
179:            }
180:
181:            public void setDebug(boolean debug) {
182:                this.debug = debug;
183:            }
184:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.