środa, 23 grudnia 2015

Quick fix for non-responding keyboard in Ubuntu

In order to unlock keyboard just restart ibus deamon
ibus-daemon -rd
PS. Check batteries in your keyboard if restarting does not help :)

Fun with awk - how to aggregate column values in bash

How to aggregate values in particular column

for i in $(ls); do du -sm $i; done | awk '{s+=$1;print s,$0} END{print "S:",s}'

środa, 16 grudnia 2015

Quick fix for disappeared time panel in Unity top bar

Forcing restart of unity-panel by killing it do the job. Ingenious ;D.
sudo killall unity-panel-service

niedziela, 13 grudnia 2015

How to deploy chosen artifacts of multimodule maven application

In case there is parent module you need to install also this parent pom.
  REPO_ID=myRepoIdDefinedInSettingsXml && \
  REPO_URL=http://example.com/nexus/content/repositories/releases && \
  VERSION=2.3.1 && \
  /usr/local/bin/mvn deploy:deploy-file \
     -DgroupId=${GROUP_ID} \
     -DartifactId=${PARENT_ARTIFACT_ID} \
     -Dpackaging=pom \
     -Dversion=${VERSION} \
     -DrepositoryId=${REPO_ID} \
     -Durl=${REPO_URL} \
     -Dfile=build/release-*/pom.xml && \
Once parent module is installed (but in fact doesn't need to be first) we can install our modules (MODULE1, MODULE4 and MODULE_N). Installing sources is optional (if your package phase does not generate source bundle - just skip -Dsource option.
for ARTIFACTID in MODULE1 MODULE4 MODULE_N; do \
    for FILE in $(find target/${ARTIFACTID} -name "${ARTIFACTID}*.jar" | grep -v sources); do \
      /usr/local/bin/mvn deploy:deploy-file \
        -DgroupId=${GROUP_ID} \
        -DartifactId=${ARTIFACTID} \
        -Dversion=${VERSION} \
        -DpomFile=$(dirname ${FILE})/../pom.xml \
        -Dpackaging=jar \
        -DrepositoryId=${REPO} \
        -Durl=${REPO_URL} \
        -Dfile=$(dirname ${FILE})/${ARTIFACTID}-${VERSION}.jar \
        -Dsources=$(dirname ${FILE})/${ARTIFACTID}-${VERSION}-sources.jar; \
    done; \
  done; \

How to run sql script against H2 file database with single command

There are cases when the quickest way to fix database is by running some sql script. Assuming you have used h2 in the past you're most likly have h2 driver in your m2 repo. Once you found it try the following:
 java -cp h2*.jar org.h2.tools.RunScript -url <URL> -script init.sql
where URL is default jdbc string and can be jdbc:h2:~/test

wtorek, 17 listopada 2015

How to get around git aliases?

Are you tired with typing the same long git commands? Here you are the solution. GIT ALIASES!!

Here you have a bundle of most useful:

git config --global alias.hist "log --pretty=format:'%h %s%d [%an]' --graph --date=short" 
git config --global alias.lol "log --graph --decorate --pretty=oneline --abbrev-commit --all"
git config --global alias.sts status
git config --global alias.co checkout
git config --global alias.newbranch 'checkout -b'
git config --global alias.last 'log -1 HEAD'
git config --global alias.unstage 'reset HEAD --'

poniedziałek, 12 października 2015

[PDE] How to find out all plugins that given extension point is used

There are two ways to do so from PDE UI level. First is by opening "Open Plug-in Artifact" view (Ctrl + Shift + A) - technically org.eclipse.pde.internal.ui.search.dialogs.FilteredPluginArtifactsSelectionDialog class.

Once you type "expressions.propertyTesters" in filter box and deselect all but "Show Extension" item hidden under black triangle on the right, the matching items will contain all plugins are using "org.eclipse.core.expressions.propertyTesters" extension point.

The another way is using default Search Dialog (Ctrl + H). The last tab is "Plug-in Search".

  • Search For: Extension Point
  • Limit To: References
This time you need type fully qualified id or use wild card (*expressions.propertyTesters)

niedziela, 11 października 2015

How to programmatically open ProblemsView in Eclipse

In order to open up a default "Problem View" view we need invoke showView method of on IViewPart instance. Here you are little snippet opening view as a separate job.
Job job = new Job("Opening problems view") {
 @Override
 protected IStatus run(IProgressMonitor monitor) {
  IWorkbench workbench = PlatformUI.getWorkbench();
  try {
   workbench.getWorkbenchWindows()[0].getActivePage().showView("org.eclipse.ui.views.ProblemView");
  } catch (PartInitException e) {
   e.printStackTrace();
  }
  return Status.OK_STATUS;
 }
};
job.schedule(500);

środa, 23 września 2015

Generic JSON SerDe with Jackson2

import com.fasterxml.jackson.annotation.JsonTypeInfo.As;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

static class SerDe<T> {
 
 public static <T>  String serialize(T obj) throws JsonProcessingException {
  ObjectMapper mapper = new ObjectMapper();
  mapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL, As.PROPERTY);
  return mapper.writeValueAsString(obj);
 }
 
 public static <T> T deserialize(String s, Class<T> clazz) throws JsonProcessingException, IOException {
  ObjectMapper mapper = new ObjectMapper();
  mapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL, As.PROPERTY);
  return mapper.readerFor(clazz).readValue(s);
 }

}

