1 package com.weinuts.controller;
2
3 import java.util.ArrayList;
4 import java.util.List;
5
6 import org.apache.camel.CamelContext;
7 import org.apache.camel.Route;
8 import org.slf4j.Logger;
9 import org.slf4j.LoggerFactory;
10 import org.springframework.beans.factory.annotation.Autowired;
11 import org.springframework.beans.factory.annotation.Qualifier;
12 import org.springframework.stereotype.Controller;
13 import org.springframework.ui.Model;
14 import org.springframework.web.bind.annotation.PathVariable;
15 import org.springframework.web.bind.annotation.RequestMapping;
16 import org.springframework.web.bind.annotation.RequestMethod;
17
18 import com.weinuts.to.RouteStatus;
19
20 /**
21 * Web service to manage Camel routes.
22 * It provides list of routes with current sataus and abbiliti to start or stop them.
23 *
24 * @author ke.zhang
25 * @since Mar 03, 2016
26 */
27 @Controller
28 public class IntegrationLayerRouteController {
29
30 private static final Logger LOGGER = LoggerFactory.getLogger(IntegrationLayerRouteController.class);
31
32 @Autowired
33 @Qualifier("integrationLayer")
34 private CamelContext camelContext;
35
36 @RequestMapping(value = "/", method = RequestMethod.GET)
37 public String index(Model model) {
38 return dashboard(model);
39 }
40
41 @RequestMapping(value = "/dashboard", method = RequestMethod.GET)
42 public String dashboard(Model model) {
43
44 if(LOGGER.isDebugEnabled()) {
45 LOGGER.debug("camel context is suspended : " + camelContext.isSuspended());
46 }
47
48 List<Route> routes = camelContext.getRoutes();
49 List<RouteStatus> routeStatuses = new ArrayList<RouteStatus>();
50 for (Route route : routes) {
51 RouteStatus rs = new RouteStatus();
52 rs.setId(route.getId());
53 rs.setServiceStatus(camelContext.getRouteStatus(route.getId()));
54 routeStatuses.add(rs);
55 }
56
57 model.addAttribute("routeStatuses", routeStatuses);
58
59 return "dashboard";
60 }
61
62 @RequestMapping(value = "/dashboard/{routeId}/start", method = RequestMethod.GET)
63 public String startRoute(@PathVariable String routeId) {
64 try {
65 camelContext.startRoute(routeId);
66
67 LOGGER.info("camel context is starting route [" + routeId + "]");
68 } catch (Exception e) {
69 LOGGER.error("failed to start camel context [" + camelContext + "]", e);
70 }
71 return "redirect:/dashboard";
72 }
73
74 @RequestMapping(value = "/dashboard/{routeId}/stop", method = RequestMethod.GET)
75 public String stopRoute(@PathVariable String routeId) {
76 try {
77 camelContext.stopRoute(routeId);
78
79 LOGGER.info("camel context is stopping route [" + routeId + "]");
80 } catch (Exception e) {
81 LOGGER.error("failed to stop camel context [" + camelContext + "]", e);
82 }
83 return "redirect:/dashboard";
84 }
85
86 }