Snippets
Contents
-
Linux
- Create *nix timestamp
- Convert screencast video
- monitor udev events from usb port etc..
- bash/zsh use cursor up/down keys for command history up/down
- TrueCrypt GUIless/console mount encrypted volume
- Enable MTP Support
- Convert all mp3's in a directory to aac
- Find out which application runs on a given port and protocol
- List your hardware
- Directory tree comparison
- Recording audio from the command line to a mp3
- List files and dirs by size
- Backup directory using rsync with progress bar
- Debian/Ubuntu save a list of all installed packages and how to reinstall using this list
- Benchmarking the Network Speed between to Workstations
- Tell SVN not to store your password in .svn ..
- Execute function on shell script exit
- Curl send header
- Curl send POST request
- Curl get all response headers
- Run Apache Benchmark Tool (10 Requests, 5 Concurrent)
- Screen detach session/ list / reattach
- Join multiple videos into a single one
- Mount SSH filesystem with user mapping and symlinks
- netcat emulate http server
- Emulate a http server using python
- Emulate a mail server using python
- Colored and comfortable alternative to top
- Colorize Logs
- SQL
- Java
- Scala
- Apache Webserver
- Maven
- Javascript / jQuery
- Cascading Stylesheets / CSS
- Firefox
- Eclipse
-
Atlassian
- Disable YUI compressor
- Confluence - Get favourites by user
- Confluence - Determine the base URL
- Confluence - Get the context path
- Confluence - Using Velocity Template for a Macro
- Confluence - Perform a content search with restrictions
- Reference exported Web Resources like CSS/JS in a Velocity Template
- Webworks/XWorks Action - Rendering a template without the page frame
- Stash - Enable a Package Logger via REST
- JIRA - Find existing locations for web-items and web-sections
- Confluence - Search for Spaces by Space Label
- Confluence - Read User Details
- ActiveObjects Create Entity with NotNull Constraint
- Git
- Misc
- XML / XSLT / XPath / Schema
A collection of snippets I’m using often but sometimes my brain refuses to remember ..
Linux
Create *nix timestamp
date --utc --date "2011-11-11 11:11:11" +%s
Convert screencast video
mencoder -idx input.ogv -ovc lavc -oac mp3lame -o output
monitor udev events from usb port etc..
udevadm monitor
bash/zsh use cursor up/down keys for command history up/down
bindkey "^[[A" history-search-backward bindkey "^[[B" history-search-forward
TrueCrypt GUIless/console mount encrypted volume
sudo truecrypt -t -k "" --protect-hidden=no /path/to/encfile /path/to/mountpoint
Enable MTP Support
sudo apt-get install mtp-tools mtpfs
-> connect the device and detect vendor- and product-id
mtp-detect | grep idVendor mtp-detect | grep idProduct
Add to /etc/udev/rules.d/55-android.rules:
SUBSYSTEM=="usb", ATTR{idVendor}=="VENDORID", ATTR{idProduct}=="PRODUCTID", MODE="0666"
sudo service udev restartConnect:
mtpfs -o allow_other /media/GalaxyNexus
Disconnect:
fusermount -u /media/GalaxyNexus
Convert all mp3′s in a directory to aac
for f in *.mp3; do ffmpeg -i "$f" -acodec aac -strict experimental -ab 128k "${f%.mp3}.aac"; done
Find out which application runs on a given port and protocol
sudo fuser -n tcp 8080
List your hardware
sudo lshwDirectory tree comparison
diff -r dir1 dir2
Recording audio from the command line to a mp3
arecord -f cd -t raw | lame -x -r – outfile.mp3
List files and dirs by size
du -cks * |sort -gr
Backup directory using rsync with progress bar
rsync -h --progress --stats -r -tgo -l -p -D --update --delete-after /path/to/dir-to-be-saved /path/to/targetdir/
Debian/Ubuntu save a list of all installed packages and how to reinstall using this list
dpkg --get-selections > installed-packages dpkg --set-selections < installed-packages dselect
Benchmarking the Network Speed between to Workstations
sudo apt-get install iperf
On the target workstation start iperf in server mode:
iperf -sOn the other workstation start the benchmark
iperf -c SERVER_IP_ADDRESSTell SVN not to store your password in .svn ..
svn command --no-auth-cache
Execute function on shell script exit
#!/bin/sh # exit function onExit() { # do something here .. cleanup etc .... } # bind exit function trap onExit ERR EXIT # here's the normal code to be executed
Curl send header
curl -H "headername:value" url e.g. curl -H "REMOTE_USER:foo" http://developers.android.com
Curl send POST request
curl -X POST url e.g. curl -X POST http://developers.android.com
Curl get all response headers
curl -i url e.g. curl -i http://developers.android.com
Run Apache Benchmark Tool (10 Requests, 5 Concurrent)
ab -n 10 -c 5 http://site.com
Screen detach session/ list / reattach
Detach: Ctrl + A + D
List:
$ screen -ls There is a screen on: 2859.pts-0.host (11/07/11 20:18:00) (Detached) 1 Socket in /var/run/screen/S-user.
Reattach:
screen -r 2859.pts-0.host
Join multiple videos into a single one
mencoder -oac copy -ovc copy -o outfile infile1 infile2 ... infileN # or if ls -l filename* matches the correct order .. mencoder -oac copy -ovc copy -o outfile filename*
Mount SSH filesystem with user mapping and symlinks
sudo sshfs user@host.com:/path/to/directory /path/to/local-directory -o uid=1000,allow_other,follow_symlinks,transform_symlinks,workaround=rename
netcat emulate http server
sudo nc -l 80
Emulate a http server using python
python -m SimpleHTTPServerEmulate a mail server using python
python -m smtpd -n -c DebuggingServer localhost:1025
Colored and comfortable alternative to top
if not installed: sudo apt-get install htop htop
Colorize Logs
Three different tools to solve this: multitail, grc, ccze
Installation:
sudo apt-get install multitail grc ccze
multitail:
multitail logfile
grc:
grc tail -f logfile
ccze:
tail -f logfile | ccze -m ansi // or html or curses
SQL
Count unique / all values
SELECT COUNT(DISTINCT <FIELDNAME>), COUNT(ALL <FIELDNAME>) FROM <TABLE>;
Partially anonymize e-mail addresses
UPDATE <TABLE_NAME> SET <EMAIL_FIELD>= INSERT( <EMAIL_FIELD>, POSITION('@' IN <EMAIL_FIELD>), 100, CONCAT(FLOOR(1 + (RAND() * 100)),'@hascode.com')) WHERE POSITION('@' IN <EMAIL_FIELD>)>0;
Find duplicate entries
SELECT COUNT(*), <FIELDNAME> FROM <TABLENAME> GROUP BY <FIELDNAME> HAVING COUNT(*)>1;
Oracle Multiple Levels of Subtotals
SELECT <FIELD>, <otherfield..> FROM <TABLE> GROUP BY ROLLUP(<FIELD>);
Postgres create roles
CREATE ROLE name; DROP ROLE name; SELECT rolname FROM pg_roles; \du GRANT name TO username
Postgres set user password
ALTER USER ALTER USER root WITH password 'xxx';
Postgres change database
\cDBNAME
Java
Show processes
jps
Create heap dump from running process
jmap -dump:format=b,file=/tmp/heap.bin 2381
Use SLF4J and Logback
Maven dependencies:
<project> <properties> <logback.version>1.0.3</logback.version> </properties> <dependencies> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> <version>1.6.6</version> </dependency> <dependency> <groupId>ch.qos.logback</groupId> <artifactId>logback-core</artifactId> <version>${logback.version}</version> </dependency> <dependency> <groupId>ch.qos.logback</groupId> <artifactId>logback-classic</artifactId> <version>${logback.version}</version> </dependency> </dependencies> </project>
Example logback.xml:
<configuration> <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender"> <encoder> <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern> </encoder> </appender> <root level="debug"> <appender-ref ref="STDOUT"/> </root> </configuration>
Service Discovery using ServiceLoader
interface MyService {} class MyServiceImpl {} class AnotherMyServiceImpl {} ServiceLoader<MyService> serviceLoader = ServiceLoader.load(MyService.class); for (MyService s : serviceLoader) { System.out.println(s); }
Dynamic Proxy
package com.hascode.sample; public interface SampleService { String printInformation(); } package com.hascode.sample; import java.lang.reflect.InvocationHandler; import java.lang.reflect.Method; public class SampleServiceInvocationHandler implements InvocationHandler { @Override public Object invoke(final Object proxy, final Method method, final Object[] args) throws Throwable { return "test"; } } package com.hascode.sample; import java.lang.reflect.InvocationHandler; import java.lang.reflect.Proxy; public class Main { public static void main(String[] args) { InvocationHandler handler = new SampleServiceInvocationHandler(); SampleService service = (SampleService) Proxy.newProxyInstance(SampleService.class.getClassLoader(), new Class[]{SampleService.class}, handler); System.out.println(service.printInformation()); } }
JAX-RS return a generic list in a Response
List list = new ArrayList(); GenericEntity> entity = new GenericEntity>(list) {}; Response response = Response.ok(entity).build();
JAX-RS / Jersey – Return a JSON Array for a Collection with a single element
Lets say you’re mapping a collection of books e.g. List<Book> books using jaxb passed as a jaxrs Response .. you may encounter a situation where you receive the following JSON output if there are many books in the collection:
{ books:[ {id:1, title:"foo"}, {id:2, title:"bar"} ] }
But if there is only one book in the collection your receive a single JSON object instead of an array .. e.g.:
{ books:{ id:1, title:"foo" } }
The solution is to provide a custom JAXBContext ContextResolver and advise the context always to render arrays here:
import javax.ws.rs.ext.ContextResolver; import javax.ws.rs.ext.Provider; import javax.xml.bind.JAXBContext; import com.sun.jersey.api.json.JSONConfiguration; import com.sun.jersey.api.json.JSONJAXBContext; import com.hascode.entity.Book; @Provider public class BookContextResolver implements ContextResolver<JAXBContext> { private final JAXBContext context; private final Class[] types = { Book.class }; public PersonContextResolver() throws Exception { this.context = new JSONJAXBContext(JSONConfiguration.mapped().arrays("books").build(), Book.class); } @Override public JAXBContext getContext(final Class<?> objectType) { for (Class type : types) { if (type == objectType) { return context; } } return null; } }
Create heap dump on OutOfMemoryError
-XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=/path/to/save/dumps
Scala
SBT – Eclipse Plugin
Add to your ~/.sbt/plugins/plugins.sbt
addSbtPlugin("com.typesafe.sbteclipse" % "sbteclipse-plugin" % "2.1.1")
Apache Webserver
Deny all methods excepting POST and GET
RewriteCond %{REQUEST_METHOD} ^(TRACE|TRACK|OPTIONS|HEAD) RewriteRule .* - [F]
Rewrite all aliases for a domain to a single domain
RewriteEngine On RewriteCond %{HTTP_HOST} ^(www\.)?mydomain1\.com [NC,OR] RewriteCond %{HTTP_HOST} ^(www\.)?mydomain2\.com [NC,OR] RewriteCond %{HTTP_HOST} ^(www\.)?mydomain3\.com [NC,OR] RewriteRule (.*) http://mydomain.com/$1 [R=301,L]
Maven
Colourize output
export MAVEN_COLOR=true
Run build with 4 threads
mvn -T 4 clean install
Run build with 2 threads per core
mvn -T 2C clean install
Adjust java source/target version for compiler
<build> <plugins> <plugin> <artifactId>maven-compiler-plugin</artifactId> <configuration> <source>1.6</source> <target>1.6</target> </configuration> </plugin> </plugins> </build>
Javascript / jQuery
Firebug XPath Testing Build in
$x("//div/span/em");
Create a custom jQuery selector
The following selector matches all elements with a width greater than 200px. Just run the example on a website like jQuery.com in the Firebug console.
$.extend($.expr[':'], { widthGT200Pixels: function(elem) { return $(elem).width() > 200; } }); $('div:widthGT200Pixels').css('border','1px solid red').click(function() { alert('The width of this div exceeds 200px'); });
Afterscriptexecute Event
Allows to hook into the execution of every script tag in a page
<script type="text/javascript"> document.addEventListener('afterscriptexecute', function(e){ console.log('exec script:', e.target); }, false); </script>
jQuery function to center an element
jQuery.fn.center = function () { return this.css('position','absolute') .css('top', ( $(window).height() - this.height() ) / 2+$(window).scrollTop() + 'px') .css('left', ( $(window).width() - this.width() ) / 2+$(window).scrollLeft() + 'px'); } // centering an element $('#jq-footerNavigation').center();
Cascading Stylesheets / CSS
Calculations
see also W3C draft
.container { width: calc(100% - 80px); }
Firefox
Configure address bar to return search results
- Enter about:config in the address bar
- Search for the key keyword.url
- Modify the value for your search engine of choice .. e.g. for the google search: http://www.google.com/search?q=
Eclipse
Favorites
Spare my time when using static imports ..
Window > Preferences > Java > Editor > Content Assist > Favorites:
com.google.common.collect.Lists com.jayway.restassured.matcher.RestAssuredMatchers com.jayway.restassured.RestAssured org.hamcrest.MatcherAssert org.hamcrest.Matchers org.mockito.Mockito
Template to insert a static logger instance
Go Windows > Preferences > Java > Editor > Templates > New …
Enter logger as name and as template: private static final Logger log = LoggerFactory.getLogger(${enclosing_type}.class);
Afterwards you’re able to type logger in your code and ctrl+space gives the option to insert the logger
Atlassian
Disable YUI compressor
<plugin> <groupId>com.atlassian.maven.plugins</groupId> <artifactId>maven-jira-plugin</artifactId> <version>${amps.version}</version> <extensions>true</extensions> <configuration> <compressResources>false</compressResources> [..] </configuration> </plugin>
Confluence – Get favourites by user
Using the label manager.
List getFavouriteSpaces(String username)
Confluence – Determine the base URL
Using the SettingsManager:
String baseUrl = settingsManager.getGlobalSettings().getBaseUrl();
Confluence – Get the context path
Using the BootstrapManager:
String contextPath = bootstrapManager.getWebAppContextPath();
Confluence – Using Velocity Template for a Macro
final VelocityContext contextMap = new VelocityContext(MacroUtils.defaultVelocityContext()); contextMap.put("key", obj); // references obj as variable named $key in the velocity template VelocityUtils.getRenderedTemplate("path/to/template.vm", contextMap);
Confluence – Perform a content search with restrictions
final SearchManager searchManager // <- injected final SearchQuery inSpacesQuery = new InSpaceQuery("DS"); final SearchQuery contentTypeQuery = new ContentTypeQuery(ContentTypeEnum.ATTACHMENT); final SearchQuery query = BooleanQuery.andQuery(inSpacesQuery,contentTypeQuery); final SearchSort searchSort = new CreatedSort(Order.DESCENDING); final SearchFilter contentPermissionFilter = ContentPermissionsSearchFilter.getInstance(); final SearchFilter spacePermissionFilter = SpacePermissionsSearchFilter.getInstance(); final SearchFilter permissionsFilter = new ChainedSearchFilter(contentPermissionFilter, spacePermissionFilter); final ResultFilter maxResultFilter = new SubsetResultFilter(20); final ISearch search = new ContentSearch(query, searchSort,permissionsFilter, maxResultFilter); final SearchResults results = searchManager.search(search);
Reference exported Web Resources like CSS/JS in a Velocity Template
Include this directive in the velocity template:
#requireResource("PLUGINKEY:WEBRESOURCEKEY")Example declaration of a web resource in the atlassian-plugin.xml:
<web-resource name="Some Web Resources" i18n-name-key="some-web-resources.name" key="WEBRESOURCEKEY"> <description key="some-web-resources.description">Some Web Resources</description> <resource name="some.css" type="download" location="somepath/some-min.css"/> </web-resource>
Webworks/XWorks Action – Rendering a template without the page frame
Applying the parameter decorator=popup or decorator=none is one option.
Another option is to specify the decorator in the velocity template’s header section
<head> <meta name="decorator" content="atl.popup" /> </head>
Stash – Enable a Package Logger via REST
curl -u admin -v -X PUT -d "" -H "Content-Type: application/json" http://localhost:7990/stash/rest/api/latest/logs/logger/com.hascode/debug
JIRA – Find existing locations for web-items and web-sections
Their declarations can be found in <source-installation-directory>/jira-project/jira-components/jira-core/src/main/resources/webfragment/system-user-nav-bar-sections.xml
Confluence – Search for Spaces by Space Label
Label label ... List spaces = labelManager.getSpacesWithLabel(label); or List spaces = labelManager.getSpacesWithLabel("labelString", Namespace.GLOBAL);
Confluence – Read User Details
for (String group : userDetailsManager.getProfileGroups()) { System.err.println(“Group: ” + group + ” has keys:”); for (String key : userDetailsManager.getProfileKeys(group)) { System.err.println(“key: ” + key + ” / current user’s value: ” + userDetailsManager.getStringProperty(currentUser, key)); } } Group: personal has keys: key: phone / current user’s value: 1234 key: im / current user’s value: - key: website / current user’s value: www.hascode.com Group: business has keys: key: position / current user’s value: CTO key: department / current user’s value: Technologies key: location / current user’s value: Wiesbaden
ActiveObjects Create Entity with NotNull Constraint
The entity:
public interface Book extends Entity { [..] @NotNull String getTitle(); }
Create a new book entity:
ActiveObjects ao; // -> set via dependency injection Book book = ao.create(Book.class, new DBParam("TITLE", "some title"));
Git
Show changes in a dedicated date range
git log --since=4.days --until=3.days
Git setup a server using the git protocol
cd projectdir git daemon --reuseaddr --base-path=. --export-all --verbose git clone git://ipaddress/ appname
Misc
Data Normalizing
newVal = (oldVal-min) / (max-min)
Ugly Scala Example
package com.hascode import scala.collection.mutable.LinkedList object NormalizerExample extends App { val dataSet = LinkedList(1., 6.5, 3., 6.2, 20., 31.2, 50.2, 12., 0.24, 1.224, 2.2, 3.) for ((num, index) <- dataSet.zipWithIndex) { dataSet(index) = (num - dataSet.min) / (dataSet.max - dataSet.min) } println("Normalized: " + dataSet) }
Normalized: LinkedList(0.01521216973578863, 0.12921819759798853, 0.05947594797769014, 0.12324029048767723, 0.3982240175619966, 0.6213992163469515, 1.0, 1.0, 0.07531115879828326, 0.40498283261802576, 0.7319742489270387, 1.0)
XML / XSLT / XPath / Schema
XSLT Strip Namespaces for Output
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:foo="http://foo" xmlns:bar="http://bar" exclude-result-prefixes="foo bar">