Sharing spring context between web apps

Load all jars part of server classpath loading which will be used to create the spring application context and can be shared/available between Web apps/EARs.

Web.xml (in all WEB apps)

<context-param>
  <param-name>parentContextKey</param-name>
  <param-value>beanRefFactory</param-value>
 </context-param>
 <listener>
  <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
 </listener>

 It looks for beanRefContext.xml part of classpath and a spring bean named “beanRefFactory”

beanRefContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="
     http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
 <bean id="beanRefFactory"
  class="org.springframework.context.support.ClassPathXmlApplicationContext">
  <constructor-arg >
    <list>
      <value>classpath*:spring-context.xml</value>
   </list> 
  </constructor-arg>
 </bean>
</beans>

spring-context.xml contains spring beans definition needed by the application and been loaded to create spring application context. This will be the parent application context.

Ensure that applicationContext.xml is placed in WEB-INF folder, used to create its own spring context which extends the parent application context created already. Define the spring beans specific to web modules here. if not, have dummy declaration given below.

applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
</beans>

How to configure number of views in session – Perfomance tuning in JSF

JSF will maintain all its views & its state in HTTP session object if state saving method is server which will cause increased usage of memory. To avoid it, we can minimize the number of views stored in session which suits to the application need that can be defined in web.xml given below

<context-param>
 <param-name>javax.faces.STATE_SAVING_METHOD</param-name>
 <param-value>server</param-value>
</context-param>

<context-param>
 <param-name>com.sun.faces.numberOfViewsInSession</param-name>
 <param-value>3</param-value>
</context-param>

<context-param>
 <param-name>com.sun.faces.numberOfLogicalViews</param-name>
 <param-value>10</param-value>
</context-param>

numberOfViewsInSession defines the number of view states (pages) to support back button operation.

numberOfLogicalViews defines the number of logical views (frames) that can present in a page.

Merging two persistence units

We can merge two persistence unit if both have same persistence-unit name. This can be done using persistence unit post processor.

Note: “default” is the PU name for both persistence1.xml and persistence2.xml given below.

persistence1.xml

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="1.0" xmlns="http://java.sun.com/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd">
<persistence-unit name="default">
    <class>examples.A</class>
    .
    .
    .
    <exclude-unlisted-classes>true</exclude-unlisted-classes>
</persistence-unit>
</persistence>

persistence2.xml

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="1.0" xmlns="http://java.sun.com/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd">
<persistence-unit name="default">
    <class>samples.A</class>
    .
    .
    .
    <exclude-unlisted-classes>true</exclude-unlisted-classes>
</persistence-unit>
</persistence>

2. Persistence post processor is used to merge all entities present in persistence1.xml & persistence2.xml and be

part of persistence unit manager.

package examples;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class MergingPersistenceUnitPostProcessor
implements PersistenceUnitPostProcessor {
Map<String, List<String>> puiClasses = new HashMap<String, List<String>>();

public void postProcessPersistenceUnitInfo( MutablePersistenceUnitInfo pui ) {
List<String> classes = puiClasses.get( pui.getPersistenceUnitName() );
if ( classes == null ) {
classes = new ArrayList<String>();
puiClasses.put( pui.getPersistenceUnitName(), classes );
}
pui.getManagedClassNames().addAll( classes );
final List<String> names = pui.getManagedClassNames();
classes.addAll( pui.getManagedClassNames() );
}
}

3. Configure the merging persistence unit post processor in persistence unit manager.

    <bean    class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" />
    <bean id="entityManagerFactory"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
        <property name="dataSource" ref="websphereDataSource" />
        <property name="persistenceUnitManager" ref="pum" />
        <property name="jpaVendorAdapter">
            <bean
                class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
                <property name="database" value="ORACLE" />
                <property name="showSql" value="true" />
            </bean>
        </property>
    </bean>

    <bean id="pum" class="org.springframework.orm.jpa.persistenceunit.DefaultPersistenceUnitManager">
        <property name="persistenceXmlLocations">
            <list>
             <value>classpath*:META-INF/persistence1.xml</value>
             <value>classpath*:META-INF/persistence2.xml</value>
            </list>
        </property>
        <property name="defaultDataSource" ref="websphereDataSource"/>
        <property name="persistenceUnitPostProcessors">
            <bean class="examples.MergingPersistenceUnitPostProcessor"/>
        </property>
    </bean>

JAD Eclipse plugin

1. Download the Jad executable file.

http://www.varaneckas.com/jad
http://www.softpedia.com/get/Programming/Debuggers-Decompilers-Dissasemblers/JAD.shtml

2. Download Jad Eclipse plugin
http://sourceforge.net/projects/jadclipse/

3. Copy the downloaded Jad Eclipse plugin – Jabclipse(jadclipse_x.x.x.jar) to Eclipse plugin folder.

4. Restart Eclipse to make the plugin take effect.

5. In Eclipse, Click Window –> Preference –> Java –> Jadclipse , Key in Jad’s path in “Path to Decompiler” field
E.G d:\j2ee\jad\jad.exe

6. In Eclipse, try F3 into any class which does not has the source, Jad will decompile it automatically.