001: /*
002: * Copyright 2001 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.rdmserver;
007:
008: import com.sun.portal.search.soif.*;
009: import com.sun.portal.search.rdm.*;
010: import com.sun.portal.search.db.*;
011: import com.sun.portal.search.util.*;
012: import com.sun.portal.log.common.PortalLogger;
013:
014: import java.util.*;
015: import java.util.logging.Logger;
016: import java.util.logging.Level;
017: import java.util.logging.LogRecord;
018: import java.text.*;
019: import java.io.*;
020:
021: /**
022: * RDM message processing facilities: service routing, etc.
023: */
024: public class RDMServer {
025:
026: static RDMServer rdmsvr; // for use by rdmui bridge
027:
028: RDMConfig rdmconfig;
029: RDMContext rdmcontext; // container access
030:
031: // Service registry (RDMServiceDescriptor <=> class)
032: Map RDMServices = new HashMap();
033: List RDMServiceClasses = new ArrayList();
034:
035: RDMService unsupportedService = new UnsupportedService();
036:
037: public RDMServer(RDMContext rdmctx) {
038:
039: rdmsvr = this ;
040:
041: rdmcontext = rdmctx;
042:
043: // Load known service names and classes
044: // XXX should read from config file
045:
046: // Database services
047: RegisterRDMService("com.sun.portal.search.rdmserver.DatabaseService");
048:
049: // Taxonomy services (XXX taxonmy should become a normal database)
050: RegisterRDMService("com.sun.portal.search.rdmserver.TaxonomyService");
051:
052: // Schema Description services
053: RegisterRDMService("com.sun.portal.search.rdmserver.SchemaService");
054:
055: // Server Description services
056: RegisterRDMService("com.sun.portal.search.rdmserver.ServerDescriptionService");
057:
058: // Server Status services
059: RegisterRDMService("com.sun.portal.search.rdmserver.ServerStatusService");
060:
061: }
062:
063: RDMContext getRDMContext() {
064: return rdmcontext;
065: }
066:
067: /** Register an RDM service. Use 'null' QL to support a default query language */
068: public void RegisterRDMService(String className) {
069: RDMService svc = null;
070: RDMServiceDescriptor svd = null;
071: try {
072: svc = (RDMService) Class.forName(className).newInstance();
073: RDMServiceClasses.add(svc);
074: Set supportedServices = svc.getSupportedServices();
075: for (Iterator i = supportedServices.iterator(); i.hasNext();) {
076: svd = (RDMServiceDescriptor) i.next();
077: RDMServices.put(svd, svc);
078: SearchLogger.getLogger().log(
079: Level.FINEST,
080: "PSSH_CSPSRDMS0042",
081: new Object[] { svd.getRDMType(),
082: svd.getQueryLanguage(), className });
083: }
084: } catch (Exception e) {
085: if (svd == null)
086: if (SearchLogger.getLogger().isLoggable(Level.WARNING)) {
087: LogRecord rec = new LogRecord(Level.WARNING,
088: "PSSH_CSPSRDMS0043");
089: Object[] obj = new Object[] { className };
090: rec.setParameters(obj);
091: rec.setThrown(e);
092: rec.setLoggerName(SearchLogger.getLogger()
093: .getName());
094: SearchLogger.getLogger().log(rec);
095: } else if (SearchLogger.getLogger().isLoggable(
096: Level.WARNING)) {
097: LogRecord rec = new LogRecord(Level.WARNING,
098: "PSSH_CSPSRDMS0044");
099: Object[] obj = new Object[] { svd.getRDMType(),
100: svd.getQueryLanguage(), className };
101: rec.setParameters(obj);
102: rec.setThrown(e);
103: rec.setLoggerName(SearchLogger.getLogger()
104: .getName());
105: SearchLogger.getLogger().log(rec);
106: }
107: }
108: }
109:
110: /**
111: * service() - Main RDM service function.
112: * Routes the RDM message to the appropriate service function.
113: * The result RDM message is sent.
114: *
115: * Assumes that the RDMRequest @RDMHEADER is 100% valid...
116: */
117: public void service(RDMRequest req, RDMResponse res)
118: throws Exception {
119: SearchLogger.getLogger().log(Level.FINE, "PSSH_CSPSRDMS0045",
120: Thread.currentThread());
121: try {
122: req.setRDMServer(this );
123: normalizeRequest(req, res);
124: String csid = req.getHeader().getCSID();
125: if (csid != null)
126: res.getHeader().setCSID(csid); // only return csid if legacy client supplied it
127: RDMServiceDescriptor reqsvc = new RDMServiceDescriptor(req
128: .getHeader().getType(), req.getHeader()
129: .getQueryLanguage());
130: RDMService svc = (RDMService) RDMServices.get(reqsvc);
131: if (svc == null)
132: svc = unsupportedService;
133: svc.service(req, res);
134: } catch (Exception e) {
135: // log and continue with an error response
136: SearchLogger.getLogger().log(Level.WARNING,
137: "PSSH_CSPSRDMS0046", e);
138: String msg = "Internal Error";
139: if (e.getMessage() != null)
140: msg += ": " + e.getMessage();
141: res.getHeader().setErrorMessage(msg); // XXX client won't see this if hdr was already sent
142: } finally {
143: SearchLogger.getLogger().log(Level.FINE,
144: "PSSH_CSPSRDMS0047", res.getHeader());
145: res.sendHeader(); // send RDM response header if not already sent
146: SearchLogger.getLogger().log(Level.FINE,
147: "PSSH_CSPSRDMS0048");
148: }
149:
150: }
151:
152: /** general normalizations - nothing service specific */
153: void normalizeRequest(RDMRequest req, RDMResponse res)
154: throws Exception {
155:
156: RDMHeader hdr = req.getHeader();
157:
158: SearchLogger.getLogger().log(Level.FINE, "PSSH_CSPSRDMS0049",
159: req);
160:
161: // normalize rdm type
162: String rdmtype = hdr.getType();
163: if (rdmtype == null || rdmtype.length() == 0)
164: rdmtype = "-"; // handled later
165: hdr.setType(rdmtype.toLowerCase());
166:
167: SearchLogger.getLogger().log(Level.FINE, "PSSH_CSPSRDMS0050",
168: rdmtype);
169:
170: // some bwd compat details
171: if (rdmtype.equalsIgnoreCase(RDM.RDM_SD_REQ_LEGACY)) {
172: rdmtype = RDM.RDM_SD_REQ;
173: hdr.setType(rdmtype);
174: } else if (rdmtype.equalsIgnoreCase(RDM.RDM_SD_RES_LEGACY)) {
175: rdmtype = RDM.RDM_SD_RES;
176: hdr.setType(rdmtype);
177: } else if (rdmtype.equalsIgnoreCase(RDM.RDM_SCH_REQ_LEGACY)) {
178: rdmtype = RDM.RDM_SCH_REQ;
179: hdr.setType(rdmtype);
180: } else if (rdmtype.equalsIgnoreCase(RDM.RDM_SCH_RES_LEGACY)) {
181: rdmtype = RDM.RDM_SCH_RES;
182: hdr.setType(rdmtype);
183: } else if (rdmtype.equalsIgnoreCase(RDM.RDM_TAX_REQ_LEGACY)) {
184: rdmtype = RDM.RDM_TAX_REQ;
185: hdr.setType(rdmtype);
186: } else if (rdmtype.equalsIgnoreCase(RDM.RDM_TAX_RES_LEGACY)) {
187: rdmtype = RDM.RDM_TAX_RES;
188: hdr.setType(rdmtype);
189: }
190:
191: // handle empty QL
192: String ql = hdr.getQueryLanguage();
193: if (ql != null && ql.length() == 0) {
194: hdr.getSOIF().remove(RDM.A_RDM_QL);
195: }
196:
197: // Check the RDMRequest CSID
198: String csid = hdr.getCSID();
199: if (csid != null && csid.length() > 0) {
200: // Validate requested CSID
201: if (!rdmconfig.searchcf.getDefault().equalsIgnoreCase(csid))
202: throw new Exception("Unsupported CSID.");
203: }
204:
205: }
206:
207: /**
208: * Start things up
209: */
210: public void init(RDMConfig rdmconfig) throws Exception {
211: if (this .rdmconfig != null) {
212: SearchLogger.getLogger().log(Level.WARNING,
213: "PSSH_CSPSRDMS0051");
214: return;
215: }
216: this .rdmconfig = rdmconfig;
217: }
218:
219: /**
220: * Shut down
221: */
222: public void shutdown() throws Exception {
223: try {
224: SearchLogger.getLogger().log(Level.INFO,
225: "PSSH_CSPSRDMS0052");
226: for (Iterator i = RDMServiceClasses.iterator(); i.hasNext();) {
227: RDMService svc = (RDMService) i.next();
228: svc.shutdown();
229: }
230: } catch (Exception e) {
231: SearchLogger.getLogger().log(Level.WARNING,
232: "PSSH_CSPSRDMS0053", e);
233: }
234: }
235:
236: // some space-saving type names
237: static Map rdm_logmsg_typestr;
238: static {
239: rdm_logmsg_typestr = new HashMap();
240: rdm_logmsg_typestr.put(RDM.RDM_RD_REQ, "rd");
241: rdm_logmsg_typestr.put(RDM.RDM_RD_DEL_REQ, "rd-delete");
242: rdm_logmsg_typestr.put(RDM.RDM_RD_RES, "rd-response");
243: rdm_logmsg_typestr
244: .put(RDM.RDM_RD_DEL_RES, "rd-delete-response");
245: rdm_logmsg_typestr.put(RDM.RDM_RD_SUBMIT_REQ, "rd-submit");
246: rdm_logmsg_typestr.put(RDM.RDM_RD_SUBMIT_RES,
247: "rd-submit-response");
248: rdm_logmsg_typestr.put(RDM.RDM_SD_REQ, "server");
249: rdm_logmsg_typestr.put(RDM.RDM_SD_RES, "server-response");
250: rdm_logmsg_typestr.put(RDM.RDM_SCH_REQ, "schema");
251: rdm_logmsg_typestr.put(RDM.RDM_SCH_RES, "schema-response");
252: rdm_logmsg_typestr.put(RDM.RDM_TAX_REQ, "tax");
253: rdm_logmsg_typestr.put(RDM.RDM_TAX_RES, "taxonomy-response");
254: rdm_logmsg_typestr.put(RDM.RDM_STAT_REQ, "status");
255: rdm_logmsg_typestr.put(RDM.RDM_STAT_RES, "status-response");
256: rdm_logmsg_typestr.put(RDM.RDM_MSGTYPE_UNKNOWN, "unknown");
257: }
258:
259: public static String getRDMTypeLogMsg(String type) {
260: String m = (String) rdm_logmsg_typestr.get(type);
261: if (m == null)
262: m = (String) rdm_logmsg_typestr
263: .get(RDM.RDM_MSGTYPE_UNKNOWN);
264: return m;
265: }
266:
267: static java.text.DateFormat df;
268: static {
269: //df = DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT, Locale.US);
270: df = new SimpleDateFormat("dd/MMM/yyyy:HH:mm:ss");
271: }
272:
273: public String getVersion() {
274: try {
275: ResourceBundle prodRB = PropertyResourceBundle
276: .getBundle("PSversion");
277: StringBuffer vinfo = new StringBuffer();
278: vinfo.append(prodRB.getString("productname")).append(
279: " RDM Server ").append(
280: prodRB.getString("productversion"));
281: return vinfo.toString();
282: } catch (Exception e) {
283: return "Sun ONE RDM Server Version 6.0";
284: }
285: }
286:
287: public void logRDM(RDMRequest req, String msg) {
288: String rdmtype = req.getHeader().getType();
289: String ql = req.getHeader().getQueryLanguage();
290: StringBuffer buf = new StringBuffer();
291: buf.append("[");
292: buf.append(df.format(new Date()));
293: buf.append("]");
294: buf.append(" RDM=" + getRDMTypeLogMsg(rdmtype) + " ql="
295: + (ql != null ? ql : "-") + " " + msg);
296: rdmcontext.logrdm(buf.toString());
297: }
298: }
|