Monday, November 24, 2008

SCJP 6 Upgrade Exam (310-056)

by Fábio Souza

Guys and girls,

I guess the best way to start this post is with my appologies. I really wanted to post it earlier, but you all know how things are =P.

It has been almost three months from when I took the SCJP 6 upgrade exam (310-056). The exam follows the same SCJP 5's (310-055) difficulty level where it's needed to pay attention to every detail. There is time enough to do everything calmly but the tricks are there like they were in SCJP 5. The "secret" is still the same: MAKE LOTS OF EXERCISES. In my case, my knowledge on 310-055 was pretty sharp and I just had to work out the new features of Java 6. Lets talk about the exam and how to go for it.

PS: To get your knowledge on 310-055 "pretty sharp", I recommend my previous post: SCJP5 and 6


Technical information:
  • Number of Questions: 48
  • Score needed: 66% (32 of 48 questions)
  • Time limit: 150 minutes

Resource material:

For those who don't have (or want to have) specific SCJP 6 material (the same of the previous post):

For those who want to buy new material:
* I've never read this book but if the quality is the same of the previous version, there is no doubt it should work.

I made an abstract to help me with the SCJP 6 content by comparing the requisites for both exams (310-055 and 310-065), and studying the Java 6 new features. Here it is:

- Ordered and Sorted comparison:

Click me

- NavigableSet and NavigableMap methods comparison:

Click me

- Other things:
  • Deque: in this interface's javadoc there is a full explanation about the different methods that can be used to add/remover an object of this queue. Be sure that you know the ones that returns an special value (generally null) or that throws an exception instead, when something goes wrong.

  • ArrayDeque: the best (fastest) way to make FIFO and LIFO queues. This is also the recommended implementation when we need a Stack.

  • LinkedList: choose this implementation when objects should be added/removed between the first and the last element.

  • java.util.Collections: there are two new methods in this class, newSetFromMap and asLifoQueue, learn them.

  • java.util.Arrays: avoid System.arraycopy, use the new methods Arrays.copyOf or Arrays.copyOfRange instead.

  • java.io.File: getFreeSpace, getTotalSpace, setExecutable, setWritable, setReadable. These are new methods to learn.

  • java.io.IOException: there are new constructors that enable exception chaining IOException(String,
    Throwable)
    and IOException(Throwable).

  • java.io.Console: this is a new class and its instance should be got using System.console. I think all of its methods are importants, take a look at the javadoc. Pay attention to the Console.readPassword, it will fill an array with the provided password, it is a good think to know how to clean this array. This is a good way: java.util.Arrays.fill(src, '\0') and this is a valid and bizarre way: java.util.Arrays.fill(src, 0, src.length, (char)0).
SCJP 6 - To do or not to do

Because the difference between Java 5 and Java 6 is very little, in my opinion, people who already have the SCJP 5 don't need to worry about SCJP 6. For those who don't have SCJP 5 or any SCJP certificate at all: DON'T DO the Java 5 exam; GO STRAIGHT to Java 6.
In my exam (just to freeze: 310-056), the Java 6 content was kind of ridiculous. Only THREE specific questions. It means (this is not an advice) I could take the SCJP 6 exam without any knowledge about its specific content. I'll tell you how it was like:
  • java.io.Console: A question about its usage. It was necessary to know how to get the Console instance and how to get typed lines.

  • java.util.NavigableSet and the implementation java.util.TreeSet: The question was about how to use them focusing the method subset.

  • Chosing the best Collection implementation: I don't remember exactly what was asked, but keep this in mind:

    • Stack or queue needed: use ArrayDeque.

    • Removing/Adding elements that might not be at the top or at the bottom: LinkedList.

    • Sorted Collection (unique elements): TreeSet.
That is all folks! Please send your comments, corrections and share your own experiences.

Friday, August 8, 2008

SCJP 5 and 6

by Fábio Souza

OK, I know that is a little late to talk about SCJP 5 but I will give my feedback about the exam. This will help people that feels unprepared to take SCJP 6 (that was my case) and the ones that are going to take this new version, what I know is that both exams (5 and 6) are very similar.

