Category Archives: Java

Configurations in the web.xml file to maximize ADF Application Performance


Disable Automation

<context-param>
<param-name>oracle.adf.view.rich.automation.ENABLED</param-name>
<param-value>false</param-value>
</context-param>

Disable Assertion

<context-param>
<param-name>oracle.adf.view.rich.ASSERT_ENABLED</param-name>
<param-value>false</param-value>
</context-param>

Disable Javascript Profiler

<context-param>
<param-name>oracle.adf.view.rich.profiler.ENABLED</param-name>
<param-value>false</param-value>
</context-param>

Disable Debug Mode

<context-param>
<param-name>org.apache.myfaces.trinidad.resource.DEBUG</param-name>
<param-value>false</param-value>
</context-param>

Disable Javascript Debugging

<context-param>
<param-name>org.apache.myfaces.trinidad.DEBUG_JAVASCRIPT</param-name>
 <param-value>false</param-value>
</context-param>

Disable File Modification

<context-param>
<param-name>org.apache.myfaces.trinidad.CHECK_FILE_MODIFICATION</param-name>
<param-value>false</param-value>
</context-param>

Enable partitioning

<context-param>
<param-name>oracle.adf.view.rich.libraryPartitioning.DISABLED</param-name>
<param-value>false</param-value>
</context-param>

Enable compression

<context-param>
 <param-name>org.apache.myfaces.trinidad.DISABLE_CONTENT_COMPRESSION</param-name>
 <param-value>false</param-value>
</context-param>

All are explained in the documentation.
http://docs.oracle.com/cd/E17904_01/core.1111/e10108/adf.htm#BDCBIJAB
Only web.xml related parameters are added here

Advertisements

Solving Java code in jsp source files is not allowed in ojsp.next mode


Java code in jsp source files is not allowed in ojsp.next mode

when I got this error searched over the net and found this link

http://fmwtips.wordpress.com/2011/05/22/error-java-code-in-jsp-source-files-is-not-allowed-in-ojsp-next-mode/

but this changes didn’t help me much. Finally removed all the entries from web.xml to resolve this.

Jdeveloper 11.1.1.6 released


Jdeveloper 11.1.1.6 released..

http://www.oracle.com/technetwork/developer-tools/jdev/index-088099.html

Interesting Questions


Why map is not a collection?

collection acts upon values. map acts on key value pair

Why the finalize method is protected?

Finalize method is for the JVM to execute before GC.imagine if finalize is public then any object can call directly which is not what we wanted. If you have some open connection you can write cleanup codes in the finalize method in your class and leave the rest of the work to the JVM to call the finalize method.

The finalize method is made protected for the subclass to override it or any finalize operations.

Can a finalize method be called for twice?

The finalize method is never invoked more than once by a Java virtual machine for any given object.

Any exception thrown by the <code>finalize</code> method causes the finalization of this object to be halted, but is otherwise ignored.

why the clone is protected?

clone is protected because it is something that ought to be overridden so that it is specific to the current class.It’s protected so you don’t use the one in object (it’ll just throw an exception anyway). They want you to override it in a class, then you make it public

How to convert BigDecimal to Integer?


BigDecimal b = new BigDecimal("123");

Integer i = new Integer(String.valueOf(b));

Why Serializable and Cloneable is not having anything?

Serializable and Cloneable are called marker or tag interface because they tag all derived class on purpose. Java has a default clone() method in Object class which should not be applied for all the class. So whenever user creates a class the object is not cloneable. The object is cloneable only when it has the marking of Cloneable interface. if any class implements Cloneable then it is understood by the JVM that the object is eligible for cloning and Object by default provides the cloning method this also provide the class to implement their own clone operation by making it protected

The class is marked Serializable for the JVM to understand that the class can be serialized

 

Breaking singleton pattern


Singleton pattern in java can be implemented by the approaches explained here

Now here is the code to break it using reflection


public class NormalClass{

 private NormalClass(){ //having private constructor
 }

 private void print(){
 System.out.println(&amp;quot;broken&amp;quot;);
 }

}

/////////////////////////////////

public class Main{

 public static void main(String[] args) throws InstantiationException, IllegalAccessException, SecurityException, NoSuchMethodException, IllegalArgumentException, InvocationTargetException {

 Class c = NormalClass.class;
 final Constructor con = c.getDeclaredConstructor();
 System.out.println(con);

 final Method meth = c.getDeclaredMethod(&amp;quot;print&amp;quot;, null);

//---- You will get IllegalAccessException without this ---
AccessController.doPrivileged(new PrivilegedAction() {

public Object run() {
 con.setAccessible(true);// turning off the access check
 meth.setAccessible(true);// turning off the access check
return null;
 }

});

 NormalClass n = (NormalClass)con.newInstance(null);
 meth.invoke(n,null);
 }

}

