| 1 |
/* |
| 2 |
* Copyright 2020, Seqera Labs |
| 3 |
* Copyright 2013-2019, Centre for Genomic Regulation (CRG) |
| 4 |
* |
| 5 |
* Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 |
* you may not use this file except in compliance with the License. |
| 7 |
* You may obtain a copy of the License at |
| 8 |
* |
| 9 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 10 |
* |
| 11 |
* Unless required by applicable law or agreed to in writing, software |
| 12 |
* distributed under the License is distributed on an "AS IS" BASIS, |
| 13 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 |
* See the License for the specific language governing permissions and |
| 15 |
* limitations under the License. |
| 16 |
*/ |
| 17 |
|
| 18 |
import java.nio.file.Path; |
| 19 |
import java.util.ArrayList; |
| 20 |
import java.util.List; |
| 21 |
import java.util.Map; |
| 22 |
|
| 23 |
/** |
| 24 |
* Implement capsule custom strategy to add maven dependencies as |
| 25 |
* a command line argument as required for Nextflow project |
| 26 |
* |
| 27 |
* @author Paolo Di Tommaso <[email protected]> |
| 28 |
*/ |
| 29 |
public class NextflowLoader extends Capsule { |
| 30 |
|
| 31 |
/** |
| 32 |
* Constructs a capsule from the given JAR file |
| 33 |
* |
| 34 |
* @param jarFile the path to the JAR file |
| 35 |
*/ |
| 36 |
protected NextflowLoader(Path jarFile) { |
| 37 |
super(jarFile); |
| 38 |
} |
| 39 |
|
| 40 |
@Override |
| 41 |
protected ProcessBuilder prelaunch(List<String> jvmArgs, List<String> args) { |
| 42 |
ProcessBuilder pb = super.prelaunch(jvmArgs, args); |
| 43 |
|
| 44 |
String drip = System.getenv().get("NXF_DRIP"); |
| 45 |
if( drip != null && !drip.isEmpty() ) { |
| 46 |
pb.command().set(0, drip); |
| 47 |
return pb; |
| 48 |
} |
| 49 |
|
| 50 |
return pb; |
| 51 |
} |
| 52 |
|
| 53 |
@Override |
| 54 |
@SuppressWarnings("unchecked") |
| 55 |
protected <T> T attribute(Map.Entry<String, T> attr) { |
| 56 |
|
| 57 |
if (ATTR_DEPENDENCIES == attr && System.getenv("NXF_GRAB") != null ) { |
| 58 |
String deps = System.getenv("NXF_GRAB"); |
| 59 |
List<String> parent = super.attribute((Map.Entry<String,List<String>>)attr); |
| 60 |
return (T)extendDepsWith(deps, parent); |
| 61 |
} |
| 62 |
|
| 63 |
if (ATTR_APP_CLASS_PATH == attr && System.getenv("NXF_CLASSPATH") != null) { |
| 64 |
String classpath = System.getenv("NXF_CLASSPATH"); |
| 65 |
List<String> parent = super.attribute((Map.Entry<String, List<String>>)attr); |
| 66 |
return (T)extendClassPathWith(classpath, parent); |
| 67 |
} |
| 68 |
|
| 69 |
return super.attribute(attr); |
| 70 |
} |
| 71 |
|
| 72 |
static List<String> extendClassPathWith(String classpath, List<String> origin) { |
| 73 |
if( classpath == null || "".equals(classpath.trim()) ) { |
| 74 |
return origin; |
| 75 |
} |
| 76 |
|
| 77 |
List<String> result = new ArrayList<>(); |
| 78 |
if( origin != null ) { |
| 79 |
result.addAll(origin); |
| 80 |
} |
| 81 |
|
| 82 |
for( String lib : classpath.split(":") ) { |
| 83 |
String trimmed = lib.trim(); |
| 84 |
if( trimmed.length()>0 ) |
| 85 |
result.add(trimmed); |
| 86 |
} |
| 87 |
|
| 88 |
return result; |
| 89 |
} |
| 90 |
|
| 91 |
/* |
| 92 |
* get a blank separated list of dependencies |
| 93 |
* having the "maven" syntax i.e. group:name:version |
| 94 |
*/ |
| 95 |
static List<String> extendDepsWith(String dependencies, List<String> origin) { |
| 96 |
if( dependencies == null || "".equals(dependencies.trim()) ) { |
| 97 |
return origin; |
| 98 |
} |
| 99 |
|
| 100 |
// split by black |
| 101 |
List<String> result = new ArrayList<>(); |
| 102 |
for( String dep : dependencies.split(" |\n") ) { |
| 103 |
String trimmed = dep.trim(); |
| 104 |
if( trimmed.length()>0 ) |
| 105 |
result.add(trimmed); |
| 106 |
} |
| 107 |
|
| 108 |
// add all the inherited if any |
| 109 |
if( origin != null && origin.size()>0 ) { |
| 110 |
result.addAll(0, origin); |
| 111 |
} |
| 112 |
|
| 113 |
return result; |
| 114 |
} |
| 115 |
|
| 116 |
} |