Skip to content

Instantly share code, notes, and snippets.

@langmi
langmi / get_memory_vps.sh
Created July 23, 2012 21:39
shows free and available vps memory on a shared server
#!/bin/bash
# von/from http://devnulled.com/content/2007/06/how-to-display-linux-vps-memory-usage/
bean=`cat /proc/user_beancounters`
guar=`echo "$bean" | grep vmguar | awk '{ print $4;}'`
burst=`echo "$bean" | grep privvm | awk '{ print $5;}'`
priv=`echo "$bean" | grep privvm | awk '{ print $2;}'`
let total=guar/256
let used=priv/256
let burst=burst/256
echo "VPS memory usage:"
@langmi
langmi / node-and-npm-in-30-seconds.sh
Created February 25, 2012 17:26 — forked from isaacs/node-and-npm-in-30-seconds.sh
Use one of these techniques to install node and npm without having to sudo. Discussed in more detail at http://joyeur.com/2010/12/10/installing-node-and-npm/ Note: npm >=0.3 is *safer* when using sudo.
echo 'export PATH=$HOME/local/bin:$PATH' >> ~/.bashrc
. ~/.bashrc
mkdir ~/local
mkdir ~/node-latest-install
cd ~/node-latest-install
curl http://nodejs.org/dist/node-latest.tar.gz | tar xz --strip-components=1
./configure --prefix=~/local
make install # ok, fine, this step probably takes more than 30 seconds...
curl http://npmjs.org/install.sh | sh
@langmi
langmi / CompositeItemStreamReader.java
Created February 3, 2012 07:23
CompositeItemStreamReader wraps multiple ItemStreamReaders and reads them simultaneously.
import java.util.ArrayList;
import java.util.List;
import org.springframework.batch.item.ExecutionContext;
import org.springframework.batch.item.ItemStream;
import org.springframework.batch.item.ItemStreamException;
import org.springframework.batch.item.ItemStreamReader;
import org.springframework.batch.item.NonTransientResourceException;
import org.springframework.batch.item.ParseException;
import org.springframework.batch.item.UnexpectedInputException;
@langmi
langmi / AggregatingSimpleItemsWriter.java
Created January 24, 2012 22:20
Itemwriter which aggregates items before delegating to another wrapped itemwriter. See https://gist.github.com/1673074 for example of Reader which checks if resource is exhausted.
/**
* Copyright 2012 Michael R. Lange <michael.r.lange@langmi.de>.
*
* 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
@langmi
langmi / ReaderExhaustedWrapper.java
Created January 24, 2012 22:18
Spring Batch Reader wrapping another reader, for each read it peeks ahead to look for end of data.
/**
* Copyright 2012 Michael R. Lange <michael.r.lange@langmi.de>.
*
* 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
@langmi
langmi / gist:1582202
Created January 9, 2012 09:39
access target behind spring proxy
// from http://www.techper.net/2009/06/05/how-to-acess-target-object-behind-a-spring-proxy/
@SuppressWarnings({"unchecked"})
protected <T> T getTargetObject(Object proxy, Class<T> targetClass) throws Exception {
if (AopUtils.isJdkDynamicProxy(proxy)) {
return (T) ((Advised)proxy).getTargetSource().getTarget();
} else {
return (T) proxy; // expected to be cglib proxy then, which is simply a specialized class
}
}
@langmi
langmi / WrappedMrirToGetCurrentResource.java
Created January 9, 2012 07:57
(Spring-Batch) Example implementation wraps a MultiResourceItemReader to get the current resource.
public class WrappedMrirToGetCurrentResource<T> implements ItemStreamReader<T> {
private MultiResourceItemReader<T> mrir;
@Override
public void open(ExecutionContext executionContext) throws ItemStreamException {
mrir.open(executionContext);
// current.resource is only available here if it's a restart
if (mrir.getCurrentResource() != null && mrir.getCurrentResource().getFilename() != null) {
System.out.println("open:" + mrir.getCurrentResource().getFilename());
@langmi
langmi / FieldSetSqlParameterSourceProvider.java
Created September 30, 2011 09:19
Implementation for ItemSqlParameterSourceProvider which creates a MapSqlParameterSource directly from the FieldSet
/**
* Implementation for {@link ItemSqlParameterSourceProvider},
* creates {@link MapSqlParameterSource} from {@link FieldSet}.
*
* @author Michael R. Lange <michael.r.lange@langmi.de>
*/
public class FieldSetSqlParameterSourceProvider implements ItemSqlParameterSourceProvider<FieldSet> {
/** {@inheritDoc} */
@Override
@langmi
langmi / CustomSqlParameterSourceProvider.java
Created September 30, 2011 09:14
simple example CustomSqlParameterSourceProvider which constructs a MapSqlParameterSource with given item
/**
* CustomSqlParameterSourceProvider, constructs {@link MapSqlParameterSource} with
* given item.
*
* @author Michael R. Lange <michael.r.lange@langmi.de>
*/
public class CustomSqlParameterSourceProvider implements ItemSqlParameterSourceProvider<YourItemObject> {
@Override
public SqlParameterSource createSqlParameterSource(final YourItemObject item) {
@langmi
langmi / SetupHsqldbDatasource.java
Created September 24, 2011 09:24
Setup HSQLDB Datasource
public class JdbcPagingItemReaderTests {
private BasicDataSource dataSource;
public void setUp() throws Exception {
// DataSource Setup, apache commons
dataSource = new BasicDataSource();
dataSource.setDriverClassName("org.hsqldb.jdbcDriver");
dataSource.setUrl("jdbc:hsqldb:mem:testdb");
dataSource.setUsername("sa");