View Javadoc
1   package org.apache.torque.betwixt;
2   
3   /*
4    * Copyright 2001-2006 The Apache Software Foundation.
5    *
6    * Licensed under the Apache License, Version 2.0 (the "License")
7    * you may not use this file except in compliance with the License.
8    * You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  
19  import java.util.Iterator;
20  import java.util.List;
21  import java.util.Vector;
22  
23  import org.apache.commons.logging.Log;
24  import org.apache.commons.logging.LogFactory;
25  
26  /***
27   * A Base class with methods that manage event notifications.
28   * 
29   * @author <a href="mailto:greg.monroe@dukece.com">Greg Monroe</a>
30   */
31  public class XMLEventNotifier 
32  {
33      static Log logger = LogFactory.getLog(XMLEventNotifier.class);
34      
35      /*** Tag for error notifications **/
36      public static final String ACTION_ERROR = "ERROR";
37      
38      /*** Tag for general Info notifications **/
39      public static final String ACTION_INFO = "INFO";
40      
41      /*** Tag for general Export Start notifications **/
42      public static String EXPORT_START = "EXPORT_START";
43  
44      /*** Tag for general Export Record notifications **/
45      public static String EXPORT_RECORD = "EXPORT_RECORD";
46  
47      /*** Tag for general Export End notifications **/
48      public static String EXPORT_END = "EXPORT_END";
49      
50      /*** Tag for import start notifications **/
51      public static final String IMPORT_START = "IMPORT_START";
52  
53      /*** Tag for import end notifications **/
54      public static final String IMPORT_END = "IMPORT_END";
55  
56      /*** Tag for record type notifications **/
57      public static final String ACTION_TYPE = "RECORD_TYPE";
58  
59      /*** Tag for record update notifications **/
60      public static final String ACTION_UPDATE = "RECORD_UPDATED";
61  
62      /*** Tag for record added (Inserted) notifications **/
63      public static final String ACTION_ADD = "RECORD_ADDED";
64  
65      /*** Tag for record delete notifications **/
66      public static final String ACTION_DELETE = "RECORD_DELETED";
67  
68      private List listeners = new Vector();
69  
70      public XMLEventNotifier() 
71      {
72          super();
73      }
74      /***
75       * Calls all registered listeners to let them know an action occured. 
76       * Notification will occure in the order they were registered.
77       * 
78       * @param recordNum the (1 based) index of the record object being 
79       *                  processed. 
80       * @param action The action type of this info.
81       * @param info The message with information about this event.
82       */
83      public void notifyListeners(int recordNum, String action, String info )
84                                                              throws Exception
85      {
86          logger.info( action+": "+info );
87          synchronized ( listeners ) 
88          {
89              Iterator iList = listeners.iterator();
90              while ( iList.hasNext() )  
91              {
92                  XMLEventListener l = (XMLEventListener) iList.next();
93                  l.actionPerformed( recordNum, action, info );
94              }
95          }
96      }
97      
98      /***
99       * Register a listener that will be notified of export actions. 
100      * 
101      * @param listener The listener to add.
102      */
103     public synchronized void registerListener( XMLEventListener listener ) 
104     {
105         if ( ! getListeners().contains(listener)) 
106         {
107             getListeners().add(listener);
108         }
109     }
110     
111     /***
112      * Remove a listener. 
113      * 
114      * @param listener The listener to remove.
115      */
116     public synchronized void deregisterListener( XMLEventListener listener ) 
117     {
118         getListeners().remove(listener);
119     }
120     
121     /***
122      * Gets the registered listeners.
123      * 
124      * @return A list of listeners.
125      */
126     protected synchronized List getListeners() 
127     {
128         if ( listeners == null ) 
129         {
130             listeners = new Vector();
131         }
132         return listeners;
133     }
134 }