mercoledì 23 gennaio 2013

Autoboxing pitfalls

  1. Use carefully Boolean in setter method for boolean attribute:
EXAMPLE :
DON'T DO THIS (throws null pointer exception):

public
class BooleanMethod {
boolean attribute = false;
/**
*
@return the attribute
*/
public boolean isAttribute() {
return attribute;
}
/**
*
@param attribute the attribute to set
*/
public void setAttribute(boolean attribute) {
this.attribute = attribute;
}
public static void main (String [] args) {
BooleanMethod obj =
new BooleanMethod();
Boolean condition =
null;
obj.setAttribute(condition);
System.
out.println(obj.isAttribute());
}
}


Exception in thread "main"
java.lang.NullPointerExceptionat BooleanMethod.main(BooleanMethod.java:23)

BETTER

public
class BooleanMethod {
boolean attribute = false;
/**
*
@return the attribute
*/
public boolean isAttribute() {
return attribute;
}
/**
*
@param attribute the attribute to set
*/
public void setAttribute(boolean attribute) {
this.attribute = attribute;
}
public static void main (String [] args) {
BooleanMethod obj =
new BooleanMethod();
Boolean condition =
null;
if (condition != null){
obj.setAttribute(condition);
}
System.
out.println(obj.isAttribute());
condition = Boolean.
TRUE;
if (condition != null){
obj.setAttribute(condition);
}
System.
out.println(obj.isAttribute());
}
}

giovedì 6 settembre 2012

Notes about presentation Performance Tuning by Kirk Pepperdine

In case of performance troubles :
- don't start with code inspection.
- start with Execution profiling instead (may help to find out code bugs)
- add GC debuggin/loggins options or take a look to JMX GCBeans
Correct performance tuning process
1 Set a baseline
2 Modify only one thing a time
3 Make performane test / create new baseline
4 Restart the process again if performance doesnt increase significantly

