<commit_before>/**
 * Copyright 2007-2015, Kaazing Corporation. 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 org.kaazing.k3po.driver.internal.resolver;

import java.net.URI;

import javax.el.ELContext;
import javax.el.PropertyNotFoundException;
import javax.el.ValueExpression;

import org.jboss.netty.logging.InternalLogger;
import org.jboss.netty.logging.InternalLoggerFactory;
import org.kaazing.k3po.driver.internal.behavior.visitor.GenerateConfigurationVisitor;
import org.kaazing.k3po.lang.internal.ast.value.AstLocation;
import org.kaazing.k3po.lang.internal.ast.value.AstLocationExpression;
import org.kaazing.k3po.lang.internal.ast.value.AstLocationLiteral;

/**
 * The class is used to defer the evaluation of location such as
 * accept/connect uri in {@link GenerateConfigurationVisitor}. In scenarios when
 * accept/connect takes expression value which only gets resolved during the
 * script execution, it is necessary to defer the resolution of accept/connect
 * uri.
 *
 */
public class LocationResolver {

    private static final LocationVisitorImpl VISITOR = new LocationVisitorImpl();
    private static final InternalLogger LOGGER = InternalLoggerFactory.getInstance(LocationResolver.class);

    private final AstLocation location;
    private final ELContext environment;

    private URI evaluatedURI;

    public LocationResolver(AstLocation location, ELContext environment) {
        this.location = location;
        this.environment = environment;
    }

    public URI resolve() throws Exception {
        if (evaluatedURI == null) {
            evaluatedURI = location.accept(VISITOR, environment);
        }
        return evaluatedURI;
    }

    private static class LocationVisitorImpl implements AstLocation.Visitor<URI, ELContext> {

        @Override
        public URI visit(AstLocationLiteral value, ELContext parameter) throws Exception {
            return value.getValue();
        }

        @Override
        public URI visit(AstLocationExpression value, ELContext environment) throws Exception {
            Object location;

            // TODO: Remove when JUEL sync bug is fixed https://github.com/k3po/k3po/issues/147
            try {
                synchronized (environment) {
                    ValueExpression expression = value.getValue();
                    location = expression.getValue(environment);
                }
            } catch (PropertyNotFoundException e) {
                LOGGER.warn(e.getMessage());
                location = null;
            }

            if (location == null) {
                throw new NullPointerException("Location expression result is null");
            }

            return (URI) location;
        }

    }
}<commit_msg>passed e to log message<commit_after>/**
 * Copyright 2007-2015, Kaazing Corporation. 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 org.kaazing.k3po.driver.internal.resolver;

import java.net.URI;

import javax.el.ELContext;
import javax.el.PropertyNotFoundException;
import javax.el.ValueExpression;

import org.jboss.netty.logging.InternalLogger;
import org.jboss.netty.logging.InternalLoggerFactory;
import org.kaazing.k3po.driver.internal.behavior.visitor.GenerateConfigurationVisitor;
import org.kaazing.k3po.lang.internal.ast.value.AstLocation;
import org.kaazing.k3po.lang.internal.ast.value.AstLocationExpression;
import org.kaazing.k3po.lang.internal.ast.value.AstLocationLiteral;

/**
 * The class is used to defer the evaluation of location such as
 * accept/connect uri in {@link GenerateConfigurationVisitor}. In scenarios when
 * accept/connect takes expression value which only gets resolved during the
 * script execution, it is necessary to defer the resolution of accept/connect
 * uri.
 *
 */
public class LocationResolver {

    private static final LocationVisitorImpl VISITOR = new LocationVisitorImpl();
    private static final InternalLogger LOGGER = InternalLoggerFactory.getInstance(LocationResolver.class);

    private final AstLocation location;
    private final ELContext environment;

    private URI evaluatedURI;

    public LocationResolver(AstLocation location, ELContext environment) {
        this.location = location;
        this.environment = environment;
    }

    public URI resolve() throws Exception {
        if (evaluatedURI == null) {
            evaluatedURI = location.accept(VISITOR, environment);
        }
        return evaluatedURI;
    }

    private static class LocationVisitorImpl implements AstLocation.Visitor<URI, ELContext> {

        @Override
        public URI visit(AstLocationLiteral value, ELContext parameter) throws Exception {
            return value.getValue();
        }

        @Override
        public URI visit(AstLocationExpression value, ELContext environment) throws Exception {
            Object location;

            // TODO: Remove when JUEL sync bug is fixed https://github.com/k3po/k3po/issues/147
            try {
                synchronized (environment) {
                    ValueExpression expression = value.getValue();
                    location = expression.getValue(environment);
                }
            } catch (PropertyNotFoundException e) {
                LOGGER.warn(e.getMessage(), e);
                location = null;
            }

            if (location == null) {
                throw new NullPointerException("Location expression result is null");
            }

            return (URI) location;
        }

    }
}
{'fraction_non_alphanumeric': 0.07619483890670331, 'fraction_numerical': 0.00671858298976943, 'mean_word_length': 2.9793438639125154, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 2, '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': '6038401', 'n_tokens_mistral': 1646, 'n_tokens_neox': 1563, 'n_words': 533}