001: /**
002: * Licensed to the Apache Software Foundation (ASF) under one
003: * or more contributor license agreements. See the NOTICE file
004: * distributed with this work for additional information
005: * regarding copyright ownership. The ASF licenses this file
006: * to you under the Apache License, Version 2.0 (the
007: * "License"); you may not use this file except in compliance
008: * with the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing,
013: * software distributed under the License is distributed on an
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015: * KIND, either express or implied. See the License for the
016: * specific language governing permissions and limitations
017: * under the License.
018: */package org.apache.cxf.clustering;
019:
020: import java.util.ArrayList;
021: import java.util.Collection;
022: import java.util.List;
023: import java.util.logging.Level;
024: import java.util.logging.Logger;
025:
026: import javax.xml.namespace.QName;
027:
028: import org.apache.cxf.common.logging.LogUtils;
029: import org.apache.cxf.endpoint.Endpoint;
030: import org.apache.cxf.message.Exchange;
031: import org.apache.cxf.service.model.EndpointInfo;
032: import org.apache.cxf.service.model.ServiceInfo;
033:
034: /**
035: * Failover strategy based on a static cluster represented by
036: * multiple endpoints associated with the same service instance.
037: */
038: public abstract class AbstractStaticFailoverStrategy implements
039: FailoverStrategy {
040:
041: private static final Logger LOG = LogUtils
042: .getL7dLogger(AbstractStaticFailoverStrategy.class);
043:
044: /**
045: * Get the alternate endpoints for this invocation.
046: *
047: * @param exchange the current Exchange
048: * @return a List of alternate endpoints if available
049: */
050: public List<Endpoint> getAlternateEndpoints(Exchange exchange) {
051: Endpoint endpoint = exchange.get(Endpoint.class);
052: Collection<ServiceInfo> services = endpoint.getService()
053: .getServiceInfos();
054: QName currentBinding = endpoint.getBinding().getBindingInfo()
055: .getName();
056: List<Endpoint> alternates = new ArrayList<Endpoint>();
057: for (ServiceInfo service : services) {
058: Collection<EndpointInfo> candidates = service
059: .getEndpoints();
060: for (EndpointInfo candidate : candidates) {
061: QName candidateBinding = candidate.getBinding()
062: .getName();
063: if (candidateBinding.equals(currentBinding)) {
064: if (!candidate.getAddress().equals(
065: endpoint.getEndpointInfo().getAddress())) {
066: Endpoint alternate = endpoint.getService()
067: .getEndpoints()
068: .get(candidate.getName());
069: if (alternate != null) {
070: LOG.log(Level.INFO,
071: "FAILOVER_CANDIDATE_ACCEPTED",
072: candidate.getName());
073: alternates.add(alternate);
074: }
075: }
076: } else {
077: LOG.log(Level.INFO, "FAILOVER_CANDIDATE_REJECTED",
078: new Object[] { candidate.getName(),
079: candidateBinding });
080: }
081: }
082: }
083: return alternates;
084: }
085:
086: /**
087: * Select one of the alternate endpoints for a retried invocation.
088: *
089: * @param a List of alternate endpoints if available
090: * @return the selected endpoint
091: */
092: public Endpoint selectAlternateEndpoint(List<Endpoint> alternates) {
093: Endpoint selected = null;
094: if (alternates != null && alternates.size() > 0) {
095: selected = getNextAlternate(alternates);
096: LOG.log(Level.WARNING, "FAILING_OVER_TO", new Object[] {
097: selected.getEndpointInfo().getName(),
098: selected.getEndpointInfo().getAddress() });
099: } else {
100: LOG.warning("NO_ALTERNATE_TARGETS_REMAIN");
101: }
102: return selected;
103: }
104:
105: /**
106: * Get next alternate endpoint.
107: *
108: * @param alternates non-empty List of alternate endpoints
109: * @return
110: */
111: protected abstract Endpoint getNextAlternate(
112: List<Endpoint> alternates);
113: }
|