KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > geronimo > derby > DerbyLogGBean


1 /**
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. 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 JavaDoc;
23 import java.io.RandomAccessFile JavaDoc;
24 import java.nio.CharBuffer JavaDoc;
25 import java.nio.MappedByteBuffer JavaDoc;
26 import java.nio.channels.FileChannel JavaDoc;
27 import java.nio.charset.Charset JavaDoc;
28 import java.util.LinkedList JavaDoc;
29 import java.util.List JavaDoc;
30 import java.util.regex.Matcher JavaDoc;
31 import java.util.regex.Pattern JavaDoc;
32 import java.util.regex.PatternSyntaxException JavaDoc;
33
34 /**
35  * ReplaceMe
36  *
37  * @version $Rev: 476049 $ $Date: 2006-11-16 23:35:17 -0500 (Thu, 16 Nov 2006) $
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 JavaDoc FULL_LINE_PATTERN = Pattern.compile("^.*", Pattern.MULTILINE);
42     private final DerbySystem derby;
43     private File JavaDoc logFile = null;
44
45     public DerbyLogGBean(DerbySystem derby) {
46         this.derby = derby;
47     }
48
49     public SearchResults searchLog(Integer JavaDoc startLine, Integer JavaDoc endLine, Integer JavaDoc max, String JavaDoc text) {
50         // Get log file
51
if(logFile == null) {
52             logFile = new File JavaDoc(derby.getDerbyHome(), "derby.log");
53             if(!logFile.canRead()) {
54                 throw new IllegalStateException JavaDoc("Cannot read Derby log file at '"+logFile.getAbsolutePath()+"'");
55             }
56         }
57         // Check that the text pattern is valid
58
Pattern JavaDoc textPattern;
59         try {
60             textPattern = text == null || text.equals("") ? null : Pattern.compile(text);
61         } catch (PatternSyntaxException JavaDoc e) {
62             throw new IllegalArgumentException JavaDoc("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 JavaDoc file, Pattern JavaDoc textSearch, Integer JavaDoc start, Integer JavaDoc stop, int max) {
69         List JavaDoc list = new LinkedList JavaDoc();
70         boolean capped = false;
71         int lineCount = 0;
72         try {
73             RandomAccessFile JavaDoc raf = new RandomAccessFile JavaDoc(file, "r");
74             FileChannel JavaDoc fc = raf.getChannel();
75             MappedByteBuffer JavaDoc bb = fc.map(FileChannel.MapMode.READ_ONLY, 0, fc.size());
76             CharBuffer JavaDoc cb = Charset.forName("US-ASCII").decode(bb); //todo: does Derby use a different charset on a foreign PC?
77
Matcher JavaDoc lines = FULL_LINE_PATTERN.matcher(cb);
78             Matcher JavaDoc 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 JavaDoc 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 JavaDoc 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 JavaDoc[]{"DerbySystem"});
114
115         GBEAN_INFO = infoFactory.getBeanInfo();
116     }
117
118     public static GBeanInfo getGBeanInfo() {
119         return GBEAN_INFO;
120     }
121 }
122
Popular Tags