Skip to content

Instantly share code, notes, and snippets.

View jarrodhroberson's full-sized avatar

Jarrod Roberson jarrodhroberson

View GitHub Profile
@jarrodhroberson
jarrodhroberson / truncate.groovy
Last active August 29, 2015 13:56
Truncate String in Groovy
// this adds a "truncate(#)" method to String
String.metaClass.truncate = { len ->
if (delegate == null) {return''}
if (delegate.length() > len) {return delegate[0..(len - 2)] + "\u2026"}
return delegate
}
@jarrodhroberson
jarrodhroberson / ant_fat_jar.xml
Created February 14, 2014 03:30
How to bundle Java programs into a single executable .jar file using Ant
<target name="build-jar" depends="compile">
<jar destfile="${dist.dir}/app.jar" compress="true">
<manifest>
<attribute name="Main-Class" value="main"/>
</manifest>
<fileset dir="${build.dir}/classes" includes="**/*.class"/>
<zipgroupfileset dir="${lib.dir}">
<includesfile name="dependencies.list"/>
</zipgroupfileset>
</jar>
@jarrodhroberson
jarrodhroberson / zeroconf.erl
Created February 14, 2014 03:24
Bonjour / Zeroconf In Erlang ( a start at least )
-module(zeroconf).
-include("zeroconf.hrl").
-export([open/0,start/0]).
-export([stop/1,receiver/0]).
-export([send/1]).
-define(ADDR, {224,0,0,251}).
-define(PORT, 5353).
@jarrodhroberson
jarrodhroberson / counter.erl
Created February 14, 2014 03:22
Counters In Erlang
-module(counter).
-export([inc/1,inc/2,dec/1,dec/2,current/1,reset/1,start/1,loop/1]).
start(Name) ->
register(Name,spawn(counter,loop,[0])),
ok.
inc(Name, Amount) ->
Name ! {inc_counter, Amount},
current(Name).
@jarrodhroberson
jarrodhroberson / timestamp_in_ms.erl
Created February 14, 2014 03:21
How to get a timestamp in milliseconds from Erlang
% gets a timestamp in ms from the epoch
get_timestamp() ->
{Mega,Sec,Micro} = erlang:now(),
(Mega*1000000+Sec)*1000000+Micro.
@jarrodhroberson
jarrodhroberson / inet_mdns.erl
Created February 14, 2014 03:19
Bonjour / Zeroconf in Erlang with Caching Responses with Subscriptions
-module(inet_mdns).
-include_lib("kernel/src/inet_dns.hrl").
-export([open/2,start/0]).
-export([stop/1,receiver/1]).
-export([subscribe/2,unsubscribe/2,getsubs/1]).
% gets a timestamp in ms from the epoch
get_timestamp() ->
{Mega,Sec,Micro} = erlang:now(),
@jarrodhroberson
jarrodhroberson / TestThreadStackSizes.java
Created February 14, 2014 03:15
How to determine how many threads you can create from Java
public class TestThreadStackSizes
{
public static void main(final String[] args)
{
Thread.currentThread().setUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() {
public void uncaughtException(final Thread t, final Throwable e)
{
System.err.println(e.getMessage());
System.exit(1);
}
@jarrodhroberson
jarrodhroberson / jsapi_sample.cpp
Created February 14, 2014 03:11
SpiderMonkey in Xcode 3.2.3
#define XP_UNIX
#include "jsapi.h"
/* The class of the global object. */
static JSClass global_class = {
"global", JSCLASS_GLOBAL_FLAGS,
JS_PropertyStub, JS_PropertyStub, JS_PropertyStub, JS_PropertyStub,
JS_EnumerateStub, JS_ResolveStub, JS_ConvertStub, JS_FinalizeStub,
JSCLASS_NO_OPTIONAL_MEMBERS
};
@jarrodhroberson
jarrodhroberson / jasper_reports_dependency_fragment.xml
Created February 14, 2014 03:04
How to combine GWT and Jasper Reports using Maven
<dependency>
<groupId>net.sf.jasperreports</groupId>
<artifactId>jasperreports</artifactId>
<version>3.7.4</version>
<exclusions>
<exclusion>
<artifactId>jdtcore</artifactId>
<groupId>eclipse</groupId>
</exclusion>
</exclusions>
@jarrodhroberson
jarrodhroberson / java_pid_finder.sh
Created February 14, 2014 03:00
Get the PID of a running Java process using only Bash tools
ps -ax | grep "java -jar" | grep -v "grep" | cut -d " " -f 1