View Javadoc

1   /*****************************************************************
2    * Licensed to the Apache Software Foundation (ASF) under one   *
3    * or more contributor license agreements.  See the NOTICE file *
4    * distributed with this work for additional information        *
5    * regarding copyright ownership.  The ASF licenses this file   *
6    * to you under the Apache License, Version 2.0 (the            *
7    * "License"); you may not use this file except in compliance   *
8    * with the License.  You may obtain a copy of the License at   *
9    *                                                              *
10   *   http://www.apache.org/licenses/LICENSE-2.0                 *
11   *                                                              *
12   * Unless required by applicable law or agreed to in writing,   *
13   * software distributed under the License is distributed on an  *
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY       *
15   * KIND, either express or implied.  See the License for the    *
16   * specific language governing permissions and limitations      *
17   * under the License.                                           *
18   ****************************************************************/
19  
20  package org.apache.james.transport.matchers;
21  
22  import java.io.IOException;
23  
24  import org.apache.oro.text.regex.MalformedPatternException;
25  import org.apache.mailet.RFC2822Headers;
26  import javax.mail.MessagingException;
27  
28  /***
29   * Initializes RegexMatcher with regular expressions from a file.
30   *
31   */
32  public class FileRegexMatcher extends GenericRegexMatcher {
33      public void init() throws MessagingException {
34          java.io.RandomAccessFile patternSource = null;
35          try {
36              patternSource = new java.io.RandomAccessFile(getCondition(), "r");
37              int lines = 0;
38              while(patternSource.readLine() != null) lines++;
39              patterns = new Object[lines][2];
40              patternSource.seek(0);
41              for (int i = 0; i < lines; i++) {
42                  String line = patternSource.readLine();
43                  patterns[i][0] = line.substring(0, line.indexOf(':'));
44                  patterns[i][1] = line.substring(line.indexOf(':')+1);
45              }          
46              compile(patterns);
47                    
48          }
49          catch (java.io.FileNotFoundException fnfe) {
50              throw new MessagingException("Could not locate patterns.", fnfe);
51          }
52          catch (java.io.IOException ioe) {
53              throw new MessagingException("Could not read patterns.", ioe);
54          }
55          catch(MalformedPatternException mp) {
56              throw new MessagingException("Could not initialize regex patterns", mp);
57          } finally {
58              if (patternSource != null) {
59                  // close the file
60                  try {
61                      patternSource.close();
62                  } catch (IOException e) {
63                      // just ignore on close
64                  }
65              }
66          }
67      }
68  }