001    /**
002     *  Licensed to the Apache Software Foundation (ASF) under one or more
003     *  contributor license agreements.  See the NOTICE file distributed with
004     *  this work for additional information regarding copyright ownership.
005     *  The ASF licenses this file to You under the Apache License, Version 2.0
006     *  (the "License"); you may not use this file except in compliance with
007     *  the License.  You may obtain a copy of the License at
008     *
009     *     http://www.apache.org/licenses/LICENSE-2.0
010     *
011     *  Unless required by applicable law or agreed to in writing, software
012     *  distributed under the License is distributed on an "AS IS" BASIS,
013     *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     *  See the License for the specific language governing permissions and
015     *  limitations under the License.
016     */
017    
018    package org.apache.geronimo.console.derbylogmanager;
019    
020    import java.io.BufferedReader;
021    import java.io.File;
022    import java.io.FileReader;
023    import java.io.IOException;
024    import java.io.InputStreamReader;
025    import java.util.ArrayList;
026    import java.util.Collection;
027    import java.util.Stack;
028    
029    import org.apache.geronimo.console.util.KernelHelper;
030    
031    import org.apache.geronimo.console.internaldb.DerbyConnectionUtil;
032    
033    public class DerbyLogHelper extends KernelHelper {
034        private static ArrayList logs = new ArrayList();
035    
036        private static boolean cached = false;
037    
038        private static int lineCount = 0;
039    
040        private static final String LOG_FILENAME = "derby.log";
041    
042        public static Collection getLogs() throws IOException {
043            if (!cached) {
044                refresh();
045            }
046            return logs;
047        }
048    
049        public static void refresh() throws IOException {
050            cached = false;
051            logs.clear();
052            BufferedReader in = new BufferedReader(getFileReader());
053            lineCount = 0;
054            Stack holder = new Stack();
055            for (String line = in.readLine(); line != null; line = in.readLine()) {
056                holder.push(line);
057                lineCount++;
058            }
059            logs.addAll(holder);
060            cached = true;
061        }
062    
063        public static int getLineCount() {
064            return lineCount;
065        }
066    
067        private static InputStreamReader getFileReader() throws IOException {
068            String pathToFile = getSystemHome() + File.separator + LOG_FILENAME;
069            return new FileReader(new File(pathToFile));
070        }
071    
072        public static String getSystemHome() {
073            return DerbyConnectionUtil.getDerbyHome();
074        }
075    
076    }