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: import javax.naming.directory.*;
014:
015: import java.util.*;
016: import java.io.*;
017: import java.text.MessageFormat;
018:
019: /**
020: *
021: * Search engine interface implementation using JNDI to search a Directory db
022: *
023: */
024: public class JNDIDb implements RDMDb {
025:
026: Hashtable env = new Hashtable(11);
027: String queryPattern;
028:
029: /**
030: * Store - creates a transaction for atomic indexing, if none supplied.
031: * @param st Token for search
032: * @param insoif SOIF document
033: * @param view Set of Attributes
034: * @param flags Options
035: * @param t RDM transaction
036: * @throws RDMException
037: */
038: public void store(SToken st, SOIF insoif, Set view, int flags,
039: RDMTransaction t) throws RDMException {
040: throw new RDMException("JNDIDb.store(): not implemented");
041: }
042:
043: /**
044: * Empty the database
045: * @param st Token for search
046: * @param t RDM transaction handle
047: * @throws RDMException
048: */
049: public int purge(SToken st, RDMTransaction t) throws RDMException {
050: throw new RDMException("JNDIDb.purge(): not implemented");
051: }
052:
053: /**
054: * Open a database
055: * @param st Token for search
056: * @param rootdir Database home dir
057: * @param dbname Name of database from root database
058: * @param rw RDMDb.WRITER or RDMDb.WRCREAT or RDMDb.READER
059: * @param mode Unix mode
060: * @throws RDMException
061: */
062: public void open(SToken st, String rootdir, String dbname, int rw,
063: int mode) throws RDMException {
064:
065: SOIF dbsoif = DbManager.getRootDbEntry(st, dbname);
066:
067: String contextfactory = dbsoif.getValue("contextfactory");
068: String providerurl = dbsoif.getValue("providerurl");
069: String basedn = dbsoif.getValue("basedn");
070: String binddn = dbsoif.getValue("binddn");
071: String passwd = dbsoif.getValue("passwd");
072: queryPattern = dbsoif.getValue("queryPattern");
073:
074: env.put(Context.INITIAL_CONTEXT_FACTORY, contextfactory);
075: env.put(Context.PROVIDER_URL, providerurl + "/" + basedn);
076:
077: if ((binddn != null) && (binddn != "")
078: && (!binddn.equalsIgnoreCase("anonymous"))) {
079: env.put(Context.SECURITY_PRINCIPAL, binddn);
080: env.put(Context.SECURITY_CREDENTIALS, passwd);
081: }
082:
083: /*
084: * we don't do this here since the connection will time out,
085: * for now we will reconnect with each request
086: */
087: /*
088: try {
089: // Create the initial directory context
090: dctx = new InitialDirContext(env);
091: } catch (Exception e) {
092: throw new RDMException("JNDIDb.open(): Failed to open jndi context", e);
093: }
094: */
095:
096: }
097:
098: /**
099: * Close database and index extents
100: * @param st Token for search
101: * @throws RDMException
102: */
103: public void close(SToken st) throws RDMException {
104: /*
105: try {
106: dctx.close();
107: } catch (Exception ne) {
108: throw new RDMException("JNDIDb.close(): Error during close()", ne);
109: }
110: */
111: }
112:
113: /**
114: * Obtain the name for the search engine interface
115: * @return the name of the search engine activity
116: */
117: public String getName() {
118: return "JNDI Search";
119: }
120:
121: /**
122: * Submit the query to the search engine
123: * @param st Token for search
124: * @param queryString Search query
125: * @param numHits Number of hits
126: * @param view Set of attributes to be retrieved
127: * @param sortOrder Order for sorting the results
128: * @param t RDM transaction handle
129: * @return RDMResultSet
130: * @throws RDMException
131: */
132: public RDMResultSet search(SToken st, String queryString,
133: int numHits, Set view, String sortOrder, RDMTransaction t)
134: throws RDMException {
135:
136: numHits = Math.min(100, numHits);
137: NamingEnumeration jsr;
138: DirContext ctx = null;
139: String query = null;
140:
141: if (queryString.indexOf("=") != -1) {
142: query = queryString;
143: } else {
144: Object[] arguments = { queryString };
145: query = getQueryFormat(arguments, queryPattern);
146: }
147:
148: try {
149: //open and close cxn with each request
150: DirContext dctx = new InitialDirContext(env);
151: jsr = jndiSearch(query, numHits, view, dctx);
152: dctx.close();
153: } catch (Exception e) {
154: throw new RDMException(e);
155: }
156: JNDIResultSet js = new JNDIResultSet(st, this , query, t);
157: js.setJNDISearchResult(jsr);
158: return js;
159: }
160:
161: /**
162: * Search the directory db
163: * @param filter Search filter
164: * @param numHits Number of search hits
165: * @param view Set of Attributes
166: * @param ctx Directory Context for search
167: * @return NamingEnumeration
168: * @throws RDMException
169: */
170: NamingEnumeration jndiSearch(String filter, int numHits, Set view,
171: DirContext ctx) throws RDMException {
172:
173: NamingEnumeration ne;
174: SearchControls sc = new SearchControls();
175: sc.setCountLimit(numHits);
176: sc.setSearchScope(SearchControls.SUBTREE_SCOPE);
177: String[] attrs = null;
178: if (view != null) {
179: attrs = (String[]) view.toArray(new String[0]);
180: sc.setReturningAttributes(attrs);
181: }
182: try {
183: ne = ctx.search("", filter, sc);
184: } catch (NamingException e) {
185: throw new RDMException(e);
186: }
187: return ne;
188: }
189:
190: /**
191: * Transform the query format based on the argument pattern
192: * @param arguments Query arguments
193: * @param patterm Query pattern in the directory search format
194: * @return a query string that is in the format defined by "pattern"
195: */
196: private String getQueryFormat(Object[] arguments, String pattern) {
197:
198: MessageFormat queryFormat = new MessageFormat("");
199: queryFormat.applyPattern(pattern);
200: return queryFormat.format(arguments);
201: }
202:
203: /**
204: * Get search count
205: * @param st Token for search
206: * @param t RDM transaction handle
207: * @return Number of docs.
208: */
209: public int count(SToken st, RDMTransaction t) throws RDMException {
210: return 250000000; // overflows int - need long
211: }
212:
213: /**
214: * Delete RD from database
215: * @param st Token for search
216: * @param s SOIF document
217: * @param view Set of attributes
218: * @param flags Options
219: * @param t RDM transaction handle
220: * @throws RDMException
221: */
222: public void delete(SToken st, SOIF s, Set view, int flags,
223: RDMTransaction t) throws RDMException {
224: throw new RDMException("JNDIDb.delete() not implemented");
225: }
226:
227: /**
228: * Update an RD with attributes set in View
229: * @param st Token for search
230: * @param insoif SOIF document
231: * @param view Set of Attributes
232: * @param flags Options
233: * @param t RDM transaction
234: * @throws RDMException
235: */
236: public void update(SToken st, SOIF insoif, Set view, int flags,
237: RDMTransaction t) throws RDMException {
238: throw new RDMException("JNDIDb.update() not implemented");
239: }
240:
241: /**
242: * Retrieve RD from database, filtered by view
243: * @param st Token for search
244: * @param url URL string
245: * @param view Set of Attributes
246: * @param flags Options
247: * @param t RDM transaction
248: * @throws RDMException
249: */
250: public SOIF fetch(SToken st, String url, Set view, int flags,
251: RDMTransaction t) throws RDMException {
252: throw new RDMException("JNDIDb.fetch() not implemented");
253: }
254:
255: /**
256: * Optimize the db
257: * @param st Token for search
258: * @throws RDMException
259: */
260: public void optimize(SToken st) throws RDMException {
261: throw new RDMException("JNDIDb.optimize() not implemented");
262: }
263:
264: /**
265: * Batch Index
266: * @param st Token for search
267: * @throws RDMException
268: */
269: public void indexBatch(SToken st) throws RDMException {
270: throw new RDMException("JNDIDb.indexBatch() not implemented");
271: }
272:
273: /**
274: * Recover the db - must be run stand alone (ie, no one else has the db open)
275: * @param st Token for search
276: * @param dbhome Database home dir
277: * @param fatal Option
278: * @throws RDMException
279: */
280: public void recover(SToken st, String dbhome, boolean fatal)
281: throws RDMException {
282: throw new RDMException("JNDIDb.recover() not implemented");
283: }
284:
285: }
|