Archive for May, 2008

JRuby Protecting me From Forgery

Tuesday, May 20th, 2008

Tonight I took it upon myself to attempt to get a JRuby app to deploy on an OC4J Instance and connect to an Oracle database.  Locally this was no issue however when I deployed in “production” mode up on the slice I kept getting this error:

ActionView::TemplateError (No :secret given to the #protect_from_forgery call.  Set that or use a session store capable of generating its own keys (Cookie Session Store).) in welcome/index.html.erb:

Needless to say I was a bit befuddled and couldn’t find much on the google pipes of consequence until I stumbled up on this line in application.rb (controllers folder):
protect_from_forgery #:secret => ‘xxxxxxxxxx’
I uncommented this line and life was good.  Apparently in the application server the session store cannot generate its own secret key.  Oddly enough this works fine (with the line commented) on Glassfish but not on OC4J.  So I am chalking it up to a server oddity for now until I have more time to research.

Hql Basics (Parameters)

Friday, May 16th, 2008

In using Hibernate Query Language to include a Parameter of Type Long in a statement you would use the following code:

Query q = session.createQuery(”from Class1 c where c.class2.class2_id = :class2_id”);
q.setLong(”class2_id”, class2_id);
q.list();…..

One thing to Immediately notice (if you are familiar with Hibernate) is that I am calling class2_id through class2.  This is because in my little world class2 has a one to many relationship with Class1.  This means there is an object called class2 joining them together in the annotations.  However, I digress.  The real take away is that a parameter called :class2_id was established in the query and the referenced in the setLong statement to set the value.  After that calling the q.list function will return the values pulled back from the query. And TADA!

Oracle App Server 10.1.3 Ram

Wednesday, May 14th, 2008

I ran into a situation today where I needed to increase the amount of ram (and perm gen) space a server was able to use.  This server is running oracle application server 10.1.3.  FYI the Error was java.lang.OutofMemory Perm Gen.

To do this, first off, I stopped opmn using the command:

opmnctl stopall

Secondly I edited the opmn.xml file in the opmn/conf folder and added the following variables to the java start up parameters:

-XX:MaxPermSize=512m -Xmx1024m

*** ful code ***

<ias-component id=”OC4J”>
            <process-type id=”home” module-id=”OC4J” status=”enabled”>
               <module-data>
                  <category id=”start-parameters“>
<data id=”java-options” value=”-server -XX:MaxPermSize=512m -Xmx1024m -Djava.security.policy=$ORACLE_HOME/j2ee/home/config/java2.policy -Dj
ava.awt.headless=true -Dhttp.webdir.enable=false”/>

After that I restarted opmn and I had enough ram to support the massive app I was working on.

Java Reflection

Tuesday, May 13th, 2008

In a project I was confronted with a reason to use Java Reflection. What I found was a way to dynamically scale a class to find out its variables, methods, etc. During this process fortunately we changed gears and moved away from the design which was forcing this way of thinking. I have, ever since, been looking into it more and more and trying to discover why it isn’t more commonly used. Aside from the inherent complexity and detracting from readability I found a list of “draw backs” to using reflection which I will paste below: 

http://java.sun.com/docs/books/tutorial/reflect/


Drawbacks of Reflection

 

Reflection is powerful, but should not be used indiscriminately. If it is possible to perform an operation without using reflection, then it is preferable to avoid using it. The following concerns should be kept in mind when accessing code via reflection.

Performance Overhead
Because reflection involves types that are dynamically resolved, certain Java virtual machine optimizations can not be performed. Consequently, reflective operations have slower performance than their non-reflective counterparts, and should be avoided in sections of code which are called frequently in performance-sensitive applications.
Security Restrictions
Reflection requires a runtime permission which may not be present when running under a security manager. This is in an important consideration for code which has to run in a restricted security context, such as in an Applet.
Exposure of Internals
Since reflection allows code to perform operations that would be illegal in non-reflective code, such as accessing private fields and methods, the use of reflection can result in unexpected side-effects, which may render code dysfunctional and may destroy portability. Reflective code breaks abstractions and therefore may change behavior with upgrades of the platform.

 

Annotations

Monday, May 12th, 2008

After starting my new job I have been working alot with Java Annotations (EJB) so below is a quick reference of the 4 Common Data ones.  This information has been taken partly from the Java Documentation and from my own Experience using both JPA and Hibernate.  Each of the examples below deals with 2 Classes which we will call class_1 and class_2 affectionately.  

@OneToMany -> @ManyToOne
A class_1 can have many class_2

@OneToMany(targetEntity=com.class_2.class, cascade=ALL, fetch=FetchType.EAGER)
@JoinColumn(name=”class_2_id”)
private Set<class_2> c2s = new HashSet();

<flip side>

@ManyToOne(targetEntity=com.class_1.class, cascade=ALL, fetch=FetchType.EAGER)
@JoinColumn(name=”class_2_id”, updatable=false, insertable=false)
private class_1 c1; 

@OneToOne
A class_1 can have one class_2

private class_2 c2;

<flip side>

@OneToOne (targetEntity=struts.hibernate.model.class_1.class, fetch=FetchType.EAGER, mappedBy=”c2″)
private class_1 c1; 

@ManyToMany
A class_1 can have many class_2 and a class_2 can have many class_1
*Note - This configuration uses a join table

@ManyToMany
@JoinTable(name=”class_1_to_class_2″
 joinColumns=
@JoinColumn(name=”class_1_id”, referencedColumnName=”ID”),
inverseJoinColumns=
@JoinColumn(name=”class_2_id”, referencedColumnName=”ID”))
private Set<class_2> c2s = new HashSet();

<flip side>

 

@ManyToMany
@JoinTable(name=”class_1_to_class_2″
 joinColumns=
@JoinColumn(name=”class_2_id”, referencedColumnName=”ID”),
inverseJoinColumns=
@JoinColumn(name=”class_1_id”, referencedColumnName=”ID”))
private Set<class_1> c1s = new HashSet();