protected void connectSensors() {
    setAttribute(
        DATASTORE_URL,
        String.format(
            "postgresql://%s:%s/", getAttribute(HOSTNAME), getAttribute(POSTGRESQL_PORT)));

    Maybe<SshMachineLocation> machine = Locations.findUniqueSshMachineLocation(getLocations());

    if (machine.isPresent()) {
      feed =
          SshFeed.builder()
              .entity(this)
              .machine(machine.get())
              .poll(
                  new SshPollConfig<Boolean>(SERVICE_UP)
                      .command("ps -ef | grep [p]ostgres")
                      .setOnSuccess(true)
                      .setOnFailureOrException(false))
              .build();
    } else {
      LOG.warn(
          "Location(s) {} not an ssh-machine location, so not polling for status; setting serviceUp immediately",
          getLocations());
    }
  }
  @SuppressWarnings("unchecked")
  public static <T> Maybe<Class<T>> tryLoadFromClasspath(String typeName, ManagementContext mgmt) {
    Class<T> clazz;
    try {
      clazz = (Class<T>) mgmt.getCatalog().getRootClassLoader().loadClass(typeName);
    } catch (ClassNotFoundException e) {
      LOG.debug("Class {} not found on classpath", typeName);
      return Maybe.absent(new Throwable("Could not find " + typeName + " on classpath"));
    }

    return Maybe.<Class<T>>of(clazz);
  }
 @SuppressWarnings("unchecked")
 public static <T extends Entity> Maybe<Class<T>> tryLoadEntityFromCatalogue(
     String entityTypeName, ManagementContext mgmt) {
   try {
     return (Maybe<Class<T>>)
         (Maybe<?>)
             Maybe.<Class<? extends Entity>>of(
                 mgmt.getCatalog().loadClassByType(entityTypeName, Entity.class));
   } catch (NoSuchElementException e) {
     LOG.debug("Class {} not found in catalogue classpath", entityTypeName);
     return Maybe.absent();
   }
 }
  public static HostAndPort getBrooklynAccessibleAddress(Entity entity, int port) {
    String host;

    // look up port forwarding
    PortForwardManager pfw = entity.getConfig(PORT_FORWARDING_MANAGER);
    if (pfw != null) {
      Collection<Location> ll = entity.getLocations();
      Maybe<SupportsPortForwarding> machine =
          Machines.findUniqueElement(ll, SupportsPortForwarding.class);
      if (machine.isPresent()) {
        synchronized (BrooklynAccessUtils.class) {
          // TODO finer-grained synchronization

          HostAndPort hp = pfw.lookup((MachineLocation) machine.get(), port);
          if (hp != null) return hp;

          Location l = (Location) machine.get();
          if (l instanceof SupportsPortForwarding) {
            Cidr source = entity.getConfig(MANAGEMENT_ACCESS_CIDR);
            if (source != null) {
              log.debug(
                  "BrooklynAccessUtils requesting new port-forwarding rule to access "
                      + port
                      + " on "
                      + entity
                      + " (at "
                      + l
                      + ", enabled for "
                      + source
                      + ")");
              // TODO discuss, is this the best way to do it
              // (will probably _create_ the port forwarding rule!)
              hp = ((SupportsPortForwarding) l).getSocketEndpointFor(source, port);
              if (hp != null) return hp;
            } else {
              log.warn(
                  "No "
                      + MANAGEMENT_ACCESS_CIDR.getName()
                      + " configured for "
                      + entity
                      + ", so cannot forward port "
                      + port
                      + " "
                      + "even though "
                      + PORT_FORWARDING_MANAGER.getName()
                      + " was supplied");
            }
          }
        }
      }
    }

    host = entity.getAttribute(Attributes.HOSTNAME);
    if (host != null) return HostAndPort.fromParts(host, port);

    throw new IllegalStateException(
        "Cannot find way to access port "
            + port
            + " on "
            + entity
            + " from Brooklyn (no host.name)");
  }
 /** Returns the CAMP platform associated with a management context, if there is one. */
 public static Maybe<CampPlatform> getCampPlatform(ManagementContext mgmt) {
   CampPlatform result = mgmt.getConfig().getConfig(BrooklynServerConfig.CAMP_PLATFORM);
   if (result != null) return Maybe.of(result);
   return Maybe.absent("No CAMP Platform is registered with this Brooklyn management context.");
 }
 @Override
 public synchronized Maybe<OsgiManager> getOsgiManager() {
   if (!isRunning()) throw new IllegalStateException("Management context no longer running");
   return Maybe.of(osgiManager);
 }