View Javadoc

1   package org.apache.helix.webapp;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
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        // Attach the application to the component and start it
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  }