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

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

There is a newer version: 0.7.0-M1
Show newest version
package brooklyn.location.basic;

import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Collection;
import java.util.Map;
import java.util.Set;

import brooklyn.location.MachineLocation;
import brooklyn.location.MachineProvisioningLocation;
import brooklyn.location.OsDetails;
import brooklyn.location.PortRange;
import brooklyn.location.PortSupplier;
import brooklyn.util.collections.MutableMap;
import brooklyn.util.config.ConfigBag;

import com.google.common.base.Throwables;
import com.google.common.collect.Iterables;
import com.google.common.collect.Sets;


/** Location for use in dev/test, defining custom start/stop support, and/or tweaking the ports which are permitted to be available
 * (using setPermittedPorts(Iterable))
 */
public class SimulatedLocation extends AbstractLocation implements MachineProvisioningLocation, MachineLocation, PortSupplier {

    private static final long serialVersionUID = 1L;
    
    private static final InetAddress address;
    static {
        try {
            address = InetAddress.getLocalHost();
        } catch (UnknownHostException e) {
            throw Throwables.propagate(e);
        }
    }

    Iterable permittedPorts = PortRanges.fromString("1+");
    Set usedPorts = Sets.newLinkedHashSet();

    public SimulatedLocation() {
        this(MutableMap.of());
    }
    public SimulatedLocation(Map flags) {
        super(flags);
    }
    
    @Override
    public SimulatedLocation newSubLocation(Map newFlags) {
        return LocationCreationUtils.newSubLocation(newFlags, this);
    }

    public MachineLocation obtain(Map flags) {
        return this;
    }

    public void release(MachineLocation machine) {
    }

    public Map getProvisioningFlags(Collection tags) {
        return MutableMap.of();
    }
    
    public InetAddress getAddress() {
        return address;
    }

    public synchronized boolean obtainSpecificPort(int portNumber) {
        if (!Iterables.contains(permittedPorts, portNumber)) return false;
        if (usedPorts.contains(portNumber)) return false;
        usedPorts.add(portNumber);
        return true;
    }

    public synchronized int obtainPort(PortRange range) {
        for (int p: range)
            if (obtainSpecificPort(p)) return p;
        return -1;
    }

    public synchronized void releasePort(int portNumber) {
        usedPorts.remove(portNumber);
    }
    
    public synchronized void setPermittedPorts(Iterable ports) {
        permittedPorts  = ports;
    }

    @Override
    public OsDetails getOsDetails() {
        return BasicOsDetails.Factory.ANONYMOUS_LINUX;
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy