get PID hogging a port

 i keep needing this but keep forgetting how to as well

netstat -abo | grep -A 1 '61618'

-a: display all connections and listening port

-b: display the executable involved

-o: display PID of the above

-A x: print x line following a match. This is important because on windows, the PID of the owning process is printed on the next line

Bash directory string expansion

 Nifty way to shorten commands involving multiple directories in bash.

{term1,term2,term3} in a directory string will be expanded and will result in 3 separate strings with each term inside the parentheses plugged in.

Examples:

  1. Removing files from multiple directories can be done with either

    rm /d/dir1/lib/xyz.jar /d/dir2/lib/xyz.jar /d/dir3/lib/xyz.jar

    or shorter

    rm /d/{dir1,dir2,dir3}/lib/xyz.jar

  2. Copying files FROM multiple directories to a single directory

    cp /d/{dir1,dir2,dir3}/lib/xyz.jar /c/doc

    is equal to longer copy command as follows

    cp /d/dir1/lib/xyz.jar /d/dir2/lib/xyz.jar /d/dir3/lib/xyz.jar /c/doc

  3. With xargs, the often needed task to copy files to multiple directories can also be done.

    xargs -n 1 cp -v xyz.jar <<< `echo /d/{dir1,dir2,dir3}/lib/`

    The above example is copying (verbosely) xyz.jar from current directory to 3 directories, namely /d/dir1/lib, /d/dir2/lib and /d/dir3/lib 

Port range reserved, but not used.

 So I have been pulling my hair about an issue: I'm trying to install openAM on my machine but it complained about not being able to bind to port 50389. netstat -aon | findstr 50389 returns empty line, indicating that the port in question is actually not used by any process. Digging around, I found this stackoverflow post about being unable to bind to port, tho that port is unused. In short, Windows let programs to reserve a range of ports while not using them, to prevent other programs to use that range. To tell which ports are reserved, you can run netsh int ip show excludedportrange protocol=tcp. To try to free port range: netsh int ip delete excludedportrange protocol=tcp numberofports=100 startport=49909. If you got Access Denied, it probably means the process reserving that port is currently running.

The author of the post thought turning off Hyper-V would release the reserved ports. I tried that and it worked in my case. 

Add new certificate to Java certificate store

I tried to deploy artifact to company's nexus when I got Java exceptions. I forgot the exact exception but it sounded something like SSL handshake failure and invalid certificate. After further investigation, I found out that the company's nexus is using self signed certificate for its SSL. This certificate is not recognized by Java because it is not yet in Java's keystore. It needs to be added to the keystore.
First step would be to find the keystore. There can be multiple Java installations and maven is using one of them. The keystore is located in jre/lib/security/cacerts.
The content of the keystore can be dumped using the command:
keytool -list -v -keystore /path/to/cacerts  > java_cacerts.txt
The default password is changeit.

To add new certificate to the keystore:
keytool -import -alias certificatealias -keystore  /path/to/cacerts -file certificatefilename
Certificate alias can be anything.

Maven created web app does not scan for annotations

I created webapp using maven and it behaved oddly. Whenever there is web.xml file, then annotations will be ignored. So if you have servlets not registered in web.xml but annotated, http requests won't be forwarded to them. Annotations are only scanned if you remove the web.xml. After hours of digging, I found out the root cause of the problem is because maven creates the most ancient version of web.xml which is version 2.3.

<!DOCTYPE web-app PUBLIC

"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"

"http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app>

<display-name>Archetype Created Web Application</display-name>

</web-app>

You need to have web.xml version 3 to make web.xml takes precedence over annotations while still having the servlet container scan for annotations.

<web-app xmlns="http://java.sun.com/xml/ns/javaee"

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"

version="3.0">

I am unaware of any earlier version of web.xml that behaves like version 3.

Maven debug

When running a Java app using maven and you need to debug it, simply pass debugging option as Maven option and then connect to it using remote debug from IDE.


MAVEN_OPTS="-Xdebug -Xnoagent -Djava.compiler=NONE -Xrunjdwp:transport=dt_socket,address=4000,server=y,suspend=y" mvn jetty:run

Intalling Websphere AS for Developers on Centos via command line

