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.admin.model;
007:
008:import java.io.*;
009:import java.util.*;
010:import java.util.logging.Logger;
011:import java.util.logging.Level;
012:import java.net.*;
013:import java.text.DateFormat;
014:import java.lang.Runtime;
015:import java.lang.Process;
016:
017:import javax.servlet.http.HttpServletRequest;
018:
019:import com.iplanet.jato.model.*;
020:import com.iplanet.jato.util.*;
021:
022:import com.sun.portal.search.soif.SOIF;
023:import com.sun.portal.search.admin.RDSearch;
024:import com.sun.portal.search.admin.CSConfig;
025:import com.sun.portal.search.admin.util.DBUtil;
026:
027:
028:import com.iplanet.am.console.base.model.AMModelBase;
029:import com.iplanet.am.console.components.view.html.DynamicGUI;
030:
031:import com.sun.portal.search.util.*;
032:import com.sun.portal.search.db.SToken;
033:import com.sun.portal.search.soif.*;
034:import com.sun.portal.log.common.PortalLogger;
035:
036:/**
037: *
038: *
039: *
040: */
041:public class DbStatModelImpl extends DefaultModel implements Serializable
042:{
043: private SToken st;
044:
045: public static final String ON_FILE_DB_ANALYSIS = "onFileDbAnalysis";
046: public static final String filePath = SearchConfig.getValue(SearchConfig.TMPDIR)
047: + File.separator + "sites.html";
048:
049: // Create a Logger for this class
050: private static Logger debugLogger = PortalLogger.getLogger (DbStatModelImpl.class);
051:
052: /**
053: *
054: *
055: */
056: public DbStatModelImpl() {
057: super ();
058: }
059:
060:
061: /**
062: *
063: *
064: */
065: public DbStatModelImpl(String name) {
066: super (name);
067: setDbName(name);
068: retrieve();
069: }
070:
071: /**
072: * initializes
073: */
074: protected void init(HttpServletRequest new_req) {
075: retrieve();
076: }
077:
078: public void setSToken(SToken stoken) {
079: st = stoken;
080: }
081:
082: //////////////////////////////////////////////////////
083: // Convenience accessors & mutators
084: //////////////////////////////////////////////////////
085:
086: /**
087: * verifies if a Database analysis has be created for a specified DB
088: */
089: public boolean isCreated() {
090: try {
091: SOIF dbSoif = DBUtil.getDBSoif(CSConfig.getServerRoot(), getDbName());
092: if (dbSoif != null) {
093: String lastModifiedStr = dbSoif.getValue(DBUtil.STAT_LAST_MODIFIED);
094: if (lastModifiedStr != null) {
095: DBUtil.df.setLenient(true);
096: Date statLastModified =DBUtil.df.parse(lastModifiedStr);
097: return true;
098: } else {
099: return false;
100: }
101: } else {
102: return false;
103: }
104: } catch (Exception e) {
105: debugLogger.log(Level.INFO, "PSSH_CSPSAM0012", e.getMessage());
106: return false;
107: }
108: }
109:
110:
111: /*
112: * this method return a boolean telling if the files sites.html is older than the RDs Db
113: * return true if sites.html older that RDs Db file or if sites.html doesn't exists
114: * return false if sites.html is same dates as RDs Db file.
115: */
116: public boolean isOld() {
117: if (isCreated()) {
118: SOIF dbSoif = DBUtil.getDBSoif(CSConfig.getServerRoot(), getDbName());
119: if (dbSoif != null) {
120: try {
121: DBUtil.df.setLenient(true);
122: Date statLastModified = DBUtil.df.parse(dbSoif.getValue(DBUtil.STAT_LAST_MODIFIED));
123: File rdDbFile = new File (CSConfig.getServerRoot()+File.separator+"db"+File.separator+getDbName()+File.separator+"AL");
124: if (rdDbFile.exists()) {
125: Date rdDbDate = new Date(rdDbFile.lastModified());
126: // comparing the dates of each files
127: if (getLastUpdated().before(rdDbDate)) {
128: // there is a new RD Db
129: return true;
130: } else {
131: return false;
132: }
133: } else {
134: debugLogger.log(Level.FINER, "PSSH_CSPSAM0014", getDbName());
135: return false;
136: }
137: } catch (Exception e) {
138: debugLogger.log(Level.FINER, "PSSH_CSPSAM0015", new String[] { getDbName(), e.getMessage() });
139: return false;
140: }
141: } else {
142: debugLogger.log(Level.FINER, "PSSH_CSPSAM0016", getDbName());
143: return true;
144: }
145: } else {
146: return true;
147: }
148: }
149:
150: public int getTotalNbServer() {
151: if (_listServer != null) {
152: return _listServer.size();
153: } else {
154: return 0;
155: }
156: }
157:
158: public int getTotalNbRd() {
159: return _totalNbRd;
160: }
161:
162: public void setTotalNbRd(int nbRd) {
163: _totalNbRd = nbRd;
164: }
165:
166: public Hashtable getListServerRd() {
167: return _listServer;
168: }
169:
170: public void setListServerRd(Hashtable listServer) {
171: _listServer = listServer;
172: }
173:
174: public Enumeration getListServerName() {
175: return _serverHref;
176: }
177:
178: public void setListServerName(Enumeration serverName) {
179: _serverHref = serverName;
180: }
181:
182: public Enumeration getListServerCounter() {
183: return _serverRDCounter;
184: }
185:
186: public void setListServerCounter(Enumeration serverCounter) {
187: _serverRDCounter = serverCounter;
188: }
189:
190: public Hashtable getListProtocolRd() {
191: return _listProtocol;
192: }
193:
194: public void setListProtocolRd(Hashtable listProtocol) {
195: _listProtocol = listProtocol;
196: }
197:
198: public Enumeration getListProtocolName() {
199: return _protocolName;
200: }
201:
202: public void setListProtocolName(Enumeration protocolName) {
203: _protocolName = protocolName;
204: }
205:
206: public Enumeration getListProtocolCounter() {
207: return _protocolRDCounter;
208: }
209:
210: public void setListProtocolCounter(Enumeration protocolCounter) {
211: _protocolRDCounter = protocolCounter;
212: }
213:
214: public void setLastUpdated(Date date) {
215: _lastUpdated = date;
216: }
217:
218: public Date getLastUpdated() {
219: return _lastUpdated;
220: }
221:
222: public String getDbName() {
223: return _dbName;
224: }
225:
226: public void setDbName(String dbName) {
227: _dbName = new String(dbName);
228: }
229:
230: /*
231: * saving the object into the static html file SERVER_ROOT/docs/sites.html
232: */
233: public void store() {
234: try {
235: if (DBUtil.updateDB(CSConfig.getServerRoot(), getDbName(), getLastUpdated(), _listServer, _listProtocol) != true) {
236: debugLogger.finer("PSSH_CSPSAM0017");
237: }
238: } catch (Exception e) {
239: debugLogger.log(Level.FINER, "PSSH_CSPSAM0018", e.getMessage());
240: }
241: }
242:
243: public Object retrieve() {
244: return retrieve(true);
245: }
246:
247: /**
248: * fromFile : true = read OnFile stats
249: * false = generate Stats from database
250: */
251: public Object retrieve(boolean fromFile)
252: {
253: if (fromFile) {
254: // get the stats from the onfile backup
255: _totalNbRd = 0;
256: _listServer =new Hashtable();
257: _listProtocol =new Hashtable();
258: try {
259: SOIF dbSoif = DBUtil.getDBSoif(CSConfig.getServerRoot(), getDbName());
260: String attrib = null;
261: String attribCount = null;
262: int nbServer = Integer.parseInt(dbSoif.getValue(DBUtil.STAT_NB_SERVER));
263:
264: if (nbServer != 0) {
265: for (int i = 0; i < nbServer; i++) {
266: attrib = DBUtil.STAT_HREF;
267: attribCount = DBUtil.STAT_HREF_COUNT;
268: _listServer.put(dbSoif.getValue(attrib,i),dbSoif.getValue(attribCount,i));
269: }
270: _serverHref = _listServer.keys();
271: _serverRDCounter = _listServer.elements();
272: } else {
273: _listServer = null;
274: _serverHref = null;
275: _serverRDCounter = null;
276: }
277: int nbProtocol = Integer.parseInt(dbSoif.getValue(DBUtil.STAT_NB_PROTOCOL));
278: if (nbProtocol != 0) {
279: for (int i = 0; i < nbProtocol; i++) {
280: attrib = DBUtil.STAT_PROTOCOL;
281: attribCount = DBUtil.STAT_PROTOCOL_COUNT;
282: _listProtocol.put(dbSoif.getValue(attrib,i),dbSoif.getValue(attribCount,i));
283: _totalNbRd += Integer.parseInt(dbSoif.getValue(attribCount,i));
284: }
285: _protocolName = _listProtocol.keys();
286: _protocolRDCounter = _listProtocol.elements();
287: } else {
288: _listProtocol= null;
289: _protocolName= null;
290: _protocolRDCounter = null;
291: }
292: setLastUpdated(DBUtil.df.parse(dbSoif.getValue(DBUtil.STAT_LAST_MODIFIED)));
293: return this ;
294: } catch (Exception e) {
295: debugLogger.finer("PSSH_CSPSAM0019");
296: return this ;
297: }
298: } else {
299: // extract the URL - URI and increment the number of RDs for that server URL
300: // extract the protocol - and increment the number of RDs for that protocol
301: // increment the total number of RDs
302: try {
303: BufferedReader buf = DBUtil.urlStatDatabase(CSConfig.getServerRoot(),getDbName());
304: // resetting object members
305: setTotalNbRd(0);
306: setListServerRd(new Hashtable());
307: setListProtocolRd(new Hashtable());
308: setLastUpdated(null);
309: String outLine = null;
310: URL rdURL;
311: int rdUrlPort;
312: String serverHref;
313: String protocol;
314: while ((outLine = buf.readLine()) != null) {
315: try {
316: rdURL = new URL(outLine);
317: } catch (MalformedURLException mfe) {
318: rdURL = null;
319: }
320: if (rdURL != null) {
321: // get the port
322: rdUrlPort = rdURL.getPort();
323:
324: // get the protocol
325: protocol = rdURL.getProtocol();
326: // if protocol already mapped add 1 to the protocol
327: // else map the protocol with RD count = 1
328: if (getListProtocolRd().containsKey(protocol)) {
329: // get current count
330: int serverProtocolCount = Integer.parseInt((String)_listProtocol.get(protocol));
331: // remove protocol entry from Map
332: _listProtocol.remove(protocol);
333: // put back protocol entry in Map with count incremented by 1
334: _listProtocol.put(protocol, Integer.toString(serverProtocolCount + 1));
335: } else {
336: _listProtocol.put(protocol, "1");
337: }
338:
339: // get the ServerHRef
340: if (protocol != null) {
341: serverHref = protocol + "://";
342: } else {
343: serverHref = new String();
344: }
345: serverHref = serverHref + rdURL.getHost();
346: if (rdUrlPort != -1) {
347: serverHref = serverHref + ":" + rdUrlPort;
348: }
349: serverHref = serverHref + "/";
350: // if ServerHRef already mapped add 1 to the protocol
351: // else map the protocol with RD count = 1
352: if (_listServer.containsKey(serverHref)) {
353: // get current count
354: int serverHrefCount = Integer.parseInt((String)_listServer.get(serverHref));
355: // remove serverHref entry from Map
356: _listServer.remove(serverHref);
357: // put back serverHref entry in Map with count incremented by 1
358: _listServer.put(serverHref, Integer.toString(serverHrefCount + 1));
359: } else {
360: _listServer.put(serverHref, "1");
361: }
362:
363: // add 1 RD to the total
364: setTotalNbRd(getTotalNbRd() + 1);
365: } else {
366: if (outLine != null) {
367: // this is probably an invalid java.net.URL
368: // for example a discussion/comment from discussion database
369: // adding to the undiscriminatory section others
370: if (outLine.startsWith("comment")) {
371: if (_listProtocol.containsKey("discussions")) {
372: int discussionProtocolCount = Integer.parseInt((String)_listProtocol.get("discussions"));
373: _listProtocol.put("discussions",(new Integer(discussionProtocolCount + 1)).toString());
374: } else {
375: _listProtocol.put("discussions","1");
376: }
377: } else {
378: if (_listProtocol.containsKey("other")) {
379: int otherProtocolCount = Integer.parseInt((String)_listProtocol.get("other"));
380: _listProtocol.put("other",(new Integer(otherProtocolCount + 1)).toString());
381: } else {
382: _listProtocol.put("other","1");
383: }
384: }
385: setTotalNbRd(getTotalNbRd() + 1);
386: } else {
387: debugLogger.log(Level.FINER, "PSSH_CSPSAM0020", SearchConfig.getValue(SearchConfig.DBDIR));
388: }
389: }
390: }
391: // store the analysis in the root-DB
392: setLastUpdated(new Date());
393: store();
394: return this ;
395: } catch (Exception e) {
396: debugLogger.log(Level.FINER, "PSSH_CSPSAM0021", e.getMessage());
397: return null;
398: }
399: }
400: }
401:
402:
403: /**
404: *
405: */
406: public boolean hasMoreServer() {
407: if (_serverHref != null) {
408: boolean hasMore = _serverHref.hasMoreElements();
409: return hasMore;
410: } else {
411: return false;
412: }
413: }
414:
415: /**
416: *
417: */
418: public boolean hasMoreProtocol() {
419: if (_protocolName != null) {
420: boolean hasMore = _protocolName.hasMoreElements();
421: return hasMore;
422: } else {
423: return false;
424: }
425: }
426:
427: /*
428: *
429: */
430: public String nextServerHref() {
431: return nextElement(_serverHref, "serverHref");
432: }
433:
434: /*
435: *
436: */
437: public String nextServerRDCounter() {
438: return nextElement(_serverRDCounter, "serverRDCounter");
439: }
440:
441: /*
442: *
443: */
444: public String nextProtocolName() {
445: return nextElement(_protocolName, "protocolName");
446: }
447:
448: /*
449: *
450: */
451: public String nextProtocolRDCounter() {
452: return nextElement(_protocolRDCounter, "protocolRDCounter");
453: }
454:
455:
456: /**
457: *
458: */
459: private String nextElement(Enumeration enum, String enumLabel) {
460: if (enumLabel == null) {
461: enumLabel = "Enumeration";
462: }
463: if (enum != null) {
464: String nextElement = null;
465: if (enum.hasMoreElements()) {
466: nextElement = (String) enum.nextElement();
467: } else {
468: debugLogger.log(Level.FINER, "PSSH_CSPSAM0022", enumLabel);
469: }
470: return nextElement;
471: } else {
472: debugLogger.finer("PSSH_CSPSAM0023");
473: return null;
474: }
475: }
476:
477: /**
478: * returning the number of Servers
479: */
480: public int nbServers() {
481: if (_listServer != null) {
482: return _listServer.size();
483: } else {
484: return 0;
485: }
486: }
487:
488: /**
489: * returning the number of Protocol
490: */
491: public int nbProtocols() {
492: if (_listProtocol != null) {
493: return _listProtocol.size();
494: } else {
495: return 0;
496: }
497: }
498:
499:
500: /*
501: * this method should be called in the beginDisplay() of the TiledView that displays the
502: * content of the _schema conversions
503: * this methodes will delete and reinstantiate the private Enumeration objects on the
504: * keys/values of the Properties
505: */
506: public void resetServers() {
507: setListServerName(null);
508: setListServerCounter(null);
509: if (getListServerRd() != null) {
510: setListServerName(getListServerRd().keys());
511: setListServerCounter(getListServerRd().elements());
512: } else {
513: debugLogger.finer("PSSH_CSPSAM0024");
514: }
515: }
516:
517: /*
518: * this method should be called in the beginDisplay() of the TiledView that displays the
519: * content of the _schema conversions
520: * this methodes will delete and reinstantiate the private Enumeration objects on the
521: * keys/values of the Properties
522: */
523: public void resetProtocols() {
524: setListProtocolName(null);
525: setListProtocolCounter(null);
526: if (getListProtocolRd() != null) {
527: setListProtocolName(getListProtocolRd().keys());
528: setListProtocolCounter(getListProtocolRd().elements());
529: } else {
530: debugLogger.finer("PSSH_CSPSAM0025");
531: }
532: }
533:
534: //////////////////////////////////////////////////////
535: // Class variables
536: //////////////////////////////////////////////////////
537:
538:
539: //////////////////////////////////////////////////////
540: // Member variables
541: //////////////////////////////////////////////////////
542: private Hashtable _listServer = null; // key= server URL / value = nb RDs
543: private Enumeration _serverHref = null; // Enumeration of the values
544: private Enumeration _serverRDCounter = null; // Enumeration of the keys
545: private Hashtable _listProtocol = null; // key= protocol name / value = nb RDs
546: private Enumeration _protocolName = null; // Enumeration of the values
547: private Enumeration _protocolRDCounter = null; // Enumeration of the keys
548: private Date _lastUpdated = new Date();;
549: private int _totalNbRd = 0;
550: private String _dbName = null;
551:}
|