Friday, December 30, 2022
Disable automatic backslash insertion before $ when using auto-complete in bash
Thursday, May 6, 2021
Extract files from RPM package without installing
An RPM package is a file consisting of a cpio archive that contains the files to be installed and a header that contains metadata information about the package.
You can use the simple utility tool rpm2cpio to convert the contents of an RPM package into a cpio archive and then use the cpio command to extract the contents of that archive without needing to install the RPM package.
rpm2cpio name_of_rpm_package.rpm | cpio -idmv
Note that this will extract the files to the current folder.
Here is an explanation of the options used for the cpio command:
- i: extract files
- d: create leading directories where needed
- m: preserve previous file modification times for the extracted files
- v: verbose
Wednesday, May 5, 2021
Handling Exceptions in EJB using Interceptors
In my previous posts Using EJB Interceptors to add functionality to existing Beans, and Using EJB Interceptors to execute code AFTER service method completes, we saw how to use EJB3 Interceptors to add common concern features like logging, etc. to J2EE service methods.
In case you want to have common exception handling logic for all service method calls, you can wrap the return context.proceed(); in a try-catch block and handle the exception in the catch block as shown below:
package com.my.ejb.interceptors;
import javax.interceptor.AroundInvoke;
import javax.interceptor.InvocationContext;
public class MyInterceptor {
@AroundInvoke
public Object interceptorMethod(InvocationContext context) throws Exception {
try {
System.out.println("Before invoking method: " + context.getMethod());
return context.proceed();
} catch (Exception e) { // You can catch ALL or ONLY Specific Exceptions as per your requirement
System.out.println("Exception in method " + context.getMethod() + ": " + e.getMessage());
} finally {
System.out.println("After invoking method: " + context.getMethod());
}
}
}
Once this interceptor is created, we just need to add an "@Interceptors" annotation to the target EJB as:
import javax.interceptor.Interceptors;
@Interceptors({com.my.ejb.interceptors.MyInterceptor.class})
Now you should see log lines "Before ..." before the the method executes and "After..." after the method executes and in case of any exceptions in the metod call, you will see "Exception ..."
Tuesday, May 4, 2021
Using EJB Interceptors to execute code AFTER service method completes
In my previous post Using EJB Interceptors to add functionality to existing Beans, we saw how to use EJB3 Interceptors to add common concern features like logging, etc. to J2EE service methods.
In case you want to run any piece of code after each service method call, you can wrap the return context.proceed(); in a try-finally block and add the "after" code in the finally block as shown below:
package com.my.ejb.interceptors;
import javax.interceptor.AroundInvoke;
import javax.interceptor.InvocationContext;
public class MyInterceptor {
@AroundInvoke
public Object interceptorMethod(InvocationContext context) throws Exception {
try {
System.out.println("Before invoking method: " + context.getMethod());
return context.proceed();
} finally {
System.out.println("After invoking method: " + context.getMethod());
}
}
}
Once this interceptor is created, we just need to add an "@Interceptors" annotation to the target EJB as:
import javax.interceptor.Interceptors;
@Interceptors({com.my.ejb.interceptors.MyInterceptor.class})
Now you should see log lines "Before ..." before the the method executes and "After..." after the method executes!
Tuesday, November 10, 2020
Using EJB Interceptors to add functionality to existing Beans
To add any decorations or cross-cutting features like logging, profiling or performance measurement for service method calls of existing enterprise beans, you can use EJB3 Interceptors that were provided starting with JavaEE5.
Interceptor is a method that wraps around the invocation of the actual business method call allowing you to apply the required feature and can either exist in the target class if it is very speficic to that class; or in an external class if it applies to multiple service methods. If it is a part of an external class, then it has to be the only method in that class having the annotation "@AroundInvoke"
This interceptor method takes the following form:
package com.my.ejb.interceptors;
import javax.interceptor.AroundInvoke;
import javax.interceptor.InvocationContext;
public class MyInterceptor {
@AroundInvoke
public Object interceptorMethod(InvocationContext context) throws Exception {
System.out.println("Going to invoke method: " + context.getMethod());
return context.proceed();
}
}
Once this interceptor is created, we just need to add an "@Interceptors" annotation to the target EJB as:
@Interceptors({com.my.ejb.interceptors.MyInterceptor.class})
And viola - it simply works, no need for any additionaly libraries or frameworks!
Note: Do not forget the return context.proceed(); call at the end of the interceptor method - this will allow the chain of interceptors to proceed and the target metod to be invoked!
Monday, November 9, 2020
Undo git stash clear
git stash drop stash@{stash_index}
### OR
git stash clear
If you need to recover very recently deleted stashes, you can try the following two commands to try to recover them:
1. List all the available stashes that can be receovered using the below command:
git fsck --unreachable | grep commit | cut -d ' ' -f3 | xargs git log --merger --no-walk2. Choose commit id of the stash that you want to restore and run the below command:
git stash apply [commit_id]
Wednesday, June 17, 2020
Syntax Highlighting Code in Blogger
- highlight.js : https://highlightjs.org/
- code prettifier: https://github.com/googlearchive/code-prettify
- dilinger.io : https://dillinger.io/
- github gist: https://gist.github.com/
- pastebin : https://pastebin.com/
- hilite.me : http://hilite.me/
<script src="https://cdn.jsdelivr.net/gh/google/code-prettify@master/loader/run_prettify.js"></script>
<style>
li.L0, li.L1, li.L2, li.L3,
li.L5, li.L6, li.L7, li.L8 {
list-style-type: decimal !important;
}
</style>
Once this is done, all you need to do is to put encoded code snippet in a pre block with the class set to prettyprint and viola!
<pre class="prettyprint linenums lang-sh"> ... your code ... </pre>
"bsh", "c", "cc", "cpp", "cs", "csh", "cyc", "cv", "htm", "html", "java", "js", "m", "mxml", "perl", "pl", "pm", "py", "rb", "sh", "xhtml", "xml","xsl"
<pre class="prettyprint"><code class="language-java">...</code></pre>
Saturday, March 24, 2018
Copy specific files from within nested folders
There are many ways to do this - but here is the simplest that I could figure out using rsync:
rsync -aihv --include='*/' --include='*.log*' --exclude='*' /source_folder/to/search/in/ /destination_folder/to/copy/to/
Important thing to note here is the order of include and exclude flags passed to rsync command - it is sensitive to the order in which these flags are provided.
First include flag '*/' makes rsync look into subfolders and the second flag selects all files of the format '*.log*'.
The next exclude flag excludes any file not selected as yet and then copies them over to the destination folder.
You can provide more include flags before the exclude flag to select multiple types of files as per your requirement.
Rename multiple files in one command
This requires us to bring out something different from our Unix arsenal - the "rename" command!
Use the rename command in conjunction with find to rename multiple files:
find /source_folder/to/search/in/ -name 'myapp*.log.201*' -exec rename -v .log. .oldlog. {} \; >> /temp/rename.log
Here we are searching for files named 'myapp*.log.201*' and renaming them to 'myapp*.oldlog.201*'
In case you need to do this on multiple servers, use the for loops trick!
Running same set of commands on multiple servers using for loop
Create a file containing a list of server names on which we want to check the logs - one on each line and then run this for loop if using bash:
for s1 in $(cat servers_list.txt); do
ssh -q ${s1} "grep 'search_string' /search/folder/*/*.log* | sed 's/^/${s1} => /'" >> /tmp/search.log
# Other commands here if required
done
If you are using csh, then the for loop syntax is slightly different:
foreach s1 in (cat servers_list.txt)
ssh -q ${s1} "grep 'search_string' /search/folder/*/*.log* | sed 's/^/${s1} => /'" >> /tmp/search.log
# Other commands here if required
end
The code is simple - for each server, ssh to that server and run the grep command to find out the log lines having the specific string. The bigger trick here is to use sed to pre-pend server name to the grepped log line to make the report useful. This should return a file with lines in the format:
server_name => [log line grepped from the log file]