package com.perforce.api;
import java.io.*;
import java.util.*;
public class Info
extends SourceControlObject
implements Comparable
{
private static Info info;
private String userName;
private String clientName;
private String clientHost;
private String clientRoot;
private String currentDirectory;
private String clientAddress;
private String serverAddress;
private String serverRoot;
private String serverVersion;
private String serverLicense;
public static synchronized Info getInstance()
{
if(info == null) info = new Info();
return info;
}
public static synchronized Info getInstance(Env env)
{
return new Info(env);
}
public Info() {
sync();
}
public Info(Env env) {
setEnv(env);
sync();
}
public int compareTo(Object o) {
return getClass().getName().compareTo(o.getClass().getName());
}
/** Returns the HashDecay instance for this class */
public HashDecay getCache()
{
throw new UnsupportedOperationException("not implemented yet");
}
/**
* Stores this object back into Perforce, creating it if it
* didn't already exist.
*/
public void commit()
throws CommitException
{
// Info is immutable, so this method doesn't have to do anything.
}
public void sync()
//throws PerforceException
{
String l;
String[] cmd = { "p4", "info" };
try {
P4Process p = new P4Process(getEnv());
p.exec(cmd);
while (null != (l = p.readLine())) {
//System.err.println(l);
if (l.startsWith("#")) { continue; }
int colon = l.indexOf(":");
if(colon < 0) continue;
String label = l.substring(0, colon).toLowerCase();
if (label.equals("user name")) {
userName = l.substring(colon+1).trim();
} else if (label.equals("client name")) {
clientName = l.substring(colon+1).trim();
} else if (label.equals("client host")) {
clientHost = l.substring(colon+1).trim();
} else if (label.equals("client root")) {
clientRoot = l.substring(colon+1).trim();
} else if (label.equals("current directory")) {
currentDirectory = l.substring(colon+1).trim();
} else if (label.equals("client address")) {
clientAddress = l.substring(colon+1).trim();
} else if (label.equals("server address")) {
serverAddress = l.substring(colon+1).trim();
} else if (label.equals("server root")) {
serverRoot = l.substring(colon+1).trim();
} else if (label.equals("server version")) {
serverVersion = l.substring(colon+1).trim();
} else if (label.equals("server license")) {
serverLicense = l.substring(colon+1).trim();
}
}
p.close();
} catch (IOException ex) { Debug.out(Debug.ERROR, ex); }
refreshUpdateTime();
}
public String getUserName() { return userName; }
public String getClientName() { return clientName; }
public String getClientHost() { return clientHost; }
public String getClientRoot() { return clientRoot; }
public String getCurrentDirectory() { return currentDirectory; }
public String getClientAddress() { return clientAddress; }
public String getServerAddress() { return serverAddress; }
public String getServerRoot() { return serverRoot; }
public String getServerVersion() { return serverVersion; }
public String getServerLicense() { return serverLicense; }
/**
* Returns a string containing the object in XML form.
*/
private static String repr(String s)
{
if(s == null) return "null";
else return "\"" + s + "\"";
}
public String toXML() {
StringBuffer sb = new StringBuffer("<info");
sb.append(" userName=");
sb.append(repr(getUserName()));
sb.append(" clientName=");
sb.append(repr(getClientName()));
sb.append(" clientHost=");
sb.append(repr(getClientHost()));
sb.append(" clientRoot=");
sb.append(repr(getClientRoot()));
sb.append(" currentDirectory=");
sb.append(repr(getCurrentDirectory()));
sb.append(" clientAddress=");
sb.append(repr(getClientAddress()));
sb.append(" serverAddress=");
sb.append(repr(getServerAddress()));
sb.append(" serverRoot=");
sb.append(repr(getServerRoot()));
sb.append(" serverVersion=");
sb.append(repr(getServerVersion()));
sb.append(" serverLicense=");
sb.append(repr(getServerLicense()));
sb.append("/>");
return sb.toString();
}
public String toString() {
StringBuffer sb = new StringBuffer("Info\n");
sb.append(" userName=");
sb.append(repr(getUserName()));
sb.append("\n clientName=");
sb.append(repr(getClientName()));
sb.append("\n clientHost=");
sb.append(repr(getClientHost()));
sb.append("\n clientRoot=");
sb.append(repr(getClientRoot()));
sb.append("\n currentDirectory=");
sb.append(repr(getCurrentDirectory()));
sb.append("\n clientAddress=");
sb.append(repr(getClientAddress()));
sb.append("\n serverAddress=");
sb.append(repr(getServerAddress()));
sb.append("\n serverRoot=");
sb.append(repr(getServerRoot()));
sb.append("\n serverVersion=");
sb.append(repr(getServerVersion()));
sb.append("\n serverLicense=");
sb.append(repr(getServerLicense()));
sb.append("\n");
return sb.toString();
}
}