1 17 package org.apache.geronimo.derby; 18 19 import org.apache.geronimo.gbean.GBeanInfo; 20 import org.apache.geronimo.gbean.GBeanInfoBuilder; 21 22 import java.io.File ; 23 import java.io.RandomAccessFile ; 24 import java.nio.CharBuffer ; 25 import java.nio.MappedByteBuffer ; 26 import java.nio.channels.FileChannel ; 27 import java.nio.charset.Charset ; 28 import java.util.LinkedList ; 29 import java.util.List ; 30 import java.util.regex.Matcher ; 31 import java.util.regex.Pattern ; 32 import java.util.regex.PatternSyntaxException ; 33 34 39 public class DerbyLogGBean implements DerbyLog { 40 private final static Pattern FULL_LINE_PATTERN = Pattern.compile("^.*", Pattern.MULTILINE); 42 private final DerbySystem derby; 43 private File logFile = null; 44 45 public DerbyLogGBean(DerbySystem derby) { 46 this.derby = derby; 47 } 48 49 public SearchResults searchLog(Integer startLine, Integer endLine, Integer max, String text) { 50 if(logFile == null) { 52 logFile = new File (derby.getDerbyHome(), "derby.log"); 53 if(!logFile.canRead()) { 54 throw new IllegalStateException ("Cannot read Derby log file at '"+logFile.getAbsolutePath()+"'"); 55 } 56 } 57 Pattern textPattern; 59 try { 60 textPattern = text == null || text.equals("") ? null : Pattern.compile(text); 61 } catch (PatternSyntaxException e) { 62 throw new IllegalArgumentException ("Bad regular expression '"+text+"'"); 63 } 64 return searchFile(logFile, textPattern, startLine, endLine, 65 max == null ? MAX_SEARCH_RESULTS : Math.min(MAX_SEARCH_RESULTS, max.intValue())); 66 } 67 68 private static SearchResults searchFile(File file, Pattern textSearch, Integer start, Integer stop, int max) { 69 List list = new LinkedList (); 70 boolean capped = false; 71 int lineCount = 0; 72 try { 73 RandomAccessFile raf = new RandomAccessFile (file, "r"); 74 FileChannel fc = raf.getChannel(); 75 MappedByteBuffer bb = fc.map(FileChannel.MapMode.READ_ONLY, 0, fc.size()); 76 CharBuffer cb = Charset.forName("US-ASCII").decode(bb); Matcher lines = FULL_LINE_PATTERN.matcher(cb); 78 Matcher text = textSearch == null ? null : textSearch.matcher(""); 79 max = Math.min(max, MAX_SEARCH_RESULTS); 80 while(lines.find()) { 81 ++lineCount; 82 if(start != null && start.intValue() > lineCount) { 83 continue; 84 } 85 if(stop != null && stop.intValue() < lineCount) { 86 continue; 87 } 88 CharSequence line = cb.subSequence(lines.start(), lines.end()); 89 if(text != null) { 90 text.reset(line); 91 if(!text.find()) { 92 continue; 93 } 94 } 95 list.add(new LogMessage(lineCount,line.toString())); 96 if(list.size() > max) { 97 list.remove(0); 98 capped = true; 99 } 100 } 101 fc.close(); 102 raf.close(); 103 } catch (Exception e) {} 104 return new SearchResults(lineCount, (LogMessage[]) list.toArray(new LogMessage[list.size()]), capped); 105 } 106 public static final GBeanInfo GBEAN_INFO; 107 108 static { 109 GBeanInfoBuilder infoFactory = GBeanInfoBuilder.createStatic("Derby Log", DerbyLogGBean.class); 110 111 infoFactory.addReference("DerbySystem", DerbySystem.class, "GBean"); 112 infoFactory.addInterface(DerbyLog.class); 113 infoFactory.setConstructor(new String []{"DerbySystem"}); 114 115 GBEAN_INFO = infoFactory.getBeanInfo(); 116 } 117 118 public static GBeanInfo getGBeanInfo() { 119 return GBEAN_INFO; 120 } 121 } 122 | Popular Tags |