Thursday, 23 July 2020

Liferay OSGi framework

1. OSGi Framework provides runtime environment (container) and development environment (annotations, dependency injection)
Note: @Reference works for concrete classes only. That means not work for interface and abstract classes (getting null as object value) 

2. Component lifecycle - active, activated and deactivated

3. Component registered in OSGi Container and service registered in Service registry

4. invoke method (getTime()) from gogo shell - "osgi.command.function=getTime", "osgi.command.scope=custom"

5. We can determine multiple implementations for single interface by using 1. ranking, 2. target

6. Export-Package & Import-Package vs Provide-Capability & Require-Capability

7. Bundle lifecycle - 1. Installed, 2. Resolved, 3. Active, 4.

8. Spring beans generated by liferay service builder by which annotation can use/reference it ?
sol : @ServiceReference annotation.
Explanation : AssignmentServiceImpl is not OSGi component class. Hence, need to use @servicereference.

9. MVC Commands take care of the portlet life-cycle and interaction b/w UI & Back-end.
     a. MVC Commands are wired to a certain portlet life-cycle (render, action & resource)
     b. They respond to certain 'mvc.command.name' defined in component properties.
     c. they register to a certain portlet with the 'javax.portlet.name' component property.

10. To make silence the generated API under any xxxxLocalServiceImpl classes, simply override that API by @override and define to throw UnsupportedOperationException

11. what is the bundle State when its working as expected after deployment of fragment module ?
Sol : Resolved

12. how many ways override JSPs and what are those ?
Sol : 2 ways and  a. override that JSP by creating fragment module. b. update that JSP by creating render filter and customize the JSP output.

13. What is LifecycleAction and why will use it?
Sol : LifecycleAction allows us to customize liferay's application by using events. Events are 1. application.startup.events, 2. prelogin.events, 3. postlogin.events and etc.,

14. what is  the use of servicewrapper?
Sol : By using Servicewrapper we can customize service methods either pre or post execution.

15. How we can override struts action?

Sol : Our custom component class should extends BaseStrutsAction and property is "path=/portal/logout"

Sample :
1. Step 1 :  @Component(
2.        immediate = true,
3.        property = {
4.            "path=/portal/logout"
5.        },
6.        service = StrutsAction.class)

1.Step 2 :        @Override
2.        public String execute(
3.            StrutsAction originalStrutsAction, HttpServletRequest request,
4.            HttpServletResponse response)
5.            throws Exception {
6.    
7.            System.out.println("Overriding the Portal Logout Action!");
8.    
9.            originalStrutsAction.execute(request, response);
10.   
11.           response.sendRedirect("https://university.liferay.com");
12.   
13.           return null;
14.       }

16. How to override MVCActionCommand?
Sol : Our custom component class should extends BaseMVCActionCommand, and property is property = {
"javax.portlet.name=" + DLPortletKeys.DOCUMENT_LIBRARY,
"javax.portlet.name=" + DLPortletKeys.DOCUMENT_LIBRARY_ADMIN,
"javax.portlet.name=" + DLPortletKeys.MEDIA_GALLERY_DISPLAY,
"mvc.command.name=/document_library/edit_folder",
"service.ranking:Integer=100"
}
17. Model entity listener ?
Sol : Events we are able to listen for when an action on a Model Entity is executed in the persistence layer. There are a few use cases for Model Listeners such as an Audit Listener, Cache Clearing Listener, and Validation Listener. The use case we are implementing is the fourth use case, the Entity Update Listener.

Snippet : @Component(immediate = true, service = ModelListener.class)
public class UserPostUpdateModelListener extends BaseModelListener<User> {
}

18. message bus listener ?



Monday, 29 July 2019

Gogo shel commands

Gogo shell is available inside of the OSGi Container.
Gogo shell commands :
1. lb (to see list of bundles)
2. start <<bundle-id>>
3. stop <<bundle-id>>
4. headers <<bundle-id>>

OSGi Basics

OSGi is specification and set of standards to develop Java based modular applications.

1. OSGi Framework : It is runtime (OSGi Container) & development environment.
2. Bundle : Basic unit to deploy into the OSGi container. It is a JAR file which contains executable code and OSGi headers in its MANIFEST.MF file.

