<commit_before>/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. 
 */
package org.ocelotds.resolvers;

import org.ocelotds.spi.DataServiceException;
import org.ocelotds.spi.DataServiceResolver;
import org.ocelotds.spi.IDataServiceResolver;
import org.ocelotds.Constants;
import org.ocelotds.spi.Scope;
import java.lang.annotation.Annotation;
import java.util.Set;
import javax.enterprise.context.spi.CreationalContext;
import javax.enterprise.inject.spi.Bean;
import javax.enterprise.inject.spi.BeanManager;
import javax.inject.Inject;

/**
 * Resolver of CDI
 *
 * @author hhfrancois
 */
@DataServiceResolver(Constants.Resolver.CDI)
public class CdiResolver implements IDataServiceResolver {

	@Inject
	BeanManager beanManager;

	@Override
	public <T> T resolveDataService(Class<T> clazz) throws DataServiceException {
//		return CDI.current().select(clazz).get(); // equivalent, but no testable
		Set<Bean<?>> beans = beanManager.getBeans(clazz, new DataserviceLiteral());
		for (Bean<?> b : beans) {
			final CreationalContext context = beanManager.createCreationalContext(b);
			return clazz.cast(beanManager.getReference(b, b.getBeanClass(), context));
		}
		throw new DataServiceException(clazz.getName());
	}

	@Override
	public Scope getScope(Class clazz) {
		for (Annotation anno : clazz.getAnnotations()) {
			String annoName = anno.annotationType().getName();
			switch (annoName) {
				case "javax.enterprise.context.Dependent": 
					return Scope.SESSION;
				case "javax.ejb.Stateful":
					return Scope.SESSION;
				default:
			}
		}
		return Scope.MANAGED;
	}
}<commit_msg>comment<commit_after>/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. 
 */
package org.ocelotds.resolvers;

import org.ocelotds.spi.DataServiceException;
import org.ocelotds.spi.DataServiceResolver;
import org.ocelotds.spi.IDataServiceResolver;
import org.ocelotds.Constants;
import org.ocelotds.spi.Scope;
import java.lang.annotation.Annotation;
import java.util.Set;
import javax.enterprise.context.spi.CreationalContext;
import javax.enterprise.inject.spi.Bean;
import javax.enterprise.inject.spi.BeanManager;
import javax.inject.Inject;

/**
 * Resolver of CDI
 *
 * @author hhfrancois
 */
@DataServiceResolver(Constants.Resolver.CDI)
public class CdiResolver implements IDataServiceResolver {

	@Inject
	BeanManager beanManager;

	@Override
	public <T> T resolveDataService(Class<T> clazz) throws DataServiceException {
//		return CDI.current().select(clazz, new DataserviceLiteral()).get(); // equivalent, but no testable
		Set<Bean<?>> beans = beanManager.getBeans(clazz, new DataserviceLiteral());
		for (Bean<?> b : beans) {
			final CreationalContext context = beanManager.createCreationalContext(b);
			return clazz.cast(beanManager.getReference(b, b.getBeanClass(), context));
		}
		throw new DataServiceException(clazz.getName());
	}

	@Override
	public Scope getScope(Class clazz) {
		for (Annotation anno : clazz.getAnnotations()) {
			String annoName = anno.annotationType().getName();
			switch (annoName) {
				case "javax.enterprise.context.Dependent": 
					return Scope.SESSION;
				case "javax.ejb.Stateful":
					return Scope.SESSION;
				default:
			}
		}
		return Scope.MANAGED;
	}
}
{'fraction_non_alphanumeric': 0.11450599657338664, 'fraction_numerical': 0.002284408909194746, 'mean_word_length': 4.659127625201939, 'pattern_counts': {'":': 4, '<': 13, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '5526900', 'n_tokens_mistral': 1195, 'n_tokens_neox': 1117, 'n_words': 266}