Skip to content

Instantly share code, notes, and snippets.

@shyiko
shyiko / findclass.sh
Created May 19, 2011 13:00
Looks for class inside JARs/WARs/EARs/SARs
#!/bin/bash
DIRECTORY=$1
CLASS_NAME=$2
find $DIRECTORY -name '*.[jwes]ar' | while read LINE; do
grep -q $CLASS_NAME "$LINE";
if [ $? -eq 0 ]; then
REFERENCES=$(jar tvf "$LINE" | grep $CLASS_NAME);
if [ -n "$REFERENCES" ]; then
echo "$LINE";
echo "$REFERENCES";
@shyiko
shyiko / TomcatEmbeddedServer.java
Created June 7, 2011 11:50
Tomcat Embedded Server
import org.apache.catalina.Context;
import org.apache.catalina.Engine;
import org.apache.catalina.Host;
import org.apache.catalina.LifecycleException;
import org.apache.catalina.connector.Connector;
import org.apache.catalina.startup.Embedded;
import java.io.File;
/**
@shyiko
shyiko / DevModeJettyLauncher.java
Created June 26, 2011 14:05
Vaadin GWT Jetty Launcher
/*
* Copyright 2011 Stanley Shyiko
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
@shyiko
shyiko / intellij-idea-jd-gui-launcher.sh
Created November 12, 2011 09:27
JD-GUI Launcher for Intellij IDEA (via External Tools)
#!/bin/bash
JDGUI_BIN=$1; FILE_PATH=$2
DELIMITER_INDEX=$(awk -v a="$FILE_PATH" -v b="!/" 'BEGIN{print index(a,b)}')
if [ $DELIMITER_INDEX -eq 0 ]; then
$JDGUI_BIN $FILE_PATH
else
JAR_FILE=${FILE_PATH:0:$DELIMITER_INDEX-1}
CLASS_RELATIVE_LOCATION=${FILE_PATH:$DELIMITER_INDEX+1}
if [[ $CLASS_RELATIVE_LOCATION == "" ]]; then
$JDGUI_BIN $JAR_FILE
@shyiko
shyiko / SequenceFileAppender.java
Created December 4, 2011 10:57
Hadoop SequenceFileAppender
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.SequenceFile;
import org.apache.hadoop.io.Writable;
import java.io.IOException;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.List;
@shyiko
shyiko / AdvisoryLocking.sql
Created December 27, 2011 13:12
PostgreSQL Advisory Locking
<tableid> = select oid from pg_class where relname = '<tablename>';
<rowid> = select id from (
select rr.id from <tablename> rr left join pg_locks pgl
on pgl.classid = <tableid> and pgl.objid = rr.id
where pgl.objid is null limit 1
) iq where pg_try_advisory_lock(<tableid>, id);
-- select * from pg_locks where locktype = 'advisory';
select pg_advisory_unlock(<tableid>, <rowid>);
@shyiko
shyiko / Cache.java
Created January 4, 2012 17:52
SoftReferenceMap
public class Cache<K, V> {
private final Map<K, SoftReference<V>> map;
public Cache(final int cacheSize) {
map = Collections.synchronizedMap(new LinkedHashMap<K, SoftReference<V>>() {
@Override
protected boolean removeEldestEntry(java.util.Map.Entry<K, SoftReference<V>> eldest) {
return size() > cacheSize;
@shyiko
shyiko / pom.xml
Created February 12, 2012 18:06
Sonar & JaCoCo & Maven 3 integration
<project ...>
...
<properties>
<sonar.core.codeCoveragePlugin>jacoco</sonar.core.codeCoveragePlugin>
<sonar.dynamicAnalysis>reuseReports</sonar.dynamicAnalysis>
<sonar.jacoco.reportPath>${user.dir}/target/jacoco.exec</sonar.jacoco.reportPath>
</properties>
...
<build>
<plugins>
@shyiko
shyiko / mvnfy
Created February 12, 2012 21:55
Maven powered with Ubuntu Notify OSD
#!/bin/bash
mvn $*
if [ $? -gt 0 ]
then
notify-send -i /usr/share/icons/Humanity/status/64/dialog-warning.svg "Maven" "Build failed."
else
notify-send -i /usr/share/icons/Humanity/actions/64/gtk-info.svg "Maven" "Build succeeded."
fi
@shyiko
shyiko / InvocationLocal.java
Created May 17, 2012 20:19
InvocationLocal<T>
/**
* Provides method-invocation-scope variables.
* <p/>
* Each invocation of {@link #get()} within same method (including nested calls) will yield value previously set by
* {@link #set(Object)}. Once {@link #set(Object)} region ends, value is rolled back to the default one (provided
* during {@link InvocationLocal} instance construction).
* <p/>
* Implementation is thread-safe.
*
* @author <a href="mailto:sshyiko@cogniance.com">sshyiko</a>