This project has retired. For details please refer to its Attic page.
PolicyLocalFirst xref
View Javadoc
1   /*
2    * Copyright 2001-2011 The Apache Software Foundation.
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * 
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   *
16   */
17  package org.apache.juddi.v3.client.mapping;
18  
19  import java.util.Properties;
20  
21  public class PolicyLocalFirst extends PolicyRoundRobin {
22  
23  	public final static String JUDDI_CLIENT_LOCAL   = "juddi.client.local";
24  	public final static String DEFAULT_CLIENT_LOCAL = "localhost";
25  	private static String local;
26  	
27  	/**
28  	 * This policy prefers 'local' EPR over remote EPRs. By default 'local' means
29  	 * the EPR contains the String 'localhost'. This setting can be overwritten
30  	 * by setting the 'juddi.client.local' property. An example would be 'localhost:8080'.
31  	 * 
32  	 * @param properties
33  	 */
34  	public PolicyLocalFirst(Properties properties) {
35  		super(properties);
36  		init(properties);
37  	}
38          
39          private static synchronized void init(Properties properties){
40                  if (properties!=null) {
41  			local = properties.getProperty(JUDDI_CLIENT_LOCAL, DEFAULT_CLIENT_LOCAL);
42  		} else {
43  			local = DEFAULT_CLIENT_LOCAL;
44  		}
45          }
46  	
47  	public String select(Topology topology) {
48  		
49  		if (topology.getEprs().size()==0) return null;
50  		
51  		if ((topology.getHasLocal()==null)) {
52  			int pointer = 0;
53  			topology.setHasLocal(Boolean.FALSE);
54  			for (String epr : topology.getEprs()) {
55  				if (epr.toLowerCase().contains(local.toLowerCase())) {
56  					topology.setPointer(pointer);
57  					topology.setHasLocal(Boolean.TRUE);
58  					break;
59  				}
60  				pointer++;
61  			}
62  			
63  		}
64  		
65  		if (topology.getHasLocal()) {
66  			//return the localEpr
67  			return topology.getEprs().get(topology.getPointer());
68  		} else {
69  			//no local EPR, fall back on roundrobin
70  			return super.select(topology);
71  		}
72  	}
73  
74  }