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 perforce counters. This class can be used to set and get * the value of a counter. It can also be used to determine who reviews the * changes represented by a particular value. * * @author <a href="mailto:david@markley.cc">David Markley</a> * @version $Date: 2002/01/10 $ $Revision: #1 $ */ public final class Counter extends SourceControlObject { private String name = ""; private int value = -1; private static HashDecay counters = null; /** Indicates that the Counter is pending submission. */ public final static int PENDING = 1; /** Indicates that the Counter has been submitted. */ public final static int SUBMITTED = 2; /** * Default no-argument constructor. */ public Counter() { super(); getCache(); } public Counter(String name) { this(null, name); } public Counter(Env environ, String name) { this(); this.setEnv(environ); this.name = name; } private static HashDecay setCache() { if (null == counters) { counters = new HashDecay(300000); counters.start(); } return counters; } public HashDecay getCache() { return setCache(); } public String getName() { return this.name; } public void setName(String name) { this.name = name; } /** * Sets the value for the Counter. * * @param value New counter value */ public void setValue(int value) throws SubmitException { boolean valid = false; String l; this.value = value; String[] cmd = { "p4", "counter", this.name, String.valueOf(value)}; try { P4Process p = new P4Process(getEnv()); p.exec(cmd); while (null != (l = p.readLine())) { if (l.startsWith("Counter") && l.endsWith("set.")) valid = true; } p.close(); } catch (Exception ex) { throw new SubmitException(ex.getMessage()); } if (! valid) throw new SubmitException("Counter "+name+" not set."); } /** * Returns the number of this Counter. */ public int getValue() throws PerforceException { String l; String[] cmd = { "p4", "counter", this.name}; try { P4Process p = new P4Process(getEnv()); p.exec(cmd); l = p.readLine(); value = Integer.valueOf(l).intValue(); while (null != (l = p.readLine())) { } p.close(); } catch (Exception ex) { throw new PerforceException(ex.getMessage()); } return value; } /** * This is a no-op. Bad OO. */ public void commit() { } /** * This is a no-op. Bad OO. */ public void sync() { } /* public static Counter[] getCounters(Env env, String path, int max, String start, String end, boolean use_integs, String ufilter) { String tpath = path; if (null != start && ! start.trim().equals("")) { tpath += "@"+start; if (null != end && ! end.trim().equals("")) { tpath += ","+end; } } String[] cmd; if (use_integs) { cmd = new String[9]; cmd[7] = "-i"; } else { cmd = new String[8]; } cmd[0] = "p4"; cmd[1] = "counters"; cmd[2] = "-m"; cmd[3] = String.valueOf(max); cmd[4] = "-l"; cmd[5] = "-s"; cmd[6] = "submitted"; cmd[cmd.length-1] = tpath; Vector v = new Vector(); Counter[] chngs; StringTokenizer st; int num; String l, id, description = ""; User user; Counter c = null; String modtime, client_name; try { P4Process p = new P4Process(env); p.exec(cmd); while (null != (l = p.readLine())) { if (l.startsWith("Counter")) { st = new StringTokenizer(l); if (! st.nextToken().equals("Counter")) continue; num = Integer.parseInt(st.nextToken()); if (! st.nextToken().equals("on")) continue; modtime = st.nextToken(); if (! st.nextToken().equals("by")) continue; id = st.nextToken(); int pos = id.indexOf("@"); client_name = id.substring(pos+1); id = id.substring(0,pos); user = User.getUser(id); if (null != c) { c.setDescription(description); } description = ""; c = new Counter(num); c.setEnv(env); c.setUser(user); c.setClientName(client_name); c.setModtimeString(modtime); if (null == ufilter || id.equals(ufilter)) { v.addElement(c); } } else { description += l + "\n"; } } if (null != c) { c.setDescription(description); } p.close(); } catch (IOException ex) { Debug.out(Debug.ERROR, ex); } chngs = new Counter[v.size()]; for (int i = 0; i < v.size(); i++) { chngs[i] = (Counter)v.elementAt(i); counters.put(new Integer(chngs[i].getNumber()), chngs[i]); } return chngs; } */ /** * Determine the changelists that need to be reviewed. This method * returns an array of Changes that need to be reviewed. */ public Change[] review() throws PerforceException { Vector changes = new Vector(); Change[] chngs = new Change[0]; Change chng; User usr; int num; String uid; String email; String name, t; StringTokenizer st; String l; String[] cmd = { "p4", "review", "-t", this.name}; try { P4Process p = new P4Process(getEnv()); p.exec(cmd); while (null != (l = p.readLine())) { if (l.startsWith("Change")) { st = new StringTokenizer(l); if (! st.nextToken().equals("Change")) continue; num = Integer.parseInt(st.nextToken()); uid = st.nextToken(); email = st.nextToken("<> \t"); name = st.nextToken("<> ()\t"); try { while (null != (t = st.nextToken("<> ()\t"))) { name += (" "+t); } } catch (NoSuchElementException ex) { } // System.out.print("U: "+uid+", E: "+email+", N: "+name+"\n"); chng = new Change(getEnv()); chng.setNumber(num); usr = new User(uid); usr.setEnv(getEnv()); usr.setEmail(email); usr.setFullName(name); chng.setUser(usr); changes.addElement(chng); } } p.close(); } catch (Exception ex) { throw new PerforceException(ex.getMessage()); } if (0 == changes.size()) return null; return (Change[])changes.toArray(chngs); } public String toXML() { StringBuffer sb = new StringBuffer("<counter name=\""); sb.append(this.name); sb.append("\" value=\""); sb.append(this.value); sb.append("\"/>"); return sb.toString(); } /** * Used for testing. * * @deprecated Actually in use, but this keeps it out of the docs. */ public static void main(String[] argv) { Debug.setDebugLevel(Debug.VERBOSE); Properties props = new Properties(System.getProperties()); try { props.load(new BufferedInputStream(new FileInputStream(argv[0]))); System.setProperties(props); } catch (Exception e) { System.err.println("Unable to load properties."); e.printStackTrace(System.err); System.exit(-1); } Env environ = new Env(props); System.out.println(environ); Counter cnt = new Counter(environ, "testval"); try { cnt.setValue(42); System.out.println("VAL(1): "+cnt.getValue()); cnt.setValue(66); System.out.println("VAL(2): "+cnt.getValue()); cnt = new Counter(environ, "testval"); System.out.println("VAL(3): "+cnt.getValue()); cnt.setValue(6850); System.out.println("VAL(4): "+cnt.getValue()); Change[] chngs = cnt.review(); User[] usrs; int i, max = 0; for (i = 0; i < chngs.length; i++) { System.out.println("Must review change(1): "+chngs[i].getNumber()); if (max < chngs[i].getNumber()) max = chngs[i].getNumber(); usrs = chngs[i].reviews(); if (null != usrs) { for (int j = 0; j < usrs.length; j++) { System.out.println("\t"+usrs[j].getFullName()); } } } cnt.setValue(max); chngs = cnt.review(); if (null != chngs && chngs.length > 0) { for (i = 0; i < chngs.length; i++) { System.out.println("Must review change(2): "+chngs[i].getNumber()); if (max < chngs[i].getNumber()) max = chngs[i].getNumber(); } } } catch (PerforceException e) { System.err.println("Unable to set counter value."); e.printStackTrace(); System.exit(-1); } System.err.println("Cleaning up."); Utils.cleanUp(); } }
# | Change | User | Description | Committed | |
---|---|---|---|---|---|
#4 | 1338 | David Markley | Corrected the Change parsing. | ||
#3 | 1337 | David Markley | Modified to ensure that all created processes are terminated. | ||
#2 | 1274 | David Markley |
Added the ability to get all the counters at once. Changes the sync method in the Cacheable interface to throw a PerforceException if something goes wrong during an object sync. |
||
#1 | 1272 | David Markley | Added Counter class and ability to review changes. |