You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@ignite.apache.org by ak...@apache.org on 2016/02/24 03:50:21 UTC

[12/20] ignite git commit: IGNITE-2287 Reworked code generation of example code.

IGNITE-2287 Reworked code generation of example code.


Project: http://git-wip-us.apache.org/repos/asf/ignite/repo
Commit: http://git-wip-us.apache.org/repos/asf/ignite/commit/c01cca41
Tree: http://git-wip-us.apache.org/repos/asf/ignite/tree/c01cca41
Diff: http://git-wip-us.apache.org/repos/asf/ignite/diff/c01cca41

Branch: refs/heads/ignite-843-rc3
Commit: c01cca4109f9984ce4aec53b11c8af76e2b84cd5
Parents: ea979d8
Author: vsisko <vs...@gridgain.com>
Authored: Sat Feb 20 11:22:27 2016 +0700
Committer: Alexey Kuznetsov <ak...@apache.org>
Committed: Sat Feb 20 11:22:27 2016 +0700

----------------------------------------------------------------------
 .../configuration/summary/summary.controller.js | 15 ++++++
 .../js/helpers/generator/generator-common.js    | 12 +++++
 .../main/js/helpers/generator/generator-java.js | 57 +++++++++++++++++---
 3 files changed, 77 insertions(+), 7 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/ignite/blob/c01cca41/modules/control-center-web/src/main/js/app/modules/states/configuration/summary/summary.controller.js
----------------------------------------------------------------------
diff --git a/modules/control-center-web/src/main/js/app/modules/states/configuration/summary/summary.controller.js b/modules/control-center-web/src/main/js/app/modules/states/configuration/summary/summary.controller.js
index 5f677d9..7cd564a 100644
--- a/modules/control-center-web/src/main/js/app/modules/states/configuration/summary/summary.controller.js
+++ b/modules/control-center-web/src/main/js/app/modules/states/configuration/summary/summary.controller.js
@@ -73,6 +73,14 @@ export default [
             ]
         };
 