czwartek, 9 kwietnia 2015

Remmina, Freerdp - how to change keyboard layout on target windows machine

Cannot type underscore sign - question mark is put instead? Most probably keyboard layout is wrong. Check what keyboard layout you have set at the moment.
$ setxkbmap -query
rules:      evdev
model:      pc105
layout:     pl,us
Above we can see two layouts in order: "pl" followed by "us". Lets switch them.
setxkbmap -layout us,pl
Now try to reconnect to the server. Underscore is again underscore ;)

piątek, 3 kwietnia 2015

3 most appealing WYSIWYG HTML editor widgets

Three most promising and most appealing WYSIWYG online html editor widgets


1. Quill

Quill is a free, open source WYSIWYG editor built for the modern web. With its extensible architecture and a expressive API you can completely customize it to fulfill your needs.

Some built in features include:
  • Fast and lightweight
  • Semantic markup
  • Standardized HTML between browsers
  • Cross browser support including Chrome, Firefox, Safari, and IE 9+


  • Get fine-grained access to editor contents and event notifications.
  • Works across all modern browsers on desktops, tablets and phones.
  •  It's easy to add custom behavior or modifications on top of Quill.
  • Quill is open source. 



URL: http://quilljs.com

2. Aloha

Aloha Editor provides you capabilities to create editing experiences embedded seamlessly in your web application.
 
  • Use the clean and modern user interface of Aloha Editor, customize it or use the one you already have.
  • Aloha Editor is a stand alone library with a functional and stateless API that provides you with essential editing capabilities not available in browsers.
  • GPL v2 or commercial license

URL: http://www.alohaeditor.org

3. Froala


URL: https://editor.froala.com

wtorek, 17 marca 2015

This account is currently not available - how to execute command as user with nologin shell set

How to run a command as a user without bash shell set?

There is actually one nifty way to do this - use the su command with combination of two options s and c.
# su -s /bin/bash -c 'touch /tmp/file' 
What actually will happen here is basically overriding configured shell with the one passed with "-s" option.

How to run a long operation with a spinner/hourglass indicator within eclipse plugin (swt application) ?

From time to time there is a need to run some relatively long operation synchronously. In such a case will be good to indicate the fact to the user. There is very simple way of doing this with BusyIndicator class. Just replace syncExec with BusyIndicator.showWhile.
Display.getCurrent().syncExec(new Runnable() {
    @Override
    public void run() {
           // long operation
    }
});
with the following:
BusyIndicator.showWhile(Display.getCurrent(), new Runnable() {
    @Override
    public void run() {
           // long operation
    }
});

How to invoke java method with a time limit?

Java has no handy way to do so. You can always use Thread to run a method and after some time out interrupt it.

import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

public class Test {

 public static void main(String[] args) throws InterruptedException, ExecutionException, TimeoutException {

  ExecutorService executor = Executors.newSingleThreadExecutor();
  Future future = executor.submit(new Runnable() {
   @Override
   public void run() {
    try {
     TimeUnit.DAYS.sleep(1);
    } catch (InterruptedException e) {
     e.printStackTrace();
    }
   }
  });

  try {
   future.get(5, TimeUnit.SECONDS);
  } catch (InterruptedException e) {
   future.cancel(true);
   throw e;
  } catch (TimeoutException e) {
   future.cancel(true);
   throw e;
  }
 }

}

środa, 11 lutego 2015

Dockerfile - how to run bash script without using external files

The usual way of smuggling external scripts to docker image builds is by adding them from directory where Dockerfile is located (ADD command). But this cause a need to maintain external resources which is not so handy, especially when scripts are rather short. The workaround is to echo all the commands and pass them to sh:
RUN echo -e "su -c \"echo 'N' | hdfs namenode -format\" hdfs \n\
su -c \"hdfs datanode 2>&1 > /var/log/hadoop-hdfs/hadoop-hdfs-datanode.log\" hdfs& \n\
su -c \"hdfs namenode 2>&1 > /var/log/hadoop-hdfs/hadoop-hdfs-namenode.log\" hdfs& \n\
sleep 5 \n\
/usr/lib/hadoop/libexec/init-hdfs.sh \n\
killall java \n\
" | sh

wtorek, 3 lutego 2015

GIMP - how to export image to base64

