/*******************************************************************************
* Copyright (c) 2013, Perforce Software
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
*
* Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
******************************************************************************/
package com.perforce.p4java.extension.operation;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import org.apache.log4j.Logger;
import com.perforce.p4java.exception.MessageSeverityCode;
import com.perforce.p4java.extension.command.PerforceDomain;
import com.perforce.p4java.extension.command.RequestImpl;
import com.perforce.p4java.extension.command.ServerMessages;
import com.perforce.p4java.extension.exception.ServerAccessException;
import com.perforce.p4java.extension.exception.ServerConnectionException;
import com.perforce.p4java.extension.exception.ServerErrorException;
import com.perforce.p4java.extension.server.Server;
import com.perforce.p4java.extension.utility.CommandUtil;
import com.perforce.p4java.extension.utility.Validate;
import com.perforce.p4java.impl.mapbased.rpc.msg.RpcMessage;
/**
*
*/
public abstract class Command<T> extends RequestImpl {
private static final Logger log = Logger.getLogger(Command.class);
protected List<String> arguments;
protected String client;
protected PerforceDomain model;
protected boolean nullExpected = false;
protected String command;
protected Map<String, Object> inputMap;
public Command(PerforceDomain model) {
Validate.notNull(model, "model must not be null to Command");
this.model = model;
arguments = new ArrayList<String>();
}
/**
* @see com.perforce.p4java.extension.command.Request#getCommand()
*/
public String getCommand() {
return model.getCommand();
}
/**
* @see com.perforce.p4java.extension.command.Request#getCommandArgs()
*/
public String[] getCommandArgs() {
return arguments.toArray(new String[arguments.size()]);
}
/**
* @see com.perforce.p4java.extension.command.Request#getClient()
*/
public String getClient() {
return client;
}
public Map<String, Object> getInputMap() {
return inputMap;
}
public T executeModelRequest(Server server)
throws ServerConnectionException, ServerAccessException,
ServerErrorException {
command = model.getCommand();
Map<String, Object>[] maps = server.execute(this);
T t;
if (emptyMap(maps))
return null;
if ((t = process(maps)) == null) {
checkForErrors(maps);
return null;
}
return t;
}
private boolean emptyMap(Map<String, Object>[] maps) {
return !(maps != null && maps.length > 0 && maps[0] != null);
}
protected abstract T process(Map<String, Object>[] maps);
protected void checkForErrors(Map<String, Object>[] maps)
throws ServerConnectionException, ServerAccessException,
ServerErrorException {
for (Map<String, Object> map : maps) {
String msg = ServerMessages.getServerMessage(map,
MessageSeverityCode.E_INFO);
for (int i = 0;; i++) {
String code = "code" + i;
if (map.containsKey(code)) {
processError(map, (String) map.get(code), msg);
} else
break;
}
}
}
protected void processError(Map<String, Object> map, String code,
String message) throws ServerConnectionException,
ServerAccessException, ServerErrorException {
if (code == null)
return;
int severity = RpcMessage.getSeverity(code);
if (ServerMessages.isAuthFail(message)) {
log.error(message);
throw new ServerAccessException(message);
} else if (severity >= MessageSeverityCode.E_WARN) {
log.error(message);
throw new ServerErrorException(message, code);
} else if (CommandUtil.isNotSeriousErrorButNeedReport(code)) {
log.error(message);
throw new ServerErrorException(message, code);
}
}
}