IGNITE-1121 Fix minor problems.
Project: http://git-wip-us.apache.org/repos/asf/incubator-ignite/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-ignite/commit/4ce20925 Tree: http://git-wip-us.apache.org/repos/asf/incubator-ignite/tree/4ce20925 Diff: http://git-wip-us.apache.org/repos/asf/incubator-ignite/diff/4ce20925 Branch: refs/heads/ignite-843 Commit: 4ce209256b37817620ff2b018194f8a904594e6e Parents: e3162d2 Author: sevdokimov <sevdoki...@gridgain.com> Authored: Fri Jul 31 15:02:50 2015 +0300 Committer: sevdokimov <sevdoki...@gridgain.com> Committed: Fri Jul 31 15:02:50 2015 +0300 ---------------------------------------------------------------------- .../org/apache/ignite/agent/AgentSocket.java | 35 ++-- .../java/org/apache/ignite/agent/Utils.java | 41 ----- .../ignite/agent/handlers/DBExtractor.java | 45 +++++ .../ignite/agent/handlers/RestExecutor.java | 166 +++++++++++++++++++ .../ignite/agent/remote/RemoteHandler.java | 55 +++--- .../src/main/js/agents/agent-manager.js | 14 +- 6 files changed, 252 insertions(+), 104 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/4ce20925/modules/control-center-agent/src/main/java/org/apache/ignite/agent/AgentSocket.java ---------------------------------------------------------------------- diff --git a/modules/control-center-agent/src/main/java/org/apache/ignite/agent/AgentSocket.java b/modules/control-center-agent/src/main/java/org/apache/ignite/agent/AgentSocket.java index 9370160..935f417 100644 --- a/modules/control-center-agent/src/main/java/org/apache/ignite/agent/AgentSocket.java +++ b/modules/control-center-agent/src/main/java/org/apache/ignite/agent/AgentSocket.java @@ -18,15 +18,13 @@ package org.apache.ignite.agent; import com.google.gson.*; +import org.apache.ignite.agent.handlers.*; import org.apache.ignite.agent.remote.*; -import org.apache.ignite.schema.parser.*; import org.eclipse.jetty.websocket.api.*; import org.eclipse.jetty.websocket.api.annotations.*; import java.io.*; import java.net.*; -import java.sql.*; -import java.util.*; import java.util.concurrent.*; import java.util.logging.*; @@ -39,6 +37,12 @@ public class AgentSocket implements WebSocketSender { private static final Logger log = Logger.getLogger(AgentSocket.class.getName()); /** */ + public static final Gson GSON = new Gson(); + + /** */ + public static final JsonParser PARSER = new JsonParser(); + + /** */ private final CountDownLatch closeLatch = new CountDownLatch(1); /** */ @@ -80,11 +84,11 @@ public class AgentSocket implements WebSocketSender { */ @OnWebSocketConnect public void onConnect(Session ses) { - log.log(Level.INFO, "Authentication..."); + log.log(Level.INFO, "Connection established"); this.ses = ses; - remote = RemoteHandler.wrap(this, this, restExecutor); + remote = RemoteHandler.wrap(this, this, restExecutor, new DBExtractor()); JsonObject authMsg = new JsonObject(); @@ -100,7 +104,7 @@ public class AgentSocket implements WebSocketSender { * @return Whether or not message was sent. */ @Override public boolean send(JsonObject msg) { - return send(Utils.GSON.toJson(msg)); + return send(GSON.toJson(msg)); } /** @@ -142,7 +146,7 @@ public class AgentSocket implements WebSocketSender { */ @OnWebSocketMessage public void onMessage(String msg) { - JsonElement jsonElement = Utils.PARSER.parse(msg); + JsonElement jsonElement = PARSER.parse(msg); remote.onMessage((JsonObject)jsonElement); } @@ -163,23 +167,6 @@ public class AgentSocket implements WebSocketSender { /** * - * @param jdbcDriverJarPath JDBC driver JAR path. - * @param jdbcDriverClass JDBC driver class. - * @param jdbcUrl JDBC URL. - * @param jdbcInfo Properties to connect to database. - * - * @return Collection of tables. - */ - @Remote - public Collection<DbTable> extractMetadata(String jdbcDriverJarPath, String jdbcDriverClass, String jdbcUrl, - Properties jdbcInfo, boolean tablesOnly) throws SQLException { - Connection conn = DBReader.getInstance().connect(jdbcDriverJarPath, jdbcDriverClass, jdbcUrl, jdbcInfo); - - return DBReader.getInstance().extractMetadata(conn, tablesOnly); - } - - /** - * */ public void waitForClose() throws InterruptedException { closeLatch.await(); http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/4ce20925/modules/control-center-agent/src/main/java/org/apache/ignite/agent/Utils.java ---------------------------------------------------------------------- diff --git a/modules/control-center-agent/src/main/java/org/apache/ignite/agent/Utils.java b/modules/control-center-agent/src/main/java/org/apache/ignite/agent/Utils.java deleted file mode 100644 index b86e1df..0000000 --- a/modules/control-center-agent/src/main/java/org/apache/ignite/agent/Utils.java +++ /dev/null @@ -1,41 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.apache.ignite.agent; - -import com.google.gson.*; - -/** - * - */ -public class Utils { - /** */ - public static final Gson GSON = new Gson(); - - /** */ - public static final JsonParser PARSER = new JsonParser(); - - /** */ - public static final Object[] EMPTY_OBJECTS = new Object[0]; - - /** - * Default constructor. - */ - private Utils() { - // No-op. - } -} http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/4ce20925/modules/control-center-agent/src/main/java/org/apache/ignite/agent/handlers/DBExtractor.java ---------------------------------------------------------------------- diff --git a/modules/control-center-agent/src/main/java/org/apache/ignite/agent/handlers/DBExtractor.java b/modules/control-center-agent/src/main/java/org/apache/ignite/agent/handlers/DBExtractor.java new file mode 100644 index 0000000..676e900 --- /dev/null +++ b/modules/control-center-agent/src/main/java/org/apache/ignite/agent/handlers/DBExtractor.java @@ -0,0 +1,45 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.ignite.agent.handlers; + +import org.apache.ignite.agent.remote.*; +import org.apache.ignite.schema.parser.*; + +import java.sql.*; +import java.util.*; + +/** + * Remote API to extract DB metadata. + */ +public class DBExtractor { + /** + * @param jdbcDriverJarPath JDBC driver JAR path. + * @param jdbcDriverCls JDBC driver class. + * @param jdbcUrl JDBC URL. + * @param jdbcInfo Properties to connect to database. + * + * @return Collection of tables. + */ + @Remote + public Collection<DbTable> extractMetadata(String jdbcDriverJarPath, String jdbcDriverCls, String jdbcUrl, + Properties jdbcInfo, boolean tblsOnly) throws SQLException { + Connection conn = DBReader.getInstance().connect(jdbcDriverJarPath, jdbcDriverCls, jdbcUrl, jdbcInfo); + + return DBReader.getInstance().extractMetadata(conn, tblsOnly); + } +} http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/4ce20925/modules/control-center-agent/src/main/java/org/apache/ignite/agent/handlers/RestExecutor.java ---------------------------------------------------------------------- diff --git a/modules/control-center-agent/src/main/java/org/apache/ignite/agent/handlers/RestExecutor.java b/modules/control-center-agent/src/main/java/org/apache/ignite/agent/handlers/RestExecutor.java new file mode 100644 index 0000000..8b0ae98 --- /dev/null +++ b/modules/control-center-agent/src/main/java/org/apache/ignite/agent/handlers/RestExecutor.java @@ -0,0 +1,166 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.ignite.agent.handlers; + +import org.apache.commons.codec.*; +import org.apache.http.*; +import org.apache.http.client.entity.*; +import org.apache.http.client.methods.*; +import org.apache.http.client.utils.*; +import org.apache.http.entity.*; +import org.apache.http.impl.client.*; +import org.apache.ignite.agent.*; +import org.apache.ignite.agent.remote.*; + +import java.io.*; +import java.net.*; +import java.nio.charset.*; +import java.util.*; +import java.util.logging.*; + +/** + * Executor for REST requests. + */ +public class RestExecutor { + /** */ + private static final Logger log = Logger.getLogger(RestExecutor.class.getName()); + + /** */ + private final AgentConfiguration cfg; + + /** */ + private CloseableHttpClient httpClient; + + /** + * @param cfg Config. + */ + public RestExecutor(AgentConfiguration cfg) { + this.cfg = cfg; + } + + /** + * + */ + public void start() { + httpClient = HttpClientBuilder.create().build(); + } + + /** + * + */ + public void stop() throws IOException { + if (httpClient != null) + httpClient.close(); + } + + /** + * @param path Path. + * @param method Method. + * @param params Params. + * @param headers Headers. + * @param body Body. + */ + @Remote + public RestResult executeRest(String path, Map<String, String> params, String method, Map<String, String> headers, + String body) throws IOException, URISyntaxException { + URIBuilder builder = new URIBuilder(cfg.getNodeUri()); + + if (path != null) { + if (!path.startsWith("/") && !cfg.getNodeUri().toString().endsWith("/")) + path = '/' + path; + + builder.setPath(path); + } + + if (params != null) { + for (Map.Entry<String, String> entry : params.entrySet()) + builder.addParameter(entry.getKey(), entry.getValue()); + } + + HttpRequestBase httpReq; + + if ("GET".equalsIgnoreCase(method)) + httpReq = new HttpGet(builder.build()); + else if ("POST".equalsIgnoreCase(method)) { + HttpPost post; + + if (body == null) { + List<NameValuePair> nvps = builder.getQueryParams(); + + builder.clearParameters(); + + post = new HttpPost(builder.build()); + + if (!nvps.isEmpty()) + post.setEntity(new UrlEncodedFormEntity(nvps)); + } + else { + post = new HttpPost(builder.build()); + + post.setEntity(new StringEntity(body)); + } + + httpReq = post; + } + else + throw new IOException("Unknown HTTP-method: " + method); + + if (headers != null) { + for (Map.Entry<String, String> entry : headers.entrySet()) + httpReq.addHeader(entry.getKey(), entry.getValue()); + } + + try (CloseableHttpResponse resp = httpClient.execute(httpReq)) { + ByteArrayOutputStream out = new ByteArrayOutputStream(); + + resp.getEntity().writeTo(out); + + Charset charset = Charsets.UTF_8; + + Header encodingHdr = resp.getEntity().getContentEncoding(); + + if (encodingHdr != null) { + String encoding = encodingHdr.getValue(); + + charset = Charsets.toCharset(encoding); + } + + return new RestResult(resp.getStatusLine().getStatusCode(), new String(out.toByteArray(), charset)); + } + } + + /** + * + */ + public static class RestResult { + /** */ + private int code; + + /** */ + private String message; + + /** + * @param code Code. + * @param msg Message. + */ + public RestResult(int code, String msg) { + this.code = code; + message = msg; + } + } +} http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/4ce20925/modules/control-center-agent/src/main/java/org/apache/ignite/agent/remote/RemoteHandler.java ---------------------------------------------------------------------- diff --git a/modules/control-center-agent/src/main/java/org/apache/ignite/agent/remote/RemoteHandler.java b/modules/control-center-agent/src/main/java/org/apache/ignite/agent/remote/RemoteHandler.java index fee5ba6..6579578 100644 --- a/modules/control-center-agent/src/main/java/org/apache/ignite/agent/remote/RemoteHandler.java +++ b/modules/control-center-agent/src/main/java/org/apache/ignite/agent/remote/RemoteHandler.java @@ -18,7 +18,6 @@ package org.apache.ignite.agent.remote; import com.google.gson.*; -import org.apache.ignite.agent.*; import java.lang.reflect.*; import java.util.*; @@ -33,6 +32,15 @@ public class RemoteHandler implements AutoCloseable { private static final Logger log = Logger.getLogger(RemoteHandler.class.getName()); /** */ + public static final Gson GSON = new Gson(); + + /** */ + private static final String INTERNAL_EXCEPTION_TYPE = "org.apache.ignite.agent.AgentException"; + + /** */ + public static final Object[] EMPTY_OBJECTS = new Object[0]; + + /** */ private final WebSocketSender snd; /** */ @@ -79,7 +87,7 @@ public class RemoteHandler implements AutoCloseable { final MethodDescriptor desc = methods.get(mtdName); if (desc == null) { - sendError(reqId, "Unknown method: " + mtdName); + sendException(reqId, INTERNAL_EXCEPTION_TYPE, "Unknown method: " + mtdName); return; } @@ -94,19 +102,19 @@ public class RemoteHandler implements AutoCloseable { args = new Object[paramTypes.length]; if (argsJson == null || argsJson.size() != paramTypes.length) { - sendError(reqId, "Inconsistent parameters"); + sendException(reqId, INTERNAL_EXCEPTION_TYPE, "Inconsistent parameters"); return; } for (int i = 0; i < paramTypes.length; i++) - args[i] = Utils.GSON.fromJson(argsJson.get(i), paramTypes[i]); + args[i] = GSON.fromJson(argsJson.get(i), paramTypes[i]); } else { - args = Utils.EMPTY_OBJECTS; + args = EMPTY_OBJECTS; if (argsJson != null && argsJson.size() > 0) { - sendError(reqId, "Inconsistent parameters"); + sendException(reqId, INTERNAL_EXCEPTION_TYPE, "Inconsistent parameters"); return; } @@ -118,9 +126,12 @@ public class RemoteHandler implements AutoCloseable { try { res = desc.mtd.invoke(desc.hnd, args); - } catch (Exception e) { + } catch (Throwable e) { + if (e instanceof InvocationTargetException) + e = ((InvocationTargetException)e).getTargetException(); + if (reqId != null) - sendException(reqId, e); + sendException(reqId, e.getClass().getName(), e.getMessage()); else log.log(Level.SEVERE, "Exception on execute remote method", e); @@ -139,9 +150,10 @@ public class RemoteHandler implements AutoCloseable { /** * @param reqId Request id. - * @param ex Exception. + * @param exType Exception class name. + * @param exMsg Exception message. */ - protected void sendException(Long reqId, Exception ex) { + protected void sendException(Long reqId, String exType, String exMsg) { if (reqId == null) return; @@ -151,8 +163,8 @@ public class RemoteHandler implements AutoCloseable { res.addProperty("reqId", reqId); JsonObject exJson = new JsonObject(); - exJson.addProperty("type", ex.getClass().getName()); - exJson.addProperty("message", ex.getMessage()); + exJson.addProperty("type", exType); + exJson.addProperty("message", exMsg); res.add("ex", exJson); @@ -161,23 +173,6 @@ public class RemoteHandler implements AutoCloseable { /** * @param reqId Request id. - * @param err Exception. - */ - protected void sendError(Long reqId, String err) { - if (reqId == null) - return; - - JsonObject res = new JsonObject(); - - res.addProperty("type", "CallRes"); - res.addProperty("reqId", reqId); - res.addProperty("error", err); - - snd.send(res); - } - - /** - * @param reqId Request id. * @param res Result. * @param type Type. */ @@ -196,7 +191,7 @@ public class RemoteHandler implements AutoCloseable { if (type == void.class) resJson = JsonNull.INSTANCE; else - resJson = Utils.GSON.toJsonTree(res, type); + resJson = GSON.toJsonTree(res, type); resp.add("res", resJson); http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/4ce20925/modules/control-center-web/src/main/js/agents/agent-manager.js ---------------------------------------------------------------------- diff --git a/modules/control-center-web/src/main/js/agents/agent-manager.js b/modules/control-center-web/src/main/js/agents/agent-manager.js index 3734c39..875cdab 100644 --- a/modules/control-center-web/src/main/js/agents/agent-manager.js +++ b/modules/control-center-web/src/main/js/agents/agent-manager.js @@ -163,10 +163,8 @@ Client.prototype.executeRest = function(path, params, method, headers, body, cb) var newArgs = argsToArray(arguments); - newArgs[5] = function(err, ex, res) { - if (err) - cb(err); - else if (ex) + newArgs[5] = function(ex, res) { + if (ex) cb(ex.message); else cb(null, res.code, res.message) @@ -251,12 +249,10 @@ Client.prototype._rmtCallRes = function(msg) { delete this._cbMap[msg.reqId]; - if (msg.error) - cb(msg.error); - else if (msg.ex) - cb(null, ex); + if (msg.ex) + cb(msg.ex); else - cb(null, null, msg.res); + cb(null, msg.res); }; /**