Commit 98dff65a authored by Carlos Galindo's avatar Carlos Galindo
Browse files

Move jinterface from e-Knife/src to Maven.

parent 2f3d9abf
Loading
Loading
Loading
Loading
+3 −2
Original line number Diff line number Diff line
VERSION:=1.1.0
MVN_OPTIONS:=

.PHONY: all
all: beams
@@ -6,7 +7,7 @@ all: beams
.PHONY: release
release: beams
	rm -rf e-knife-$(VERSION) dist
	mvn clean package
	mvn $(MVN_OPTIONS) clean package
	mkdir -p e-knife-$(VERSION)/ebin
	cp e-Knife/target/e-Knife-$(VERSION)-jar-with-dependencies.jar e-knife-$(VERSION)/e-knife.jar
	cd e-knife-$(VERSION)/ebin/ && jar -xf ../e-knife.jar ast.beam ast.erl saver.beam saver.erl
@@ -15,7 +16,7 @@ release: beams

.PHONY: clean
clean:
	mvn clean
	mvn $(MVN_OPTIONS) clean
	rm -f e-Knife/src/main/resources/*.beam

.PHONY: beams
+23 −15
Original line number Diff line number Diff line
@@ -10,14 +10,14 @@ This project consists of three modules:

## Build

Requirements: Java ≥ 11, Erlang/OTP 24, Maven and Make.
Requirements: Java ≥ 17, Erlang/OTP 26, Maven and Make.

Just run `make release` to generate the zipped release `e-knife-VERSION.zip`.
You'll also find it unzipped in the `dist` folder, ready to be run.

## Run

Requirements: Java ≥ 11 and Erlang/OTP 24
Requirements: Java ≥ 17 and Erlang/OTP 26

The release version contains a jar file and a folder (`ebin`) with additional resources.
It is important to keep them in the same directory.
@@ -76,17 +76,25 @@ If not, see https://www.gnu.org/licenses.

## Troubleshooting

### I can't use/install Erlang/OTP 24
### I can't use/install Erlang/OTP 26

Part of the process requires some communication with an Erlang process. To
that end, the `jinterface` library must match the version of Erlang installed
in your machine. If your installation of Erlang is not OTP 24, you should replace
the bundled `jinterface` library with the one from your Erlang installation.
in your machine. If your installation of Erlang is not OTP 26, you should indicate
the version of `jinterface` that matches the version of Erlang in your system.

1. Locate the directory in which Erlang is installed (typically `/usr/lib/erlang`).
Other common locations are `/usr/local/lib/erlang` (admin-compiled), `~/.local/lib/erlang`
(user-compiled) and `/usr/local/Cellar/erlang/VERSION/lib/erlang` (Homebrew on macOS).
2. Enter the Erlang installation directory and look for a folder `lib`, and in it,
a folder called `jinterface-VERSION`.
3. When running `make` commands, use:
```bash
make MVN_OPTIONS="-Djinterface.version=VERSION -Djinterface.erlangRoot=ERLANG_INSTALLATION"
```
The default values are `VERSION=1.14` and `ERLANG_INSTALLATION=/usr/lib/erlang`.

Before running `make`, you must replace the package `com.ericsson.otp.erlang`
from `e-Knife/src/main/java` with the one that corresponds to your Erlang version.
You can either copy the files from your local installation (if available) or
download it from the OTP repository. After that, [rebuild the project](#build).
With this change, you can now build the project with past and future versions of Erlang.

#### Copy jinterface from existing installation

@@ -133,17 +141,17 @@ mv otp-OTP-$VERSION/$ji_path/$pkg $dest
rm -rf otp-OTP-$VERSION/
```

[credit]: https://stackoverflow.com/a/34326368
[so1]: https://stackoverflow.com/a/34326368

### UnsupportedClassVersionError: eknife/EKnife has been compiled by a more recent version of the Java Runtime

e-Knife requires at least Java 11. Your version may be lower. You can check it
e-Knife requires at least Java 17. Your version may be lower. You can check it
by running `java -version`. To install a more modern version, check the instructions
below, according to your operating system.

<details><summary>Linux</summary>

Search your package manager for a JDK (typically `openjdk`) with at least version 11.
Search your package manager for a JDK (typically `openjdk`) with at least version 17.
If you don't know how to search or install packages, you can check [pkgs.org][pkgs-jdk].

</details>
@@ -167,8 +175,8 @@ Download and install the Microsoft build (.msi) from [their site][w10-java-downl
### Cannot run program "erl": error=2, No such file or directory

You haven't installed Erlang, or it is not correctly setup to be available
on your `PATH`. Please install Erlang/OTP 24. If you install a different version,
you may need to follow [the instructions above](#i-cant-useinstall-erlangotp-24).
on your `PATH`. Please install Erlang/OTP 26. If you install a different version,
you may need to follow [the instructions above](#i-cant-useinstall-erlangotp-26).

<details><summary>Linux</summary>

@@ -192,5 +200,5 @@ You can download the Erlang/OTP installer from [Erlang Solutions][erlang-solutio

</details>

[pkgs-jdk]: https://pkgs.org/search/?q=erlang
[pkgs-erlang]: https://pkgs.org/search/?q=erlang
[erlang-solutions]: https://www.erlang-solutions.com/downloads
+12 −0
Original line number Diff line number Diff line
@@ -12,6 +12,11 @@
        <version>1.1.0</version>
    </parent>

    <properties>
        <jinterface.version>1.14</jinterface.version>
        <jinterface.erlangRoot>/usr/lib/erlang</jinterface.erlangRoot>
    </properties>

    <build>
        <plugins>
            <plugin>
@@ -56,6 +61,13 @@
            <artifactId>EDG</artifactId>
            <version>1.1.0</version>
        </dependency>
    	<dependency>
            <groupId>com.ericsson.otp</groupId>
            <artifactId>jinterface</artifactId>
            <scope>system</scope>
            <version>${jinterface.version}</version>
            <systemPath>${jinterface.erlangRoot}/lib/jinterface-${jinterface.version}/priv/OtpErlang.jar</systemPath>
        </dependency>
    </dependencies>

</project>
+0 −1551

File deleted.

Preview size limit exceeded, changes collapsed.

+0 −314
Original line number Diff line number Diff line
/*
 * %CopyrightBegin%
 *
 * Copyright Ericsson AB 2000-2016. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * %CopyrightEnd%
 */
