Unknown's avatar

Posts by subham mohapatra

An IT professional , intrested in new technologies. Writes blogs about java and hybris

How to customize platform code without creating extension in Hybris

The topic is about a basic feature of hybris where we can add or customize Platform code without writing one extention. So now the point is when we need to do this or go for this scenario ? The answer is when we are going for a very small change like one java file or a xml file or adding a new version of jar file then despite of creating one extension for it we can do it with some hybris inbuild feature. So lets check the steps with one example how to do this.

Process to Customize Platform:

As you know after hybris ant build there is config folder created in hybris directory as below.folder_structure

So when we go in to the config folder it gives us on customize folder.

customize_folder

Now if we want to customize a java file which is in the directory as hybris\bin\platform\ext\platformservices\src\de\hybris\platform\catalog named as CatalogService.java. So what we exactly have to do is write a customize java file CatalogService.java and put it inside the customize folder as the same directory structure it is defined. So you have to keep the customized java file in the directory as customize/platform\ext\platformservices\src\de\hybris\platform\catalog 

Then do one ant customize and it will take the file from the customize folder.

Hope I have explained the topic properly,you can reach to me or comment here if facing some issues with understanding the topic.

Assigning Template To A particular Product Through hMC In HYBRIS

Step-1:  Create one Item template through hmc:

First, create a new item template in the hybris Management Console (hMC) as can be seen in the screenshot below.

create_template
Go to the Cockpit node, right-click the Item Template and select Create Item Template.

In the displayed editor you need to specify at least the following values:

      Code – The template’s unique code

     Type – The item type to which this template can be applied
add_classification
You can add some classification classes to your template. This is easily done by right clicking in the editor next to Classification Classes and selecting Add Classifying Category (see the screenshot below)

select_classification_categories

Select the classification classes you want to add and click the Use button.

Step-2:  Assigning A Template to an Item Through HMC.

Go to hMC–>catalogàproducts

Select the product you want to assign template.

Then there will be one place assigned  template.

assign_template_toproduct_hmc

Add your template over here and save it.

Thank you ,If you guys need any clarification on this then comment here I will definitely help you out.

MARKER INTERFACE

What is a Marker Interface?

Marker interface in Java is interfaces with no field or methods or in simple word empty interface in java is called marker interface.

What Is the Use?

It is used to convey to the JVM that the class implementing an interface of this category will have some special behavior. It means it will tell jvm that this class have some functionality or capable of doing something.

Simple Understanding:

I will give you a non-technical example.Two things are there, a MOBILE and a CAR. I am having one room. What ever i will put into that room it will wash it give me back. if i will put my car then it will wash it and give me back,but if i put my mobile then it will be washed and it will be damaged. So what exactly I am going to do here is I will tell to the room that which is washable and which is not.So in this case we go for a MARKER interface.

In technical words i will create one marker interface “Washable” and i will implement this interface in Car but not in Mobile, so before washing the room should check is it a instance of washable interface or not.So by this we can overcome our problem.

More On Marker Interface:

In java we have the following major marker interfaces as under:

  • Searilizable interface
  • Cloneable interface
  • Remote interface
  • ThreadSafe interface

The marker interface can be described as a design pattern which is used by many languages to provide run-time type information about the objects. The marker interface provides a way to associate metadata with the class where the language support is not available.

A normal interface specifies functionality which an implementing class must implement. But a marker interface does not follow that pattern. On the other side, the implementing class defines the behavior. There are some hybrid interfaces which act as a marker interface along with some methods. But this type of design is confusing if not handled carefully.

Usage of Marker Interface in java:

Marker interface in Java e.g. Serializable, Clonnable and Remote is used to indicate something to compiler or JVM that the class implementing any of these would have some special behavior. Hence, if the JVM sees a Class is implementing the Serializable interface it does some special operation on it and writes the state of the object into object stream. This object stream is then available to be read by another JVM. Similarly if JVM finds that a class is implementing Clonnable interface, it performs some special operation in order to support cloning. The same theory goes for RMI and Remote interface. This indication (to the JVM) can also be done using a boolean flag or a String variable inside the class.

Apart from using the built-in marker interface, to mark a class as serializable or clonnable, we can also have our own marker interface. Marker interface is a good way to logically segregate the code and also if we have our own tool to perform some preprocessing operation on the classes. It is very useful for developing frameworks or APIs e.g. struts or spring.

With the introduction of annotation in java 5, annotation has become a better choice over maker interface.

The Thread Safe interface is a marker interface which can be used to communicate to other developers that classes implementing this marker interface gives thread-safe guarantee and any modification should not violate that. Marker interface also helps code coverage or code review tool to find bugs based on specified behavior of marker interfaces. Here also annotations are better choice. @ThreadSafe looks lot better than implementing Thread Safe marker interface.

