diff options
author | Kjetil Ørbekk <kjetil.orbekk@gmail.com> | 2012-02-06 21:34:42 +0100 |
---|---|---|
committer | Kjetil Ørbekk <kjetil.orbekk@gmail.com> | 2012-02-06 21:34:42 +0100 |
commit | 7e8f0f25129785068db661cab80cc13d658120a4 (patch) | |
tree | 10ef32cded6f01477495a80ad509165d38910cf6 /same/src/main | |
parent | 5a6fc8506af742694c856215dc0e182ba8790c08 (diff) |
Support setting state in StateServlet.
Diffstat (limited to 'same/src/main')
-rw-r--r-- | same/src/main/java/com/orbekk/same/Client.java | 17 | ||||
-rw-r--r-- | same/src/main/java/com/orbekk/same/http/StateServlet.java | 26 |
2 files changed, 43 insertions, 0 deletions
diff --git a/same/src/main/java/com/orbekk/same/Client.java b/same/src/main/java/com/orbekk/same/Client.java index 2a5f11e..60faf1b 100644 --- a/same/src/main/java/com/orbekk/same/Client.java +++ b/same/src/main/java/com/orbekk/same/Client.java @@ -28,6 +28,23 @@ public class Client implements DiscoveryListener { public State getState() { return new State(state); } + + public void set(String name, String data) throws UpdateConflict { + String masterUrl = state.getDataOf(".masterUrl"); + long revision = state.getRevision(name) + 1; + MasterService master = connections.getMaster(masterUrl); + try { + boolean success = master.updateStateRequest(name, data, + revision); + if (!success) { + throw new UpdateConflict("State update conflict when " + + "updating " + name); + } + } catch (Exception e) { + logger.error("Unable to contact master. Update fails.", e); + throw new UpdateConflict("Unable to contact master. Update fails."); + } + } } private ClientInterface clientInterface = new ClientInterface(); diff --git a/same/src/main/java/com/orbekk/same/http/StateServlet.java b/same/src/main/java/com/orbekk/same/http/StateServlet.java index 0ccf062..0ca1682 100644 --- a/same/src/main/java/com/orbekk/same/http/StateServlet.java +++ b/same/src/main/java/com/orbekk/same/http/StateServlet.java @@ -1,5 +1,7 @@ package com.orbekk.same.http; +import static com.orbekk.same.StackTraceUtil.throwableToString; + import java.io.IOException; import javax.servlet.http.HttpServlet; @@ -10,6 +12,7 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import com.orbekk.same.Client; +import com.orbekk.same.UpdateConflict; public class StateServlet extends HttpServlet { private Logger logger = LoggerFactory.getLogger(getClass()); @@ -19,9 +22,32 @@ public class StateServlet extends HttpServlet { this.client = client; } + private void handleSetState(HttpServletRequest request, + HttpServletResponse response) throws IOException { + if (request.getParameter("key") == null || + request.getParameter("value") == null) { + response.getWriter().println( + "Usage: action=set&key=DesiredKey&value=DesiredValue"); + } + + try { + client.set(request.getParameter("key"), request.getParameter("value")); + response.getWriter().println("Updated component: " + + request.getParameter("key") + "=" + + request.getParameter("value")); + } catch (UpdateConflict e) { + response.getWriter().println("Update conflict: " + + throwableToString(e)); + } + } + @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException { + if ("set".equals(request.getParameter("action"))) { + handleSetState(request, response); + } + response.setContentType("text/plain; charset=utf8"); response.getWriter().println(client.getState()); response.setStatus(HttpServletResponse.SC_OK); |