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;
 import java.util.Map;
 
It is the Directory LookupService to perform the lookup functionality. It looks up ServiceInstance from Directory Server.
 
 public class DirectoryLookupService {

    
The DirectoryServiceClientManager to get the DirectoryServiceClient.
 
     private final DirectoryServiceClient directoryServiceClient;

    
The Service NotificationHandler Map.
 
     private final Map<StringList<NotificationHandler>> notificationHandlers  = new HashMap<StringList<NotificationHandler>>();

    
Constructor.

Parameters:
directoryServiceClient the DirectoryServiceClient.
 
     public DirectoryLookupService(DirectoryServiceClient directoryServiceClient){
         this. = directoryServiceClient;
     }

    
Get the ModelService by service name.

Parameters:
serviceName the Service name.
Returns:
the ModelService.
 
     protected ModelService getModelService(String serviceName){
         return getDirectoryServiceClient().lookupService(serviceName);
     }

    
Get ModelMetadataKey, which is an object holding a list of service instances that contain the key name in the service metadata.

Parameters:
keyName the metadata key name.
Returns:
the ModelMetadataKey.
 
     protected ModelMetadataKey getModelMetadataKey(String keyName){
         return getDirectoryServiceClient().getMetadataKey(keyName);
     }

    
Get the ModelServiceInstance by serviceName and instanceId.

Parameters:
serviceName the service name.
instanceId the instanceId.
Returns:
the ModelServiceInstance.
    public ModelServiceInstance getModelServiceInstance(String serviceNameString instanceId){
        ModelService service = getModelService(serviceName);
        if(service != null && service.getServiceInstances() != null ){
            for(ModelServiceInstance instance : service.getServiceInstances()){
                if(instance.getInstanceId().equals(instanceId)){
                    return instance;
                }
            }
        }
        return null;
    }

    
Get the ModelServiceInstance list that contains the metadata key.

Parameters:
keyName the metadata key name.
Returns:
the UP ModelServiceInstances that has the metadata key.
        ModelMetadataKey key = getModelMetadataKey(keyName);
        if(key == null || key.getServiceInstances().isEmpty()){
            return Collections.<ModelServiceInstance>emptyList();
        }else{
            return new ArrayList<ModelServiceInstance>(key.getServiceInstances());
        }
    }

    
Get the UP ModelServiceInstance list that contains the metadata key.

Parameters:
keyName the metadata key name.
Returns:
the ModelServiceInstances that have the metadata key.
        List<ModelServiceInstancelist = new ArrayList<ModelServiceInstance>();
        for (ModelServiceInstance instance : getModelInstancesByMetadataKey(keyName)){
            if(instance.getStatus().equals(.)){
                list.add(instance);
            }
        }
        return list;
    }

    
Get the ModelServiceInstance list of the Service.

Parameters:
serviceName the service name.
Returns:
the ModelServiceInstance list of the Service.
    public List<ModelServiceInstancegetModelInstances(String serviceName){
        ModelService service = getModelService(serviceName);
        if(service == null || service.getServiceInstances().isEmpty()){
            return Collections.<ModelServiceInstance>emptyList();
        }else{
            return new ArrayList<ModelServiceInstance>(service.getServiceInstances());
        }
    }

    
Get All ModelServiceInstance on the Directory Server.

Returns:
the ModelServiceInstance List.
        return getDirectoryServiceClient().getAllInstances();
    }

    
Get the UP ModelServiceInstance list of the Service.

Parameters:
serviceName the service name.
Returns:
the ModelServiceInstance list of the Service.
    public List<ModelServiceInstancegetUPModelInstances(String serviceName){
        List<ModelServiceInstancelist = new ArrayList<ModelServiceInstance>();
        for(ModelServiceInstance instance : getModelInstances(serviceName)){
            if(instance.getStatus().equals(.)) {
                list.add(instance);
            }
        }
        return list;
    }

    
Add a NotificationHandler to the Service. This method checks the duplicated NotificationHandler for the serviceName, if the NotificationHandler already exists for the serviceName, do nothing.

Parameters:
serviceName the service name.
handler the NotificationHandler for the service.
    public void addNotificationHandler(String serviceNameNotificationHandler handler){
        ServiceInstanceUtils.validateServiceName(serviceName);
        if (handler == null) {
                    ..getMessageTemplate(),
                    "NotificationHandler");
        }
  
        synchronized(){
            if(! .containsKey(serviceName)){
                .put(serviceNamenew ArrayList<NotificationHandler>());
            }
            .get(serviceName).add(handler);
        }
    }

    
Remove the NotificationHandler from the Service.

Parameters:
serviceName the service name.
handler the NotificationHandler for the service.
    public void removeNotificationHandler(String serviceNameNotificationHandler handler){
        ServiceInstanceUtils.validateServiceName(serviceName);
        if (handler == null) {
                    ..getMessageTemplate(),
                    "NotificationHandler");
        }
  
        synchronized(){
            if(.containsKey(serviceName)){
                List<NotificationHandlerlist = .get(serviceName);
                if(list.contains(handler)){
                    list.remove(handler);
                }
                if(list.isEmpty()){
                    .remove(serviceName);
                }
            }
        }
    }

    
Get the DirectoryServiceClient.

Returns:
the DirectoryServiceClient
        return this.;
    }

    
Invoke the serviceInstanceUnavailable of the NotificationHandler.

Parameters:
instance the ServiceInstance.
    protected void onServiceInstanceUnavailable(ServiceInstance instance){
        if(instance == null){
            return ;
        }
        String serviceName = instance.getServiceName();
        List<NotificationHandlerhandlerList = new ArrayList<NotificationHandler>();
        synchronized () {
            if (.containsKey(serviceName)) {
                handlerList.addAll(.get(serviceName));
            }
        }
        for(NotificationHandler h : handlerList) {
            h.serviceInstanceUnavailable(instance);
        }
    }

    
Invoke the serviceInstanceChange of the NotificationHandler.

Parameters:
instance the ServiceInstance.
    protected void onServiceInstanceChanged(ServiceInstance instance){
        String serviceName = instance.getServiceName();
        List<NotificationHandlerhandlerList = new ArrayList<NotificationHandler>();
        synchronized () {
            if (.containsKey(serviceName)) {
                handlerList.addAll(.get(serviceName));
            }
        }
        for (NotificationHandler h : handlerList) {
            h.serviceInstanceChange(instance);
        }
    }

    
Invoke the serviceInstanceAvailable of the NotificationHandler.

Parameters:
instance the ServiceInstance.
    protected void onServiceInstanceAvailable(ServiceInstance instance){
        String serviceName = instance.getServiceName();
        List<NotificationHandlerhandlerList = new ArrayList<NotificationHandler>();
        synchronized () {
            if (.containsKey(serviceName)) {
                handlerList.addAll(.get(serviceName));
            }
        }
        for (NotificationHandler h : handlerList) {
            h.serviceInstanceAvailable(instance);
        }
    }
New to GrepCode? Check out our FAQ X