In the next part I will come up with real time example of marker interface to make you understand better.Follow for more topics.

Java-Marker-Interface

 

Comparable and Comparator continue…

N:B: If you have not gone through my previous post about comparator then once go through the link Comparable & Comparator: then it will be easy to understand this topic.

Comparator:

  • Java Comparator interfaceis used to order the objects of user-defined class.
  • It is basically used for custom sorting, that means you can sort employee objects by name by salary.
  • Comparator interface is in java.util package and its having one method compare() which takes two user defined objects as argument.

public int compare(Object obj1,Object obj2)

  • Compare() method compares first object data member with second object data member and its return type is “int”.
  • While shorting through Collections.sort() method we have to pass the Collection object as well as the Comparator object.
  • Let me discuss it though examples.

Employee.java:

package com.tcoj.model;

 

public class Employee {

int id;

public String name;

public double salary;

@Override

public String toString() {

 

return “[“+id+”,”+name+”,”+salary+”]”;

}

public Employee(int id, String name, double salary) {

super();

this.id = id;

this.name = name;

this.salary = salary;

}

}

  • Now Let me write two comparators one as NameComparator another SalaryComparator for two custom comparing.
  • The Idea here is to compare the Employee objects through different fields.

NameComparator.java:

import java.util.Comparator;

import com.tcoj.model.Employee;

 

public class NameComparator implements Comparator<Employee> {

 

@Override

public int compare(Employee emp1, Employee emp2) {

 

return emp1.name.compareTo(emp2.name);

}

 

}

 

SalaryComarator.java:

import java.util.Comparator;

 

import com.tcoj.model.Employee;

 

public class SalaryComparator implements Comparator<Employee> {

 

@Override

public int compare(Employee emp1, Employee emp2) {

double d=emp1.salary-emp2.salary;

if(d>0)

return 1;

else if(d < 0)

return -1;

else

return 0;

}

}

 

  • Now Let me write the client class where I will add some Employee objects and add them to list.
  • Then after passing two different Comparator objects we will see the sorting.

Client.java:

package com.tcoj.clint;

 

import java.util.ArrayList;

import java.util.Collections;

import java.util.Iterator;

import java.util.List;

 

import com.tcoj.comparators.NameComparator;

import com.tcoj.comparators.SalaryComparator;

import com.tcoj.model.Employee;

 

public class Clint {

 

public static void main(String[] args) {

List<Employee> list=new ArrayList<Employee>();

Employee emp1=new Employee(102, “shaswot”, 11500.89);

Employee emp2=new Employee(100, “subham”, 12500.89);

Employee emp3=new Employee(107, “ashok”, 11500.99);

Employee emp4=new Employee(108, “rahul”, 13501.09);

 

list.add(emp1);

list.add(emp2);

list.add(emp3);

list.add(emp4);

Collections.sort(list,new NameComparator()); //passing namecomparator object

Iterator<Employee> itr=list.iterator();

System.out.println(“Sort by name=============================”);

while(itr.hasNext())

{

System.out.println(itr.next());

}

System.out.println(“Sort by Salary===========================”);

Collections.sort(list,new SalaryComparator()); //passing salarycomparator object

Iterator<Employee> itr1=list.iterator();

while(itr1.hasNext())

{

System.out.println(itr1.next());

}

}

}

 

Output:

Sort by name=============================

[107,ashok,11500.99]

[108,rahul,13501.09]

[102,shaswot,11500.89]

[100,subham,12500.89]

Sort by Salary===========================

[102,shaswot,11500.89]

[107,ashok,11500.99]

[100,subham,12500.89]

[108,rahul,13501.09]

 

 

  • Here you can see when I am passing NameComparator it is sorting by name and same in SalaryComparator also.

If You guys have any doubt on this topic then feel free to comment and ask questions.

GARBAGE COLLECTION

  • Garbage Collection Is The Process Of Looking At Heap Memory, Identifying Which Objects Are In Use And Which Are Not, And Deleting The Unused Objects.
  • An unused object, or unreferenced object, is no longer referenced by any part of your program. So the memory used by an unreferenced object can be reclaimed.
  • In java the process of garbage collection is automated.

STEPS OF GARBAGE COLLECTION:

STEP-1 (MARKING)

The first step in the process is called marking. This is where the garbage collector identifies which pieces of memory are in use and which are not.

part1

STEP 2:( NORMAL DELETION)

Normal deletion removes unreferenced objects leaving referenced objects and pointers to free space.

part2

 

