| /******************************************************************************* |
| * Copyright (c) 2012 Rushan R. Gilmullin and others. |
| * All rights reserved. This program and the accompanying materials |
| * are made available under the terms of the Eclipse Public License v1.0 |
| * which accompanies this distribution, and is available at |
| * http://www.eclipse.org/legal/epl-v10.html |
| * |
| * Contributors: |
| * Rushan R. Gilmullin - initial API and implementation |
| * Florian Pirchner - adjustings for osbp implementation |
| *******************************************************************************/ |
| |
| package org.eclipse.osbp.vaaclipse.addons.app.servlet; |
| |
| import java.util.HashMap; |
| import java.util.HashSet; |
| import java.util.LinkedList; |
| import java.util.Map; |
| import java.util.Queue; |
| import java.util.Set; |
| |
| import org.eclipse.osbp.vaaclipse.api.VaadinExecutorService; |
| |
| /** |
| * The Class VaadinExecutorServiceImpl. |
| */ |
| public class VaadinExecutorServiceImpl implements VaadinExecutorService { |
| |
| /** The runnables. */ |
| private Queue<Runnable> runnables = new LinkedList<Runnable>(); |
| |
| /** The keys. */ |
| private Set<Object> keys = new HashSet<Object>(); |
| |
| /** The runnable2 key. */ |
| private Map<Runnable, Object> runnable2Key = new HashMap<Runnable, Object>(); |
| |
| /** The runnables2. */ |
| private Queue<Runnable> runnables2 = new LinkedList<Runnable>(); |
| |
| /** |
| * Exec. |
| */ |
| public synchronized void exec() { |
| // System.out.println("exec called!"); |
| Runnable runnable; |
| while ((runnable = runnables.poll()) != null) { |
| try { |
| // System.out.println("Runnable 1"); |
| runnable.run(); |
| Object key = runnable2Key.remove(runnable); |
| keys.remove(key); |
| } catch (Throwable e) { |
| e.printStackTrace(); |
| } |
| } |
| |
| for (Runnable runnable2 : runnables2) { |
| try { |
| // System.out.println("Runnable 2"); |
| runnable2.run(); |
| } catch (Throwable e) { |
| e.printStackTrace(); |
| } |
| } |
| |
| // clean runnables that may added during runnables2 execution |
| runnables.clear(); |
| runnable2Key.clear(); |
| keys.clear(); |
| } |
| |
| /* (non-Javadoc) |
| * @see org.eclipse.osbp.vaaclipse.api.VaadinExecutorService#invokeLater(java.lang.Runnable) |
| */ |
| public synchronized void invokeLater(Runnable runnable) { |
| this.runnables.add(runnable); |
| } |
| |
| /* (non-Javadoc) |
| * @see org.eclipse.osbp.vaaclipse.api.VaadinExecutorService#invokeLater(java.lang.Object, java.lang.Runnable) |
| */ |
| @Override |
| public void invokeLater(Object key, Runnable runnable) { |
| if (!this.keys.contains(key)) { |
| this.keys.add(key); |
| this.runnable2Key.put(runnable, key); |
| this.runnables.add(runnable); |
| } |
| } |
| |
| /* (non-Javadoc) |
| * @see org.eclipse.osbp.vaaclipse.api.VaadinExecutorService#containsKey(java.lang.Object) |
| */ |
| @Override |
| public boolean containsKey(Object key) { |
| return this.keys.contains(key); |
| } |
| |
| /* (non-Javadoc) |
| * @see org.eclipse.osbp.vaaclipse.api.VaadinExecutorService#invokeLaterAlways(java.lang.Runnable) |
| */ |
| public synchronized void invokeLaterAlways(Runnable runnable) { |
| this.runnables2.add(runnable); |
| } |
| |
| /* (non-Javadoc) |
| * @see org.eclipse.osbp.vaaclipse.api.VaadinExecutorService#removeAlwaysRunnable(java.lang.Runnable) |
| */ |
| @Override |
| public synchronized void removeAlwaysRunnable(Runnable runnable) { |
| this.runnables2.remove(runnable); |
| } |
| |
| /* (non-Javadoc) |
| * @see org.eclipse.osbp.vaaclipse.api.VaadinExecutorService#removeAllAlwaysRunnables() |
| */ |
| @Override |
| public void removeAllAlwaysRunnables() { |
| runnables2.clear(); |
| } |
| |
| /* (non-Javadoc) |
| * @see org.eclipse.osbp.vaaclipse.api.VaadinExecutorService#removeAllInvokeLater() |
| */ |
| @Override |
| public void removeAllInvokeLater() { |
| this.runnables.clear(); |
| this.keys.clear(); |
| this.runnable2Key.clear(); |
| } |
| |
| /* (non-Javadoc) |
| * @see org.eclipse.osbp.vaaclipse.api.VaadinExecutorService#dispose() |
| */ |
| @Override |
| public void dispose() { |
| removeAllAlwaysRunnables(); |
| removeAllInvokeLater(); |
| |
| runnables = null; |
| runnables2 = null; |
| keys = null; |
| runnable2Key = null; |
| } |
| } |