View Javadoc
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  
18  package org.apache.log4j.db;
19  
20  import org.apache.log4j.plugins.Pauseable;
21  import org.apache.log4j.plugins.Receiver;
22  import org.apache.log4j.scheduler.Scheduler;
23  import org.apache.log4j.spi.LoggerRepositoryEx;
24  import org.apache.log4j.xml.DOMConfigurator;
25  import org.apache.log4j.xml.UnrecognizedElementHandler;
26  import org.w3c.dom.Element;
27  
28  import java.util.Properties;
29  
30  /**
31   *
32   * @author Scott Deboy <sdeboy@apache.org>
33   * @author Ceki Gülcü
34   *
35   */
36  public class DBReceiver extends Receiver implements Pauseable, UnrecognizedElementHandler {
37    /**
38     * By default we refresh data every 1000 milliseconds.
39     * @see #setRefreshMillis
40     */
41    static int DEFAULT_REFRESH_MILLIS = 1000;
42    ConnectionSource connectionSource;
43    int refreshMillis = DEFAULT_REFRESH_MILLIS;
44    DBReceiverJob receiverJob;
45    boolean paused = false;
46  
47    public void activateOptions() {
48      
49      if(connectionSource == null)  {
50        throw new IllegalStateException(
51          "DBAppender cannot function without a connection source");
52      }
53    
54      receiverJob = new DBReceiverJob(this);
55      receiverJob.setLoggerRepository(repository);
56        
57      if(this.repository == null) {
58        throw new IllegalStateException(
59        "DBAppender cannot function without a reference to its owning repository");
60      }
61  
62      if (repository instanceof LoggerRepositoryEx) {
63          Scheduler scheduler = ((LoggerRepositoryEx) repository).getScheduler();
64      
65          scheduler.schedule(
66              receiverJob, System.currentTimeMillis() + 500, refreshMillis);
67      }
68     
69    }
70  
71    public void setRefreshMillis(int refreshMillis) {
72      this.refreshMillis = refreshMillis;
73    }
74  
75    public int getRefreshMillis() {
76      return refreshMillis;
77    }
78  
79  
80    /**
81     * @return Returns the connectionSource.
82     */
83    public ConnectionSource getConnectionSource() {
84      return connectionSource;
85    }
86  
87  
88    /**
89     * @param connectionSource The connectionSource to set.
90     */
91    public void setConnectionSource(ConnectionSource connectionSource) {
92      this.connectionSource = connectionSource;
93    }
94  
95  
96    /* (non-Javadoc)
97     * @see org.apache.log4j.plugins.Plugin#shutdown()
98     */
99    public void shutdown() {
100     getLogger().info("removing receiverJob from the Scheduler.");
101 
102     if(this.repository instanceof LoggerRepositoryEx) {
103       Scheduler scheduler = ((LoggerRepositoryEx) repository).getScheduler();
104       scheduler.delete(receiverJob);
105     }
106   }
107 
108 
109   /* (non-Javadoc)
110    * @see org.apache.log4j.plugins.Pauseable#setPaused(boolean)
111    */
112   public void setPaused(boolean paused) {
113     this.paused = paused;
114   }
115 
116   /* (non-Javadoc)
117    * @see org.apache.log4j.plugins.Pauseable#isPaused()
118    */
119   public boolean isPaused() {
120     return paused;
121   }
122 
123     /**
124      * {@inheritDoc}
125      */
126   public boolean parseUnrecognizedElement(Element element, Properties props) throws Exception {
127         if ("connectionSource".equals(element.getNodeName())) {
128             Object instance =
129                     DOMConfigurator.parseElement(element, props, ConnectionSource.class);
130             if (instance instanceof ConnectionSource) {
131                 ConnectionSource source = (ConnectionSource) instance;
132                 source.activateOptions();
133                 setConnectionSource(source);
134             }
135             return true;
136         }
137         return false;
138   }
139 
140 }