Skip to content
This repository has been archived by the owner on Apr 19, 2023. It is now read-only.

Tidy up and bug fixes #185

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
48 changes: 48 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,52 @@ For additional client implementations in other languages, see:

For more information, see [the nailgun website](https://github.com/facebook/nailgun).

## Additional information

### Example usage

```bash
$ ./nailgun-client/target/ng com.facebook.nailgun.examples.ThreadTest
```

### Logging

To enable logging, start the server with `-Djava.util.logging.config.file="logging.properties"`. A relative file path is resolved in respect to the working directory
(i.e. system property `user.dir`). An example file is at `nailgun-server/src/main/resources/logging.properties` but will only be used if (soft-) linked appropriate.

### Aliases

* Aliases are loaded from `com/facebook/nailgun/builtins/builtins.properties`
* Adding an alias example:
```bash
$ ./nailgun-client/target/ng ng-alias test com.facebook.nailgun.examples.ThreadTest
$ ./nailgun-client/target/ng test
```

#### Built-in aliases

```bash
$ ./nailgun-client/target/ng ng-alias
ng-alias com.facebook.nailgun.builtins.NGAlias
Displays and manages command aliases

ng-cp com.facebook.nailgun.builtins.NGClasspath
Displays and manages the current system classpath

ng-stats com.facebook.nailgun.builtins.NGServerStats
Displays nail statistics

ng-stop com.facebook.nailgun.builtins.NGStop
Shuts down the nailgun server

ng-version com.facebook.nailgun.builtins.NGVersion
Displays the server version number.
```

The alias `ng-cp` is _very_ dangerous and YOU are encouraged to set
`com.facebook.nailgun.builtins.NGClasspath.ALLOW_CLASSPATH_MODIFICATION` to
`false`.

### Links

* [original homepage](http://www.martiansoftware.com/nailgun/)
1 change: 1 addition & 0 deletions logging.properties
2 changes: 1 addition & 1 deletion nailgun-examples/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
<parent>
<groupId>com.facebook</groupId>
<artifactId>nailgun-all</artifactId>
<version>1.0.1</version>
<version>1.0.2-SNAPSHOT</version>
</parent>

<dependencies>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
package com.facebook.nailgun.examples;

import com.facebook.nailgun.NGContext;

import java.io.PrintStream;
import java.util.TreeSet;

/**
Expand All @@ -28,24 +30,26 @@
public class DumpAll {

public static void nailMain(NGContext context) {
context.out.println();
context.out.println(" context.getCommand(): " + context.getCommand());
context.out.println(" context.getInetAddress(): " + context.getInetAddress());
context.out.println(" context.getPort(): " + context.getPort());
context.out.println("context.getWorkingDirectory(): " + context.getWorkingDirectory());
context.out.println(" context.getFileSeparator(): " + context.getFileSeparator());
context.out.println(" context.getPathSeparator(): " + context.getPathSeparator());

context.out.println("\ncontext.getArgs():");
PrintStream out = context.getOut();
out.println();
out.println(" context.getCommand(): " + context.getCommand());
out.println(" context.getInetAddress(): " + context.getInetAddress());
out.println(" context.getPort(): " + context.getPort());
out.println("context.getWorkingDirectory(): " + context.getWorkingDirectory());
out.println(" context.getFileSeparator(): " + context.getFileSeparator());
out.println(" context.getPathSeparator(): " + context.getPathSeparator());

out.println("\ncontext.getArgs():");
for (int i = 0; i < context.getArgs().length; ++i) {
context.out.println(" args[" + i + "]=" + context.getArgs()[i]);
out.println(" args[" + i + "]=" + context.getArgs()[i]);
}

context.out.println("\ncontext.getEnv():");
out.println("\ncontext.getEnv():");
TreeSet keys = new TreeSet(context.getEnv().keySet());
for (Object okey : keys) {
String key = (String) okey;
context.out.println(" env[\"" + key + "\"]=" + context.getEnv().getProperty(key));
out.println(" env[\"" + key + "\"]=" + context.getEnv().getProperty(key));
}
out.flush();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -83,16 +83,17 @@ public static void nailMain(NGContext context)
// display available algorithms
Set algs = getCryptoImpls("MessageDigest");
for (Object alg : algs) {
context.out.println(alg);
context.getOut().println(alg);
}
context.getOut().flush();
return;
}

// perform the actual hash. throw any exceptions back to the user.
MessageDigest md = MessageDigest.getInstance(args[0]);

byte[] b = new byte[1024];
int bytesRead = context.in.read(b);
int bytesRead = context.getIn().read(b);
while (bytesRead != -1) {
md.update(b, 0, bytesRead);
bytesRead = System.in.read(b);
Expand All @@ -105,6 +106,7 @@ public static void nailMain(NGContext context)
buf.append(HEXCHARS[(result[i] >> 4) & 0x0f]);
buf.append(HEXCHARS[result[i] & 0x0f]);
}
context.out.println(buf);
context.getOut().println(buf);
context.getOut().flush();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public static void nailMain(final NGContext context) {
}
});

context.addHeartbeatListener(() -> context.out.print("H"));
context.addHeartbeatListener(() -> context.getOut().print("H"));

synchronized (lock) {
if (!shutdown.get()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ public static void nailMain(NGContext context) {
if (result == null) {
context.exit(1);
} else {
context.out.println(result);
context.getOut().println(result);
context.getOut().flush();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,11 @@ public static void nailMain(NGContext context) throws InterruptedException {
sharedStack.wait();
}
if (sharedStack.size() > 0) {
context.out.println(sharedStack.pop());
context.getOut().println(sharedStack.pop());
exitCode = 0;
}
}
context.getOut().flush();
context.exit(exitCode);
return;
}
Expand Down
2 changes: 1 addition & 1 deletion nailgun-server/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
<parent>
<groupId>com.facebook</groupId>
<artifactId>nailgun-all</artifactId>
<version>1.0.1</version>
<version>1.0.2-SNAPSHOT</version>
</parent>

<dependencies>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,10 @@ public class AliasManager {
private Map aliases;

/** Creates a new AliasManager, populating it with default Aliases. */
public AliasManager() {
public AliasManager(ClassLoader cl) {
aliases = new java.util.HashMap();

Properties props = new Properties();
ClassLoader cl = getClass().getClassLoader();
if (cl == null)
cl = ClassLoader.getSystemClassLoader(); // needed if nailgun classes are loaded in the boot
// classpath.
try (InputStream is =
cl.getResourceAsStream("com/facebook/nailgun/builtins/builtins.properties")) {
props.load(is);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package com.facebook.nailgun;

@FunctionalInterface
public interface NGClientListener {

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
package com.facebook.nailgun;

import java.io.ByteArrayInputStream;
import java.io.Closeable;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.EOFException;
Expand Down Expand Up @@ -48,7 +47,7 @@
* using underlying socket streams. Also handles client disconnect events based on client
* heartbeats.
*/
public class NGCommunicator implements Closeable {
public class NGCommunicator implements AutoCloseable {

private static final Logger LOG = Logger.getLogger(NGCommunicator.class.getName());
private final ExecutorService orchestratorExecutor;
Expand Down Expand Up @@ -383,6 +382,7 @@ private void stopOut() throws IOException {
if (outClosed) {
return;
}
out.flush();
outClosed = true;

LOG.log(Level.FINE, "Shutting down socket for output");
Expand Down
50 changes: 37 additions & 13 deletions nailgun-server/src/main/java/com/facebook/nailgun/NGConstants.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,12 @@

package com.facebook.nailgun;

import java.io.File;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Properties;
import java.util.logging.Logger;

/**
* Just a simple holder for various NailGun-related contants.
Expand All @@ -27,6 +31,8 @@
*/
public class NGConstants {

private static final Logger LOG = Logger.getLogger(NGConstants.class.getName());

/** The default NailGun port (2113) */
public static final int DEFAULT_PORT = 2113;
/** The exit code sent to clients if nail completed successfully */
Expand Down Expand Up @@ -63,9 +69,6 @@ public class NGConstants {
/** Chunk type marker for heartbeats sent to let the server know the client is still alive. */
public static final byte CHUNKTYPE_HEARTBEAT = 'H';

/** Server version number */
public static final String VERSION = getVersion();

/** Expected interval between heartbeats in milliseconds. */
public static final short HEARTBEAT_INTERVAL_MILLIS = 1000;

Expand All @@ -78,17 +81,38 @@ public class NGConstants {
/** Maximum chunk len sent from client. */
public static final short MAXIMUM_CHUNK_LENGTH = 2048;

private static String VERSION = null;

/** Loads the version number from a file generated by Maven. */
private static String getVersion() {
Properties props = new Properties();
try (InputStream is =
NGConstants.class.getResourceAsStream(
"/META-INF/maven/com.facebook/nailgun-server/pom.properties")) {
props.load(is);
} catch (Throwable e) {
// In static initialization context, outputting or logging an exception is dangerous
// It smells bad, but let's ignore it
public static String getVersion() throws MalformedURLException {
if (VERSION == null) {
Properties props = new Properties();
URL url =
NGServer.getInstance()
.getClassLoader()
.getResource("META-INF/maven/com.facebook/nailgun-server/pom.properties");
if (url == null) {
File file = new File("target/maven-archiver/pom.properties");
if (file.isFile()) {
url = file.toURI().toURL();
}
}
if (url == null) {
File file = new File("nailgun-server/target/maven-archiver/pom.properties");
if (file.isFile()) {
url = file.toURI().toURL();
}
}
LOG.info("get version info from " + url);
try (InputStream is = url.openStream()) {
props.load(is);
} catch (Throwable e) {
// In static initialization context, outputting or logging an exception is dangerous
// It smells bad, but let's ignore it
}
VERSION =
props.getProperty("version", System.getProperty("nailgun.server.version", "[UNKNOWN]"));
}
return props.getProperty("version", System.getProperty("nailgun.server.version", "[UNKNOWN]"));
return VERSION;
}
}
30 changes: 23 additions & 7 deletions nailgun-server/src/main/java/com/facebook/nailgun/NGContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -63,18 +63,22 @@ public class NGContext {
private String workingDirectory = null;

/** The client's stdin */
public InputStream in = null;
private InputStream in = null;

/** The client's stdout */
public PrintStream out = null;
private PrintStream out = null;

/** The client's stderr */
public PrintStream err = null;
private PrintStream err = null;

private NGCommunicator communicator = null;

/** Creates a new, empty NGContext */
public NGContext() {}
public NGContext(InputStream in, PrintStream out, PrintStream err) {
setIn(in);
setOut(out);
setErr(err);
}

public void setCommunicator(NGCommunicator comm) {
this.communicator = comm;
Expand Down Expand Up @@ -123,7 +127,7 @@ public String getWorkingDirectory() {
* @param in The {@link InputStream} to use as stdin for the current nail. This should be an
* InputStream that ultimately reads from {@link NGInputStream}.
*/
public void setIn(InputStream in) {
private void setIn(InputStream in) {
this.in = in;
if (!(System.in instanceof ThreadLocalInputStream)) {
throw new IllegalStateException("System.in should be set by nailgun.");
Expand All @@ -138,7 +142,7 @@ public void setIn(InputStream in) {
* @param out The {@link PrintStream} to use as stdout for the current nail. This should be a
* PrintStream that ultimately writes to {@link NGOutputStream}.
*/
public void setOut(PrintStream out) {
private void setOut(PrintStream out) {
this.out = out;
if (!(System.out instanceof ThreadLocalPrintStream)) {
throw new IllegalStateException("System.out should be set by nailgun.");
Expand All @@ -153,7 +157,7 @@ public void setOut(PrintStream out) {
* @param err The {@link PrintStream} to use as stderr for the current nail. This should be a
* PrintStream that ultimately writes to {@link NGOutputStream}.
*/
public void setErr(PrintStream err) {
private void setErr(PrintStream err) {
this.err = err;
if (!(System.err instanceof ThreadLocalPrintStream)) {
throw new IllegalStateException("System.err should be set by nailgun.");
Expand All @@ -162,6 +166,18 @@ public void setErr(PrintStream err) {
tls.init(err);
}

public InputStream getIn() {
return in;
}

public PrintStream getOut() {
return out;
}

public PrintStream getErr() {
return err;
}

void setEnv(Properties remoteEnvironment) {
this.remoteEnvironment = remoteEnvironment;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package com.facebook.nailgun;

@FunctionalInterface
public interface NGHeartbeatListener {

/**
Expand Down
Loading