001 /* 002 * Copyright (C) 2006-2007 the original author or authors. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 017 package org.codehaus.gmaven.runtime.v1_5; 018 019 import groovy.lang.Closure; 020 import groovy.util.AntBuilder; 021 import org.apache.tools.ant.BuildLogger; 022 import org.codehaus.gmaven.feature.Component; 023 import org.codehaus.gmaven.feature.ComponentException; 024 import org.codehaus.gmaven.feature.support.FeatureSupport; 025 import org.codehaus.gmaven.runtime.ClassFactory; 026 import org.codehaus.gmaven.runtime.ScriptExecutor; 027 import org.codehaus.gmaven.runtime.support.ScriptExecutorSupport; 028 import org.codehaus.gmaven.runtime.util.Callable; 029 import org.codehaus.gmaven.runtime.util.MagicAttribute; 030 031 /** 032 * Provides the script execution feature. 033 * 034 * @version $Id: ScriptExecutorFeature.java 19 2009-07-16 09:40:33Z user57 $ 035 * @author <a href="mailto:jason@planet57.com">Jason Dillon</a> 036 */ 037 public class ScriptExecutorFeature 038 extends FeatureSupport 039 { 040 public ScriptExecutorFeature() { 041 super(ScriptExecutor.KEY); 042 } 043 044 protected Component doCreate() throws Exception { 045 return new ScriptExecutorImpl(); 046 } 047 048 // 049 // ScriptExecutorImpl 050 // 051 052 private class ScriptExecutorImpl 053 extends ScriptExecutorSupport 054 { 055 private ScriptExecutorImpl() throws Exception { 056 super(ScriptExecutorFeature.this); 057 } 058 059 protected ClassFactory getClassFactory() { 060 try { 061 return (ClassFactory) provider().feature(ClassFactory.KEY).create(config()); 062 } 063 catch (Exception e) { 064 throw new ComponentException(e); 065 } 066 } 067 068 protected Object createClosure(final Callable target) { 069 assert target != null; 070 071 return new Closure(this) { 072 public Object call(final Object[] args) { 073 try { 074 return target.call(args); 075 } 076 catch (Exception e) { 077 return throwRuntimeException(e); 078 } 079 } 080 }; 081 } 082 083 private AntBuilder createAntBuilder() { 084 AntBuilder ant = new AntBuilder(); 085 086 Object obj = ant.getAntProject().getBuildListeners().elementAt(0); 087 088 if (obj instanceof BuildLogger) { 089 BuildLogger logger = (BuildLogger)obj; 090 091 logger.setEmacsMode(true); 092 } 093 094 return ant; 095 } 096 097 protected Object createMagicAttribute(final MagicAttribute attr) { 098 assert attr != null; 099 100 if (attr == MagicAttribute.ANT_BUILDER) { 101 return createAntBuilder(); 102 } 103 104 throw new ComponentException("Unknown magic attribute: " + attr); 105 } 106 } 107 }