/*
 * Copyright (c) 2012-present NAVER Corp.
 *
 * This file is part of The nGrinder software distribution. Refer to
 * the file LICENSE which is part of The nGrinder distribution for
 * licensing details. The nGrinder distribution is available on the
 * Internet at https://naver.github.io/ngrinder
 *
 * 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 net.grinder.scriptengine.groovy;

import net.grinder.common.GrinderProperties;
import net.grinder.engine.common.EngineException;
import net.grinder.engine.common.ScriptLocation;
import net.grinder.engine.process.JavaDCRInstrumenterEx;
import net.grinder.scriptengine.DCRContext;
import net.grinder.scriptengine.Instrumenter;
import net.grinder.scriptengine.ScriptEngineService;
import net.grinder.util.FileExtensionMatcher;

import java.util.ArrayList;
import java.util.List;

/**
 * Groovy script engine service.
 *
 * @author Mavlarn
 * @since 3.0
 */
public class GroovyScriptEngineService implements ScriptEngineService {

	private final FileExtensionMatcher m_groovyFileMatcher = new FileExtensionMatcher(".groovy");

	@SuppressWarnings("unused")
	private final boolean m_forceDCRInstrumentation;
	private final DCRContext m_dcrContext;

	/**
	 * Constructor.
	 *
	 * @param properties		Properties.
	 * @param dcrContext		DCR context.
	 * @param scriptLocation	Script location.
	 */
	public GroovyScriptEngineService(GrinderProperties properties, //
					DCRContext dcrContext, ScriptLocation scriptLocation) {

		// This property name is poor, since it really means "If DCR
		// instrumentation is available, avoid the traditional Jython
		// instrumenter". I'm not renaming it, since I expect it only to last
		// a few releases, until DCR becomes the default.
		m_forceDCRInstrumentation = properties.getBoolean("grinder.dcrinstrumentation", false)
		// Hack: force DCR instrumentation for non-Jython scripts.
						|| m_groovyFileMatcher.accept(scriptLocation.getFile());

		m_dcrContext = dcrContext;
	}

	/**
	 * Constructor used when DCR is unavailable.
	 */
	public GroovyScriptEngineService() {
		m_dcrContext = null;
		m_forceDCRInstrumentation = false;
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public List<Instrumenter> createInstrumenters() {

		final List<Instrumenter> instrumenters = new ArrayList<>();

		/*
		 * if (!m_forceDCRInstrumentation) {
		 * System.out.println("m_forceDCRInstrumentation is false."); // must using Instrumentation
		 * }
		 */
		if (m_dcrContext != null) {
			instrumenters.add(new JavaDCRInstrumenterEx(m_dcrContext));
		}

		return instrumenters;
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public ScriptEngine createScriptEngine(ScriptLocation script) throws EngineException {

		if (m_groovyFileMatcher.accept(script.getFile())) {
			return new GroovyScriptEngine(script);
		}

		return null;
	}

}

@@ -1066,66 +1066,8 @@
 n;%0D%0A
-import net.grinder.engine.process.JavaDCRInstrumenterEx;%0D%0A
 impo
@@ -1278,27 +1278,31 @@
 til.
-Array
 List;%0D%0A
+%0D%0A
 import 
+static 
 java
@@ -1299,32 +1299,49 @@
 tatic java.util.
+Collections.empty
 List;%0D%0A%0D%0A/**%0D%0A *
@@ -1673,49 +1673,8 @@
 on;%0D
-%0A%09private final DCRContext m_dcrContext;%0D
 %0A%0D%0A%09
@@ -1722,18 +1722,21 @@
 operties
-%09%09
+     
 Properti
@@ -1761,18 +1761,21 @@
 rContext
-%09%09
+     
 DCR cont
@@ -1805,17 +1805,17 @@
 Location
-%09
+ 
 Script l
@@ -1898,17 +1898,8 @@
 es, 
-//%0D%0A%09%09%09%09%09
 DCRC
@@ -1951,18 +1951,16 @@
 ion) %7B%0D%0A
-%0D%0A
 %09%09// Thi
@@ -2298,16 +2298,17 @@
 lse)%0D%0A%09%09
+%09
 // Hack:
@@ -2359,19 +2359,16 @@
 ripts.%0D%0A
-%09%09%09
 %09%09%09%7C%7C m_
@@ -2423,40 +2423,8 @@
 ));%0D
-%0A%0D%0A%09%09m_dcrContext = dcrContext;%0D
 %0A%09%7D%0D
@@ -2528,32 +2528,8 @@
  %7B%0D%0A
-%09%09m_dcrContext = null;%0D%0A
 %09%09m_
@@ -2667,355 +2667,28 @@
  %7B%0D%0A
-%0D%0A%09%09final List%3CInstrumenter%3E instrumenters = new ArrayList%3C%3E();%0D%0A%0D%0A%09%09/*%0D%0A%09%09 * if (!m_forceDCRInstrumentation) %7B%0D%0A%09%09 * System.out.println(%22m_forceDCRInstrumentation is false.%22); // must using Instrumentation%0D%0A%09%09 * %7D%0D%0A%09%09 */%0D%0A%09%09if (m_dcrContext != null) %7B%0D%0A%09%09%09instrumenters.add(new JavaDCRInstrumenterEx(m_dcrContext));%0D%0A%09%09%7D%0D%0A%0D%0A%09%09return instrumenters
+%09%09return emptyList()
 ;%0D%0A%09

Make groovy engine service always delegate instrumentation