{"@attributes":{"version":"2.0"},"channel":{"title":"Java on Kevin Sookocheff","link":"https:\/\/sookocheff.com\/tags\/java\/","description":"Recent content in Java on Kevin Sookocheff","generator":"Hugo","language":"en-us","lastBuildDate":"Wed, 14 Dec 2022 09:22:09 -0600","item":[{"title":"Using Neovim as a Java IDE","link":"https:\/\/sookocheff.com\/post\/vim\/neovim-java-ide\/","pubDate":"Wed, 14 Dec 2022 09:22:09 -0600","guid":"https:\/\/sookocheff.com\/post\/vim\/neovim-java-ide\/","description":"<p>I first learned Vim in university and, since then, it has been a welcome\ncompanion for the majority of my software engineering career. Working with\nPython and Go programs felt natural with Vim and I was always felt productive.\nYet Java was always a different beast. Whenever an opportunity to work with\nJava came up, I would inevitably try Vim for a while, but fall back to IntelliJ\nand the IdeaVim plugin to take advantage of the rich language features a\nfull-featured IDE can give you.<\/p>"},{"title":"Java For The Experienced Beginner","link":"https:\/\/sookocheff.com\/post\/java\/java-for-the-experienced-beginner\/","pubDate":"Wed, 27 Apr 2022 08:31:12 -0400","guid":"https:\/\/sookocheff.com\/post\/java\/java-for-the-experienced-beginner\/","description":"<p>Java was the first programming language I was taught at University, and\nthe language I used for the first decade of my career. It continues to be\na reliable companion throughout my software development career.\nUnfortunately, not having developed with Java professionally for several\nyears, I\u2019ve found there are many aspects of the modern Java language that\nI\u2019m simply not familiar with. To rectify this, I\u2019ve collected the major\nimprovements to the language beginning with Java 8, combined with a short\nexplanation of how they work and how to use them. It assumes you know\nJava, but don\u2019t really <em>know<\/em> Java. Hopefully, it can take you from\nexperienced beginner to just plain experienced again.<\/p>"},{"title":"Behaviour Parameterization","link":"https:\/\/sookocheff.com\/post\/design-patterns\/behaviour-parameterization\/","pubDate":"Tue, 26 Apr 2022 07:37:12 -0400","guid":"https:\/\/sookocheff.com\/post\/design-patterns\/behaviour-parameterization\/","description":"<p>One of the core features of modern Java is lambda expressions. Introduced\nin Java 8, lambdas provide concise syntax allowing the deferred execution\nof a block of code. Put a different way, lambdas allow us to pass\nbehaviour as a method parameter. When the method executes, the lambda\nexpression is run. This capability is often referred to as <em>behaviour\nparameterization<\/em>.<\/p>\n<p>Behaviour parameterization can be achieved in a number of ways, of which\nlambda expressions are usually the most convenient, and they are\ndefinitely the most concise. But what is behaviour parameterization, and\nwhy would we want to use it? To motivate this discussion, let\u2019s work\nthrough a real-world example of filtering a list of items according to\nsome criteria. More concretely, let\u2019s investigate the problem filtering\na list of students to find the ones with the best grades.<\/p>"},{"title":"Augmenting Spring Data REST with Additional Endpoints","link":"https:\/\/sookocheff.com\/post\/java\/augmenting-spring-data-rest-with-additional-endpoints\/","pubDate":"Wed, 19 May 2021 08:01:52 -0600","guid":"https:\/\/sookocheff.com\/post\/java\/augmenting-spring-data-rest-with-additional-endpoints\/","description":"<p>Spring Data REST combines the features of <a href=\"https:\/\/spring.io\/projects\/spring-data\">Spring\nData<\/a> with <a href=\"https:\/\/spring.io\/projects\/spring-hateoas\">Spring\nHATEOAS<\/a> to make it easy to\nbuild hypermedia-driven REST APIs on top of Spring Data repositories. The\nbasic functionality provided out of the box creates and exposes simple\nREST endpoints for performing CRUD operations on Spring Data repositories.\nFor a lot of use cases, this is entirely enough functionality to meet your\nneeds. In other cases, you need to extend the REST API to include\nadditional functionality that isn\u2019t provided by Spring Data REST and it\ncan be difficult to determine exactly how to do this. In this blog post,\nI show you how to augment a Spring Data REST API with additional endpoints\nto turn a basic CRUD API into a full featured web service.<\/p>"},{"title":"Understanding Spring's Environment Abstraction","link":"https:\/\/sookocheff.com\/post\/java\/understanding-springs-environment-abstraction\/","pubDate":"Wed, 27 May 2020 09:57:12 -0600","guid":"https:\/\/sookocheff.com\/post\/java\/understanding-springs-environment-abstraction\/","description":"<p>When working with Spring Boot, some auto-configuration can happen seemingly by\nmagic. For example, in a traditional Spring application with Java-based\nconfiguration, you might configure an H2 database using the following block of\ncode to set the type of the database and run some scripts to initialize it:<\/p>\n<div class=\"highlight\"><pre tabindex=\"0\" class=\"chroma\"><code class=\"language-java\" data-lang=\"java\"><span class=\"line\"><span class=\"cl\"><span class=\"nd\">@Bean<\/span><span class=\"w\">\n<\/span><\/span><\/span><span class=\"line\"><span class=\"cl\"><span class=\"kd\">public<\/span><span class=\"w\"> <\/span><span class=\"n\">DataSource<\/span><span class=\"w\"> <\/span><span class=\"nf\">dataSource<\/span><span class=\"p\">()<\/span><span class=\"w\"> <\/span><span class=\"p\">{<\/span><span class=\"w\">\n<\/span><\/span><\/span><span class=\"line\"><span class=\"cl\"><span class=\"w\">    <\/span><span class=\"k\">return<\/span><span class=\"w\"> <\/span><span class=\"k\">new<\/span><span class=\"w\"> <\/span><span class=\"n\">EmbeddedDataSourceBuilder<\/span><span class=\"p\">()<\/span><span class=\"w\">\n<\/span><\/span><\/span><span class=\"line\"><span class=\"cl\"><span class=\"w\">        <\/span><span class=\"p\">.<\/span><span class=\"na\">setType<\/span><span class=\"p\">(<\/span><span class=\"n\">H2<\/span><span class=\"p\">)<\/span><span class=\"w\">\n<\/span><\/span><\/span><span class=\"line\"><span class=\"cl\"><span class=\"w\">        <\/span><span class=\"p\">.<\/span><span class=\"na\">addScript<\/span><span class=\"p\">(<\/span><span class=\"s\">&#34;taco_schema.sql&#34;<\/span><span class=\"p\">)<\/span><span class=\"w\">\n<\/span><\/span><\/span><span class=\"line\"><span class=\"cl\"><span class=\"w\">        <\/span><span class=\"p\">.<\/span><span class=\"na\">addScripts<\/span><span class=\"p\">(<\/span><span class=\"s\">&#34;user_data.sql&#34;<\/span><span class=\"p\">,<\/span><span class=\"w\"> <\/span><span class=\"s\">&#34;ingredient_data.sql&#34;<\/span><span class=\"p\">)<\/span><span class=\"w\">\n<\/span><\/span><\/span><span class=\"line\"><span class=\"cl\"><span class=\"w\">        <\/span><span class=\"p\">.<\/span><span class=\"na\">build<\/span><span class=\"p\">();<\/span><span class=\"w\">\n<\/span><\/span><\/span><span class=\"line\"><span class=\"cl\"><span class=\"p\">}<\/span><span class=\"w\">\n<\/span><\/span><\/span><\/code><\/pre><\/div><p>With Spring Boot, you can remove this entire block of code and use\nauto-configuration. If the H2 dependency is available in the classpath, Spring\nBoot will automatically create the <code>DataSource<\/code> and add it as a <code>Bean<\/code> to\nSpring&rsquo;s application context and apply SQL scripts as long as they are named\n<code>schema.sql<\/code> or <code>data.sql<\/code>. Spring Boot does this auth-configuration by\nleveraging the Spring environment that collects the set of properties available\nto a Java application and uses those properties to configure beans in Spring&rsquo;s\napplication context. The following figure from <a href=\"https:\/\/www.manning.com\/books\/spring-in-action-fifth-edition\">Spring in\nAction<\/a> shows how\nthe Spring environment is generated by some of the available configuration\nproperty sources available in a typical Java application.<\/p>"},{"title":"Building a Modern Java Web App with Spring Boot","link":"https:\/\/sookocheff.com\/post\/java\/building-a-modern-java-web-app-with-spring-boot\/","pubDate":"Wed, 20 May 2020 09:13:12 -0600","guid":"https:\/\/sookocheff.com\/post\/java\/building-a-modern-java-web-app-with-spring-boot\/","description":"<p>I&rsquo;m a <a href=\"https:\/\/sookocheff.com\/post\/java\/why-java\/\">fan of Java<\/a>. If you haven&rsquo;t\ngiven it a chance in a while, you may not have noticed that, Java has quietly\nbeen adopting some of the best practices that make dynamic and functional\nlanguages so appealing, without sacrificing the many hundred person-years of\neffort that have made Java and the JVM a world-class development environment.\nJava is still one the world\u2019s <a href=\"https:\/\/www.tiobe.com\/tiobe-index\/\">most popular programming\nlanguages<\/a> with roughly 9 million\nprogrammers using Java. It doesn\u2019t make sense to ignore all of that history and\ndevelopment effort because of a niggling feeling that Java is a little too\nverbose, or that XML is so last year.<\/p>"},{"title":"Inversion of Control, Dependency Injection, and the Spring IoC Container","link":"https:\/\/sookocheff.com\/post\/java\/inversion-of-control-and-the-spring-ioc-container\/","pubDate":"Thu, 14 May 2020 09:13:12 -0600","guid":"https:\/\/sookocheff.com\/post\/java\/inversion-of-control-and-the-spring-ioc-container\/","description":"<p>Inversion of Control (IoC), also known as Dependency Injection (DI), allows an\nobject to define their dependencies as constructor arguments (strictly speaking,\nyou can set these dependencies as properties, but the examples I will use today\nare constructor-based). This is the inverse of the object itself controlling the\ninstantiation or location of its dependencies, hence the name <em>Inversion of Control<\/em>.<\/p>\n<p>Let&rsquo;s look at an example from\n<a href=\"https:\/\/stackoverflow.com\/questions\/3058\/what-is-inversion-of-control\">Stackoverflow<\/a>\nusing a text editor with a spell checking component:<\/p>"},{"title":"What is OSGi? Microservices for the JVM","link":"https:\/\/sookocheff.com\/post\/java\/what-is-osgi\/","pubDate":"Tue, 11 Oct 2016 11:34:28 -0600","guid":"https:\/\/sookocheff.com\/post\/java\/what-is-osgi\/","description":"<p>As someone who is being recently re-introduced to the Java platform, there\nare a lot of components and pieces to become familiar with. One of those\npieces is OSGi, a service registry and deployment specification for Java\napps.<\/p>\n<p>The OSGi specification describes a complete environment for running Java\napplications as components. Each component is a bundle that can be\nremotely installed, started, stopped, updated or uninstalled. In addition,\nOSGi provides a service registry that allows existing services to detect\nthe addition or removal of other services and adapt accordingly.<\/p>"},{"title":"Testing a Producer-Consumer Design using a CyclicBarrier","link":"https:\/\/sookocheff.com\/post\/java\/testing-producer-consumers-with-cyclic-barrier\/","pubDate":"Thu, 21 Jul 2016 13:57:12 -0600","guid":"https:\/\/sookocheff.com\/post\/java\/testing-producer-consumers-with-cyclic-barrier\/","description":"<p>Testing concurrent objects can be challenging. One particular pattern that\nis useful for objects used in producer-consumer designs is to ensure that\neverything put in to a shared concurrent queue by a producer is correctly\nexecuted by consumers.<\/p>"},{"title":"Server-to-server OAuth with the Google OAuth Client Library for Java","link":"https:\/\/sookocheff.com\/post\/oauth\/server-to-server-oauth-with-java\/","pubDate":"Thu, 12 May 2016 13:27:12 -0600","guid":"https:\/\/sookocheff.com\/post\/oauth\/server-to-server-oauth-with-java\/","description":"<p>This post describes how to validate a JWT token using the Google OAuth\nlibrary for making server-to-server OAuth requests.<\/p>\n<p>First, there is a prerequisite of being able to read a key file from your\nlocal file system. This key file is obtained from the system that you wish\nto authorize against and contains the private-key pair authorizing your\nserver with the other system.<\/p>\n<div class=\"highlight\"><pre tabindex=\"0\" class=\"chroma\"><code class=\"language-java\" data-lang=\"java\"><span class=\"line\"><span class=\"cl\"><span class=\"cm\">\/**\n<\/span><\/span><\/span><span class=\"line\"><span class=\"cl\"><span class=\"cm\"> * Return private key from a file. Must be a valid PEM file with PKCS#8 encoding standard.\n<\/span><\/span><\/span><span class=\"line\"><span class=\"cl\"><span class=\"cm\"> *\n<\/span><\/span><\/span><span class=\"line\"><span class=\"cl\"><span class=\"cm\"> * @return a private key\n<\/span><\/span><\/span><span class=\"line\"><span class=\"cl\"><span class=\"cm\"> *\/<\/span><span class=\"w\">\n<\/span><\/span><\/span><span class=\"line\"><span class=\"cl\"><span class=\"n\">PrivateKey<\/span><span class=\"w\"> <\/span><span class=\"nf\">loadPrivateKey<\/span><span class=\"p\">(<\/span><span class=\"n\">File<\/span><span class=\"w\"> <\/span><span class=\"n\">keyFile<\/span><span class=\"p\">)<\/span><span class=\"w\"> <\/span><span class=\"kd\">throws<\/span><span class=\"w\"> <\/span><span class=\"n\">IOException<\/span><span class=\"p\">,<\/span><span class=\"w\"> <\/span><span class=\"n\">NoSuchAlgorithmException<\/span><span class=\"p\">,<\/span><span class=\"w\"> <\/span><span class=\"n\">InvalidKeySpecException<\/span><span class=\"w\"> <\/span><span class=\"p\">{<\/span><span class=\"w\">\n<\/span><\/span><\/span><span class=\"line\"><span class=\"cl\"><span class=\"w\">  <\/span><span class=\"kt\">byte<\/span><span class=\"o\">[]<\/span><span class=\"w\"> <\/span><span class=\"n\">content<\/span><span class=\"w\"> <\/span><span class=\"o\">=<\/span><span class=\"w\"> <\/span><span class=\"n\">Files<\/span><span class=\"p\">.<\/span><span class=\"na\">toByteArray<\/span><span class=\"p\">(<\/span><span class=\"n\">keyFile<\/span><span class=\"p\">);<\/span><span class=\"w\">\n<\/span><\/span><\/span><span class=\"line\"><span class=\"cl\"><span class=\"w\">  <\/span><span class=\"n\">PKCS8EncodedKeySpec<\/span><span class=\"w\"> <\/span><span class=\"n\">ks<\/span><span class=\"w\"> <\/span><span class=\"o\">=<\/span><span class=\"w\"> <\/span><span class=\"k\">new<\/span><span class=\"w\"> <\/span><span class=\"n\">PKCS8EncodedKeySpec<\/span><span class=\"p\">(<\/span><span class=\"n\">content<\/span><span class=\"p\">);<\/span><span class=\"w\">\n<\/span><\/span><\/span><span class=\"line\"><span class=\"cl\"><span class=\"w\">  <\/span><span class=\"k\">return<\/span><span class=\"w\"> <\/span><span class=\"n\">KeyFactory<\/span><span class=\"p\">.<\/span><span class=\"na\">getInstance<\/span><span class=\"p\">(<\/span><span class=\"s\">&#34;RSA&#34;<\/span><span class=\"p\">).<\/span><span class=\"na\">generatePrivate<\/span><span class=\"p\">(<\/span><span class=\"n\">ks<\/span><span class=\"p\">);<\/span><span class=\"w\">\n<\/span><\/span><\/span><span class=\"line\"><span class=\"cl\"><span class=\"p\">}<\/span><span class=\"w\">\n<\/span><\/span><\/span><\/code><\/pre><\/div><p>Now, assuming we have a valid private key, authenticating with an OAuth\nend-point using a JWT token is a matter of mapping the JWT token\nproperties with the correct GoogleCredential methods. When\nGoogleCredential calls the API to obtain a new access token, it converts\nthe methods set on the credential to the correct JWT token properties\naccording to the following table.<\/p>"},{"title":"Generating Java with JCodeModel","link":"https:\/\/sookocheff.com\/post\/java\/generating-java-with-jcodemodel\/","pubDate":"Sun, 21 Feb 2016 21:10:12 -0600","guid":"https:\/\/sookocheff.com\/post\/java\/generating-java-with-jcodemodel\/","description":"<p>Have you come across the misfortune of needing to auto-generate Java source\ncode? Luckily, anything you&rsquo;ve wanted to do with Java has already been done\n\u2014 and auto-generating Java is no different. I recently used\n<a href=\"https:\/\/codemodel.java.net\/\">JCodeModel<\/a> to translate from JSON to Java \u2014 it\nworked great but it lacks any tutorial-style documentation. This article means\nto fill that gap. If you feel the need to go more in-depth, consult the <a href=\"https:\/\/codemodel.java.net\/nonav\/apidocs\/com\/sun\/codemodel\/JCodeModel.html\">the\nJavadoc<\/a>.<\/p>"},{"title":"Why Java? Tales from a Python Convert","link":"https:\/\/sookocheff.com\/post\/java\/why-java\/","pubDate":"Fri, 20 Nov 2015 12:01:52 -0600","guid":"https:\/\/sookocheff.com\/post\/java\/why-java\/","description":"<p>Whenever I tell people I&rsquo;ve been working with Java I get the same reaction:<\/p>\n<blockquote>\n<p>&ldquo;Yuck! Java? Why Java?&rdquo;<\/p>\n<\/blockquote>\n<p>And, admittedly, I had the same reaction \u2014 at first. But over time, I&rsquo;ve come to\nappreciate Java for its type safety, performance, and rock-solid tooling. I&rsquo;ve\nalso come to notice that this isn&rsquo;t the Java I was used to \u2014 it&rsquo;s been steadily\nimproving over the last ten years.<\/p>"},{"title":"Including a local package as a Maven dependency","link":"https:\/\/sookocheff.com\/post\/java\/local-maven-repository\/","pubDate":"Thu, 12 Nov 2015 13:57:12 -0600","guid":"https:\/\/sookocheff.com\/post\/java\/local-maven-repository\/","description":"<p>Lately I&rsquo;ve been tasked with developing a Java library for internal use. For\ntesting, its proved useful to package the library for local use. This article\ndescribes how to add a Jar file to a local Maven repository for use in your own\ntesting and development.<\/p>\n<h2 id=\"create-your-local-maven-repository\">Create your local Maven repository<\/h2>\n<p>Your local Maven repository lives within the project you are developing for.\nCreating your local repository is as simple as making a new directory.<\/p>"},{"title":"Writing Repeated BigQuery records using the Java Client Library","link":"https:\/\/sookocheff.com\/post\/bigquery\/writing-repeated-bigquery-records\/","pubDate":"Tue, 27 Oct 2015 06:05:37 -0600","guid":"https:\/\/sookocheff.com\/post\/bigquery\/writing-repeated-bigquery-records\/","description":"<p>I&rsquo;ve recently been working with Java via the Google Cloud Dataflow SDK. One\nproblem I&rsquo;ve had is working with the BigQuery Java Client. It was never entirely\nclear how to create a repeated record. This article explains how it works and\nhow you can accomplish the same thing.<\/p>\n<p>First, you need to create a new <code>TableRow<\/code>. For this example, let&rsquo;s assume we\nare logging events using a guid and a timestamp.<\/p>"},{"title":"From JSON to a Google API Client Library object","link":"https:\/\/sookocheff.com\/post\/java\/from-json-to-google-api-client-object\/","pubDate":"Sun, 25 Oct 2015 06:57:12 -0600","guid":"https:\/\/sookocheff.com\/post\/java\/from-json-to-google-api-client-object\/","description":"<p>I have been working on a Cloud Dataflow project that parses incoming App Engine logs to generate status statistics such as numer of errors in the last 10 minutes. The incoming data is a JSON representation of a <a href=\"https:\/\/cloud.google.com\/logging\/docs\/api\/ref\/rest\/v1beta3\/projects.logs.entries\/write#LogEntry\">LogEntry<\/a> object. A LogEntry object is represented in Java as a <a href=\"https:\/\/developers.google.com\/resources\/api-libraries\/documentation\/logging\/v1beta3\/java\/latest\/com\/google\/api\/services\/logging\/model\/LogEntry.html\">LogEntry class<\/a>. The task I found myself in was converting the JSON representation of a LogEntry into the Java object for downstream processing.<\/p>"},{"title":"Creating a BigQuery Table using the Java Client Library","link":"https:\/\/sookocheff.com\/post\/bigquery\/creating-a-big-query-table-java-api\/","pubDate":"Mon, 23 Mar 2015 15:32:37 -0600","guid":"https:\/\/sookocheff.com\/post\/bigquery\/creating-a-big-query-table-java-api\/","description":"<p>I haven&rsquo;t been able to find great documentation on creating a BigQuery\nTableSchema using the Java Client Library. This blog post hopes to rectify that\n:).<\/p>\n<p>You can use the <a href=\"https:\/\/github.com\/GoogleCloudPlatform\/bigquery-samples-java\">BigQuery sample\ncode<\/a> for an idea\nof how to create a client connection to BigQuery. Assuming you have the\nconnection set up you can start by creating a new <code>TableSchema<\/code>. The\n<code>TableSchema<\/code> provides a method for setting the list of fields that make up the\ncolumns of your BigQuery Table. Those columns are defined as an Array of\n<code>TableFieldSchema<\/code> objects.<\/p>"}]}}