<commit_before>/*
 * (C) Copyright 2010 Nuxeo SA (http://nuxeo.com/) and contributors.
 *
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the GNU Lesser General Public License
 * (LGPL) version 2.1 which accompanies this distribution, and is available at
 * http://www.gnu.org/licenses/lgpl.html
 *
 * This library 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
 * Lesser General Public License for more details.
 *
 * Contributors:
 *     <NAME> (a.k.a. sunix) <<EMAIL>>
 */
package org.nuxeo.ecm.platform.queue.core;

import java.net.URI;
import java.net.URISyntaxException;
import java.util.Date;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.nuxeo.ecm.core.api.ClientException;
import org.nuxeo.ecm.core.management.statuses.AdministrativeStatus;
import org.nuxeo.ecm.platform.lock.api.AlreadyLockedException;
import org.nuxeo.ecm.platform.lock.api.LockCoordinator;
import org.nuxeo.ecm.platform.lock.api.NoSuchLockException;
import org.nuxeo.ecm.platform.lock.api.NotOwnerException;
import org.nuxeo.ecm.platform.queue.api.QueueContent;
import org.nuxeo.ecm.platform.queue.api.QueueException;
import org.nuxeo.ecm.platform.queue.api.QueueExecutor;
import org.nuxeo.ecm.platform.queue.api.QueueFactory;
import org.nuxeo.ecm.platform.queue.api.QueueHandler;
import org.nuxeo.ecm.platform.queue.api.QueueNotFoundException;
import org.nuxeo.ecm.platform.queue.api.QueuePersister;
import org.nuxeo.runtime.api.Framework;

/**
 * @author <NAME> (a.k.a. sunix) <<EMAIL>>
 *
 */
public class QueueHandlerImpl implements QueueHandler {

    public static final Log log = LogFactory.getLog(QueueHandlerImpl.class);

    public void handleEndOfProcessing(QueueContent content) {

        try {
            QueueFactory factory = Framework.getLocalService(QueueFactory.class);
            QueuePersister persister = factory.getPersister(content);
            persister.forgetContent(content);
        } catch (QueueException e) {
            log.error("Couldn't remove the Queue item", e);
        }
    }

    public void handleNewContent(QueueContent content) {
        throw new UnsupportedOperationException("Not yet implemented");
    }

    protected boolean isServerActive() throws ClientException {
        AdministrativeStatus administrativeStatus = Framework.getLocalService(AdministrativeStatus.class);
        return administrativeStatus.isActive();
    }

    public void handleNewContentIfUnknown(QueueContent content)
            throws QueueException {

        try {
            if (!isServerActive()) {
                return;
            }
        } catch (ClientException e2) {
            throw new Error(e2);
        }
        URI resource;
        try {
            resource = content.getResourceURI();
        } catch (URISyntaxException e1) {
            throw new Error("Could not get the server status",e1);
        }

        QueueFactory factory = Framework.getLocalService(QueueFactory.class);
        LockCoordinator coordinator = Framework.getLocalService(LockCoordinator.class);

        QueuePersister persister = factory.getPersister(content);

        try {
            coordinator.lock(content.getOwner(), content.getResourceURI(),
                    content.getComments(), content.getDelay());
        } catch (AlreadyLockedException e) {
            log.debug("Already locked resource " + content.getDestination()
                    + ":" + content.getName(), e);
            return;
        } catch (Throwable e) {
            throw new QueueException("Couldn't lock the resource", e, content);
        }

        try {
            if (persister.hasContent(content)) {
                return;
            }
            persister.saveContent(content);
        } finally {
            try {
                coordinator.unlock(content.getOwner(), resource);
            } catch (NoSuchLockException e) {
                log.warn("Resource is unexpectedly not locked", e);
                return;
            } catch (NotOwnerException e) {
                log.warn("Resource is unexpectedly locked by another user", e);
                return;
            } catch (InterruptedException e) {
                throw new Error(
                        "Unexpected error while trying to unlock the resource",
                        e);

            }
        }

        QueueExecutor executor = factory.getExecutor(content);
        persister.setExecuteTime(content, new Date());
        executor.execute(content, this);
    }

    public void handleRetry(QueueContent content) throws QueueNotFoundException {
        QueueFactory factory = Framework.getLocalService(QueueFactory.class);
        QueueExecutor executor = factory.getExecutor(content);
        QueuePersister persister = factory.getPersister(content);
        persister.setExecuteTime(content, new Date());
        executor.execute(content, this);
    }
}<commit_msg>NXP-5567 replaced interrupted exceptions by errors when no timing out requested (fixing compilation in client modules)<commit_after>/*
 * (C) Copyright 2010 Nuxeo SA (http://nuxeo.com/) and contributors.
 *
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the GNU Lesser General Public License
 * (LGPL) version 2.1 which accompanies this distribution, and is available at
 * http://www.gnu.org/licenses/lgpl.html
 *
 * This library 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
 * Lesser General Public License for more details.
 *
 * Contributors:
 *     <NAME> (a.k.a. sunix) <<EMAIL>>
 */
