Monday, January 5, 2015

Usefull linux commands

  • find folderABC -mtime -9 -- show files in and below folder ABC modified less than 9 days ago
  • find ~ -name font* -print  -- show files and folders in my home folder and below starting with the work 'font'
  • find . -type f \( -iname *.jpg -o -iname *.jpeg \)  -- find only files with extension jpg or jpeg
  • find . -type f -path ./.config -prune -false -o -iname.jpeg  -- skip files with and 'Android' in path
  • ps
    • ps aux | grep myprocesspartialname - not no '-' before the options
    • /bin/ps -context -www -p processID -- shows wide command line for the process
  • svn status | grep '^M' -- shows files modified in the current SVN copy
  • svn diff -r HEAD fileABC -- compares the file ABC to its most recent version in the repo
  • zip+unzip:
    • unzip zip-file -d destination-folder  -- unzip a file
  • tar+gzip:
    • tar -ztvf file.tar.gz -- list contents of gzipped tar archive file.tar.gz
    • tar -czf file.tar.gz file1 file2 file3 -- create a gzipped archive file.tar.gz with three files in it
    • tar -xzf tar-file-name.tar.gz
  • bzip2: 
    • to unpack a bz2 file use command:  bzip2 -dk filename.bz2
  •  vim:
    • search - just like in vi, by hitting / you can search by specifying a regex pattern
    • to search for a whole word: \<...\>
    • to escape a special char, for example to search for a path, do: \/opt\/jboss
    • to cut lines into a vim internal clipboard, use dd command.
    • option :set-nowrap turns of the default, and sometimes annoying, line wrapping
    • to toggle word wrapping, use command:  :set wrap!
    • to do case-ignore search do /\cAlicja, and to do case sensitive search use \C
    • to mark a location type m L, where L is any letter, e.g. m a, and to move back to this location type: ' L (single apostrophe)
    • to place the current line at the top, center or bottom, hit zt, z., or zb.
  •  symbolic link: ln -s target link-name
    To me, it seems backward, but I guess you can say it like this: take target and call it link-name 
  •  folders: 
    •  remove: rm -r folder-name
      (note: rmdir will delete only an empty folder)
    • copy: cp -r folder-name dest-folder-name
    •  with -f option, you can avoid any questions 
    • by convention: folder/* means contents of the folder, while folder/ means all of the folder 
  • diff files and folders
    •  -wBb file1 file2 -- diff two files with all whitespaces ignored (new lines are not ignored-this is a much more complex problen, even just conceptually) 
    • -r dir1 ird2 -- recursively, shows files found only in one of the trees, for binaries shows message "files different"
  • du which tells "how much of the precious space does a this folder or files use":
    • du -sh my-folder/*  -- shows size of all files and folders in my-folder folder, one number per file/folder
  • df -h  which shows you the list of the files systems and which of them full if any.
  • grep
    •  ignoring the useless Permission denied entries.  Can be done using a redirection: grep my-cat-Lois my-folder/Lois-files 2> /dev/null
    • with a regex.  The simplest regex to find one or more tabs in the file was:
      grep '^This[[:space:]]\+5' parms*.txt
    • to show only the file names, use option -l
    • to search through compressed .bz2 files, use bzgrep instead of grep
    • to search for words, use option -w
  • su and sudo  -- both can take a user as a parm (superuser by default).  The su switches to that user until you do exit, while sudo is followed by a command, that only this one command is executed in the assumed user context.  
  • chown -R jboss:jboss_grp * --  change ownership of all files in this folder and below to user jboss, group jboss_grp.
  • adduser username -- add a new user and add user to the group. Once the user is created, to add user to the sudo group, repeat the same command with one more argument at the end: sudo
  • usermod -a -G sudo jdoe  -- add user to group sudo
  • passwd user1 -- change password for user1. Use option -e to have force user to change it at the first login.
  • awk -F '\t' ' /^Product\t/ {print $7} ' file.txt 
    • -F - Tab as a separator (deault: spc)
    • '...'  - program, inline  
    • default program structure: condition action
    •  Together: above command prints 7th (tab-separated) element of all lines of file file.txt that start with 'Product\TAB'
    • a bit more complex script:  { if( $0 ~ /^Fund\t/ && $10 == "False") {print $13} } -- It prints 13th element of the record, but only for lines starting with 'Fund', where the element 10th is 'False'.
    • and, if you need to process mutliple files, one option is a shell script:
      #!/bin/bash
      for var in $(ls /var/GOLL/import/exportPP_*.txt); do
        ls -l $var
        awk -F '\t' ' { if( $0 ~ /^Fund\t/ && $10 == "True") {print $3, $6, "ClosedDate:", $8, $10, $11, $12, $13} }' $var
      done
  • dmsg  show system messages, e.g. out-of-memory errors and processes killed as a result
  • working in command line:
    • to find a previous command you can use Ctrl-R (reverse search) 
  • less, tail and vim - you can open a file to view anypart of it using vim-like commands and switch to the 'tail -f' mode with Ctrl-F.  Then, Ctrl-C will take you back to viewing the whole file.
  • mkdir -p /var/dir1/dir2  -- create directory with all its missing parents
  • getfacl folder  -- show details of custome access specs for a folder (it shows as + on the long directory/file listing)
  • cat /etc/*release  -- show the name and version of the OS

Groups

  • groups [user]  -- show groups that user belongs to (by default - this user)
  • groupadd grouptoadd  -- creates a new group
  • cat /etc/group  -- shows one line per group with all its members; grep the list as needed
  • gpasswd -a user group  -- adds user to the group (-d for delete from the group)

Friday, November 14, 2014

hashCode() vs. equals() vs. Hibernate: Are These Two Persons Exact Twins?

Basics:
  • In general, even when using Hibernate or other ORM, don't have to overwrite the Object's hashCode() and equals().
  • However, you need to do it if both conditions below are true in your case:
    • you use dettach() with later reAttach(), and
    • you use your identity in Sets
  • Hibernate uses equals() (and/or hashCode() ?) only to tell that two objects represent the same entity and not whether it's values have changed (it iterates over the attributes for this purpose).
  • If you use entities in a Set, never change any component of the hashCode() while the object is in the Set. The best way is to make the business key immutable.
  • For overwriting hashCode()/equals(), you can use EqualsBuilder and HashCodeBuilder from the Apache Commons Lang library. Or semit-auto generate them both, always both of them, using eclipse.
 So, to tell whether the data is different/same between two objects, don't use hashCode() nor equals(), but create another method.

Saturday, November 1, 2014

Find-like and Grep-like Tools in Windows

Search by File Name

  • open CMD.exe
  • use command (more info): dir *.class *.java /b/s

Search by File Content

  • open CMD.exe
  • use command:
    findstr /s /i mock *.java *.groovy
  • what it does:
    • search not only in the current directory but also in subdirectories (/s)
    • searches for the string "mock", ignoring the case (/i)
    • in all .java and .groovy files
  • to find a string "pink rose", do:
    findstr /s /i /c:"pink rose" *.java *.groovy
    Without the "/c:", you will get all occurances of pink and rose.
  • option /r allows to use regular expression, np.:
    findstr /i /r /c:"references .*\.country" *
  • more info
(tested in win7)

Monday, September 29, 2014

JPA Criteria API

When you join a table with JoinType.LEFT and join another to that one, you may need to use JoinType.LEFT again, even when there should be a record in the third table for each record in the second table.

Why?

Tuesday, September 18, 2012

Eclipse Quirks

Here's a garden variety of Eclipse quirks and gotschas:

  • I frequently use Ctrl-, and Ctrl-. to move between next and previous usage of the current variable. It suddenly stopped working recently and to my surprise I, found that this functionality requires the two following settings to be set correctly:
    • the Next Annotation needs to have Occurances checked (it can be accesses from the standard toolbar under a button with an arrow and a dot), and
    • the Toggle Marke Occurances needs to be on (button with a highlighter)
  • A maven project got stuck with an old set of dependencies and Maven plugin Update Project Configuration doesn't help, even if repeated 7 times.
    Answer: When you do the Update Project Dependencies, check the Force Update of Snapshots/Releases.
    Background: May be, for maven gurus, it should have been obvious what I was missing, but that just means I'm not one of them.  It's true, that only SNAPSHOT and RELEASE dependencies were stuck, and yet it took me almost two hours of tearing my hair off.

Friday, December 16, 2011

Java Project Layout - Best Practices

Simple Project
  • project-name
    • trunk
      • src...
      • pom.xml
    • branches
      • branch-abc
        • src...
        • pom.xml
    • tags...

 Multi-Module Project
  • project-name 
    • project-name-parent
      • pom.xml - defines all modules that are part of the project
    • module 1 
      • trunk
        • src...
        • pom.xml - points to the parent pom.xml
      • branches
      • tags
    • module 2
      • trunk
        • src...
        • pom.xml - points to the parent pom.xml
      • branches
      • tags
    • module ...
 The key point is that the parent folder doesn't define the parent project, but is just a container for the parent and the children-modules.

Advantages:
  • Nicely groups the modules under one folder.
  • Allows to build the whole project by calling "mvn clean install" from parent's folder.
  • Keeps the SVN (and/or Git) operations simple, unlike the layout where the project-name-parent is the parent folder.

    Thursday, November 17, 2011

    Project Versioning and Release - Best Practice

    • A new project starts usually as version 1.0.0.0-SNAPSHOT. Three number versions (1.0.0) are also used but, for example, ServiceMix OSGi server requires three dots.
    • A new feature/fix is frequently developed on the trunk and committed there when ready for release.
    • Release. It is usually done on the trunk (does it have to be? what if someone commits changes in the middle of a release?). It is all done by maven release plugin, (more datails here). Is done in two steps: mvn release:prepare i mvn release:perform. What maven does during a release:
      • up the version by 1 (often on the last numbered position: 1.0.0.1)
      • build, maven-release and deploy to organization's repository if any
      • tag it in SCM
      • up the version number on the trunk (it will become 1.0.0.1-SNAPSHOT)
    Notes:
    • On the trunk, we have always -SNAPSHOT versions
    • Each release has a tag
    • If the maven release plugin fails, it usually leaves the pom.xml messed up on the trunk.  Make sure to fix the version and scm tags. 
    • Alternatively,  a new feature/bug fix can be developed on a branch.  In this case, the branch needs to be merged to the trunk, when feature/bug fix is ready. I guess, this merge should happen before the release.
    • In multi-module projects,  if you want to give the same new version to all modules, add set the option autoVersionSubmodules to true (in parent pom?  where?). Otherwise, you'll be asked for the new version for each module.