STEP-3: (COMPACTING)

To further improve performance, in addition to deleting unreferenced objects, you can also compact the remaining referenced objects. By moving referenced object together, this makes new memory allocation much easier and faster.

part3

 

GENERATIONAL GARBAGE COLLECTION:

The partition of objects into different generations (time intervals) based on time of allocation, and giving them different GC policies depending on age. Based on the heuristic that most objects are discarded shortly after being used–hence the GC is tuned to get rid of those first.

part4

Here is an example of such data. The Y axis shows the number of bytes allocated and the X access shows the number of bytes allocated over time.

As you can see, fewer and fewer objects remain allocated over time. In fact most objects have a very short life as shown by the higher values on the left side of the graph.

part5

The heap is broken up into smaller parts or generations. The heap parts are:   Young Generation, Old or Tenured Generation, and Permanent Generation.

Comparable & Comparator:

Comparable & Comparator:

  • Comparable and Comparator both are interfaces and are used to do sorting in Collection framework.
  • Comparable is used for single sorting where as Comparator is used or multiple sorting.
  • I will discuss comparable first and its uses with examples then after we will go for Comparator.

Comparable Interface in java:

  • It is used to order the objects of user defined classes in defined order.
  • Comparable interface is in java.lang package and its having one method compareTo(Object) which takes user defined object as argument.
  • It provides Single sorting only that means you can sort the elements on based on single data member only.
  • compareTo() method compares current object data member with the specified object. The return type of the method is int.
  • Based on the return value that is (+ve,0,-ve) collection will sort the objects.
  • Lets go for a sample program and i will explain step by step.
public class Employee implements Comparable<Employee>{

int id;

String name;

double salary;

 

@Override

public int compareTo(Employee emp) {

 

return this.id-emp.id;

}

 

}

 

  • Here I wrote a Employee class which implements Comparable interface and I overridden compareTo()
  • Here I compared on a data member called “id” and retrned difference of the two employee object ids.
  • Now i will write the clint class where i will insert employee objects into a collection type and will sort it.
package com.tcoj.clint;

 

import java.util.ArrayList;

import java.util.Collections;

import java.util.Iterator;

import java.util.List;

 

import com.tcoj.model.Employee;

 

public class Clint {

 

public static void main(String[] args) {

List<Employee> list=new ArrayList<Employee>();

Employee emp1=new Employee(102, “shaswot”, 11500.89);

Employee emp2=new Employee(100, “subham”, 12500.89);

Employee emp3=new Employee(107, “ashok”, 11500.99);

Employee emp4=new Employee(103, “rahul”, 13501.09);

 

list.add(emp1);

list.add(emp2);

list.add(emp3);

list.add(emp4);

Collections.sort(list);

Iterator<Employee> itr=list.iterator();

while(itr.hasNext())

{

System.out.println(itr.next());

}

}

 

}

 

  • Here I created one List and added 4 employee objects.
  • When i called Collections.sort() method it sorted according to the compareTo() method defined in Employee class.

OUTPUT:

 

[100,subham,12500.89]

[102,shaswot,11500.89]

[103,rahul,13501.09]

[107,ashok,11500.99]

It sorted according to id of Employee.You guys can try with name and salary.Let me know with comments if you guys having any doubt on this part.Next topic Comparators.

 

New Feature of Java 1.8

Want to share some new features of Java 1.8. It’s high time to look into these features.

 One of the new feature of Java 1.8 is from Java 8, interfaces are enhanced to have method with implementation. We can use default and static keyword to create interfaces with method implementation. 

We know that Java doesn’t provide multiple inheritance in  Classes because it leads to Diamond Problem. So how it will be handled with interfaces now, since interfaces are now similar to abstract classes. The solution is that compiler will throw exception in this scenario and we will have to provide implementation logic in the class implementing the interfaces.

I will discuss with examples for a better understanding in my next post. 


Java: The beginning

Just going to start with Java.this is the first blog.share your opinion.

Java is a programming language and computing platform first released by Sun Microsystems in 1995. There are lots of applications and websites that will not work unless you have Java installed, and more are created every day. Java is fast, secure, and reliable. From laptops to datacenters, game consoles to scientific supercomputers, cell phones to the Internet, Java is everywhere!

1) James GoslingMike Sheridan, and Patrick Naughton initiated the Java language project in June 1991. The small team of sun engineers called Green Team.

2) Originally designed for small, embedded systems in electronic appliances like set-top boxes.

3) Firstly, it was called “Greentalk” by James Gosling and file extension was .gt.

4) After that, it was called Oak and was developed as a part of the Green project.Latter in 1995 it named as “JAVA”.