<commit_before>/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you 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.apache.felix.dm.samples.annotation;

import java.io.PrintStream;
import java.util.Map;
import java.util.concurrent.CopyOnWriteArrayList;

import org.apache.felix.dm.annotation.api.Service;
import org.apache.felix.dm.annotation.api.ServiceDependency;
import org.apache.felix.shell.Command;
import org.osgi.service.log.LogService;

/**
 * Felix "spellcheck" Shell Command, used to check correct word spelling.
 */
@Service
public class SpellChecker implements Command
{
    /**
     * We'll use the OSGi log service for logging. If no log service is available, then we'll use a NullObject.
     */
    @ServiceDependency(required = false)
    private LogService m_log;

    /**
     * We'll store all Dictionaries is a CopyOnWrite list, in order to avoid method synchronization.
     */
    private CopyOnWriteArrayList<DictionaryService> m_dictionaries = new CopyOnWriteArrayList<DictionaryService>();

    /**
     * Inject a dictionary into this service.
     * @param serviceProperties the dictionary OSGi service properties
     * @param dictionary the new dictionary
     */
    @ServiceDependency(removed = "removeDictionary")
    protected void addDictionary(Map<String, String> serviceProperties, DictionaryService dictionary)
    {
        m_log.log(LogService.LOG_INFO, "added dictionary: " + dictionary + " (language="
            + serviceProperties.get("language") + ")");
        m_dictionaries.add(dictionary);
    }

    /**
     * Remove a dictionary from our service.
     * @param dictionary
     */
    protected void removeDictionary(DictionaryService dictionary)
    {
        m_log.log(LogService.LOG_INFO, "added dictionary: " + dictionary);
        m_dictionaries.remove(dictionary);
    }

    // --- Felix Shell Command interface ---

    public String getName()
    {
        return "spellcheck";
    }

    public String getUsage()
    {
        return "spellcheck word";
    }

    public String getShortDescription()
    {
        return "Spell checker application using DependencyManager annotations";
    }

    public void execute(String commandLine, PrintStream out, PrintStream err)
    {
        String[] tokens = commandLine.split(" ");
        if (tokens == null || tokens.length < 2)
        {
            err.println("Invalid parameters: " + commandLine + ". Usage: " + getUsage());
            return;
        }
        String word = tokens[1];
        for (DictionaryService dictionary : m_dictionaries)
        {
            if (dictionary.checkWord(tokens[1]))
            {
                out.println("word " + word + " is correct");
                return;
            }
        }<commit_msg>don't log in addDictionary callback, because the logger may not be injected yet<commit_after>/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you 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.apache.felix.dm.samples.annotation;

import java.io.PrintStream;
import java.util.concurrent.CopyOnWriteArrayList;

import org.apache.felix.dm.annotation.api.Service;
import org.apache.felix.dm.annotation.api.ServiceDependency;
import org.apache.felix.shell.Command;
import org.osgi.service.log.LogService;

/**
 * Felix "spellcheck" Shell Command, used to check correct word spelling.
 */
@Service
public class SpellChecker implements Command
{
    /**
     * We'll use the OSGi log service for logging. If no log service is available, then we'll use a NullObject.
     */
    @ServiceDependency(required = false)
    private LogService m_log;

    /**
     * We'll store all Dictionaries is a CopyOnWrite list, in order to avoid method synchronization.
     */
    private CopyOnWriteArrayList<DictionaryService> m_dictionaries = new CopyOnWriteArrayList<DictionaryService>();

    /**
     * Inject a dictionary into this service.
     * @param serviceProperties the dictionary OSGi service properties
     * @param dictionary the new dictionary
     */
    @ServiceDependency(removed = "removeDictionary")
    protected void addDictionary(DictionaryService dictionary)
    {
        m_dictionaries.add(dictionary);
    }

    /**
     * Remove a dictionary from our service.
     * @param dictionary
     */
    protected void removeDictionary(DictionaryService dictionary)
    {
        m_dictionaries.remove(dictionary);
    }

    // --- Felix Shell Command interface ---

    public String getName()
    {
        return "spellcheck";
    }

    public String getUsage()
    {
        return "spellcheck word";
    }

    public String getShortDescription()
    {
        return "Spell checker application using DependencyManager annotations";
    }

    public void execute(String commandLine, PrintStream out, PrintStream err)
    {
        String[] tokens = commandLine.split(" ");
        if (tokens == null || tokens.length < 2)
        {
            err.println("Invalid parameters: " + commandLine + ". Usage: " + getUsage());
            return;
        }
        String word = tokens[1];

        m_log.log(LogService.LOG_DEBUG, "Checking spelling of word \"" + word
            + "\" using the following dictionaries: " + m_dictionaries);

        for (DictionaryService dictionary : m_dictionaries)
        {
            if (dictionary.checkWord(tokens[1]))
            {
                out.println("word " + word + " is correct");
                return;
            }
        }
{'fraction_non_alphanumeric': 0.08858553586988092, 'fraction_numerical': 0.0020331106593087424, 'mean_word_length': 2.8843767625493513, 'pattern_counts': {'":': 0, '<': 10, '<?xml version=': 0, '>': 8, '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': '2898951', 'n_tokens_mistral': 1777, 'n_tokens_neox': 1729, 'n_words': 608}