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 package org.apache.geronimo.derby; 018 019 import org.apache.geronimo.gbean.GBeanInfo; 020 import org.apache.geronimo.gbean.GBeanInfoBuilder; 021 022 import java.io.BufferedReader; 023 import java.io.File; 024 import java.io.FileInputStream; 025 import java.io.IOException; 026 import java.io.InputStreamReader; 027 import java.util.LinkedList; 028 import java.util.List; 029 import java.util.regex.Matcher; 030 import java.util.regex.Pattern; 031 import java.util.regex.PatternSyntaxException; 032 033 /** 034 * ReplaceMe 035 * 036 * @version $Rev: 706640 $ $Date: 2008-10-21 14:44:05 +0000 (Tue, 21 Oct 2008) $ 037 */ 038 public class DerbyLogGBean implements DerbyLog { 039 040 private final DerbySystem derby; 041 private File logFile = null; 042 043 public DerbyLogGBean(DerbySystem derby) { 044 this.derby = derby; 045 } 046 047 public SearchResults searchLog(Integer startLine, Integer endLine, Integer max, String text) { 048 // Get log file 049 if(logFile == null) { 050 logFile = new File(derby.getDerbyHome(), "derby.log"); 051 if(!logFile.canRead()) { 052 throw new IllegalStateException("Cannot read Derby log file at '"+logFile.getAbsolutePath()+"'"); 053 } 054 } 055 // Check that the text pattern is valid 056 Pattern textPattern; 057 try { 058 textPattern = text == null || text.equals("") ? null : Pattern.compile(text); 059 } catch (PatternSyntaxException e) { 060 throw new IllegalArgumentException("Bad regular expression '"+text+"'"); 061 } 062 return searchFile(logFile, textPattern, startLine, endLine, 063 max == null ? MAX_SEARCH_RESULTS : Math.min(MAX_SEARCH_RESULTS, max.intValue())); 064 } 065 066 private static SearchResults searchFile(File file, Pattern textSearch, Integer start, Integer stop, int max) { 067 List list = new LinkedList(); 068 boolean capped = false; 069 int lineCount = 0; 070 FileInputStream logInputStream = null; 071 try { 072 logInputStream = new FileInputStream(file); 073 BufferedReader reader = new BufferedReader(new InputStreamReader(logInputStream, "US-ASCII")); 074 Matcher text = textSearch == null ? null : textSearch.matcher(""); 075 max = Math.min(max, MAX_SEARCH_RESULTS); 076 String line; 077 while ((line = reader.readLine()) != null) { 078 ++lineCount; 079 if(start != null && start.intValue() > lineCount) { 080 continue; 081 } 082 if(stop != null && stop.intValue() < lineCount) { 083 continue; 084 } 085 if(text != null) { 086 text.reset(line); 087 if(!text.find()) { 088 continue; 089 } 090 } 091 list.add(new LogMessage(lineCount,line.toString())); 092 if(list.size() > max) { 093 list.remove(0); 094 capped = true; 095 } 096 } 097 098 } catch (Exception e) { 099 // TODO: improve exception handling 100 } finally { 101 if (logInputStream != null) { 102 try { 103 logInputStream.close(); 104 } catch (IOException e) { 105 // ignore 106 } 107 } 108 } 109 return new SearchResults(lineCount, (LogMessage[]) list.toArray(new LogMessage[list.size()]), capped); 110 } 111 public static final GBeanInfo GBEAN_INFO; 112 113 static { 114 GBeanInfoBuilder infoFactory = GBeanInfoBuilder.createStatic("Derby Log", DerbyLogGBean.class); 115 116 infoFactory.addReference("DerbySystem", DerbySystem.class, "GBean"); 117 infoFactory.addInterface(DerbyLog.class); 118 infoFactory.setConstructor(new String[]{"DerbySystem"}); 119 120 GBEAN_INFO = infoFactory.getBeanInfo(); 121 } 122 123 public static GBeanInfo getGBeanInfo() { 124 return GBEAN_INFO; 125 } 126 }