/*
 * Druid - a distributed column store.
 * Copyright (C) 2012, 2013  Metamarkets Group Inc.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */

package io.druid.server;

import com.fasterxml.jackson.annotation.JsonProperty;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;

/**
 */
@Path("/{a:status|health}")
public class StatusResource
{
  @GET
  @Produces("application/json")
  public Status doGet()
  {
    return new Status(
        StatusResource.class.getPackage().getImplementationVersion(),
        new Memory(Runtime.getRuntime())
    );
  }

  public static class Status {
    final String version;
    final Memory memory;

    public Status(String version, Memory memory)
    {
      this.version = version;
      this.memory = memory;
    }

    @JsonProperty
    public String getVersion()
    {
      return version;
    }

    @JsonProperty
    public Memory getMemory()
    {
      return memory;
    }
  }

  public static class Memory {
    final long maxMemory;
    final long totalMemory;
    final long freeMemory;
    final long usedMemory;

    public Memory(Runtime runtime) {
      maxMemory = runtime.maxMemory();
      totalMemory = runtime.totalMemory();
      freeMemory = runtime.freeMemory();
      usedMemory = totalMemory - freeMemory;
    }

    @JsonProperty
    public long getMaxMemory()
    {
      return maxMemory;
    }

    @JsonProperty
    public long getTotalMemory()
    {
      return totalMemory;
    }

    @JsonProperty
    public long getFreeMemory()
    {
      return freeMemory;
    }

    @JsonProperty
    public long getUsedMemory()
    {
      return usedMemory;
    }
  }
}

@@ -972,16 +972,101 @@
 roduces;
+%0Aimport java.io.IOException;%0Aimport java.io.InputStream;%0Aimport java.util.Properties;
 %0A%0A/**%0A *
@@ -1227,115 +1227,652 @@
     
-StatusResource.class.getPackage().getImplementationVersion(),%0A        new Memory(Runtime.getRuntime())
+getMavenVersion(%22io.druid%22,%22druid-server%22),%0A        new Memory(Runtime.getRuntime())%0A    );%0A  %7D%0A%0A  private String getMavenVersion(String groupId, String artifactId)%7B%0A%0A    Properties properties = new Properties();%0A    try %7B%0A      InputStream is = StatusResource.class.getClassLoader().getResourceAsStream(%0A          String.format(%0A              %22META-INF/maven/%25s/%25s/pom.properties%22,%0A              groupId,%0A              artifactId%0A          )%0A      );%0A      if (is == null) %7B%0A        return null;%0A      %7D%0A      properties.load(is);%0A    %7D%0A    catch (IOException e) %7B%0A//      e.printStackTrace();
 %0A    
+%7D%0A    return properties.getProperty(%22version%22
 );%0A 

version of druid-server