Friday, 17 February 2012

ojaudit - create custom rule with JDeveloper 11g R1 - Part 1

After a long-long struggle I was able to create and execute a customized rule for ojaudit in JDeveloper 11g R1.
If you are struggling to separate ojaudit from JDeveloper ... forget it! ojaudit cannot be separated from JDeveloper. It needs the installed folder of JDeveloper to work upon.

The audit framework is very handy and company coding standards can be verified upon by leveraging on this.

Now... for your customized rule I have taken an example that John 'JB' Brock has mentioned in his blog where he has described customization of rule for JDeveloper 11g R2.

This custom rule checks your view objects and checks the FetchSize and returns a message if your view-object fetches more than 100 objects.

Start with creating a new extension project

Following is the snapshot of the files in my project:
For the time being I will not make use of and There is a feature provided in ojaudit which makes automated fixes for your rule. We will explore this feature in Part-2 of this post.

Files to be used are :
(4) extension.xml

We start with extension.xml :

Below is the code for it... so that you can copy-paste in your project.

<?xml version="1.0" encoding="UTF-8" ?>
<extension id="oracle.ide.extsamples.audits" version="1.0" esdk-version="1.0"
           rsbundle-class="oracle.ide.extsamples.audits.Res" xmlns="">
  <name>Audit Test Rule</name>



    <jdeveloper-hook xmlns="">
        <addin headless="true">oracle.ide.extsamples.audits.FetchSizeAnalyzerAddin</addin>

IMPORTANT :: The headless="true" property in the addin tag above enables your rule to be executed when you run ojaudit from command prompt and display it in your output file. Else the result will be displayed only in the JDeveloper editor where-ever your rules satisfies the error condition.

Next we create the

package oracle.ide.extsamples.audits;

import oracle.ide.model.Project;
import oracle.ide.model.Workspace;

import oracle.jdeveloper.audit.analyzer.Analyzer;
import oracle.jdeveloper.audit.analyzer.AuditContext;
import oracle.jdeveloper.audit.analyzer.Category;
import oracle.jdeveloper.audit.analyzer.Rule;
import oracle.jdeveloper.audit.analyzer.Severity;
import oracle.jdeveloper.audit.analyzer.ViolationReport;
import oracle.jdeveloper.audit.extension.ExtensionResource;

import oracle.jdeveloper.audit.service.Localizer;

import org.w3c.dom.Document;
import org.w3c.dom.Element;

 * This Analyzer will check the value of the FetchSize in a ViewObject
 * and make sure that it is not set larger then 100. This could result
 * in a performance issue if set to high
public class FetchSizeAnalyzer extends Analyzer {

    // Refers to
    private static final Localizer LOCALIZER =
    private final Category SAMPLE_CATEGORY =
        new Category("category-invalid-fetch-size", LOCALIZER);
  private Rule INVALID_FETCH_SIZE = new Rule("rule-invalid-fetch-size", SAMPLE_CATEGORY, Severity.WARNING,LOCALIZER);

        // Do this to make the rule enabled by default.
    public Rule[] getRules() {
        return new Rule[] { INVALID_FETCH_SIZE };

   * Enter a workspace.
   * @param context
   * @param workspace
  public void enter(AuditContext context, Workspace workspace) {
    //TODO add any workspace (application) validation code here

   * Enter a project.
   * @param context
   * @param project
  public void enter(AuditContext context, Project project) {
    //TODO add any project validation code here

   * Enter a document.
   * Check to see if this is a ViewObject file. If not, we can stop here.
   * @param context
   * @param document
  public void enter(AuditContext context, Document document) {

    String firstNodeOfdocument = document.getDocumentElement().getNodeName();
    if ("ViewObject".equals(firstNodeOfdocument)) {
      String filePath = context.getUrl().getPath();
      if (!filePath.substring(filePath.lastIndexOf("/") + 1).matches(".*View.xml")) {

   * Enter an element.
   * This is where we will check the value of the FetchSize element and report
   * the results back to the Editor.
   * @param context
   * @param element
  public void enter(AuditContext context, Element element) {

    if (element.getAttribute("FetchSize") != null &&
        element.getAttribute("FetchSize").trim().length() > 0) {
      int size = Integer.parseInt(element.getAttribute("FetchSize"));
      if (size > 100) {
        ViolationReport report =;
        report.addParameter("currentsize", size);

Now we create

package oracle.ide.extsamples.audits;

import oracle.jdeveloper.audit.AbstractAuditAddin;

public class FetchSizeAnalyzerAddin  extends AbstractAuditAddin{
    private static final Class[] ANALYZERS = new Class[] { FetchSizeAnalyzer.class };

    public Class[] getAnalyzers() {
        return ANALYZERS;

Lastly we have
# Translatable resources for extension oracle.ide.extsamples.audits

category.category-invalid-fetch-size.label=Sample Audits(ESDK)
category.category-invalid-fetch-size.description=Sample audits covering both XML and Java documents.

rule.rule-invalid-fetch-size.description=The fetch size of the view object cannot be more than 100.
rule.rule-invalid-fetch-size.message=Invalid Fetch size of ''{currentsize}''. The fetch size of the view object should not be more than 100 for better performance.
rule.rule-invalid-fetch-size.label=Invalid Fetch size.
rule.rule-invalid-fetch-size.tip=The fetch size of the view object cannot be more than 100.

#transform-set-fetch-size.label=Set the FetchSize attribute to 100.
#transform-set-fetch-size.description=This will rest the FetchSize attribute to the recommended value of  100.

Now do a Make of your project

Next... do a 'Deploy to Target Platform'

After this do a 'Run Extension'
A new instance of JDeveloper will open.
Goto Tools => Preferences => Audit => Profiles and check the rule listed with the rules as below:

To use the above Rule make sure you have selected it in your Profile.

Create a view object with name <something>View and set the Fetch Size property > 100 shown as below :

Your xml will show the audit message like below :

You can use this rule to generate an audit report by running the ojaudit tool from command prompt as well.


Melvin van der Kuijl said...


Thanks for this blogpost. Im currently experimenting with a typical Oracle SOA Projects. Is it possible to prevent a succesfull compilation if for example a Serverity.ERROR rule is compromised.

thanks in advantage



Arvinder Singh said...

Hi Melvin,

I am not sure whether the compilation can be stopped or not...I believe that can be done.
You can contact with John 'JB' Brock on his blog


Anonymous said...

HI Melvin,

I am using this blog to create the extended rules in Audit, in Jdeveloper version, but things are not going in same way as given in your blog. I have done all things as directed but now able to see anything in tools->preference->audit. Need help


Anonymous said...

Hi Arvinder,

Thanks for your post. I have implemented this and its working fine for me.

But I wanted to implemented the "FetchSizeTransform" also so that it it can fix automatically also.

Can you please share the Part 2 of post as you mentioned in this post.

Thanks in Advance

Anonymous said...

Thanks for your article , saved my day .