Using Neovim as a Java IDE

I first learned Vim in university and, since then, it has been a welcome companion for the majority of my software engineering career. Working with Python and Go programs felt natural with Vim and I was always felt productive. Yet Java was always a different beast. Whenever an opportunity to work with Java came up, I would inevitably try Vim for a while, but fall back to IntelliJ and the IdeaVim plugin to take advantage of the rich language features a full-featured IDE can give you. ...

December 14, 2022 · 20 min · Kevin Sookocheff

Java For The Experienced Beginner

Java was the first programming language I was taught at University, and the language I used for the first decade of my career. It continues to be a reliable companion throughout my software development career. Unfortunately, not having developed with Java professionally for several years, I’ve found there are many aspects of the modern Java language that I’m simply not familiar with. To rectify this, I’ve collected the major improvements to the language beginning with Java 8, combined with a short explanation of how they work and how to use them. It assumes you know Java, but don’t really know Java. Hopefully, it can take you from experienced beginner to just plain experienced again. ...

April 27, 2022 · 43 min · Kevin Sookocheff

Behaviour Parameterization

One of the core features of modern Java is lambda expressions. Introduced in Java 8, lambdas provide concise syntax allowing the deferred execution of a block of code. Put a different way, lambdas allow us to pass behaviour as a method parameter. When the method executes, the lambda expression is run. This capability is often referred to as behaviour parameterization. Behaviour parameterization can be achieved in a number of ways, of which lambda expressions are usually the most convenient, and they are definitely the most concise. But what is behaviour parameterization, and why would we want to use it? To motivate this discussion, let’s work through a real-world example of filtering a list of items according to some criteria. More concretely, let’s investigate the problem filtering a list of students to find the ones with the best grades. ...

April 26, 2022 · 6 min · Kevin Sookocheff

Augmenting Spring Data REST with Additional Endpoints

Spring Data REST combines the features of Spring Data with Spring HATEOAS to make it easy to build hypermedia-driven REST APIs on top of Spring Data repositories. The basic functionality provided out of the box creates and exposes simple REST endpoints for performing CRUD operations on Spring Data repositories. For a lot of use cases, this is entirely enough functionality to meet your needs. In other cases, you need to extend the REST API to include additional functionality that isn’t provided by Spring Data REST and it can be difficult to determine exactly how to do this. In this blog post, I show you how to augment a Spring Data REST API with additional endpoints to turn a basic CRUD API into a full featured web service. ...

May 19, 2021 · 3 min · Kevin Sookocheff

Understanding Spring's Environment Abstraction

When working with Spring Boot, some auto-configuration can happen seemingly by magic. For example, in a traditional Spring application with Java-based configuration, you might configure an H2 database using the following block of code to set the type of the database and run some scripts to initialize it: @Bean public DataSource dataSource() { return new EmbeddedDataSourceBuilder() .setType(H2) .addScript("taco_schema.sql") .addScripts("user_data.sql", "ingredient_data.sql") .build(); } With Spring Boot, you can remove this entire block of code and use auto-configuration. If the H2 dependency is available in the classpath, Spring Boot will automatically create the DataSource and add it as a Bean to Spring’s application context and apply SQL scripts as long as they are named schema.sql or data.sql. Spring Boot does this auth-configuration by leveraging the Spring environment that collects the set of properties available to a Java application and uses those properties to configure beans in Spring’s application context. The following figure from Spring in Action shows how the Spring environment is generated by some of the available configuration property sources available in a typical Java application. ...

May 27, 2020 · 3 min · Kevin Sookocheff

Building a Modern Java Web App with Spring Boot

I’m a fan of Java. If you haven’t given it a chance in a while, you may not have noticed that, Java has quietly been adopting some of the best practices that make dynamic and functional languages so appealing, without sacrificing the many hundred person-years of effort that have made Java and the JVM a world-class development environment. Java is still one the world’s most popular programming languages with roughly 9 million programmers using Java. It doesn’t make sense to ignore all of that history and development effort because of a niggling feeling that Java is a little too verbose, or that XML is so last year. ...

May 20, 2020 · 10 min · Kevin Sookocheff

Inversion of Control, Dependency Injection, and the Spring IoC Container

Inversion of Control (IoC), also known as Dependency Injection (DI), allows an object to define their dependencies as constructor arguments (strictly speaking, you can set these dependencies as properties, but the examples I will use today are constructor-based). This is the inverse of the object itself controlling the instantiation or location of its dependencies, hence the name Inversion of Control. Let’s look at an example from Stackoverflow using a text editor with a spell checking component: ...

May 14, 2020 · 5 min · Kevin Sookocheff

What is OSGi? Microservices for the JVM

As someone who is being recently re-introduced to the Java platform, there are a lot of components and pieces to become familiar with. One of those pieces is OSGi, a service registry and deployment specification for Java apps. The OSGi specification describes a complete environment for running Java applications as components. Each component is a bundle that can be remotely installed, started, stopped, updated or uninstalled. In addition, OSGi provides a service registry that allows existing services to detect the addition or removal of other services and adapt accordingly. ...

October 11, 2016 · 2 min · Kevin Sookocheff

Testing a Producer-Consumer Design using a CyclicBarrier

Testing concurrent objects can be challenging. One particular pattern that is useful for objects used in producer-consumer designs is to ensure that everything put in to a shared concurrent queue by a producer is correctly executed by consumers. ...

July 21, 2016 · 3 min · Kevin Sookocheff

Server-to-server OAuth with the Google OAuth Client Library for Java

This post describes how to validate a JWT token using the Google OAuth library for making server-to-server OAuth requests. First, there is a prerequisite of being able to read a key file from your local file system. This key file is obtained from the system that you wish to authorize against and contains the private-key pair authorizing your server with the other system. /** * Return private key from a file. Must be a valid PEM file with PKCS#8 encoding standard. * * @return a private key */ PrivateKey loadPrivateKey(File keyFile) throws IOException, NoSuchAlgorithmException, InvalidKeySpecException { byte[] content = Files.toByteArray(keyFile); PKCS8EncodedKeySpec ks = new PKCS8EncodedKeySpec(content); return KeyFactory.getInstance("RSA").generatePrivate(ks); } Now, assuming we have a valid private key, authenticating with an OAuth end-point using a JWT token is a matter of mapping the JWT token properties with the correct GoogleCredential methods. When GoogleCredential calls the API to obtain a new access token, it converts the methods set on the credential to the correct JWT token properties according to the following table. ...

May 12, 2016 · 2 min · Kevin Sookocheff