View Javadoc

1   /*
2    * $Id: InterceptorRegistry.java,v 1.3 2004/03/29 15:26:52 eelco12 Exp $
3    * $Revision: 1.3 $
4    * $Date: 2004/03/29 15:26:52 $
5    *
6    * ====================================================================
7    * Copyright (c) 2003, Open Edge B.V.
8    * All rights reserved.
9    * Redistribution and use in source and binary forms, with or without 
10   * modification, are permitted provided that the following conditions are met:
11   * Redistributions of source code must retain the above copyright notice, 
12   * this list of conditions and the following disclaimer. Redistributions 
13   * in binary form must reproduce the above copyright notice, this list of 
14   * conditions and the following disclaimer in the documentation and/or other 
15   * materials provided with the distribution. Neither the name of OpenEdge B.V. 
16   * nor the names of its contributors may be used to endorse or promote products 
17   * derived from this software without specific prior written permission.
18   * 
19   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 
20   * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 
21   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 
22   * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 
23   * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 
24   * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 
25   * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 
26   * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 
27   * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 
28   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF 
29   * THE POSSIBILITY OF SUCH DAMAGE.
30   */
31   
32  package nl.openedge.baritus;
33  
34  import java.util.ArrayList;
35  import java.util.Iterator;
36  import java.util.List;
37  
38  import nl.openedge.baritus.interceptors.Interceptor;
39  
40  /***
41   * Registry for interceptors. Each instance of FormBeanBase has its own instance.
42   * 
43   * @author Eelco Hillenius
44   */
45  final class InterceptorRegistry
46  {
47  
48  	private List interceptors = null;
49  	
50  	private List[] flowInterceptors;
51  	
52  
53  	/***
54  	 * add an interceptor to the current list of interceptors
55  	 * @param interceptor the interceptor to add to the current list of interceptors
56  	 */
57  	public void addInterceptor(Interceptor interceptor)
58  	{
59  		if(interceptors == null)
60  		{
61  			interceptors = new ArrayList();
62  		}
63  		interceptors.add(interceptor);
64  	}
65  	
66  	/***
67  	 * add an interceptor to the current list of interceptors at the specified position
68  	 * @param index index position where to insert the interceptor
69  	 * @param interceptor the interceptor to add to the current list of interceptors
70  	 */
71  	public void addInterceptor(int index, Interceptor interceptor)
72  	{
73  		if(interceptors == null)
74  		{
75  			interceptors = new ArrayList();
76  		}
77  		interceptors.add(index, interceptor);
78  	}
79  
80  	
81  	/***
82  	 * remove an interceptor from the current list of interceptors
83  	 * @param interceptor the interceptor to remove from the current list of interceptors
84  	 */
85  	public void removeInterceptor(Interceptor interceptor)
86  	{
87  		if(interceptors != null)
88  		{
89  			interceptors.remove(interceptor);
90  			if(interceptors.isEmpty()) interceptors = null;
91  		}
92  	}
93  	
94  	/***
95  	 * get all registered interceptors of the provided type 
96  	 * @param type the type
97  	 * @return array of Interceptors or null if none
98  	 */
99  	public Interceptor[] getInterceptors(Class type)
100 	{
101 		Interceptor[] result = null;
102 		if(interceptors != null && (!interceptors.isEmpty()))
103 		{
104 			List temp = new ArrayList();
105 			for(Iterator i = interceptors.listIterator(); i.hasNext(); )
106 			{
107 				Interceptor intc = (Interceptor)i.next();
108 				if(type.isAssignableFrom(intc.getClass()))
109 				{
110 					temp.add(intc);
111 				}
112 			}
113 			if(!temp.isEmpty())
114 			{
115 				result = (Interceptor[])temp.toArray(new Interceptor[temp.size()]);
116 			}
117 		}
118 		return result;
119 	}
120 	
121 	/***
122 	 * get the flow interceptors for the provided interceptionPoint
123 	 * @param interceptionPoint the interception point
124 	 * @return List flow interceptors or null if none were registered
125 	 */
126 	public List getInterceptorsAtPoint(int interceptionPoint)
127 	{
128 		return (flowInterceptors != null) ?
129 			flowInterceptors[interceptionPoint] : null;
130 	}
131 
132 }