Save This Page
Home » geronimo-2.2-source-release » org.apache.geronimo.kernel.config.transformer » [javadoc | source]
    1   /*
    2    * Licensed to the Apache Software Foundation (ASF) under one
    3    * or more contributor license agreements.  See the NOTICE file
    4    * distributed with this work for additional information
    5    * regarding copyright ownership.  The ASF licenses this file
    6    * to you under the Apache License, Version 2.0 (the
    7    * "License"); you may not use this file except in compliance
    8    * with the License.  You may obtain a copy of the License at
    9    *
   10    *  http://www.apache.org/licenses/LICENSE-2.0
   11    *
   12    * Unless required by applicable law or agreed to in writing,
   13    * software distributed under the License is distributed on an
   14    * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
   15    * KIND, either express or implied.  See the License for the
   16    * specific language governing permissions and limitations
   17    * under the License.
   18    */
   19   
   20   package org.apache.geronimo.kernel.config.transformer;
   21   
   22   import groovy.lang.Binding;
   23   import groovy.lang.GroovyShell;
   24   
   25   import java.io.File;
   26   import java.util.ArrayList;
   27   import java.util.HashMap;
   28   import java.util.List;
   29   import java.util.Map;
   30   import java.util.regex.Pattern;
   31   
   32   import org.apache.geronimo.gbean.GBeanData;
   33   import org.apache.geronimo.kernel.config.ConfigurationData;
   34   import org.apache.geronimo.kernel.config.ConfigurationDataTransformer;
   35   import org.apache.geronimo.kernel.config.InvalidConfigException;
   36   import org.apache.geronimo.kernel.repository.Artifact;
   37   
   38   
   39   /**
   40    *
   41    * @version $Rev:$ $Date:$
   42    */
   43   public class GroovyTransformer implements ConfigurationDataTransformer {
   44       private static final String BINDING_CONFIGURATION_DATA = "configurationData";
   45       private static final String BINDING_CONFIGURATION_DATA_BUILDER = "configurationDataBuilder";
   46       private static final Pattern PATTERN_DEPENDENCIES_FILE = Pattern.compile("Dependencies(.*).groovy"); 
   47   
   48       private static final String BINDING_GBEAN_DATAS = "gbeanDatas";
   49       private static final String BINDING_GBEAN_DATAS_BUILDER = "gbeanDataBuilder";
   50       private static final Pattern PATTERN_GBEANS_FILE = Pattern.compile("GBeans(.*).groovy"); 
   51       
   52       private final ScriptLocater scriptLocater;
   53   
   54       public GroovyTransformer() {
   55           scriptLocater = newScriptLocater();
   56       }
   57   
   58       public void transformDependencies(ConfigurationData configurationData) throws InvalidConfigException {
   59           configurationData.setConfigurationDataTransformer(this);
   60   
   61           File scriptDir = scriptLocater.locate(configurationData);
   62           
   63           Map<String, Object> bindings = new HashMap<String, Object>();
   64           bindings.put(BINDING_CONFIGURATION_DATA, configurationData);
   65           bindings.put(BINDING_CONFIGURATION_DATA_BUILDER, new ConfigurationDataBuilder(configurationData));
   66           Binding binding = new Binding(bindings);
   67           GroovyShell shell = new GroovyShell(binding);
   68   
   69           executeScripts(shell, scriptDir, PATTERN_DEPENDENCIES_FILE);
   70       }
   71   
   72       public List<GBeanData> transformGBeans(ClassLoader classLoader,
   73               ConfigurationData configurationData,
   74               List<GBeanData> gbeanDatas) throws InvalidConfigException {
   75           gbeanDatas = new ArrayList<GBeanData>(gbeanDatas);
   76           
   77           File scriptDir = scriptLocater.locate(configurationData);
   78   
   79           Map<String, Object> bindings = new HashMap<String, Object>();
   80           bindings.put(BINDING_CONFIGURATION_DATA, configurationData);
   81           bindings.put(BINDING_GBEAN_DATAS, gbeanDatas);
   82           bindings.put(BINDING_GBEAN_DATAS_BUILDER, new GBeanDataBuilder(configurationData, gbeanDatas));
   83           Binding binding = new Binding(bindings);
   84           GroovyShell shell = new GroovyShell(classLoader, binding);
   85   
   86           executeScripts(shell, scriptDir, PATTERN_GBEANS_FILE);
   87   
   88           return gbeanDatas;
   89       }
   90       
   91       public void remove(Artifact configId) {
   92       }
   93       
   94       protected ScriptLocater newScriptLocater() {
   95           return new CollocatedWithConfigInfoLocater();
   96       }
   97   
   98       protected void executeScripts(GroovyShell shell, File scriptDir, Pattern pattern) throws InvalidConfigException {
   99           File[] scripts = scriptDir.listFiles(new PatternFilter(pattern));
  100           executeScripts(shell, scripts);
  101       }
  102   
  103       protected void executeScripts(GroovyShell shell, File[] scripts) throws InvalidConfigException {
  104           for (File script : scripts) {
  105               try {
  106                   shell.evaluate(script);
  107               } catch (Exception e) {
  108                   if (e instanceof InvalidConfigException) {
  109                       throw (InvalidConfigException) e;
  110                   }
  111                   throw new GroovyScriptException("Error while evaluating [" + script.getAbsolutePath() + "]", e);
  112               }
  113           }
  114       }
  115   
  116   }

Save This Page
Home » geronimo-2.2-source-release » org.apache.geronimo.kernel.config.transformer » [javadoc | source]