Resources:
  • Main resource material: SCJP Sun Certified Programmer for Java 5 Study Guide (Exam 310-055), by Katherine Sierra (Author), Bert Bates (Author). This book is just great! The authors are very careful with the explanations and they also made very good illustrations. Each chapter ends with the "Two-minute Drill" section and the exercises. The "Two-minute Drill" has an abstract about the chapter that makes very easy to review forgotten items. The exercises are very well written, and every answer is explained (both wrong and right).
  • Main resource material for exercises: MasterExam. My SCJP Sun Certified Programmer for Java 5 Study Guide (Exam 310-055) book came with a CD containing the MasterExam software. This software gives two ways to do its questions, like the real exam or "ad-hoc". The MasterExam's questions are like the book. A "permission" to get a bonus exam also come with the CD, you just need to register on their site and then download the program.
  • Other resources for exercises that I found:
    • Whizlabs: There is a software like MasterExam made by Whizlabs. It seems to be a very good software. Its interface is great and you have the possibility to do "adaptive" tests. I downloaded a trial version from their website and I liked it pretty much.
    • EPractize: Their software is like the MasterExam too but it doesn't have any strength point. I didn't like the software's interface. I also downloaded a trial version from their website.
    • "TestKiller": This one is an exercise's book and seems to be made by Troytec. It's a very weird book, with a lot of writing mistakes on it. The strength point is that it promises to cover only questions from the real exam. When I took the exam I had the feeling of recognizing one or two questions (I took a look on 60 from 224 questions of this book).
My study schedule: That was the way I worked out to pass the exam.
  • 8 months to read the SCJP Sun Certified Programmer for Java 5 Study Guide (Exam 310-055) and to do all exercises. It took me something like one hour per day (only workdays). It's a good way to start because you can understand how Java works without pushing yourself.
  • 1 month to read again all "Two-minute Drill" sections and the most complicated chapters (chapters 7, 9, 8, 6, in descending difficulty order, in my opinion).
  • 4 days (eight hours per day) to do A LOT of exercises. This is the main point. The exercises are full of tricks and the only way to become familiar with those tricks is doing a lot of exercises.
The exam: In my opinion, the exam is difficult. You have to be really confident before taking it. A good thing that I noticed was that the time is enough to do everything with attention and patience. In my opinion to take this exam you must know about everything, but with a special care with these points below:
  • Threads.
  • Generics.
  • Methods Override/Overload (and covariant return).
  • Box/Unbox.
  • Method calling with widening and autoboxing.
  • Access Control.
  • java and javac commands.
  • Differences between collections (Ordered, Sorted, Hash).
  • How equals and hashCode works with collections.
  • Inner Classes.
Differences between SCJP 310-055 and 310-065 (you can find it here):
  • Questions concerning System.gc() have been removed.
  • Coverage of the java.io.Console class has been added.
  • Coverage of navigable collections has been added.
  • Several of the previous objectives have been strengthened (so you can expect more questions and more detailed questions on them). These strengthened objectives include: exception handling, collection classes and collection interfaces, assertions, threads, and flow control.
  • Number of questions: 310-055 = 72; 310-065 = 72
  • Pass score: 310-055 = 59%; 310-065 = 65%
  • Time limit: 310-055 = 175 minutes; 310-065 = 210 minutes
Advice: If you aren't feeling confident to take the SCJP 6 exam just because you don't know what is new in this version, don't take the SCJP 5. I did it and I am a little regretful. There is no "SCJP 5 to 6" exam, there is only one "upgrade" exam to version 6 called "Exam 310-056". In this exam you have to prove your Java 5 knowledge again. Well, soon I will take the 310-056 and then I will post about it.

Please comment and share your experiences with SCJP (any version is welcome :)).

Links:

Tuesday, July 8, 2008

There's a cook in JDev's development team indeed

Sometime ago I was surprised with a peculiar "tip of the day" which simply mentioned a traditional angel cake recipe.

Well, today I got the confirmation. There's certainly a cook amongst JDev's developers!

Look at the "tip" showed to me today:



Hmmmm... interesting... :)

Wednesday, May 7, 2008

Windows XP SP3 crashing JDeveloper 10.1.3.3

by Eduardo Rodrigues


Attention!!!



Today, my Windows XP automatically upgraded to SP3 and, after rebooting, my JDev 10.1.3.3 began to crash during its initialization, generating a JVM core dump in directory <JDEV_HOME>\jdev\bin.

After a quick look into the dump file, I noticed some problem with VFS, so I decided to disable VFS commenting the following line in file <JDEV_HOME>\jdev\bin\jdev.conf:

#AddVMOption -DVFS_ENABLE=true

Then, thank God, JDev came back to life again. So, don't panic.

Cheers!

Friday, April 25, 2008

OracleAS Java Object Cache

by Fábio Souza

Before I start, I would like to say that many texts and observations were took from the Oracle® Containers for J2EE Services Guide 10g (10.1.3.1.0), Chapter 7.

What is it?
The Java Object Cache (JOC) is a OC4J service that makes cache (in memory or disk) of any kind of Java object.
Use it:
  • To store frequently used objects.

  • To store objects that are costly to create/retrieve.

  • To share objects between applications.
