View Javadoc

1   package org.apache.helix;
2   /*
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *   http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing,
14   * software distributed under the License is distributed on an
15   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16   * KIND, either express or implied.  See the License for the
17   * specific language governing permissions and limitations
18   * under the License.
19   */
20  import java.io.IOException;
21  import java.io.Reader;
22  import java.nio.ByteBuffer;
23  import java.nio.CharBuffer;
24  import java.nio.channels.ReadableByteChannel;
25  import java.util.List;
26  
27  import org.apache.helix.tools.ClusterSetup;
28  import org.apache.log4j.Logger;
29  import org.codehaus.jackson.JsonGenerationException;
30  import org.codehaus.jackson.map.JsonMappingException;
31  import org.restlet.Component;
32  import org.restlet.Context;
33  import org.restlet.data.MediaType;
34  import org.restlet.data.Method;
35  import org.restlet.data.Request;
36  import org.restlet.data.Response;
37  import org.restlet.data.Status;
38  import org.restlet.resource.Representation;
39  import org.restlet.resource.Resource;
40  import org.restlet.resource.ResourceException;
41  import org.restlet.resource.StringRepresentation;
42  import org.restlet.resource.Variant;
43  
44  
45  public class StopServiceResource extends Resource {
46  
47  
48  	private static final Logger logger = Logger
49  			.getLogger(StopServiceResource.class);
50  
51  	Context _context;
52  
53  	public StopServiceResource(Context context,
54  			Request request,
55  			Response response) 
56  	{
57  		super(context, request, response);
58  		getVariants().add(new Variant(MediaType.TEXT_PLAIN));
59  		getVariants().add(new Variant(MediaType.APPLICATION_JSON));
60  		_context = context;
61  	}
62  
63  	public boolean allowGet()
64  	{
65  		System.out.println("PutResource.allowGet()");
66  		return true;
67  	}
68  
69  	public boolean allowPost()
70  	{
71  		System.out.println("PutResource.allowPost()");
72  		return false;
73  	}
74  
75  	public boolean allowPut()
76  	{
77  		System.out.println("PutResource.allowPut()");
78  		return false;
79  	}
80  
81  	public boolean allowDelete()
82  	{
83  		return false;
84  	}
85  
86  
87  	class StopThread implements Runnable {
88  
89  		Component _component;
90  		MockNode _mockNode;
91  		
92  		StopThread(Component c, MockNode m) {
93  			_component = c;
94  			_mockNode = m;
95  		}
96  		
97  		@Override
98  		public void run() {
99  			try {
100 				//sleep for 1 second, then end service
101 				Thread.sleep(1000);
102 				_component.stop();
103 				_mockNode.disconnect();
104 				System.exit(0);
105 			} catch (Exception e) {
106 				logger.error("Unable to stop service: "+e);
107 				e.printStackTrace();
108 			}
109 		}
110 		
111 	}
112 	
113 	//XXX: handling both gets and puts here for now
114 	public Representation represent(Variant variant)
115 	{
116 		System.out.println("StopServiceResource.represent()");
117 		StringRepresentation presentation = null;
118 		try
119 		{
120 			logger.debug("in represent, stopping service");
121 			Component component = (Component)_context.getAttributes().get(MockEspressoService.COMPONENT_NAME);
122 			EspressoStorageMockNode mock = (EspressoStorageMockNode)_context.getAttributes().get(MockEspressoService.CONTEXT_MOCK_NODE_NAME);
123 			presentation = new StringRepresentation("Stopping in 1 second", MediaType.APPLICATION_JSON);
124 			Thread stopper = new Thread(new StopThread(component, mock));
125 			stopper.start();
126 		}
127 
128 		catch(Exception e)
129 		{
130 			String error = "Error shutting down";
131 			presentation = new StringRepresentation(error, MediaType.APPLICATION_JSON);	      
132 			e.printStackTrace();
133 		}  
134 		return presentation;
135 	}
136 
137 	public void storeRepresentation(Representation entity) throws ResourceException {
138 
139 	}
140 
141 
142 
143 }