Skip to content

Instantly share code, notes, and snippets.

@Ikhiloya
Created May 13, 2020 18:38
Show Gist options
  • Select an option

  • Save Ikhiloya/a82e509e10d062303d691bbb9e7d042d to your computer and use it in GitHub Desktop.

Select an option

Save Ikhiloya/a82e509e10d062303d691bbb9e7d042d to your computer and use it in GitHub Desktop.
Factory that creates the various strategy
@Service
public class StorageFactory {
private final Logger log = LoggerFactory.getLogger(StorageFactory.class);
private final Environment environment;
private final AwsStorageStrategy awsStorageStrategy;
private final FileStorageStrategy fileStorageStrategy;
public StorageFactory(Environment environment, AwsStorageStrategy awsStorageStrategy, FileStorageStrategy fileStorageStrategy) {
this.environment = environment;
this.awsStorageStrategy = awsStorageStrategy;
this.fileStorageStrategy = fileStorageStrategy;
}
public StorageStrategy createStrategy() {
String[] activeProfiles = environment.getActiveProfiles();
log.info("Active profiles '{}'", Arrays.toString(activeProfiles));
if (Arrays.stream(environment.getActiveProfiles()).anyMatch(
env -> (env.equalsIgnoreCase(Constant.DEV_PROFILE)))) {
return this.fileStorageStrategy;
} else if (Arrays.stream(environment.getActiveProfiles()).anyMatch(
env -> (env.equalsIgnoreCase(Constant.PROD_PROFILE)))) {
return this.awsStorageStrategy;
} else {
return this.fileStorageStrategy;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment