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.plugins;
19
20 import org.apache.log4j.spi.ComponentBase;
21 import org.apache.log4j.spi.LoggerRepository;
22
23 import java.beans.PropertyChangeEvent;
24 import java.beans.PropertyChangeListener;
25 import java.beans.PropertyChangeSupport;
26
27
28 /**
29 * A convienent abstract class for plugin subclasses that implements
30 * the basic methods of the Plugin interface. Subclasses are required
31 * to implement the isActive(), activateOptions(), and shutdown()
32 * methods.
33 * <p></p>
34 * <p>Developers are not required to subclass PluginSkeleton to
35 * develop their own plugins (they are only required to implement the
36 * Plugin interface), but it provides a convenient base class to start
37 * from.
38 * <p></p>
39 * Contributors: Nicko Cadell
40 *
41 * @author Mark Womack (mwomack@apache.org)
42 * @author Paul Smith (psmith@apache.org)
43 */
44 public abstract class PluginSkeleton extends ComponentBase implements Plugin {
45 /**
46 * Name of this plugin.
47 */
48 protected String name = "plugin";
49
50 /**
51 * Active state of plugin.
52 */
53 protected boolean active;
54
55 /**
56 * This is a delegate that does all the PropertyChangeListener
57 * support.
58 */
59 private PropertyChangeSupport propertySupport =
60 new PropertyChangeSupport(this);
61
62 /**
63 * Construct new instance.
64 */
65 protected PluginSkeleton() {
66 super();
67 }
68
69 /**
70 * Gets the name of the plugin.
71 *
72 * @return String the name of the plugin.
73 */
74 public String getName() {
75 return name;
76 }
77
78 /**
79 * Sets the name of the plugin and notifies
80 * PropertyChangeListeners of the change.
81 *
82 * @param newName the name of the plugin to set.
83 */
84 public void setName(final String newName) {
85 String oldName = this.name;
86 this.name = newName;
87 propertySupport.firePropertyChange("name", oldName, this.name);
88 }
89
90 /**
91 * Gets the logger repository for this plugin.
92 *
93 * @return LoggerRepository the logger repository this plugin will affect.
94 */
95 public LoggerRepository getLoggerRepository() {
96 return repository;
97 }
98
99 /**
100 * Sets the logger repository used by this plugin and notifies a
101 * relevant PropertyChangeListeners registered. This
102 * repository will be used by the plugin functionality.
103 *
104 * @param repository the logger repository that this plugin should affect.
105 */
106 public void setLoggerRepository(final LoggerRepository repository) {
107 Object oldValue = this.repository;
108 this.repository = repository;
109 firePropertyChange("loggerRepository", oldValue, this.repository);
110 }
111
112 /**
113 * Returns whether this plugin is Active or not.
114 *
115 * @return true/false
116 */
117 public synchronized boolean isActive() {
118 return active;
119 }
120
121 /**
122 * Returns true if the plugin has the same name and logger repository as the
123 * testPlugin passed in.
124 *
125 * @param testPlugin The plugin to test equivalency against.
126 * @return Returns true if testPlugin is considered to be equivalent.
127 */
128 public boolean isEquivalent(final Plugin testPlugin) {
129 return (repository == testPlugin.getLoggerRepository())
130 && ((this.name == null && testPlugin.getName() == null)
131 || (this.name != null
132 && name.equals(testPlugin.getName())))
133 && this.getClass().equals(testPlugin.getClass());
134 }
135
136 /**
137 * Add property change listener.
138 * @param listener listener.
139 */
140 public final void addPropertyChangeListener(
141 final PropertyChangeListener listener) {
142 propertySupport.addPropertyChangeListener(listener);
143 }
144
145 /**
146 * Add property change listener for one property only.
147 * @param propertyName property name.
148 * @param listener listener.
149 */
150 public final void addPropertyChangeListener(
151 final String propertyName,
152 final PropertyChangeListener listener) {
153 propertySupport.addPropertyChangeListener(propertyName, listener);
154 }
155
156 /**
157 * Remove property change listener.
158 * @param listener listener.
159 */
160 public final void removePropertyChangeListener(
161 final PropertyChangeListener listener) {
162 propertySupport.removePropertyChangeListener(listener);
163 }
164
165 /**
166 * Remove property change listener on a specific property.
167 * @param propertyName property name.
168 * @param listener listener.
169 */
170 public final void removePropertyChangeListener(
171 final String propertyName,
172 final PropertyChangeListener listener) {
173 propertySupport.removePropertyChangeListener(propertyName, listener);
174 }
175
176 /**
177 * Fire a property change event to appropriate listeners.
178 * @param evt change event.
179 */
180 protected final void firePropertyChange(
181 final PropertyChangeEvent evt) {
182 propertySupport.firePropertyChange(evt);
183 }
184
185 /**
186 * Fire property change event to appropriate listeners.
187 * @param propertyName property name.
188 * @param oldValue old value.
189 * @param newValue new value.
190 */
191 protected final void firePropertyChange(
192 final String propertyName,
193 final boolean oldValue,
194 final boolean newValue) {
195 propertySupport.firePropertyChange(propertyName, oldValue, newValue);
196 }
197
198 /**
199 * Fire property change event to appropriate listeners.
200 * @param propertyName property name.
201 * @param oldValue old value.
202 * @param newValue new value.
203 */
204 protected final void firePropertyChange(
205 final String propertyName,
206 final int oldValue, final int newValue) {
207 propertySupport.firePropertyChange(propertyName, oldValue, newValue);
208 }
209
210 /**
211 * Fire property change event to appropriate listeners.
212 * @param propertyName property name.
213 * @param oldValue old value.
214 * @param newValue new value.
215 */
216 protected final void firePropertyChange(
217 final String propertyName,
218 final Object oldValue,
219 final Object newValue) {
220 propertySupport.firePropertyChange(propertyName, oldValue, newValue);
221 }
222 }