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 {

    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:

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

You can clear git stashes using the command:
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-walk
2. 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

I was using Syntax Highlighter created by Alex Gorbatchev to highlight code snippets on this blog, but of late, it was not working or taking too long to load up. 
So it was time again to figure out a new way to do this and there were a few options that work in a similar fashion:
The other option was to pre-format code and then paste it into your post:

I found Code Prettier to be the simplest option that can get me closest to Syntax Highlighter and the setup is very easy - just need to add these lines to the theme HTML code:

<script src="https://cdn.jsdelivr.net/gh/google/code-prettify@master/loader/run_prettify.js"></script>
li.L0, li.L1, li.L2, li.L3,
li.L5, li.L6, li.L7, li.L8 {
  list-style-type: decimal !important;

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!
Note that the above code has a style override which makes line numbers show up on all lines of code. Without that block of style code, only every fifth line of could would have a line number.

Here is an example of the pre block that you need to use to syntax highlight your code:
 <pre class="prettyprint linenums lang-sh"> ... your code ... </pre>

You don't need to specify the language since prettyPrint will guess it, however you can specify a language by specifying the language extension along with the prettyprint class. Languages supported out of the box are:
"bsh", "c", "cc", "cpp", "cs", "csh", "cyc", "cv", "htm", "html", "java",
"js", "m", "mxml", "perl", "pl", "pm", "py", "rb", "sh", "xhtml", "xml","xsl"
You may also use the HTML 5 convention of embedding a <code> element inside the <pre> and using language-java style classes:
  <pre class="prettyprint"><code class="language-java">...</code></pre>
Note: Remember to escape your code using HTML entities :)


Related Posts Plugin for WordPress, Blogger...