View Javadoc

1   /************************************************************************
2    * Copyright (c) 2000-2006 The Apache Software Foundation.             *
3    * All rights reserved.                                                *
4    * ------------------------------------------------------------------- *
5    * Licensed under the Apache License, Version 2.0 (the "License"); you *
6    * may not use this file except in compliance with the License. You    *
7    * 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     *
14   * implied.  See the License for the specific language governing       *
15   * permissions and limitations under the License.                      *
16   ***********************************************************************/
17  
18  package org.apache.james.transport.matchers;
19  
20  import java.io.IOException;
21  
22  import org.apache.oro.text.regex.MalformedPatternException;
23  import org.apache.mailet.RFC2822Headers;
24  import javax.mail.MessagingException;
25  
26  /***
27   * Initializes RegexMatcher with regular expressions from a file.
28   *
29   */
30  public class FileRegexMatcher extends GenericRegexMatcher {
31      public void init() throws MessagingException {
32          java.io.RandomAccessFile patternSource = null;
33          try {
34              patternSource = new java.io.RandomAccessFile(getCondition(), "r");
35              int lines = 0;
36              while(patternSource.readLine() != null) lines++;
37              patterns = new Object[lines][2];
38              patternSource.seek(0);
39              for (int i = 0; i < lines; i++) {
40                  String line = patternSource.readLine();
41                  patterns[i][0] = line.substring(0, line.indexOf(':'));
42                  patterns[i][1] = line.substring(line.indexOf(':')+1);
43              }          
44              compile(patterns);
45                    
46          }
47          catch (java.io.FileNotFoundException fnfe) {
48              throw new MessagingException("Could not locate patterns.", fnfe);
49          }
50          catch (java.io.IOException ioe) {
51              throw new MessagingException("Could not read patterns.", ioe);
52          }
53          catch(MalformedPatternException mp) {
54              throw new MessagingException("Could not initialize regex patterns", mp);
55          } finally {
56              if (patternSource != null) {
57                  // close the file
58                  try {
59                      patternSource.close();
60                  } catch (IOException e) {
61                      // just ignore on close
62                  }
63              }
64          }
65      }
66  }