Blog

Java code snippet: Enumeration to return String value

I probably should be adding this kind of stuff on GitHub, but I’m lazy. 🙂


public enum TokenType {

    TICKET("ticket"),
    UNKNOWN("unknown");

    private String tokenType;

    TokenType(String tokenType) {
        this.tokenType = tokenType;
    }

    public String getTokenType() {
        return tokenType;
    }

    public boolean isTicket() {
        return this == TICKET;
    }

    public boolean isValidType() {
        return this == TICKET;
    }

    public static TokenType getByTokenType(String tokenType) {
        String safeTokenType = (tokenType != null) ? tokenType.toLowerCase().trim() : "";

        for (TokenType tt : values()) {
            if (tt.getTokenType().equals(safeTokenType)) {
                return tt;
            }
        }

        return UNKNOWN;
    }

    public boolean isUnknown() {
        return this == UNKNOWN;
    }
}

Bash script to check health checks of multiple servers

I had to check over 20 servers to verify they were updated with a new build. The servers had a health check page that had this information along with whether the server was up-and-running. I wrote this quick-and-dirty bash script to do just that. 

for i in jetson1 jetson4; do for j in {1..10}; do curl -igX GET "http://app$j.tpa.$i.coresys.tmcs:8080/health/heartbeat"; done; done | grep -E '1.4.37' | grep -E "Overall Status: Success" | wc -l<br>

Hope this helps someone. Cheers!

OS X: Setting system-wide environment variables

I believe up until Yosemite, I didn’t have issues setting up environmental variables (i.e. JAVA_HOME) via my .bash_profile. As I upgraded, that no longer worked, so I found another way: use a custom .plist in your LaunchAgents folder:

  1. Go to ~/Library/LaunchAgents
  2. Add something like the below and then reboot, or unload-load it via launchctl unload
  3. That’s it!
  4. 
    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
    <plist version="1.0">
    <dict>
      <key>Label</key>
      <string>my.startup</string>
      <key>ProgramArguments</key>
      <array>
        <string>sh</string>
        <string>-c</string>
        <string>launchctl setenv APPS_CONFIG '/Users/anton/box/Source/apps_config' | launchctl setenv CATALINA_BASE '/usr/local/Cellar/tomcat7/7.0.63/libexec' | launchctl setenv CATALINA_HOME $CATALINA_BASE | launchctl setenv HOMEBREW_GITHUB_API_TOKEN 'b20aeb90b4d0cbc8352bdc6059073b7be473e4e3' | launchctl setenv JAVA_HOME '/Library/Java/JavaVirtualMachines/jdk1.8.0_51.jdk/Contents/Home/' | launchctl setenv M2_HOME $'/usr/local/Cellar/maven@3.2/3.2.5/libexec' | launchctl setenv GATLING_HOME '/usr/local/Cellar/gatling/2.1.7/'</string>
      </array>
      <key>RunAtLoad</key>
      <true/>
    </dict>
    </plist>

Maven error with Java 6 on OS X: “Unsupported major.minor version 51.0”

If you encounter the error below with Java 6 on OS X, make sure you’re using Maven 3.2.5 or older.

$ mvn
Exception in thread "main" java.lang.UnsupportedClassVersionError: org/apache/maven/cli/MavenCli : Unsupported major.minor version 51.0
  at java.lang.ClassLoader.defineClass1(Native Method)
  at java.lang.ClassLoader.defineClassCond(ClassLoader.java:637)
  at java.lang.ClassLoader.defineClass(ClassLoader.java:621)
  at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:141)
  at java.net.URLClassLoader.defineClass(URLClassLoader.java:283)
  at java.net.URLClassLoader.access$000(URLClassLoader.java:58)
  at java.net.URLClassLoader$1.run(URLClassLoader.java:197)
  at java.security.AccessController.doPrivileged(Native Method)
  at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
  at org.codehaus.plexus.classworlds.realm.ClassRealm.loadClassFromSelf(ClassRealm.java:401)
  at org.codehaus.plexus.classworlds.strategy.SelfFirstStrategy.loadClass(SelfFirstStrategy.java:42)
  at org.codehaus.plexus.classworlds.realm.ClassRealm.unsynchronizedLoadClass(ClassRealm.java:271)
  at org.codehaus.plexus.classworlds.realm.ClassRealm.loadClass(ClassRealm.java:254)
  at org.codehaus.plexus.classworlds.realm.ClassRealm.loadClass(ClassRealm.java:239)
  at org.codehaus.plexus.classworlds.launcher.Launcher.getMainClass(Launcher.java:144)
  at org.codehaus.plexus.classworlds.launcher.Launcher.launchEnhanced(Launcher.java:266)
  at org.codehaus.plexus.classworlds.launcher.Launcher.launch(Launcher.java:229)
  at org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode(Launcher.java:415)
  at org.codehaus.plexus.classworlds.launcher.Launcher.main(Launcher.java:356)