Skip to content

Instantly share code, notes, and snippets.

View deckerego's full-sized avatar

John Ellis deckerego

View GitHub Profile
@deckerego
deckerego / odd_http_traffic_patterns.r
Created December 6, 2013 14:16
If you consider aberrant traffic hit rates ones that are more or equal to two standard deviations away from the mean
# Massage your data into a data frame that provides access by Hour and URI
traffic.df <- parse.log("access.log") #parse.log is left as an exercise for the reader
# Aggregate
uri.hits <- ddply(traffic.df, .(Hour, URI), summarise, Hits=length(URI), .parallel = TRUE)
uri.stats <- ddply(uri.hits, .(URI), summarise, Mean=mean(Hits), Variance=sd(Hits), Total=sum(Hits), .parallel = TRUE)
uri.stats <- join(uri.hits, uri.stats, c("URI"))
# Find two std dev away from mean
uri.bad <- subset(uri.stats, Variance > 0)
@deckerego
deckerego / rss_df.r
Created December 6, 2013 14:18
Reading an RSS XML Feed as a Dataframe
rssToDataframe <- function(uri) {
doc <- getURL(uri)
doc <- xmlParse(doc)
src <- xpathApply(doc, "/rss/channel/item")
items.df <- data.frame()
if(length(src) > 0) {
for(i in 1:length(src)) {
item <- xmlSApply(src[[i]], xmlValue)
record.df <- data.frame(t(item), stringsAsFactors=FALSE)
@deckerego
deckerego / rss_atom_df.r
Created December 6, 2013 14:18
Reading an RSS Atom Feed as a Dataframe
atomToDataframe <- function(uri) {
doc <- getURL(uri)
doc <- xmlParse(doc)
src <- getNodeSet(doc, "/d:feed/d:entry", c(d = "http://www.w3.org/2005/Atom"))
items.df <- data.frame()
if(length(src) > 0) {
for(i in 1:length(src)) {
item <- xmlSApply(src[[i]], xmlValue)
record.df <- data.frame(t(item), stringsAsFactors=FALSE)
@deckerego
deckerego / DatabaseTester.java
Created December 6, 2013 14:24
DBUnit is oooooooooooooooold and hasn't been updated in forever, but still quasi-useful. It never had an official way of loading spreadsheets based on the absolute path of a flie name; paths were always relative to its own class context. Snippets load a spreadsheet using DBUnit specified by an absolute file path, and then demonstrate what a setu…
private static IDatabaseTester databaseTester;
@Resource
protected ComboPooledDataSource dataSource;
@Before //You can use @BeforeClass if dataSource isn't loaded by dependency injection
public void setupDatabase() throws Exception {
if(databaseTester == null) { //Only load once, this can be removed if using @BeforeClass
databaseTester = new DataSourceDatabaseTester(dataSource);
IDataSet dataSet = new XlsDataFileLoader().load("TestDAO-data.xls");
databaseTester.setDataSet(dataSet);
@deckerego
deckerego / TestServlet.java
Created December 6, 2013 14:24
Unit testing a Servlet managed by Spring
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration()
public class TestServlet {
private HttpServlet servlet;
@Resource
protected ApplicationContext applicationContext;
@Before
public void initServlet() throws Exception {
StaticWebApplicationContext webContext = new StaticWebApplicationContext();
@deckerego
deckerego / LookupJini.java
Created December 6, 2013 14:28
Looking Up a Jini Service Entry, with thanks to Shay at the GigaSpaces forums ( http://forum.openspaces.org/thread.jspa?messageID=10328 )
LookupCache getLookupCache(String host, Class template) {
if (System.getSecurityManager() == null)
System.setSecurityManager(new RMISecurityManager());
LookupLocator primaryRegistrar = new LookupLocator(host);
LookupDiscoveryManager discoveryManager =
new LookupDiscoveryManager(null, new LookupLocator[] {primaryRegistrar}, null);
ServiceDiscoveryManager serviceDiscoveryManager =
new ServiceDiscoveryManager(discoveryManager, new LeaseRenewalManager());
ServiceTemplate serviceTemplate = new ServiceTemplate(null, new Class[] {template}, null);
@deckerego
deckerego / management.jmxremote.password
Created December 6, 2013 14:27
Enabling Remote JMX with Plaintext Authentication
monitorRole readOnlyPassword
controlRole adminPassword
@deckerego
deckerego / HashCode.java
Created December 6, 2013 14:25
Several hashcode implementations (including the one within the Java foundation classes) relies on integer overflows to have predictable behavior. This assumption prevents these hashcode implementations from being portable, since integer overflows result in different values across runtime environments. The following hashcode implementation does n…
@Override
public int hashCode() {
int hash = 0, buffer = 0;
int modulo, logicalShiftLeft;
for(int index = 0, max = this.stringProperty.length(); index < max; ++index) {
modulo = index % 4;
logicalShiftLeft = 8 * modulo;
buffer += this.stringProperty.charAt(index) << logicalShiftLeft;
if(modulo == 3) {
@deckerego
deckerego / XMLSearch.java
Created December 6, 2013 14:29
Search an XML Document with dom4j
File file = new File("/path/to/file");
SAXReader reader = new SAXReader();
Document document = reader.read(file);
XPath pathSelector = DocumentHelper.createXPath("/root/childOne/leaf[@attributeName='attributeValue']");
List pathNodes = pathSelector.selectNodes(document);
for(Object pathNode : pathNodes) {
Element pathElement = (Element) pathNode;
System.out.println(pathElement.getTextTrim());
@deckerego
deckerego / MyPID.java
Created December 6, 2013 15:36
Finding The Current Process ID
import java.io.IOException;
import java.util.UUID;
import com.sun.tools.attach.AttachNotSupportedException;
import com.sun.tools.attach.VirtualMachine;
import com.sun.tools.attach.VirtualMachineDescriptor;
public class MyPID {
public static void main(String[] args) throws AttachNotSupportedException, IOException {
String uniqueId = UUID.randomUUID().toString();
System.setProperty("process.unique.id", uniqueId);