Jan 8 2020

Best Homebrew Formulae

Homebew is convenient package manager for the OS X. Once you install Homebrew on your Mac, you can then easily intall and uninstall tools, libraries, and applications through the brew command. The brew command accepts an install and unistall option, as well as list option that prints all currently installed applications.

You can install an application through a brew formula. There is a plathora of homebrew formulae.

The following homebrew formulae are the ones that I’ve found most useful as a software developer.

Best Homebrew Formulae

  • brew install mysql
    • Installs MySQL database
  • brew install awscli
    • Installs AWS CLI
  • brew install sqlite
    • Installs SQLite CLI
  • brew install groovy
    • Installs Groovy scripting language
  • brew install jq
    • Installs jq command to process JSON content
  • brew install jo
    • Installs jo command to generate JSON content
  • brew install bats
    • Installs test framework for Bash scripts
  • brew install wget
    • Installs wget command to fetch HTTP content
  • brew install markdown
    • Installs text-to-HTML converter
  • brew install hugo
    • Installs hugo static site generator


Jan 6 2020

POJOs Made Easy with Project Lombok

Every Java application has some sort of bean or Plain Old Java Object (POJO) to hold and manage state. Because of good Object Oriented Programming (OOP) principles we encapsulate field access with getter and setter methods. Most modern IDEs allow you to quickly create getters and getters. You can often find a Generate option under the Code menu from your favorite IDE. But generating getters and setters produces a lot of unnecessary boilerplate code.

With Project Lombok, you can reduce all that boilerplate getter and setter code with a few annotations.

The following bit of code will produce a Java class with the necessary getters and setters to access all the defined fields.

package com.juixe.core.pojo;

import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;

@ToString @EqualsAndHashCode
@Getter @Setter
public class Person {
   private Long id;
   private String firstName;
   private String lastName;
}

Given the Person class above, you can start coding the following.

   Person p = new Person();
   p.setFirstName("Boingo");
   p.setLastName("API");
   p.setId(1000l);

The getter and setter methods are added to the bytecode for the Person class by Project Lombok through the annotations. If you remove one of the annotations, like the @Setter annotation, then the setters methods will be deleted.

We just saw how a few annotation can generate getters, setters, equals, hashCode, and toString methods for simple Java classes. If you want all these methods generated for you, automatically, you can just use the @Data annotation like the following.

package com.juixe.core.pojo;

import lombok.Data;

@Data
public class Person {
   private Long id;
   private String firstName;
   private String lastName;
}

There is also a @Value annotation that is similar to @Data except that it is intended for immutable objects, so it doesn’t generate any setter methods.


Jan 2 2020

Human JSON

JSON is a versatile format to define data. I read and write JSON all the time but as a subset of JavaScript, it has a few limitations, most notably the lack of support for comments. Enter HJSON, or Human JSON. HJSON makes quotes around object names optional, it makes quotes around strings optional as well, it adds support for multi-line strings, and allows you to add comments in a number of ways. The following is valid HJSON.

{
  // use #, // or /**/ comments,
  // omit quotes for keys
  key: 1
  // omit quotes for strings
  contains: everything on this line
  // omit commas at the end of a line
  cool: {
    foo: 1
    bar: 2
  }
  // allow trailing commas
  list: [
    1,
    2,
  ]
  // and use multiline strings
  realist:
    '''
    My half empty glass,
    I will fill your empty half.
    Now you are half full.
    '''
}

There is an implementation of HJSON for most popular languages, like Java, Python, Go, Python, etc. I’ve used the hjson-java library to read HJSON file and converted it to JSON in a few lines.


Dec 27 2019

Create a HTTP Server in Java

Here is a quick tutorial on how to create a local embedded HTTP server in Java to serve JSON data. The whole code in less than 30 lines on Java.

package com.juixe.json.server;

import com.sun.net.httpserver.HttpExchange;
import com.sun.net.httpserver.HttpHandler;
import com.sun.net.httpserver.HttpServer;

import java.io.IOException;
import java.net.InetSocketAddress;

public class JSONServer implements HttpHandler {

    public static void main(String[] args) throws IOException  {
        HttpServer server = HttpServer.create(new InetSocketAddress(8080), 0);
        server.createContext("/data", new JSONServer());
        server.setExecutor(null);
        server.start();
    }