Characteristics:
  • By default, cached objects are stored in memory but disk persistence can be configured.

  • By default, cached objects are local. The DISTRIBUTE mode can be configured.

  • By default, the "write lock" is disabled.

  • Cached objects doesn't have a "read lock".

  • Cached objects are represented by name spaces.

  • Cached objects are invalidated based on time or an explicit request.

  • Cached objects can be invalidated by group or individually.

  • Each cached Java object has a set of associated attributes that control how the object is loaded into the cache, where the object is stored, and how the object is invalidated.

  • When an object is invalidated or updated, the invalid version of the object remains in the cache as long as there are references to that particular version of the object.
The Java Object Cache organization:
  • Cache Environment. The cache environment includes cache regions, subregions, groups, and attributes. Cache regions, subregions, and groups associate objects and collections of objects. Attributes are associated with cache regions, subregions, groups, and individual objects. Attributes affect how the Java Object Cache manages objects.

  • Cache Object Types. The cache object types include memory objects, disk objects, pooled objects, and StreamAccess objects.

    This table gives a little description about the cache environment and the object types.
JOC API:
Only "cache.jar" must be added to application's classpath to start with JOC API. This archive is located in $ORACLE_HOME\javacache\lib\cache.jar. A project in JDeveloper just needs to import the built-in "Java Cache" library.
Distributed Cache Characteristics:
  • The cache management is not centralized. Cache updates and invalidation are propagated to all application server nodes.

  • The OAS' JOC configuration of each OAS node is not propagated to other nodes.

  • The distributed objects must be in the same namespace in each node.
Distributed Cache Configurations:
There are two ways to configure JOC: programmatically (using the oracle.ias.cache.Cache class) and through the javacache.xml file. This post only covers javacache.xml.

Configuring the javacache.xml file:

The javacache.xml localization is specified in the server.xml OC4J configuration file (tag: "<javacache-config>"). To work with javacache.xml it is necessary to start the container with the -Doracle.ias.jcache=true property.
To run JOC in "distributed mode", <communication> tag must be configured in the javacache.xml file like the example below:

<communication>
   <!-- "isdistributed" must be "true" to JOC work with DISTRIBUTE marked objects -->
   <isdistributed>true</isdistributed>
   <!-- Each JOC node must have a "discoverer" element to itself. -->
   <discoverer ip="192.168.0.2" port="7000">
   <discoverer ip="192.168.0.3" port="7000">
</communication>

Configuring the distributed cached objects:
There are three important attributes that we can use when configuring objects:
  • DISTRIBUTE: used to mark objects that will be shared between applications.

  • SYNCHRONIZE: used to allow "write locking" on cached objects.

  • SYNCHRONIZE_DEFAULT: the same use as the SYNCHRONIZE attribute, but it can mark only a region or a group. When they are marked, all objects will be allowed to use the write lock.
The example below shows a distributed cache utilization:

public void defineRegions() {
   // Creating attributes
   Attributes remoteRegionAttr = new Attributes();

   /*
    * Extracted from Attributes.setFlags() javadoc:
    * specifies which of the listed attributes should be set in the Attributes object.
    * The flags may be OR'ed together, i.e., Attributes.DISTRIBUTE | Attributes.SPOOL.
    * Any previous settings will be disregard.
    */
   remoteRegionAttr.setFlags(Attributes.SYNCHRONIZE_DEFAULT | Attributes.DISTRIBUTE);

   try {
      // Creates the "RemoteRegion" with the specified attributes.
      CacheAccess.defineRegion("RemoteRegion", remoteRegionAttr);
   } catch (Exception e) {
      e.printStackTrace();
   }
}

public void createCachedObject(Object object) {
   CacheAccess access = null;
   try {
      // Accessing the created region.
      access = CacheAccess.getAccess("RemoteRegion");
      // Owning cache object's write lock (5 seconds to timeout)
      access.getOwnership("CachedObject", 5000);
      // Caching the object
      access.put("CachedObject", object);
      // Releasing cache object's write lock (5 seconds to timeout)
      access.releaseOwnership("CachedObject", 5000);
   } catch (Exception ex) {
      ex.printStackTrace();
   } finally {
      if (access != null) {
         /*
          * Extracted from CacheAccess.close() javadoc:
          * releases the resource used by current CacheAccess object.
          * Application is required to make this call when it no longer need this CacheAccess instance.
          */
         access.close();
      }
   }
}