Java Annotations


Why do we use Annotations?

Annotations give information about the java program and is not part of the program itslf
They give inforamtion to the compiler for Compiler time processing, deployment time processing and runtime processing

Some of the annotations are

@Author(
 name="vinod",
 date="3/12/2010"

)
public class MyClass{}

@suppressWarnings{value="unchecked"}
public void myMethod(){}

@override
public void run(){}

@Deprecated
public void printWriter(){}

There are two types of annotations

Simple and Meta Annotations

Simple Annotations – provided by JDK5

* Override
* Deprecated
* Suppresswarnings

Meta Annotations – Annotations of Annotation

* Target
* Retention
* Documented
* Inherited

Target – target element for which the annotation is applicable

* @Target(ElementType.TYPE) – applied to any element of a class
* @Target(ElementType.FIELD) – applied to a field or property
* @Target(ElementType.METHOD) – applied to a method level annotation
* @Target(ElementType.PARAMETER) – applied to the parameters of a method
* @Target(ElementType.CONSTRUCTOR) – applied to constructors
* @Target(ElementType.LOCAL_VARIABLE) – applied to local variables
* @Target(ElementType.ANNOTATION_TYPE) – indicates that the declared type itself is an annotation type

Retention – how long the annotations should be retained

* RetentionPolicy.SOURCE – retained only at the source level and will be ignored by the compiler
* RetentionPolicy.CLASS – retained by the compiler at compile time, but will be ignored by the VM
* RetentionPolicy.RUNTIME – retained by the VM so they can be read only at run-time

Documented – should be documented in by the javadoc tool

Inherited -annotations of the parent class is inherited to the subclass

Producer and Consumer Problem


Self explanatory Producer and Consumer problem

Producer.java

package com.producer;

import java.util.ArrayList;

import java.util.Random;

public class Producer implements Runnable{

//Engine Batch ID reference

ArrayList engineList;

//Engine No is stored here

String str;

//Assigning the BATCH ID reference

public Producer(ArrayList engineList) {

this.engineList = engineList;

}

/*

* Produce Engines

*/

public void produce(){

//Random number for engine number

str = ""+Math.abs(new Random(System.currentTimeMillis()).nextLong());

//Only one thread can enter the BATCH for any operation

synchronized (engineList) {

//Add the Engine to the batch

engineList.add(str);

//Notify all other threads that the new engine is added to the BATCH

engineList.notifyAll();

}

//Print Engine Number

System.out.println("Produced :: ENGINE CHASIS NO ["+ str + "]");

}

@Override

public void run() {

System.out.println("Production started");

//10 Engine is to be produced

for(int i=0;i<10;i++){

try {

//Produce the engine

this.produce();

//Sleep for 2 seconds for the next operation

Thread.sleep(2000);

} catch (InterruptedException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

}

}

Consumer.java


package com.producer;

import java.util.ArrayList;

public class Consumer extends Thread{

//Engine Batch ID reference

ArrayList<String> engineList;

//Engine No is stored here

String str;

//Assigning the BATCH ID reference

public Consumer(ArrayList<String> engineList) {

this.engineList = engineList;

}

/*

* Consume Engines from the BATCH

*/

publicĀ  void consume(int index) throws InterruptedException{

//Only one thread can enter the BATCH for any operation

synchronized (engineList) {

//if the Batch has no engines

while(engineList.size() == 0){

//then wait for some time until you get a new engine to consume

engineList.wait();

}

//If the engine is deployed to the BATCH

if(engineList.size() >= 0){

//The consume the engine from the respective position

str = engineList.get(index);

}

}

//Print Engine Number that it has been consumed

System.out.println("Consumed :: -------> ENGINE CHASIS NO ["+ str + "]" );

}

@Override

public void run() {

//10 Engine has to be consumed

for(int i=0;i<10;i++){

try {

//Produce the engine

this.consume(i);

//Sleep for 3 seconds for the next operation

Thread.sleep(3000);

} catch (InterruptedException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

}

}

Main.java


package com.producer;

import java.util.ArrayList;

public class Main {

public static void main(String[] args) throws InterruptedException {

//ArrayList to store the produced engines - BATCH

ArrayList<String> engine = new ArrayList<String>();

//Producer taking the engine BATCH ID Reference

Producer producer = new Producer(engine);

//Consumer taking the reference of the engine BATCH ID Reference

Consumer consumer = new Consumer(engine);

//Threads

Thread prodThread = new Thread(producer);

Thread conThread = new Thread(consumer);

//Starting the production

prodThread.start();

//Start Consumption

conThread.start();

}

}