[BROOKLYN-182] Don't type-switch amongst location types Duplicating functions like getSsh* with getWinrm* counterparts should be avoided. Replace with a global get*(Class) instead.
Project: http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/commit/676b48e3 Tree: http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/tree/676b48e3 Diff: http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/diff/676b48e3 Branch: refs/heads/master Commit: 676b48e35866c264a1f21dcfdbabed7f4297b993 Parents: 8fa4df7 Author: Ciprian Ciubotariu <[email protected]> Authored: Wed Oct 14 17:38:49 2015 +0300 Committer: Ciprian Ciubotariu <[email protected]> Committed: Wed Oct 14 23:36:29 2015 +0300 ---------------------------------------------------------------------- .../brooklyn/core/effector/EffectorTasks.java | 35 +++++++++++--------- .../brooklyn/core/entity/EntitySuppliers.java | 2 +- .../brooklyn/core/location/Locations.java | 2 +- .../apache/brooklyn/core/location/Machines.java | 12 ++----- .../org/apache/brooklyn/feed/ssh/SshFeed.java | 2 +- .../policy/ha/SshMachineFailureDetector.java | 2 +- .../entity/chef/ChefLifecycleEffectorTasks.java | 2 +- .../entity/machine/MachineEntityImpl.java | 2 +- .../entity/network/bind/BindDnsServerImpl.java | 4 +-- .../entity/dns/AbstractGeoDnsServiceTest.java | 2 +- .../camp/brooklyn/ByonLocationsYamlTest.java | 2 +- 11 files changed, 33 insertions(+), 34 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/676b48e3/core/src/main/java/org/apache/brooklyn/core/effector/EffectorTasks.java ---------------------------------------------------------------------- diff --git a/core/src/main/java/org/apache/brooklyn/core/effector/EffectorTasks.java b/core/src/main/java/org/apache/brooklyn/core/effector/EffectorTasks.java index e990bac..68d45a5 100644 --- a/core/src/main/java/org/apache/brooklyn/core/effector/EffectorTasks.java +++ b/core/src/main/java/org/apache/brooklyn/core/effector/EffectorTasks.java @@ -35,7 +35,7 @@ import org.apache.brooklyn.core.mgmt.internal.EffectorUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.apache.brooklyn.location.ssh.SshMachineLocation; -import org.apache.brooklyn.location.winrm.WinRmMachineLocation; +import org.apache.brooklyn.api.location.MachineLocation; import org.apache.brooklyn.util.core.config.ConfigBag; import org.apache.brooklyn.util.core.task.DynamicSequentialTask; import org.apache.brooklyn.util.core.task.DynamicTasks; @@ -199,6 +199,24 @@ public class EffectorTasks { return Reflections.cast(t, type); } + /** Finds a unique {@link MachineLocation} attached to the entity + * where this task is running + * @throws NullPointerException if {@link #findEntity()} fails + * @throws IllegalStateException if call to {@link #getSshMachine(Entity)} fails */ + public static <T extends MachineLocation> T findMachine(Class<T> clazz) { + return getMachine(findEntity(), clazz); + } + + /** Finds a unique {@link MachineLocation} attached to the supplied entity + * @throws IllegalStateException if there is not a unique such {@link SshMachineLocation} */ + public static <T extends MachineLocation> T getMachine(Entity entity, Class<T> clazz) { + try { + return Machines.findUniqueMachineLocation(entity.getLocations(), clazz).get(); + } catch (Exception e) { + throw new IllegalStateException("Entity "+entity+" (in "+Tasks.current()+") requires a single " + clazz.getName() + ", but has "+entity.getLocations(), e); + } + } + /** Finds a unique {@link SshMachineLocation} attached to the entity * where this task is running * @throws NullPointerException if {@link #findEntity()} fails @@ -210,20 +228,7 @@ public class EffectorTasks { /** Finds a unique {@link SshMachineLocation} attached to the supplied entity * @throws IllegalStateException if there is not a unique such {@link SshMachineLocation} */ public static SshMachineLocation getSshMachine(Entity entity) { - try { - return Machines.findUniqueSshMachineLocation(entity.getLocations()).get(); - } catch (Exception e) { - throw new IllegalStateException("Entity "+entity+" (in "+Tasks.current()+") requires a single SshMachineLocation, but has "+entity.getLocations(), e); - } + return getMachine(entity, SshMachineLocation.class); } - /** Finds a unique {@link WinRmMachineLocation} attached to the supplied entity - * @throws IllegalStateException if there is not a unique such {@link WinRmMachineLocation} */ - public static WinRmMachineLocation getWinRmMachine(Entity entity) { - try { - return Machines.findUniqueWinRmMachineLocation(entity.getLocations()).get(); - } catch (Exception e) { - throw new IllegalStateException("Entity "+entity+" (in "+Tasks.current()+") requires a single WinRmMachineLocation, but has "+entity.getLocations(), e); - } - } } http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/676b48e3/core/src/main/java/org/apache/brooklyn/core/entity/EntitySuppliers.java ---------------------------------------------------------------------- diff --git a/core/src/main/java/org/apache/brooklyn/core/entity/EntitySuppliers.java b/core/src/main/java/org/apache/brooklyn/core/entity/EntitySuppliers.java index 4469d24..406485a 100644 --- a/core/src/main/java/org/apache/brooklyn/core/entity/EntitySuppliers.java +++ b/core/src/main/java/org/apache/brooklyn/core/entity/EntitySuppliers.java @@ -41,7 +41,7 @@ public class EntitySuppliers { } @Override public SshMachineLocation get() { - return Machines.findUniqueSshMachineLocation(entity.getLocations()).get(); + return Machines.findUniqueMachineLocation(entity.getLocations(), SshMachineLocation.class).get(); } } } http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/676b48e3/core/src/main/java/org/apache/brooklyn/core/location/Locations.java ---------------------------------------------------------------------- diff --git a/core/src/main/java/org/apache/brooklyn/core/location/Locations.java b/core/src/main/java/org/apache/brooklyn/core/location/Locations.java index 2cae5d7..0b8a060 100644 --- a/core/src/main/java/org/apache/brooklyn/core/location/Locations.java +++ b/core/src/main/java/org/apache/brooklyn/core/location/Locations.java @@ -66,7 +66,7 @@ public class Locations { /** as {@link Machines#findUniqueSshMachineLocation(Iterable)} */ public static Maybe<SshMachineLocation> findUniqueSshMachineLocation(Iterable<? extends Location> locations) { - return Machines.findUniqueSshMachineLocation(locations); + return Machines.findUniqueMachineLocation(locations, SshMachineLocation.class); } /** if no locations are supplied, returns locations on the entity, or in the ancestors, until it finds a non-empty set, http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/676b48e3/core/src/main/java/org/apache/brooklyn/core/location/Machines.java ---------------------------------------------------------------------- diff --git a/core/src/main/java/org/apache/brooklyn/core/location/Machines.java b/core/src/main/java/org/apache/brooklyn/core/location/Machines.java index b95d42f..9ec97e7 100644 --- a/core/src/main/java/org/apache/brooklyn/core/location/Machines.java +++ b/core/src/main/java/org/apache/brooklyn/core/location/Machines.java @@ -34,8 +34,6 @@ import com.google.common.collect.Iterables; import org.apache.brooklyn.location.localhost.LocalhostMachineProvisioningLocation; import org.apache.brooklyn.location.localhost.LocalhostMachineProvisioningLocation.LocalhostMachine; -import org.apache.brooklyn.location.ssh.SshMachineLocation; -import org.apache.brooklyn.location.winrm.WinRmMachineLocation; import org.apache.brooklyn.util.guava.Maybe; import org.apache.brooklyn.util.net.HasNetworkAddresses; @@ -102,15 +100,11 @@ public class Machines { } public static Maybe<MachineLocation> findUniqueMachineLocation(Iterable<? extends Location> locations) { - return findUniqueElement(locations, MachineLocation.class); + return findUniqueMachineLocation(locations, MachineLocation.class); } - public static Maybe<SshMachineLocation> findUniqueSshMachineLocation(Iterable<? extends Location> locations) { - return findUniqueElement(locations, SshMachineLocation.class); - } - - public static Maybe<WinRmMachineLocation> findUniqueWinRmMachineLocation(Iterable<? extends Location> locations) { - return findUniqueElement(locations, WinRmMachineLocation.class); + public static <T extends MachineLocation> Maybe<T> findUniqueMachineLocation(Iterable<? extends Location> locations, Class<T> clazz) { + return findUniqueElement(locations, clazz); } public static Maybe<String> findSubnetHostname(Iterable<? extends Location> ll) { http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/676b48e3/core/src/main/java/org/apache/brooklyn/feed/ssh/SshFeed.java ---------------------------------------------------------------------- diff --git a/core/src/main/java/org/apache/brooklyn/feed/ssh/SshFeed.java b/core/src/main/java/org/apache/brooklyn/feed/ssh/SshFeed.java index 8663137..6a13528 100644 --- a/core/src/main/java/org/apache/brooklyn/feed/ssh/SshFeed.java +++ b/core/src/main/java/org/apache/brooklyn/feed/ssh/SshFeed.java @@ -202,7 +202,7 @@ public class SshFeed extends AbstractFeed { /** @deprecated since 0.7.0, use static convenience on {@link Locations} */ @Deprecated public static SshMachineLocation getMachineOfEntity(Entity entity) { - return Machines.findUniqueSshMachineLocation(entity.getLocations()).orNull(); + return Machines.findUniqueMachineLocation(entity.getLocations(), SshMachineLocation.class).orNull(); } /** http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/676b48e3/policy/src/main/java/org/apache/brooklyn/policy/ha/SshMachineFailureDetector.java ---------------------------------------------------------------------- diff --git a/policy/src/main/java/org/apache/brooklyn/policy/ha/SshMachineFailureDetector.java b/policy/src/main/java/org/apache/brooklyn/policy/ha/SshMachineFailureDetector.java index e5da346..bd5738b 100644 --- a/policy/src/main/java/org/apache/brooklyn/policy/ha/SshMachineFailureDetector.java +++ b/policy/src/main/java/org/apache/brooklyn/policy/ha/SshMachineFailureDetector.java @@ -67,7 +67,7 @@ public class SshMachineFailureDetector extends AbstractFailureDetector { @Override protected CalculatedStatus calculateStatus() { - Maybe<SshMachineLocation> sshMachineOption = Machines.findUniqueSshMachineLocation(entity.getLocations()); + Maybe<SshMachineLocation> sshMachineOption = Machines.findUniqueMachineLocation(entity.getLocations(), SshMachineLocation.class); if (sshMachineOption.isPresent()) { SshMachineLocation sshMachine = sshMachineOption.get(); try { http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/676b48e3/software/base/src/main/java/org/apache/brooklyn/entity/chef/ChefLifecycleEffectorTasks.java ---------------------------------------------------------------------- diff --git a/software/base/src/main/java/org/apache/brooklyn/entity/chef/ChefLifecycleEffectorTasks.java b/software/base/src/main/java/org/apache/brooklyn/entity/chef/ChefLifecycleEffectorTasks.java index 57c54bf..e58c4ab 100644 --- a/software/base/src/main/java/org/apache/brooklyn/entity/chef/ChefLifecycleEffectorTasks.java +++ b/software/base/src/main/java/org/apache/brooklyn/entity/chef/ChefLifecycleEffectorTasks.java @@ -159,7 +159,7 @@ public class ChefLifecycleEffectorTasks extends MachineLifecycleEffectorTasks im @SuppressWarnings({ "unchecked", "deprecation" }) protected void startWithChefSoloAsync() { - String baseDir = MachineLifecycleEffectorTasks.resolveOnBoxDir(entity(), Machines.findUniqueSshMachineLocation(entity().getLocations()).get()); + String baseDir = MachineLifecycleEffectorTasks.resolveOnBoxDir(entity(), Machines.findUniqueMachineLocation(entity().getLocations(), SshMachineLocation.class).get()); String installDir = Urls.mergePaths(baseDir, "installs/chef"); @SuppressWarnings("rawtypes") http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/676b48e3/software/base/src/main/java/org/apache/brooklyn/entity/machine/MachineEntityImpl.java ---------------------------------------------------------------------- diff --git a/software/base/src/main/java/org/apache/brooklyn/entity/machine/MachineEntityImpl.java b/software/base/src/main/java/org/apache/brooklyn/entity/machine/MachineEntityImpl.java index 58c64fd..881dd7b 100644 --- a/software/base/src/main/java/org/apache/brooklyn/entity/machine/MachineEntityImpl.java +++ b/software/base/src/main/java/org/apache/brooklyn/entity/machine/MachineEntityImpl.java @@ -146,7 +146,7 @@ public class MachineEntityImpl extends EmptySoftwareProcessImpl implements Machi } public SshMachineLocation getMachine() { - return Machines.findUniqueSshMachineLocation(getLocations()).get(); + return Machines.findUniqueMachineLocation(getLocations(), SshMachineLocation.class).get(); } @Override http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/676b48e3/software/network/src/main/java/org/apache/brooklyn/entity/network/bind/BindDnsServerImpl.java ---------------------------------------------------------------------- diff --git a/software/network/src/main/java/org/apache/brooklyn/entity/network/bind/BindDnsServerImpl.java b/software/network/src/main/java/org/apache/brooklyn/entity/network/bind/BindDnsServerImpl.java index 120486a..2ee89e2 100644 --- a/software/network/src/main/java/org/apache/brooklyn/entity/network/bind/BindDnsServerImpl.java +++ b/software/network/src/main/java/org/apache/brooklyn/entity/network/bind/BindDnsServerImpl.java @@ -224,7 +224,7 @@ public class BindDnsServerImpl extends SoftwareProcessImpl implements BindDnsSer for (Map.Entry<String, Entity> e : hostnameToEntity.entries()) { String domainName = e.getKey(); - Maybe<SshMachineLocation> location = Machines.findUniqueSshMachineLocation(e.getValue().getLocations()); + Maybe<SshMachineLocation> location = Machines.findUniqueMachineLocation(e.getValue().getLocations(), SshMachineLocation.class); if (!location.isPresent()) { LOG.debug("Member {} of {} does not have an SSH location so will not be configured", e.getValue(), this); continue; @@ -255,7 +255,7 @@ public class BindDnsServerImpl extends SoftwareProcessImpl implements BindDnsSer } protected void configureResolver(Entity entity) { - Maybe<SshMachineLocation> machine = Machines.findUniqueSshMachineLocation(entity.getLocations()); + Maybe<SshMachineLocation> machine = Machines.findUniqueMachineLocation(entity.getLocations(), SshMachineLocation.class); if (machine.isPresent()) { if (getConfig(REPLACE_RESOLV_CONF)) { machine.get().copyTo(new StringReader(getConfig(RESOLV_CONF_TEMPLATE)), "/etc/resolv.conf"); http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/676b48e3/software/webapp/src/test/java/org/apache/brooklyn/entity/dns/AbstractGeoDnsServiceTest.java ---------------------------------------------------------------------- diff --git a/software/webapp/src/test/java/org/apache/brooklyn/entity/dns/AbstractGeoDnsServiceTest.java b/software/webapp/src/test/java/org/apache/brooklyn/entity/dns/AbstractGeoDnsServiceTest.java index e8aa73f..5ce2c1b 100644 --- a/software/webapp/src/test/java/org/apache/brooklyn/entity/dns/AbstractGeoDnsServiceTest.java +++ b/software/webapp/src/test/java/org/apache/brooklyn/entity/dns/AbstractGeoDnsServiceTest.java @@ -220,7 +220,7 @@ public class AbstractGeoDnsServiceTest { if (includeServiceUp) ((EntityInternal)e).sensors().set(Attributes.SERVICE_UP, true); - SshMachineLocation l = Machines.findUniqueSshMachineLocation(e.getLocations()).get(); + SshMachineLocation l = Machines.findUniqueMachineLocation(e.getLocations(), SshMachineLocation.class).get(); if (includeAddress) ((EntityInternal)e).sensors().set(Attributes.ADDRESS, l.getAddress().getHostAddress()); String h = (String) l.config().getBag().getStringKey("hostname"); http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/676b48e3/usage/camp/src/test/java/org/apache/brooklyn/camp/brooklyn/ByonLocationsYamlTest.java ---------------------------------------------------------------------- diff --git a/usage/camp/src/test/java/org/apache/brooklyn/camp/brooklyn/ByonLocationsYamlTest.java b/usage/camp/src/test/java/org/apache/brooklyn/camp/brooklyn/ByonLocationsYamlTest.java index 42db9ec..ff89c25 100644 --- a/usage/camp/src/test/java/org/apache/brooklyn/camp/brooklyn/ByonLocationsYamlTest.java +++ b/usage/camp/src/test/java/org/apache/brooklyn/camp/brooklyn/ByonLocationsYamlTest.java @@ -244,7 +244,7 @@ public class ByonLocationsYamlTest extends AbstractYamlTest { FixedListMachineProvisioningLocation<MachineLocation> loc = (FixedListMachineProvisioningLocation<MachineLocation>) Iterables.get(app.getLocations(), 0); // Machine should have been given the inbound-ports - SshMachineLocation machine = Machines.findUniqueSshMachineLocation(entity.getLocations()).get(); + SshMachineLocation machine = Machines.findUniqueMachineLocation(entity.getLocations(), SshMachineLocation.class).get(); Asserts.assertEqualsIgnoringOrder((Iterable<?>)machine.config().get(CloudLocationConfig.INBOUND_PORTS), ImmutableList.of(22, 1024)); // Stop the entity; should release the machine
