View Javadoc

1   /**
2    *
3    * Copyright 2005 The Apache Software Foundation
4    *
5    *  Licensed under the Apache License, Version 2.0 (the "License");
6    *  you may not use this file except in compliance with the License.
7    *  You may obtain a copy of the License at
8    *
9    *     http://www.apache.org/licenses/LICENSE-2.0
10   *
11   *  Unless required by applicable law or agreed to in writing, software
12   *  distributed under the License is distributed on an "AS IS" BASIS,
13   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   *  See the License for the specific language governing permissions and
15   *  limitations under the License.
16   */
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  /**
35   * ReplaceMe
36   *
37   * @version $Rev: 355877 $ $Date: 2005-12-10 18:48:27 -0800 (Sat, 10 Dec 2005) $
38   */
39  public class DerbyLogGBean implements DerbyLog {
40      // Pattern that matches a single line  (used to calculate line numbers and text search in a line)
41      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          // Get log file
51          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          // Check that the text pattern is valid
58          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); //todo: does Derby use a different charset on a foreign PC?
77              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 }