package com.perforce.api;
import java.io.*;
import java.util.*;
/*
* Copyright (c) 2001, Perforce Software, All rights reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
/**
* Representation of a source control group. Each instance can store one p4
* group's information. This is custom defined class at Quinstreet
*
* @author <a href="mailto:srporeddy@yahoo.com">Sivananda Poreddy</a>
* @version $Date: 2007/02/12 $ $Revision: #1 $
*/
public final class Group extends SourceControlObject {
private String id = "";
private Vector users = new Vector();
private static HashDecay groups = null;
/**
* Default no-argument constructor.
*/
public Group() {
super();
getCache();
}
/**
* Constructor that accepts the id of the group. This simply creates an
* instance that has the id set. No other information in the class will be
* present until the <a href="#sync()">sync() method is called.
*
* @param id Id for the Group.
*/
public Group(String id) {
this();
this.id = id;
}
private static HashDecay setCache() {
if (null == groups) {
groups = new HashDecay(600000);
groups.start();
}
return groups;
}
public HashDecay getCache() {
return setCache();
}
/**
* Sets the id for this Group.
*
* @param id Id for the Group.
*/
public void setId(String id) {
this.id = id;
}
/**
* Returns an <code>Enumeration</code> of all <code>Group</code> objects.
*/
public static synchronized Enumeration getGroups() {
return getGroups(null);
}
/**
* Returns an <code>Enumeration</code> of all <code>Group</code> objects.
*/
public static synchronized Enumeration getGroups(Env env) {
String l;
Group g;
String[] cmd = { "p4", "groups" };
try {
P4Process p = new P4Process(env);
p.exec(cmd);
while (null != (l = p.readLine())) {
if (l.startsWith("#")) { continue; }
g = getGroup(env, l);
g.setEnv(env);
}
p.close();
}
catch (IOException ex) {
}
return setCache().elements();
}
/**
* Gets the group information for the specified group.
* If that group has been gotten before, that group information will not
* be reloaded from P4. It is instead loaded from an internal HashDecay.
* <p>
* If the group information must be up to date, then the
* <a href="#sync()">sync() method must be called.
*
* @param env Source control environment to use.
* @param gid The group id of the group information to get from p4.
*/
public static synchronized Group getGroup(Env env, String gid) {
if (null == gid) {
return null;
}
if (gid.trim().equals("")) {
return null;
}
String tid;
int i, left = 0;
gid = gid.trim();
if (-1 == (left = gid.indexOf("<"))) {
tid = gid;
} else {
tid = gid.substring(0,left-1).trim();
}
Group g = (Group)setCache().get(gid);
if (null != g) {
return g;
} else {
g = new Group(tid);
}
g.setEnv(env);
groups.put(gid,g);
return g;
}
/**
* Returns the id for this Group.
*
* @return Id for the Group.
*/
public String getId() {
return id;
}
/**
* Synchronizes the Group information with P4. This method must be called
* to ensure that this contains the latest information from p4. This form
* of the method can be used to change the Group Id in at the same time.
*
* @param id The Group id for this to synchronize from p4.
*/
public void sync(String id) {
this.id = id;
sync();
}
/**
* TBD: The <code>commit</code> method is not working yet.
*/
public void commit() {
}
/**
* Synchronizes the Group information with P4. This method must be called
* to ensure that this contains the latest information from p4.
*/
public void sync() {
if (! outOfSync(300000)) return;
String l;
String[] cmd = { "p4", "group", "-o", "id" };
cmd[3] = id;
try {
P4Process p = new P4Process(getEnv());
p.exec(cmd);
while (null != (l = p.readLine())) {
if (l.startsWith("#")) { continue; }
if (l.startsWith("Group:")) {
id = l.substring(7).trim();
} else if (l.startsWith("Users:")) {
while (null != (l = p.readLine())) {
users.add(l);
}
} else {
continue;
}
}
p.close();
inSync();
}
catch (IOException ex) {
}
}
public String toString() {
return id;
}
/**
* Returns the list of users for a given group
*/
public Vector getUsers() throws PerforceException {
if (null != users) {
return users;
} else {
return null;
}
}
public String toXML() {
StringBuffer sb = new StringBuffer("<group id=\"");
sb.append(getId());
sb.append("\"/>");
return sb.toString();
}
}