<commit_before>/*
 * Copyright 2007 Wyona
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.wyona.org/licenses/APACHE-LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */

package org.wyona.yanel.impl.resources.usecase.thread;

import java.io.Serializable;

import javax.servlet.http.HttpSession;

import org.wyona.yarep.core.Node;
import org.wyona.yarep.core.Repository;

import org.apache.log4j.Logger;

/**
 * A thread which can be executed by the ThreadUsecaseResource.
 */
public abstract class UsecaseThread extends Thread implements Serializable {

    private static Logger log = Logger.getLogger(UsecaseThread.class);

    protected String threadID;
    protected int progress = 0;
    protected boolean cancelled = false;
    protected boolean done = false;
    protected StringBuffer eventLog;
 
    /**
     * @param threadID Thread ID
     */
    public UsecaseThread(String threadID) {
        this.threadID = threadID;
        this.eventLog = new StringBuffer();
    }

    /**
     * Attaches this thread ID to the HTTP session of the 'user'.
     * @param session HTTP session of the 'user'
     * @throws IllegalStateException if a thread with the same key already exists in this session.
     */
    public void attachThreadToSession(HttpSession session) throws IllegalStateException {
        String attrName = getThreadKey(this.threadID);
        if (session.getAttribute(attrName) != null) {
            String errorMsg = "Thread with id '" + threadID + "' exists already within 'user' session!";
            log.error(errorMsg);
            throw new IllegalStateException(errorMsg);
        }
        session.setAttribute(attrName, this);
    }

    /**
     * Attach this thread ID to a repository.
     * @param repository Repository
     * @param directoryPath Collection path which contains the thread key
     * @throws IllegalStateException if a thread with the same key already exists in this repository.
     */
    public void attachThreadIDToRepository(org.wyona.yarep.core.Repository repository, String directoryPath) throws IllegalStateException, org.wyona.yarep.core.RepositoryException {
        // Also see http://grizzly.java.net/
        log.debug("Java thread ID: " + getId());
        String threadKey = getThreadKey(this.threadID);
        String threadKeyPath = directoryPath + "/" + threadKey;
        if (repository.existsNode(threadKeyPath)) {
            String errorMsg = "Thread with id '" + threadID + "' exists already within repository: " + threadKeyPath;
            log.error(errorMsg);
            throw new IllegalStateException(errorMsg);
        }
        org.wyona.yarep.core.Node node = org.wyona.yarep.util.YarepUtil.addNodes(repository, threadKeyPath, org.wyona.yarep.core.NodeType.RESOURCE);
        try {
            org.apache.commons.io.IOUtils.copy(new java.io.StringBufferInputStream("" + getId()), node.getOutputStream());
        } catch(java.io.IOException e) {
            log.error(e, e);
            throw new org.wyona.yarep.core.RepositoryException(e.getMessage());
        }
    }
    
    /**
     * Creates the key for this thread which is used to store and identify the thread in the session.
     * @param threadID
     * @return
     */
    protected static String getThreadKey(String threadID) {
        return "yanel.thread." + threadID;
    }
    
    /**
     * Gets the thread with the given id from the session.
     * @param session
     * @param threadID<commit_msg>javadoc fixed<commit_after>/*
 * Copyright 2007 Wyona
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.wyona.org/licenses/APACHE-LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */

package org.wyona.yanel.impl.resources.usecase.thread;

import java.io.Serializable;

import javax.servlet.http.HttpSession;

import org.wyona.yarep.core.Node;
import org.wyona.yarep.core.Repository;

import org.apache.log4j.Logger;

/**
 * A thread which can be executed by the ThreadUsecaseResource.
 */
public abstract class UsecaseThread extends Thread implements Serializable {

    private static Logger log = Logger.getLogger(UsecaseThread.class);

    protected String threadID;
    protected int progress = 0;
    protected boolean cancelled = false;
    protected boolean done = false;
    protected StringBuffer eventLog;
 
    /**
     * @param threadID Thread ID
     */
    public UsecaseThread(String threadID) {
        this.threadID = threadID;
        this.eventLog = new StringBuffer();
    }

    /**
     * Attaches this thread ID to the HTTP session of the 'user'.
     * @param session HTTP session of the 'user'
     * @throws IllegalStateException if a thread with the same key already exists in this session.
     */
    public void attachThreadToSession(HttpSession session) throws IllegalStateException {
        String attrName = getThreadKey(this.threadID);
        if (session.getAttribute(attrName) != null) {
            String errorMsg = "Thread with id '" + threadID + "' exists already within 'user' session!";
            log.error(errorMsg);
            throw new IllegalStateException(errorMsg);
        }
        session.setAttribute(attrName, this);
    }

    /**
     * Attach this thread ID to a repository.
     * @param repository Repository
     * @param directoryPath Collection path which contains the thread key
     * @throws IllegalStateException if a thread with the same key already exists in this repository.
     */
    public void attachThreadIDToRepository(org.wyona.yarep.core.Repository repository, String directoryPath) throws IllegalStateException, org.wyona.yarep.core.RepositoryException {
        // Also see http://grizzly.java.net/
        log.debug("Java thread ID: " + getId());
        String threadKey = getThreadKey(this.threadID);
        String threadKeyPath = directoryPath + "/" + threadKey;
        if (repository.existsNode(threadKeyPath)) {
            String errorMsg = "Thread with id '" + threadID + "' exists already within repository: " + threadKeyPath;
            log.error(errorMsg);
            throw new IllegalStateException(errorMsg);
        }
        org.wyona.yarep.core.Node node = org.wyona.yarep.util.YarepUtil.addNodes(repository, threadKeyPath, org.wyona.yarep.core.NodeType.RESOURCE);
        try {
            org.apache.commons.io.IOUtils.copy(new java.io.StringBufferInputStream("" + getId()), node.getOutputStream());
        } catch(java.io.IOException e) {
            log.error(e, e);
            throw new org.wyona.yarep.core.RepositoryException(e.getMessage());
        }
    }
    
    /**
     * Creates the key for this thread which is used to store and identify the thread in the session.
     * @param threadID
     */
    protected static String getThreadKey(String threadID) {
        return "yanel.thread." + threadID;
    }
    
    /**
     * Gets the thread with the given id from the session.
     * @param session
     * @param threadID
{'fraction_non_alphanumeric': 0.08977811978966269, 'fraction_numerical': 0.0025650891368475055, 'mean_word_length': 2.8970514742628684, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '5220294', 'n_tokens_mistral': 2101, 'n_tokens_neox': 2103, 'n_words': 679}