-
-
-
-
- Bay Area
-
- Massachusetts
Anything that interests me finds place here. Software dominates my interests and hence a good part of this blog as well.
-
-
-
-
- Bay Area
-
- Massachusetts
There may be situations where in you only want to log the call stack (to find out how a method got invoked). An easy way to do this is to throw a new java.lang.Exception(“”). Catch it and log it in the log file. This way you can have the call stack for a method invocation without having the bebug.
try
{
throw new java.lang.Exception("Dummy exception to display call stack");
} catch (java.lang.Exception e)
{
log.debug("stack trace", e); // This will print the exception on to the log file
}
public String[] getFieldValues( DataObject bean )
{
List<String> fieldValues = new ArrayList<String>();
try
{
// TBD: handle null key
fieldValues.add(PropertyUtils
.getSimpleProperty(bean, "key")
.toString());
PropertyDescriptor[] properties =
PropertyUtils.getPropertyDescriptors(bean);
for (PropertyDescriptor property : properties)
{
String propertyName = property.getName();
Object propertyValue =
PropertyUtils.getSimpleProperty(bean, propertyName);
if (null != propertyValue
&& !propertyValue.toString().isEmpty())
{
fieldValues.add(propertyValue.toString());
}
}
}
catch (Exception ite)
{
// TBD: handle this
}
return fieldValues.toArray(new String[0]);
}
public static boolean isBlank(String str) will return true if str is null or trimmed str is “”
You write in java and GWT compiles to javascript. Different dialects for each browser generated.
Used to write rich client applications like think client applications. GWT is a client side. Had remote procedure calls to made server side calls. On server side
JSF and other web framework support GWT. So GWT can be used to make
Put the pedal to the metal (use a tech. to achieve traction). :-)
· Use sparingly and try not to overload variable argument methods as it becomes difficult to figure out which method will get invoked. They are invoked when no other method signature matches the call.
· Zero or more arguments can be passed for variable arguments
· Variable arguments can only be used as the final set of argument in a method
e.g.
…..func1(String one, String… args) // correct
…..func1(String… args, String one) // not correct
Old:
public class Test2
{
public static void main(String[] args)
{
System.out.println(contact(",",new String[]{"one", "two", "three"}));
}
private static String contact(String seperator, String[] tokens)
{
StringBuilder s = new StringBuilder();
for (String currentValue : tokens)
{
s.append(currentValue);
s.append(seperator);
}
return s.toString();
}
}
New:
public class Test3
{
public static void main(String[] args)
{
System.out.println(contact(",","one", "two", "three"));
}
private static String contact(String seperator, String... tokens)
{
StringBuilder s = new StringBuilder();
for (String currentValue : tokens)
{
s.append(currentValue);
s.append(seperator);
}
return s.toString();
}
}
A business object is a software representation of a real life entity. The entity could be a person, process, event or place. Examples of business objects are employees, products, invoices and payments. Business objects contain business data and model business behavior/process.
Business objects are different from data object which only hold data and do not have any behavior.
There are 2 ways or 2 strategies to achieve business objects:
EJB Strategy: Uses Entity Beans to model business data (with or without Container Managed Persistence) and Session beans to model Business process.
Pojo Strategy: Use simple Pojo (Plain Old Java Objects) along with light weight persistence mechanism such as hibernate/JDO (Java Data Object) or JDBC.
Hibrid: Uses POJOs behind the scenes using EJBs. Implement session façade in session beans and use POJOs to persist data.
public interface OPCAdminFacade extends EJBObject {
public OrdersTO getOrdersByStatus(String status)
throws RemoteException, OPCAdminFacadeException;
public Map getChartInfo(String request,
Date start,
Date end,
String requestedCategory)
throws RemoteException, OPCAdminFacadeException;
}
java.util.Date – Is a point in time (maintains millisecond accuracy)
java.util.Calendar –helps manipulate and compare java.util.Date
To convert Date to Calendar
Calendar cal1 = Calendar.getInstance();
Date currentTime = new Date(); // run on May, 28th 2009
cal1.setTime(currentTime);
assertEquals(2009, cal1.get(Calendar.YEAR)); // returns true
assertEquals(Calendar.MAY, cal1.get(Calendar.MONTH)); // returns true
assertEquals(28, cal1.get(Calendar.DATE)); // returns true
To convert Calendar to Date
Date date2 = cal1.getTime();
To find dates relative to each other – example to find the date tomorrow and yesterday
Calendar cal1 = Calendar.getInstance();
Date currentTime = new Date();// run on May, 28th 2009
cal1.setTime(currentTime);
cal1.roll(Calendar.DATE, 1); // one day after the date represented by cal1 i.e. 29th
assertEquals(2009, cal1.get(Calendar.YEAR)); // returns true
assertEquals(Calendar.MAY, cal1.get(Calendar.MONTH)); // returns true
assertEquals(29, cal1.get(Calendar.DATE)); // returns true
cal1.roll(Calendar.DATE, -1); // one day before the date represented by cal1 i.e. 28th
assertEquals(2009, cal1.get(Calendar.YEAR)); // returns true
assertEquals(Calendar.MAY, cal1.get(Calendar.MONTH)); // returns true
assertEquals(28, cal1.get(Calendar.DATE)); // returns true
Class.forName("org.apache.derby.jdbc.EmbeddedDriver");// Driver document details class name to use
Connection conn = DriverManager.getConnection("jdbc:derby:COFFEES");
Connection conn = DriverManager.getConnection("jdbc:derby:COFFEES", "babu", "babupassword"); // Connect to COFFEES database using babu/babupassword as cred.
// DataSource as a JNDI resource
InitialContext ic = new InitialContext()
DataSource ds = ic.lookup("java:comp/env/jdbc/myDB");
Connection con = ds.getConnection();
// DataSource implementation provided by vendor
DataSource ds = (DataSource) org.apache.derby.jdbc.ClientDataSource()
ds.setPort(1527);
ds.setHost("localhost");
ds.setUser("APP");
ds.setPassword("APP");
Connection con = ds.getConnection();
import java.io.IOException;
interface TestInterface
{
void test();
}
public class Second implements TestInterface
{
public void test() throws IOException
{
System.out.println("Second.test() invoked");
}
public static void main(String[] args) throws IOException
{
TestInterface j = new Second();
j.test();
}
}
C:\users\Babu\temp\java>javac Second.java
Second.java:10: test() in Second cannot implement test() in TestInterface; overridden method does not throw java.io.IOException
public void test() throws IOException
^
1 error
import java.io.IOException;
interface TestInterface
{
void test() throws IOException;
}
public class Second implements TestInterface
{
public void test() // DOES NOT THROW IOEXCEPTION
{
System.out.println("Second.test() invoked");
}
public static void main(String[] args) throws IOException
{
TestInterface j = new Second();
j.test();
}
}
C:\users\Babu\temp\java>javac Second.java
C:\users\Babu\temp\java>java Second
Second.test() invoked
import java.io.IOException;
interface TestInterface
{
void test() throws IOException;
}
public class Second implements TestInterface
{
void test() throws IOException
{
System.out.println("Second.test() invoked");
}
public static void main(String[] args) throws IOException
{
Second j = new Second();
j.test();
}
}
C:\users\Babu\temp\java>javac Second.java
Second.java:10: test() in Second cannot implement test() in TestInterface; attempting to assign weaker access privileges; was public
void test() throws IOException
^
1 error
Eric says:
Trace – I use for start and end of methods.
Debug – I use for low level debugging, info about the processing of single records.
Info – I use as a sparse group type comment, like ‘101 records processed.’
Warn – I use when something I don’t like happened, but the user may not care.
Error – I use when something un-expected happened and I’m stopping the processing.
Fatal –
I don’t really use fatal I suppose I should – but how is it different then Error