Friday, September 6, 2019

How interface reference variable can access methods of Object class ?



In above example, we have defined one empty Interface and one empty Class which implements Interface.
Try to run this java program, at #15, we get the output Class@6d06d69c something like that. But Instead of output, Compiler should throw an exception. Because toString method (Object Class Method) is not defined in the Interface.
If we check the parent of Class, It returns Object Class (#17) and parent of Inteface is null (#18).
It means Class gets the reference of toString method from the Object Class, but Interface has no any reference of toString method then why compiler is not throwing an exception ?
Lets dive into the Java Langauage Specification (§9.2).
If an interface has no direct superinterfaces, then the interface implicitly declares a public abstract member method m with signature s, return type r, and throws clause t corresponding to each public instance method m with signature s, return type r, and throws clause t declared in Object, unless an abstract method with the same signature, same return type, and a compatible throws clause is explicitly declared by the interface.
In simple words, interface (which is not extending any interface) implicitly declares Methods of Object Class. 

Thursday, June 13, 2019

Attach Java Source in Eclipse

In Eclipse editor, when you press F3 key on any User Defined Class / Interface / Annotation names, the IDE takes you to the source.
But, in case you want the same behavior for Java’s core classes or third party library, you can have it by attaching the Java source with the Eclipse IDE.
Steps to attach Java Source
1. Select any Class / Annotation / Interface / (Autowired) name and press F3 key


2. You get this page in editor


3. Attach Java source (If you do not see this in Context Menu, Please download Java Source Attacher plugin from Eclipse Marketplace)


4. It automatically downloads the source for you and attaches it.


Sunday, January 29, 2017

Steps for creating Data Source for EJB JPA in WebLogic


1. Please enter username and password

Default :
Username : weblogic
Password : welcome1



2. Click on Services -> Data Sources (any one link)



3. New -> Generic Data Source


4. Name : untitled (which is in persistence.xml in source code), 
Leave other fields blank, Click on next


persistence.xml



5. Click on Next

select database and drive according to your requirement


6. Click on Next


7. Click on Next

Database Name : Database SID (pune)
Host Name : Database IP (172.94.96.213)
Port : Database port (1521)
User Name : database user name (abc)
Password : database password (abc)


8. Click on Test Configuration


9. Find Message "Connection test succeeded." and Click on Next


10. Check AdminServer and Click on Finish


11. Find below message And restart the server


What is relation between toString() and hashCode() methods ?


Yes, of course both methods belongs to the Object class. but there is something which links these two methods.

toString()

When you call this method, It returns a String. (com.pradip.Test@3dd4ab05)

It consists of class name followed by an "at" sign (@) and the unsigned Hexadecimal number.

hashCode() 

and when you call this method, It returns a integer. (1037347589)

When you convert this integer to hexadecimal, It is same as toString() method hexadecimal.




Thursday, April 28, 2016

@PreAuthorize & @Secured not working in Spring Security ?


Do not put <global-method-security/> in spring-security.xml file


Please find the tag(<global-method-security/>) in mvc-dispatcher-servlet.xml




source : http://huahsin68.blogspot.in/2014/05/why-secured-and-preauthorize-isnt.html

Monday, August 31, 2015

Data Encryption using AES (Advanced Encryption Standard) in MySql


This example is tested in MySql Version : 5.0.80-enterprise-nt-log. to know your version please fire below query.

SHOW VARIABLES LIKE 'version';


Table Script :

CREATE TABLE `test` (
  `id` bigint(6) unsigned NOT NULL auto_increment,
  `simple_name` varchar(100) default NULL,
  `encrypted_name` varbinary(112) default NULL,
  PRIMARY KEY  (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;


Why VARBINARY or BLOB ?


This will avoid potential problems with trailing space removal or character set conversion that would change data values, such as may occur if you use a nonbinary string data type (CHAR, VARCHAR, TEXT).


How to calculate encrypted text length ?

function : 16 * (trunc(string_length / 16) + 1)

Example : 
       lets consider string_length = 100.
       similar mysql query : SELECT (16 * (TRUNCATE(100 / 16, 0) + 1)) encrypted_length;
       ans : 112 (simple multiplication of 16).


simple_name : Procrastination is the art of keeping up with yesterday
key : SHA1('Lorem ipsum dolor sit amet')

Why (SHA / SHA1 / SHA2) in key generation ?

For a key length of 128 bits, the most secure way to pass a key to the key argument is to create a truly random 128-bit value and pass it as a binary value.


AES Encrypt :

Syntax : AES_ENCRYPT(str,key)

INSERT INTO test(simple_name, encrypted_name)
VALUES('Procrastination is the art of keeping up with yesterday',
AES_ENCRYPT('Procrastination is the art of keeping up with yesterday', SHA1('Lorem ipsum dolor sit amet')));

AES Decrypt :

Syntax : AES_DECRYPT(crypt_str,key)

SELECT AES_DECRYPT(encrypted_name, SHA1('Lorem ipsum dolor sit amet')) encrypted_name FROM test;


source : https://dev.mysql.com/doc/refman/5.5/en/encryption-functions.html