public Object retrieveCachedObject() {
   Object object = null;
   CacheAccess access = null;
   try {
      access = CacheAccess.getAccess("RemoteRegion"); // Accessing the created region.
      /*
       * Extracted from CacheAccess.get() javadoc:
       * returns a reference to the object associated with name.
       * If the object is not currently in the cache and a loader object has been
       * registered to the name of the object, then the object will be loaded into
       * the cache. If a loader object has not been defined for this name, then, for
       * DISTRIBUTE object, the default load method will do a netSearch for the object
       * to see if the object exist in any other cache in the system; for a non
       * DISTRIBUTE object, an ObjectNotFoundException will be thrown. The object
       * returned by get is always a reference to a shared object. Get will always
       * return the latest version of the object. A CacheAccess object will only
       * maintain a reference to one cached object at any given time. If get is called
       * multiple times, the object accessed previously will be released.
       */
      object = access.get("CachedObject");
      // It's not necessary to own the writing lock to read cached objects.
   } catch (Exception ex) {
      ex.printStackTrace();
   } finally {
      if (access != null) {
         /*
          * Extracted from CacheAccess.close() javadoc:
          * releases the resource used by current CacheAccess object.
          * Application is required to make this call when it no longer need this CacheAccess instance.
          */
         access.close();
      }
   }
   return object;
}


Concurrent access sensible applications may need read and write synchronism. Because JOC only offers write lock, I had to "force" read lock. This is the solution I found:

/**
 * This method reads and updates a cached object forcing synchronizing both to
 * reading and writing.
 * If it is being executed in multiple threads, each thread will only be able to
 * read the object when another thread release it's writing lock. The writing lock will
 * be released after a thread finishes it's reading and writing.
 */
public void synchronizedReadUpdateCachedObject() {
   CacheAccess access = null;
   try {
      access = CacheAccess.getAccess("RemoteRegion");
      access.getOwnership("CachedObject", 5000);
      Object object = access.get("CachedObject");
      System.out.println(object.toString());
      Object newCachedObject = "New Cached Object";
      // This is the method used to update cached objects.
      access.replace("CachedObject", newCachedObject);
      access.releaseOwnership("CachedObject", 5000);
   } catch (Exception ex) {
      ex.printStackTrace();
   } finally {
      if (access != null) {
         access.close();
      }
   }
}

Declarative Cache:
The JOC offers a way to configure its regions, subregions, groups and objects using XMLs. To use this feature the "preload-file" tag must be added to the javacache.xml. This tag will point to the XML with the environment definitions. More information at: http://http//download.oracle.com/docs/cd/B32110_01/web.1013/b28958/joc.htm#i1085809

CacheWatchUtil:
By default, the Cache Service provides the CacheWatchUtil cache monitoring utility that can display current caches in the system, display a list of cached objects, display caches' attributes, reset cache logger severity, dump cache contents to the log, and so on. It depends on the "dms"library. To execute:

java -classpath $ORACLE_HOME\lib\dms.jar;$ORACLE_HOME\javacache\lib\cache.jar oracle.ias.cache.CacheWatchUtil -config=<path_to_javacache.xml>
More Oracle Application Server 10g caches (entirely took from OC4J documentation):
  • Oracle Application Server Web Cache. The Web Cache sits in front of the application servers (Web servers), caching their content and providing that content to Web browsers that request it. When browsers access the Web site, they send HTTP requests to the Web Cache. The Web Cache, in turn, acts as a virtual server to the application servers. If the requested content has changed, the Web Cache retrieves the new content from the application servers.

    The Web Cache is an HTTP-level cache, maintained outside the application, providing fast cache operations. It is a pure, content-based cache, capable of caching static data (such as HTML, GIF, or JPEG files) or dynamic data (such as servlet or JSP results). Given that it exists as a flat content-based cache outside the application, it cannot cache objects (such as Java objects or XML DOM—Document Object Model—objects) in a structured format. In addition, it offers relatively limited postprocessing abilities on cached data.

  • Web Object Cache. The Web Object Cache is a Web-application-level caching facility. It is an application-level cache, embedded and maintained within a Java Web application. The Web Object Cache is a hybrid cache, both Web-based and object-based. Using the Web Object Cache, applications can cache programmatically, using application programming interface (API) calls (for servlets) or custom tag libraries (for JSPs). The Web Object Cache is generally used as a complement to the Web cache. By default, the Web Object Cache uses the Java Object Cache as its repository.

    A custom tag library or API enables you to define page fragment boundaries and to capture, store, reuse, process, and manage the intermediate and partial execution results of JSP pages and servlets as cached objects. Each block can produce its own resulting cache object. The cached objects can be HTML or XML text fragments, XML DOM objects, or Java serializable objects. These objects can be cached conveniently in association with HTTP semantics. Alternatively, they can be reused outside HTTP, such as in outputting cached XML objects through Simple Mail Transfer Protocol (SMTP), Java Message Service (JMS), Advanced Queueing (AQ), or Simple Object Access Protocol (SOAP).
References:
Oracle Application Server Containers for J2EE Services Guide 10g Release 3 (10.1.3)
Oracle Application Server 10g (10.1.2) JOC Tutorial
Oracle Application Server 10g (10.1.2) JOC javadoc