package org.cloudfoundry.services; import java.io.IOException; import java.io.PrintWriter; import java.util.ArrayList; import java.util.List; import java.util.Map; import javax.servlet.http.HttpServletResponse; import javax.sql.DataSource; import org.apache.tomcat.dbcp.dbcp.BasicDataSource; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.amqp.rabbit.connection.ConnectionFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.keyvalue.redis.connection.RedisConnectionFactory; import org.springframework.data.keyvalue.redis.connection.jedis.JedisConnectionFactory; import org.springframework.jdbc.datasource.SimpleDriverDataSource; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import com.mongodb.Mongo; /** * Handles requests for the application home page. */ @Controller public class HomeController { private static final Logger logger = LoggerFactory.getLogger(HomeController.class); @Autowired(required=false) DataSource dataSource; @Autowired(required=false) ConnectionFactory rabbitConnectionFactory; @Autowired(required=false) RedisConnectionFactory redisConnectionFactory; @Autowired(required=false) Mongo mongo; /** * Simply selects the home view to render by returning its name. */ @RequestMapping(value="/", method=RequestMethod.GET) public String home(Model model) { List<String> services = new ArrayList<String>(); if (dataSource instanceof BasicDataSource) { services.add("Data Source: " + ((BasicDataSource) dataSource).getUrl()); } else if (dataSource instanceof SimpleDriverDataSource) { services.add("Data Source: " + ((SimpleDriverDataSource) dataSource).getUrl()); } if (rabbitConnectionFactory != null) { services.add("Rabbit: " + rabbitConnectionFactory); } if (redisConnectionFactory != null) { services.add("Redis: " + ((JedisConnectionFactory) redisConnectionFactory).getHostName() + ":" + ((JedisConnectionFactory) redisConnectionFactory).getPort()); } if (mongo != null) { services.add("Mongo: " + mongo.getAddress()); } model.addAttribute("services", services); return "home"; } @RequestMapping("/env") public void env(HttpServletResponse response) throws IOException { response.setContentType("text/plain"); PrintWriter out = response.getWriter(); out.println("System Properties:"); for (Map.Entry<Object, Object> property : System.getProperties().entrySet()) { out.println(property.getKey() + ": " + property.getValue()); } out.println(); out.println("System Environment:"); for (Map.Entry<String, String> envvar : System.getenv().entrySet()) { out.println(envvar.getKey() + ": " + envvar.getValue()); } } }