title: "Correct" way to lookup Cloud Service Configurations from Java in AEM 6
body: <p>AEM's Cloud Services API isn't terribly well documented, and provides a number of somewhat awkward to use methods that can be used to find a given <code>Configuration</code> from a resource. Below are a few examples I've found "in the wild" that do this.</p>
  
  <p><strong>Method 1:</strong> Getting all <code>Configurations</code> and iterating them until a named config is found:</p>
  
  <pre><code>public static Configuration findConfiguration(ConfigurationManager configurationManager, Resource resource, String serviceName) {
      Iterator configurations = configurationManager.getConfigurations(resource);
      while (configurations.hasNext()) {
          Configuration configuration = (Configuration) configurations.next();
          if (serviceName.equals(configuration.get("cq:cloudservicename", ""))) {
              return configuration;
          }
      }
      return null;
  }
  </code></pre>
  
  <p><strong>Method 2:</strong> Lookup all the <code>cq:cloudserviceconfigs</code> that apply to the resource and ask for the one you want:</p>
  
  <pre><code>public static Configuration findConfiguration(ConfigurationManager configurationManager, Resource resource, String serviceName) {
      HierarchyNodeInheritanceValueMap pageProperties = 
          new HierarchyNodeInheritanceValueMap(resource);
  
      String[] allServices = 
          pageProperties.getInherited("cq:cloudserviceconfigs", new String[0]);
  
      return configurationManager.getConfiguration(serviceName, allServices);
  }
  </code></pre>
  
  <p><strong>Method 3:</strong> Similar to method 2, but looks like it'll only work if the requested config is also defined on the first parent that has a <code>cq:cloudserviceconfigs</code> property? </p>
  
  <pre><code>public static Configuration findConfiguration(ConfigurationManager configurationManager, Resource resource, String serviceName) {
  
      Resource configurationResource = 
          configurationManager.getConfigurationResource(resource);
  
      if (configurationResource != null) {
          String[] cloudServiceConfigs = configurationResource.adaptTo(Page.class)
              .getProperties().get("cq:cloudserviceconfigs", String[].class);
  
          if (cloudServiceConfigs != null &amp;&amp; cloudServiceConfigs.length &gt; 0) {
              return configurationManager.getConfiguration(serviceName, cloudServiceConfigs);
          }
      }
      return null;
  }
  </code></pre>
  
  <p>I find it odd that whoever designed this interface didn't simply add a method like <code>Configuration ConfigurationManager#getService(Resource resource, String serviceName)</code> - the hoops you have to jump through to achieve what seems like a common use case are frustrating, but that's how it is apparently.</p>
  
  <p>So my question is this:</p>
  
  <ol>
  <li><p>Is there a better API or library to use than <code>ConfigurationManager</code> for looking up a specific cloud service config for a page or resource?</p></li>
  <li><p>If not, what is good, robust, performant way of using <code>ConfigurationManager</code> to do this?</p></li>
  </ol>
  
  <p>Thanks.</p>