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

com.alachisoft.ncache.client.internal.command.AddDependencyCommand Maven / Gradle / Ivy

/*
 * Command.java
 *
 * Created on September 8, 2006, 11:45 AM
 *
 * Copyright 2005 Alachisoft, Inc. All rights reserved.
 * ALACHISOFT PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 */
package com.alachisoft.ncache.client.internal.command;

import Alachisoft.NCache.Common.Util.DependencyHelper;
import com.alachisoft.ncache.common.protobuf.AddDependencyCommandProtocol;
import com.alachisoft.ncache.common.protobuf.CommandProtocol;
import com.alachisoft.ncache.runtime.dependencies.CacheDependency;
import com.alachisoft.ncache.runtime.exceptions.CommandException;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.nio.charset.StandardCharsets;

/**
 * @author Administrator
 * @version 1.0
 */
public final class AddDependencyCommand extends Command {

    private CacheDependency dependency;
    private boolean isResyncRequired = false;
    protected com.alachisoft.ncache.common.protobuf.AddDependencyCommandProtocol.AddDependencyCommand _commandInstance;
    AddDependencyCommandProtocol.AddDependencyCommand.Builder builder;

    /**
     * Creates a new instance of AddCommand
     *
     * @throws java.io.UnsupportedEncodingException
     * @throws java.io.IOException
     */
    public AddDependencyCommand(String key, CacheDependency dependency,
                                boolean isResyncRequired) throws CommandException {
        name = "ADDDEPENDENCY";
        this.key = key;
        this.dependency = dependency;
        this.isResyncRequired = isResyncRequired;
        build();
    }

    private void build() throws CommandException {
        if (key == null) {
            throw new IllegalArgumentException("Value cannot be null."+System.lineSeparator()+"Parameter name: key");
        }
        if (key.equals("")) {
            throw new IllegalArgumentException("Value cannot be empty.\nParameter name: key");
        }
        builder = AddDependencyCommandProtocol.AddDependencyCommand.newBuilder();
        try {
        builder.setKey(key).setIsResync(isResyncRequired).setDependency(DependencyHelper.getDependencyProtocol(dependency));
        } catch (Exception ex) {
            throw new CommandException(ex.getMessage());
        }
    }

    public void createCommand() throws CommandException {
         builder.setRequestId(this.getRequestId());
        _commandInstance =builder.build();
    }

    /**
     * @return
     */
    public CommandType getCommandType() {
        return CommandType.ADD_DEPENDENCY;
    }

    @Override
    public RequestType getCommandRequestType() {
        return RequestType.AtomicWrite;
    }

    @Override
    protected  void serializeCommandInternal(ByteArrayOutputStream stream) throws IOException
    {
        _commandInstance.writeTo(stream);
    }
    @Override
    protected short getCommandHandle()
    {
        return (short)CommandProtocol.Command.Type.ADD_DEPENDENCY.getNumber();
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy