Skip to content

Commit

Permalink
Support passing custom environment, OSGIfy jar,generate source Jar.Su…
Browse files Browse the repository at this point in the history
…pport passing custom environment, OSGIfy jar,generate source Jar. (#27)
  • Loading branch information
nedtwigg authored Jul 10, 2023
2 parents d273778 + 6c77611 commit 0d585d0
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 2 deletions.
2 changes: 2 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# MatConsoleCtl releases

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

## [4.6.0] - 2020-11-12
- Added checks for directories and non-exisiting files in classpath converters: In Configuration.getClassPathAsRMICodebase() and Configuration.getClassPathAsCanonicalPaths(). This prevents unnecessary IOExceptions for invalid classpaths.
Expand Down
46 changes: 44 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,30 @@ task testMatlabAll(dependsOn: [
testMatlabInteractive
])

//////////
// OSGI //
//////////

// delete the old manifest to ensure there's no caching or merging going on
jar.doFirst {
project.file('META-INF/MANIFEST.MF').delete()
}

jar.manifest {
attributes (
'Bundle-Description': "${project.description}",
'Bundle-DocURL': "https://${project.git_url}",
'Bundle-License': "https://${project.git_url}/blob/v${version}/LICENSE",
'Bundle-ManifestVersion': "${osgi_manifest_ver}",
'Bundle-SymbolicName': "${project.group}.${project.name}",
'Bundle-Vendor': "${project.org}",
'Bundle-Version': "${project.version}",
'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}\"",
'Main-Class': 'matlabcontrol.demo.DemoMain',
'Require-Capability': 'osgi.ee;filter:="(&(osgi.ee=JavaSE)(version=1.7))"'
)
}

////////////
// JAVA 6 //
////////////
Expand Down Expand Up @@ -111,9 +135,23 @@ def JMI_STUB = 'com/mathworks/jmi/**'

// make a source jar
task sourcesJar(type: Jar) {
classifier = 'sources'
archiveClassifier = 'sources'
from sourceSets.main.allJava
exclude(JMI_STUB)
manifest {
attributes (
'Bundle-Description': "${project.description}",
'Bundle-DocURL': "https://${project.git_url}",
'Bundle-License': "https://${project.git_url}/blob/v${version}/LICENSE",
'Bundle-ManifestVersion': "${osgi_manifest_ver}",
'Bundle-SymbolicName': "${project.group}.${project.name}",
'Bundle-Vendor': "${project.org}",
'Bundle-Version': "${project.version}",
'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}\"",
'Main-Class': 'matlabcontrol.demo.DemoMain',
'Require-Capability': 'osgi.ee;filter:="(&(osgi.ee=JavaSE)(version=1.7))"'
)
}
}

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

task javadocJar(type: Jar, dependsOn: javadoc) {
classifier = 'javadoc'
archiveClassifier = 'javadoc'
from javadoc.destinationDir
}

artifacts {
archives sourcesJar
}

// it all needs to get published and formatted
apply from: 干.file('base/maven.gradle')
apply from: 干.file('base/sonatype.gradle')
2 changes: 2 additions & 0 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ group=com.diffplug.matsim
description=MatConsoleCtl - control MATLAB from Java
org=diffplug

osgi_manifest_ver=2

git_url=github.com/diffplug/matconsolectl
maven_group=com.diffplug.matsim
maven_name=matconsolectl
Expand Down
20 changes: 20 additions & 0 deletions src/main/java/matlabcontrol/MatlabProxyFactoryOptions.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import java.util.concurrent.atomic.AtomicLong;

import matlabcontrol.MatlabProxyFactory.CopyPasteCallback;
import java.util.Map;

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

private MatlabProxyFactoryOptions(Builder options) {
_matlabLocation = options._matlabLocation;
Expand All @@ -51,6 +53,7 @@ private MatlabProxyFactoryOptions(Builder options) {
_port = options._port;
_outputWriter = options._outputWriter;
_errorWriter = options._errorWriter;
_matlabEnvironment = options._matlabEnvironment;
}

String getMatlabLocation() {
Expand Down Expand Up @@ -109,6 +112,10 @@ Writer getErrorWriter() {
return _errorWriter;
}

Map<String, String> getMatlabEnvironment() {
return _matlabEnvironment;
}

/**
* Creates instances of {@link MatlabProxyFactoryOptions}. Any and all of these properties may be left unset, if so
* then a default will be used. Depending on how the factory operates, not all properties may be used. Currently all
Expand Down Expand Up @@ -142,6 +149,7 @@ public static class Builder {
private volatile int _port = 2100;
private volatile Writer _outputWriter = null;
private volatile Writer _errorWriter = null;
private volatile Map<String, String> _matlabEnvironment = null;

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

/**
* Sets the matlab environment as a key-value pair mapping.
* This map is appended to the environment map of the spawned matlab proxy instance.
*
* @param matlabEnvironment
*/
public final Builder setMatlabEnvironment(Map<String, String> matlabEnvironment) {
_matlabEnvironment = matlabEnvironment;

return this;
}

/**
* Builds a {@code MatlabProxyFactoryOptions} instance.
*
Expand Down
5 changes: 5 additions & 0 deletions src/main/java/matlabcontrol/RemoteMatlabProxyFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import java.util.UUID;
import java.util.concurrent.Callable;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.Map;

import matlabcontrol.MatlabProxy.Identifier;
import matlabcontrol.MatlabProxyFactory.Request;
Expand Down Expand Up @@ -268,6 +269,10 @@ private Process createProcess(RemoteRequestReceiver receiver) throws MatlabConne
//Create process
ProcessBuilder builder = new ProcessBuilder(processArguments);
builder.directory(_options.getStartingDirectory());
Map<String, String> env = builder.environment();
if (_options.getMatlabEnvironment() != null && !_options.getMatlabEnvironment().isEmpty()) {
env.putAll(_options.getMatlabEnvironment());
}

try {
Process process = builder.start();
Expand Down

0 comments on commit 0d585d0

Please sign in to comment.