View Javadoc

1   /**
2    * Logback: the generic, reliable, fast and flexible logging framework for Java.
3    * 
4    * Copyright (C) 2000-2006, QOS.ch
5    * 
6    * This library is free software, you can redistribute it and/or modify it under
7    * the terms of the GNU Lesser General Public License as published by the Free
8    * Software Foundation.
9    */
10  package ch.qos.logback.core.joran;
11  
12  import java.io.File;
13  import java.io.FileInputStream;
14  import java.io.IOException;
15  import java.io.InputStream;
16  import java.net.URL;
17  import java.util.List;
18  
19  import org.xml.sax.InputSource;
20  
21  import ch.qos.logback.core.joran.event.SaxEvent;
22  import ch.qos.logback.core.joran.event.SaxEventRecorder;
23  import ch.qos.logback.core.joran.spi.DefaultNestedComponentRegistry;
24  import ch.qos.logback.core.joran.spi.EventPlayer;
25  import ch.qos.logback.core.joran.spi.InterpretationContext;
26  import ch.qos.logback.core.joran.spi.Interpreter;
27  import ch.qos.logback.core.joran.spi.JoranException;
28  import ch.qos.logback.core.joran.spi.Pattern;
29  import ch.qos.logback.core.joran.spi.RuleStore;
30  import ch.qos.logback.core.joran.spi.SimpleRuleStore;
31  import ch.qos.logback.core.spi.ContextAwareBase;
32  
33  public abstract class GenericConfigurator extends ContextAwareBase {
34  
35    protected Interpreter interpreter;
36  
37    final public void doConfigure(URL url) throws JoranException {
38      try {
39        InputStream in = url.openStream();
40        doConfigure(in);
41        in.close();
42      } catch (IOException ioe) {
43        String errMsg = "Could not open URL [" + url + "].";
44        addError(errMsg, ioe);
45        throw new JoranException(errMsg, ioe);
46      }
47    }
48  
49    final public void doConfigure(String filename) throws JoranException {
50      doConfigure(new File(filename));
51    }
52  
53    final public void doConfigure(File file) throws JoranException {
54      FileInputStream fis = null;
55      try {
56        fis = new FileInputStream(file);
57        doConfigure(fis);
58      } catch (IOException ioe) {
59        String errMsg = "Could not open [" + file.getName() + "].";
60        addError(errMsg, ioe);
61        throw new JoranException(errMsg, ioe);
62      } finally {
63        if (fis != null) {
64          try {
65            fis.close();
66          } catch (java.io.IOException ioe) {
67            String errMsg = "Could not close [" + file.getName() + "].";
68            addError(errMsg, ioe);
69            throw new JoranException(errMsg, ioe);
70          }
71        }
72      }
73    }
74  
75    final public void doConfigure(InputStream inputStream) throws JoranException {
76      doConfigure(new InputSource(inputStream));
77    }
78  
79    abstract protected void addInstanceRules(RuleStore rs);
80  
81    abstract protected void addImplicitRules(Interpreter interpreter);
82  
83    protected void addDefaultNestedComponentRegistryRules(DefaultNestedComponentRegistry registry) {
84      
85    }
86    
87    protected Pattern initialPattern() {
88      return new Pattern();
89    }
90    
91    protected void buildInterpreter() {
92      RuleStore rs = new SimpleRuleStore(context);
93      addInstanceRules(rs);
94      this.interpreter = new Interpreter(context, rs, initialPattern());
95      InterpretationContext ec = interpreter.getInterpretationContext();
96      ec.setContext(context);
97      addImplicitRules(interpreter);
98      addDefaultNestedComponentRegistryRules(ec.getDefaultNestedComponentRegistry());
99    }
100 
101   final public void doConfigure(final InputSource inputSource)
102       throws JoranException {
103     SaxEventRecorder recorder = new SaxEventRecorder();
104     recorder.setContext(context);
105     recorder.recordEvents(inputSource);
106     buildInterpreter();
107     // disallow simultaneous configurations of the same context
108     synchronized (context) {
109       interpreter.play(recorder.saxEventList);
110     }
111   }
112 
113   public void doConfigure(final List<SaxEvent> eventList)
114       throws JoranException {
115     buildInterpreter();
116     EventPlayer player = new EventPlayer(interpreter);
117     player.play(eventList);
118   }
119 }