    001/*
    002 * Copyright (c) 2016-2017 Chris K Wensel <chris@wensel.net>. All Rights Reserved.
    003 * Copyright (c) 2007-2017 Xplenty, Inc. All Rights Reserved.
    004 *
    005 * Project and contact information: http://www.cascading.org/
    006 *
    007 * This file is part of the Cascading project.
    008 *
    009 * Licensed under the Apache License, Version 2.0 (the "License");
    010 * you may not use this file except in compliance with the License.
    011 * You may obtain a copy of the License at
    012 *
    013 *     http://www.apache.org/licenses/LICENSE-2.0
    014 *
    015 * Unless required by applicable law or agreed to in writing, software
    016 * distributed under the License is distributed on an "AS IS" BASIS,
    017 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    018 * See the License for the specific language governing permissions and
    019 * limitations under the License.
    020 */
    021
    022package cascading.flow.local;
    023
    024import java.io.IOException;
    025import java.util.Collection;
    026import java.util.Collections;
    027import java.util.Map;
    028import java.util.Properties;
    029
    030import cascading.CascadingException;
    031import cascading.flow.FlowProcess;
    032import cascading.flow.FlowSession;
    033import cascading.stats.local.LocalStepStats;
    034import cascading.tap.Tap;
    035import cascading.tuple.TupleEntryCollector;
    036import cascading.tuple.TupleEntryIterator;
    037
    038/** Class LocalFlowProcess is the local mode implementation of {@link FlowProcess}. */
    039public class LocalFlowProcess extends FlowProcess<Properties>
    040  {
    041  private final Properties config;
    042  private LocalStepStats stepStats;
    043
    044  public LocalFlowProcess()
    045    {
    046    config = new Properties();
    047    }
    048
    049  public LocalFlowProcess( Properties config )
    050    {
    051    this.config = config;
    052    }
    053
    054  public LocalFlowProcess( FlowSession flowSession, Properties config )
    055    {
    056    super( flowSession );
    057    this.config = config;
    058    }
    059
    060  public LocalFlowProcess( LocalFlowProcess flowProcess, Properties properties )
    061    {
    062    super( flowProcess );
    063    this.config = properties;
    064    this.stepStats = flowProcess.stepStats;
    065    }
    066
    067  public void setStepStats( LocalStepStats stepStats )
    068    {
    069    this.stepStats = stepStats;
    070    }
    071
    072  @Override
    073  public int getNumProcessSlices()
    074    {
    075    return 1;
    076    }
    077
    078  @Override
    079  public int getCurrentSliceNum()
    080    {
    081    return 0;
    082    }
    083
    084  @Override
    085  public Object getProperty( String key )
    086    {
    087    return config.getProperty( key );
    088    }
    089
    090  @Override
    091  public Collection<String> getPropertyKeys()
    092    {
    093    return Collections.unmodifiableSet( config.stringPropertyNames() );
    094    }
    095
    096  @Override
    097  public Object newInstance( String className )
    098    {
    099    if( className == null || className.isEmpty() )
    100      return null;
    101
    102    try
    103      {
    104      Class type = LocalFlowProcess.class.getClassLoader().loadClass( className );
    105
    106      return type.newInstance();
    107      }
    108    catch( ClassNotFoundException exception )
    109      {
    110      throw new CascadingException( "unable to load class: " + className, exception );
    111      }
    112    catch( InstantiationException exception )
    113      {
    114      throw new CascadingException( "unable to instantiate class: " + className, exception );
    115      }
    116    catch( IllegalAccessException exception )
    117      {
    118      throw new CascadingException( "unable to access class: " + className, exception );
    119      }
    120    }
    121
    122  @Override
    123  public void keepAlive()
    124    {
    125    }
    126
    127  @Override
    128  public void increment( Enum counter, long amount )
    129    {
    130    stepStats.increment( counter, amount );
    131    }
    132
    133  @Override
    134  public void increment( String group, String counter, long amount )
    135    {
    136    stepStats.increment( group, counter, amount );
    137    }
    138
    139  @Override
    140  public long getCounterValue( Enum counter )
    141    {
    142    return stepStats.getCounterValue( counter );
    143    }
    144
    145  @Override
    146  public long getCounterValue( String group, String counter )
    147    {
    148    return stepStats.getCounterValue( group, counter );
    149    }
    150
    151  @Override
    152  public void setStatus( String status )
    153    {
    154
    155    }
    156
    157  @Override
    158  public boolean isCounterStatusInitialized()
    159    {
    160    return true;
    161    }
    162
    163  @Override
    164  public TupleEntryIterator openTapForRead( Tap tap ) throws IOException
    165    {
    166    return tap.openForRead( this );
    167    }
    168
    169  @Override
    170  public TupleEntryCollector openTapForWrite( Tap tap ) throws IOException
    171    {
    172    return tap.openForWrite( this, null ); // do not honor sinkmode as this may be opened across tasks
    173    }
    174
    175  @Override
    176  public TupleEntryCollector openTrapForWrite( Tap trap ) throws IOException
    177    {
    178    return trap.openForWrite( this, null ); // do not honor sinkmode as this may be opened across tasks
    179    }
    180
    181  @Override
    182  public TupleEntryCollector openSystemIntermediateForWrite() throws IOException
    183    {
    184    return null;
    185    }
    186
    187  @Override
    188  public FlowProcess copyWith( Properties object )
    189    {
    190    return new LocalFlowProcess( this, object );
    191    }
    192
    193  @Override
    194  public Properties getConfig()
    195    {
    196    return config;
    197    }
    198
    199  @Override
    200  public Properties getConfigCopy()
    201    {
    202    return new Properties( config );
    203    }
    204
    205  @Override
    206  public <C> C copyConfig( C config )
    207    {
    208    return (C) new Properties( (Properties) config );
    209    }
    210
    211  @Override
    212  public <C> Map<String, String> diffConfigIntoMap( C defaultConfig, C updatedConfig )
    213    {
    214    return null;
    215    }
    216
    217  @Override
    218  public Properties mergeMapIntoConfig( Properties defaultConfig, Map<String, String> map )
    219    {
    220    return null;
    221    }
    222  }