Snippets
Contents
-
Linux
- Create *nix timestamp
- Convert screencast video
- Cut ogv videos without reencoding with ffmpeg
- Gather RAM Information
- monitor udev events from usb port etc..
- JSON Command Line Filtering with JQ
- Analyze Apache Logs
- Pretty Print JSON (Python installed)
- Pretty Print and colorize JSON (Python installed)
- bash/zsh use cursor up/down keys for command history up/down
- Grep and Pipe - Keep color codes
- Execute last command as root
- Copy to Clipboard from Console
- TrueCrypt GUIless/console mount encrypted volume
- Enable MTP Support
- Convert all mp3's in a directory to aac
- Generate SSH Keys
- 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
- Show installed files for a package
- Benchmarking the Network Speed between to Workstations
- Tell SVN not to store your password in .svn ..
- Execute function on shell script exit
- Join multiple PDF Files with GhostScript
- Curl send header
- Curl send POST request
- Curl get all response headers
- Curl send JSON
- 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
- Neo4j / Cypher
- SQL
-
Java
- Show processes
- Create heap dump from running process
- Create jhat webserver to show heap dump analysis
- Caller / Calling class from Stacktrace
- Print Thread Dump with Deadlock Analysis
- Recompile Class and Set Method Return to true
- Set Timezone in Unit Test
- Starting Java with Debugging enabled
- Debugging with jdb
- Use JUnit 5 with Spring Boot
- Use SLF4J and Logback with Maven
- Get Class Information from Generic Class without Constructor Parameter
- Externalizable Example
- Lambda Expression implementing multiple Interfaces
- CDI / Weld Logging for GlassFish
- Service Discovery using ServiceLoader
- Disassemble Java Class
- Importing Certificates into the Keystore
- Spring Boot Quick Security Config
- Log4j 1.x log by package to different log files
- Java FX 2 Maven Dependency
- Java FX 2 Fullscreen Stage
- WildFly Deploy via Console
- Patching WildFly
- Dynamic Proxy
- Java Receiver Parameters
- Add Class Diagrams to the JavaDocs using Maven and GraphViz
- Separating Unit and Integration Tests with JUnit Categories
- JAX-RS return a generic list in a Response
- JAX-RS / Jersey - Return a JSON Array for a Collection with a single element
- Increase Eclipse Memory Settings
- Different Logging Library Placeholders
- Create heap dump on OutOfMemoryError
- Scala
- Apache Webserver
-
Maven
- Colourize output
- Extract a Project's Version
- Add build number via resource filtering
- Pass JVM Arguments
- Disable Plugin Execution using a Profile
- Run build with 4 threads
- Run build with 2 threads per core
- Setup Maven Wrapper with local settings.xml
- Restrict Resource Filtering to Specific Files
- Maven Plugin and Goal Information
- Adjust java source/target version for compiler
- Javascript
- Cascading Stylesheets / CSS
- Firefox
- IntelliJ IDEA
- 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
- Bitbucket - Stash - Enable a Package Logger via REST
- JIRA - Find existing locations for web-items and web-sections
- JIRA - Detect current JIRA version
- Confluence - Search for Spaces by Space Label
- Confluence - Read User Details
- ActiveObjects Create Entity with NotNull Constraint
-
Git
- Show changes in a dedicated date range
- Move existing Tag pushed to remote to another Commit
- Git Deactivate Fast-Forward-Merges per Default and per Branch
- Git Contributor Stats for a given Period
- Convenience Alias for Force-With-Lease Push
- Remove sensitive file from Git history
- Search for occurance in a commit
- Merge and override with foreign branch changes
- Git Syncing with forked Repository
- Git ignore versioned files
- Git Diff across Branches
- Git setup a server using the git protocol
- Misc
- Docker
-
Kubernetes
- Cluster Info
- Query Information
- Start Container
- Execute Command in POD
- Start HTTP Proxy to Kubernetes API
- Expose as Kubernetes Service
- Label a pod
- Find pod by label
- Delete Service
- Scale Deployment Replicas
- Create Deployment from YAML File
- YAML Descriptor
- Store Information as Environment Variable
- Minikube
- XML / XSLT / XPath / Schema
- PHP
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
Cut ogv videos without reencoding with ffmpeg
ffmpeg -i in.ogv -vcodec copy -acodec copy -ss 00:00:00 -t 00:10:00 out.ogv
Gather RAM Information
sudo dmidecode --type 17
monitor udev events from usb port etc..
udevadm monitor
JSON Command Line Filtering with JQ
jq download: http://stedolan.github.io/jq/
$ echo '{"foo":{"bar":1234}}' | jq .foo.bar 1234
Analyze Apache Logs
sudo apt install goaccess goaccess access.log -c
Pretty Print JSON (Python installed)
echo '{"tutorials": 100, "url": "https://www.hascode.com/"}' | python -m json.tool
Result in the following output:
{ "tutorials": 100, "url": "https://www.hascode.com/" }
Pretty Print and colorize JSON (Python installed)
echo '{"tutorials": 100, "url": "https://www.hascode.com/"}' | python -m json.tool | pygmentize -l javascript
bash/zsh use cursor up/down keys for command history up/down
bindkey "^[[A" history-search-backward bindkey "^[[B" history-search-forward
Grep and Pipe – Keep color codes
grep --color=always | less
Execute last command as root
sudo !!
Copy to Clipboard from Console
Use xclip
sudo apt-get install xclip
Examples:
uptime | xclip
Clicking the third mouse button allows to paste the result in any x-application
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 restart
Connect:
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
Generate SSH Keys
ssh-keygen -t rsa -C "my@email.com"
Find out which application runs on a given port and protocol
sudo fuser -n tcp 8080
List your hardware
sudo lshw
Directory 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
Show installed files for a package
dpkg-query -L PACKAGE_NAME
Benchmarking the Network Speed between to Workstations
sudo apt-get install iperf
On the target workstation start iperf in server mode:
iperf -s
On the other workstation start the benchmark
iperf -c SERVER_IP_ADDRESS
Tell 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
Join multiple PDF Files with GhostScript
gs -dBATCH -dNOPAUSE -q -sDEVICE=pdfwrite -sOutputFile=output_file.pdf file1.pdf file2.pdf ...
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
Curl send JSON
curl -H "Content-Type: application/json" -XPOST -d '{"id":null, "title":"New book", "published":"2009-11-15T14:12:12"}' http://localhost:8080/javaee7-wildfly-liquibase-tutorial-1.0.0/rs/book
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 SimpleHTTPServer
Emulate 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
Neo4j / Cypher
Aggregate existing Labels
MATCH (n) RETURN DISTINCT labels(n)
Or unwinding label pairs
MATCH (n) WITH DISTINCT labels(n) AS labels UNWIND labels AS label RETURN DISTINCT label ORDER BY label
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;
MySQL Fix Zero Dates for Data Imports
Error: ERROR 1067 (42000) at line 1234: Invalid default value for ‘datefield’
In newer MySQL Versions zero in date or zero dates are forbidden .. check this with:
SHOW VARIABLES LIKE 'sql_mode';
In the import script, remove these both values: NO_ZERO_IN_DATE,NO_ZERO_DATE
e.g. like this:
SET sql_mode = 'ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION';
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
Create jhat webserver to show heap dump analysis
jhat -port 7401 -J-Xmx4G heap.bin
Caller / Calling class from Stacktrace
StackTraceElement[] stackTrace = Thread.currentThread().getStackTrace(); String className = stackTrace[2].getClassName();
Print Thread Dump with Deadlock Analysis
jcmd PID Thread.print
Recompile Class and Set Method Return to true
- Download Recaf from GitHub: https://github.com/Col-E/Recaf
- Open Jar file in Recaf
- Find the designated class and method
- Right-click on the method and select “Opcodes”
- Select the last line with opcode IRETURN
- Select “New Opcode before..”
- Select “Insn” in the first dropdown and “ICONST_1” as opcode from the second dropdown and click on “Add opcode”
- Save the modified Jar-file (File – Save) and enjoy
Set Timezone in Unit Test
@Before public void setup(){ TimeZone.setDefault(TimeZone.getTimeZone("UTC")); }
Starting Java with Debugging enabled
java -Xdebug -Xrunjdwp:transport=dt_socket,address=8000,server=y,suspend=y -jar target/vertx-websocket-chat-1.0.0-fat.jar
Debugging with jdb
Connecting
jdb -attach localhost:8000 -sourcepath /project/hascode-sample/src/main/java
Set breakpoint
stop in com.hascode.sample.Example.doSth(java.lang.String, java.lang.String)
Breakpoint hit
Outputs the thread-name (e.g. “thread=http-nio-8080-exec-1″), class-name, method-name, line-number and bci-counz (byte-code-instructions)
Exploring code at breakpoint
http-nio-8080-exec-1[1] list
Exploring data at breakpoint
http-nio-8080-exec-1[1] locals http-nio-8080-exec-10[1] print VARIABLE
Use JUnit 5 with Spring Boot
Use newer versions of Surefire and Failsafe plugins:
<properties> [..] <maven-failsafe-plugin.version>2.22.0</maven-failsafe-plugin.version> <maven-surefire-plugin.version>2.22.0</maven-surefire-plugin.version> </properties>
Remove JUnit from the test-starter
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> <!-- Exclude JUnit 4 from starter-test (and all other related test-starter, i.e those for security and project reactor --> <exclusions> <exclusion> <groupId>junit</groupId> <artifactId>junit</artifactId> </exclusion> </exclusions> </dependency>
Add JUnit5 dependencies
dependency> <groupId>org.junit.jupiter</groupId> <artifactId>junit-jupiter-api</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.junit.jupiter</groupId> <artifactId>junit-jupiter-engine</artifactId> <scope>test</scope> </dependency>
Add Failsafe Plugin
<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-failsafe-plugin</artifactId> </plugin>
Use SLF4J and Logback with Maven
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>
Get Class Information from Generic Class without Constructor Parameter
Class<T> entityClass = (Class) ((ParameterizedType) getClass().getGenericSuperclass()).getActualTypeArguments()[0];
Externalizable Example
package com.hascode.sample; import java.io.Externalizable; import java.io.IOException; import java.io.ObjectInput; import java.io.ObjectOutput; import java.time.ZonedDateTime; public class User implements Externalizable { private String name; private ZonedDateTime birthday; public User(){} public User(String name, ZonedDateTime birthday) { this.name = name; this.birthday = birthday; } public String getName() { return name; } public ZonedDateTime getBirthday() { return birthday; } @Override public String toString() { final StringBuilder sb = new StringBuilder("User{"); sb.append("name='").append(name).append('\''); sb.append(", birthday=").append(birthday); sb.append('}'); return sb.toString(); } @Override public void writeExternal(ObjectOutput out) throws IOException { out.writeUTF(name); out.writeObject(birthday); } @Override public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException { this.name = in.readUTF(); this.birthday = (ZonedDateTime) in.readObject(); } }
package com.hascode.sample; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.time.ZonedDateTime; public class Main { public static void main(String[] args) throws Exception { User user = new User("Fred", ZonedDateTime.now()); // serializing try (FileOutputStream fos = new FileOutputStream( "user.ser"); ObjectOutputStream outStream = new ObjectOutputStream(fos)) { System.out.printf("serializing user: %s\n", user); outStream.writeObject(user); } // deserializing try (FileInputStream fis = new FileInputStream( "user.ser"); ObjectInputStream in = new ObjectInputStream(fis)) { User user1 = (User) in.readObject(); System.out.printf("deserialized user: %s\n", user1); } } }
Lambda Expression implementing multiple Interfaces
Object r = (Runnable & Serializable) () -> {};
CDI / Weld Logging for GlassFish
add to domaindir/config/logging.properties
org.jboss.weld.level=FINE
Service Discovery using ServiceLoader
package com.hascode.api; interface MyService {} package com.hascode.impl1; class MyServiceImpl implements MyService {} package com.hascode.impl2; class AnotherMyServiceImpl implements MyService {} ServiceLoader serviceLoader = ServiceLoader.load(MyService.class); for (MyService s : serviceLoader) { System.out.println(s); } // SPI Mechanism to load from external jar-file package com.someotherprovider; class CustomMyServiceImpl implements MyService {} // create a file named com.hascode.api.MyService in META-INF/services with following content: com.someotherprovider.CustomMyServiceImpl
Disassemble Java Class
javap -c Sample.class
Importing Certificates into the Keystore
View cert > Details > Save to file (format: Base 64 X.509 / .CER)
keytool -import -file /tmp/downloadedCert.cer -keystore /tmp/myKeystore
Default password here is: changeit
Keystore passed as parameter to Maven:
mvn -Djavax.net.ssl.trustStore=/tmp/myKeystore
List Certificates in Keystore
keytool -list -keystore /etc/ssl/certs/java/cacerts
http://maven.apache.org/guides/mini/guide-repository-ssl.html
Spring Boot Quick Security Config
Maven Dependency:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-security</artifactId> </dependency>
Configuration class:
@Configuration @EnableWebSecurity public class SecurityConfig extends WebSecurityConfigurerAdapter { @Override protected void configure(HttpSecurity http) throws Exception { http .antMatcher("/info") .csrf().disable() .authorizeRequests() .anyRequest().hasRole("API_USER") .and() .httpBasic(); } @Autowired public void configureUsers(AuthenticationManagerBuilder auth) throws Exception { auth.inMemoryAuthentication().withUser("user").password("secret").roles("API_USER"); } }
Log4j 1.x log by package to different log files
Main class calling two classes in separate packages
package com.hascode.tutorial; import com.hascode.tutorial.alpha.Foo; import com.hascode.tutorial.beta.Bar; public class Main { public static void main(String[] args) { final String input = "hello, logger"; new Foo().doSth(input); new Bar().doSth(input); } }
Class in package com.hascode.tutorial.alpha
package com.hascode.tutorial.alpha; import org.slf4j.Logger; import org.slf4j.LoggerFactory; public class Foo { private final Logger log = LoggerFactory.getLogger(getClass()); public void doSth(String input) { log.info("doSth called with parameter {}", input); System.out.println("foo " + input); } }
Class in package com.hascode.tutorial.beta
package com.hascode.tutorial.beta; import org.slf4j.Logger; import org.slf4j.LoggerFactory; public class Bar { private final Logger log = LoggerFactory.getLogger(getClass()); public void doSth(String input) { log.info("doSth called with parameter {}", input); System.out.println("bar " + input); } }
Configuration file log4j.properties
log4j.rootLogger=DEBUG, CONSOLE log4j.logger.com.hascode.tutorial.alpha=DEBUG, ALPHA log4j.logger.com.hascode.tutorial.beta=DEBUG, BETA log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender log4j.appender.CONSOLE.Target=System.out log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout log4j.appender.CONSOLE.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c:%L - %m%n log4j.appender.ALPHA=org.apache.log4j.RollingFileAppender log4j.appender.ALPHA.File=./alpha.log log4j.appender.ALPHA.layout=org.apache.log4j.PatternLayout log4j.appender.ALPHA.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c:%L - %m%n log4j.appender.BETA=org.apache.log4j.RollingFileAppender log4j.appender.BETA.File=./beta.log log4j.appender.BETA.layout=org.apache.log4j.PatternLayout log4j.appender.BETA.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c:%L - %m%n
Running the program writes to console and following log files:
alpha.log
2016-05-19 15:22:30 INFO com.hascode.tutorial.alpha.Foo:10 - doSth called with parameter hello, logger
beta.log
2016-05-19 15:22:30 INFO com.hascode.tutorial.beta.Bar:10 - doSth called with parameter hello, logger
Full sources available here.
Java FX 2 Maven Dependency
<dependency> <groupId>com.oracle</groupId> <artifactId>javafx</artifactId> <version>2.2</version> <systemPath>${java.home}/lib/jfxrt.jar</systemPath> <scope>system</scope> </dependency>
Java FX 2 Fullscreen Stage
@Override public void start(final Stage stage) throws Exception { Screen screen = Screen.getPrimary(); Rectangle2D bounds = screen.getVisualBounds(); stage.setX(bounds.getMinX()); stage.setY(bounds.getMinY()); stage.setWidth(bounds.getWidth()); stage.setHeight(bounds.getHeight()); [...] }
WildFly Deploy via Console
Deploy
jboss-cli.sh --connect --command="deploy target/myapp-1.0.0.war"
Deployment Status
jboss-cli.sh --connect --command=deployment-info
Undeploy
jboss-cli.sh --connect --command="undeploy myapp-1.0.0.war"
Patching WildFly
$ sh jboss-cli.sh You are disconnected at the moment. Type 'connect' to connect to the server or 'help' for the list of supported commands. [disconnected /] connect [standalone@localhost:9990 /] patch apply ~/Downloads/wildfly-8.1.0.Final wildfly-8.1.0.Final-weld-2.2.2.Final-patch.zip wildfly-8.1.0.Final.tar.gz [standalone@localhost:9990 /] patch apply ~/Downloads/wildfly-8.1.0.Final-weld-2.2.2.Final-patch.zip { "outcome" : "success", "response-headers" : { "operation-requires-restart" : true, "process-state" : "restart-required" } }
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()); } }
Java Receiver Parameters
Available since Java 8 (link)
package com.hascode.tutorial; public class Foo { void foo(@SampleAnnotation1 Foo this) { } class Bar { Bar(@SampleAnnotation2 Foo Foo.this) { } } }
Add Class Diagrams to the JavaDocs using Maven and GraphViz
Step 1: Install graphviz:
sudo apt-get install graphviz
Step 2: Modify maven reports
<reporting> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-javadoc-plugin</artifactId> <version>2.10.3</version> <configuration> <doclet>org.umlgraph.doclet.UmlGraphDoc</doclet> <docletArtifact> <groupId>org.umlgraph</groupId> <artifactId>doclet</artifactId> <version>5.1</version> </docletArtifact> <show>private</show> <additionalparam>-all -constructors</additionalparam> <useStandardDocletOptions>false</useStandardDocletOptions> </configuration> </plugin> </plugins> </reporting>
Step 3: Generate JavaDocs using Maven
mvn site
Separating Unit and Integration Tests with JUnit Categories
Create a marker interface for the JUnit category:
package com.hascode; public interface IntegrationTest {}
Write an integration test and annotate it with @Category using the marker interface as value
import com.hascode.IntegrationTest; import org.junit.experimental.categories.Category; @Category(IntegrationTest.class) public class MyIntegrationTest { // ... }
Now configuring the Maven Surefire Plugin (Unit-Tests) and the Failsafe Plugin (Integration-Tests)
<plugins> <plugin> <artifactId>maven-surefire-plugin</artifactId> <version>2.19.1</version> <configuration> <excludedGroups>com.hascode.IntegrationTest</excludedGroups> </configuration> </plugin> <plugin> <artifactId>maven-failsafe-plugin</artifactId> <version>2.19.1</version> <configuration> <includes> <include>**/*.java</include> </includes> <groups>com.hascode.IntegrationTest</groups> </configuration> <executions> <execution> <goals> <goal>integration-test</goal> <goal>verify</goal> </goals> </execution> </executions> </plugin> </plugins>
Unit tests may be run now with mvn test, integration tests with mvn verify or mvn integration-test.
JAX-RS return a generic list in a Response
List<Book> list = new ArrayList<>(); GenericEntity<List<Book>> entity = new GenericEntity<List<Book>>(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; } }
Increase Eclipse Memory Settings
In the eclipse/configuration directory edit the config.ini
--launcher.XXMaxPermSize 128M -Xms1024m -Xmx2048m -XX:MaxPermSize=1048m
Different Logging Library Placeholders
SLF4J
log.info("{}, {}", "Hello", "World");
JULI Logger
log.log(Level.INFO, "{0}, {1}", new Object[]{"Hello", "World"});
Seam Logger
log.info("#0, #1", "Hello", "World");
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
Extract a Project’s Version
atlas-mvn org.apache.maven.plugins:maven-help-plugin:2.1.1:evaluate -Dexpression='project.version'| tail -n 9 | grep -v "\["
Add build number via resource filtering
<plugin> <groupId>org.codehaus.mojo</groupId> <artifactId>buildnumber-maven-plugin</artifactId> <version>1.4</version> <executions> <execution> <phase>validate</phase> <goals> <goal>create</goal> </goals> </execution> </executions> <configuration> <doCheck>true</doCheck> <shortRevisionLength>7</shortRevisionLength> </configuration> </plugin>
Afterwards the following placeholder may be used in a filtered resource in src/main/resources: ${buildNumber}
Pass JVM Arguments
On the command line:
mvn -DargLine="-Xmx8192m" compile
Since Maven 3.3.1 with a file in the project baseDir .mvn/jvm.config that contains this parameter .. e.g.:
-DargLine="-Xmx8192m"
Disable Plugin Execution using a Profile
Give the plugin execution an identifier and override this execution in the profile by setting the phase to “none”.
src/main/resources true config/domain.xml dbmigration/liquibase.properties src/main/resources false config/domain.xml dbmigration/liquibase.properties
Run build with 4 threads
mvn -T 4 clean install
Run build with 2 threads per core
mvn -T 2C clean install
Setup Maven Wrapper with local settings.xml
Create a file named maven.config in .mvn also as the designated settings.xml.
The maven.config contains this entry:
-s .mvn\settings.xml
Restrict Resource Filtering to Specific Files
E.g. filtering only one specific property file:
<resources> <resource> <directory>src/main/resources</directory> <filtering>true</filtering> <includes> <include>db/dbmigrations/liquibase.properties</include> </includes> </resource> <resource> <directory>src/main/resources</directory> <filtering>false</filtering> <excludes> <exclude>db/dbmigrations/liquibase.properties</exclude> </excludes> </resource> </resources>
Maven Plugin and Goal Information
Using the Maven Help plugin like this:
mvn help:describe -Dplugin=groupid:artifactid -Dgoal=thegoal -Ddetail=true
JIRA example:
atlas-mvn help:describe -Dplugin=com.atlassian.maven.plugins:maven-jira-plugin -Dgoal=compress-resources -Ddetail=true
Adjust java source/target version for compiler
<build> <plugins> <plugin> <artifactId>maven-compiler-plugin</artifactId> <configuration> <source>1.7</source> <target>1.7</target> </configuration> </plugin> </plugins> </build>
Or using the following properties:
<properties> <maven.compiler.source>1.9</maven.compiler.source> <maven.compiler.target>1.9</maven.compiler.target> </properties>
Javascript
Creating a Nodejs Module
1) Create a package.json using npm init
$ npm init This utility will walk you through creating a package.json file. It only covers the most common items, and tries to guess sane defaults. See `npm help json` for definitive documentation on these fields and exactly what they do. Use `npm install <pkg> --save` afterwards to install a package and save it as a dependency in the package.json file. Press ^C at any time to quit. name: (mymodule) mymodule version: (0.0.0) 1.0.0 description: hasCode.com Node Module entry point: (index.js) test command: git repository: https://github.com/hascode/hascode-nodejs-module.git keywords: tutorial test example author: Micha Kops license: (BSD-2-Clause) About to write to /tmp/mymodule/package.json: { "name": "mymodule", "version": "1.0.0", "description": "hasCode.com Node Module", "main": "index.js", "scripts": { "test": "echo \"Error: no test specified\" && exit 1" }, "repository": { "type": "git", "url": "https://github.com/hascode/hascode-nodejs-module.git" }, "keywords": [ "tutorial", "test", "example" ], "author": "Micha Kops", "license": "BSD-2-Clause", "bugs": { "url": "https://github.com/hascode/hascode-nodejs-module/issues" }, "homepage": "https://github.com/hascode/hascode-nodejs-module" } Is this ok? (yes) Yes
2) Create exports in index.js
exports.greet = function(){ console.log('greetings from hasCode.com :)'); }
3) Create a test js
var greeter = require('./index.js'); greeter.greet();
4) Test the module
$ node test.js
greetings from hasCode.com :)
Firebug XPath Testing Build in
$x("//div/span/em");
nvm, node, angular Installation
nvm installation:
curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.33.7/install.sh | bash
Add to ~/.profile:
export NVM_DIR="$HOME/.nvm" [ -s "$NVM_DIR/nvm.sh" ] && . "$NVM_DIR/nvm.sh" # This loads nvm
Test if installed by running
$ nvm
node installation with nvm
$ nvm ls-remote // select version $ nvm install v9.2.1
angular installation with node
npm install -g @angular/cli
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=
IntelliJ IDEA
Live Templates
JUnit 5 Test
@org.junit.jupiter.api.Test @org.junit.jupiter.api.DisplayName("$NAME$") void $METHOD$() throws Exception { $END$ }
Edit Variables:
- NAME
- METHOD: default-value camelCase(NAME)
SLF4J Logger Template
private static final org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger( $CLASS$.class );
IMG
Edit variables:
- CLASS: expression: className()
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 io.restassured.matcher.RestAssuredMatchers io.restassured.RestAssured org.hamcrest.MatcherAssert org.hamcrest.Matchers org.mockito.Mockito javaslang.API javaslang.Predicates
Template to insert a static logger instance
Go Windows > Preferences > Java > Editor > Templates > New …
Enter logger as name and as template:
${:import(org.slf4j.Logger,org.slf4j.LoggerFactory)} 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
Template for JUnit setup method using @Before
${:import(org.junit.Before)}; @Before public void setup() throws Exception { ${cursor} }
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>
Bitbucket – 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
JIRA – Detect current JIRA version
This utility class helps matching versions against the current version
package com.hascode.jira.workflow; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import com.atlassian.jira.util.BuildUtilsInfo; import com.google.common.primitives.Ints; public class JiraVersionHelper { private static final Logger logger = LoggerFactory.getLogger(JiraVersionHelper.class); private final BuildUtilsInfo buildUtilsInfo; public JiraVersionHelper(final BuildUtilsInfo buildUtilsInfo) { this.buildUtilsInfo = buildUtilsInfo; } public boolean currentVersionGreaterOrEqualThan(int major, int minor, int micro) { int[] version = { major, minor, micro }; if (Ints.lexicographicalComparator().compare(buildUtilsInfo.getVersionNumbers(), version) >= 0) { if (logger.isDebugEnabled()) { logger.debug("given version {}.{}.{} is greater or equal than the current JIRA version: {}", major, minor, micro, buildUtilsInfo.getVersion()); } return true; } if (logger.isDebugEnabled()) { logger.debug("given version {}.{}.{} is smaller than the current JIRA version: {}", major, minor, micro, buildUtilsInfo.getVersion()); } return false; } }
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
Move existing Tag pushed to remote to another Commit
Assuming that the tag has already been pushed to remote .. we’re doing …
- delete the tag local
- delete the tag remote
- tag specific commit
- pash tag remote
git tag -d <tagname> git push origin :refs/tags/<tagname> git tag <tagname> <commithash> git push origin <tagname>
Git Deactivate Fast-Forward-Merges per Default and per Branch
Using git config:
git config branch.master.mergeoptions "--no-ff"
Setting manually in the git configuration:
[branch "master"] mergeoptions = --no-ff
Git Contributor Stats for a given Period
git shortlog -s --number --since='last 2 year'
Convenience Alias for Force-With-Lease Push
git config --global alias.please "push --force-with-lease"
Remove sensitive file from Git history
git filter-branch –force –index-filter ‘git rm –cached –ignore-unmatch path/sensitive-file‘ cat — –all // force push
Search for occurance in a commit
git log --follow -S 'term' -- path/to/file
Merge and override with foreign branch changes
git merge --strategy-option theirs BRANCHNAME
Git Syncing with forked Repository
git remote -v git remote add upstream git@github.com:ORIGINAL_OWNER/PROJECT.git git remote -v git fetch upstream git checkout master git merge upstream/master
Git ignore versioned files
git update-index --assume-unchanged /path/to/file
Git Diff across Branches
git diff HEAD...other/branch -- path/to/file
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)
Docker
Search for image
docker search mysql
Run local mysql db
docker run --name local-mysql -e MYSQL_ROOT_PASSWORD=password -p 3306:3306 -d mysql:5.7
Run bash in container
docker exec -it NAME /bin/bash
Kubernetes
Cluster Info
kubectl cluster-info
Query Information
kubectl get nodes
kubectl get deployments
kubectl describe roles
kubectl describe services
kubectl logs
kubectl get all | less
kubectl describe jobs
kubectl describe endpoints kubectl top
Start Container
kubectl run some-name --image=image:label --port=8080
Execute Command in POD
kubectl exec PODNAME /bin/ls
Start HTTP Proxy to Kubernetes API
kubectl proxy
Expose as Kubernetes Service
using a deployment …
kubectl expose deployment/NAME --type="NodePort" --port 8080
Label a pod
kubectl label pod POD_NAME app=v1
Find pod by label
kubectl get pods -l app=v1
Delete Service
kubectl delete service -l app=v1
Scale Deployment Replicas
kubectl scale deployments/NAME --replicas=4
Create Deployment from YAML File
kubectl apply -f deployment.yaml
YAML Descriptor
apiVersion: apps/v1beta2 kind: Deployment metadata: name: nginx-deployment spec: selector: matchLabels: app: nginx replicas: 2 template: metadata: labels: app: nginx spec: containers: - name: nginx image: nginx:1.7.9 ports: - containerPort: 80
Store Information as Environment Variable
export POD_NAME=$(kubectl get pods -o go-template --template '{{range .items}}{{.metadata.name}}{{"\n"}}{{end}}')\necho Name of the Pod: $POD_NAME export NODE_PORT=$(kubectl get services/kubernetes-bootcamp -o go-template='{{(index .spec.ports 0).nodePort}}')\necho NODE_PORT=$NODE_PORT
Minikube
Starten
minikube start
Stoppen
minikube stop
IP Address
minikube ip
Services
minikube service nginx
XML / XSLT / XPath / Schema
Pretty Print XML in the Console using xmllint
echo '<blogs><blog url="https://www.hascode.com/">hasCode.com</blog></blogs>' | xmllint --format - <?xml version="1.0"?> <blogs> <blog url="https://www.hascode.com/">hasCode.com</blog> </blogs>
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">
Extract Nodes from XML via XPath in the console
xmllint --format --xpath '//plugin/name/text()' application.xml
PHP
WordPress anonymize IP in comments
Add as last line in theme’s functions.php:
function wpb_remove_commentsip($comment_author_ip) { return '127.0.0.1'; } add_filter('pre_comment_user_ip','wpb_remove_commentsip');
WordPress remove version info
Add as last line in theme’s functions.php:
function remove_version_info() { return ''; } add_filter('the_generator', 'remove_version_info');