001/*******************************************************************************
002 * (C) Copyright 2015 Nuxeo SA (http://nuxeo.com/) and contributors.
003 * 
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the GNU Lesser General Public License
006 * (LGPL) version 2.1 which accompanies this distribution, and is available at
007 * http://www.gnu.org/licenses/lgpl-2.1.html
008 * 
009 * This library is distributed in the hope that it will be useful,
010 * but WITHOUT ANY WARRANTY; without even the implied warranty of
011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012 * Lesser General Public License for more details.
013 ******************************************************************************/
014package org.nuxeo.ecm.core.test;
015
016import java.util.concurrent.BlockingQueue;
017
018import org.apache.commons.lang3.SerializationException;
019import org.apache.commons.lang3.SerializationUtils;
020import org.nuxeo.ecm.core.api.NuxeoException;
021import org.nuxeo.ecm.core.work.MemoryBlockingQueue;
022import org.nuxeo.ecm.core.work.MemoryWorkQueuing;
023import org.nuxeo.ecm.core.work.WorkHolder;
024import org.nuxeo.ecm.core.work.WorkManagerImpl;
025import org.nuxeo.ecm.core.work.WorkQueueDescriptorRegistry;
026import org.nuxeo.ecm.core.work.api.Work;
027import org.nuxeo.ecm.core.work.api.WorkQueueDescriptor;
028
029public class TestWorkQueuing extends MemoryWorkQueuing {
030
031    public TestWorkQueuing(WorkManagerImpl mgr, WorkQueueDescriptorRegistry workQueueDescriptors) {
032        super(mgr, workQueueDescriptors);
033    }
034
035    @Override
036    protected BlockingQueue<Runnable> newBlockingQueue(WorkQueueDescriptor workQueueDescriptor) {
037        return new MemoryBlockingQueue(this, workQueueDescriptor.getCapacity()) {
038            @Override
039            public void putElement(Runnable r) throws InterruptedException {
040                super.putElement(clone(r));
041            }
042
043            Runnable clone(Runnable r) {
044                Work original = WorkHolder.getWork(r);
045                try {
046                    return new WorkHolder(SerializationUtils.clone(original));
047                } catch (SerializationException cause) {
048                    throw new NuxeoException("Cannot serialize work of type " + original.getClass().getName());
049                }
050            }
051        };
052    }
053}