Thursday 29 June 2023

How to install Javascript Engine in GraalVM

I've already mentioned it in the reference below, but just for easy reference for me.

The JavaScript engine no longer comes installed by default in the GraalVM. You have to install it as follows:

% ./gu install js
Downloading: Artifacts catalog from gds.oracle.com
Skipping ULN EE channels, no username provided.
Downloading: Component catalog from www.graalvm.org
Processing Component: Graal.js
Processing Component: ICU4J
Processing Component: TRegex
Additional Components are required:
    ICU4J (org.graalvm.icu4j, version 23.0.0), required by: Graal.js (org.graalvm.js)
    TRegex (org.graalvm.regex, version 23.0.0), required by: Graal.js (org.graalvm.js)
Downloading: Component js: Graal.js from gds.oracle.com
Downloading: Component org.graalvm.icu4j: ICU4J from gds.oracle.com
Downloading: Component org.graalvm.regex: TRegex from gds.oracle.com
Installing new component: TRegex (org.graalvm.regex, version 23.0.0)
Installing new component: ICU4J (org.graalvm.icu4j, version 23.0.0)
Installing new component: Graal.js (org.graalvm.js, version 23.0.0)

References

Upgrading to Jakarta 10
https://randomthoughtsonjavaprogramming.blogspot.com/2022/12/upgrading-to-jakarta-10.html

Thursday 15 June 2023

NullPointerException in Stream

Just a small blurb. I recently helped a colleague who got a NullPointerException whilst dealing with streams.

It looked very innocuous at first.

  @Test(expectedExceptions = NullPointerException.class)
  public void testStream() {
    Optional<String> first = Stream.of(new Person(null, "mrBear"), new Person("George", "Boole"),
            new Person("Ada", "Lovelace"),
            new Person("Tim", "Berners-Lee"), new Person("James", "Gosling"), new Person("Linus", "Torvalds"))
        .map(Person::firstName).findFirst();
  }

Well, it turns out that, if you have a list with null-values, and you try a findFirst() on the stream, and the first value is null, Java will attempt to wrap the null-value into an Optional, which is not allowed.

You get a fancy stacktrace, that shows just this:

java.lang.NullPointerException
	at java.base/java.util.Objects.requireNonNull(Objects.java:208)
	at java.base/java.util.Optional.of(Optional.java:113)
	at java.base/java.util.stream.FindOps$FindSink$OfRef.get(FindOps.java:194)
	at java.base/java.util.stream.FindOps$FindSink$OfRef.get(FindOps.java:191)
	at java.base/java.util.stream.FindOps$FindOp.evaluateSequential(FindOps.java:150)
	at java.base/java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234)
	at java.base/java.util.stream.ReferencePipeline.findFirst(ReferencePipeline.java:647)
	at com.mrbear.streams.NullPointerExceptionTest.testStream(NullPointerExceptionTest.java:18)
	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77)
	at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.base/java.lang.reflect.Method.invoke(Method.java:568)
	at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:124)
	at org.testng.internal.Invoker.invokeMethod(Invoker.java:571)
	at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:707)
	at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:979)
	at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:125)
	at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:109)
	at org.testng.TestRunner.privateRun(TestRunner.java:648)
	at org.testng.TestRunner.run(TestRunner.java:505)
	at org.testng.SuiteRunner.runTest(SuiteRunner.java:455)
	at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:450)
	at org.testng.SuiteRunner.privateRun(SuiteRunner.java:415)
	at org.testng.SuiteRunner.run(SuiteRunner.java:364)
	at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
	at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:84)
	at org.testng.TestNG.runSuitesSequentially(TestNG.java:1187)
	at org.testng.TestNG.runSuitesLocally(TestNG.java:1116)
	at org.testng.TestNG.runSuites(TestNG.java:1028)
	at org.testng.TestNG.run(TestNG.java:996)
	at com.intellij.rt.testng.IDEARemoteTestNG.run(IDEARemoteTestNG.java:66)
	at com.intellij.rt.testng.RemoteTestNGStarter.main(RemoteTestNGStarter.java:105)

