/*
* Copyright 2012-2020 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.boot.context.config;
import org.springframework.boot.origin.Origin;
import org.springframework.boot.origin.OriginProvider;
import org.springframework.util.StringUtils;
A user specified location that can be resolved
to one or config data resources
. A ConfigDataLocation
is a simple wrapper around a String
value. The exact format of the value will depend on the underlying technology, but is usually a URL like syntax consisting of a prefix and path. For example, crypt:somehost/somepath
. Locations can be mandatory or optional
. Optional locations are prefixed with optional:
.
Author: Phillip Webb Since: 2.4.0
/**
* A user specified location that can be {@link ConfigDataLocationResolver resolved} to
* one or {@link ConfigDataResource config data resources}. A {@link ConfigDataLocation}
* is a simple wrapper around a {@link String} value. The exact format of the value will
* depend on the underlying technology, but is usually a URL like syntax consisting of a
* prefix and path. For example, {@code crypt:somehost/somepath}.
* <p>
* Locations can be mandatory or {@link #isOptional() optional}. Optional locations are
* prefixed with {@code optional:}.
*
* @author Phillip Webb
* @since 2.4.0
*/
public final class ConfigDataLocation implements OriginProvider {
Prefix used to indicate that a ConfigDataResource
is optional. /**
* Prefix used to indicate that a {@link ConfigDataResource} is optional.
*/
public static final String OPTIONAL_PREFIX = "optional:";
private final boolean optional;
private final String value;
private final Origin origin;
private ConfigDataLocation(boolean optional, String value, Origin origin) {
this.value = value;
this.optional = optional;
this.origin = origin;
}
Return the the location is optional and should ignore ConfigDataNotFoundException
. Returns: if the location is optional
/**
* Return the the location is optional and should ignore
* {@link ConfigDataNotFoundException}.
* @return if the location is optional
*/
public boolean isOptional() {
return this.optional;
}
Return the value of the location (always excluding any user specified optional:
prefix. Returns: the location value
/**
* Return the value of the location (always excluding any user specified
* {@code optional:} prefix.
* @return the location value
*/
public String getValue() {
return this.value;
}
Return if getValue()
has the specified prefix. Params: - prefix – the prefix to check
Returns: if the value has the prefix
/**
* Return if {@link #getValue()} has the specified prefix.
* @param prefix the prefix to check
* @return if the value has the prefix
*/
public boolean hasPrefix(String prefix) {
return this.value.startsWith(prefix);
}
Return getValue()
with the specified prefix removed. If the location does not have the given prefix then the getValue()
is returned unchanged. Params: - prefix – the prefix to check
Returns: the value with the prefix removed
/**
* Return {@link #getValue()} with the specified prefix removed. If the location does
* not have the given prefix then the {@link #getValue()} is returned unchanged.
* @param prefix the prefix to check
* @return the value with the prefix removed
*/
public String getNonPrefixedValue(String prefix) {
if (hasPrefix(prefix)) {
return this.value.substring(prefix.length());
}
return this.value;
}
@Override
public Origin getOrigin() {
return this.origin;
}
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null || getClass() != obj.getClass()) {
return false;
}
ConfigDataLocation other = (ConfigDataLocation) obj;
return this.value.equals(other.value);
}
@Override
public int hashCode() {
return this.value.hashCode();
}
@Override
public String toString() {
return (!this.optional) ? this.value : OPTIONAL_PREFIX + this.value;
}
Create a new ConfigDataLocation
with a specific Origin
. Params: - origin – the origin to set
Returns: a new ConfigDataLocation
instance.
/**
* Create a new {@link ConfigDataLocation} with a specific {@link Origin}.
* @param origin the origin to set
* @return a new {@link ConfigDataLocation} instance.
*/
ConfigDataLocation withOrigin(Origin origin) {
return new ConfigDataLocation(this.optional, this.value, origin);
}
Factory method to create a new ConfigDataLocation
from a string. Params: - location – the location string
Returns: a ConfigDataLocation
instance or null
if no location was provided
/**
* Factory method to create a new {@link ConfigDataLocation} from a string.
* @param location the location string
* @return a {@link ConfigDataLocation} instance or {@code null} if no location was
* provided
*/
public static ConfigDataLocation of(String location) {
boolean optional = location != null && location.startsWith(OPTIONAL_PREFIX);
String value = (!optional) ? location : location.substring(OPTIONAL_PREFIX.length());
if (!StringUtils.hasText(value)) {
return null;
}
return new ConfigDataLocation(optional, value, null);
}
}