<commit_before>/*
 * Copyright 2004-2009 the original author or authors.
 * 
 * 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.apache.org/licenses/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.compass.core.impl;

import java.lang.ref.WeakReference;
import javax.naming.NamingException;
import javax.naming.Reference;
import javax.naming.StringRefAddr;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.compass.core.Compass;
import org.compass.core.CompassException;
import org.compass.core.CompassIndexSession;
import org.compass.core.CompassQueryBuilder;
import org.compass.core.CompassQueryFilterBuilder;
import org.compass.core.CompassSearchSession;
import org.compass.core.CompassSession;
import org.compass.core.CompassTransaction;
import org.compass.core.ResourceFactory;
import org.compass.core.cache.first.FirstLevelCache;
import org.compass.core.cache.first.FirstLevelCacheFactory;
import org.compass.core.config.CompassConfiguration;
import org.compass.core.config.CompassEnvironment;
import org.compass.core.config.CompassSettings;
import org.compass.core.config.RuntimeCompassSettings;
import org.compass.core.converter.ConverterLookup;
import org.compass.core.engine.SearchEngineFactory;
import org.compass.core.engine.SearchEngineIndexManager;
import org.compass.core.engine.SearchEngineOptimizer;
import org.compass.core.engine.naming.PropertyNamingStrategy;
import org.compass.core.engine.spellcheck.SearchEngineSpellCheckManager;
import org.compass.core.engine.spi.InternalSearchEngineFactory;
import org.compass.core.events.CompassEventManager;
import org.compass.core.events.RebuildEventListener;
import org.compass.core.executor.ExecutorManager;
import org.compass.core.id.IdentifierGenerator;
import org.compass.core.id.UUIDGenerator;
import org.compass.core.jndi.CompassObjectFactory;
import org.compass.core.lucene.LuceneEnvironment;
import org.compass.core.lucene.engine.LuceneSearchEngineFactory;
import org.compass.core.mapping.CompassMapping;
import org.compass.core.metadata.CompassMetaData;
import org.compass.core.spi.InternalCompass;
import org.compass.core.spi.InternalCompassSession;
import org.compass.core.transaction.InternalCompassTransaction;
import org.compass.core.transaction.LocalTransactionFactory;
import org.compass.core.transaction.TransactionException;
import org.compass.core.transaction.TransactionFactory;
import org.compass.core.transaction.TransactionFactoryFactory;
import org.compass.core.transaction.context.TransactionContext;
import org.compass.core.transaction.context.TransactionContextCallback;
import org.compass.core.transaction.context.TransactionContextCallbackWithTr;

/**
 * @author kimchy
 */
public class DefaultCompass implements InternalCompass {

    private static final Log logger = LogFactory.getLog(DefaultCompass.class);

    private static final long serialVersionUID = 3256446884762891059L;

    private static final IdentifierGenerator UUID_GENERATOR = new UUIDGenerator();

    private final String name;

    private String uuid;

    private final CompassMapping mapping;

    private final InternalSearchEngineFactory searchEngineFactory;

    private final TransactionFactory transactionFactory;

    private final LocalTransactionFactory localTransactionFactory;

    private final ConverterLookup converterLookup;

    private final CompassMetaData compassMetaData;

    private final PropertyNamingStrategy propertyNamingStrategy;

    private final ExecutorManager executorManager;

    private final CompassEventManager eventManager;

    protected final CompassSettings settings;

    private final FirstLevelCacheFactory firstLevelCacheFactory;

    private volatile ShutdownThread shutdownThread;

    private final boolean duplicate;

    private volatile boolean closed = false;

    private final boolean debug;

    public DefaultCompass(CompassMapping mapping, ConverterLookup converterLookup, CompassMetaData compassMetaData,
                          PropertyNamingStrategy propertyNamingStrategy, ExecutorManager executorManager,
                          CompassSettings settings) throws CompassException {
        this(mapping, converterLookup, compassMetaData, propertyNamingStrategy, executorManager, settings, false);
    }

    public DefaultCompass(CompassMapping mapping, ConverterLookup converterLookup, CompassMetaData compassMetaData,
                          PropertyNamingStrategy propertyNamingStrategy, ExecutorManager executorManager,
                          CompassSettings settings, boolean duplicate) throws CompassException {

        this(mapping, converterLookup, compassMetaData, propertyNamingStrategy, executorManager, settings, duplicate,
                new LuceneSearchEngineFactory(propertyNamingStrategy, settings, mapping, executorManager));
    }

    public DefaultCompass(CompassMapping mapping, ConverterLookup converterLookup, CompassMetaData compassMetaData,
                          PropertyNamingStrategy propertyNamingStrategy, CompassSettings settings,
                          LuceneSearchEngineFactory searchEngineFactory) throws CompassException {
        this(mapping, converterLookup, compassMetaData, propertyNamingStrategy, searchEngineFactory.getExecutorManager(),
                settings, false, searchEngineFactory);
    }

