001/*
002 * Copyright (c) 2007-2016 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
021package cascading.provider;
022
023import java.net.URL;
024import java.util.Collection;
025import java.util.Collections;
026import java.util.HashMap;
027import java.util.Map;
028import java.util.Properties;
029
030import static cascading.property.PropertyUtil.getStringProperty;
031
032/**
033 * Class ServiceLoader is an implementation of a {@link ProviderLoader} and is used to load
034 * {@link CascadingService} instances used by internal frameworks.
035 *
036 * @see CascadingService
037 */
038public class ServiceLoader extends ProviderLoader<CascadingService>
039  {
040  private static ServiceLoader serviceLoader;
041
042  private Map<String, CascadingService> singletons = new HashMap<String, CascadingService>();
043
044  public synchronized static ServiceLoader getInstance( URL libraryURL, String[] exclusions )
045    {
046    if( serviceLoader == null )
047      serviceLoader = new ServiceLoader( libraryURL, exclusions );
048
049    return serviceLoader;
050    }
051
052  public synchronized static void releaseSingletonsAndDestroy()
053    {
054    if( serviceLoader != null )
055      serviceLoader.releaseSingletonServices();
056
057    serviceLoader = null;
058    }
059
060  ServiceLoader( URL libraryURL, String[] exclusions )
061    {
062    super( exclusions, libraryURL );
063    }
064
065  public synchronized CascadingService loadSingletonServiceFrom( Properties defaultProperties, Map<Object, Object> properties, String property )
066    {
067    String className = getStringProperty( defaultProperties, properties, property );
068
069    if( !singletons.containsKey( className ) )
070      singletons.put( className, loadServiceFrom( defaultProperties, properties, property ) );
071
072    return singletons.get( className );
073    }
074
075  public synchronized Collection<CascadingService> releaseSingletonServices()
076    {
077    try
078      {
079      return Collections.unmodifiableCollection( singletons.values() );
080      }
081    finally
082      {
083      singletons.clear();
084      }
085    }
086
087  public CascadingService loadServiceFrom( Properties defaultProperties, Map<Object, Object> properties, String property )
088    {
089    String className = getStringProperty( defaultProperties, properties, property );
090
091    CascadingService service = createProvider( className );
092
093    if( service != null )
094      service.setProperties( properties );
095
096    return service;
097    }
098  }