package com.ericsson.otp.erlang;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.net.InetAddress;
import java.net.UnknownHostException;

/**
 * <p>
 * Represents an OTP node.
 * </p>
 *
 * <p>
 * About nodenames: Erlang nodenames consist of two components, an alivename and
 * a hostname separated by '@'. Additionally, there are two nodename formats:
 * short and long. Short names are of the form "alive@hostname", while long
 * names are of the form "alive@host.fully.qualified.domainname". Erlang has
 * special requirements regarding the use of the short and long formats, in
 * particular they cannot be mixed freely in a network of communicating nodes,
 * however Jinterface makes no distinction. See the Erlang documentation for
 * more information about nodenames.
 * </p>
 *
 * <p>
 * The constructors for the AbstractNode classes will create names exactly as
 * you provide them as long as the name contains '@'. If the string you provide
 * contains no '@', it will be treated as an alivename and the name of the local
 * host will be appended, resulting in a shortname. Nodenames longer than 255
 * characters will be truncated without warning.
 * </p>
 *
 * <p>
 * Upon initialization, this class attempts to read the file .erlang.cookie in
 * the user's home directory, and uses the trimmed first line of the file as the
 * default cookie by those constructors lacking a cookie argument. If for any
 * reason the file cannot be found or read, the default cookie will be set to
 * the empty string (""). The location of a user's home directory is determined
 * using the system property "user.home", which may not be automatically set on
 * all platforms.
 * </p>
 *
 * <p>
 * Instances of this class cannot be created directly, use one of the subclasses
 * instead.
 * </p>
 */
public class AbstractNode implements OtpTransportFactory {
    static String localHost = null;
    String node;
    String host;
    String alive;
    String cookie;
    static String defaultCookie = null;
    final OtpTransportFactory transportFactory;

    static final int NTYPE_R6 = 110; // 'n' post-r5, all nodes

    // Node capability flags
    static final int dFlagPublished = 1;
    static final int dFlagAtomCache = 2;
    static final int dFlagExtendedReferences = 4;
    static final int dFlagDistMonitor = 8;
    static final int dFlagFunTags = 0x10;
    static final int dFlagDistMonitorName = 0x20; // NOT USED
    static final int dFlagHiddenAtomCache = 0x40; // NOT SUPPORTED
    static final int dflagNewFunTags = 0x80;
    static final int dFlagExtendedPidsPorts = 0x100;
    static final int dFlagExportPtrTag = 0x200;
    static final int dFlagBitBinaries = 0x400;
    static final int dFlagNewFloats = 0x800;
    static final int dFlagUnicodeIo = 0x1000;
    static final int dFlagUtf8Atoms = 0x10000;
    static final int dFlagMapTag = 0x20000;
    static final int dFlagBigCreation = 0x40000;
    static final int dFlagHandshake23 = 0x1000000;
    static final int dFlagUnlinkId = 0x2000000;
    static final long dFlagV4PidsRefs = 0x4L << 32;

    int ntype = NTYPE_R6;
    int proto = 0; // tcp/ip
    int distHigh = 6;
    int distLow = 5; // Cannot talk to nodes before R6
    int creation = 0;
    long flags = dFlagExtendedReferences | dFlagExtendedPidsPorts
            | dFlagBitBinaries | dFlagNewFloats | dFlagFunTags
            | dflagNewFunTags | dFlagUtf8Atoms | dFlagMapTag
            | dFlagExportPtrTag
	    | dFlagBigCreation
            | dFlagHandshake23
            | dFlagUnlinkId
            | dFlagV4PidsRefs;