+        const exampleFolder = {
+            type: 'folder',
+            name: 'example',
+            children: [
+                { type: 'file', name: 'ExampleStartup.java' }
+            ]
+        };
+
         const javaFolder = {
             type: 'folder',
             name: 'java',
@@ -188,6 +196,9 @@ export default [
 
             javaFolder.children = [javaConfigFolder, javaStartupFolder];
 
+            if ($generatorCommon.dataForExampleConfigured(cluster))
+                javaFolder.children.push(exampleFolder);
+
             _.forEach(cluster.caches, (cache) => {
                 if (cache.cacheStoreFactory) {
                     const store = cache.cacheStoreFactory[cache.cacheStoreFactory.kind];
@@ -247,6 +258,10 @@ export default [
             zip.file(srcPath + 'config/ServerConfigurationFactory.java', $generatorJava.cluster(cluster, 'config', 'ServerConfigurationFactory', null));
             zip.file(srcPath + 'config/ClientConfigurationFactory.java', $generatorJava.cluster(cluster, 'config', 'ClientConfigurationFactory', clientNearCfg));
 
+            if ($generatorCommon.dataForExampleConfigured(cluster))
+                zip.file(srcPath + 'example/ExampleStartup.java', $generatorJava.nodeStartup(cluster, 'startup', 'ExampleStartup',
+                    'ServerConfigurationFactory.createConfiguration()', 'config.ServerConfigurationFactory'));
+
             zip.file(srcPath + 'startup/ServerNodeSpringStartup.java', $generatorJava.nodeStartup(cluster, 'startup', 'ServerNodeSpringStartup', '"' + serverXml + '"'));
             zip.file(srcPath + 'startup/ClientNodeSpringStartup.java', $generatorJava.nodeStartup(cluster, 'startup', 'ClientNodeSpringStartup', '"' + clientXml + '"'));
 

http://git-wip-us.apache.org/repos/asf/ignite/blob/c01cca41/modules/control-center-web/src/main/js/helpers/generator/generator-common.js
----------------------------------------------------------------------
diff --git a/modules/control-center-web/src/main/js/helpers/generator/generator-common.js b/modules/control-center-web/src/main/js/helpers/generator/generator-common.js
index 40614b6..3731070 100644
--- a/modules/control-center-web/src/main/js/helpers/generator/generator-common.js
+++ b/modules/control-center-web/src/main/js/helpers/generator/generator-common.js
@@ -508,3 +508,15 @@ $generatorCommon.binaryIsDefined = function (binary) {
 $generatorCommon.domainQueryMetadata = function(domain) {
     return domain.queryMetadata ? domain.queryMetadata : 'Configuration';
 };
+
+/**
+ * Checks if cluster has demo types.
+ *
+ * @param cluster Cluster to check.
+ * @returns {boolean} True if cluster has caches with demo types.
+ */
+$generatorCommon.dataForExampleConfigured = function(cluster) {
+    return $commonUtils.isDefined(_.find(cluster.caches, function (cache) {
+        return _.find(cache.domains, {demo: true});
+    }));
+};

http://git-wip-us.apache.org/repos/asf/ignite/blob/c01cca41/modules/control-center-web/src/main/js/helpers/generator/generator-java.js
----------------------------------------------------------------------
diff --git a/modules/control-center-web/src/main/js/helpers/generator/generator-java.js b/modules/control-center-web/src/main/js/helpers/generator/generator-java.js
index 57ac1da..16079be 100644
--- a/modules/control-center-web/src/main/js/helpers/generator/generator-java.js
+++ b/modules/control-center-web/src/main/js/helpers/generator/generator-java.js
@@ -2759,9 +2759,6 @@ function _multilineQuery(res, query, prefix, postfix) {
                 res.line('"' + line + '"' + (ix === lines.length - 1 ? postfix : ' +'));
         });
 
-        if (lines.length > 0)
-            res.needEmptyLine = true;
-
         if (lines.length > 1)
             res.endBlock();
     }
@@ -2900,6 +2897,14 @@ $generatorJava.generateExample = function (cluster, res, factoryCls) {
 
         res.needEmptyLine = true;
 
+        res.line('/** Print result table to console */');
+        res.startBlock('private static void printResult(' + res.importClass('java.util.List') + '<' + res.importClass('javax.cache.Cache') + '.Entry<Object, Object>> rows) {');
+        res.startBlock('for (Cache.Entry<Object, Object> row: rows) {');
+        res.line('System.out.println(row);');
+        res.endBlock('}');
+        res.endBlock('}');
+        res.needEmptyLine = true;
+
         // Generation of execute queries method.
         res.line('/** Run demo examples. */');
         res.startBlock('private static void runExamples(Ignite ignite) throws SQLException {');
@@ -2908,6 +2913,9 @@ $generatorJava.generateExample = function (cluster, res, factoryCls) {
             return fullType.substr(fullType.lastIndexOf('.') + 1);
         };
 
+        var cacheLoaded = [];
+        var rowVariableDeclared = false;
+
         _.forEach(typeByDs, function (types, ds) {
             var conVar = ds + 'Con';
 
@@ -2921,8 +2929,19 @@ $generatorJava.generateExample = function (cluster, res, factoryCls) {
 
                     if (desc) {
                         _.forEach(desc.selectQuery, function (query) {
-                            _multilineQuery(res, query, 'ignite.cache("' + type.cache.name + '").query(new ' + res.importClass('org.apache.ignite.cache.query.SqlQuery') +
-                                '<>("' + getType(domain.valueType) + '", ', '));');
+                            var cacheName = type.cache.name;
+
+                            if (!_.contains(cacheLoaded, cacheName)) {
+                                res.line('ignite.cache("' + cacheName + '").loadCache(null);');
+
+                                cacheLoaded.push(cacheName);
+                            }
+
+                            _multilineQuery(res, query, (rowVariableDeclared ? 'rows' : 'List<Cache.Entry<Object, Object>> rows') +
+                                ' = ignite.cache("' + cacheName + '").query(new ' + res.importClass('org.apache.ignite.cache.query.SqlQuery') +
+                                '<>("' + getType(domain.valueType) + '", ', ')).getAll();');
+                            res.line('printResult(rows);');
+                            rowVariableDeclared = true;
 
                             res.needEmptyLine = true;
                         })
@@ -2954,14 +2973,26 @@ $generatorJava.generateExample = function (cluster, res, factoryCls) {
  * @param clientNearCfg Optional near cache configuration for client node.
  */
 $generatorJava.nodeStartup = function (cluster, pkg, cls, cfg, factoryCls, clientNearCfg) {
+    var example = cls === 'ExampleStartup';
+
     var res = $generatorCommon.builder();
 
     res.line('/**');
     res.line(' * ' + $generatorCommon.mainComment());
+
+    if (example) {
+        res.line(' *');
+        res.line(' * To start example configure data sources in secret.properties file.');
+        res.line(' * For H2 database it should be like following:');
+        res.line(' * dsH2.jdbc.url=jdbc:h2:tcp://localhost/mem:ExampleDb;DB_CLOSE_DELAY=-1');
+        res.line(' * dsH2.jdbc.username=sa');
+        res.line(' * dsH2.jdbc.password=');
+    }
+
     res.line(' */');
     res.startBlock('public class ' + cls + ' {');
 
-    if (factoryCls)
+    if (example)
         var demoTypes = $generatorJava.generateExample(cluster, res, factoryCls);
 
     res.line('/**');
@@ -2973,6 +3004,18 @@ $generatorJava.nodeStartup = function (cluster, pkg, cls, cfg, factoryCls, clien
 
     res.startBlock('public static void main(String[] args) throws Exception {');
 
+    if (example) {
+        res.startBlock('try {');
+        res.line('// Start H2 database server.');
+        res.line(res.importClass('org.h2.tools.Server') + '.createTcpServer("-tcpDaemon").start();');
+        res.endBlock('}');
+        res.startBlock('catch (' + res.importClass('java.sql.SQLException') + ' ignore) {');
+        res.line('// No-op.');
+        res.endBlock('}');
+
+        res.needEmptyLine = true;
+    }
+
     if (factoryCls)
         res.importClass(factoryCls);
 
@@ -3002,7 +3045,7 @@ $generatorJava.nodeStartup = function (cluster, pkg, cls, cfg, factoryCls, clien
         }
     }
 
-    if ($commonUtils.isDefinedAndNotEmpty(demoTypes)) {
+    if (example) {
         res.needEmptyLine = true;
 
         res.line('prepareExampleData();');