package org.nuxeo.ecm.platform.queue.core;

import java.net.URI;
import java.net.URISyntaxException;
import java.util.Date;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.nuxeo.ecm.core.api.ClientException;
import org.nuxeo.ecm.core.management.statuses.AdministrativeStatus;
import org.nuxeo.ecm.platform.lock.api.AlreadyLockedException;
import org.nuxeo.ecm.platform.lock.api.LockCoordinator;
import org.nuxeo.ecm.platform.lock.api.NoSuchLockException;
import org.nuxeo.ecm.platform.lock.api.NotOwnerException;
import org.nuxeo.ecm.platform.queue.api.QueueContent;
import org.nuxeo.ecm.platform.queue.api.QueueException;
import org.nuxeo.ecm.platform.queue.api.QueueExecutor;
import org.nuxeo.ecm.platform.queue.api.QueueFactory;
import org.nuxeo.ecm.platform.queue.api.QueueHandler;
import org.nuxeo.ecm.platform.queue.api.QueueNotFoundException;
import org.nuxeo.ecm.platform.queue.api.QueuePersister;
import org.nuxeo.runtime.api.Framework;

/**
 * @author <NAME> (a.k.a. sunix) <<EMAIL>>
 *
 */
public class QueueHandlerImpl implements QueueHandler {

    public static final Log log = LogFactory.getLog(QueueHandlerImpl.class);

    public void handleEndOfProcessing(QueueContent content) {

        try {
            QueueFactory factory = Framework.getLocalService(QueueFactory.class);
            QueuePersister persister = factory.getPersister(content);
            persister.forgetContent(content);
        } catch (QueueException e) {
            log.error("Couldn't remove the Queue item", e);
        }
    }

    public void handleNewContent(QueueContent content) {
        throw new UnsupportedOperationException("Not yet implemented");
    }

    protected boolean isServerActive() throws ClientException {
        AdministrativeStatus administrativeStatus = Framework.getLocalService(AdministrativeStatus.class);
        return administrativeStatus.isActive();
    }

    public void handleNewContentIfUnknown(QueueContent content)
            throws QueueException {

        try {
            if (!isServerActive()) {
                return;
            }
        } catch (ClientException e2) {
            throw new Error(e2);
        }
        URI resource;
        try {
            resource = content.getResourceURI();
        } catch (URISyntaxException e1) {
            throw new Error("Could not get the server status",e1);
        }

        QueueFactory factory = Framework.getLocalService(QueueFactory.class);
        LockCoordinator coordinator = Framework.getLocalService(LockCoordinator.class);

        QueuePersister persister = factory.getPersister(content);

        try {
            coordinator.lock(content.getOwner(), content.getResourceURI(),
                    content.getComments(), content.getDelay());
        } catch (AlreadyLockedException e) {
            log.debug("Already locked resource " + content.getDestination()
                    + ":" + content.getName(), e);
            return;
        } catch (Throwable e) {
            throw new QueueException("Couldn't lock the resource", e, content);
        }

        try {
            if (persister.hasContent(content)) {
                return;
            }
            persister.saveContent(content);
        } finally {
            try {
                coordinator.unlock(content.getOwner(), resource);
            } catch (NoSuchLockException e) {
                log.warn("Resource is unexpectedly not locked", e);
                return;
            } catch (NotOwnerException e) {
                log.warn("Resource is unexpectedly locked by another user", e);
                return;
            }
        }

        QueueExecutor executor = factory.getExecutor(content);
        persister.setExecuteTime(content, new Date());
        executor.execute(content, this);
    }

    public void handleRetry(QueueContent content) throws QueueNotFoundException {
        QueueFactory factory = Framework.getLocalService(QueueFactory.class);
        QueueExecutor executor = factory.getExecutor(content);
        QueuePersister persister = factory.getPersister(content);
        persister.setExecuteTime(content, new Date());
        executor.execute(content, this);
    }
}
{'fraction_non_alphanumeric': 0.09502664298401421, 'fraction_numerical': 0.002368265245707519, 'mean_word_length': 2.7746741154562384, 'pattern_counts': {'":': 2, '<': 15, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5331510', 'n_tokens_mistral': 2793, 'n_tokens_neox': 2654, 'n_words': 648}