WAS for Developers (the full version, not the liberty version) turns out to be not quite straightforward to install on Centos using only the command line (since my Centos installation is a minimal one). So I decided to wriote down my experience installing it.

Preparing Centos:

  1. yum install "Development Tools"
  2. If your Centos is 64 bit, you need to install some 32 bit libraries to make IBM Installation Manager works... yum provides \*/libgcc_s.so.1 then pick from the list the package for 32 bit and yum install it.
  3. Open 9080 and 9060 (the former application port, latter management port of WAS). firewall-cmd --zone=public --add-port=9080/tcp --permanent. Do that for 9060 too and then firewall-cmd --reload.
Installation of WAS
  1. Download IBM installation manager (IIM) for linux. As of this writing, the URL is: https://www-01.ibm.com/marketing/iwm/iwm/web/download.do?source=swerpws-wasdevdl85&S_PKG=500023135&S_TACT=109J84JW&S_CMP=web_opp_ibm_ws_appinfra_wasdev&lang=en_US&dlmethod=http
  2. Download WAS for Developers, Installation Managers Repository. Same URL as above. It comes in three zip files, each 1 GB.
  3. Unzip IIM, run userinstc -c. It will install IIM to a directory of your choice.
  4. When you finish installing IIM, it will ask whether to restart IIM. Choose cancel.
  5. Unzip all three WAS for Developers zip files to one folder. This folder is your repository.
  6. In order to install WAS without GUI, you need what IBM calls response file: https://www-01.ibm.com/support/knowledgecenter/SSEQTP_8.5.5/com.ibm.websphere.installation.nd.doc/ae/cins_WASv85_nd_install_Win32.html
  7. My response file is in the end of this post.
  8. Go to IIM install folder, navigate to eclipse/tools subfolder. Run imcl. imcl input full-path-to-response-file -log logfile -acceptLicense
  9. After WAS is installed, go to WAS install directory and bin subdir and run manageprofiles. manageprofiles.sh -create will create default profile.
  10. Now, WAS can be started. startServer.sh server1.
This is my response.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE agent-input [
<!ENTITY home "USERHOMEDIR">
]>
<!--The "acceptLicense" attribute has been deprecated. Use "-acceptLicense" command line option to accept license agreements.-->
<agent-input acceptLicense="true">
<server>
<repository location="/code/was" />
</server>
<profile id="IBM WebSphere Application Server V8.5" installLocation="/code/IBM/WebSphere/AppServer" />
<install modify="false">
<offering id="com.ibm.websphere.DEVELOPERSILAN.v85" profile="IBM WebSphere Application Server V8.5" features="core.feature,ejbdeploy,thinclient,embeddablecontainer,com.ibm.sdk.6_64bit" installFixes="none" />
</install>
<preference name="com.ibm.cic.common.core.preferences.eclipseCache" value="/code/IBM/IMShared" />
<preference name="com.ibm.cic.common.core.preferences.connectTimeout" value="30" />
<preference name="com.ibm.cic.common.core.preferences.readTimeout" value="45" />
<preference name="com.ibm.cic.common.core.preferences.downloadAutoRetryCount" value="0" />
<preference name="offering.service.repositories.areUsed" value="true" />
<preference name="com.ibm.cic.common.core.preferences.ssl.nonsecureMode" value="false" />
<preference name="com.ibm.cic.common.core.preferences.http.disablePreemptiveAuthentication" value="false" />
<preference name="http.ntlm.auth.kind" value="NTLM" />
<preference name="http.ntlm.auth.enableIntegrated.win32" value="true" />
<preference name="com.ibm.cic.common.core.preferences.preserveDownloadedArtifacts" value="true" />
<preference name="com.ibm.cic.common.core.preferences.keepFetchedFiles" value="false" />
<preference name="PassportAdvantageIsEnabled" value="false" />
<preference name="com.ibm.cic.common.core.preferences.searchForUpdates" value="false" />
<preference name="com.ibm.cic.agent.ui.displayInternalVersion" value="false" />
<preference name="com.ibm.cic.common.sharedUI.showErrorLog" value="true" />
<preference name="com.ibm.cic.common.sharedUI.showWarningLog" value="true" />
<preference name="com.ibm.cic.common.sharedUI.showNoteLog" value="true" />
</agent-input>