Home » geronimo-2.2-source-release » org.apache.geronimo.derby » [javadoc | source]

    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.BufferedReader;
   23   import java.io.File;
   24   import java.io.FileInputStream;
   25   import java.io.IOException;
   26   import java.io.InputStreamReader;
   27   import java.util.LinkedList;
   28   import java.util.List;
   29   import java.util.regex.Matcher;
   30   import java.util.regex.Pattern;
   31   import java.util.regex.PatternSyntaxException;
   32   
   33   /**
   34    * ReplaceMe
   35    *
   36    * @version $Rev: 681472 $ $Date: 2008-07-31 13:28:03 -0700 (Thu, 31 Jul 2008) $
   37    */
   38   public class DerbyLogGBean implements DerbyLog {
   39   
   40       private final DerbySystem derby;
   41       private File logFile = null;
   42   
   43       public DerbyLogGBean(DerbySystem derby) {
   44           this.derby = derby;
   45       }
   46   
   47       public SearchResults searchLog(Integer startLine, Integer endLine, Integer max, String text) {
   48           // Get log file
   49           if(logFile == null) {
   50               logFile = new File(derby.getDerbyHome(), "derby.log");
   51               if(!logFile.canRead()) {
   52                   throw new IllegalStateException("Cannot read Derby log file at '"+logFile.getAbsolutePath()+"'");
   53               }
   54           }
   55           // Check that the text pattern is valid
   56           Pattern textPattern;
   57           try {
   58               textPattern = text == null || text.equals("") ? null : Pattern.compile(text);
   59           } catch (PatternSyntaxException e) {
   60               throw new IllegalArgumentException("Bad regular expression '"+text+"'");
   61           }
   62           return searchFile(logFile, textPattern, startLine, endLine,
   63                   max == null ? MAX_SEARCH_RESULTS : Math.min(MAX_SEARCH_RESULTS, max.intValue()));
   64       }
   65   
   66       private static SearchResults searchFile(File file, Pattern textSearch, Integer start, Integer stop, int max) {
   67           List list = new LinkedList();
   68           boolean capped = false;
   69           int lineCount = 0;
   70           FileInputStream logInputStream = null;
   71           try {
   72               logInputStream = new FileInputStream(file);
   73               BufferedReader reader = new BufferedReader(new InputStreamReader(logInputStream, "US-ASCII"));
   74               Matcher text = textSearch == null ? null : textSearch.matcher("");
   75               max = Math.min(max, MAX_SEARCH_RESULTS);
   76               String line;
   77               while ((line = reader.readLine()) != null) {
   78                   ++lineCount;
   79                   if(start != null && start.intValue() > lineCount) {
   80                       continue;
   81                   }
   82                   if(stop != null && stop.intValue() < lineCount) {
   83                       continue;
   84                   }
   85                   if(text != null) {
   86                       text.reset(line);
   87                       if(!text.find()) {
   88                           continue;
   89                       }
   90                   }
   91                   list.add(new LogMessage(lineCount,line.toString()));
   92                   if(list.size() > max) {
   93                       list.remove(0);
   94                       capped = true;
   95                   }
   96               }
   97   
   98           } catch (Exception e) {            
   99               // 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   }

Home » geronimo-2.2-source-release » org.apache.geronimo.derby » [javadoc | source]