Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
Copyright 2014 Cisco Systems, Inc. Licensed 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 com.cisco.oss.foundation.directory.impl;
 
 import java.util.List;
 
 
The LookupManager implementation.
 
 public class LookupManagerImpl implements LookupManagerStoppable {
 
     private static final Logger LOGGER = LoggerFactory.getLogger(LookupManagerImpl.class);


    
The loadbalancer manager for Services.
 
     private final LoadBalancerManager lbManager;

    
The LookupService.
 
     private final DirectoryLookupService lookupService;

    
Mark component started or not
 
     private final AtomicBoolean isStarted = new AtomicBoolean(false);
 
 
 
     public LookupManagerImpl(DirectoryLookupService lookupService){
         this. = new LoadBalancerManager();
         this. = lookupService;
     }

    
Start the LookupManagerImpl.
 
     @Override
     public void start(){
         .set(true);
     }

    
Stop the LookupManagerImpl It is thread safe.
 
     @Override
     public void stop(){
         if (.compareAndSet(truefalse)) {
            ((StoppablegetLookupService()).stop();
         }
     }

    
    @Override
    public ServiceInstance lookupInstance(String serviceName)
            throws ServiceException {
        ServiceInstanceUtils.validateManagerIsStarted();
        ServiceInstanceUtils.validateServiceName(serviceName);
        return lb.vote(lookupInstances(serviceName));
    }

    
    @Override
    public List<ServiceInstancelookupInstances(String serviceName)
            throws ServiceException {
        
        ServiceInstanceUtils.validateManagerIsStarted();
        ServiceInstanceUtils.validateServiceName(serviceName);
        List<ModelServiceInstancemodelSvc = getLookupService().getUPModelInstances(serviceName);
        List<ServiceInstanceinstances = new ArrayList<ServiceInstance>();
        for (ModelServiceInstance modelInstance : modelSvc) {
            instances.add(ServiceInstanceUtils.toServiceInstance(modelInstance));
        }
        return instances;
    }

    
    @Override
    public ServiceInstance queryInstanceByName(String serviceNameServiceInstanceQuery query)
            throws ServiceException {
        
        ServiceInstanceUtils.validateManagerIsStarted();
        ServiceInstanceUtils.validateServiceName(serviceName);
        if (query == null) {
                    ..getMessageTemplate(),
                    "ServiceInstanceQuery");
        }
        return lb.vote(queryInstancesByName(serviceName,query));
    }

    
    @Override
            throws ServiceException {
        
        ServiceInstanceUtils.validateManagerIsStarted();
        ServiceInstanceUtils.validateServiceName(serviceName);
        if (query == null) {
                    ..getMessageTemplate(),
                    "ServiceInstanceQuery");
        }
        List<ModelServiceInstancemodelSvc = getLookupService().getUPModelInstances(serviceName);
        List<ModelServiceInstancefilteredInstances = ServiceInstanceQueryHelper.filter(querymodelSvc);
        List<ServiceInstanceinstances = new ArrayList<ServiceInstance>();
        for (ModelServiceInstance model : filteredInstances) {
            instances.add(ServiceInstanceUtils.toServiceInstance(model));
        }
        return instances;
    }

    
    @Override
            throws ServiceException {
        
        ServiceInstanceUtils.validateManagerIsStarted();
        return lb.vote(queryInstancesByMetadataKey(query));
    }

    
    @Override
            throws ServiceException {
        
        ServiceInstanceUtils.validateManagerIsStarted();
        List<ServiceInstanceinstances = null;
        String keyName = null;
        if (query.getCriteria().size() > 0) {
            keyName = query.getCriteria().get(0).getMetadataKey();
        }
        if (keyName != null && !keyName.isEmpty()) {
            List<ModelServiceInstancemodelInstances = getLookupService()
                    .getUPModelInstancesByMetadataKey(keyName);
            List<ModelServiceInstancefilteredInstances = ServiceInstanceQueryHelper
                    .filter(querymodelInstances);
            instances = new ArrayList<ServiceInstance>();
            for (ModelServiceInstance model : filteredInstances) {
                instances.add(ServiceInstanceUtils.toServiceInstance(model));
            }
            return instances;
        }
        return Collections.<ServiceInstance>emptyList();
    }

    
    @Override
    public ServiceInstance getInstance(String serviceNameString instanceId)
            throws ServiceException {
        ServiceInstanceUtils.validateManagerIsStarted();
        ServiceInstanceUtils.validateServiceName(serviceName);
        ServiceInstanceUtils.validateServiceInstanceID(instanceId);
        ModelServiceInstance instance = getLookupService().getModelServiceInstance(serviceNameinstanceId);
        if (instance!=null) {
            return ServiceInstanceUtils.toServiceInstance(instance);
        }
        return null;
    }

    
    @Override
    public List<ServiceInstancegetAllInstances(String serviceName)
            throws ServiceException {
        ServiceInstanceUtils.validateManagerIsStarted();
        ServiceInstanceUtils.validateServiceName(serviceName);
        List<ModelServiceInstancemodelSvc = getLookupService().getModelInstances(serviceName);
        List<ServiceInstanceinstances = new ArrayList<ServiceInstance>();
        for (ModelServiceInstance modelInstance : modelSvc) {
            instances.add(ServiceInstanceUtils.toServiceInstance(modelInstance));
        }
        return instances;
    }

    
    @Override
    public List<ServiceInstancegetAllInstances(String serviceName,
            ServiceInstanceQuery querythrows ServiceException {
        ServiceInstanceUtils.validateManagerIsStarted();
        ServiceInstanceUtils.validateServiceName(serviceName);
        if (query == null) {
                     ..getMessageTemplate(),
                    "ServiceInstanceQuery");
        }
        List<ModelServiceInstancemodelSvc = getLookupService().getModelInstances(serviceName);
        List<ModelServiceInstancefilteredInstances = ServiceInstanceQueryHelper.filter(querymodelSvc);
        List<ServiceInstanceinstances = new ArrayList<ServiceInstance>();
        for (ModelServiceInstance model : filteredInstances) {
            instances.add(ServiceInstanceUtils.toServiceInstance(model));
        }
        return instances;
    }

    
    @Override
            throws ServiceException {
        
        ServiceInstanceUtils.validateManagerIsStarted();
        String keyName = null;
        if (query.getCriteria().size() > 0) {
            keyName = query.getCriteria().get(0).getMetadataKey();
        }
        if (keyName != null && !keyName.isEmpty()) {
            List<ModelServiceInstancemodelInstances = getLookupService()
                    .getModelInstancesByMetadataKey(keyName);
            List<ModelServiceInstancefilteredInstances = ServiceInstanceQueryHelper
                    .filter(querymodelInstances);
            List<ServiceInstanceinstances = new ArrayList<ServiceInstance>();
            for (ModelServiceInstance model : filteredInstances) {
                instances.add(ServiceInstanceUtils.toServiceInstance(model));
            }
            return instances;
        }
        return Collections.<ServiceInstance>emptyList();
    }

    
    @Override
        
        ServiceInstanceUtils.validateManagerIsStarted();
        List<ServiceInstanceinstances = new ArrayList<ServiceInstance>();
        List<ModelServiceInstanceallInstances = getLookupService().getAllInstances();
        for (ModelServiceInstance serviceInstance : allInstances) {
            instances.add(ServiceInstanceUtils.toServiceInstance(serviceInstance));
        }
        
        return instances;
    }


    