    @Override
    public void handle(HttpExchange he) throws IOException {
        String response = "{\"message\": \"Hello, HTTP Client\"}";

        he.getResponseHeaders().set("Content-Type", "application/json;charset=UTF-8");
        he.sendResponseHeaders(200, response.length());
        he.getResponseBody().write(response.getBytes());
        he.close();
    }
}

The HttpServer and HttpHandler classes are part of the JRE, so no additional libraries are required. Java also includes a HttpsServer which supports SSL. In the HttpHandler, you can return any data but for this example we are returning a simple JSON response.


Dec 15 2019

Useful Bash Commands

Every programmer should be well versed with Bash, from finding files to schedule tasks.  The following are some of the most common Bash commands that I use on a weekly basis.

  • grep -i aBc <FILE>
    • Grep ignore case
  • grep –color abc <FILE>
    • Color highlight matching pattern
  • grep -v ABC <FILE>
    • Ignore any lines that include ABC
  • grep abc $(find . -mtime 0 -type f)
    • Grep recently modified files
  • grep “abc xyz” <FILE> | grep “02/03/19 09”
    • Find all lines that contain “abc xyz” and then find the remaining lines that contain the following timestamp
  • ps -ef | grep java
    • Find Java processes
  • history | grep git
    • Find all commands in the history that contain the word git
  • history -cw
    • Clear history
  • wc -l <FILE>
    • Number of lines in file
  • ls -1 | wc -l
    • Count the number of files in the current directory
  • ls -lh
    • Show the size unit of the file, such as K for kilobyte, M for megabyte, etc
  • ln -s <SOURCE_FILE> <LINK_FILE>
    • Create a symbolic file link reference to <SOURCE_FILE>
  • df -P <FILE>
    • Find free disk space and if it’s mounted
  • du -hs
    • Find disk usage for each file in current directory
  • cat /dev/null > <FILE>
    • Clear/wipe file
  • zip -r <FILE>
    • Zip all contents in current directory into the specified zip file
  • zip -er <FILE> <DIRECTORY>
    • Zip and encrypt all contents in given directory into the specified zip file
  • zip -d <FILE> “file.txt”
    • Remove file file.txt from the specified zip file
  • unzip -l <FILE>
    • List contents of given zip file
  • diff -rq <DIRECTORY_ONE> <DIRECTORY_TWO>
    • Recursively diff two directory structures
  • crontab -l
    • List cron rules for current user
  • crontab -e
    • Open crontab for editing
  • at -f <COMMAND> -t 201906160701
    • Schedule a command or batch file to run at the specified time
  • atq
    • List current scheduled tasks
  • atrm
    • Remove a scheudle task
  • uuidgen
    • Create a 128-bit UUID
  • date | md5
    • Use md5 on a date to generate a UUID based on a hash
  • $RANDOM
    • Create a random number between 0 and 32k
  • tcpdump -ni eth0 -vvv -s0 -w capture.pcap
    • Listen to eth0 interface in verbose mode and write out to file
  • tcpdump -ni eth0 port 1812 or port 1813 -vvv -s0 -w capture.pcap
    • Listen to eth0 interface for only ports 1812 and 1813

Dec 6 2019

Export Your Data From Google, Twitter, and Instagram

It feels like every year we rely more on online services, from Google, Twitter, Instagram, Facebook, and others. Each of these online services stores a lot of your personal data, not just meta data, but actual data like contacts, posts, photos, chats. Data is more than just personal and private, it is often our work and livelihood.

Unfortunately, to drive user engagement on their own platforms, these services don’t make it easy to interoperate with others, in essence locking you in. Much the personal and private data that we pour into these services is locked into the one service you posted, uploaded, or commented on.

It is a good idea to export all your personal and private data you consider valuable, at least once a year, from any online platform or services. Be sure to backup this data accordingly, using both physical and cloud backup solutions.

For example, with Google Takeout you can export your Blogger posts and pages, GMail email and contacts, Drive documents, YouTube videos and comments, and much more.

Google Takeout

I don’t backup my data off of Google because they would lose it, but because it has been documented a number of times that Google can suspend any account, even for what would be considered normal behavior and usage by the account holder. In the case of services like Twitter and Instagram, there are cases that accounts have been hijacked by hackers or even former friends. Additionally, these services can be bought up by other companies and can change their terms of service in a way that impacts your data and work, like when Flickr got acquired by SmugMug in 2018.