Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,7 @@ public void execute(String... args) {
.addSubcommand("get", new CommandLine(new VersionGet(this)))
.addSubcommand("list", new CommandLine(new VersionList(this)))
.addSubcommand("set", new CommandLine(new VersionSet(this))))
.addSubcommand("wrapper", new CommandLine(new WrapperCommand(this)))
.setParameterExceptionHandler(new MissingPluginParameterExceptionHandler());

postAddCommands(commandLine, args);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
/*
* 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.dsl.jbang.core.commands;

import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.attribute.PosixFilePermission;
import java.util.Set;

import org.apache.camel.catalog.CamelCatalog;
import org.apache.camel.catalog.DefaultCamelCatalog;
import org.apache.camel.util.IOHelper;
import picocli.CommandLine;
import picocli.CommandLine.Command;

@Command(name = "wrapper", description = "Install Camel wrapper scripts for version pinning", sortOptions = false,
showDefaultValues = true)
public class WrapperCommand extends CamelCommand {

private static final String DEFAULT_REPO_URL = "https://repo1.maven.org/maven2";
private static final String WRAPPER_PROPERTIES_FILE = "camel-wrapper.properties";
private static final String CAMEL_DIR = ".camel";

@CommandLine.Option(names = { "--camel-version" },
description = "Camel version to pin (defaults to current version)")
String camelVersion;

@CommandLine.Option(names = { "--repo-url" },
description = "Maven repository URL for downloading the launcher",
defaultValue = DEFAULT_REPO_URL)
String repoUrl = DEFAULT_REPO_URL;

@CommandLine.Option(names = { "--dir", "--directory" },
description = "Directory where wrapper files will be created",
defaultValue = ".")
String directory = ".";

public WrapperCommand(CamelJBangMain main) {
super(main);
}

@Override
public Integer doCall() throws Exception {
if (camelVersion == null || camelVersion.isEmpty()) {
CamelCatalog catalog = new DefaultCamelCatalog();
camelVersion = catalog.getCatalogVersion();
}

Path baseDir = Paths.get(directory).toAbsolutePath().normalize();
Path camelDir = baseDir.resolve(CAMEL_DIR);

// Create .camel directory
Files.createDirectories(camelDir);

// Write camel-wrapper.properties
writeProperties(camelDir);

// Write camelw script
writeScript(baseDir, "camelw");

// Write camelw.cmd script
writeScript(baseDir, "camelw.cmd");

printer().println("Apache Camel wrapper installed successfully.");
printer().println(" Camel version: " + camelVersion);
printer().println(" Properties: " + camelDir.resolve(WRAPPER_PROPERTIES_FILE));
printer().println(" Unix script: " + baseDir.resolve("camelw"));
printer().println(" Windows script: " + baseDir.resolve("camelw.cmd"));
printer().println();
printer().println("You can now use ./camelw instead of camel to run with the pinned version.");

return 0;
}

void writeProperties(Path camelDir) throws IOException {
String distributionUrl = buildDistributionUrl();
String content = """
# 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.
camel.version=%s
distributionUrl=%s
""".formatted(camelVersion, distributionUrl);

Files.writeString(camelDir.resolve(WRAPPER_PROPERTIES_FILE), content);
}

String buildDistributionUrl() {
return repoUrl
+ "/org/apache/camel/camel-launcher/"
+ camelVersion
+ "/camel-launcher-"
+ camelVersion
+ ".jar";
}

void writeScript(Path baseDir, String scriptName) throws IOException {
String resourcePath = "camel-wrapper/" + scriptName;
try (InputStream is = WrapperCommand.class.getClassLoader().getResourceAsStream(resourcePath)) {
if (is == null) {
throw new IOException("Resource not found: " + resourcePath);
}
String content = IOHelper.loadText(is);
Path scriptPath = baseDir.resolve(scriptName);
Files.writeString(scriptPath, content);

// Make Unix script executable
if (!scriptName.endsWith(".cmd")) {
makeExecutable(scriptPath);
}
}
}

private void makeExecutable(Path path) {
try {
Set<PosixFilePermission> perms = Files.getPosixFilePermissions(path);
perms.add(PosixFilePermission.OWNER_EXECUTE);
perms.add(PosixFilePermission.GROUP_EXECUTE);
perms.add(PosixFilePermission.OTHERS_EXECUTE);
Files.setPosixFilePermissions(path, perms);
} catch (UnsupportedOperationException | IOException e) {
// Windows or other OS that doesn't support POSIX permissions
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,160 @@
#!/bin/sh
# ----------------------------------------------------------------------------
# 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.
# ----------------------------------------------------------------------------

# ----------------------------------------------------------------------------
# Apache Camel Wrapper startup script
#
# Required ENV vars:
# ------------------
# JAVA_HOME - location of a JDK home dir
#
# Optional ENV vars
# -----------------
# CAMEL_OPTS - parameters passed to the Java VM when running Camel
# ----------------------------------------------------------------------------

# OS specific support
cygwin=false
darwin=false
mingw=false
case "$(uname)" in
CYGWIN*) cygwin=true ;;
MINGW*) mingw=true ;;
Darwin*)
darwin=true
if [ -z "$JAVA_HOME" ]; then
if [ -x "/usr/libexec/java_home" ]; then
JAVA_HOME="$(/usr/libexec/java_home)"
export JAVA_HOME
else
JAVA_HOME="/Library/Java/Home"
export JAVA_HOME
fi
fi
;;
esac

if [ -z "$JAVA_HOME" ]; then
if [ -r /etc/gentoo-release ]; then
JAVA_HOME=$(java-config --jre-home)
fi
fi

# Resolve the script directory
if [ -z "$CAMEL_WRAPPER_DIR" ]; then
# resolve links - $0 may be a softlink
PRG="$0"
while [ -h "$PRG" ]; do
ls=$(ls -ld "$PRG")
link=$(expr "$ls" : '.*-> \(.*\)$')
if expr "$link" : '/.*' > /dev/null; then
PRG="$link"
else
PRG=$(dirname "$PRG")/"$link"
fi
done
CAMEL_WRAPPER_DIR=$(dirname "$PRG")
CAMEL_WRAPPER_DIR=$(cd "$CAMEL_WRAPPER_DIR" && pwd)
fi

# Read properties file
CAMEL_PROPERTIES="$CAMEL_WRAPPER_DIR/.camel/camel-wrapper.properties"
if [ ! -f "$CAMEL_PROPERTIES" ]; then
echo "Error: Could not find $CAMEL_PROPERTIES" >&2
echo "Please run 'camel wrapper' to set up the Camel wrapper." >&2
exit 1
fi

# Parse properties
CAMEL_VERSION=$(grep '^camel.version=' "$CAMEL_PROPERTIES" | cut -d'=' -f2-)
DISTRIBUTION_URL=$(grep '^distributionUrl=' "$CAMEL_PROPERTIES" | cut -d'=' -f2-)

if [ -z "$CAMEL_VERSION" ]; then
echo "Error: camel.version not found in $CAMEL_PROPERTIES" >&2
exit 1
fi

if [ -z "$DISTRIBUTION_URL" ]; then
echo "Error: distributionUrl not found in $CAMEL_PROPERTIES" >&2
exit 1
fi

# Determine the Java command to use
if [ -n "$JAVA_HOME" ]; then
if [ -x "$JAVA_HOME/jre/sh/java" ]; then
JAVACMD="$JAVA_HOME/jre/sh/java"
else
JAVACMD="$JAVA_HOME/bin/java"
fi
if [ ! -x "$JAVACMD" ]; then
echo "Error: JAVA_HOME is set to an invalid directory: $JAVA_HOME" >&2
echo "Please set the JAVA_HOME variable in your environment to match the" >&2
echo "location of your Java installation." >&2
exit 1
fi
else
JAVACMD="java"
command -v java >/dev/null 2>&1 || {
echo "Error: JAVA_HOME is not set and 'java' command not found in PATH." >&2
echo "Please set the JAVA_HOME variable in your environment to match the" >&2
echo "location of your Java installation." >&2
exit 1
}
fi

# Set up the cache directory for the launcher jar
CAMEL_CACHE_DIR="${HOME}/.camel/wrapper"
CAMEL_LAUNCHER_JAR="$CAMEL_CACHE_DIR/camel-launcher-${CAMEL_VERSION}.jar"

# Download the launcher jar if it doesn't exist
if [ ! -f "$CAMEL_LAUNCHER_JAR" ]; then
mkdir -p "$CAMEL_CACHE_DIR"
echo "Downloading Camel Launcher ${CAMEL_VERSION}..."

if command -v curl > /dev/null 2>&1; then
curl -fsSL -o "$CAMEL_LAUNCHER_JAR" "$DISTRIBUTION_URL" || {
echo "Error: Failed to download Camel Launcher from $DISTRIBUTION_URL" >&2
rm -f "$CAMEL_LAUNCHER_JAR"
exit 1
}
elif command -v wget > /dev/null 2>&1; then
wget -q -O "$CAMEL_LAUNCHER_JAR" "$DISTRIBUTION_URL" || {
echo "Error: Failed to download Camel Launcher from $DISTRIBUTION_URL" >&2
rm -f "$CAMEL_LAUNCHER_JAR"
exit 1
}
else
echo "Error: Neither curl nor wget found. Please install one of them." >&2
exit 1
fi

echo "Camel Launcher ${CAMEL_VERSION} downloaded successfully."
fi

# For Cygwin/MinGW, switch paths to Windows format
if $cygwin; then
CAMEL_LAUNCHER_JAR=$(cygpath --path --windows "$CAMEL_LAUNCHER_JAR")
fi

# Run the Camel launcher
exec "$JAVACMD" \
$CAMEL_OPTS \
-jar "$CAMEL_LAUNCHER_JAR" \
"$@"
Loading
Loading