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

com.perforce.p4java.impl.mapbased.rpc.CommandEnv Maven / Gradle / Ivy

Go to download

P4Java, the Perforce Java API is a Java-native API for accessing Perforce SCM services from within Java applications, servlets, plug-ins, and other Java contexts.

The newest version!
/*
 * Copyright 2009 Perforce Software Inc., All Rights Reserved.
 */
package com.perforce.p4java.impl.mapbased.rpc;

import com.perforce.p4java.Log;
import com.perforce.p4java.exception.P4JavaException;
import com.perforce.p4java.impl.mapbased.rpc.connection.RpcConnection;
import com.perforce.p4java.impl.mapbased.rpc.func.RpcFunctionMapKey;
import com.perforce.p4java.impl.mapbased.rpc.func.proto.ProtocolCommand;
import com.perforce.p4java.impl.mapbased.rpc.packet.helper.RpcPacketFieldRule;
import com.perforce.p4java.server.callback.IFilterCallback;
import com.perforce.p4java.server.callback.IParallelCallback;
import com.perforce.p4java.server.callback.IProgressCallback;
import com.perforce.p4java.server.callback.IStreamingCallback;

import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Used to package up the Perforce function environment for a single
 * Perforce command across multiple RPC function calls.
 * 

* In particular, we need to keep things like file handles, * arbitrary RPC function arguments, etc., around for use during * complex long-running commands that span many dispatch calls * in loop or duplex mode, etc., in response to single * user commands like 'sync'. *