That was somewhat extremely surprising to me that GIMP does not provide Base64 exporter out of the box (but I am not the GIMP expert - maybe there is ;)). Nonetheless, as it seems to me a quite straightforward to implement such feature, I faced the challenge. After short googling and refreshing deep buried Python knowledge, it became simple, amazingly simple in fact. Here you are some details in three simple steps: 0. Open "Python Console" (don't be afraid, it does not bite you, or choke either ;)
Go to "Filters" menu -> "Python-Fu". The python console should appear.
1. Take an image
img = gimp.image_list()[0]  // assuming you'd got just one image opened
2. Read all bytes
bytes = open(img.filename, "rb").read()
3. Encode bytes with 'base64' algorithm
bytes.encode('base64')
Done.

And for "one-liner" fanboys, we can compact it into one line:

open(gimp.image_list()[0].filename, "rb").read().encode('base64')
bytes.encode('base64') puts some new-line characters, alternatively you can use the following:
import base64
base64.b64encode(open(gimp.image_list()[0].filename, "rb").read())
Reference: Python Scripting Official Doc

poniedziałek, 2 lutego 2015

How to obtain Eclipse absolute installation path programmatically

The easiest way is to use org.eclipse.core.runtime.Platform class and its static getInstallLocation method.
Location installLocation = Platform.getInstallLocation();
A Location interface is defined in OSGi specification and basically represents an java.net.URL object. Additionally it allows creating nested locations and mechanism for locking locations. The default and the only one shipped implementation is BasicLocation, but it is internal class and just pure implementation (no other fancy features ;) To obtain path as a String user the following code:
String absolutePath = Platform.getInstallLocation().getURL().getFile();
Alternatively you can read the content
InputStream is = Platform.getInstallLocation().getURL().getContent();

środa, 28 stycznia 2015

Why running docker command returns "/var/run/docker.sock: permission denied"

/var/run/docker.sock: permission denied

$ docker ps
FATA[0000] Get http:///var/run/docker.sock/v1.16/containers/json: 
dial unix /var/run/docker.sock: permission denied.
Are you trying to connect to a TLS-enabled daemon without TLS?
First quick look at the /var/run/docker.sock file.
$ ls -l /var/run/docker.sock
srw-rw---- 1 root docker 0 sty 28 11:53 /var/run/docker.sock
The solution should be now clear - you can solve the issue by adding user you are logged in to the docker group.

So, let's do this:

$ sudo gpasswd -a ${USER} docker
There should be now a change in /etc/group file.
$ cat /etc/group | grep ^docker
Next you need start new terminal session to apply the change and check if you are in the docker group. Should be listed in the following command execution:
$ groups
If still cannot see docker in the groups you're in - and you run linux with graphical interface (eg Ubuntu) you may need basically restart machine. Once done restart your docker container (if you were not needed restarting machine in previous step ;)
$ sudo service docker.io restart
That's all, now docker ps should be no problem to run.

niedziela, 25 stycznia 2015

How to use rsync to synchronize two folders without overriding permissions

Very popular and easy to remeber rsync command is rsync -a source_dir target_dir . "-a" is equivalent of "-rlptgoD". What does it mean?? Let's analyze it step by step.
  • "-r" - reqursive
  • "-l" - copy symbolic links as symbolic links
  • "-p" - preserve permessions
  • "-t" - preserve modification dates
  • "-g" - preserve group
  • "-o" - preserve owner (works only with root account)
  • "-D" - preserve device files and special files
So to do the same as "-a" does we need to ommit "-p" switch.
rsync -rltgoD m/bdg .

wtorek, 13 stycznia 2015

Rsync - ERROR: rsync error: protocol incompatibility (code 2) at compat.c(171) [sender=3.0.6]

$rsync -av file.zip user@host:file.zip
protocol version mismatch -- is your shell clean?
(see the rsync man page for an explanation)
rsync error: protocol incompatibility (code 2) at compat.c(171) [sender=3.0.6]
This message can be somewhat misleading at first glance, but the key to solve this issue is answering what actually means "clean shell" ;). Most probably some MOTD is displayed in one of the config files (eg. .bashrc) when user starts new session. To check if it is a case try the following command:

ssh designer@10.1.1.162 true | wc -c
If this command returns 0 that means there is no output produced.

środa, 7 stycznia 2015

Ubuntu - how to download Oracle JDK with single command

Downloading jdk from oracle download web page requires passing a cookie with request header. The template:
curl -LO ${URL} -H "Cookie: oraclelicense=accept-securebackup-cookie"

Java 8

curl -LO http://download.oracle.com/otn-pub/java/jdk/8u25-b17/jdk-8u25-linux-x64.tar.gz -H "Cookie: oraclelicense=accept-securebackup-cookie"

Java 7

curl -LO  http://download.oracle.com/otn-pub/java/jdk/7u67-b01/jdk-7u67-linux-x64.tar.gz -H "Cookie: oraclelicense=accept-securebackup-cookie"

Java 9 - early access

curl -LO http://www.java.net/download/jdk9/archive/b44/binaries/jdk-9-ea-bin-b44-linux-x64-23_dec_2014.tar.gz -H "Cookie: oraclelicense=accept-securebackup-cookie"
Alternatively you can use wget command
wget --no-check-certificate --no-cookies --header "Cookie: oraclelicense=accept-securebackup-cookie" http://download.oracle.com/otn-pub/java/jdk/8u25-b17/jdk-8u25-linux-x64.tar.gz