Author: tchemit Date: 2008-04-03 21:51:50 +0000 (Thu, 03 Apr 2008) New Revision: 321 Added: trunk/lutinvcs/lutinvcs-core/src/main/java/org/codelutin/vcs/VCSActionManager.java Log: VCSActionManager Added: trunk/lutinvcs/lutinvcs-core/src/main/java/org/codelutin/vcs/VCSActionManager.java =================================================================== --- trunk/lutinvcs/lutinvcs-core/src/main/java/org/codelutin/vcs/VCSActionManager.java (rev 0) +++ trunk/lutinvcs/lutinvcs-core/src/main/java/org/codelutin/vcs/VCSActionManager.java 2008-04-03 21:51:50 UTC (rev 321) @@ -0,0 +1,157 @@ +/** + * # #% Copyright (C) 2008 Code Lutin, Tony Chemit + * This program is free software; you + * can redistribute it and/or modify it under the terms of the GNU General + * Public License as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. This program is + * distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; + * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A + * PARTICULAR PURPOSE. See the GNU General Public License for more details. You + * should have received a copy of the GNU General Public License along with this + * program; if not, write to the Free Software Foundation, Inc., 59 Temple Place + * - Suite 330, Boston, MA 02111-1307, USA. + * # #% + */ +package org.codelutin.vcs; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import static org.codelutin.i18n.I18n._; + +import java.util.Date; +import java.util.concurrent.BlockingQueue; +import java.util.concurrent.PriorityBlockingQueue; + +/** + * Manager used to fired vcs actions + * <p/> + * use a {@link org.codelutin.vcs.VCSActionManager.VCSActionThread}. + * + * @author chemit + */ +public class VCSActionManager { + + /** vcs actions runner thread */ + protected VCSActionThread thread; + + /** queue */ + protected PriorityBlockingQueue<QueueItem> queue; + + public boolean add(VCSAction action, VCSFileState[] states) { + QueueItem queueItem = new QueueItem(action, states); + return queue.add(queueItem); + } + + public VCSActionManager(VCSHandler handler) { + queue = new PriorityBlockingQueue<QueueItem>(1024); + thread = new VCSActionThread(handler, queue); + thread.start(); + } + + + /** @author chemit */ + public static class VCSActionThread extends Thread implements VCSHandlerEventListener { + + /** to use log facility, just put in your code: log.info(\"...\"); */ + static private Log log = LogFactory.getLog(VCSActionThread.class); + + /** queue */ + protected BlockingQueue<QueueItem> queue; + + /** handler to use for vcs operations */ + protected VCSHandler handler; + + public VCSActionThread(VCSHandler handler, BlockingQueue<QueueItem> queue) { + super("VCSAction Thread [" + handler + "]"); + this.handler = handler; + this.queue = queue; + this.handler.addVCSHandlerEventListener(this); + } + + @Override + public void run() { + log.info("Start " + getName() + " at " + new Date()); + boolean run = true; + QueueItem item = null; + try { + while (run) { + // item qui sert a faire la simulation courante + + log.info("waiting for action..."); + item = queue.take(); + if (item.getAction() == null) { + // this is a special case to stop thread + run = false; + continue; + } + log.info("Just take " + item); + } + } + catch (Throwable eee) { + if (log.isWarnEnabled()) { + log.warn(_("lutinvcs.error.thread.action", item), eee); + } + } + log.info("Stop " + getName() + " at " + new Date()); + } + + + public void init(VCSHandlerEvent event) { + } + + public void open(VCSHandlerEvent event) { + } + + public void close(VCSHandlerEvent event) { + + handler.removeVCSHandlerEventListener(this); + if (!queue.isEmpty()) { + queue.clear(); + } + queue.add(new QueueItem(null, new VCSFileState[0])); + } + } + + public static class QueueItem implements Comparable<QueueItem> { + + protected long time; + protected VCSFileState[] states; + protected VCSAction action; + + + public QueueItem(VCSAction action, VCSFileState[] states) { + this.time = System.nanoTime(); + this.states = states; + this.action = action; + } + + public VCSAction getAction() { + return action; + } + + public VCSFileState[] getStates() { + return states; + } + + @Override + public boolean equals(Object o) { + return this == o || o instanceof QueueItem && time == ((QueueItem) o).time; + } + + @Override + public int hashCode() { + return (int) (time ^ (time >>> 32)); + } + + @Override + public String toString() { + return super.toString() + ", time:" + time + ", action:" + action + ", size:" + (states == null ? 0 : states.length); + } + + public int compareTo(QueueItem o) { + return time == o.time ? 0 : time > o.time ? 1 : -1; + } + } + + +}