<commit_before>/***
 * Copyright (c) 2009 Caelum - www.caelum.com.br/opensource All rights reserved.
 *
 * 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 br.com.caelum.vraptor.view;

import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javassist.CannotCompileException;
import javassist.ClassPool;
import javassist.CtClass;
import javassist.CtMethod;
import javassist.CtNewMethod;

import javax.annotation.PostConstruct;
import javax.enterprise.context.ApplicationScoped;
import javax.inject.Inject;
import javax.inject.Named;
import javax.servlet.ServletContext;

import net.vidageek.mirror.dsl.Mirror;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import br.com.caelum.vraptor.controller.BeanClass;
import br.com.caelum.vraptor.http.route.Router;
import br.com.caelum.vraptor.proxy.MethodInvocation;
import br.com.caelum.vraptor.proxy.Proxifier;
import br.com.caelum.vraptor.proxy.ProxyCreationException;
import br.com.caelum.vraptor.proxy.SuperMethod;
import br.com.caelum.vraptor.util.StringUtils;

import com.google.common.collect.ForwardingMap;

/**
 * View helper for generating uris
 * @author <NAME>
 * @since 3.4.0
 *
 */
@Named("linkTo")
@ApplicationScoped
public class LinkToHandler extends ForwardingMap<Class<?>, Object> {

	private static final Logger logger = LoggerFactory.getLogger(LinkToHandler.class);

	private ServletContext context;
	private Router router;

	private Proxifier proxifier;

	@Deprecated // CDI eyes only
	public LinkToHandler() {
	}

	@Inject
	public LinkToHandler(ServletContext context, Router router, Proxifier proxifier) {
		this.context = context;
		this.router = router;
		this.proxifier = proxifier;
	}

	@PostConstruct
	public void start() {
		logger.info("Registering linkTo component");
	}

	@Override
	protected Map<Class<?>, Object> delegate() {
		return Collections.emptyMap();
	}

	@Override
	public Object get(Object key) {
		BeanClass beanClass = (BeanClass) key;
		final Class<?> controller = beanClass.getType();
		String interfaceName = controller.getName() + "$linkTo";
		Class<?> linkToInterface;
		try {
			linkToInterface = Class.forName(interfaceName);
		} catch (ClassNotFoundException _) {
			linkToInterface = createLinkToInterface(controller, interfaceName);
		}
		return proxifier.proxify(linkToInterface, new MethodInvocation<Object>() {
			@Override
			public Object intercept(Object proxy, Method method, Object[] args, SuperMethod superMethod) {
				String methodName = StringUtils.decapitalize(method.getName().replaceFirst("^get", ""));<commit_msg>Locking the class creation, since it must not be executed more than once<commit_after>/***
 * Copyright (c) 2009 Caelum - www.caelum.com.br/opensource All rights reserved.
 *
 * 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 br.com.caelum.vraptor.view;

import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

import javassist.CannotCompileException;
import javassist.ClassPool;
import javassist.CtClass;
import javassist.CtMethod;
import javassist.CtNewMethod;

import javax.annotation.PostConstruct;
import javax.enterprise.context.ApplicationScoped;
import javax.inject.Inject;
import javax.inject.Named;
import javax.servlet.ServletContext;

import net.vidageek.mirror.dsl.Mirror;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import br.com.caelum.vraptor.controller.BeanClass;
import br.com.caelum.vraptor.http.route.Router;
import br.com.caelum.vraptor.proxy.MethodInvocation;
import br.com.caelum.vraptor.proxy.Proxifier;
import br.com.caelum.vraptor.proxy.ProxyCreationException;
import br.com.caelum.vraptor.proxy.SuperMethod;
import br.com.caelum.vraptor.util.StringUtils;

import com.google.common.collect.ForwardingMap;

/**
 * View helper for generating uris
 * @author <NAME>
 * @since 3.4.0
 *
 */
@Named("linkTo")
@ApplicationScoped
public class LinkToHandler extends ForwardingMap<Class<?>, Object> {

	private static final Logger logger = LoggerFactory.getLogger(LinkToHandler.class);

	private ServletContext context;
	private Router router;

	private Proxifier proxifier;

	private ConcurrentMap<Class<?>, Class<?>> interfaces = new ConcurrentHashMap<>();

	@Deprecated // CDI eyes only
	public LinkToHandler() {
	}

	@Inject
	public LinkToHandler(ServletContext context, Router router, Proxifier proxifier) {
		this.context = context;
		this.router = router;
		this.proxifier = proxifier;
	}

	@PostConstruct
	public void start() {
		logger.info("Registering linkTo component");
	}

	@Override
	protected Map<Class<?>, Object> delegate() {
		return Collections.emptyMap();
	}

	private Lock lock = new ReentrantLock();

	@Override
	public Object get(Object key) {
		BeanClass beanClass = (BeanClass) key;
		final Class<?> controller = beanClass.getType();
		Class<?> linkToInterface = interfaces.get(controller);
		if (linkToInterface == null) {
			lock.lock();
			try {
				linkToInterface = interfaces.get(controller);
				if (linkToInterface == null) {
					String interfaceName = controller.getName() + "$linkTo";
					linkToInterface = createLinkToInterface(controller, interfaceName);
					interfaces.put(controller, linkToInterface);
				}
			} finally {
				lock.unlock();
			}
		}
		return proxifier.proxify(linkToInterface, new MethodInvocation<Object>() {
			@Override
			public Object intercept(Object proxy, Method method, Object[] args, SuperMethod superMethod) {
				String methodName = StringUtils.decapitalize(method.getName().replaceFirst("^get", ""));
{'fraction_non_alphanumeric': 0.1052860246198407, 'fraction_numerical': 0.003765387400434468, 'mean_word_length': 4.98959236773634, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 4, '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': '6649269', 'n_tokens_mistral': 2200, 'n_tokens_neox': 2084, 'n_words': 553}