Skip to content

Commit 0d585d0

Browse files
authored
Support passing custom environment, OSGIfy jar,generate source Jar.Support passing custom environment, OSGIfy jar,generate source Jar. (#27)
2 parents d273778 + 6c77611 commit 0d585d0

5 files changed

Lines changed: 73 additions & 2 deletions

File tree

CHANGES.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
# MatConsoleCtl releases
22

33
## [Unreleased]
4+
- Support passing custom MATLAB environment & modify build gradle to OSGIfy(add the MANIFEST headers) the generated jar.
5+
- https://github.com/diffplug/matconsolectl/pull/27
46

57
## [4.6.0] - 2020-11-12
68
- Added checks for directories and non-exisiting files in classpath converters: In Configuration.getClassPathAsRMICodebase() and Configuration.getClassPathAsCanonicalPaths(). This prevents unnecessary IOExceptions for invalid classpaths.

build.gradle

Lines changed: 44 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,30 @@ task testMatlabAll(dependsOn: [
5454
testMatlabInteractive
5555
])
5656

57+
//////////
58+
// OSGI //
59+
//////////
60+
61+
// delete the old manifest to ensure there's no caching or merging going on
62+
jar.doFirst {
63+
project.file('META-INF/MANIFEST.MF').delete()
64+
}
65+
66+
jar.manifest {
67+
attributes (
68+
'Bundle-Description': "${project.description}",
69+
'Bundle-DocURL': "https://${project.git_url}",
70+
'Bundle-License': "https://${project.git_url}/blob/v${version}/LICENSE",
71+
'Bundle-ManifestVersion': "${osgi_manifest_ver}",
72+
'Bundle-SymbolicName': "${project.group}.${project.name}",
73+
'Bundle-Vendor': "${project.org}",
74+
'Bundle-Version': "${project.version}",
75+
'Export-Package': "matlabcontrol;version=\"${project.version}\",matlabcontrol.demo;version=\"${project.version}\",matlabcontrol.extensions;uses:=matlabcontrol;version=\"${project.version}\",matlabcontrol.internal;version=\"${project.version}\",matlabcontrol.link;uses:=matlabcontrol;version=\"${project.version}\"",
76+
'Main-Class': 'matlabcontrol.demo.DemoMain',
77+
'Require-Capability': 'osgi.ee;filter:="(&(osgi.ee=JavaSE)(version=1.7))"'
78+
)
79+
}
80+
5781
////////////
5882
// JAVA 6 //
5983
////////////
@@ -111,9 +135,23 @@ def JMI_STUB = 'com/mathworks/jmi/**'
111135

112136
// make a source jar
113137
task sourcesJar(type: Jar) {
114-
classifier = 'sources'
138+
archiveClassifier = 'sources'
115139
from sourceSets.main.allJava
116140
exclude(JMI_STUB)
141+
manifest {
142+
attributes (
143+
'Bundle-Description': "${project.description}",
144+
'Bundle-DocURL': "https://${project.git_url}",
145+
'Bundle-License': "https://${project.git_url}/blob/v${version}/LICENSE",
146+
'Bundle-ManifestVersion': "${osgi_manifest_ver}",
147+
'Bundle-SymbolicName': "${project.group}.${project.name}",
148+
'Bundle-Vendor': "${project.org}",
149+
'Bundle-Version': "${project.version}",
150+
'Export-Package': "matlabcontrol;version=\"${project.version}\",matlabcontrol.demo;version=\"${project.version}\",matlabcontrol.extensions;uses:=matlabcontrol;version=\"${project.version}\",matlabcontrol.internal;version=\"${project.version}\",matlabcontrol.link;uses:=matlabcontrol;version=\"${project.version}\"",
151+
'Main-Class': 'matlabcontrol.demo.DemoMain',
152+
'Require-Capability': 'osgi.ee;filter:="(&(osgi.ee=JavaSE)(version=1.7))"'
153+
)
154+
}
117155
}
118156

119157
// Where it's possible to name parameters and methods clearly enough
@@ -131,10 +169,14 @@ javadoc {
131169
}
132170

133171
task javadocJar(type: Jar, dependsOn: javadoc) {
134-
classifier = 'javadoc'
172+
archiveClassifier = 'javadoc'
135173
from javadoc.destinationDir
136174
}
137175

176+
artifacts {
177+
archives sourcesJar
178+
}
179+
138180
// it all needs to get published and formatted
139181
apply from: 干.file('base/maven.gradle')
140182
apply from: 干.file('base/sonatype.gradle')

gradle.properties

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ group=com.diffplug.matsim
55
description=MatConsoleCtl - control MATLAB from Java
66
org=diffplug
77

8+
osgi_manifest_ver=2
9+
810
git_url=github.com/diffplug/matconsolectl
911
maven_group=com.diffplug.matsim
1012
maven_name=matconsolectl

src/main/java/matlabcontrol/MatlabProxyFactoryOptions.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import java.util.concurrent.atomic.AtomicLong;
1111

1212
import matlabcontrol.MatlabProxyFactory.CopyPasteCallback;
13+
import java.util.Map;
1314

1415
/**
1516
* Options that configure how a factory operates. Use a {@link Builder} to create an instance of this class.
@@ -35,6 +36,7 @@ public class MatlabProxyFactoryOptions {
3536
private final int _port;
3637
private final Writer _outputWriter;
3738
private final Writer _errorWriter;
39+
private final Map<String, String> _matlabEnvironment;
3840

3941
private MatlabProxyFactoryOptions(Builder options) {
4042
_matlabLocation = options._matlabLocation;
@@ -51,6 +53,7 @@ private MatlabProxyFactoryOptions(Builder options) {
5153
_port = options._port;
5254
_outputWriter = options._outputWriter;
5355
_errorWriter = options._errorWriter;
56+
_matlabEnvironment = options._matlabEnvironment;
5457
}
5558

5659
String getMatlabLocation() {
@@ -109,6 +112,10 @@ Writer getErrorWriter() {
109112
return _errorWriter;
110113
}
111114

115+
Map<String, String> getMatlabEnvironment() {
116+
return _matlabEnvironment;
117+
}
118+
112119
/**
113120
* Creates instances of {@link MatlabProxyFactoryOptions}. Any and all of these properties may be left unset, if so
114121
* then a default will be used. Depending on how the factory operates, not all properties may be used. Currently all
@@ -142,6 +149,7 @@ public static class Builder {
142149
private volatile int _port = 2100;
143150
private volatile Writer _outputWriter = null;
144151
private volatile Writer _errorWriter = null;
152+
private volatile Map<String, String> _matlabEnvironment = null;
145153

146154
//Assigning to a long is not atomic, so use an AtomicLong so that a thread always sees an intended value
147155
private final AtomicLong _proxyTimeout = new AtomicLong(180000L);
@@ -426,6 +434,18 @@ public final Builder setErrorWriter(Writer errorWriter) {
426434
return this;
427435
}
428436

437+
/**
438+
* Sets the matlab environment as a key-value pair mapping.
439+
* This map is appended to the environment map of the spawned matlab proxy instance.
440+
*
441+
* @param matlabEnvironment
442+
*/
443+
public final Builder setMatlabEnvironment(Map<String, String> matlabEnvironment) {
444+
_matlabEnvironment = matlabEnvironment;
445+
446+
return this;
447+
}
448+
429449
/**
430450
* Builds a {@code MatlabProxyFactoryOptions} instance.
431451
*

src/main/java/matlabcontrol/RemoteMatlabProxyFactory.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import java.util.UUID;
2525
import java.util.concurrent.Callable;
2626
import java.util.concurrent.CopyOnWriteArrayList;
27+
import java.util.Map;
2728

2829
import matlabcontrol.MatlabProxy.Identifier;
2930
import matlabcontrol.MatlabProxyFactory.Request;
@@ -268,6 +269,10 @@ private Process createProcess(RemoteRequestReceiver receiver) throws MatlabConne
268269
//Create process
269270
ProcessBuilder builder = new ProcessBuilder(processArguments);
270271
builder.directory(_options.getStartingDirectory());
272+
Map<String, String> env = builder.environment();
273+
if (_options.getMatlabEnvironment() != null && !_options.getMatlabEnvironment().isEmpty()) {
274+
env.putAll(_options.getMatlabEnvironment());
275+
}
271276

272277
try {
273278
Process process = builder.start();

0 commit comments

Comments
 (0)