Basic things to watch at
cpu utilization (don't waste cpu cycles)
application  OS interactions
locks
network io
disk io

Performance monitoring TOOLS TO START with 
System monitoring tools (info about CPU, server Memory used , Network IO, Disk IO
Memory monitoring tools
JVM monitoring tools (garbage collection, memory usage, threads)
Example
Free sample tool visual vm for JMV 
Memory leaks analysis
thread monitoring (starving threads, blocked threads )
thead dump (shows blocked threads and running code )
Different memory areas usage (Survivor space bigger / hidden space)

Resources
java performance tuning by  charlie hunt
http://java.sun.com/performance/reference/whitepapers/tuning.html 

Other Presentations
(Also available in you tube at  http://www.youtube.com/watch?v=nvvPM2OES58 )
http://presentz.org/jugtorino/201010_gc

Mailing list
hotspot-gc-use@openjdk.java.net

mercoledì 5 settembre 2012

VarArgs method samples

/**
 *
 */
package test;
/**
 * @author Paolo
 *
 */
public class VarArgsTest {

 public static void method1VarArgs(int ...intsArray){
  System.out.println("length ="  + intsArray.length);

 }


 public static void method2VarArgs(String ...stringsArray){
  System.out.println("length ="  + stringsArray.length);
 }

 /**
  * @param args
  */
 public static void main(String[] args) {
  method1VarArgs();
  method1VarArgs(1,2,3);
  method2VarArgs();
  method2VarArgs("a", "b");
 
 }
}


And output is :

length =0
length =3
length =0
length =2


NOTES: Arrays  are never null (neither if method is called without any parameter )

sabato 25 agosto 2012

JSF EL different behaviour with boolean vs Boolean Bean attribute

According Java Bean specification when you define a bean attribute (any kind of type - primitive or Object) you should also define getter and setter methods.

For get method a special attribute is the boolean attribute; infact for boolean attribute you may implement both  'is' method and 'get' method.

Sometimes you may choose for some reason to change an attribute from boolean to Boolean type.

You can make your change but with some concerns:

-consider how attribute initialization differs between  primitive boolean (compiler force you to initializate to 'false' or 'true' value) and Boolean (you can leave it null).

- another great difference is about getter methods: infact with Boolean type you have to implement get method and not is method (allowed only for boolean) otherwise you may expect errors.



Here a sample:

HTML JSF page

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:h="http://java.sun.com/jsf/html">
   <h:head>
      <title>Welcome</title>
   </h:head>
   <h:body>
      <h:form>
         <h3>Please enter your name and password.</h3>  
         <table>
            <tr>
               <td>Name:</td>
               <td><h:inputText value="#{user.name}" rendered="#{user.illiquid}" /></td>
            </tr>
            <tr>
               <td>Password:</td>
               <td><h:inputSecret value="#{user.password}"/></td>
            </tr>
         </table>
         <p><h:commandButton value="Login" action="welcome"/></p>
      </h:form>
   </h:body>
</html>

 That's is user backing bean with visible property type changed from boolean to Boolean.

package com.corejsf;

import java.io.Serializable;
import javax.inject.Named;
   // or import javax.faces.bean.ManagedBean;
import javax.enterprise.context.SessionScoped;
   // or import javax.faces.bean.SessionScoped;

@Named("user") // or @ManagedBean(name="user")
@SessionScoped
public class UserBean implements Serializable {
   private String name;
   private String password;
   private Boolean visible = true;

   public String getName() { return name; }  
   public void setName(String newValue) { name = newValue; }

   public String getPassword() { return password; }
   public void setPassword(String newValue) { password = newValue; }  
  
   public Boolean isVisible(){
       return visible;
   }
  }

Here the error
/index.xhtml @15,83 rendered="#{user.visible}": The class 'com.corejsf.UserBean$Proxy$_$$_WeldClientProxy' does not have the property 'visible'.

And here fixed bean

package com.corejsf;

import java.io.Serializable;
import javax.inject.Named;
   // or import javax.faces.bean.ManagedBean;
import javax.enterprise.context.SessionScoped;
   // or import javax.faces.bean.SessionScoped;

@Named("user") // or @ManagedBean(name="user")
@SessionScoped
public class UserBean implements Serializable {
   private String name;
   private String password;
   private Boolean visible = true;

   public String getName() { return name; }  
   public void setName(String newValue) { name = newValue; }

   public String getPassword() { return password; }
   public void setPassword(String newValue) { password = newValue; }  
  
   public Boolean getVisible(){
       return visible;
   }
  
  



giovedì 16 agosto 2012

index rebuild oracle crontab - beta

Still BETA


#!/bin/bash
 
##function
check_id_oracle() {
whoami | grep oracle > /dev/null
if [[ "$?" -eq 0 ]]
then
echo "good - you are oracle"
else
echo "you must be oracle"
exit 1
fi
}
 
 
check_be_active() {
ps -ef | grep "[/j]boss-4.2.3GA" > /dev/null
if [[ "$?" -eq 0 ]]
then
echo "good - be active"
else
echo "be stand by"
exit 1
fi
}
 
. .profile
check_id_oracle
check_be_active
v_schema=INEM_REP
sqlplus -s INEM_REP/INEM_REP@XE <<-EOF 2>&1 > /dev/null
set head off
set linesize 60
spool /tmp/rebuild_idx_${v_schema}.sql
select 'spool /tmp/rebuild_idx_${v_schema}.log' from dual;
select 'alter index ' ||INDEX_NAME||' rebuild online;' from user_indexes where TABLE_NAME='ITEMCONFIG' and INDEX_TYPE='NORMAL';
select 'alter index ' ||INDEX_NAME||' rebuild online;' from user_indexes where TABLE_NAME='SINGLE_VALUE_ATTRIBUTE' and INDEX_TYPE='NORMAL';
select 'alter index ' ||INDEX_NAME||' rebuild online;' from user_indexes where TABLE_NAME='STRUCT_VALUE_ATTRIBUTE' and INDEX_TYPE='NORMAL';
select 'alter index ' ||INDEX_NAME||' rebuild online;' from user_indexes where TABLE_NAME='MULTI_VALUE_ATTRIBUTE' and INDEX_TYPE='NORMAL';
select 'alter index ' ||INDEX_NAME||' rebuild online;' from user_indexes where TABLE_NAME='VALUES_MV_ATTRIBUTES' and INDEX_TYPE='NORMAL';
select 'alter index ' ||INDEX_NAME||' rebuild online;' from user_indexes where TABLE_NAME='VALUES_STRUCT_ATTRIBUTES' and INDEX_TYPE='NORMAL';
select 'alter index ' ||INDEX_NAME||' rebuild online;' from user_indexes where TABLE_NAME='NGN_ENTITY_NODE_PROPERTY' and INDEX_TYPE='NORMAL';
select 'alter index ' ||INDEX_NAME||' rebuild online;' from user_indexes where TABLE_NAME='NGN_NODE_INTERFACE' and INDEX_TYPE='NORMAL';
select 'alter index ' ||INDEX_NAME||' rebuild online;' from user_indexes where TABLE_NAME='NGN_NODE_INTERFACE_CREDENTIALS' and INDEX_TYPE='NORMAL';
select 'alter index ' ||INDEX_NAME||' rebuild online;' from user_indexes where TABLE_NAME='L_AGGR_TO_NGN_NODE' and INDEX_TYPE='NORMAL';
select 'alter index ' ||INDEX_NAME||' rebuild online;' from user_indexes where TABLE_NAME='L_AGGR_DOMAINS' and INDEX_TYPE='NORMAL';
select 'alter index ' ||INDEX_NAME||' rebuild online;' from user_indexes where TABLE_NAME='L_AGGR_INTERFACE' and INDEX_TYPE='NORMAL';
select 'alter index ' ||INDEX_NAME||' rebuild online;' from user_indexes where TABLE_NAME='L_AGGR_INTERFACE_CREDENTIALS' and INDEX_TYPE='NORMAL';
select 'alter index ' ||INDEX_NAME||' rebuild online;' from user_indexes where TABLE_NAME='L_AGGR_INTER_CRED_ATTRIBUTE' and INDEX_TYPE='NORMAL';
select 'alter index ' ||INDEX_NAME||' rebuild online;' from user_indexes where TABLE_NAME='L_AGGR_MV_PROPERTY' and INDEX_TYPE='NORMAL';
select 'alter index ' ||INDEX_NAME||' rebuild online;' from user_indexes where TABLE_NAME='L_AGGR_VALUES_MV_PROPERTY' and INDEX_TYPE='NORMAL';
select 'alter index ' ||INDEX_NAME||' rebuild online;' from user_indexes where TABLE_NAME='LOGICAL_AGGREGATION' and INDEX_TYPE='NORMAL';
select 'alter index ' ||INDEX_NAME||' rebuild online;' from user_indexes where TABLE_NAME='NGN_ENTITY_NODE' and INDEX_TYPE='NORMAL';
select 'alter index ' ||INDEX_NAME||' rebuild online;' from user_indexes where TABLE_NAME='WOS_LOGICAL_AGGREGATION' and INDEX_TYPE='NORMAL';
select 'alter index ' ||INDEX_NAME||' rebuild online;' from user_indexes where TABLE_NAME='WOS_REQUIRED_ATTRIBUTE' and INDEX_TYPE='NORMAL';
select 'alter index ' ||INDEX_NAME||' rebuild online;' from user_indexes where TABLE_NAME='WOS_LOGICAL_CONFIGURATION' and INDEX_TYPE='NORMAL';
select 'alter index ' ||INDEX_NAME||' rebuild online;' from user_indexes where TABLE_NAME='WOS_ATTR' and INDEX_TYPE='NORMAL';
 
select 'alter index ' ||INDEX_NAME||' rebuild online;' from user_indexes where TABLE_NAME='WOS_OTHER_NODE_IP_TYPE' and INDEX_TYPE='NORMAL';
select 'alter index ' ||INDEX_NAME||' rebuild online;' from user_indexes where TABLE_NAME='WOS_REQUIRED_PROPERTY' and INDEX_TYPE='NORMAL';
select 'alter index ' ||INDEX_NAME||' rebuild online;' from user_indexes where TABLE_NAME='WOS_PROTOCOLS' and INDEX_TYPE='NORMAL';
select 'alter index ' ||INDEX_NAME||' rebuild online;' from user_indexes where TABLE_NAME='WOS_CREDENTIAL_ATTRIBUTES' and INDEX_TYPE='NORMAL';
spool off
@/tmp/rebuild_idx_${v_schema}.sql
exit;
EOF
echo "done"
echo "logs index under /tmp/rebuild_idx_INEM_REP.log"

Working with working set in eclipse

Working with working set in eclipse:

Let assume an application made of many related  eclipse (for example a gui project many depends on many projects of back end layer). To have a working compilation no choice, you have to import all projects in eclipse. But it is noising have so many projects you don’t work to on project explorer only for compilation needs (see example). To avoid that problem you may use eclipse working set which allows you to have a restricted view of only projects of your interest (other projects still live behind)

See example by pictures:
Step1 = 2 projects in my workspace -> select working set menu




Step2= create a new working set

Step3 = select projects belonging to working set



Step4 = from fist menu (see figure for step 1) select working set you want work to