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