    public DefaultCompass(CompassMapping mapping, ConverterLookup converterLookup, CompassMetaData compassMetaData,
                          PropertyNamingStrategy propertyNamingStrategy, ExecutorManager executorManager,
                          CompassSettings settings, boolean duplicate, LuceneSearchEngineFactory searchEngineFactory) throws CompassException {

        this.mapping = mapping;
        this.converterLookup = converterLookup;
        this.compassMetaData = compassMetaData;
        this.propertyNamingStrategy = propertyNamingStrategy;
        this.executorManager = executorManager;
        this.name = settings.getSetting(CompassEnvironment.NAME, "default");
        this.settings = settings;
        this.duplicate = duplicate;

        this.eventManager = new CompassEventManager(this, mapping);
        eventManager.configure(settings);

        if (!duplicate) {
            registerJndi();
        }

        searchEngineFactory.setTransactionContext(new CompassTransactionContext(this));
        this.searchEngineFactory = searchEngineFactory;

        // build the transaction factory
        transactionFactory = TransactionFactoryFactory.createTransactionFactory(this, settings);
        localTransactionFactory = TransactionFactoryFactory.createLocalTransactionFactory(this, settings);

        firstLevelCacheFactory = new FirstLevelCacheFactory();
        firstLevelCacheFactory.configure(settings);

        searchEngineFactory.getIndexManager().verifyIndex();

        if (!duplicate) {
            start();
        }

        if (settings.getSettingAsBoolean(CompassEnvironment.REGISTER_SHUTDOWN_HOOK, true)) {
            shutdownThread = new ShutdownThread(this);
            if (logger.isDebugEnabled()) {
                logger.debug("Registering shutdown hook [" + System.identityHashCode(shutdownThread) + "]");
            }
            Runtime.getRuntime().addShutdownHook(shutdownThread);
        }

        this.debug = settings.getSettingAsBoolean(CompassEnvironment.DEBUG, false);
    }

    public CompassConfiguration getConfig() {
        throw new UnsupportedOperationException();
    }

    public void rebuild() {
        throw new UnsupportedOperationException();
    }

    public void addRebuildEventListener(RebuildEventListener eventListener) {
        throw new UnsupportedOperationException();
    }

    public void removeRebuildEventListener(RebuildEventListener eventListener) {
        throw new UnsupportedOperationException();
    }
<commit_msg>don't register a shutdown hook if there are no permissions to do so<commit_after>/*
 * Copyright 2004-2009 the original author or authors.
 * 
 * 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.apache.org/licenses/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.compass.core.impl;

import java.lang.ref.WeakReference;
import java.security.AccessControlException;
import javax.naming.NamingException;
import javax.naming.Reference;
import javax.naming.StringRefAddr;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.compass.core.Compass;
import org.compass.core.CompassException;
import org.compass.core.CompassIndexSession;
import org.compass.core.CompassQueryBuilder;
import org.compass.core.CompassQueryFilterBuilder;
import org.compass.core.CompassSearchSession;
import org.compass.core.CompassSession;
import org.compass.core.CompassTransaction;
import org.compass.core.ResourceFactory;
import org.compass.core.cache.first.FirstLevelCache;
import org.compass.core.cache.first.FirstLevelCacheFactory;
import org.compass.core.config.CompassConfiguration;
import org.compass.core.config.CompassEnvironment;
import org.compass.core.config.CompassSettings;
import org.compass.core.config.RuntimeCompassSettings;
import org.compass.core.converter.ConverterLookup;
import org.compass.core.engine.SearchEngineFactory;
import org.compass.core.engine.SearchEngineIndexManager;
import org.compass.core.engine.SearchEngineOptimizer;
import org.compass.core.engine.naming.PropertyNamingStrategy;
import org.compass.core.engine.spellcheck.SearchEngineSpellCheckManager;
import org.compass.core.engine.spi.InternalSearchEngineFactory;
import org.compass.core.events.CompassEventManager;
import org.compass.core.events.RebuildEventListener;
import org.compass.core.executor.ExecutorManager;
import org.compass.core.id.IdentifierGenerator;
import org.compass.core.id.UUIDGenerator;
import org.compass.core.jndi.CompassObjectFactory;
import org.compass.core.lucene.LuceneEnvironment;
import org.compass.core.lucene.engine.LuceneSearchEngineFactory;
import org.compass.core.mapping.CompassMapping;
import org.compass.core.metadata.CompassMetaData;
import org.compass.core.spi.InternalCompass;
import org.compass.core.spi.InternalCompassSession;
import org.compass.core.transaction.InternalCompassTransaction;
import org.compass.core.transaction.LocalTransactionFactory;
import org.compass.core.transaction.TransactionException;
import org.compass.core.transaction.TransactionFactory;
import org.compass.core.transaction.TransactionFactoryFactory;
import org.compass.core.transaction.context.TransactionContext;
import org.compass.core.transaction.context.TransactionContextCallback;
import org.compass.core.transaction.context.TransactionContextCallbackWithTr;

/**
 * @author kimchy
 */
public class DefaultCompass implements InternalCompass {

