001/**
002 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003 *
004 * Copyright (c) 2006 Sun Microsystems Inc. All Rights Reserved
005 *
006 * The contents of this file are subject to the terms
007 * of the Common Development and Distribution License
008 * (the License). You may not use this file except in
009 * compliance with the License.
010 *
011 * You can obtain a copy of the License at
012 * https://opensso.dev.java.net/public/CDDLv1.0.html or
013 * opensso/legal/CDDLv1.0.txt
014 * See the License for the specific language governing
015 * permission and limitations under the License.
016 *
017 * When distributing Covered Code, include this CDDL
018 * Header Notice in each file and include the License file
019 * at opensso/legal/CDDLv1.0.txt.
020 * If applicable, add the following below the CDDL Header,
021 * with the fields enclosed by brackets [] replaced by
022 * your own identifying information:
023 * "Portions Copyrighted [year] [name of copyright owner]"
024 *
025 * $Id: LogReader.java,v 1.7 2008/10/30 04:11:06 bigfatrat Exp $
026 *
027 */
028
029/*
030 * Portions Copyrighted [2011] [ForgeRock AS]
031 */
032package com.sun.identity.log;
033
034import java.io.File;
035import java.io.IOException;
036import java.lang.reflect.Array;
037import java.util.ArrayList;
038import java.util.HashSet;
039import java.util.Iterator;
040import java.util.Set;
041import java.util.StringTokenizer;
042
043import com.sun.identity.log.spi.Debug;
044
045/**LogReader class provides mechanism to read a log file to the caller.
046 * It does the authorization check, reads line from the file, applies the
047 * query (if any), collects most recent records, sorts the records, and
048 * returns the result in a two dimensional String. Where columns in the
049 * the first row, i.e. 0th row, always holds the header info (field names)
050 * present in the ELF formatted file.
051 * Other rows hold the value present under those columns.
052 * @supported.all.api
053 */
054public class LogReader {
055    /* private attributes */
056    private static int maxReordToReturn = 1;
057    private static final String FILE_SOURCE = "File";
058    private static java.util.logging.LogManager manager;
059    private static String [][] queryResult = null;
060    private static String logFileName = null;
061    private static String logPathName = null;
062    private static String logFields = null;
063    private static String fileHandlerClass = null;
064    private static String maxRecStr = null;
065    private static String dbHandlerClass = null;
066    private static String logStorageType = null;
067    private static String logSecurity = null;
068    private static String securityPrefix = "_secure";
069    private static String loggerName;   /* name of the logger object */
070    private static com.sun.identity.log.handlers.LogReadHandler currentHandler
071        = null;
072    private static com.sun.identity.log.handlers.LogReadDBHandler
073        currentDBHandler = null;
074    private static boolean logTypeIsFile = true;  /* default "File" */
075
076    /* private constructor. Only assigns manager. */
077    private LogReader() {
078        this.manager = LogManagerUtil.getLogManager();
079    }
080    
081
082    /**
083     * Returns the units (LogConstants.NUM_BYTES or LogConstants.NUM_RECORDS)
084     * that applies to the value returned by getSize(logName).
085     *
086     * @return the units applying to the return value of getSize(logName),
087     *         LogConstants.NUM_BYTES (in the case of File logging), or
088     *         LogConstants.NUM_RECORDS (in the case of DB logging).
089     * @throws Exception if unrecoverable problem occurs, that is beyond
090     *         its control.
091     **/
092
093    public static int getSizeUnits()
094        throws Exception
095    {
096        LogReader lr = new LogReader();
097        try {
098            lr.readConfiguration();
099        } catch (Exception ex) {
100            Debug.error("LogReader.getSizeUnits:could not read configuration");
101            throw ex;
102        }
103
104        int i = LogConstants.NUM_RECORDS;
105        if (logTypeIsFile) {
106            return (LogConstants.NUM_BYTES);
107        }
108        return (i);
109    }
110
111    /**
112     * Returns the number of LogRecords in the specified table in the DB.
113     * In the case where logging is to a file, the number of bytes in
114     * the specified file is returned.  Use getSizeUnits() to get which
115     * units apply.
116     *
117     * @param logName the name of the Table or File.
118     * @return the number of LogRecords (in the DB table), or number of
119     *         bytes (in the file).
120     * @throws IOException if file does not exist.
121     * @throws Exception if unrecoverable problem occurs, that is beyond
122     *         its control.
123     **/
124    public static long getSize(String logName)
125        throws IOException, Exception
126    {
127        LogReader lr = new LogReader();
128        try {
129            lr.readConfiguration();
130        } catch (Exception ex) {
131            Debug.error("LogReader.getSize: could not read configuration");
132            throw ex;
133        }
134
135        long li;
136        if (logTypeIsFile) {
137            String file = logPathName + logName;
138            File thisFile = new File (file);
139            if (thisFile.exists()) {
140                li = thisFile.length();
141            } else {
142                throw new IOException(logName + " does not exist.");
143            }
144        } else {
145            li = currentDBHandler.getNumberOfRows(manager, logName);
146        }
147        return li;
148    }
149
150    /**
151     * Returns the names of the Log Files or Tables. The Set of names is
152     * what is found in the directory or DB, filtered through a default
153     * list of names.  Thus, log files or tables with custom names will
154     * not be included.  This does not preclude querying those log files
155     * or tables with custom names.
156     *
157     * @return the Log File/Table names in a Set.
158     **/
159
160    public static Set getLogNames() {
161        LogReader lr = new LogReader();
162        Set logNames = new HashSet();
163
164        try {
165            lr.readConfiguration();
166        } catch (Exception ex) {
167            return logNames;
168        }
169
170        if (logTypeIsFile) {
171            File dir = new File(logPathName);
172            String [] dirList = dir.list();
173            if (dirList != null) {
174                int numFiles = Array.getLength(dirList);
175                int numDefFiles = Array.getLength(LogConstants.LOGFILENAMES);
176                if (numFiles > 0) {
177                    for (int i = 0; i < numFiles; i++) {
178                        for (int j = 0; j < numDefFiles; j++) {
179                            String nmToCompare = LogConstants.LOGFILENAMES[j];
180                            /*
181                             *  want to keep out the files that start with:
182                             *    _secure.log. and _secure.ver..  just want
183                             *  the _secure.am* files.
184                             */
185
186                            if (logSecurity.equalsIgnoreCase("ON")) {
187                                nmToCompare = securityPrefix + "." +
188                                    nmToCompare;
189                            }
190                            if (dirList[i].indexOf(nmToCompare) > -1) {
191                                logNames.add(dirList[i]);
192                                break;
193                            }
194                        }
195                    }
196                }
197            }
198        } else {
199            queryResult = currentDBHandler.getTableNames(manager);
200            int szOfList = Array.getLength(queryResult);
201            int numDefFiles = Array.getLength(LogConstants.LOGFILENAMES);
202
203            /*
204             *  for commonality, make both sides uppercase because
205             *  oracle makes the tablenames uppercase; mysql doesn't.
206             */
207            for (int i = 0; i < szOfList; i++) {
208                String tFile = queryResult[i][0].replace('_', '.');
209                String thisFile = tFile.toUpperCase();
210                String thatFile = null;
211                for (int j = 0; j < numDefFiles; j++) {
212                    thatFile = LogConstants.LOGFILENAMES[j].toUpperCase();
213                    if (thatFile.indexOf(thisFile) > -1) {
214                        logNames.add(queryResult[i][0]);  /* real tblname */
215                        break;
216                    }
217                }
218            }
219        }
220        return logNames;
221    }
222
223    /**
224     * Returns the names of the Log Fields that are selected for logging
225     * in the Logging Service template, plus the mandatory "time" and
226     * "Data" fields.
227     *
228     * @return the Field/Column names in an ArrayList.
229     **/
230
231    public static ArrayList getLogFields() {
232        ArrayList lFHS = new ArrayList();
233        LogReader lr = new LogReader();
234
235        try {
236            lr.readConfiguration();
237        } catch (Exception ex) {
238            return lFHS;
239        }
240
241        lFHS.add("time");
242        lFHS.add(LogConstants.DATA);
243        if ((logFields != null) && (logFields.length() != 0)) {
244            StringTokenizer stok = new StringTokenizer(logFields, ", ");
245            while (stok.hasMoreElements()) {
246                lFHS.add(stok.nextToken());
247            }
248        }
249        return lFHS;
250    }
251
252    /**
253     * Reads the specified log file provided the user has the authorization.
254     * It reads all records and returns them without any modification.
255     * This query ignores the max record parameter set through configuration.
256     * This API is present to support log verifier requirement.
257     *
258     * @param fileName the filename without path to be read.
259     * @param userCrdential user credential to check authorization.
260     * @return results in a two dimensional String, where columns in the
261     *         the first row always hold the field names present in the
262     *         file. Other rows hold the values present under those column.
263     * @throws IOException if interrupted or failed to do I/O.
264     * @throws NoSuchFieldException if invalid field has been specified.
265     * @throws IllegalArgumentException when inappropriate argument specified.
266     * @throws RuntimeException when it has been caught in any phase.
267     * @throws Exception if unrecoverable problem occurs, that is beyond
268     *         its control.
269     **/
270    public static synchronized String [][] read(
271        String fileName,
272        Object userCrdential
273    ) throws IOException, NoSuchFieldException,
274        IllegalArgumentException,
275        RuntimeException, Exception
276    {
277        /*
278         *  READ METHOD 1
279         *
280         * read the configuration from LogManager to use updated info.
281         */
282        LogReader lr = new LogReader();
283        lr.readConfiguration();
284        if (fileName == null) {
285            throw new IllegalArgumentException("filename can't be null");
286        }
287        lr.setLoggerName(fileName);
288        /* check whether user is authorized or not */
289        if (lr.isAllowed(userCrdential) != true) {
290            throw new AMLogException(fileName + ":" +
291                AMLogException.LOG_RD_AUTH_FAILED);
292        }
293        /* form the full file name */
294        String fullFileName = logPathName + fileName;
295        /* get all the records through file handler */
296        LogQuery qry = new LogQuery(LogQuery.ALL_RECORDS);
297        if (logTypeIsFile) {
298            queryResult = currentHandler.logRecRead(fullFileName,qry,false);
299        } else {
300            queryResult = currentDBHandler.logRecRead(fileName, qry,
301                manager, false);
302        }
303        return queryResult;
304    }
305    
306    /**
307     * Retrieves records from a log file provided the user has the required
308     * authorization. It identifies the filename using <code>logname</code> and
309     * <code>type</code>.
310     * It reads all records from the file but returns the maximum number
311     * of most recent records set through configuration.
312     *
313     * @param logName an identifier and is a part of file name to be read.
314     * @param logType the components of file name that will be read. it could
315     *        be either of "access", "error" or "system".
316     * @param userCrdential user credential to check authorization.
317     * @return results in a two dimensional String, where columns in the
318     *         the first row always hold the field names present in the
319     *         file. Other rows hold the values present under those column.
320     * @throws IOException if interrupted or failed to do I/O.
321     * @throws NoSuchFieldException if invalid field has been specified.
322     * @throws IllegalArgumentException when inappropriate argument specified.
323     * @throws RuntimeException when it has been caught in any phase.
324     * @throws Exception if unrecoverable problem occurs, that is beyond
325     *         its control.
326     **/
327    public static synchronized  String [][] read(String logName,
328        String logType,
329        Object userCrdential
330    ) throws IOException, NoSuchFieldException,
331        IllegalArgumentException,
332        RuntimeException, Exception
333    {
334        /*
335         * READ METHOD 2
336         *
337         * pass the call to the CORE logName based read api to collect data
338         */
339        queryResult = read(logName, logType, (String)null, (LogQuery)null,
340            userCrdential);
341        return queryResult;
342    }
343    
344    /**
345     * Reads a log file provided the user has the authorization. It reads all
346     * records but returns the maximum number of most recent records set
347     * through configuration.
348     *
349     * @param logName an identifier and is a part of file name to be read.
350     * @param logType the components of filename to be read, not null.
351     * @param timeStamp last component of filename to be read and not null.
352     * @param userCrdential user credential for authorization check.
353     * @return results in a two dimensional String, where columns in the
354     *         the first row always hold the field names present in the
355     *         file. Other rows hold the values present under those column.
356     * @throws IOException if interrupted or failed to do I/O.
357     * @throws NoSuchFieldException if invalid field has been specified.
358     * @throws IllegalArgumentException when inappropriate argument specified.
359     * @throws RuntimeException when it has been caught in any phase.
360     * @throws Exception if unrecoverable problem occurs, that is beyond
361     *         its control.
362     **/
363    public static synchronized String [][] read(String logName,
364        String logType,
365        String timeStamp,
366        Object userCrdential
367    ) throws IOException, NoSuchFieldException,
368        IllegalArgumentException,
369        RuntimeException,
370        Exception
371    {
372        /*
373         *  READ METHOD 3
374         *
375         * pass the call to the CORE logName based read api to collect data
376         */
377        queryResult = read(logName,logType,timeStamp,null,userCrdential);
378        return queryResult;
379    }
380    
381    /**
382     * Retrieves records from log file provided it has
383     * the required authorization. It reads all records applies query
384     * and returns the result as asked by the caller.
385     *
386     * @param logName an identifier and is a part of file name to be read.
387     * @param logType the components of filename to be read.
388     * @param logQuery contains search criteria details.
389     * @param userCrdential user credential for authorization check.
390     * @return results in a two dimensional String, where columns in the
391     *         the first row always hold the field names present in the
392     *         file. Other rows hold the values present under those column.
393     * @throws IOException if interrupted or failed to do I/O.
394     * @throws NoSuchFieldException if invalid field has been specified.
395     * @throws IllegalArgumentException when inappropriate argument specified.
396     * @throws RuntimeException when it has been caught in any phase.
397     * @throws Exception if unrecoverable problem occurs, that is beyond
398     *         its control.
399     */
400    public static synchronized String [][] read(String logName,
401        String logType,
402        LogQuery logQuery,
403        Object userCrdential
404    ) throws IOException, NoSuchFieldException,
405        IllegalArgumentException,
406        RuntimeException, Exception
407    {
408        /*
409         *  READ METHOD 4
410         *
411         * pass the call to the CORE logName based read api to collect data
412         */
413        queryResult = read(logName,logType,null,logQuery,userCrdential);
414        return queryResult;
415    }
416    
417    /**
418     * Reads a log file provided it has the required authorization.
419     * It reads all records but returns the maximum number of most recent
420     * records (if asked) those meet the caller's requirement as specified
421     * through query.
422     *
423     * @param logname an identifier and is a part of file name to be read.
424     * @param logtype the components of filename to be read.
425     * @param timeStamp is the last component of filename to be read and not
426     *        null.
427     * @param logQuery contains search criteria details.
428     * @param userCrdential user credential for authorization check.
429     * @return results in a two dimensional String, where columns in the
430     *         the first row always hold the field names present in the
431     *         file. Other rows hold the values present under those column.
432     * @throws IOException if interrupted or failed to do I/O.
433     * @throws NoSuchFieldException if invalid field has been specified.
434     * @throws IllegalArgumentException when inappropriate argument specified.
435     * @throws RuntimeException when it has been caught in any phase.
436     * @throws Exception if unrecoverable problem occurs, that is beyond
437     *         its control.
438     **/
439    public static String [][] read(String logname,
440        String logtype,
441        String timeStamp,
442        LogQuery logQuery,
443        Object userCrdential
444    ) throws IOException, NoSuchFieldException,
445        IllegalArgumentException,
446        RuntimeException, Exception
447    {
448        /*
449         *  READ METHOD 5
450         *
451         * Create the actual file name out of the components
452         */
453        String mainFileName = new String();
454        
455        /* set logger name */
456        setLoggerName(logname,logtype);
457        
458        mainFileName = loggerName;
459        if (timeStamp != null) {
460            if (mainFileName.length() == 0) {
461                /* atleast one of logname or logtype must be present */
462                return null;
463            }
464            mainFileName += "." + timeStamp;
465        }
466        queryResult = read(mainFileName,logQuery,userCrdential);
467        return queryResult;
468    }
469    
470    /**
471     * Retrieves specific records in a given sorted order on
472     * specific field (if user specifies valid sorting by field). The API
473     * also needs the user has a successful authorization.
474     *
475     * @param fileName filename without path that will be read.
476     * @param logQuery contains search criteria details.
477     * @param userCrdential user credential for authorization check.
478     * @return results in a two dimensional String, where columns in the
479     *         the first row always hold the field names present in the
480     *         file. Other rows hold the values present under those column.
481     * @throws IOException if interrupted or failed to do I/O.
482     * @throws NoSuchFieldException if invalid field has been specified.
483     * @throws IllegalArgumentException when inappropriate argument specified.
484     * @throws RuntimeException when it has been caught in any phase.
485     * @throws Exception if unrecoverable problem occurs, that is beyond
486     *         its control.
487     **/
488    public static String [][] read(String fileName,
489        LogQuery logQuery,
490        Object userCrdential
491    ) throws IOException, NoSuchFieldException,
492        IllegalArgumentException,
493        RuntimeException, Exception
494    {
495        /* READ METHOD 6 */
496        LogReader lr = new LogReader();
497        /* this is the CORE read api, used by all other read apis */
498        lr.readConfiguration();
499        /* pass the request to the file handler & collect all required data */
500        if (fileName == null) {
501            throw new IllegalArgumentException("filename can't be null");
502        }
503        if (maxReordToReturn <= 0) {
504            maxReordToReturn = 1;
505        }
506        if (logQuery != null) {
507            if (logQuery.getNumRecordsWanted() < LogQuery.ALL_RECORDS) {
508                logQuery.setMaxRecord(maxReordToReturn);
509            } else if ((logQuery.getNumRecordsWanted() ==
510                LogQuery.MOST_RECENT_MAX_RECORDS) && (logTypeIsFile))
511            {
512                /*  MOST_RECENT_MAX_RECORDS processed in DB reader */
513                logQuery.setMaxRecord(maxReordToReturn);
514            }
515        } else {
516            logQuery = new LogQuery(maxReordToReturn);
517        }
518        /* sets logger name */
519        setLoggerName(fileName);
520        
521        /* check whether user is authorized or not */
522        if (lr.isAllowed(userCrdential) != true) {
523            throw new AMLogException(fileName + ":" +
524                AMLogException.LOG_RD_AUTH_FAILED);
525        }
526        if (logTypeIsFile) {
527            String fullFileName = logPathName + fileName;
528            queryResult = currentHandler.logRecRead(fullFileName,
529                logQuery, true);
530        } else {
531            queryResult = currentDBHandler.logRecRead(fileName, logQuery,
532                manager, true);
533        }
534        return queryResult;
535    }
536    
537    /**
538     * Retrieves specific records in a given sorted order on
539     * specific field (if user specifies valid sorting by field). The API
540     * also needs the user has a successful authorization.
541     *
542     * @param fileNames set of filenames without path that will be read
543     * @param logQuery contains search criteria details
544     * @param userCrdential user credential for authorization check.
545     * @return results in a two dimensional String, where columns in the
546     *         the first row always hold the field names present in the
547     *         file. Other rows hold the values present under those column.
548     * @throws IOException if interrupted or failed to do I/O.
549     * @throws NoSuchFieldException if invalid field has been specified.
550     * @throws IllegalArgumentException when inappropriate argument specified.
551     * @throws RuntimeException when it has been caught in any phase.
552     * @throws Exception if unrecoverable problem occurs, that is beyond
553     *         its control.
554     **/
555    public static String [][] read(
556        Set fileNames,
557        LogQuery logQuery,
558        Object userCrdential
559    ) throws IOException, NoSuchFieldException,
560        IllegalArgumentException,
561        RuntimeException, Exception
562    {
563        /* READ METHOD 7 */
564        LogReader lr = new LogReader();
565        /* this is the CORE read api, used by all other read apis */
566        lr.readConfiguration();
567        /* pass the request to the file handler & collect all required data */
568        if (fileNames == null) {
569            throw new IllegalArgumentException("filenames can't be null");
570        }
571        if (fileNames.isEmpty()) {
572            throw new IllegalArgumentException("filenames can't be empty");
573        } else {
574            /*  check all filenames in set */
575            for (Iterator it = fileNames.iterator(); it.hasNext(); ) {
576                String ss = (String)it.next();
577                if (ss != null) {
578                    ss = ss.trim();
579                }
580                if ((ss == null) || (ss.length() == 0)) {
581                    throw new IllegalArgumentException(
582                        "filename cannot be null");
583                }
584            }
585        }
586
587        if (maxReordToReturn <= 0) {
588            maxReordToReturn = 1;
589        }
590        if (logQuery != null) {
591            if (logQuery.getNumRecordsWanted() < LogQuery.ALL_RECORDS) {
592                logQuery.setMaxRecord(maxReordToReturn);
593            } else if ((logQuery.getNumRecordsWanted() ==
594                LogQuery.MOST_RECENT_MAX_RECORDS) && (logTypeIsFile))
595            {
596                /* MOST_RECENT_MAX_RECORDS processed in DB reader */
597                logQuery.setMaxRecord(maxReordToReturn);
598            }
599        } else {
600            logQuery = new LogQuery(maxReordToReturn);
601        }
602
603        String tmpF = getAllFilenames (fileNames);
604        /* sets logger name */
605        setLoggerName(tmpF);
606        
607        /* check whether user is authorized or not */
608        if (lr.isAllowed(userCrdential) != true) {
609            throw new AMLogException(tmpF + ":" +
610                AMLogException.LOG_RD_AUTH_FAILED);
611        }
612        if (logTypeIsFile) {
613            Set fullFileNames = new HashSet();
614            for (Iterator it = fileNames.iterator(); it.hasNext(); ) {
615                String ss = (String)it.next();
616                ss = logPathName + ss;
617                fullFileNames.add(ss);
618            }
619            queryResult =
620                currentHandler.logRecRead(fullFileNames, logQuery, true);
621        } else {
622            queryResult =
623                currentDBHandler.logRecRead(fileNames, logQuery, manager,true);
624        }
625        return queryResult;
626    }
627
628    private static String getAllFilenames (Set fileNames) {
629        StringBuilder fsSB = new StringBuilder();
630
631        if (fileNames == null) {
632            return null;
633        }
634        if (fileNames.isEmpty()) {
635            return "";
636        }
637
638        String ts = null;
639        for (Iterator it=fileNames.iterator(); it.hasNext(); ) {
640            ts = (String)it.next();
641            fsSB.append(ts);
642        }
643        ts = fsSB.toString();
644        return (ts);
645    }
646
647    /*
648     * This method collects the current file name used by this Logger.
649     *
650     *@param logName an identifier and is a part of file name to be read.
651     *@param logType the components of filename to be read.
652     *@param userCrdential user credential for authorization check.
653     *@return name of the current logfile associated with this logname.
654     *        returns null, if there is no file or invalid input params.
655     *@throws IOException if interrupted or failed to do I/O.
656     *@throws NoSuchFieldException if invalid field has been specified.
657     *@throws IllegalArgumentException when inappropriate argument specified.
658     *@throws RuntimeException when it has been caught in any phase.
659     *@throws Exception if unrecoverable problem occurs, that is beyond
660     *         its control.
661     */
662    private static String getCurrentFile(String logname,
663        String logtype,
664        Object userCrdential
665    ) throws IOException, NoSuchFieldException,
666        IllegalArgumentException,
667        RuntimeException, Exception
668    {
669        // sets loggerName
670        setLoggerName(logname,logtype);
671        Logger logInstance =
672            (com.sun.identity.log.Logger)Logger.getLogger(loggerName);
673        try {
674            logFileName = logInstance.getCurrentFile();
675        } catch ( RuntimeException e) {
676            Debug.error("LogReader:getCurrentFile:" +
677                "RuntimeException: ", e);
678            logFileName = null;
679            throw e;
680        }
681        return logFileName;
682    }
683    
684    /*
685     *This method checks whether this user/ role does have the required
686     *(here reading) permission on the resourse.
687     *It uses spi based interface to check it. It returns true when allowed
688     *else false.
689     *
690     *@param userCredential contains the user/ role details required to check
691     *       the authorization.
692     */
693    private boolean isAllowed(Object userCredential) throws Exception {
694        boolean isAuthorized = false;
695        try {
696            isAuthorized =
697            com.sun.identity.log.spi.Authorizer.isAuthorized(
698            loggerName,"READ",
699            userCredential);
700        } catch (Exception e) {
701            Debug.error("LogReader:isAllowed:" + "Exception: ", e);
702            throw e;
703        }
704        return isAuthorized;
705    }
706    
707    /*
708     *This method is to read configuration details it needs to perform
709     *its task. LogManager always has the updated configuration information.
710     *So this method goes to read its well defined property from LogManager.
711     *This method should be called before each read request to ensure the
712     *latest configuration has been used.
713     */
714    private void readConfiguration() throws Exception {
715        cleanup();  // first cleans up all previous setting
716        try {
717            logPathName = manager.getProperty(LogConstants.LOG_LOCATION);
718            if (logPathName == null) {
719                Debug.error("LogReader:readConfiguration:" +
720                    "unable to get log location");
721                return;
722            }
723
724            logStorageType = manager.getProperty(LogConstants.BACKEND);
725            maxRecStr = manager.getProperty(LogConstants.MAX_RECORDS);
726
727            if ((maxRecStr == null) || (maxRecStr.length() == 0)) {
728                maxRecStr = LogConstants.MAX_RECORDS_DEFAULT;
729            }
730
731            logFields = manager.getProperty(LogConstants.LOG_FIELDS);
732
733            /* depending on whether "File" or "DB" backend... */
734            logTypeIsFile = true;
735            if (logStorageType.equals("File")) {
736                fileHandlerClass =
737                    manager.getProperty(LogConstants.FILE_READ_HANDLER);
738                if (!(logPathName.endsWith(File.separator))) {
739                    logPathName = logPathName + File.separator;
740                }
741                logSecurity =
742                    manager.getProperty(LogConstants.SECURITY_STATUS);
743            } else {
744                logTypeIsFile = false;
745                dbHandlerClass =
746                    manager.getProperty(LogConstants.DB_READ_HANDLER);
747            }
748        } catch (Exception e) {
749            Debug.error("LogReader:readConfiguration:" + "Exception: ", e);
750            throw e; // rethrow the exception as it is
751        }
752        try {
753            /* check type of log backend and instantiate appropriate object */
754            if (logStorageType.compareToIgnoreCase(FILE_SOURCE) == 0 ) {
755                Class clz = Class.forName(fileHandlerClass);
756                currentHandler =(com.sun.identity.log.handlers.LogReadHandler)
757                clz.newInstance();
758            } else if (logStorageType.compareToIgnoreCase("DB") == 0 ) {
759                /*  FOR DB Handler */
760                Class clz = Class.forName(dbHandlerClass);
761                // Following type casting has to be changed for DB support
762                currentDBHandler =
763                    (com.sun.identity.log.handlers.LogReadDBHandler)
764                        clz.newInstance();
765            }
766        } catch (Exception e) {
767            Debug.error("LogReader:readConfiguration:" + "Exception: ", e);
768            throw e; /* rethrow the exception as it is */
769        }
770        try {
771            if (maxRecStr != null) {
772                maxRecStr = maxRecStr.trim();
773                maxReordToReturn = Integer.parseInt(maxRecStr);
774            }
775        } catch (Exception ex) {
776            Debug.error("LogReader:readConfiguration:" + "Exception: ", ex);
777            throw ex; // rethrow the exception as it is
778        }
779        return;
780    }
781    
782    /*
783     *setLoggerName method set the logname i.e. logger name from filename.
784     *@param filename name of the file to read.
785     */
786    private static void setLoggerName(String name) {
787        /* checks for security prefix, if present removes it. */
788        if (name.startsWith(securityPrefix) == true) {
789            name = name.substring(securityPrefix.length()+1,name.length());
790        }
791        
792        /* check for time stamp, if present removes it. */
793        int datePos = name.lastIndexOf(".");
794        int strLen  = name.length();
795        ++datePos;
796        String dateCheck = name.substring(datePos+1,strLen);
797        try {
798            Long longTm = new Long(dateCheck);
799            long dateValue = longTm.longValue();
800            if (dateValue >= 1) {
801                /* valid time present so remove it to get file name */
802                name = name.substring(0,datePos-1);
803            }
804        } catch (Exception e) {
805            /*
806             * exception suggests the log file name doesn't have time stamp
807             * attached so loggerName = name.
808             * do nothing
809             */
810        }
811        loggerName = name;
812    }
813    
814    /*
815     *setLoggerName method set the logname i.e. logger name from filename.
816     *@param logname 1st part (if has 2 parts) of the logger name.
817     *@param logtype 2nd part (if any) of the logger name
818     */
819    private static void setLoggerName(String logname, String logtype) {
820        /* checks for security prefix, if present removes it. */
821        if (logname != null) {
822            loggerName = logname;
823        }
824        
825        if (logtype != null) {
826            if (logname != null) {
827                loggerName += ".";
828            }
829            loggerName += logtype;
830        }
831    }
832    
833    /*This method clears all the stored information and previous result
834     *to make sure there is no effect from previous query. It should be
835     *be called before reading configuration.
836     */
837    private void cleanup() {
838        this.maxReordToReturn = LogQuery.MOST_RECENT_MAX_RECORDS;
839        this.queryResult = null;
840        this.currentHandler = null;
841        return;
842    }
843
844    public static boolean isLogSecure() {
845        return (logSecurity.equalsIgnoreCase("ON"));
846    }
847}