<commit_before>/*
 * Copyright 2009-2012 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.codehaus.griffon.runtime.core;

import griffon.core.*;
import griffon.util.ApplicationHolder;
import groovy.lang.MetaClass;
import groovy.lang.MetaProperty;
import org.codehaus.groovy.runtime.InvokerHelper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Collections;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import static griffon.util.ConfigUtils.getConfigValueAsBoolean;

/**
 * Handler for 'Service' artifacts.
 *
 * @author <NAME>
 * @since 0.9.1
 */
public class ServiceArtifactHandler extends ArtifactHandlerAdapter {
    private static final Logger LOG = LoggerFactory.getLogger(ServiceArtifactHandler.class);
    private final ServiceManager serviceManager;

    private class DefaultServiceManager extends AbstractServiceManager {
        private final Map<String, GriffonService> serviceInstances = new ConcurrentHashMap<String, GriffonService>();

        public DefaultServiceManager(GriffonApplication app) {
            super(app);
        }

        public Map<String, GriffonService> getServices() {
            return Collections.unmodifiableMap(serviceInstances);
        }

        protected GriffonService doFindService(String name) {
            return serviceInstances.get(name);
        }

        protected GriffonService doInstantiateService(String name) {
            GriffonService serviceInstance = null;
            GriffonClass griffonClass = findClassFor(name);
            if (griffonClass != null) {
                serviceInstance = instantiateServiceInternal(griffonClass);
                serviceInstances.put(name, serviceInstance);
            }
            return serviceInstance;
        }

        private GriffonService instantiateServiceInternal(GriffonClass griffonClass) {
            GriffonService serviceInstance = (GriffonService) griffonClass.newInstance();
            getApp().addApplicationEventListener(serviceInstance);
            return serviceInstance;
        }
    }

    public ServiceArtifactHandler(GriffonApplication app) {
        super(app, GriffonServiceClass.TYPE, GriffonServiceClass.TRAILING);
        serviceManager = new DefaultServiceManager(app);
        if (LOG.isDebugEnabled()) {
            LOG.debug("Registering " + serviceManager + " as ServiceManager.");
        }
        InvokerHelper.setProperty(app, "serviceManager", serviceManager);
    }

    protected GriffonClass newGriffonClassInstance(Class clazz) {
        return new DefaultGriffonServiceClass(getApp(), clazz);
    }

    public void initialize(ArtifactInfo[] artifacts) {
        super.initialize(artifacts);
        if (isBasicInjectionDisabled()) return;
        if (isEagerInstantiationEnabled()) {
            if (LOG.isDebugEnabled()) {
                LOG.debug("Instantiating service instances eagerly");
            }
            for (ArtifactInfo artifactInfo : artifacts) {
                GriffonClass griffonClass = getClassFor(artifactInfo.getClazz());
                serviceManager.findService(griffonClass.getPropertyName());
            }
        }
        getApp().addApplicationEventListener(this);<commit_msg>GRIFFON-592 resolve SOE when circular dependencies are defined on Service classes<commit_after>/*
 * Copyright 2009-2012 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.codehaus.griffon.runtime.core;

import griffon.core.*;
import griffon.exceptions.BeanInstantiationException;
import griffon.util.ApplicationHolder;
import groovy.lang.MetaClass;
import groovy.lang.MetaProperty;
import org.codehaus.groovy.runtime.InvokerHelper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Collections;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import static griffon.util.ConfigUtils.getConfigValueAsBoolean;
import static java.util.Arrays.asList;

/**
 * Handler for 'Service' artifacts.
 *
 * @author <NAME>
 * @since 0.9.1
 */
public class ServiceArtifactHandler extends ArtifactHandlerAdapter {
    private static final Logger LOG = LoggerFactory.getLogger(ServiceArtifactHandler.class);
    private final DefaultServiceManager serviceManager;

    private class DefaultServiceManager extends AbstractServiceManager {
        private final Map<String, GriffonService> serviceInstances = new ConcurrentHashMap<String, GriffonService>();

        public DefaultServiceManager(GriffonApplication app) {
            super(app);
        }

        public Map<String, GriffonService> getServices() {
            return Collections.unmodifiableMap(serviceInstances);
        }

        protected GriffonService doFindService(String name) {
            return serviceInstances.get(name);
        }

        protected GriffonService doInstantiateService(String name) {
            return doInstantiateService0(name, true);
        }

        private GriffonService doInstantiateService0(String name, boolean triggerEvent) {
            GriffonService serviceInstance = null;
            GriffonClass griffonClass = findClassFor(name);
            if (griffonClass != null) {
                serviceInstance = instantiateService(griffonClass);
                serviceInstances.put(name, serviceInstance);
                getApp().addApplicationEventListener(serviceInstance);
                if (triggerEvent) {
                    getApp().event(GriffonApplication.Event.NEW_INSTANCE.getName(),
                        asList(griffonClass.getClazz(), GriffonServiceClass.TYPE, serviceInstance));
                }
            }
            return serviceInstance;
        }

        private GriffonService instantiateService(GriffonClass griffonClass) {
            try {
                return (GriffonService) griffonClass.getClazz().newInstance();
            } catch (InstantiationException e) {
                throw new BeanInstantiationException(e);
            } catch (IllegalAccessException e) {
                throw new BeanInstantiationException(e);
            }
        }
    }

    public ServiceArtifactHandler(GriffonApplication app) {
        super(app, GriffonServiceClass.TYPE, GriffonServiceClass.TRAILING);
        serviceManager = new DefaultServiceManager(app);
        if (LOG.isDebugEnabled()) {
            LOG.debug("Registering " + serviceManager + " as ServiceManager.");
        }
        InvokerHelper.setProperty(app, "serviceManager", serviceManager);
    }

    protected GriffonClass newGriffonClassInstance(Class clazz) {
        return new DefaultGriffonServiceClass(getApp(), clazz);
    }

    public void initialize(ArtifactInfo[] artifacts) {
        super.initialize(artifacts);
        if (isBasicInjectionDisabled()) return;
        getApp().addApplicationEventListener(this);
        if (isEagerInstantiationEnabled()) {
            if (LOG.isDebugEnabled()) {
                LOG.debug("Instantiating service instances eagerly");
            }
            for (ArtifactInfo artifactInfo : artifacts) {
                GriffonClass griffonClass = getClassFor(artifactInfo.getClazz());
                serviceManager.doInstantiateService0(griffonClass.getPropertyName(), false);
            }
            for (ArtifactInfo artifactInfo : artifacts) {
                GriffonClass griffonClass = getClassFor(artifactInfo.getClazz());
                GriffonService serviceInstance = serviceManager.findService(griffonClass.getPropertyName());
                getApp().event(GriffonApplication.Event.NEW_INSTANCE.getName(),
                    asList(griffonClass.getClazz(), GriffonServiceClass.TYPE, serviceInstance));
            }
        }
{'fraction_non_alphanumeric': 0.08109032846715329, 'fraction_numerical': 0.004562043795620438, 'mean_word_length': 3.057843590930125, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, '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': '1700345', 'n_tokens_mistral': 2257, 'n_tokens_neox': 2183, 'n_words': 542}