    private static final Log logger = LogFactory.getLog(DefaultCompass.class);

    private static final long serialVersionUID = 3256446884762891059L;

    private static final IdentifierGenerator UUID_GENERATOR = new UUIDGenerator();

    private final String name;

    private String uuid;

    private final CompassMapping mapping;

    private final InternalSearchEngineFactory searchEngineFactory;

    private final TransactionFactory transactionFactory;

    private final LocalTransactionFactory localTransactionFactory;

    private final ConverterLookup converterLookup;

    private final CompassMetaData compassMetaData;

    private final PropertyNamingStrategy propertyNamingStrategy;

    private final ExecutorManager executorManager;

    private final CompassEventManager eventManager;

    protected final CompassSettings settings;

    private final FirstLevelCacheFactory firstLevelCacheFactory;

    private volatile ShutdownThread shutdownThread;

    private final boolean duplicate;

    private volatile boolean closed = false;

    private final boolean debug;

    public DefaultCompass(CompassMapping mapping, ConverterLookup converterLookup, CompassMetaData compassMetaData,
                          PropertyNamingStrategy propertyNamingStrategy, ExecutorManager executorManager,
                          CompassSettings settings) throws CompassException {
        this(mapping, converterLookup, compassMetaData, propertyNamingStrategy, executorManager, settings, false);
    }

    public DefaultCompass(CompassMapping mapping, ConverterLookup converterLookup, CompassMetaData compassMetaData,
                          PropertyNamingStrategy propertyNamingStrategy, ExecutorManager executorManager,
                          CompassSettings settings, boolean duplicate) throws CompassException {

        this(mapping, converterLookup, compassMetaData, propertyNamingStrategy, executorManager, settings, duplicate,
                new LuceneSearchEngineFactory(propertyNamingStrategy, settings, mapping, executorManager));
    }

    public DefaultCompass(CompassMapping mapping, ConverterLookup converterLookup, CompassMetaData compassMetaData,
                          PropertyNamingStrategy propertyNamingStrategy, CompassSettings settings,
                          LuceneSearchEngineFactory searchEngineFactory) throws CompassException {
        this(mapping, converterLookup, compassMetaData, propertyNamingStrategy, searchEngineFactory.getExecutorManager(),
                settings, false, searchEngineFactory);
    }

    public DefaultCompass(CompassMapping mapping, ConverterLookup converterLookup, CompassMetaData compassMetaData,
                          PropertyNamingStrategy propertyNamingStrategy, ExecutorManager executorManager,
                          CompassSettings settings, boolean duplicate, LuceneSearchEngineFactory searchEngineFactory) throws CompassException {

        this.mapping = mapping;
        this.converterLookup = converterLookup;
        this.compassMetaData = compassMetaData;
        this.propertyNamingStrategy = propertyNamingStrategy;
        this.executorManager = executorManager;
        this.name = settings.getSetting(CompassEnvironment.NAME, "default");
        this.settings = settings;
        this.duplicate = duplicate;

        this.eventManager = new CompassEventManager(this, mapping);
        eventManager.configure(settings);

        if (!duplicate) {
            registerJndi();
        }

        searchEngineFactory.setTransactionContext(new CompassTransactionContext(this));
        this.searchEngineFactory = searchEngineFactory;

        // build the transaction factory
        transactionFactory = TransactionFactoryFactory.createTransactionFactory(this, settings);
        localTransactionFactory = TransactionFactoryFactory.createLocalTransactionFactory(this, settings);

        firstLevelCacheFactory = new FirstLevelCacheFactory();
        firstLevelCacheFactory.configure(settings);

        searchEngineFactory.getIndexManager().verifyIndex();

        if (!duplicate) {
            start();
        }

        if (settings.getSettingAsBoolean(CompassEnvironment.REGISTER_SHUTDOWN_HOOK, true)) {
            try {
                shutdownThread = new ShutdownThread(this);

                if (logger.isDebugEnabled()) {
                    logger.debug("Registering shutdown hook [" + System.identityHashCode(shutdownThread) + "]");
                }
                Runtime.getRuntime().addShutdownHook(shutdownThread);
            } catch (AccessControlException e) {
                // we are not allowed to register a shutdown thread, ignore
            }
        }

        this.debug = settings.getSettingAsBoolean(CompassEnvironment.DEBUG, false);
    }

    public CompassConfiguration getConfig() {
        throw new UnsupportedOperationException();
    }

    public void rebuild() {
        throw new UnsupportedOperationException();
    }

    public void addRebuildEventListener(RebuildEventListener eventListener) {
        throw new UnsupportedOperationException();
    }

    public void removeRebuildEventListener(RebuildEventListener eventListener) {
        throw new UnsupportedOperationException();
    }

{'fraction_non_alphanumeric': 0.0683097339889804, 'fraction_numerical': 0.003673203388826352, 'mean_word_length': 4.2211568202907515, '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': '6341783', 'n_tokens_mistral': 4085, 'n_tokens_neox': 3953, 'n_words': 948}