001/* 002 * (C) Copyright 2018 Nuxeo (http://nuxeo.com/) and others. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 * 016 * Contributors: 017 * Florent Guillaume 018 */ 019package org.nuxeo.runtime.aws; 020 021import static org.apache.commons.lang3.StringUtils.defaultString; 022 023import org.nuxeo.runtime.api.Framework; 024 025import com.amazonaws.SdkClientException; 026import com.amazonaws.regions.AwsRegionProvider; 027import com.amazonaws.regions.DefaultAwsRegionProviderChain; 028 029/** 030 * AWS Region Provider that uses Nuxeo configuration, or uses the default AWS chain as a fallback. 031 * 032 * @since 10.3 033 */ 034public class NuxeoAWSRegionProvider extends AwsRegionProvider { 035 036 protected static final AwsRegionProvider INSTANCE = new NuxeoAWSRegionProvider(); 037 038 protected static final AwsRegionProvider DEFAULT = new DefaultAwsRegionProviderChain(); 039 040 protected static final String DEFAULT_REGION = "us-east-1"; 041 042 protected final String id; 043 044 /** 045 * Gets a Nuxeo AWS Region Provider for the default configuration. 046 */ 047 public static AwsRegionProvider getInstance() { 048 return INSTANCE; 049 } 050 051 /** 052 * Creates a new Nuxeo AWS Region Provider for the default configuration. 053 */ 054 public NuxeoAWSRegionProvider() { 055 this(null); 056 } 057 058 /** 059 * Creates a new Nuxeo AWS Region Provider for the given configuration. 060 * 061 * @param id the configuration id, or {@code null} for the default 062 * @since 11.1 063 */ 064 public NuxeoAWSRegionProvider(String id) { 065 this.id = id; 066 } 067 068 @Override 069 public String getRegion() { 070 AWSConfigurationService service = Framework.getService(AWSConfigurationService.class); 071 if (service != null) { 072 String region = service.getAWSRegion(id); 073 if (region != null) { 074 return region; 075 } 076 } 077 String region; 078 try { 079 region = DEFAULT.getRegion(); 080 } catch (SdkClientException e) { 081 // the DefaultAwsRegionProviderChain throws when there's no provider instead of defaulting to null 082 region = null; 083 } 084 return defaultString(region, DEFAULT_REGION); 085 } 086 087}