Snippets

Contents

  1. Linux
    1. Create *nix timestamp
    2. Convert screencast video
    3. Cut ogv videos without reencoding with ffmpeg
    4. Gather RAM Information
    5. monitor udev events from usb port etc..
    6. JSON Command Line Filtering with JQ
    7. Analyze Apache Logs
    8. Pretty Print JSON (Python installed)
    9. Pretty Print and colorize JSON (Python installed)
    10. bash/zsh use cursor up/down keys for command history up/down
    11. Grep and Pipe - Keep color codes
    12. Execute last command as root
    13. Copy to Clipboard from Console
    14. TrueCrypt GUIless/console mount encrypted volume
    15. Enable MTP Support
    16. Convert all mp3's in a directory to aac
    17. Generate SSH Keys
    18. Find out which application runs on a given port and protocol
    19. List your hardware
    20. Directory tree comparison
    21. Recording audio from the command line to a mp3
    22. List files and dirs by size
    23. Backup directory using rsync with progress bar
    24. Debian/Ubuntu save a list of all installed packages and how to reinstall using this list
    25. Show installed files for a package
    26. Benchmarking the Network Speed between to Workstations
    27. Tell SVN not to store your password in .svn ..
    28. Execute function on shell script exit
    29. Join multiple PDF Files with GhostScript
    30. Curl send header
    31. Curl send POST request
    32. Curl get all response headers
    33. Curl send JSON
    34. Run Apache Benchmark Tool (10 Requests, 5 Concurrent)
    35. Screen detach session/ list  / reattach
    36. Join multiple videos into a single one
    37. Mount SSH filesystem with user mapping and symlinks
    38. netcat emulate http server
    39. Emulate a http server using python
    40. Emulate a mail server using python
    41. Colored and comfortable alternative to top
    42. Colorize Logs
  2. Neo4j / Cypher
    1. Aggregate existing Labels
  3. SQL
    1. Count unique / all values
    2. Partially anonymize e-mail addresses
    3. Find duplicate entries
    4. MySQL Fix Zero Dates for Data Imports
    5. Oracle Multiple Levels of Subtotals
    6. Postgres create roles
    7. Postgres set user password
    8. Postgres change database
  4. Java
    1. Show processes
    2. Create heap dump from running process
    3. Create jhat webserver to show heap dump analysis
    4. Caller / Calling class from Stacktrace
    5. Print Thread Dump with Deadlock Analysis
    6. Recompile Class and Set Method Return to true
    7. Set Timezone in Unit Test
    8. Starting Java with Debugging enabled
    9. Debugging with jdb
      1. Connecting
      2. Set breakpoint
      3. Breakpoint hit
      4. Exploring code at breakpoint
      5. Exploring data at breakpoint
    10. Use JUnit 5 with Spring Boot
    11. Use SLF4J and Logback with Maven
    12. Get Class Information from Generic Class without Constructor Parameter
    13. Externalizable Example
    14. Lambda Expression implementing multiple Interfaces
    15. CDI / Weld Logging for GlassFish
    16. Service Discovery using ServiceLoader
    17. Disassemble Java Class
    18. Importing Certificates into the Keystore
    19. Spring Boot Quick Security Config
    20. Log4j 1.x log by package to different log files
    21. Java FX 2 Maven Dependency
    22. Java FX 2 Fullscreen Stage
    23. WildFly Deploy via Console
    24. Patching WildFly
    25. Dynamic Proxy
    26. Java Receiver Parameters
    27. Add Class Diagrams to the JavaDocs using Maven and GraphViz
    28. Separating Unit and Integration Tests with JUnit Categories
    29. JAX-RS return a generic list in a Response
    30. JAX-RS / Jersey - Return a JSON Array for a Collection with a single element
    31. Increase Eclipse Memory Settings
    32. Different Logging Library Placeholders
      1. SLF4J
      2. JULI Logger
      3. Seam Logger
    33. Create heap dump on OutOfMemoryError
  5. Scala
    1. SBT - Eclipse Plugin
  6. Apache Webserver
    1. Deny all methods excepting POST and GET
    2. Rewrite all aliases for a domain to a single domain
  7. Maven
    1. Colourize output
    2. Extract a Project's Version
    3. Add build number via resource filtering
    4. Pass JVM Arguments
    5. Disable Plugin Execution using a Profile
    6. Run build with 4 threads
    7. Run build with 2 threads per core
    8. Setup Maven Wrapper with local settings.xml
    9. Restrict Resource Filtering to Specific Files
    10. Maven Plugin and Goal Information
    11. Adjust java source/target version for compiler
  8. Javascript
    1. Creating a Nodejs Module
    2. Firebug XPath Testing Build in
    3. nvm, node, angular Installation
    4. Create a custom jQuery selector
    5. Afterscriptexecute Event
    6. jQuery function to center an element
  9. Cascading Stylesheets / CSS
    1. Calculations
  10. Firefox
    1. Configure address bar to return search results
  11. IntelliJ IDEA
    1. Live Templates
      1. JUnit 5 Test
      2. SLF4J Logger Template
  12. Eclipse
    1. Favorites
    2. Template to insert a static logger instance
    3. Template for JUnit setup method using @Before
  13. Atlassian
    1. Disable YUI compressor
    2. Confluence - Get favourites by user
    3. Confluence - Determine the base URL
    4. Confluence - Get the context path
    5. Confluence - Using Velocity Template for a Macro
    6. Confluence - Perform a content search with restrictions
    7. Reference exported Web Resources like CSS/JS in a Velocity Template
    8. Webworks/XWorks Action - Rendering a template without the page frame
    9. Bitbucket - Stash - Enable a Package Logger via REST
    10. JIRA - Find existing locations for web-items and web-sections
    11. JIRA - Detect current JIRA version
    12. Confluence - Search for Spaces by Space Label
    13. Confluence - Read User Details
    14. ActiveObjects Create Entity with NotNull Constraint
  14. Git
    1. Show changes in a dedicated date range
    2. Move existing Tag pushed to remote to another Commit
    3. Git Deactivate Fast-Forward-Merges per Default and per Branch
    4. Git Contributor Stats for a given Period
    5. Convenience Alias for Force-With-Lease Push
    6. Remove sensitive file from Git history
    7. Search for occurance in a commit
    8. Merge and override with foreign branch changes
    9. Git Syncing with forked Repository
    10. Git ignore versioned files
    11. Git Diff across Branches
    12. Git setup a server using the git protocol
  15. Misc
    1. Data Normalizing
  16. Docker
    1. Search for image
    2. Run local mysql db
    3. Run bash in container
  17. Kubernetes
    1. Cluster Info
    2. Query Information
    3. Start Container
    4. Execute Command in POD
    5. Start HTTP Proxy to Kubernetes API
    6. Expose as Kubernetes Service
    7. Label a pod
    8. Find pod by label
    9. Delete Service
    10. Scale Deployment Replicas
    11. Create Deployment from YAML File
    12. YAML Descriptor
    13. Store Information as Environment Variable
    14. Minikube
  18. XML / XSLT / XPath / Schema
    1. Pretty Print XML in the Console using xmllint
    2. XSLT Strip Namespaces for Output
    3. Extract Nodes from XML via XPath in the console
  19. PHP
    1. Wordpress anonymize IP in comments
    2. Wordpress remove version info
 

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

  1. Enter about:config in the address bar
  2. Search for the key keyword.url
  3. 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$
 
}
IntelliJ IDEA Live Template: JUnit 5

IntelliJ IDEA Live Template: JUnit 5

Edit Variables:

  • NAME
  • METHOD: default-value camelCase(NAME)
IntelliJ IDEA Live Template: JUnit 5 - Variables

IntelliJ IDEA Live Template: JUnit 5 - Variables

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 …

  1. delete the tag local
  2. delete the tag remote
  3. tag specific commit
  4. 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');

List of all WordPress Hooks

Search
Categories