Source Code Cross Referenced for JDBCDb.java in  » Portal » Open-Portal » com » sun » portal » search » db » 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 » Portal » Open Portal » com.sun.portal.search.db 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
003:         * PROPRIETARY/CONFIDENTIAL.  Use of this product is subject to license terms.
004:         */
005:
006:        package com.sun.portal.search.db;
007:
008:        import com.sun.portal.search.rdm.*;
009:        import com.sun.portal.search.soif.*;
010:        import com.sun.portal.search.util.*;
011:
012:        import javax.naming.*;
013:
014:        import java.util.*;
015:        import java.util.regex.*;
016:        import java.io.*;
017:        import java.text.MessageFormat;
018:
019:        import java.sql.*;
020:        import javax.sql.*;
021:
022:        /**
023:         *
024:         * Search engine interface implementation using JDBC to search a relational db
025:         *
026:         */
027:        public class JDBCDb implements  RDMDb {
028:
029:            java.sql.Connection conn = null;
030:            java.sql.Statement stmt = null;
031:            String database;
032:            String queryPattern;
033:
034:            /**
035:             * Store - creates a transaction for atomic indexing, if none supplied.
036:             * @param st Token for search
037:             * @param insoif SOIF document
038:             * @param view Set of Attributes
039:             * @param flags Options
040:             * @param t RDM transaction
041:             * @throws RDMException
042:             */
043:            public void store(SToken st, SOIF insoif, Set view, int flags,
044:                    RDMTransaction t) throws RDMException {
045:                throw new RDMException("JDBCDb.store(): not implemented");
046:            }
047:
048:            /**
049:             * Purge the database
050:             * @param st Token for search
051:             * @param t RDM transaction handle
052:             * @throws RDMException
053:             */
054:            public int purge(SToken st, RDMTransaction t) throws RDMException {
055:                throw new RDMException("JDBCDb.purge(): not implemented");
056:            }
057:
058:            /**
059:             * Open a database
060:             * @param st Token for search
061:             * @param rootdir Database home dir
062:             * @param dbname Name of database from root database
063:             * @param rw RDMDb.WRITER or RDMDb.WRCREAT or RDMDb.READER
064:             * @param mode Unix mode
065:             * @throws RDMException
066:             */
067:            public void open(SToken st, String rootdir, String dbname, int rw,
068:                    int mode) throws RDMException {
069:
070:                database = dbname;
071:
072:                SOIF dbsoif = DbManager.getRootDbEntry(st, dbname);
073:
074:                String driver = dbsoif.getValue("driver");
075:                String dburl = dbsoif.getValue("databaseurl");
076:                String dbusr = dbsoif.getValue("username");
077:                String dbpwd = dbsoif.getValue("password");
078:                queryPattern = dbsoif.getValue("querypattern");
079:
080:                try {
081:                    //Load JDBC Driver
082:                    Class.forName(driver).newInstance();
083:                    //Getting connection...
084:                    conn = DriverManager.getConnection(dburl, dbusr, dbpwd);
085:                } catch (Exception e) {
086:                    throw new RDMException(
087:                            "JDBCDb.open(): Failed to open jdbc connection", e);
088:                }
089:            }
090:
091:            /**
092:             * Close database and index extents
093:             * @param st Token for search
094:             * @throws RDMException
095:             */
096:            public void close(SToken st) throws RDMException {
097:                try {
098:                    conn.close();
099:                } catch (Exception e) {
100:                    throw new RDMException(
101:                            "JDBCDb.close(): Error during close()", e);
102:                }
103:            }
104:
105:            /**
106:             * Obtain the name for the search engine interface
107:             */
108:            public String getName() {
109:                return "JDBC Search";
110:            }
111:
112:            /**
113:             * Submit the query to the search engine
114:             * @param st Token for search
115:             * @param queryString Search query
116:             * @param numHits Number of hits
117:             * @param view Set of attributes to be retrieved
118:             * @param sortOrder Order for sorting the results
119:             * @param t RDM transaction handle
120:             * @return RDMResultSet
121:             * @throws RDMException
122:             */
123:            public RDMResultSet search(SToken st, String queryString,
124:                    int numHits, Set view, String sortOrder, RDMTransaction t)
125:                    throws RDMException {
126:
127:                ResultSet srs;
128:                DatabaseMetaData dmd;
129:                String query;
130:
131:                if (isSQLQuery(queryString)) {
132:                    query = queryString;
133:                } else {
134:                    Object[] arguments = { queryString };
135:                    query = getSQLQuery(arguments, queryPattern);
136:                }
137:
138:                try {
139:                    srs = jdbcSearch(query, numHits, view);
140:                    dmd = conn.getMetaData();
141:                } catch (Exception e) {
142:                    throw new RDMException(e);
143:                }
144:
145:                JDBCResultSet js = new JDBCResultSet(st, this , query, t);
146:                js.setJDBCSearchResult(srs, dmd);
147:                return js;
148:            }
149:
150:            /**
151:             * Search the relational db
152:             * @param query Search filter
153:             * @param numHits Number of search hits
154:             * @param view Set of Attributes
155:             * @return ResultSet
156:             * @throws RDMException
157:             */
158:            ResultSet jdbcSearch(String query, int numHits, Set view)
159:                    throws Exception {
160:                ResultSet rs;
161:
162:                try {
163:                    // Getting statement...
164:                    stmt = conn.createStatement();
165:                    // Executing query...
166:                    rs = stmt.executeQuery(query);
167:                } catch (Exception e) {
168:                    throw new RDMException(e);
169:                }
170:
171:                return rs;
172:            }
173:
174:            /**
175:             * Transform the query format to SQL based on the argument pattern
176:             * @param arguments Query arguments
177:             * @param patterm Query pattern in SQL format
178:             * @return a query string that is in the SQL format defined by "pattern"
179:             */
180:            private String getSQLQuery(Object[] arguments, String pattern) {
181:
182:                boolean singlequote = false;
183:                MessageFormat queryFormat;
184:                String query;
185:
186:                if (pattern.indexOf("'") != -1) {
187:                    singlequote = true;
188:                }
189:
190:                if (singlequote) {
191:                    queryFormat = new MessageFormat(pattern.replace('\'', '^'));
192:                    query = (queryFormat.format(arguments)).replace('^', '\'');
193:                } else {
194:                    queryFormat = new MessageFormat(pattern);
195:                    query = queryFormat.format(arguments);
196:                }
197:
198:                return query;
199:
200:            }
201:
202:            /**
203:             * Check if the query is a SQL statement
204:             * @param query Search query
205:             * @return true or false
206:             */
207:            private boolean isSQLQuery(String qString) {
208:
209:                String qs = qString.toLowerCase();
210:
211:                if (qs.indexOf("select") != -1
212:                        && qs.indexOf("select") < qs.indexOf("from"))
213:                    return true;
214:                else
215:                    return false;
216:            }
217:
218:            /**
219:             * Get search count
220:             * @param st Token for search
221:             * @param t RDM transaction handle
222:             * @return Number of docs.
223:             */
224:            public int count(SToken st, RDMTransaction t) throws RDMException {
225:                return 250000000;
226:            }
227:
228:            /**
229:             * Delete RD from database
230:             * @param st Token for search
231:             * @param s SOIF document
232:             * @param view Set of attributes
233:             * @param flags Options
234:             * @param t RDM transaction handle
235:             * @throws RDMException
236:             */
237:            public void delete(SToken st, SOIF s, Set view, int flags,
238:                    RDMTransaction t) throws RDMException {
239:                throw new RDMException("JDBCDb.delete() not implemented");
240:            }
241:
242:            /**
243:             * Update an RD with attributes set in View
244:             * @param st Token for search
245:             * @param insoif SOIF document
246:             * @param view Set of Attributes
247:             * @param flags Options
248:             * @param t RDM transaction
249:             * @throws RDMException
250:             */
251:            public void update(SToken st, SOIF insoif, Set view, int flags,
252:                    RDMTransaction t) throws RDMException {
253:                throw new RDMException("JDBCDb.update() not implemented");
254:            }
255:
256:            /**
257:             * Retrieve RD from database, filtered by view
258:             * @param st Token for search
259:             * @param url URL string
260:             * @param view Set of Attributes
261:             * @param flags Options
262:             * @param t RDM transaction
263:             * @throws RDMException
264:             */
265:            public SOIF fetch(SToken st, String url, Set view, int flags,
266:                    RDMTransaction t) throws RDMException {
267:                throw new RDMException("JDBCDb.fetch() not implemented");
268:            }
269:
270:            /**
271:             * Optimize the db
272:             * @param st Token for search
273:             * @throws RDMException
274:             */
275:            public void optimize(SToken st) throws RDMException {
276:                throw new RDMException("JDBCDb.optimize() not implemented");
277:            }
278:
279:            /**
280:             * Batch Index
281:             * @param st Token for search
282:             * @throws RDMException
283:             */
284:            public void indexBatch(SToken st) throws RDMException {
285:                throw new RDMException("JDBCDb.indexBatch() not implemented");
286:            }
287:
288:            /**
289:             * Recover the db - must be run stand alone (ie, no one else has the db open)
290:             * @param st Token for search
291:             * @param dbhome Database home dir
292:             * @param fatal Option
293:             * @throws RDMException
294:             */
295:            public void recover(SToken st, String dbhome, boolean fatal)
296:                    throws RDMException {
297:                throw new RDMException("JDBCDb.recover() not implemented");
298:            }
299:
300:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.