View Javadoc

1   /*
2    * EL4J, the Extension Library for the J2EE, adds incremental enhancements to
3    * the spring framework, http://el4j.sf.net
4    * Copyright (C) 2006 by ELCA Informatique SA, Av. de la Harpe 22-24,
5    * 1000 Lausanne, Switzerland, http://www.elca.ch
6    *
7    * EL4J is published under the GNU Lesser General Public License (LGPL)
8    * Version 2.1. See http://www.gnu.org/licenses/
9    *
10   * This program is distributed in the hope that it will be useful,
11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13   * GNU Lesser General Public License for more details.
14   *
15   * For alternative licensing, please contact info@elca.ch
16   */
17  package ch.elca.el4j.services.remoting.protocol.loadbalancing.policy;
18  
19  import ch.elca.el4j.services.remoting.AbstractRemotingProtocol;
20  import ch.elca.el4j.services.remoting.protocol.loadbalancing.NoProtocolAvailableRTException;
21  
22  /**
23   *
24   * Abstract class that defines the policy of the protocol selection.
25   * Protocol comparison must be done using "=="!
26   *
27   * @svnLink $Revision: 3873 $;$Date: 2009-08-04 13:59:45 +0200 (Di, 04. Aug 2009) $;$Author: swismer $;$URL: https://el4j.svn.sourceforge.net/svnroot/el4j/branches/el4j_3_1/el4j/framework/modules/remoting_core/src/main/java/ch/elca/el4j/services/remoting/protocol/loadbalancing/policy/AbstractPolicy.java $
28   *
29   * @author Stefan Pleisch (SPL)
30   */
31  public abstract class AbstractPolicy {
32  	
33  	/**
34  	 * Array of protocols, each represented by an array of Strings, using
35  	 * a [protocolTag, arg1, arg2, ...] representation.
36  	*/
37  	protected AbstractRemotingProtocol[] m_protocols;
38  	
39  	/**
40  	 * This method is called before any other method is called. It passes the
41  	 * protocol information.
42  	 * @param protocols Array of currently defined protocols.
43  	 */
44  	public void setProtocols(AbstractRemotingProtocol[] protocols) {
45  		m_protocols = protocols;
46  	}
47  	
48  	/**
49  	 * This method is called if the use of one of the protocols has failed.
50  	 * Protocol comparison must be done using "=="!
51  	 * @param protocol The protocol that has lead to a failure.
52  	 */
53  	public void notifyFailure(AbstractRemotingProtocol protocol) {
54  		// Do nothing
55  	}
56  	
57  	/**
58  	 * @return Number of available protocols
59  	 */
60  	public int getProtocolCount() {
61  		return m_protocols.length;
62  	}
63  	
64  	/**
65  	 * Note that protocol comparison must be done using "=="!
66  	 *
67  	 * @return The next protocol to be used.
68  	 * @throws NoProtocolAvailableRTException
69  	 *             If no protocol is available any more
70  	 */
71  	public abstract AbstractRemotingProtocol getNextProtocol()
72  		throws NoProtocolAvailableRTException;
73  	
74  	
75  	/**
76  	 * Removes protocol 'pi' from {@link #m_protocols}. Does nothing if 'pi'
77  	 * does not exist.
78  	 *
79  	 * @param protocol
80  	 *            Protocol to be removed.
81  	 */
82  	protected void removeProtocol(AbstractRemotingProtocol protocol) {
83  		int index = -1;
84  		for (int i = 0; i < m_protocols.length; i += 1) {
85  			if (m_protocols[i] == protocol) {
86  				index = i;
87  				break;
88  			}
89  		}
90  		if (index >= 0) {
91  			// Element found
92  			AbstractRemotingProtocol[] tmp
93  				= new AbstractRemotingProtocol[m_protocols.length - 1];
94  			for (int k = 0; k < index; k += 1) {
95  				tmp[k] = m_protocols[k];
96  			}
97  			for (int j = index + 1; j < m_protocols.length; j += 1) {
98  				tmp[j - 1] = m_protocols[j];
99  			}
100 			m_protocols = tmp;
101 		}
102 	}
103 	
104 	
105 } // CLASS AbstractPolicy