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 com.google.soap.search.*;
013:
014: import java.util.*;
015: import java.io.*;
016:
017: /**
018: *
019: * Search engine interface implementation using Google search API
020: *
021: */
022: public class GoogleDb implements RDMDb {
023:
024: /** Google search engine API authorization key */
025: private String clientKey;
026:
027: /** http proxy host for Google search SOAP connection */
028: private String proxyHost = "";
029:
030: /** http proxy port for Google search SOAP connection */
031: private String proxyPort = "";
032:
033: /**
034: * Open a database
035: * @param st Token for search
036: * @param rootdir Database home dir
037: * @param dbname Name of database from root database
038: * @param rw RDMDb.WRITER or RDMDb.WRCREAT or RDMDb.READER
039: * @param mode Unix mode
040: * @throws RDMException
041: */
042: public void open(SToken st, String rootdir, String dbname, int rw,
043: int mode) throws RDMException {
044: SOIF dbsoif = DbManager.getRootDbEntry(st, dbname);
045: clientKey = dbsoif.getValue("clientKey");
046: proxyHost = dbsoif.getValue("proxyHost");
047: proxyPort = dbsoif.getValue("proxyPort");
048: }
049:
050: /**
051: * Close database and index extents
052: * @param st Token for search
053: * @throws RDMException
054: */
055: public void close(SToken st) throws RDMException {
056: ;//do nothing
057: }
058:
059: /**
060: * Empty the database
061: * @param st Token for search
062: * @param t RDM transaction handle
063: * @throws RDMException
064: */
065: public int purge(SToken st, RDMTransaction t) throws RDMException {
066: throw new RDMException("GoogleDb.purge() not implemented");
067: }
068:
069: /**
070: * Obtain the name for the search engine interface
071: * @return the name of the search engine activity
072: */
073: public String getName() {
074: return "Google Search";
075: }
076:
077: /**
078: * Submit the query to the search engine
079: * @param st Token for search
080: * @param query Search query
081: * @param numHits Number of hits
082: * @param view Set of attributes to be retrieved
083: * @param sortOrder Order for sorting the results
084: * @param t RDM transaction handle
085: * @return RDMResultSet
086: * @throws RDMException
087: */
088: public RDMResultSet search(SToken st, String query, int numHits,
089: Set view, String sortOrder, RDMTransaction t)
090: throws RDMException {
091:
092: //Google search result set
093: GoogleSearchResult gsr;
094:
095: try {
096: gsr = googleSearch(clientKey, "search", query);
097: } catch (GoogleSearchFault f) {
098: throw new RDMException(f);
099: }
100:
101: GoogleResultSet rs = new GoogleResultSet(st, this , query, t);
102: rs.setGoogleSearchResult(gsr);
103: rs.setView(view);
104: return rs;
105: }
106:
107: /**
108: * Call the Google APIs service to search Google
109: * @param clientKey Google API authorization key
110: * @param directive Search method
111: * @param directiveArg Search query
112: * @return GoogleSearchResult
113: * @throws GoogleSearchFault
114: */
115: GoogleSearchResult googleSearch(String clientKey, String directive,
116: String directiveArg) throws GoogleSearchFault {
117:
118: // Create a Google Search object, set the authorization key
119: GoogleSearch s = new GoogleSearch();
120: s.setKey(clientKey);
121:
122: if (proxyHost != null && !proxyHost.equals("")
123: && proxyHost.indexOf(" ") == -1) {
124: s.setProxyHost(proxyHost);
125:
126: if (proxyPort != null && !proxyPort.equals("")
127: && proxyPort.indexOf(" ") == -1) {
128: s.setProxyPort(Integer.parseInt(proxyPort));
129: }
130: }
131:
132: if (directive.equalsIgnoreCase("search")) {
133: s.setQueryString(directiveArg);
134: GoogleSearchResult gsr = s.doSearch();
135: return gsr;
136: } else {
137: throw new GoogleSearchFault("Invalid directive");
138: }
139: }
140:
141: /**
142: * Number of documents in the database/index
143: * @param st Token for search
144: * @param t RDM transaction handle
145: * @return search count
146: * @throws RDMException
147: */
148: public int count(SToken st, RDMTransaction t) throws RDMException {
149: return 2000000000;
150: }
151:
152: /**
153: * Delete RD from database
154: * @param st Token for search
155: * @param s SOIF document
156: * @param view Set of attributes
157: * @param flags Options
158: * @param t RDM transaction handle
159: * @throws RDMException
160: */
161: public void delete(SToken st, SOIF s, Set view, int flags,
162: RDMTransaction t) throws RDMException {
163: throw new RDMException("GoogleDb.delete() not implemented");
164: }
165:
166: /**
167: * Store - creates a transaction for atomic indexing, if none supplied.
168: * @param st Token for search
169: * @param soif SOIF document
170: * @param view Set of Attributes
171: * @param flags Options
172: * @param t RDM transaction
173: * @throws RDMException
174: */
175: public void store(SToken st, SOIF soif, Set view, int flags,
176: RDMTransaction t) throws RDMException {
177: throw new RDMException("GoogleDb.store() not implemented");
178: }
179:
180: /**
181: * Update an RD with attributes set in View
182: * @param st Token for search
183: * @param insoif SOIF document
184: * @param view Set of Attributes
185: * @param flags Options
186: * @param t RDM transaction
187: * @throws RDMException
188: */
189: public void update(SToken st, SOIF insoif, Set view, int flags,
190: RDMTransaction t) throws RDMException {
191: throw new RDMException("GoogleDb.update() not implemented");
192: }
193:
194: /**
195: * Retrieve RD from database, filtered by view
196: * @param st Token for search
197: * @param url URL string
198: * @param view Set of Attributes
199: * @param flags Options
200: * @param t RDM transaction
201: * @throws RDMException
202: */
203: public SOIF fetch(SToken st, String url, Set view, int flags,
204: RDMTransaction t) throws RDMException {
205: throw new RDMException("GoogleDb.fetch() not implemented");
206: }
207:
208: /**
209: * Optimize the db
210: * @param st Token for search
211: * @throws RDMException
212: */
213: public void optimize(SToken st) throws RDMException {
214: throw new RDMException("GoogleDb.optimize() not implemented");
215: }
216:
217: /**
218: * Batch Index
219: * @param st Token for search
220: * @throws RDMException
221: */
222: public void indexBatch(SToken st) throws RDMException {
223: throw new RDMException("GoogleDb.indexBatch() not implemented");
224: }
225:
226: /**
227: * Recover the db - must be run stand alone (ie, no one else has the db open)
228: * @param st Token for search
229: * @param dbhome Database home dir
230: * @param fatal Option
231: * @throws RDMException
232: */
233: public void recover(SToken st, String dbhome, boolean fatal)
234: throws RDMException {
235: throw new RDMException("GoogleDb.recover() not implemented");
236: }
237:
238: }
|