Just something to keep in mind.

Friday 9 June 2023

Podman

Podman is a drop-in replacement for Docker, at least that's what I hear.

From the little experience I currently have, the commands on the command line seem to be almost equivalent if not in syntax at least in intention to Docker.

Installing Podman

mrbear % brew install podman

Running Podman

podman machine init
Downloading VM image: fedora-coreos-38.20230514.2.0-qemu.aarch64.qcow2.xz: done
Extracting compressed file
Image resized.
Machine init complete
To start your machine run:

podman machine start
podman machine start
Starting machine "podman-machine-default"
Waiting for VM ...
Mounting volume... /Users:/Users
Mounting volume... /private:/private
Mounting volume... /var/folders:/var/folders

This machine is currently configured in rootless mode. If your containers
require root permissions (e.g. ports < 1024), or if you run into compatibility
issues with non-podman clients, you can switch using the following command:

podman machine set --rootful

API forwarding listening on: /Users/mrbear/.local/share/containers/podman/machine/qemu/podman.sock

The system helper service is not installed; the default Docker API socket
address can't be used by podman. If you would like to install it run the
following commands:

sudo /opt/homebrew/Cellar/podman/4.5.0/bin/podman-mac-helper install
podman machine stop; podman machine start

You can still connect Docker API clients by setting DOCKER_HOST using the
following command in your terminal session:

export DOCKER_HOST='unix:///Users/mrbear/.local/share/containers/podman/machine/qemu/podman.sock'

Machine "podman-machine-default" started successfully

Verify Podman

podman info

A GUI

I will be trying out Podman Desktop. Under Docker I was very used to Portainer.

Networking

Non-root containers use slirp4netns and have no routable IP address. Root containers use netavark.

Glossary of commands

podman machine init
initializes the machine
podman machine start
starts the machine
podman info
verify installation
podman ps [-a]
list creating and running containers
podman pull <container url>
pull an image, for example "podman pull docker.io/library/postgres:14" or simply "podman pull mariadb"
podman images
show all images
podman rmi <image>
remove an image, for example "podman rmi 0c1418f4071b", where the id can be ascertained using "podman images"
podman run ...
run a container, for example:
  • podman run --detach --tty --name webserver --publish 8080:80 quay.io/libpod/banner
  • podman run --name postgres --detach --publish 5432:5432 --env POSTGRES_USER=admin --env POSTGRES_PASSWORD=Passw0rd postgis/postgis
  • podman run --name postgres --detach --publish 5432:5432 --env POSTGRES_USER=admin --env POSTGRES_PASSWORD=Passw0rd docker.io/library/postgres:latest
  • podman run --detach --name mariadb --publish 3306:3306 --env MARIADB_USER=root --env MARIADB_PASSWORD=Passw0rd --env MARIADB_ROOT_PASSWORD=Passw0rd docker.io/library/mariadb
--detach/-d
run the container in the background and print the new container ID. The default is false.
--tty/-t
Allocate a pseudo-TTY. The default is false.
--publish/-p [[ip:][hostPort]:]containerPort[/protocol]¶
Publish a container’s port, or range of ports, to the host.
--env/-e
Set environment variables.
--name
Assign a name to the container.
podman ps
lists the running containers on the system. Use the --all flag to view all the containers information.
podman attach <container_id>
sttached tty to a running container
podman inspect -l
metadata and details about the running containers
podman logs <container_id>
log container logs
podman top <container_id>
show processes running in the container
podman start <container_id>
start a container
podman stop <container_id>
stop a container
podman rm <container_id>
remove a container

I'll add to this as I get more information.

References

Podman
https://podman.io/
Podman - Installation
https://podman.io/docs/installation
TechViewLeo - How To Run PostgreSQL 14|13 in Podman Container
https://techviewleo.com/how-to-run-postgresql-in-podman-container/?utm_content=cmp-true
podman desktop
https://podman-desktop.io/
ImaginaryCloud - Podman vs Docker: What are the differences?
https://www.imaginarycloud.com/blog/podman-vs-docker/
Portainer
https://www.portainer.io/