Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Licensed to the Apache Software Foundation (ASF) under one or more
   * contributor license agreements.  See the NOTICE file distributed with
   * this work for additional information regarding copyright ownership.
   * The ASF licenses this file to You under the Apache License, Version 2.0
   * (the "License"); you may not use this file except in compliance with
   * the License.  You may obtain a copy of the License at
   *
   *     http://www.apache.org/licenses/LICENSE-2.0
  *
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
 package org.jclouds.elb.loadbalancer.strategy;
 
 import static com.google.common.base.Preconditions.checkNotNull;
 import static com.google.common.base.Predicates.in;
 import static com.google.common.base.Predicates.not;
 import static com.google.common.collect.Iterables.transform;
 import static com.google.common.collect.Sets.filter;
 import static org.jclouds.aws.util.AWSUtils.getRegionFromLocationOrNull;
 
 import java.util.Set;
 
 
 

Author(s):
Adrian Cole
 
    @Resource
    protected Logger logger = .;
    protected final ELBApi api;
 
    @Inject
    protected ELBLoadBalanceNodesStrategy(ELBApi api,
             Function<LoadBalancerInRegionLoadBalancerMetadataconverter) {
       this. = checkNotNull(api"api");
       this. = checkNotNull(converter"converter");
    }
 
    @Override
    public LoadBalancerMetadata createLoadBalancerInLocation(Location locationString nameString protocol,
             int loadBalancerPortint instancePortIterable<? extends NodeMetadatanodes) {
       checkNotNull(location"location");
       String region = getRegionFromLocationOrNull(location);
 
       Set<StringzonesDesired = ImmutableSet.copyOf(transform(nodesnew Function<NodeMetadataString>() {
 
          @Override
          public String apply(NodeMetadata from) {
             return from.getLocation().getId();
          }
       }));
 
       .debug(">> creating loadBalancer(%s) in zones(%s)"namezonesDesired);
       try {
                   name,
                   ImmutableSet.of(Listener.builder().port(loadBalancerPort).instancePort(instancePort)
                            .protocol(Protocol.valueOf(protocol)).build()), zonesDesired);
          .debug("<< created loadBalancer(%s) dnsName(%s)"namednsName);
       } catch (IllegalStateException e) {
          .debug("<< converging zones(%s) in loadBalancer(%s)"zonesDesiredname);
          Set<StringcurrentZones = .getLoadBalancerApi().get(name).getAvailabilityZones();
          Set<StringzonesToAdd = Sets.difference(zonesDesiredcurrentZones);
          if (zonesToAdd.size() > 0)
             currentZones = .getAvailabilityZoneApi().addAvailabilityZonesToLoadBalancer(zonesToAddname);
          Set<StringzonesToRemove = Sets.difference(currentZoneszonesDesired);
          if (zonesToRemove.size() > 0)
             .getAvailabilityZoneApi().removeAvailabilityZonesFromLoadBalancer(zonesToRemovename);
       }
 
       Set<StringinstanceIds = ImmutableSet.copyOf(transform(nodesnew Function<NodeMetadataString>() {
         @Override
         public String apply(NodeMetadata from) {
            return from.getProviderId();
         }
      }));
      .debug(">> converging loadBalancer(%s) to instances(%s)"nameinstanceIds);
      Set<StringregisteredInstanceIds = .getInstanceApiForRegion(region).registerInstancesWithLoadBalancer(
               instanceIdsname);
      Set<StringinstancesToRemove = filter(registeredInstanceIdsnot(in(instanceIds)));
      if (instancesToRemove.size() > 0) {
         .debug(">> deregistering instances(%s) from loadBalancer(%s)"instancesToRemovename);
         .getInstanceApiForRegion(region).deregisterInstancesFromLoadBalancer(instancesToRemovename);
      }
      .debug("<< converged loadBalancer(%s) "name);
      return .apply(new LoadBalancerInRegion(.getLoadBalancerApiForRegion(region).get(name), region));
   }
New to GrepCode? Check out our FAQ X