001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: package javax.naming.event;
019:
020: import java.util.EventObject;
021: import javax.naming.Binding;
022:
023: /**
024: * An event from a directory or naming service, for passing to a listener.
025: * <p>
026: * The source of the event is always the <code>EventContext</code> that the
027: * listener registered with. Names in the <code>NamingEvent</code> object are
028: * all relative to this context.
029: * </p>
030: * <p>
031: * Note the discussion about threads and synchronization in the description for
032: * this package.
033: * </p>
034: */
035: public class NamingEvent extends EventObject {
036:
037: /*
038: * This constant is used during deserialization to check the version which
039: * created the serialized object.
040: */
041: private static final long serialVersionUID = 0x9d18b00289d22f45L;
042:
043: /**
044: * A <code>NamingEvent</code> type constant, indicating that an object was
045: * added.
046: */
047: public static final int OBJECT_ADDED = 0;
048:
049: /**
050: * A <code>NamingEvent</code> type constant, indicating that an object was
051: * changed.
052: */
053: public static final int OBJECT_CHANGED = 3;
054:
055: /**
056: * A <code>NamingEvent</code> type constant, indicating that an object was
057: * removed.
058: */
059: public static final int OBJECT_REMOVED = 1;
060:
061: /**
062: * A <code>NamingEvent</code> type constant, indicating that an object was
063: * renamed.
064: */
065: public static final int OBJECT_RENAMED = 2;
066:
067: /**
068: * Some information about the event, whose format is specified by the
069: * service provider.
070: *
071: * @serial
072: */
073: protected Object changeInfo;
074:
075: /**
076: * The binding after the event.
077: *
078: * @serial
079: */
080: protected Binding newBinding;
081:
082: /**
083: * The binding before the event.
084: *
085: * @serial
086: */
087: protected Binding oldBinding;
088:
089: /**
090: * The type of this event. Its value is one of the constant event types
091: * above.
092: *
093: * @serial
094: */
095: protected int type;
096:
097: // the context that generated this event
098: private transient EventContext eventContext;
099:
100: /**
101: *
102: * Constructs an <code>NamingEvent</code> with all parameters.
103: *
104: * @param eventContext
105: * the context that generated this event. It is the originator of
106: * this event and cannot be null.
107: * @param type
108: * the constant value that specifies the type of event
109: * @param newBinding
110: * binding after the event. <code>newBinding</code> might be
111: * null depending on the value of the <code>type</code>
112: * parameter as follows:
113: * <ul>
114: * <li> <code>OBJECT_ADDED</code> - <code>newBinding</code>
115: * cannot be null </li>
116: * <li> <code>OBJECT_CHANGED</code> - <code>newBinding</code>
117: * cannot be null </li>
118: * <li> <code>OBJECT_REMOVED</code> - <code>newBinding</code>
119: * can be null </li>
120: * <li> <code>OBJECT_RENAMED</code> - <code>newBinding</code>
121: * can be null </li>
122: * </ul>
123: * The names are relative to the <code>eventContext</code>
124: * @param oldBinding
125: * the binding before the event. <code>oldBinding</code> might
126: * be null depending on the value of the <code>type</code>
127: * parameter as follows:
128: * <ul>
129: * <li> <code>OBJECT_ADDED</code> - <code>oldBinding</code>
130: * can be null </li>
131: * <li> <code>OBJECT_CHANGED</code> - <code>oldBinding</code>
132: * cannot be null </li>
133: * <li> <code>OBJECT_REMOVED</code> - <code>oldBinding</code>
134: * cannot be null </li>
135: * <li> <code>OBJECT_RENAMED</code> - <code>oldBinding</code>
136: * can be null </li>
137: * </ul>
138: * The names are relative to the <code>eventContext</code>
139: * @param changeInfo
140: * contain some information about the event and maybe null, the
141: * format of which is specified by the service provider.
142: */
143: public NamingEvent(EventContext eventContext, int type,
144: Binding newBinding, Binding oldBinding, Object changeInfo) {
145: super (eventContext);
146:
147: this .type = type;
148: this .changeInfo = changeInfo;
149: this .newBinding = newBinding;
150: this .oldBinding = oldBinding;
151: this .eventContext = eventContext;
152:
153: }
154:
155: /**
156: * Calls a method to notify the listener of this event.
157: * <p>
158: * For <code>OBJECT_ADDED</code>, <code>OBJECT_REMOVED</code> or
159: * <code>OBJECT_RENAMED</code> type events this method calls the
160: * corresponding method in the <code>NamespaceChangedListener</code>
161: * interface. For <code>OBJECT_CHANGED</code> type events this method
162: * calls <code>objectChanged()</code> in the
163: * <code>ObjectChangeListener</code> interface.
164: * </p>
165: *
166: * @param naminglistener
167: * the listener of this event
168: */
169: public void dispatch(NamingListener naminglistener) {
170: switch (type) {
171: case OBJECT_ADDED:
172: ((NamespaceChangeListener) naminglistener)
173: .objectAdded(this );
174: break;
175: case OBJECT_REMOVED:
176: ((NamespaceChangeListener) naminglistener)
177: .objectRemoved(this );
178: break;
179: case OBJECT_RENAMED:
180: ((NamespaceChangeListener) naminglistener)
181: .objectRenamed(this );
182: break;
183: case OBJECT_CHANGED:
184: ((ObjectChangeListener) naminglistener).objectChanged(this );
185: break;
186: }
187: }
188:
189: /**
190: * Gets the change information.
191: *
192: * @return the change information object provided by the service provider,
193: * which may be null.
194: */
195: public Object getChangeInfo() {
196: return changeInfo;
197: }
198:
199: /**
200: * Gets the <code>EventContext</code> that generated this event.
201: *
202: * @return the <code>EventContext</code> that generated this event.
203: */
204: public EventContext getEventContext() {
205: return eventContext;
206: }
207:
208: /**
209: * Gets the binding after this event.
210: * <p>
211: * If it exists and is inside the scope that was specified when the listener
212: * was registered using <code>EventContext.addNamingListener</code>.
213: * Returns null otherwise. Therefore for an <code>OBJECT_RENAMED</code>
214: * event, the return value will be non-null if the new name places the
215: * binding within the scope for the listener.
216: * </p>
217: *
218: * @return the binding after this event
219: */
220: public Binding getNewBinding() {
221: return newBinding;
222: }
223:
224: /**
225: * Gets the binding before this event.
226: * <p>
227: * If it existed and was inside the scope that was specified when the
228: * listener was registered using <code>EventContext.addNamingListener</code>.
229: * Returns null otherwise. Therefore for an <code>OBJECT_RENAMED</code>
230: * event, the return value will be non-null if the old name placed the
231: * binding within the scope for the listener.
232: * </p>
233: *
234: * @return the binding before this event
235: */
236: public Binding getOldBinding() {
237: return oldBinding;
238: }
239:
240: /**
241: * Gets the type of the event.
242: * <p>
243: * The return value is constrained to a choice from:
244: * <code>OBJECT_ADDED</code>, <code>OBJECT_REMOVED</code>,
245: * <code>OBJECT_RENAMED</code>, <code>OBJECT_CHANGED</code>.
246: * </p>
247: *
248: * @return the type of the event
249: */
250: public int getType() {
251: return type;
252: }
253:
254: }
|