    /* initialize hostname and default cookie */
    static {
        try {
            localHost = InetAddress.getLocalHost().getHostName();
            /*
             * Make sure it's a short name, i.e. strip of everything after first
             * '.'
             */
            final int dot = localHost.indexOf(".");
            if (dot != -1) {
                localHost = localHost.substring(0, dot);
            }
        } catch (final UnknownHostException e) {
            localHost = "localhost";
        }

        final String homeDir = getHomeDir();
        final String dotCookieFilename = homeDir + File.separator
                + ".erlang.cookie";
        BufferedReader br = null;

        try {
            final File dotCookieFile = new File(dotCookieFilename);

            br = new BufferedReader(new FileReader(dotCookieFile));
            final String line = br.readLine();
            if (line == null) {
                defaultCookie = "";
            } else {
                defaultCookie = line.trim();
            }
        } catch (final IOException e) {
            defaultCookie = "";
        } finally {
            try {
                if (br != null) {
                    br.close();
                }
            } catch (final IOException e) {
            }
        }
    }

    protected AbstractNode(final OtpTransportFactory transportFactory) {
        this.transportFactory = transportFactory;
    }

    /**
     * Create a node with the given name and default cookie and transport
     * factory.
     */
    protected AbstractNode(final String node) {
        this(node, defaultCookie, new OtpSocketTransportFactory());
    }

    /**
     * Create a node with the given name, transport factory and the default
     * cookie.
     */
    protected AbstractNode(final String node,
            final OtpTransportFactory transportFactory) {
        this(node, defaultCookie, transportFactory);
    }

    /**
     * Create a node with the given name, cookie and default transport factory.
     */
    protected AbstractNode(final String name, final String cookie) {
        this(name, cookie, new OtpSocketTransportFactory());
    }

    /**
     * Create a node with the given name, cookie and transport factory.
     */
    protected AbstractNode(final String name, final String cookie,
            final OtpTransportFactory transportFactory) {
        this.cookie = cookie;
        this.transportFactory = transportFactory;

        final int i = name.indexOf('@', 0);
        if (i < 0) {
            alive = name;
            host = localHost;
        } else {
            alive = name.substring(0, i);
            host = name.substring(i + 1, name.length());
        }

        if (alive.length() > 0xff) {
            alive = alive.substring(0, 0xff);
        }

        node = alive + "@" + host;
    }

    /**
     * Get the name of this node.
     *
     * @return the name of the node represented by this object.
     */
    public String node() {
        return node;
    }

    /**
     * Get the hostname part of the nodename. Nodenames are composed of two
     * parts, an alivename and a hostname, separated by '@'. This method returns
     * the part of the nodename following the '@'.
     *
     * @return the hostname component of the nodename.
     */
    public String host() {
        return host;
    }

    /**
     * Get the alivename part of the hostname. Nodenames are composed of two
     * parts, an alivename and a hostname, separated by '@'. This method returns
     * the part of the nodename preceding the '@'.
     *
     * @return the alivename component of the nodename.
     */
    public String alive() {
        return alive;
    }

    /**
     * Get the authorization cookie used by this node.
     *
     * @return the authorization cookie used by this node.
     */
    public String cookie() {
        return cookie;
    }

    // package scope
    int type() {
        return ntype;
    }

    // package scope
    int distHigh() {
        return distHigh;
    }

    // package scope
    int distLow() {
        return distLow;
    }

    // package scope: useless information?
    int proto() {
        return proto;
    }

    // package scope
    int creation() {
        return creation;
    }

    /**
     * Set the authorization cookie used by this node.
     *
     * @return the previous authorization cookie used by this node.
     */
    public String setCookie(final String cookie) {
        final String prev = this.cookie;
        this.cookie = cookie;
        return prev;
    }

    @Override
    public String toString() {
        return node();
    }

    private static String getHomeDir() {
        final String home = System.getProperty("user.home");
        if (System.getProperty("os.name").toLowerCase().contains("windows")) {
            final String drive = System.getenv("HOMEDRIVE");
            final String path = System.getenv("HOMEPATH");
            return drive != null && path != null ? drive + path : home;
        }
        return home;
    }

    public OtpTransport createTransport(final String addr, final int port)
            throws IOException {
        return transportFactory.createTransport(addr, port);
    }

    public OtpTransport createTransport(final InetAddress addr, final int port)
            throws IOException {
        return transportFactory.createTransport(addr, port);
    }

    public OtpServerTransport createServerTransport(final int port)
            throws IOException {
        return transportFactory.createServerTransport(port);
    }
}
Loading