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.commons.cli.CommandLine;
23  import org.apache.commons.cli.CommandLineParser;
24  import org.apache.commons.cli.GnuParser;
25  import org.apache.commons.cli.HelpFormatter;
26  import org.apache.commons.cli.Option;
27  import org.apache.commons.cli.OptionBuilder;
28  import org.apache.commons.cli.Options;
29  import org.apache.commons.cli.ParseException;
30  import org.apache.helix.webapp.resources.ClusterResource;
31  import org.apache.helix.webapp.resources.ClustersResource;
32  import org.apache.helix.webapp.resources.ConfigResource;
33  import org.apache.helix.webapp.resources.ConstraintResource;
34  import org.apache.helix.webapp.resources.ControllerResource;
35  import org.apache.helix.webapp.resources.ControllerStatusUpdateResource;
36  import org.apache.helix.webapp.resources.CurrentStateResource;
37  import org.apache.helix.webapp.resources.CurrentStatesResource;
38  import org.apache.helix.webapp.resources.ErrorResource;
39  import org.apache.helix.webapp.resources.ErrorsResource;
40  import org.apache.helix.webapp.resources.ExternalViewResource;
41  import org.apache.helix.webapp.resources.IdealStateResource;
42  import org.apache.helix.webapp.resources.InstanceResource;
43  import org.apache.helix.webapp.resources.InstancesResource;
44  import org.apache.helix.webapp.resources.ResourceGroupResource;
45  import org.apache.helix.webapp.resources.ResourceGroupsResource;
46  import org.apache.helix.webapp.resources.SchedulerTasksResource;
47  import org.apache.helix.webapp.resources.StateModelResource;
48  import org.apache.helix.webapp.resources.StateModelsResource;
49  import org.apache.helix.webapp.resources.StatusUpdateResource;
50  import org.apache.helix.webapp.resources.StatusUpdatesResource;
51  import org.apache.helix.webapp.resources.ZkChildResource;
52  import org.apache.helix.webapp.resources.ZkPathResource;
53  import org.restlet.Application;
54  import org.restlet.Context;
55  import org.restlet.Restlet;
56  import org.restlet.Router;
57  import org.restlet.data.MediaType;
58  import org.restlet.data.Request;
59  import org.restlet.data.Response;
60  import org.restlet.resource.StringRepresentation;
61  import org.restlet.util.Template;
62  
63  
64  public class RestAdminApplication extends Application
65  {
66    public static final String HELP = "help";
67    public static final String ZKSERVERADDRESS = "zkSvr";
68    public static final String PORT = "port";
69    public static final String ZKCLIENT = "zkClient";
70    public static final int DEFAULT_PORT = 8100;
71    
72  
73    public RestAdminApplication()
74    {
75      super();
76    }
77  
78    public RestAdminApplication(Context context)
79    {
80      super(context);
81    }
82    
83    @Override
84    public Restlet createRoot()
85    {
86      Router router = new Router(getContext());
87      router.setDefaultMatchingMode(Template.MODE_EQUALS);
88      router.attach("/clusters", ClustersResource.class);
89      router.attach("/clusters/{clusterName}", ClusterResource.class);
90      router.attach("/clusters/{clusterName}/resourceGroups", ResourceGroupsResource.class);
91      router.attach("/clusters/{clusterName}/resourceGroups/{resourceName}", ResourceGroupResource.class);
92      router.attach("/clusters/{clusterName}/instances", InstancesResource.class);
93      router.attach("/clusters/{clusterName}/instances/{instanceName}", InstanceResource.class);
94      router.attach("/clusters/{clusterName}/instances/{instanceName}/currentState/{resourceName}", CurrentStateResource.class);
95      router.attach("/clusters/{clusterName}/instances/{instanceName}/statusUpdate/{resourceName}", StatusUpdateResource.class);
96      router.attach("/clusters/{clusterName}/instances/{instanceName}/errors/{resourceName}", ErrorResource.class);
97      router.attach("/clusters/{clusterName}/instances/{instanceName}/currentState", CurrentStatesResource.class);
98      router.attach("/clusters/{clusterName}/instances/{instanceName}/statusUpdate", StatusUpdatesResource.class);
99      router.attach("/clusters/{clusterName}/instances/{instanceName}/errors", ErrorsResource.class);
100     router.attach("/clusters/{clusterName}/resourceGroups/{resourceName}/idealState", IdealStateResource.class);
101     router.attach("/clusters/{clusterName}/resourceGroups/{resourceName}/externalView", ExternalViewResource.class);
102     router.attach("/clusters/{clusterName}/StateModelDefs/{modelName}", StateModelResource.class);
103     router.attach("/clusters/{clusterName}/StateModelDefs", StateModelsResource.class);
104     router.attach("/clusters/{clusterName}/SchedulerTasks", SchedulerTasksResource.class);
105     router.attach("/clusters/{clusterName}/Controller", ControllerResource.class);
106     router.attach("/clusters/{clusterName}/Controller/statusUpdates/{MessageType}/{MessageId}", ControllerStatusUpdateResource.class);
107     router.attach("/clusters/{clusterName}/configs", ConfigResource.class);
108     router.attach("/clusters/{clusterName}/configs/{scope}", ConfigResource.class);
109     router.attach("/clusters/{clusterName}/configs/{scope}/{scopeKey1}", ConfigResource.class);
110     router.attach("/clusters/{clusterName}/configs/{scope}/{scopeKey1}/{scopeKey2}", ConfigResource.class);
111     router.attach("/clusters/{clusterName}/constraints/{constraintType}", ConstraintResource.class);
112     router.attach("/clusters/{clusterName}/constraints/{constraintType}/{constraintId}", ConstraintResource.class);
113     router.attach("/zkPath", ZkPathResource.class).setMatchingMode(Template.MODE_STARTS_WITH);
114     router.attach("/zkChild", ZkChildResource.class).setMatchingMode(Template.MODE_STARTS_WITH);
115 
116     Restlet mainpage = new Restlet()
117     {
118       @Override
119       public void handle(Request request, Response response)
120       {
121         StringBuilder stringBuilder = new StringBuilder();
122         stringBuilder.append("<html>");
123         stringBuilder
124             .append("<head><title>Restlet Cluster Management page</title></head>");
125         stringBuilder.append("<body bgcolor=white>");
126         stringBuilder.append("<table border=\"0\">");
127         stringBuilder.append("<tr>");
128         stringBuilder.append("<td>");
129         stringBuilder.append("<h1>Rest cluster management interface V1</h1>");
130         stringBuilder.append("</td>");
131         stringBuilder.append("</tr>");
132         stringBuilder.append("</table>");
133         stringBuilder.append("</body>");
134         stringBuilder.append("</html>");
135         response.setEntity(new StringRepresentation(stringBuilder.toString(),
136             MediaType.TEXT_HTML));
137       }
138     };
139     router.attach("", mainpage);
140     return router;
141   }
142 
143   public static void printUsage(Options cliOptions)
144   {
145     HelpFormatter helpFormatter = new HelpFormatter();
146     helpFormatter.printHelp("java " + RestAdminApplication.class.getName(), cliOptions);
147   }
148 
149   @SuppressWarnings("static-access")
150   private static Options constructCommandLineOptions()
151   {
152     Option helpOption = OptionBuilder.withLongOpt(HELP)
153         .withDescription("Prints command-line options info").create();
154     helpOption.setArgs(0);
155     helpOption.setRequired(false);
156     helpOption.setArgName("print help message");
157 
158     Option zkServerOption = OptionBuilder.withLongOpt(ZKSERVERADDRESS)
159         .withDescription("Provide zookeeper address").create();
160     zkServerOption.setArgs(1);
161     zkServerOption.setRequired(true);
162     zkServerOption.setArgName("ZookeeperServerAddress(Required)");
163 
164     Option portOption = OptionBuilder.withLongOpt(PORT)
165     .withDescription("Provide web service port").create();
166     portOption.setArgs(1);
167     portOption.setRequired(false);
168     portOption.setArgName("web service port, default: "+ DEFAULT_PORT);
169 
170     Options options = new Options();
171     options.addOption(helpOption);
172     options.addOption(zkServerOption);
173     options.addOption(portOption);
174 
175     return options;
176   }
177 
178   public static void processCommandLineArgs(String[] cliArgs) throws Exception
179   {
180     CommandLineParser cliParser = new GnuParser();
181     Options cliOptions = constructCommandLineOptions();
182     CommandLine cmd = null;
183 
184     try
185     {
186       cmd = cliParser.parse(cliOptions, cliArgs);
187     }
188     catch (ParseException pe)
189     {
190       System.err.println("RestAdminApplication: failed to parse command-line options: "
191           + pe.toString());
192       printUsage(cliOptions);
193       System.exit(1);
194     }
195     int port = DEFAULT_PORT;
196     if(cmd.hasOption(HELP))
197     {
198       printUsage(cliOptions);
199       return;
200     }
201     else if(cmd.hasOption(PORT))
202     {
203       port = Integer.parseInt(cmd.getOptionValue(PORT));
204     }
205    
206     HelixAdminWebApp app = new HelixAdminWebApp(cmd.getOptionValue(ZKSERVERADDRESS), port);
207     app.start();
208     try
209     {
210       Thread.currentThread().join();
211     }
212     finally
213     {
214       app.stop();
215     }
216   }
217   
218   /**
219    * @param args
220    * @throws Exception
221    */
222   public static void main(String[] args) throws Exception
223   {
224     processCommandLineArgs(args);
225     
226   }
227 
228 }