001    /*
002     * Copyright (c) 2007-2015 Concurrent, 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    
021    package cascading.management.state;
022    
023    import java.util.Map;
024    
025    import cascading.management.CascadingServices;
026    import cascading.management.DocumentService;
027    import cascading.management.MetricsService;
028    import cascading.provider.CascadingService;
029    import org.slf4j.Logger;
030    import org.slf4j.LoggerFactory;
031    
032    /**
033     *
034     */
035    public abstract class BaseState implements CascadingService
036      {
037      private static final Logger LOG = LoggerFactory.getLogger( BaseState.class );
038    
039      private String id;
040    
041      MetricsService metricsService = new CascadingServices.NullMetricsService();
042      DocumentService documentService = new CascadingServices.NullDocumentService();
043    
044      public BaseState()
045        {
046        }
047    
048      @Override
049      public boolean isEnabled()
050        {
051        return metricsService.isEnabled() || documentService.isEnabled();
052        }
053    
054      @Override
055      public void setProperties( Map<Object, Object> properties )
056        {
057        }
058    
059      public void initialize( CascadingServices cascadingServices, String id )
060        {
061        this.id = id;
062    
063        if( cascadingServices == null )
064          return;
065    
066        metricsService = cascadingServices.getMetricsService();
067        documentService = cascadingServices.getDocumentService();
068        }
069    
070      /** May be called more than once. Each internal service should be idempotent. */
071      public synchronized void startService()
072        {
073        if( !safelyStartService( metricsService ) )
074          metricsService = new CascadingServices.NullMetricsService();
075    
076        if( !safelyStartService( documentService ) )
077          documentService = new CascadingServices.NullDocumentService();
078        }
079    
080      private boolean safelyStartService( CascadingService service )
081        {
082        try
083          {
084          service.startService();
085          return true;
086          }
087        catch( Throwable throwable )
088          {
089          LOG.warn( "unable to start cascading service: {}, with message: {}", service.getClass().getName(), throwable.getMessage() );
090          LOG.debug( "with exception", throwable );
091          return false;
092          }
093        }
094    
095      public void stopService()
096        {
097        // not stopping services, they are singletons,
098        // and need to live beyond the shutdown hooks
099        }
100    
101      String[] getContext( Enum context )
102        {
103        return getContext( getGroup( context ), context.toString() );
104        }
105    
106      String getGroup( Enum metric )
107        {
108        return metric.getClass().getSimpleName();
109        }
110    
111      abstract String[] getContext( String group, String metric );
112    
113      public String getID()
114        {
115        return id;
116        }
117    
118      protected void store( String id, Object value )
119        {
120        documentService.put( id, value );
121        }
122    
123      protected void setMetric( Enum metric, long value )
124        {
125        metricsService.set( getContext( metric ), value );
126        }
127    
128      protected void setMetric( String group, String metric, long value )
129        {
130        metricsService.set( getContext( group, metric ), value );
131        }
132    
133      protected void setMetric( String group, String metric, String value )
134        {
135        metricsService.set( getContext( group, metric ), value );
136        }
137    
138      private void incrementMetric( Enum metric, int value )
139        {
140        metricsService.increment( getContext( metric ), value );
141        }
142    
143      private void incrementMetric( String group, String metric, int value )
144        {
145        metricsService.increment( getContext( group, metric ), value );
146        }
147    
148      String[] asArray( String... strings )
149        {
150        return strings;
151        }
152      }