Bundle = MANIFEST.MF + Java classes + Resources.

             a. Symantic versioning : Major . Minor . Micro - Qualifier (3.0.0-BETA).
             Eg. [1.2, 3.0) (means version 1.2 onwards and upto 3.0 not included 3.0 version)

             b. Bundle lifecycle managed by BundleActivator.

             States are :
                 1. installed (after added into the container), 2. Resolved (all imported dependencies are resolved ), 3. Starting, 4. Active, 5. Stopping, 6. Unistalling.


3. Component : It is an object (java class) which has set of functionalities. It is managed by component framework.
             Component Lifecycle : 1. active, 2. activated. 3. deactivated
4. Service : It is a component registered to OSGi Container 'service registry'.
                   Implements an interface and referenced by its interface in the OSGi Container.

Saturday, 1 December 2018

XML Processing via Java - JAXB

Unmarshal is a process reading from XML to Java
Marshal is a process writing into XML from Java

image.png

Wednesday, 4 July 2018

GenericDaoImpl


@Repository

@Transactional(readOnly = true)

@SuppressWarnings({ "unchecked", "serial" })

public abstract class GenericDaoImpl<E, ID extends Serializable> extends

HibernateTemplate implements GenericDao<E, ID> {

protected Class<E> entityClass;
public GenericDaoImpl() {

setEntityClass(); 

}

public E findById(ID id, boolean lock) {

return (E) super.get(entityClass, id);

}

public E findById(ID id) {

return findById(id, false);

}

public List<E> findAll() throws Exception {

return this.loadAll(entityClass);

}

public List<E> findByCriteria(DetachedCriteria criteria, int startIndex,

int maxResults) {

return (List<E>) super.findByCriteria(criteria, startIndex, maxResults);

}

@Autowired

@Qualifier("hibernateSessionFactory")

public void setSessionFactory(SessionFactory hibernateSessionFactory) {

super.setSessionFactory(hibernateSessionFactory);



}

protected void setEntityClass() {

entityClass = (Class<E>) ((ParameterizedType) getClass()

.getGenericSuperclass()).getActualTypeArguments()[0];

}



@Transactional(readOnly = false, propagation = Propagation.REQUIRED)

public E makePersistent(E entity) {

this.checkWriteOperationAllowed(getSession());

this.saveOrUpdate(entity);

return entity;

}



@Transactional(readOnly = false, propagation = Propagation.REQUIRED)

public void deleteEntity(E entity) {

this.checkWriteOperationAllowed(getSession());

this.delete(entity);

}



@Transactional(readOnly = false, propagation = Propagation.REQUIRED)

public void deleteAllEntities(Collection<E> entities) {



for (E entity : entities) {

this.checkWriteOperationAllowed(getSession());

this.delete(entity);

}

}

@Override

public Session getSession() {
Session session = getSessionFactory().getCurrentSession();

session.setFlushMode(FlushMode.COMMIT);

return session;

}

}

Session factory configuration in spring

<bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">

<property name="jndiName" value="jdbc/TEST" />

</bean>



<bean id="hibernateSessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">

<property name="dataSource" ref="dataSource"></property>

<property name="packagesToScan" value="com.test.model"></property>

<property name="hibernateProperties">

<props>

<prop key="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</prop>

<prop key="hibernate.show_sql">false</prop>

<prop key="hibernate.bytecode.use_reflection_optimizer">false</prop>

<prop key="hibernate.search.autoregister_listeners">false</prop>

<prop key="hibernate.connection.release_mode">after_transaction</prop>

<prop key="hibernate.transaction.auto_close_session">false</prop>

</props>

</property>

</bean>




<bean id="transactionManager"

class="org.springframework.orm.hibernate5.HibernateTransactionManager">

<property name="sessionFactory" ref="hibernateSessionFactory"></property>

</bean>




<tx:annotation-driven transaction-manager="transactionManager"/>

Model listener

Model listeners were designed to perform lightweight actions in response to a create, remove, or update attempt on an entity’s database table or a mapping table

Model listeners are not called when fetching records from the database