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 *
139 * @param listener listener.
140 */
141 public final void addPropertyChangeListener(
142 final PropertyChangeListener listener) {
143 propertySupport.addPropertyChangeListener(listener);
144 }
145
146 /**
147 * Add property change listener for one property only.
148 *
149 * @param propertyName property name.
150 * @param listener listener.
151 */
152 public final void addPropertyChangeListener(
153 final String propertyName,
154 final PropertyChangeListener listener) {
155 propertySupport.addPropertyChangeListener(propertyName, listener);
156 }
157
158 /**
159 * Remove property change listener.
160 *
161 * @param listener listener.
162 */
163 public final void removePropertyChangeListener(
164 final PropertyChangeListener listener) {
165 propertySupport.removePropertyChangeListener(listener);
166 }
167
168 /**
169 * Remove property change listener on a specific property.
170 *
171 * @param propertyName property name.
172 * @param listener listener.
173 */
174 public final void removePropertyChangeListener(
175 final String propertyName,
176 final PropertyChangeListener listener) {
177 propertySupport.removePropertyChangeListener(propertyName, listener);
178 }
179
180 /**
181 * Fire a property change event to appropriate listeners.
182 *
183 * @param evt change event.
184 */
185 protected final void firePropertyChange(
186 final PropertyChangeEvent evt) {
187 propertySupport.firePropertyChange(evt);
188 }
189
190 /**
191 * Fire property change event to appropriate listeners.
192 *
193 * @param propertyName property name.
194 * @param oldValue old value.
195 * @param newValue new value.
196 */
197 protected final void firePropertyChange(
198 final String propertyName,
199 final boolean oldValue,
200 final boolean newValue) {
201 propertySupport.firePropertyChange(propertyName, oldValue, newValue);
202 }
203
204 /**
205 * Fire property change event to appropriate listeners.
206 *
207 * @param propertyName property name.
208 * @param oldValue old value.
209 * @param newValue new value.
210 */
211 protected final void firePropertyChange(
212 final String propertyName,
213 final int oldValue, final int newValue) {
214 propertySupport.firePropertyChange(propertyName, oldValue, newValue);
215 }
216
217 /**
218 * Fire property change event to appropriate listeners.
219 *
220 * @param propertyName property name.
221 * @param oldValue old value.
222 * @param newValue new value.
223 */
224 protected final void firePropertyChange(
225 final String propertyName,
226 final Object oldValue,
227 final Object newValue) {
228 propertySupport.firePropertyChange(propertyName, oldValue, newValue);
229 }
230 }