Add a NotificationHandler to the Service. This method can check the duplicated NotificationHandler for the serviceName, if the NotificationHandler already exists for the serviceName, do nothing. Throws IllegalArgumentException if serviceName or handler is null.

Parameters:
serviceName the service name.
handler the NotificationHandler for the service.
    @Override
    public void addNotificationHandler(String serviceNameNotificationHandler handlerthrows ServiceException {
        ServiceInstanceUtils.validateManagerIsStarted();
        ServiceInstanceUtils.validateServiceName(serviceName);
        if (handler == null) {
                    ..getMessageTemplate(),
                    "NotificationHandler");
        }
        ModelService service = getLookupService().getModelService(serviceName);
        if (service == null) {
            throw new ServiceException(.);
        }
        getLookupService().addNotificationHandler(serviceNamehandler);
    }

    
Remove the NotificationHandler from the Service.

Parameters:
serviceName the service name.
handler the NotificationHandler for the service.
    @Override
    public void removeNotificationHandler(String serviceNameNotificationHandler handlerthrows ServiceException {
        ServiceInstanceUtils.validateManagerIsStarted();
        ServiceInstanceUtils.validateServiceName(serviceName);
        if (handler == null) {
                    ..getMessageTemplate(),
                    "NotificationHandler");
        }
        
        getLookupService().removeNotificationHandler(serviceNamehandler);;
    }

    
Get the DirectoryLookupService to do the lookup. It is thread safe.

Returns:
the LookupService.
        return ;
    }

    
Validate the metadata query for the queryInstanceByMetadataKey, queryInstancesByMetadataKey and getAllInstancesByMetadataKey methods. For those methods, the ContainQueryCriterion and NotContainQueryCriterion are not supported.

Parameters:
query the ServiceInstanceQuery to be validated.
Throws:
com.cisco.oss.foundation.directory.exception.ServiceException when the ServiceInstanceQuery has ContainQueryCriterion or NotContainQueryCriterion.
        if (query == null) {
                    ..getMessageTemplate(),
                    "service instance query");
        }
        
        for(QueryCriterion criterion : query.getCriteria()){
            if(criterion instanceof ContainQueryCriterion || criterion instanceof NotContainQueryCriterion){
                throw new ServiceException(.);
            }
        }
    }
New to GrepCode? Check out our FAQ X