All Downloads are FREE. Search and download functionalities are using the official Maven repository.

brooklyn.location.basic.LocalhostResolver Maven / Gradle / Ivy

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.config.ConfigKey;
import brooklyn.config.ConfigUtils;
import brooklyn.entity.basic.ConfigKeys;
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.Maps;
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 static final ConfigKey[] KEYS = new ConfigKey[] { LocationConfigKeys.PRIVATE_KEY_FILE, LocationConfigKeys.PUBLIC_KEY_FILE, LocationConfigKeys.PRIVATE_KEY_DATA, LocationConfigKeys.PUBLIC_KEY_DATA, LocationConfigKeys.PRIVATE_KEY_PASSPHRASE }; private static final Map DEPRECATED_KEYS_MAPPING; static { @SuppressWarnings("deprecation") DeprecatedKeysMappingBuilder builder = new DeprecatedKeysMappingBuilder(log); for (ConfigKey key : KEYS) { builder.camelToHyphen(key); } DEPRECATED_KEYS_MAPPING = builder.build(); } 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) { 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 = ConfigUtils.filterForPrefixAndStrip(properties, "brooklyn.localhost.").asMapWithStringKeys(); MutableMap allFilteredProperties = MutableMap.builder().putAll(filteredProperties).putAll(locationFlags).build(); MutableMap flags = new MutableMap(); flags.putAll(transformDeprecated(allFilteredProperties)); if (namePart != null) { flags.put("name", namePart); } if (registry != null) { String brooklynDataDir = (String) registry.getProperties().get(ConfigKeys.BROOKLYN_DATA_DIR.getName()); if (brooklynDataDir != null && brooklynDataDir.length() > 0) { flags.put("localTempDir", new File(brooklynDataDir)); } } return managementContext.getLocationManager().createLocation(LocationSpec.spec(LocalhostMachineProvisioningLocation.class) .configure(flags)); } private static Map transformDeprecated(Map properties) { Map result = Maps.newLinkedHashMap(); for (Map.Entry entry : properties.entrySet()) { String key = entry.getKey(); Object value = entry.getValue(); if (DEPRECATED_KEYS_MAPPING.containsKey(key)) { String transformedKey = DEPRECATED_KEYS_MAPPING.get(key); log.warn("Deprecated key {}, transformed to {}; will not be supported in future versions", new Object[] {key, transformedKey}); result.put(transformedKey, value); } else { result.put(key, value); } } return result; } @Override public boolean accepts(String spec, LocationRegistry registry) { return BasicLocationRegistry.isResolverPrefixForSpec(this, spec, true); } }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy