Pages

Clicksor

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
https://blogs.oracle.com/jdevextensions/entry/don_t_fear_the_audit 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 FetchSizeTransform.java and FixFetchSize.java. 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 :
(1) FetchSizeAnalyzer.java
(2) FetchSizeAnalyzerAddin.java
(3) Res.properties
(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="http://jcp.org/jsr/198/extension-manifest">
  <name>Audit Test Rule</name>
  <owner>Oracle</owner>

  <dependencies>
    <import>oracle.jdeveloper</import>
    <import>oracle.ide.audit</import>
    <import>oracle.ide.audit.core</import>
    <import>oracle.ide</import>
    <import>oracle.javacore</import>
    <import>oracle.jdeveloper.refactoring</import>
  </dependencies>

  <classpaths>
    <classpath>../../../oracle_common/modules/oracle.javatools_11.1.1/javatools-nodeps.jar</classpath>
  </classpaths>

  <hooks>
    <jdeveloper-hook xmlns="http://xmlns.oracle.com/jdeveloper/1013/extension">
      <addins>
        <addin headless="true">oracle.ide.extsamples.audits.FetchSizeAnalyzerAddin</addin>
      </addins>
    </jdeveloper-hook>
    </hooks>
</extension>

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 FetchSizeAnalyzer.java

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 custom.rules.audit.properties
    private static final Localizer LOCALIZER =
        Localizer.instance("oracle.ide.extsamples.audits.Res");
    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.
        INVALID_FETCH_SIZE.setEnabled(true);
    }
 
    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")) {
        setEnabled(false);
      }
    }
  }

  /**
   * 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 = context.report(INVALID_FETCH_SIZE);
        report.addParameter("currentsize", size);
      }
    }
  }
}

Now we create FetchSizeAnalyzerAddin.java

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 Res.properties
# 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.

4 comments:

Melvin van der Kuijl said...

Hello,

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

Regards,

Melvin

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 https://blogs.oracle.com/jdevextensions/entry/don_t_fear_the_audit

Regards,
Arvinder.

Anonymous said...

HI Melvin,

I am using this blog to create the extended rules in Audit, in Jdeveloper version 11.1.1.7, 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

Thanks

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
Rajesh

Information

Chitika