brooklyn.location.basic.LocalhostResolver Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of brooklyn-core Show documentation
Show all versions of brooklyn-core Show documentation
Entity implementation classes, events, and other core elements
package brooklyn.location.basic;
import static com.google.common.base.Preconditions.checkNotNull;
import java.io.File;
import java.util.Collections;
import java.util.Map;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import brooklyn.entity.basic.BrooklynConfigKeys;
import brooklyn.location.Location;
import brooklyn.location.LocationRegistry;
import brooklyn.location.LocationResolver;
import brooklyn.location.LocationSpec;
import brooklyn.management.ManagementContext;
import brooklyn.util.collections.MutableMap;
import brooklyn.util.text.KeyValueParser;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Sets;
/**
* Examples of valid specs:
*
* - localhost
*
- localhost:()
*
- localhost:(name=abc)
*
- localhost:(name="abc")
*
*
* @author alex, aled
*/
public class LocalhostResolver implements LocationResolver {
private static final Logger log = LoggerFactory.getLogger(LocalhostResolver.class);
public static final String LOCALHOST = "localhost";
private static final Pattern PATTERN = Pattern.compile("("+LOCALHOST+"|"+LOCALHOST.toUpperCase()+")" + "(:\\((.*)\\))?$");
private static final Set ACCEPTABLE_ARGS = ImmutableSet.of("name");
private ManagementContext managementContext;
@Override
public void init(ManagementContext managementContext) {
this.managementContext = checkNotNull(managementContext, "managementContext");
}
@Override
public String getPrefix() {
return LOCALHOST;
}
@Override
public Location newLocationFromString(Map properties, String spec) {
return newLocationFromString(spec, null, properties, new MutableMap());
}
@Override
public Location newLocationFromString(Map locationFlags, String spec, brooklyn.location.LocationRegistry registry) {
return newLocationFromString(spec, registry, registry.getProperties(), locationFlags);
}
protected Location newLocationFromString(String spec, brooklyn.location.LocationRegistry registry, Map properties, Map locationFlags) {
String namedLocation = (String) locationFlags.get("named");
Matcher matcher = PATTERN.matcher(spec);
if (!matcher.matches()) {
throw new IllegalArgumentException("Invalid location '"+spec+"'; must specify something like localhost or localhost(name=abc)");
}
String argsPart = matcher.group(3);
Map argsMap = (argsPart != null) ? KeyValueParser.parseMap(argsPart) : Collections.emptyMap();
String namePart = argsMap.get("name");
if (!ACCEPTABLE_ARGS.containsAll(argsMap.keySet())) {
Set illegalArgs = Sets.difference(argsMap.keySet(), ACCEPTABLE_ARGS);
throw new IllegalArgumentException("Invalid location '"+spec+"'; illegal args "+illegalArgs+"; acceptable args are "+ACCEPTABLE_ARGS);
}
if (argsMap.containsKey("name") && (namePart == null || namePart.isEmpty())) {
throw new IllegalArgumentException("Invalid location '"+spec+"'; if name supplied then value must be non-empty");
}
Map filteredProperties = new LocalhostPropertiesFromBrooklynProperties().getLocationProperties("localhost", namedLocation, properties);
MutableMap flags = MutableMap.builder().putAll(filteredProperties).putAll(locationFlags).build();
if (namePart != null) {
flags.put("name", namePart);
}
if (registry != null) {
String brooklynDataDir = (String) registry.getProperties().get(BrooklynConfigKeys.BROOKLYN_DATA_DIR.getName());
if (brooklynDataDir != null && brooklynDataDir.length() > 0) {
flags.put("localTempDir", new File(brooklynDataDir));
}
}
return managementContext.getLocationManager().createLocation(LocationSpec.create(LocalhostMachineProvisioningLocation.class)
.configure(flags));
}
@Override
public boolean accepts(String spec, LocationRegistry registry) {
return BasicLocationRegistry.isResolverPrefixForSpec(this, spec, true);
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy