    001/*
    002 * Copyright (c) 2007-2017 Xplenty, Inc. All Rights Reserved.
    003 *
    004 * Project and contact information: http://www.cascading.org/
    005 *
    006 * This file is part of the Cascading project.
    007 *
    008 * Licensed under the Apache License, Version 2.0 (the "License");
    009 * you may not use this file except in compliance with the License.
    010 * You may obtain a copy of the License at
    011 *
    012 *     http://www.apache.org/licenses/LICENSE-2.0
    013 *
    014 * Unless required by applicable law or agreed to in writing, software
    015 * distributed under the License is distributed on an "AS IS" BASIS,
    016 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    017 * See the License for the specific language governing permissions and
    018 * limitations under the License.
    019 */
    020
    021package cascading.flow.planner;
    022
    023import java.util.HashMap;
    024import java.util.Map;
    025import java.util.Properties;
    026
    027import cascading.flow.Flow;
    028import cascading.flow.FlowDef;
    029import cascading.flow.planner.iso.transformer.ElementFactory;
    030import cascading.flow.planner.rule.RuleRegistry;
    031import cascading.operation.PlannerLevel;
    032import cascading.property.PropertyUtil;
    033import cascading.util.ProcessLogger;
    034
    035/**
    036 *
    037 */
    038public class PlannerContext
    039  {
    040  public static final PlannerContext NULL = new PlannerContext();
    041
    042  RuleRegistry ruleRegistry;
    043  FlowPlanner flowPlanner;
    044  FlowDef flowDef;
    045  Flow flow;
    046  boolean isTransformTracingEnabled = false;
    047  private Map properties;
    048
    049  public PlannerContext()
    050    {
    051    this.properties = new Properties();
    052    }
    053
    054  public PlannerContext( RuleRegistry ruleRegistry )
    055    {
    056    this.ruleRegistry = ruleRegistry;
    057    }
    058
    059  public PlannerContext( RuleRegistry ruleRegistry, FlowPlanner flowPlanner, FlowDef flowDef, Flow flow, boolean isTransformTracingEnabled )
    060    {
    061    this.ruleRegistry = ruleRegistry;
    062    this.flowPlanner = flowPlanner;
    063    this.flowDef = flowDef;
    064    this.flow = flow;
    065    this.isTransformTracingEnabled = isTransformTracingEnabled;
    066
    067    if( flowPlanner != null )
    068      this.properties = flowPlanner.getDefaultProperties();
    069    else
    070      this.properties = new Properties();
    071    }
    072
    073  public String getStringProperty( String property )
    074    {
    075    return PropertyUtil.getStringProperty( System.getProperties(), properties, property );
    076    }
    077
    078  public int getIntProperty( String property, int defaultValue )
    079    {
    080    return PropertyUtil.getIntProperty( System.getProperties(), properties, property, defaultValue );
    081    }
    082
    083  public RuleRegistry getRuleRegistry()
    084    {
    085    return ruleRegistry;
    086    }
    087
    088  public FlowPlanner getFlowPlanner()
    089    {
    090    return flowPlanner;
    091    }
    092
    093  public FlowDef getFlowDef()
    094    {
    095    return flowDef;
    096    }
    097
    098  public Flow getFlow()
    099    {
    100    return flow;
    101    }
    102
    103  public ProcessLogger getLogger()
    104    {
    105    Flow flow = getFlow();
    106
    107    if( flow == null )
    108      return ProcessLogger.NULL;
    109
    110    return (ProcessLogger) flow;
    111    }
    112
    113  public boolean isTransformTracingEnabled()
    114    {
    115    return isTransformTracingEnabled;
    116    }
    117
    118  public PlannerLevel getPlannerLevelFor( Class<? extends PlannerLevel> plannerLevelClass )
    119    {
    120    Map<Class<? extends PlannerLevel>, PlannerLevel> levels = new HashMap<>();
    121
    122    addLevel( levels, flowPlanner.getDebugLevel( flowDef ) );
    123    addLevel( levels, flowPlanner.getAssertionLevel( flowDef ) );
    124
    125    return levels.get( plannerLevelClass );
    126    }
    127
    128  private void addLevel( Map<Class<? extends PlannerLevel>, PlannerLevel> levels, PlannerLevel level )
    129    {
    130    if( level != null )
    131      levels.put( level.getClass(), level );
    132    }
    133
    134  public ElementFactory getElementFactoryFor( String factoryName )
    135    {
    136    return ruleRegistry.getElementFactory( factoryName );
    137    }
    138  }