001/*
002 * (C) Copyright 2015 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package org.nuxeo.ecm.core.test;
017
018import java.util.concurrent.BlockingQueue;
019
020import org.apache.commons.lang3.SerializationException;
021import org.apache.commons.lang3.SerializationUtils;
022import org.nuxeo.ecm.core.api.NuxeoException;
023import org.nuxeo.ecm.core.work.MemoryBlockingQueue;
024import org.nuxeo.ecm.core.work.MemoryWorkQueuing;
025import org.nuxeo.ecm.core.work.WorkHolder;
026import org.nuxeo.ecm.core.work.WorkManagerImpl;
027import org.nuxeo.ecm.core.work.WorkQueueDescriptorRegistry;
028import org.nuxeo.ecm.core.work.api.Work;
029import org.nuxeo.ecm.core.work.api.WorkQueueDescriptor;
030
031public class TestWorkQueuing extends MemoryWorkQueuing {
032
033    public TestWorkQueuing(WorkManagerImpl mgr, WorkQueueDescriptorRegistry workQueueDescriptors) {
034        super(mgr, workQueueDescriptors);
035    }
036
037    @Override
038    protected BlockingQueue<Runnable> newBlockingQueue(WorkQueueDescriptor workQueueDescriptor) {
039        return new MemoryBlockingQueue(workQueueDescriptor.getCapacity()) {
040            @Override
041            public void putElement(Runnable r) throws InterruptedException {
042                super.putElement(clone(r));
043            }
044
045            Runnable clone(Runnable r) {
046                Work original = WorkHolder.getWork(r);
047                try {
048                    return new WorkHolder(SerializationUtils.clone(original));
049                } catch (SerializationException cause) {
050                    throw new NuxeoException("Cannot serialize work of type " + original.getClass().getName());
051                }
052            }
053        };
054    }
055}