001: /**********************************************************************************
002: * $URL: $
003: * $Id: $
004: ***********************************************************************************
005: *
006: * Copyright (c) 2003, 2004, 2005, 2006 The Sakai Foundation.
007: *
008: * Licensed under the Educational Community License, Version 1.0 (the "License");
009: * you may not use this file except in compliance with the License.
010: * You may obtain a copy of the License at
011: *
012: * http://www.opensource.org/licenses/ecl1.php
013: *
014: * Unless required by applicable law or agreed to in writing, software
015: * distributed under the License is distributed on an "AS IS" BASIS,
016: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
017: * See the License for the specific language governing permissions and
018: * limitations under the License.
019: *
020: **********************************************************************************/package org.sakaiproject.component.osid.logging;
021:
022: /**
023: *
024: * @inheritDoc
025: *
026: */
027: public class ReadableLog implements org.osid.logging.ReadableLog {
028: org.osid.OsidContext context = null;
029: java.util.Map configuration = null;
030: java.io.BufferedReader file = null;
031: private String fileExtension = null;
032: private String displayName = null;
033: private org.osid.shared.Id id = null;
034:
035: public String getDisplayName()
036: throws org.osid.logging.LoggingException {
037: return this .displayName;
038: }
039:
040: public org.osid.shared.Id getId()
041: throws org.osid.logging.LoggingException {
042: return this .id;
043: }
044:
045: public void initialize() throws org.osid.logging.LoggingException {
046: if (configuration == null) {
047: throw new org.osid.logging.LoggingException(
048: org.osid.logging.LoggingException.CONFIGURATION_ERROR);
049: }
050: fileExtension = (String) configuration.get("FileExtension");
051: if (fileExtension == null) {
052: throw new org.osid.logging.LoggingException(
053: org.osid.logging.LoggingException.CONFIGURATION_ERROR);
054: }
055: }
056:
057: public ReadableLog(org.osid.OsidContext context,
058: java.util.Map configuration, String logName)
059: throws org.osid.logging.LoggingException {
060: if (logName == null) {
061: throw new org.osid.logging.LoggingException(
062: org.osid.logging.LoggingException.NULL_ARGUMENT);
063: }
064: this .context = context;
065: this .configuration = configuration;
066: initialize();
067: try {
068: this .displayName = logName;
069: openFile();
070: } catch (org.osid.OsidException oex) {
071: throw new org.osid.logging.LoggingException(
072: org.osid.logging.LoggingException.OPERATION_FAILED);
073: }
074: }
075:
076: public org.osid.logging.EntryIterator getEntries(
077: org.osid.shared.Type formatType,
078: org.osid.shared.Type priorityType)
079: throws org.osid.logging.LoggingException {
080: try {
081: java.util.Vector v = new java.util.Vector();
082: String line = "";
083: openFile();
084:
085: while ((line = file.readLine()) != null) {
086: org.osid.logging.Entry entry = new Entry(line);
087: if ((formatType.isEqual(entry.getFormatType()))
088: && (priorityType.isEqual(entry
089: .getPriorityType()))) {
090: v.addElement(entry);
091: }
092: }
093: file.close();
094: return (new EntryIterator(v));
095: } catch (Exception oex) {
096: throw new org.osid.logging.LoggingException(
097: org.osid.logging.LoggingException.OPERATION_FAILED);
098: }
099: }
100:
101: private void openFile() throws org.osid.logging.LoggingException {
102: try {
103: try {
104: file = new java.io.BufferedReader(
105: new java.io.FileReader(this .displayName
106: + fileExtension));
107: } catch (java.io.FileNotFoundException fnfex) {
108: throw new org.osid.logging.LoggingException(
109: org.osid.logging.LoggingException.UNKNOWN_NAME);
110: }
111: } catch (org.osid.OsidException oex) {
112: throw new org.osid.logging.LoggingException(
113: org.osid.logging.LoggingException.OPERATION_FAILED);
114: }
115: }
116: }
|