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

com.gemstone.gemfire.management.internal.cli.remote.CommandExecutionContext Maven / Gradle / Ivy

/*
 * 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.remote;

import java.util.Collections;
import java.util.Map;

import com.gemstone.gemfire.management.internal.cli.CommandResponseWriter;
import com.gemstone.gemfire.management.internal.cli.GfshParser;
import com.gemstone.gemfire.management.internal.cli.shell.Gfsh;

/**
 * 
 * @author Abhishek Chaudhari
 * @since 7.0
 */
public class CommandExecutionContext {
  // ThreadLocal variables that can be uses by commands
  private static final ThreadLocal> ENV = new ThreadLocal>();
  private static final ThreadLocal             FROM_SHELL = new ThreadLocal();
  private static final ThreadLocal            SHELL_BYTES_DATA = new ThreadLocal();

  private static final WrapperThreadLocal WRITER_WRAPPER = 
      new WrapperThreadLocal() {
        @Override
        protected CommandResponseWriter createWrapped() {
          return new CommandResponseWriter();
        }
      };

  public static String getShellEnvProperty(String propertyName, String defaultValue) {
    String propertyValue = null;
    Map gfshEnv = ENV.get();
    if (gfshEnv != null) {
      propertyValue = gfshEnv.get(propertyName);
    }
    return propertyValue != null ? propertyValue : defaultValue;
  }
// Enable when "use region" command is required. See #46110
//  public static String getShellContextPath() {
//    return getShellEnvProperty(CliConstants.ENV_APP_CONTEXT_PATH, null);
//  }

  public static int getShellFetchSize() {
    int fetchSize = Gfsh.DEFAULT_APP_FETCH_SIZE;
    String fetchSizeStr = getShellEnvProperty(Gfsh.ENV_APP_FETCH_SIZE, null);
    if (fetchSizeStr != null) {
      fetchSize = Integer.valueOf(fetchSizeStr);
    }
    return fetchSize;
  }

  public static String getShellLineSeparator() {
    return getShellEnvProperty(Gfsh.ENV_SYS_OS_LINE_SEPARATOR, GfshParser.LINE_SEPARATOR);
  }

  public static Map getShellEnv() {
    Map envMap = ENV.get();
    if (envMap != null) {
      return Collections.unmodifiableMap(envMap);
    } else {
      return Collections.emptyMap();
    }
  }

  // TODO - Abhishek make this protected & move caller code of this method 
  // from MemberMBeanBridge to MemberCommandService
  public static void setShellEnv(Map env) {
    ENV.set(env);
  }

  public static byte[][] getBytesFromShell() {
    return SHELL_BYTES_DATA.get();
  }

  public static void setBytesFromShell(byte[][] data) {
    SHELL_BYTES_DATA.set(data);
  }

  public static boolean isShellRequest() {
    return FROM_SHELL.get() != null && FROM_SHELL.get();
  }

  // TODO - Abhishek make this protected & move caller code of this method 
  // from MemberMBeanBridge to MemberCommandService
  public static void setShellRequest() {
    FROM_SHELL.set(true);
  }

  public static boolean isSetWrapperThreadLocal() {
    return WRITER_WRAPPER.isSet();
  }

  public static CommandResponseWriter getCommandResponseWriter() {
    return WRITER_WRAPPER.get();
  }

  public static CommandResponseWriter getAndCreateIfAbsentCommandResponseWriter() {
    return WRITER_WRAPPER.getAndCreateIfAbsent();
  }

  public static void clear() {
    Map map = ENV.get();
    if (map != null) {
      map.clear();
    }
    ENV.set(null);
    
    FROM_SHELL.set(false);
    SHELL_BYTES_DATA.set(null);
    WRITER_WRAPPER.set(null);
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy