Saturday 30 January 2010

OutOfMemoryError Exception caught in Display

Been developing with J2ME recently, fun as it is. Something changed in the code, ok so I changed something in the code and this error happened:

TRACE: , Exception caught in Display class
java.lang.OutOfMemoryError
   (stack trace incomplete)


...upon deploying to the emulator.

Turns out when I added the Motorola SDK to my configurations I had modified my canvas class overiding showNofity method:
/* How it was */    
protected void showNotify() {
 super.showNotify();
// My shizzles of code
}

However the Moto SDK declared this an error in someway and in my haste to go play hockey (game was cancelled btw :( ) I change the method to be this


protected void showNotify() {
 showNotify();
// My shizzles of code 
}

And this loop ( atleast I think it was a recursive loop) was causing the issue. I guess it was recursive calling... Bah!

Thursday 28 January 2010

Nokia S40 5th Edth Not Fullscreen

If you are using the gamecanvas avoid being clever and putting the call to setFullScreen(true) and Display.getDisplay(midlet).setCurrent(this); in the contructor.

It appears different implementations of J2ME initalise at different times. So do the above stuff after GameCanvas has been created.

AJAX XMLHTTP on the Opticon H15 Device

After some wrangling, Opticon came up with a patch. MSXML_AJAX.ARMV4I.CAB. Contact me for it.

Wednesday 27 January 2010

Hibernate Libraries

This one is a massive Gar...

It all started with the installation of the Fish v2.1.1 distribution. Because of updated libraries supplied by the fish installation, certain versions of hibernate won't work - mainly the one that is shipped with the Beans.

So this led me to download and manually add the libraries to the Fish (I decided that all my apps use Hibernate JPA so might as well have a mutual library), this is where the fun begins. To begin with there is no single download that will give you all the libs with dependencies required for Hibernate JPA. I will add that there are notes saying from 3.5.x this will all change.

Anyhoo, I figured out I needed the:

Annotations
Core
Entitymanager

downloads, take all the libs from here and place them in the lib folder of the fish. Then deploy an app, you will probably get an error like this:


.NoSuchMethodError: org.objectweb.asm.ClassWriter

That is because another library is required. At time of writing I don't know why this is, but anyhoo download the slf4j distribution and copy the following lib into the folder:

slf4j-jdk14-1.5.10.jar

...

Full list of libs:
antlr-2.7.6.jar
commons-collections-3.1.jar
dom4j-1.6.1.jar
javassist-3.9.0.GA.jar
jta-1.1.jar
slf4j-api-1.5.8.jar
cglib-2.2.jar
hibernate3.jar
hibernate-annotations.jar
hibernate-commons-annotations.jar
hibernate-core.jar
slf4j-api.jar
dom4j.jar
ejb3-persistence.jar
hibernate-entitymanager.jar
javassist.jar
jta.jar
slf4j-jdk14-1.5.10.jar

Also posted a question about this on the hibernate forums... Lets see how many responses I get:

https://forum.hibernate.org/viewtopic.php?f=1&t=1002285

SSL client authentication with local CA

This post describes how mutual authentication, specifically client authentication, can be implemented with Glassfish. A CA is created using OpenSSL and a root certificate for the CA is created and trusted by Glassfish. Client certificates are then created and signed by the CA such that multiple client certificates may be created and trusted transitively.

Steps:

1, Create the client certificate, register the CA cert as trusted.

As registering a new trusted client certifice into the truststore requires a Glassfish restart, we register a trusted Certificate Authority (CA) certificate instead, once and for all. Then each new client can get its certificate from the trusted CA, and become "transitively" trusted. To be exact, it is normaly the client who creates a public-private keypair, then submits the public part in the form of a Certificate Signing Request to the Certificate Authority, who then creates the certificate by signing it with its own private key. This certifies that tha CA trusts the certificate holder to be who he claims to be. Confusing at first, but rather straight forward once you get the hang of Public Key Infrastructure.

2, Establish a CA (assuming you don't have one already): source: http://www.madboa.com/geek/openssl/

openssl req \ -x509 -nodes -days 365 \ -newkey rsa:1024 -keyout cakey.pem -out cacert.pem

Edit openssl.cnf, create directories and files referenced there: mkdir demoCA echo '100001' > ./demoCA/serial mkdir ./demoCA/newcerts mkdir demoCA/private cp cacert.pem demoCA/ cp cakey.pem demoCA/private/

Enable signing foreign keys in openssl.cnf:

policy = policy_anything

3, Sign the certificate of the client key:

Create client key pair and certification request. This is normaly done by the client. The information provided here becomes part of the certificate, and will be included in the audit logs as "user principal":

openssl genrsa -out somekey.pem 2048 openssl req -new -key somekey.pem -out somekey.csr

sign (done by the CA): openssl ca -config openssl.cnf -in somekey.csr -out somekey.crt

4, package key and cert for browser consumption: openssl pkcs12 -export -out somekey.pfx -in somekey.crt -inkey somekey.pem -name "Some Cert"

5, Add own CA cert to truststore:

keytool -import -v -keystore /opt/glassfish/domains/domain1/config/cacerts.jks -storepass changeit -alias localCA -file cacert.pem

6, Restart Glassfish. Importing somekey.pfx into a browser should enable it to access your web service again at this point.

--

With thanks to:

Gabor Szokoli
Sirius Cybernetics Corporation Complaints division

Tuesday 19 January 2010

Sony Ericsson + XP x64

I just bought a Sony Ericsson Yari (U100i) phone. Unfortunately the drivers are no longer supplied on a CD (not at least with the one bought direct from SE). They make you install the PC suite then tell you, you can't use the device.

However I did find the drivers in the PC suite cab. If you look on the memory card that comes with the device look in the folder marked OTHER, unzip the pcsuite cab. Connect the phone and select the option to use with a different OS. XP x64 will then detect it and want the drivers, point it to the PCSuite\Drivers\SignedPhones directory and then click your way through.

Wednesday 13 January 2010

Java as a windows service

Good tutorial
http://blog.platinumsolutions.com/node/234
Apache stuff...
http://commons.apache.org/daemon/procrun.html
You can download the binaries without downloading Tomcat (or to get 64 bit versions) here:
http://svn.apache.org/repos/asf/tomcat/tc6.0.x/trunk/res/procrun/

Saturday 9 January 2010

Spring - PostConstruct

Found that using @PostConstruct doesn't have transactional ability. Looking around I found this link:

http://forum.springsource.org/showthread.php?t=81832

So by implementing the app listener you can have it bootstrap the configuration once the context has finished loading.

@Repository
public class MyDAOImpl implements ApplicationListener, MyDAO {

    @Transactional(propagation=Propagation.REQUIRED)
    public void onApplicationEvent(ApplicationEvent event) {
        if (event instanceof ContextRefreshedEvent) {
            postCreate();
        }

    }

    public void postCreate() {
      //Code
      
        }
    }

Friday 8 January 2010

JPA + The Cat + Spring

App context:

<tx:annotation-driven transaction-manager="transactionManager" />
<bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" />
<bean class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor"/>
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaDialect"/>

<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory" />
</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="database" value="MYSQL" />
<property name="showSql" value="false" />
</bean>
</property>
<property name="jpaProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLInnoDBDialect</prop>
<prop key="hibernate.hbm2ddl.auto">create-drop</prop>
</props>
</property>
</bean>

<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="${jdbc.driver}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
</bean>

Persistence.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="myPU" transaction-type="RESOURCE_LOCAL">
    <provider>org.hibernate.ejb.HibernatePersistence</provider>
    <properties>
      <property name="hibernate.cache.provider_class" value="org.hibernate.cache.NoCacheProvider"/>
      <property name="hibernate.hbm2ddl.auto" value="create-drop"/>
    </properties>
  </persistence-unit>
</persistence>

JPA + The Fish + Spring

In the app context
<context:annotation-config />
<tx:jta-transaction-manager />
<tx:annotation-driven transaction-manager="transactionManager" />
<jee:jndi-lookup id="entityManagerFactory" jndi-name="persistence/myPU"/>
<bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" />
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaDialect"/>

In the web.xml

<persistence-unit-ref>
<persistence-unit-ref-name>persistence/myPU</persistence-unit-ref-name>
<persistence-unit-name>myPU</persistence-unit-name>
</persistence-unit-ref>

In the persistence.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="myPU" transaction-type="JTA">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<jta-data-source>jdbc/myDatasource</jta-data-source>
<properties>
<property name="hibernate.bytecode.provider" value="cglib"/>
<property name="hibernate.hbm2ddl.auto" value="update"/>
</properties>
</persistence-unit>
</persistence>

Then to access in code:

@PersistenceContext
private EntityManager entityManager;

Thursday 7 January 2010

SNMP In Java

Tools to create an SNMP agent:

http://www.snmp4j.org/ - Java code
http://www.mibdesigner.com/ - Design the MiB
http://www.agentpp.com/ - Convert a MiB into Java code to utilize with the snmp4j agent.

Remote EJB access in Spring

<jee:jndi-lookup id="mySpringId" jndi-name="ejb/garBean">
<jee:environment>
//This tells spring to look here for the bean, if these aren't specified default to the localhost and 3700
org.omg.CORBA.ORBInitialHost=192.168.1.20
org.omg.CORBA.ORBInitialPort=3700

</jee:environment>
</jee:jndi-lookup>

 @Stateless(name="garBean", mappedName="ejb/garBean")
@Remote(GarBeanRemote.class)
public class GarBean implements GarBeanRemote

java.lang.NoSuchMethodError - Spring - SpringBeanAutowiringSupport

SEVERE: WSSERVLET11: failed to parse runtime descriptor: java.lang.NoSuchMethodError: org.springframework.web.context.ContextLoader.getC urrentWebApplicationContext()Lorg/springframework/web/context/WebApplicationContext;

Was thrown when using auto-wiring a service to a web-service class.

Fixed by moving to 2.5.6