You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by da...@apache.org on 2023/10/16 09:20:33 UTC

[camel] branch main updated: CAMEL-19995: camel-jbang - Run and reload from clipboard

This is an automated email from the ASF dual-hosted git repository.

davsclaus pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/camel.git


The following commit(s) were added to refs/heads/main by this push:
     new 1d8e3a98af0 CAMEL-19995: camel-jbang - Run and reload from clipboard
1d8e3a98af0 is described below

commit 1d8e3a98af0f8ee30a9fb6da69bd15dc532b000d
Author: Claus Ibsen <cl...@gmail.com>
AuthorDate: Mon Oct 16 11:20:04 2023 +0200

    CAMEL-19995: camel-jbang - Run and reload from clipboard
---
 .../modules/ROOT/pages/camel-jbang.adoc            | 14 ++++
 .../camel/periodic-task/clipboard-reload-strategy  |  2 +
 .../java/org/apache/camel/main/KameletMain.java    | 12 ++++
 .../camel/main/util/ClipboardReloadStrategy.java   | 78 ++++++++++++++++++++++
 4 files changed, 106 insertions(+)

diff --git a/docs/user-manual/modules/ROOT/pages/camel-jbang.adoc b/docs/user-manual/modules/ROOT/pages/camel-jbang.adoc
index 10331acfab3..314bd99da6b 100644
--- a/docs/user-manual/modules/ROOT/pages/camel-jbang.adoc
+++ b/docs/user-manual/modules/ROOT/pages/camel-jbang.adoc
@@ -1007,6 +1007,20 @@ For example. you can copy this to your clipboard and then run it afterwards:
 camel run clipboard.xml
 ----
 
+==== Run and reload from clipboard
+
+*Available since Camel 4.2*
+
+It is also possible to run from clipboard in _reload_ mode as shown:
+
+[source,bash]
+----
+camel run clipboard.xml --dev
+----
+
+Then you can quickly make changes and copy to clipboard, and Camel JBang will update while running.
+
+
 === Sending messages via Camel
 
 *Available since Camel 4*
diff --git a/dsl/camel-kamelet-main/src/generated/resources/META-INF/services/org/apache/camel/periodic-task/clipboard-reload-strategy b/dsl/camel-kamelet-main/src/generated/resources/META-INF/services/org/apache/camel/periodic-task/clipboard-reload-strategy
new file mode 100644
index 00000000000..0702c60e0e5
--- /dev/null
+++ b/dsl/camel-kamelet-main/src/generated/resources/META-INF/services/org/apache/camel/periodic-task/clipboard-reload-strategy
@@ -0,0 +1,2 @@
+# Generated by camel build tools - do NOT edit this file!
+class=org.apache.camel.main.util.ClipboardReloadStrategy
diff --git a/dsl/camel-kamelet-main/src/main/java/org/apache/camel/main/KameletMain.java b/dsl/camel-kamelet-main/src/main/java/org/apache/camel/main/KameletMain.java
index a7a4388faf8..b10a3fd5bdf 100644
--- a/dsl/camel-kamelet-main/src/main/java/org/apache/camel/main/KameletMain.java
+++ b/dsl/camel-kamelet-main/src/main/java/org/apache/camel/main/KameletMain.java
@@ -59,6 +59,7 @@ import org.apache.camel.main.download.MavenDependencyDownloader;
 import org.apache.camel.main.download.PackageNameSourceLoader;
 import org.apache.camel.main.download.TypeConverterLoaderDownloadListener;
 import org.apache.camel.main.injection.AnnotationDependencyInjection;
+import org.apache.camel.main.util.ClipboardReloadStrategy;
 import org.apache.camel.main.util.ExtraClassesClassLoader;
 import org.apache.camel.main.util.ExtraFilesClassLoader;
 import org.apache.camel.main.xml.blueprint.BlueprintXmlBeansHandler;
@@ -73,6 +74,7 @@ import org.apache.camel.spi.FactoryFinder;
 import org.apache.camel.spi.FactoryFinderResolver;
 import org.apache.camel.spi.LanguageResolver;
 import org.apache.camel.spi.LifecycleStrategy;
+import org.apache.camel.spi.PeriodTaskScheduler;
 import org.apache.camel.spi.Registry;
 import org.apache.camel.spi.ResourceLoader;
 import org.apache.camel.spi.RoutesLoader;
@@ -572,6 +574,16 @@ public class KameletMain extends MainCommandLineSupport {
                 answer.addService(new DefaultContextReloadStrategy());
             }
 
+            // special for reloading enabled on clipboard
+            String reloadDir = getInitialProperties().getProperty("camel.main.routesIncludePattern");
+            if (reloadDir != null && reloadDir.startsWith("file:.camel-jbang/generated-clipboard")) {
+                String name = reloadDir.substring(5);
+                File file = new File(name);
+                ClipboardReloadStrategy reloader = new ClipboardReloadStrategy(file);
+                answer.addService(reloader);
+                PeriodTaskScheduler scheduler = PluginHelper.getPeriodTaskScheduler(answer);
+                scheduler.schedulePeriodTask(reloader, 2000);
+            }
         } catch (Exception e) {
             throw RuntimeCamelException.wrapRuntimeException(e);
         }
diff --git a/dsl/camel-kamelet-main/src/main/java/org/apache/camel/main/util/ClipboardReloadStrategy.java b/dsl/camel-kamelet-main/src/main/java/org/apache/camel/main/util/ClipboardReloadStrategy.java
new file mode 100644
index 00000000000..b2d46807e0a
--- /dev/null
+++ b/dsl/camel-kamelet-main/src/main/java/org/apache/camel/main/util/ClipboardReloadStrategy.java
@@ -0,0 +1,78 @@
+/*
+ * 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.camel.main.util;
+
+import java.awt.*;
+import java.awt.datatransfer.Clipboard;
+import java.awt.datatransfer.DataFlavor;
+import java.io.File;
+import java.io.FileInputStream;
+
+import org.apache.camel.CamelContext;
+import org.apache.camel.CamelContextAware;
+import org.apache.camel.spi.annotations.PeriodicTask;
+import org.apache.camel.support.service.ServiceSupport;
+import org.apache.camel.util.IOHelper;
+
+@PeriodicTask("clipboard-reload-strategy")
+public class ClipboardReloadStrategy extends ServiceSupport implements CamelContextAware, Runnable {
+
+    private CamelContext camelContext;
+    private final File file;
+    private String content;
+
+    public ClipboardReloadStrategy(File file) {
+        this.file = file;
+    }
+
+    @Override
+    public CamelContext getCamelContext() {
+        return camelContext;
+    }
+
+    @Override
+    public void setCamelContext(CamelContext camelContext) {
+        this.camelContext = camelContext;
+    }
+
+    @Override
+    protected void doStart() throws Exception {
+        if (file.exists()) {
+            try (FileInputStream fis = new FileInputStream(file)) {
+                content = IOHelper.loadText(fis);
+            }
+        }
+    }
+
+    @Override
+    public void run() {
+        // grab content from clipboard
+        try {
+            Clipboard c = Toolkit.getDefaultToolkit().getSystemClipboard();
+            Object t = c.getData(DataFlavor.stringFlavor);
+            if (t != null) {
+                String text = t.toString();
+                if (text != null && !text.isBlank() && (content == null || text.compareTo(content) != 0)) {
+                    IOHelper.writeText(text, file);
+                    content = text;
+                }
+            }
+        } catch (Exception e) {
+            // ignore
+        }
+    }
+}