1 package org.apache.helix.webapp;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import org.apache.helix.manager.zk.ZNRecordSerializer;
23 import org.apache.helix.manager.zk.ZkClient;
24 import org.apache.log4j.Logger;
25 import org.restlet.Component;
26 import org.restlet.Context;
27 import org.restlet.data.Protocol;
28
29
30 public class HelixAdminWebApp
31 {
32 public final Logger LOG = Logger.getLogger(HelixAdminWebApp.class);
33 RestAdminApplication _adminApp = null;
34 Component _component = null;
35
36 int _helixAdminPort;
37 String _zkServerAddress;
38 ZkClient _zkClient;
39
40 public HelixAdminWebApp(String zkServerAddress, int adminPort)
41 {
42 _zkServerAddress = zkServerAddress;
43 _helixAdminPort = adminPort;
44 }
45
46 public synchronized void start() throws Exception
47 {
48 LOG.info("helixAdminWebApp starting");
49 if(_component == null)
50 {
51 _zkClient = new ZkClient(_zkServerAddress, ZkClient.DEFAULT_SESSION_TIMEOUT, ZkClient.DEFAULT_CONNECTION_TIMEOUT, new ZNRecordSerializer());
52 _component = new Component();
53 _component.getServers().add(Protocol.HTTP, _helixAdminPort);
54 Context applicationContext = _component.getContext().createChildContext();
55 applicationContext.getAttributes().put(RestAdminApplication.ZKSERVERADDRESS, _zkServerAddress);
56 applicationContext.getAttributes().put(RestAdminApplication.PORT, ""+_helixAdminPort);
57 applicationContext.getAttributes().put(RestAdminApplication.ZKCLIENT, _zkClient);
58 _adminApp = new RestAdminApplication(applicationContext);
59
60 _component.getDefaultHost().attach(_adminApp);
61 _component.start();
62 }
63 LOG.info("helixAdminWebApp started on port " + _helixAdminPort);
64 }
65
66 public synchronized void stop()
67 {
68 try
69 {
70 _component.stop();
71 }
72 catch(Exception e)
73 {
74 LOG.error("", e);
75 }
76 finally
77 {
78 if(_zkClient != null)
79 {
80 _zkClient.close();
81 }
82 }
83 }
84 }