* Note that this is in distinction to a) the command's external * environment (in the ExternalEnv class), and b) the command's * individual function environments, */ public class CommandEnv { /** * Max number of live handlers per command cycle. * Value copied straight from the C++ API. */ public static final int MAX_HANDLERS = 10; /** * Sequence used by operating system to separate lines in text files. * Default to "\n" if it is not available. */ public static final String LINE_SEPARATOR = System.getProperty("line.separator", "\n"); /** * P4Java's version of the notorious handler class * in the C++ API. Basically used (and abused) in * much the same way, mostly (at least) until I can * work out a better way to do things, at which * point it's likely to be factored out elsewhere. */ public class RpcHandler { private String name = null; private String type = null; private boolean error = false; private File file = null; private Map map = null; public RpcHandler(String name, boolean error, File file) { this.name = name; this.error = error; this.file = file; this.map = new HashMap(); } public String getName() { return this.name; } public void setName(String name) { this.name = name; } public String getType() { return this.type; } public void setType(String type) { this.type = type; } public boolean isError() { return this.error; } public void setError(boolean error) { this.error = error; } public File getFile() { return this.file; } public void setFile(File file) { this.file = file; } public Map getMap() { return this.map; } public void setMap(Map map) { this.map = map; } } /** * The parent server object */ private RpcServer server = null; /** * The current user function that started this all... */ private RpcCmdSpec cmdSpec = null; /** * The result maps that will ultimately be passed back to the user levels. */ private List> resultMaps = null; /** * The last result map that's being populated by fstatPartial. */ private Map lastResultMap = null; /** * State map for storing arbitrary state across RPC function * calls. */ private Map stateMap = null; /** * C++ API-like handlers. Will probably be refactored * out later when I can work out what to do with them. * NOTE: code below relies on the handlers array elements * being initialized to null. */ private RpcHandler[] handlers = new RpcHandler[MAX_HANDLERS]; /** * Protocol specs (in command form). We carry this around * for possible reference only; it may disappear in future * refactorings if it's never used. */ private ProtocolCommand protocolSpecs = null; private Map serverProtocolSpecsMap = null; private IProgressCallback progressCallback = null; private int cmdCallBackKey = 0; private boolean syncInPlace = false; private boolean nonCheckedSyncs = false; private boolean dontWriteTicket = false; private boolean streamCmd = false; private RpcPacketFieldRule fieldRule = null; private IStreamingCallback streamingCallback = null; private int streamingCallbackKey = 0; private IFilterCallback filterCallback = null; private IParallelCallback parallelCallback = null; private long syncTime = 0; private boolean nullSync = false; private boolean bufferOutput = false; /** * The Perforce RPC connection in use for this command. */ private RpcConnection rpcConnection = null; private boolean userCanceled = false; // true if the user tried to cancel the command public CommandEnv(RpcServer server, RpcCmdSpec cmdSpec, RpcConnection rpcConnection, ProtocolCommand protocolSpecs, Map serverProtocolSpecsMap, IProgressCallback progressCallback, int cmdCallBackKey, boolean syncInPlace, boolean nonCheckedSyncs) { this.server = server; this.cmdSpec = cmdSpec; this.rpcConnection = rpcConnection; this.protocolSpecs = protocolSpecs; this.stateMap = new HashMap(); this.serverProtocolSpecsMap = serverProtocolSpecsMap; this.progressCallback = progressCallback; this.cmdCallBackKey = cmdCallBackKey; this.syncInPlace = syncInPlace; this.nonCheckedSyncs = nonCheckedSyncs; } public boolean addHandler(RpcHandler handler) { for (int i = 0; i < handlers.length; i++) { if ((handlers[i] != null) && (handlers[i].name != null) && handlers[i].name.equalsIgnoreCase(handler.name)) { } if (handlers[i] == null) { handlers[i] = handler; return true; } } return false; } /** * Handle a result by either adding it to the resultsMapVec * for later processing or passing it up to the streaming results * callback handler. * * @param resultMap resultMap */ public void handleResult(Map resultMap) { if (lastResultMap != null) { lastResultMap.putAll(resultMap); } else { lastResultMap = resultMap; } if (streamingCallback != null) { if (!userCanceled) { try { userCanceled = !streamingCallback.handleResult(lastResultMap, streamingCallbackKey); } catch (P4JavaException exc) { Log.error("caught exception from streaming callback handler (key: " + streamingCallbackKey + "): " + exc.getLocalizedMessage()); Log.exception(exc); } } } else { resultMaps.add(lastResultMap); } lastResultMap = null; } /** * Handle a partial result by either adding it to the resultsMapVec * for later processing or passing it up to the streaming results * callback handler. * * @param resultMap resultMap */ public void handlePartialResult(Map resultMap) { if (lastResultMap == null) { lastResultMap = new HashMap(); } if (streamingCallback != null) { // try { // if( !streamingCallback.handlePartialResult(lastResultMap, streamingCallbackKey) ) { lastResultMap.putAll(resultMap); /* } } catch (P4JavaException exc) { Log.error("caught exception from streaming callback handler (key: " + streamingCallbackKey + "): " + exc.getLocalizedMessage()); Log.exception(exc); lastResultMap.putAll(resultMap); }*/ } else { lastResultMap.putAll(resultMap); } } public void resetPartialResult() { lastResultMap = null; } public RpcHandler getHandler(String handlerName) { for (RpcHandler handler : handlers) { if ((handler != null) && (handlerName != null) && (handler.getName() != null) && handler.getName().equalsIgnoreCase(handlerName)) { return handler; } } return null; } public RpcCmdSpec getCmdSpec() { return this.cmdSpec; } public void setCmdSpec(RpcCmdSpec cmdSpec) { this.cmdSpec = cmdSpec; } public List> getResultMaps() { return this.resultMaps; } public void setResultMaps(List> resultMaps) { this.resultMaps = resultMaps; } public void clearLastResultMap() { lastResultMap = null; } public Map getStateMap() { return this.stateMap; } public void setStateMap(Map stateMap) { this.stateMap = stateMap; } public ProtocolCommand getProtocolSpecs() { return this.protocolSpecs; } public int getServerProtocolLevel() { if (serverProtocolSpecsMap == null) { return -1; } if (serverProtocolSpecsMap.containsKey(RpcFunctionMapKey.SERVER2)) { return Integer.parseInt((String) serverProtocolSpecsMap.get(RpcFunctionMapKey.SERVER2)); } if (serverProtocolSpecsMap.containsKey(RpcFunctionMapKey.SERVER)) { return Integer.parseInt((String) serverProtocolSpecsMap.get(RpcFunctionMapKey.SERVER)); } return -1; } public void setProtocolSpecs(ProtocolCommand protocolSpecs) { this.protocolSpecs = protocolSpecs; } public RpcConnection getRpcConnection() { return this.rpcConnection; } public void setRpcConnection(RpcConnection rpcConnection) { this.rpcConnection = rpcConnection; } public RpcHandler[] getHandlers() { return this.handlers; } public void setHandlers(RpcHandler[] handlers) { this.handlers = handlers; } public void newHandler() { // Does nothing at the moment, as a standin for the C++ API version // FIXME -- HR. } public Map getServerProtocolSpecsMap() { return serverProtocolSpecsMap; } public void setServerProtocolSpecsMap(Map serverProtocolSpecsMap) { this.serverProtocolSpecsMap = serverProtocolSpecsMap; } public IProgressCallback getProgressCallback() { return progressCallback; } public void setProgressCallback(IProgressCallback progressCallback) { this.progressCallback = progressCallback; } public int getCmdCallBackKey() { return this.cmdCallBackKey; } public void setCmdCallBackKey(int cmdCallBackKey) { this.cmdCallBackKey = cmdCallBackKey; } public boolean isUserCanceled() { return this.userCanceled; } public void setUserCanceled(boolean userCanceled) { this.userCanceled = userCanceled; } public boolean isSyncInPlace() { return syncInPlace; } public void setSyncInPlace(boolean syncInPlace) { this.syncInPlace = syncInPlace; } public IStreamingCallback getStreamingCallback() { return streamingCallback; } public void setStreamingCallback(IStreamingCallback streamingCallback) { this.streamingCallback = streamingCallback; } public int getStreamingCallbackKey() { return streamingCallbackKey; } public void setStreamingCallbackKey(int streamingCallbackKey) { this.streamingCallbackKey = streamingCallbackKey; } public boolean isNonCheckedSyncs() { return nonCheckedSyncs; } public void setNonCheckedSyncs(boolean nonCheckedSyncs) { this.nonCheckedSyncs = nonCheckedSyncs; } public boolean isDontWriteTicket() { return dontWriteTicket; } public void setDontWriteTicket(boolean dontWriteTicket) { this.dontWriteTicket = dontWriteTicket; } public boolean isStreamCmd() { return streamCmd; } public void setStreamCmd(boolean streamCmd) { this.streamCmd = streamCmd; } public RpcPacketFieldRule getFieldRule() { return fieldRule; } public void setFieldRule(RpcPacketFieldRule fieldRule) { this.fieldRule = fieldRule; } public IFilterCallback getFilterCallback() { return filterCallback; } public void setFilterCallback(IFilterCallback filterCallback) { this.filterCallback = filterCallback; } public IParallelCallback getParallelCallback() { return parallelCallback; } public void setParallelCallback(IParallelCallback parallelCallback) { this.parallelCallback = parallelCallback; } public RpcServer getServer() { return server; } public long getSyncTime() { return syncTime; } public void setSyncTime(long syncTime) { this.syncTime = syncTime; } public boolean isNullSync() { return nullSync; } public void setNullSync(boolean nullSync) { this.nullSync = nullSync; } public boolean isBufferOutput() { return bufferOutput; } public void setBufferOutput(boolean bufferOutput) { this.bufferOutput = bufferOutput; } }





© 2015 - 2025 Weber Informatics LLC | Privacy Policy