/**
 * Launches a clustered and load-balanced set of web servers. Demonstrates syntax, so many of the
 * options used here are the defaults. (So the class could be much simpler, as in
 * WebClusterExampleAlt.)
 *
 * <p>Requires: -Xmx512m -Xms128m -XX:MaxPermSize=256m and brooklyn-all jar, and this jar or classes
 * dir, on classpath.
 */
public class WebClusterExample extends ApplicationBuilder {
  public static final Logger LOG = LoggerFactory.getLogger(WebClusterExample.class);

  static BrooklynProperties config = BrooklynProperties.Factory.newDefault();

  public static final String DEFAULT_LOCATION = "localhost";

  public static final String WAR_PATH = "classpath://hello-world-webapp.war";

  private NginxController nginxController;
  private ControlledDynamicWebAppCluster web;

  protected void doBuild() {
    nginxController =
        createChild(
            BasicEntitySpec.newInstance(NginxController.class)
                // .configure("domain", "webclusterexample.brooklyn.local")
                .configure("port", "8000+"));

    web =
        createChild(
            ControlledDynamicWebAppCluster.Spec.newInstance()
                .displayName("WebApp cluster")
                .controller(nginxController)
                .initialSize(1)
                .memberSpec(
                    BasicEntitySpec.newInstance(JBoss7Server.class)
                        .configure("httpPort", "8080+")
                        .configure("war", WAR_PATH)));

    web.getCluster()
        .addPolicy(
            AutoScalerPolicy.builder()
                .metric(DynamicWebAppCluster.AVERAGE_REQUESTS_PER_SECOND)
                .sizeRange(1, 5)
                .metricRange(10, 100)
                .build());
  }

  public static void main(String[] argv) {
    List<String> args = Lists.newArrayList(argv);
    String port = CommandLineUtil.getCommandLineOption(args, "--port", "8081+");
    String location = CommandLineUtil.getCommandLineOption(args, "--location", DEFAULT_LOCATION);

    // TODO Want to parse, to handle multiple locations
    BrooklynLauncherCli launcher =
        BrooklynLauncherCli.newInstance()
            .application(new WebClusterExample().appDisplayName("Brooklyn WebApp Cluster example"))
            .webconsolePort(port)
            .location(location)
            .start();

    Entities.dumpInfo(launcher.getApplications());
  }
}
  /**
   * Usual constructor, takes a set of properties; also (experimental) permits defining a
   * brooklynProperties source
   */
  public AbstractApplication(Map properties) {
    super(properties);

    if (properties.containsKey("mgmt")) {
      mgmt = (ManagementContext) properties.remove("mgmt");
    }

    // TODO decide whether this is the best way to inject properties like this
    Object propsSource = null;
    if (properties.containsKey("brooklynProperties")) {
      propsSource = properties.remove("brooklynProperties");
    } else if (properties.containsKey("brooklyn.properties")) {
      propsSource = properties.remove("brooklyn.properties");
    }
    if (propsSource instanceof String) {
      Properties p = new Properties();
      try {
        p.load(new ResourceUtils(this).getResourceFromUrl((String) propsSource));
      } catch (IOException e) {
        throw new IllegalArgumentException(
            "Invalid brooklyn properties source " + propsSource + ": " + e, e);
      }
      propsSource = p;
    }
    if (propsSource instanceof BrooklynProperties) {
      brooklynProperties = (BrooklynProperties) propsSource;
    } else if (propsSource instanceof Map) {
      brooklynProperties = BrooklynProperties.Factory.newEmpty().addFromMap((Map) propsSource);
    } else {
      if (propsSource != null)
        throw new IllegalArgumentException("Invalid brooklyn properties source " + propsSource);
      brooklynProperties = BrooklynProperties.Factory.newDefault();
    }

    setAttribute(SERVICE_UP, false);
    setAttribute(Attributes.SERVICE_STATE, Lifecycle.CREATED);
  }
 public Builder useDefaultProperties() {
   properties = BrooklynProperties.Factory.newDefault();
   return this;
 }
 private static JcloudsLocation resolve(String spec) {
   BrooklynProperties brooklynProperties = BrooklynProperties.Factory.newDefault();
   return (JcloudsLocation)
       new LocationRegistry(brooklynProperties).resolve(JcloudsResolver.JCLOUDS + ":" + spec);
 }