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!


LinkWithin

Related Posts Plugin for WordPress, Blogger...