001: package com.sun.portal.search.admin.mbeans.tasks;
002:
003: import java.io.*;
004: import java.util.*;
005: import java.util.logging.*;
006: import java.net.*;
007: import java.lang.*;
008: import java.text.*;
009:
010: import com.sun.portal.admin.server.mbeans.PSResource;
011: import com.sun.portal.admin.common.context.PortalDomainContext;
012: import com.sun.portal.admin.common.context.PSConfigContext;
013: import com.sun.portal.admin.common.PSMBeanException;
014: import com.sun.portal.search.admin.CSConfig;
015: import com.sun.portal.search.admin.mbeans.*;
016:
017: public class PopularSearch {
018:
019: private String searchServerRoot = null;
020: private static Logger logger = null;
021:
022: public PopularSearch(String searchServerRoot, Logger logger) {
023: this .searchServerRoot = searchServerRoot;
024: this .logger = logger;
025: }
026:
027: public ArrayList get(Boolean cache, Boolean skipBrowse)
028: throws PSMBeanException {
029: ArrayList searchs = new ArrayList();
030: Date date = null;
031: if (cache.booleanValue()) {
032: try {
033: date = read(searchs, skipBrowse.booleanValue());
034: } catch (Exception e1) {
035: try {
036: date = generate(searchs, skipBrowse.booleanValue());
037: write(searchs, date, skipBrowse.booleanValue());
038: } catch (Exception e2) {
039: logger.log(Level.SEVERE, "PSSH_CSPSAMB0003", e2);
040: throw new PSMBeanException("PSALI_CSPACCSH0001", e2
041: .toString(), e2);
042: }
043: }
044: } else {
045: try {
046: date = generate(searchs, skipBrowse.booleanValue());
047: write(searchs, date, skipBrowse.booleanValue());
048: } catch (Exception e) {
049: logger.log(Level.SEVERE, "PSSH_CSPSAMB0003", e);
050: throw new PSMBeanException("PSALI_CSPACCSH0001", e
051: .toString(), e);
052: }
053: }
054:
055: ArrayList list = new ArrayList();
056: for (int index = 0; index < searchs.size(); index++) {
057: SearchFrequency sf = (SearchFrequency) searchs.get(index);
058: Properties p = new Properties();
059: p.setProperty("frequency", new Integer(sf.frequency)
060: .toString());
061: p.setProperty("scope", sf.scope);
062: list.add(p);
063: }
064:
065: ArrayList data = new ArrayList();
066: data.add(new Integer(getQueries(searchs)));
067: data.add(date);
068: data.add(list);
069: return data;
070: }
071:
072: private Date generate(ArrayList popularSearchs, boolean skipBrowse)
073: throws Exception {
074: HashMap statistics = new HashMap();
075:
076: Locale userLocale = Locale.getDefault();
077:
078: String fileName = searchServerRoot + File.separator + "logs"
079: + File.separator + "rdm.0.0.log";
080:
081: BufferedReader br = new BufferedReader(new InputStreamReader(
082: new FileInputStream(fileName)));
083: String line = null;
084: while ((line = br.readLine()) != null) {
085: Map map = parseResource(line);
086:
087: String scope = (String) map.get("scope");
088: String ql = (String) map.get("ql");
089: String rdm = (String) map.get("RDM");
090:
091: if (scope == null) {
092: continue;
093: }
094:
095: boolean isBrowsing = ql.equalsIgnoreCase("taxonomy-basic");
096:
097: if (skipBrowse && isBrowsing) {
098: continue;
099: }
100:
101: if (rdm.equals("tax")) {
102: if (ql.equals("search") || skipBrowse
103: && ql.equals("taxonomy-basic")) {
104: continue;
105: }
106: }
107:
108: if (rdm.equals("rd") && ql.equals("search")) {
109: if (scope.toUpperCase().startsWith("(CLASSIFICATION")
110: || scope.toUpperCase().startsWith(
111: "<NOT> (CLASSIFICATION")) {
112: continue;
113: }
114: }
115:
116: String browse = null;
117: String search = null;
118: if (isBrowsing && scope.startsWith("children* ")) {
119: String classNodeName = scope.substring(10);
120: if (classNodeName.equals("ROOT")) {
121: continue;
122: }
123: // browse = SearchResource.geti18nString("popsearch.browse", userLocale) + " " + classNodeName;
124: browse = "Browse " + classNodeName;
125: } else if (ql.equals("search")
126: && scope.toUpperCase().startsWith(
127: "(CLASSIFICATION <STARTS> ")) {
128: int firstRB = scope.indexOf(')');
129: if (firstRB > 0) {
130: int andIndex = scope.toUpperCase().indexOf("<AND>",
131: firstRB);
132: if (andIndex > 0) {
133: int secondLB = scope.indexOf('(', andIndex);
134: int secondRB = scope.lastIndexOf(')');
135: if (secondRB > secondLB) {
136: // search = scope.substring(secondLB + 1, secondRB) + SearchResource.geti18nString("within", userLocale) + scope.substring(25, firstRB);
137: search = scope.substring(secondLB + 1,
138: secondRB)
139: + "within"
140: + scope.substring(25, firstRB);
141: }
142: }
143: }
144: } else if (ql.equals("search")) {
145: search = scope;
146: }
147:
148: String key = null;
149: if (browse != null) {
150: key = browse;
151: } else if (search != null) {
152: key = search;
153: }
154:
155: if (key != null) {
156: SearchFrequency sf = (SearchFrequency) statistics
157: .get(key);
158: if (sf == null) {
159: sf = new SearchFrequency(key, 1, browse != null);
160: statistics.put(key, sf);
161: } else {
162: sf.increment();
163: }
164: }
165:
166: }
167:
168: Iterator i = statistics.keySet().iterator();
169: while (i.hasNext()) {
170: String key = (String) i.next();
171: SearchFrequency sf = (SearchFrequency) statistics.get(key);
172: int index = 0;
173: while (index < popularSearchs.size()) {
174: SearchFrequency testSF = (SearchFrequency) popularSearchs
175: .get(index);
176: if (sf.frequency > testSF.frequency) {
177: break;
178: }
179: index += 1;
180: }
181: popularSearchs.add(index, sf);
182: }
183:
184: try {
185: File file = new File(fileName);
186: return new Date(file.lastModified());
187: } catch (Exception e) {
188: return new Date();
189: }
190: }
191:
192: private Date read(ArrayList popularSearchs, boolean skipBrowse)
193: throws Exception {
194: String browse = (skipBrowse) ? "skipbrowse" : "browse";
195:
196: String fileName = searchServerRoot + File.separator + "logs"
197: + File.separator + "popularsearch_" + browse + ".cache";
198: File file = new File(fileName);
199:
200: BufferedReader br = new BufferedReader(new InputStreamReader(
201: new FileInputStream(file), "UTF-8"));
202: String line;
203: while ((line = br.readLine()) != null) {
204: int index = line.indexOf("-->");
205: String key = line.substring(0, index).trim();
206: String value = line.substring(index + 3).trim();
207: if (!key.equals("") && !value.equals("")) {
208: SearchFrequency sf = new SearchFrequency(key,
209: new Integer(value).intValue(), false);
210: popularSearchs.add(sf);
211: }
212: }
213:
214: Date date = new Date(file.lastModified());
215: return date;
216: }
217:
218: private void write(ArrayList popularSearchs, Date date,
219: boolean skipBrowse) throws Exception {
220: String browse = (skipBrowse) ? "skipbrowse" : "browse";
221:
222: String fileName = searchServerRoot + File.separator + "logs"
223: + File.separator + "popularsearch_" + browse + ".cache";
224: File file = new File(fileName);
225:
226: PrintWriter pw = new PrintWriter(new BufferedWriter(
227: new OutputStreamWriter(new FileOutputStream(file),
228: "UTF-8")), true);
229: for (int index = 0; index < popularSearchs.size(); index++) {
230: SearchFrequency sf = (SearchFrequency) popularSearchs
231: .get(index);
232: pw.println(sf.scope + " --> " + sf.frequency);
233: }
234: pw.close();
235:
236: file.setLastModified(date.getTime());
237: }
238:
239: private int getQueries(ArrayList popularSearchs) {
240: int totalQueries = 0;
241: for (int index = 0; index < popularSearchs.size(); index++) {
242: SearchFrequency sf = (SearchFrequency) popularSearchs
243: .get(index);
244: totalQueries += sf.frequency;
245: }
246: return totalQueries;
247: }
248:
249: private Map parseResource(String str) {
250: Map map = new HashMap();
251:
252: StringBuffer sb = new StringBuffer();
253: boolean inQuotes = false;
254: int backslashLevel = 0;
255: int index = 0;
256: while (index < str.length()) {
257: char c = str.charAt(index++);
258: if (c == '"') {
259: if (backslashLevel > 0) {
260: sb.append("_QUOTES_");
261: } else {
262: inQuotes = (inQuotes) ? false : true;
263: sb.append(c);
264: }
265: } else if (c == ' ') {
266: if (inQuotes) {
267: sb.append("_SPACE_");
268: } else {
269: sb.append(c);
270: }
271: } else if (c == '=') {
272: if (inQuotes) {
273: sb.append("_EQUALS_");
274: } else {
275: sb.append(c);
276: }
277: } else if (c == '\\') {
278: backslashLevel = 2;
279: } else {
280: sb.append(c);
281: }
282: if (backslashLevel > 0) {
283: backslashLevel -= 1;
284: }
285: }
286:
287: StringTokenizer st1 = new StringTokenizer(sb.toString().trim(),
288: " ");
289: while (st1.hasMoreTokens()) {
290: StringTokenizer st2 = new StringTokenizer(st1.nextToken()
291: .trim(), "=");
292: String key = (st2.hasMoreTokens()) ? st2.nextToken().trim()
293: : "";
294: String value = (st2.hasMoreTokens()) ? st2.nextToken()
295: .trim() : "";
296: if (value.equals("") && key.indexOf("[") != -1
297: && key.indexOf("]") != -1) {
298: value = key.replaceAll("\\[", "").replaceAll("\\]", "");
299: key = "date";
300: }
301: map.put(key.trim(), value.replaceAll("^\"", "").replaceAll(
302: "\"$", "").replaceAll("_SPACE_", " ").replaceAll(
303: "_EQUALS_", "=").replaceAll("_QUOTES_", "\"")
304: .trim());
305: }
306:
307: return map;
308: }
309:
310: }
|