Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

java

Package Overview
Dependencies
Maintainers
1
Versions
58
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

java - npm Package Compare versions

Comparing version 5.0.0 to 5.0.1

testAsyncOptions/testClientBeforeError.js

127

lib/nodeJavaBridge.js

@@ -5,3 +5,6 @@ 'use strict';

var _ = require('lodash');
var async = require('async');
var path = require('path');
var fs = require('fs');
var binaryPath = path.resolve(path.join(__dirname, "../build/Release/nodejavabridge_bindings.node"));

@@ -13,2 +16,7 @@ var bindings = require(binaryPath);

java.classpath.push(path.resolve(__dirname, __dirname, "../src-java"));
java.classpath.pushDir = function(dir) {
fs.readdirSync(dir).forEach(function(file) {
java.classpath.push(path.resolve(dir, file));
});
};
java.nativeBindingLocation = binaryPath;

@@ -29,2 +37,121 @@

java.isJvmCreated = function() {
return typeof java.onJvmCreated !== 'function';
}
var clients = [];
// We provide two methods for 'clients' of node-java to 'register' their use of java.
// By registering, a client gets the opportunity to be called asynchronously just before the JVM is created,
// and just after the JVM is created. The before hook function will typically be used to add to java.classpath.
// The function may peform asynchronous operations, such as async [glob](https://github.com/isaacs/node-glob)
// resolutions of wild-carded file system paths, and then notify when it has finished via either calling
// a node-style callback function, or by resolving a promise.
// A client can register function hooks to be called before and after the JVM is created.
// If the client doesn't need to be called back for either function, it can pass null or undefined.
// Both before and after here are assumed to be functions that accept one argument that is a node-callback function.
java.registerClient = function(before, after) {
if (java.isJvmCreated()) {
throw new Error('java.registerClient() called after JVM already created.');
}
clients.push({before: before, after: after});
}
// A client can register function hooks to be called before and after the JVM is created.
// If the client doesn't need to be called back for either function, it can pass null or undefined.
// Both before and after here are assumed to be functions that return Promises/A+ `thenable` objects.
java.registerClientP = function(beforeP, afterP) {
if (java.isJvmCreated()) {
throw new Error('java.registerClient() called after JVM already created.');
}
clients.push({beforeP: beforeP, afterP: afterP});
}
function runBeforeHooks(done) {
function iterator(client, cb) {
try {
if (client.before) {
client.before(cb);
}
else if (client.beforeP) {
client.beforeP().then(function(ignored) { cb(); }, function(err) { cb(err); });
}
else {
cb();
}
}
catch (err) {
cb(err);
}
}
async.each(clients, iterator, done);
}
function createJVMAsync(callback) {
var ignore = java.newLong(0); // called just for the side effect that it will create the JVM
callback();
}
function runAfterHooks(done) {
function iterator(client, cb) {
try {
if (client.after) {
client.after(cb);
}
else if (client.afterP) {
client.afterP().then(function(ignored) { cb(); }, function(err) { cb(err); });
}
else {
cb();
}
}
catch (err) {
cb(err);
}
}
async.each(clients, iterator, done);
}
function initializeAll(done) {
async.series([runBeforeHooks, createJVMAsync, runAfterHooks], done);
}
// This function ensures that the JVM has been launched, asynchronously. The application can be notified
// when the JVM is fully created via either a node callback function, or via a promise.
// If the parameter `callback` is provided, it is assume be a node callback function.
// If the parameter is not provided, and java.asyncOptions.promisify has been specified,
// then this function will return a promise, by promisifying itself and then calling that
// promisified function.
// This function may be called multiple times -- the 2nd and subsequent calls are no-ops.
// However, once this method has been called (or the JVM is launched as a side effect of calling other java
// methods), then clients can no longer use the registerClient API.
java.ensureJvm = function(callback) {
// First see if the promise-style API should be used.
// This must be done first in order to ensure the proper API is used.
if (_.isUndefined(callback) && java.asyncOptions && _.isFunction(java.asyncOptions.promisify)) {
// Create a promisified version of this function.
var launchJvmPromise = java.asyncOptions.promisify(java.ensureJvm.bind(java));
// Call the promisified function, returning its result, which should be a promise.
return launchJvmPromise();
}
// If we get here, callback must be a node-style callback function. If not, throw an error.
else if (!_.isFunction(callback)) {
throw new Error('java.launchJvm(cb) requires its one argument to be a callback function.');
}
// Now check if the JVM has already been created. If so, we assume that the jvm was already successfully
// launched, and we can just implement idempotent behavior, i.e. silently notify that the JVM has been created.
else if (java.isJvmCreated()) {
return setImmediate(callback);
}
// Finally, queue the initializeAll function.
else {
return setImmediate(initializeAll, callback);
}
}
java.onJvmCreated = function() {

@@ -31,0 +158,0 @@ if (java.asyncOptions) {

4

package.json

@@ -10,3 +10,3 @@ {

],
"version": "5.0.0",
"version": "5.0.1",
"engines": {

@@ -30,2 +30,3 @@ "node": ">=0.10.0"

"dependencies": {
"async": "0.9.0",
"find-java-home": "0.1.2",

@@ -36,3 +37,2 @@ "glob": "5.0.5",

"devDependencies": {
"async": "0.9.0",
"chalk": "1.0.0",

@@ -39,0 +39,0 @@ "lodash": "3.7.0",

@@ -182,3 +182,3 @@ [![Build Status](https://travis-ci.org/joeferner/node-java.png)](https://travis-ci.org/joeferner/node-java)

* We've tested promises with five Promises/A+ implementations. See `testHelpers.js` for more information.
* NOTE: Due to specifics of initialization order, the methods `java.newInstancePromise`, `java.callMethodPromise`, and `java.callStaticMethodPromise` are not available until some other java method is called. You may need to call some other java method such as `java.import()` to finalize java initialization.
* NOTE: Due to specifics of initialization order, the methods `java.newInstancePromise`, `java.callMethodPromise`, and `java.callStaticMethodPromise` are not available until the JVM has been created. You may need to call some other java method such as `java.import()` to finalize java initialization, or even better, the function `java.ensureJVM()`.

@@ -205,2 +205,6 @@ ##### Special note about the exported module functions `newInstance`, `callMethod`, and `callStaticMethod`.

## JVM Creation
With v0.5.1 a new API is available to make it easier for a complex application to have full control over JVM creation. In particular, it is now easier to compose an application from several modules, each of which must add to the Java classpath and possibly do other operations just before or just after the JVM has been created. See the methods [ensureJvm](#javaEnsureJvm) and [registerClient](#javaRegisterClient). See also several of the tests in the testAsyncOptions directory.
# Release Notes

@@ -238,2 +242,6 @@

* [newProxy](#javaNewProxy)
* [isJvmCreated](#javaIsJvmCreated)
* [registerClient](#javaRegisterClient)
* [registerClientP](#javaRegisterClientP)
* [ensureJvm](#javaEnsureJvm)

@@ -259,2 +267,3 @@ ## java objects

java.classpath.push('commons.io.jar');
java.classpath.pathDir('lib');

@@ -526,3 +535,25 @@ <a name="javaOptions" />

thread.start();
<a name="javaisJvmCreated" />
**java.isJvmCreated()**
Returns true if the JVM has been created. The JVM can only be created once.
<a name="javaRegisterClient" />
**java.registerClient(before, after)**
Register that a client wants to be called back immediately before and/or immediately after the JVM is created. If used, this function must be called before the JVM has been created. The before function is typically used to add to the classpath. The function may execute asynchronous operations (such as a async glob function). The after function is sometimes useful for doing one-time initialization that requires the JVM to first be initialized. If either function is unnecessary, use `null` or `undefined`. See also `registerClientP` and `ensureJvm`. See the unit tests in `testAsyncOptions` for examples.
<a name="javaRegisterClientP" />
**java.registerClientP(before, after)**
Like java.registerClient, but before and after are assumed to be functions returning promises.
<a name="javaEnsureJvm" />
**java.ensureJvm(callback)**
If the JVM has not yet been created, execute the full JVM initialization process, then call callback function when initialization is complete. If the JVM has been created, just call the callback. Note that the full initialization process includes: 1) executing all registered client *before* hooks, 2) creating the JVM, then 3) executing all registered client *after* hooks.
<a name="javaObject"/>

@@ -529,0 +560,0 @@ ## java object

@@ -9,8 +9,41 @@ // testDefacto.js

java.asyncOptions = {
syncSuffix: "Sync",
asyncSuffix: ""
};
module.exports = {
module.exports = {
launch: function(test) {
test.expect(9);
var api = _.functions(java);
test.ok(_.includes(api, 'isJvmCreated'), 'Expected `isJvmCreated` to be present, but it is NOT.');
test.ok(!java.isJvmCreated());
java.asyncOptions = {
syncSuffix: "Sync",
asyncSuffix: ""
};
function before(callback) {
test.ok(!java.isJvmCreated());
callback();
}
function after(callback) {
test.ok(java.isJvmCreated());
callback();
}
java.registerClient(before, after);
java.registerClient(undefined, after);
java.registerClient(before, undefined);
java.ensureJvm(function(err) {
test.ifError(err);
test.ok(java.isJvmCreated());
// Verify that ensureJvm is idempotent
java.ensureJvm(function(err) {
test.ifError(err);
test.done();
});
});
},
testAPI: function(test) {

@@ -17,0 +50,0 @@ test.expect(5);

@@ -7,12 +7,38 @@ // testDefactoPlusPromise.js

var assert = require("assert");
var _ = require('lodash');
var _ = require("lodash");
java.asyncOptions = {
syncSuffix: "Sync",
asyncSuffix: "",
promiseSuffix: 'Promise',
promisify: require('when/node').lift // https://github.com/cujojs/when
};
module.exports = {
launch: function(test) {
test.expect(7);
var api = _.functions(java);
test.ok(_.includes(api, 'isJvmCreated'), 'Expected `isJvmCreated` to be present, but it is NOT.');
test.ok(!java.isJvmCreated());
module.exports = {
java.asyncOptions = {
syncSuffix: "Sync",
asyncSuffix: "",
promiseSuffix: 'Promise',
promisify: require('when/node').lift // https://github.com/cujojs/when
};
function before(callback) {
test.ok(!java.isJvmCreated());
callback();
}
function after(callback) {
test.ok(java.isJvmCreated());
callback();
}
java.registerClient(before, after);
java.registerClient(null, after);
java.registerClient(before);
java.ensureJvm().done(function() {
test.ok(java.isJvmCreated());
test.done();
});
},
testAPI: function(test) {

@@ -19,0 +45,0 @@ test.expect(5);

@@ -8,10 +8,43 @@ // testNoAsync.js

var _ = require('lodash');
var when = require('when');
java.asyncOptions = {
syncSuffix: "Sync",
promiseSuffix: 'Promise',
promisify: require('when/node').lift // https://github.com/cujojs/when
};
module.exports = {
launch: function(test) {
test.expect(7);
var api = _.functions(java);
test.ok(_.includes(api, 'isJvmCreated'), 'Expected `isJvmCreated` to be present, but it is NOT.');
test.ok(!java.isJvmCreated());
module.exports = {
java.asyncOptions = {
syncSuffix: "Sync",
promiseSuffix: 'Promise',
promisify: require('when/node').lift
};
function before() {
var promise = when.promise(function(resolve, reject) {
test.ok(!java.isJvmCreated());
resolve();
});
return promise;
}
function after() {
var promise = when.promise(function(resolve, reject) {
test.ok(java.isJvmCreated());
resolve();
});
return promise;
}
java.registerClientP(before, after);
java.registerClientP(null, after);
java.registerClientP(before);
java.ensureJvm().done(function() {
test.ok(java.isJvmCreated());
test.done();
});
},
testAPI: function(test) {

@@ -18,0 +51,0 @@ test.expect(6);

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc