package com.perforce.workspace.tjuricek.initializer; import com.perforce.p4java.client.IClient; import com.perforce.p4java.exception.P4JavaException; import com.perforce.p4java.server.IOptionsServer; import java.util.HashMap; import java.util.Map; /** * Wraps the IOptionsServer with helper methods. */ public class ServerWrapper { private String charset = null; private IOptionsServer optionsServer; private String currentLogin = null; private String currentPassword = null; private String currentClient; private Map<String, IClient> clientCache = new HashMap<>(); public ServerWrapper(IOptionsServer optionsServer) { this.optionsServer = optionsServer; } public IOptionsServer getOptionsServer() { return optionsServer; } public String getCharset() { return charset; } public void setCharset(String charset) { this.charset = charset; } public void setUser(String login, String password) { if ((currentLogin == null || !currentLogin.equals(login)) || (currentPassword == null && password != null) || (currentPassword != null && !currentPassword.equals(password))) { currentLogin = login; currentPassword = password; try { connectAsUser(login, password); } catch (P4JavaException e) { throw new IllegalStateException(e); } } } public void connectAsUser(String user, String password) throws P4JavaException { optionsServer.setUserName(user); optionsServer.connect(); if (charset != null) { optionsServer.setCharsetName(charset); } if (password != null) { optionsServer.login(password); } } public void setUserAndClient(String login, String password, String clientName) { setUser(login, password); if (!clientName.equals(currentClient)) { try { IClient client = null; if (clientCache.containsKey(clientName)) { client = clientCache.get(clientName); } else { client = optionsServer.getClient(clientName); clientCache.put(clientName, client); } optionsServer.setCurrentClient(client); currentClient = clientName; } catch (P4JavaException e){ throw new IllegalStateException(e); } } } public void resetClient() { if (optionsServer != null) { try { optionsServer.setCurrentClient(null); } catch (P4JavaException e) { throw new IllegalStateException(e); } } } public String getClientName() { return currentClient; } @Override public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; ServerWrapper that = (ServerWrapper) o; if (optionsServer != null ? !optionsServer.equals(that.optionsServer) : that.optionsServer != null) return false; return true; } @Override public int hashCode() { return optionsServer != null ? optionsServer.hashCode() : 0; } @Override public String toString() { return "ServerWrapper{" + "optionsServer=" + optionsServer + '}'; } }
# | Change | User | Description | Committed | |
---|---|---|---|---|---|
#3 | 9165 | tjuricek |
Some documentation framework and a fatjar style distribution for the initializer to run outside of a gradle plugin. Upped the version to 0.1.2, but this may need some testing before it's really guaranteed to work in standalone mode |
||
#2 | 9140 | tjuricek | 0.1.1 Fix NPE with client object cache, and update the perforce-gradle-plugin dependency | ||
#1 | 9088 | tjuricek |
0.1.0 First version of the initializer. This currently requires setting up a local version of the p4java_ext project, since that project is awating approval into the main jcenter repository. |