Sunday, April 14, 2013

Struts2 + Hibernate + Jpa 2 + Spring + Netbeans how to make it all work together

After many days of frustration, searching, trying ..and failing finally got it working and because I couldn't locate any full descriptions online I will lay out my solution here.

Bare in mind that if you use different versions of these libraries, you should be sure that they are compatible with each other, because one of the issues I was dealing with (and I didn't know that at the time) was because of incompatible libraries.

So I am using:

netbeans  7.3

hibernate: hibernate-release-4.1.8.Final download
                  (+ hibernate-entitymanager-4.0.1.Final) download


listing:
hibernate-release-4.1.8.Final/lib/required/antlr-2.7.7.jar
hibernate-release-4.1.8.Final/lib/required/dom4j-1.6.1.jar
hibernate-release-4.1.8.Final/lib/required/hibernate-commons-annotations-4.0.1.Final.jar
hibernate-release-4.1.8.Final/lib/required/hibernate-core-4.1.8.Final.jar
hibernate-release-4.1.8.Final/lib/required/hibernate-jpa-2.0-api-1.0.1.Final.jar
hibernate-release-4.1.8.Final/lib/required/javassist-3.15.0-GA.jar
hibernate-release-4.1.8.Final/lib/required/jboss-logging-3.1.0.GA.jar
hibernate-release-4.1.8.Final/lib/required/jboss-transaction-api_1.1_spec-1.0.0.Final.jar
hibernate-entitymanager-4.0.1.Final


aopalliance: aopaliance 1.0  download 

aopalliance.jar

Spring    : spring3.1.1release (bundled with netbeans  7.3)


listing: 

spring-aop-3.1.1.RELEASE.jar
spring-asm-3.1.1.RELEASE.jar
spring-aspects-3.1.1.RELEASE.jar
spring-beans-3.1.1.RELEASE.jar
spring-context-3.1.1.RELEASE.jar
spring-context-support-3.1.1.RELEASE.jar
spring-core-3.1.1.RELEASE.jar
spring-expression-3.1.1.RELEASE.jar
spring-instrument-3.1.1.RELEASE.jar
spring-instrument-tomcat-3.1.1.RELEASE.jar
spring-jdbc-3.1.1.RELEASE.jar
spring-jms-3.1.1.RELEASE.jar
spring-orm-3.1.1.RELEASE.jar
spring-oxm-3.1.1.RELEASE.jar
spring-struts-3.1.1.RELEASE.jar
spring-test-3.1.1.RELEASE.jar
spring-tx-3.1.1.RELEASE.jar
spring-web-3.1.1.RELEASE.jar
spring-webmvc-3.1.1.RELEASE.jar
spring-webmvc-portlet-3.1.1.RELEASE.jar



Struts2    : struts-2.3.12 download
listing:
(remember to remove unnecessary plugins - otherwise you will have problems deploying e.g I am using tiles, so I have left the plugin in listing but I also have the corresponding  tiles.xml to make it work, if you don't just do not include struts2-tiles-plugin)

asm-3.3.jar
asm-commons-3.3.jar
asm-tree-3.3.jar
classworlds-1.1.jar
commons-beanutils-1.8.0.jar
commons-chain-1.2.jar
commons-collections-3.1.jar
commons-digester-2.0.jar
commons-fileupload-1.2.2.jar
commons-io-2.0.1.jar
commons-lang-2.4.jar
commons-lang3-3.1.jar
commons-logging-1.1.1.jar
commons-logging-api-1.1.jar
commons-validator-1.3.1.jar
ezmorph-1.0.6.jar
freemarker-2.3.19.jar
google-collections-1.0.jar
google-gxp-0.2.4-beta.jar
jackson-core-asl-1.9.2.jar
jackson-mapper-asl-1.9.2.jar
javassist-3.11.0.GA.jar
json-lib-2.3-jdk15.jar
juli-6.0.18.jar
ognl-3.0.6.jar
org.apache.felix.bundlerepository-1.2.1.jar
org.apache.felix.framework-1.4.1.jar
org.apache.felix.main-1.4.1.jar
org.apache.felix.shell-1.0.2.jar
org.apache.felix.shell.tui-1.0.2.jar
oval-1.31.jar
plexus-container-default-1.0-alpha-10.jar
plexus-utils-1.2.jar
struts2-convention-plugin-2.3.12.jar
struts2-core-2.3.12.jar
struts2-dojo-plugin-2.3.12.jar
struts2-embeddedjsp-plugin-2.3.12.jar
struts2-jasperreports-plugin-2.3.12.jar
struts2-javatemplates-plugin-2.3.12.jar
struts2-spring-plugin-2.3.12.jar
struts2-tiles-plugin-2.3.12.jar
testng-5.1-jdk15.jar
tiles-api-2.0.6.jar
tiles-core-2.0.6.jar
tiles-jsp-2.0.6.jar
xmlpull-1.1.3.1.jar
xpp3_min-1.1.4c.jar
xstream-1.4.2.jar
xwork-core-2.3.12.jar




web.xml
...
 <filter>
        <filter-name>SpringOpenEntityManagerInViewFilter</filter-name>
        <filter-class>org.springframework.orm.jpa.support.OpenEntityManagerInViewFilter</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>SpringOpenEntityManagerInViewFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

  <!-- deprecated
    <filter>
        <filter-name>struts2</filter-name>
        <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
    </filter>
    -->
    <filter>
        <filter-name>struts2</filter-name>
        <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
        <init-param>
            <param-name>actionPackages</param-name>
            <param-value>my.action</param-value>
        </init-param>
    </filter>
    <filter-mapping>
        <filter-name>struts2</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

...

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"
               xmlns:aop="http://www.springframework.org/schema/aop"
               xmlns:tx="http://www.springframework.org/schema/tx"
               xmlns:context="http://www.springframework.org/schema/context"
               xmlns:p="http://www.springframework.org/schema/p" 
               xmlns:cache="http://www.springframework.org/schema/cache" 
               xmlns:jee="http://www.springframework.org/schema/jee"
               xsi:schemaLocation="
                    http://www.springframework.org/schema/beans
                    http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
                    http://www.springframework.org/schema/tx
                    http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
                    http://www.springframework.org/schema/aop
                    http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
                    http://www.springframework.org/schema/context     
                    http://www.springframework.org/schema/context/spring-context-3.1.xsd
                    http://www.springframework.org/schema/cache
                    http://www.springframework.org/schema/cache/spring-cache-3.1.xsd
                    http://www.springframework.org/schema/jee
                    classpath:org/springframework/ejb/config/spring-jee-3.1.xsd"> 
     
      <context:component-scan base-package="myBasePackage" />

    <bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
    <property name="jndiName" value="java:comp/env/DATABASENAME"/>
    </bean>
   
    <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"> 
                   <property name="dataSource" ref="dataSource" />
        <property name="jpaVendorAdapter">
        <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
            <property name="showSql" value="true" />
            <property name="database" value="ORACLE"/>
            <property name="databasePlatform" value="org.hibernate.dialect.Oracle10gDialect"/>
        </bean>
    </property>
      </bean>

      <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager"> 
           <property name="entityManagerFactory" ref="entityManagerFactory" /> 
      </bean> 
      <!--mybeans -->
      <bean id="daoExample" class="my.dao.DaoExample"/>
      <bean id="portfolioService" class="my.utilities.ServiceTest"/>
      <tx:annotation-driven transaction-manager="transactionManager"/>
     
</beans>



Thursday, April 4, 2013

Netbeans Maven Proxy Settings

You can set up proxy for maven project in Netbeans 7.3 (on Windows)  by editing


C:\Program Files\NetBeans 7.3\java\maven\conf\settings.xml.

That simple... It took me 30 min to find it on net  - hope you found this page much quicker ;-)

Barbecue barcode label problem and solution

I am using barbecue 1.5.beta to generate barcode images on my Windows 2003 server.
When migrating to a new server though (Windows 2008 x64 ) I encountered the following issue: text label underneath the barcode wasn't showing any more...users where baffled by this ..and so was I .

After searching the web I came to a conclusion that this must be happening because some servers (don't ask me why) are considered to be headless (without monitor) and in that case...well there is a bug in Barcode class namely in its calculateSize() method.  Credits for this solution must go to Paul

So, to fix Barcode.class you must replace its calculateSize() method, as Paul suggested:

-------------------------------------

private Dimension calculateSize() { 
Dimension d = new Dimension(); 
   if(EnvironmentFactory.getEnvironment() instanceof HeadlessEnvironment)
     try { 
          if(font == null) { 
               d = draw(new SizingOutput(font, getForeground(), getBackground()), 0, 0, barWidth, barHeight);
         } 
         else { 
              java.awt.FontMetrics fontMetrics = getFontMetrics(font); 
              d = draw(new SizingOutput(font, fontMetrics, getForeground(), getBackground()), 0, 0, barWidth, barHeight); 
              }
         } 
      catch(OutputException e) { 
          e.printStackTrace(); 
      } 
   else 
    try { 
         java.awt.FontMetrics fontMetrics = null;
         if(font != null) fontMetrics = getFontMetrics(font);
         d = draw(new SizingOutput(font, fontMetrics, getForeground(), getBackground()), 0, 0, barWidth, barHeight);
    } catch(OutputException e) { }
 return d; 
}

-------------------------------------
Works like charm.  If you have enough patience you should download Barbecue source code, downland dependencies,  use maven,  make changes ...recompile (as I did). Or if you don't -  then just take  this  Barbecue (myFix) version.

Monday, April 1, 2013

Netbeans user defined Libraries Xml Definitions

Did you know where Netbeans stores user defined libraries?  I for sure didn't.
It turns out that Net Beans creates an Xml file for each user defined library and to carry them around with you...when say upgrading, all you have to do is to copy these xmls to appropriate folder. 

I was upgrading from NB 7.0 to 7.3 so I had to copy from

[userName]\.netbeans\7.0\config\org-netbeans-api-project-libraries\Libraries

to

[userName]\AppData\Roaming\NetBeans\7.3\config\org-netbeans-api-project-libraries\Libraries.

and it worked ;-)