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 at7 * 8 * http://www.apache.org/licenses/LICENSE-2.09 * 10 * Unless required by applicable law or agreed to in writing, software11 * 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 and14 * limitations under the License.15 *16 */17package org.apache.juddi.v3.client.mapping;
1819import java.util.Properties;
2021publicclassPolicyLocalFirstextendsPolicyRoundRobin {
2223publicfinalstatic String JUDDI_CLIENT_LOCAL = "juddi.client.local";
24publicfinalstatic String DEFAULT_CLIENT_LOCAL = "localhost";
25privatestatic String local;
2627/**28 * This policy prefers 'local' EPR over remote EPRs. By default 'local' means29 * the EPR contains the String 'localhost'. This setting can be overwritten30 * by setting the 'juddi.client.local' property. An example would be 'localhost:8080'.31 * 32 * @param properties33 */34publicPolicyLocalFirst(Properties properties) {
35super(properties);
36 init(properties);
37 }
3839privatestaticsynchronizedvoid init(Properties properties){
40if (properties!=null) {
41 local = properties.getProperty(JUDDI_CLIENT_LOCAL, DEFAULT_CLIENT_LOCAL);
42 } else {
43 local = DEFAULT_CLIENT_LOCAL;
44 }
45 }
4647public String select(Topology topology) {
4849if (topology.getEprs().size()==0) returnnull;
5051if ((topology.getHasLocal()==null)) {
52int pointer = 0;
53 topology.setHasLocal(Boolean.FALSE);
54for (String epr : topology.getEprs()) {
55if (epr.toLowerCase().contains(local.toLowerCase())) {
56 topology.setPointer(pointer);
57 topology.setHasLocal(Boolean.TRUE);
58break;
59 }
60 pointer++;
61 }
6263 }
6465if (topology.getHasLocal()) {
66//return the localEpr67return topology.getEprs().get(topology.getPointer());
68 } else {
69//no local EPR, fall back on roundrobin70returnsuper.select(topology);
71 }
72 }
7374 }