All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.gemstone.gemfire.management.internal.cli.converters.ConnectionEndpointConverter Maven / Gradle / Ivy

The newest version!
/*
 * Copyright (c) 2010-2015 Pivotal Software, Inc. All rights reserved.
 *
 * 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. See accompanying
 * LICENSE file.
 */
/**
 * 
 */
package com.gemstone.gemfire.management.internal.cli.converters;

import java.util.List;

import org.springframework.shell.core.Completion;
import org.springframework.shell.core.Converter;
import org.springframework.shell.core.MethodTarget;

import com.gemstone.gemfire.management.internal.cli.util.ConnectionEndpoint;

/**
 * @author abhishek
 *
 */
public class ConnectionEndpointConverter implements Converter {
  // Defaults
  static final String DEFAULT_JMX_HOST      = "localhost";
  static final int    DEFAULT_JMX_PORT      = 1099;
  static final String DEFAULT_JMX_ENDPOINTS = DEFAULT_JMX_HOST+"["+DEFAULT_JMX_PORT+"]";
  
  public static final String DEFAULT_LOCATOR_HOST      = "localhost";
  public static final int    DEFAULT_LOCATOR_PORT      = 10334;
  public static final String DEFAULT_LOCATOR_ENDPOINTS = DEFAULT_LOCATOR_HOST+"["+DEFAULT_LOCATOR_PORT+"]";

  @Override
  public boolean supports(Class type, String optionContext) {
    return ConnectionEndpoint.class == type;
  }

  @Override
  public ConnectionEndpoint convertFromText(String value, Class targetType, String optionContext) {
    //expected format host[port], port is optional
    String endpointStr = value.trim();
    
    String hostStr = DEFAULT_JMX_HOST;
    String portStr = "";
    int port       = DEFAULT_JMX_PORT;
    
    if (!endpointStr.isEmpty()) {
      int openIndex  = endpointStr.indexOf("[");
      int closeIndex = endpointStr.indexOf("]");

      if (openIndex != -1) {//might have a port
        if (closeIndex == -1) {
          throw new IllegalArgumentException("Expected input: host[port] or host. Invalid value specified endpoints : "+value);
        }
        hostStr = endpointStr.substring(0, openIndex);
        
        portStr = endpointStr.substring(openIndex + 1, closeIndex);

        if (portStr.isEmpty()) {
          throw new IllegalArgumentException("Expected input: host[port] or host. Invalid value specified endpoints : "+value);
        }
        try {
          port = Integer.valueOf(portStr);
        } catch (NumberFormatException e) {
          throw new IllegalArgumentException("Expected input: host[port], Port should be a valid number between 1024-65536. Invalid value specified endpoints : "+value);
        }
        
        
      } else if (closeIndex != -1) { //shouldn't be there if opening brace was not there
        throw new IllegalArgumentException("Expected input: host[port] or host. Invalid value specified endpoints : "+value);
      } else {//doesn't contain brackets, assume only host name is given & assume default port
        hostStr = endpointStr;
      }
    }

    return new ConnectionEndpoint(hostStr, port);
  }

  @Override
  public boolean getAllPossibleValues(List completions,
      Class targetType, String existingData, String optionContext,
      MethodTarget target) {
    if (ConnectionEndpoint.JMXMANAGER_OPTION_CONTEXT.equals(optionContext)) {
      completions.add(new Completion(DEFAULT_JMX_ENDPOINTS));
    } else if (ConnectionEndpoint.LOCATOR_OPTION_CONTEXT.equals(optionContext)) {
      completions.add(new Completion(DEFAULT_LOCATOR_ENDPOINTS));
    }
    
    return completions.size() > 0;
  }
  
  public static void main(String[] args) {
    ConnectionEndpointConverter cec = new ConnectionEndpointConverter();
    
    try {
      System.out.println(cec.convertFromText("halibut[2555]", null, null));
    } catch (Exception e) {
      e.printStackTrace();
    }
    
    try {
      System.out.println(cec.convertFromText("halibut.pune.gemstone.com[2555]", null, null));
    } catch (Exception e) {
      e.printStackTrace();
    }
    
    try {
      System.out.println(cec.convertFromText("halibut[]", null, null));
    } catch (Exception e) {
      e.printStackTrace();
    }
    
    try {
      System.out.println(cec.convertFromText("halibut2555]", null, null));
    } catch (Exception e) {
      e.printStackTrace();
    }
    
    try {
      System.out.println(cec.convertFromText("halibut[", null, null));
    } catch (Exception e) {
      e.printStackTrace();
    }
    
    try {
      System.out.println(cec.convertFromText("halibut", null, null));
    } catch (Exception e) {
      e.printStackTrace();
    }
    
    
  }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy