Saturday, 28 July 2018

IntelliJ Shortcut

in construction ...

Navigation:
Recent Files: Ctrl + e from the popup to select recently opened files.
Go to a line: Ctrl + g
From variable to its declaration: F4
From an error to the next error: F2
Go from the interface to its implementation:  Ctrl+alt+b
Moving back to the previous edited location:  Ctrl+alt+backspace
Switching between Tabs: Alt + left arrow, or right arrow
Go to a class: Ctrl+n
Go to a file: Ctrl+shift+n
Type structure: Ctrl + h
File structure(methods and fields alone): Ctrl +F12

Search:
search anywhere:  double shift.
find a class: Ctrl+n   fx: UR:14 directly go to class line 14
find in a path: Ctrl+shift+f
find a file; Ctrl + shift + n

Usage
High-lightening usages in the file: Ctrl+Alt+F7
Comparing any two files: in project select both files and Ctrl + d

Class
Definition of class: Ctrl+shift+I
Show class hierarchy: put the cursor on a class name, and press Ctrl + h
Show call hierarchy: Ctrl + ALT + h

Imports:
Optimizing imports:  Ctrl + alt + o

Disable import wildcard:  settings (Ctrl + alt + s); editor; code style; Java; Imports; field: enable single class import; change number at : "class count to use import ...  " and "name count to use static ... "

Debug:
showing all breaks: Ctrl + shift + f8
toggle a breaking point: Ctrl +f8
evaluation: shift + f8

Refactor:
Abstracting statements into a method: Ctrl+alt+m
Rename: alt+f6

Editor:
Reformatting: Ctrl+alt+L
Switching between method and its invocation: Ctrl+b
Go to implementation from interface: Ctrl + alt + b
Close a Tab: Ctrl+ F4
Choose a content to paste: Ctrl+shift+v
Surround code fragments: ctrl+alt+t

Unit Test:
create a unit test for the current class: Ctrl+shift+T

Generate Code:
insert new code:  alt + insert
inherit from interface: Ctrl+i

Version Control
VCS tool window: Alt +9
put file into VCS: Ctrl+alt+a

Monday, 11 June 2018

Overview of OpenShift

Overview of Openshift


Openshift is a cloud platform as a service hosted by Redhat, similar to Heroku. However, Heroku is a public and commercial cloud, but Openshift is an open source, allowing to build of a private cloud.

Openshift has Kubernetes inside, automating orchestration, scaling, and application management.
Openshift has dockers inside, offering virtual containers.

Openshift may help developers and operators to build, test, deploy and run applications in a containerized cloud environment.

Openshift offers access via both web consoles and CLI. You need to install openshift CLI locally before operating openshift locally.

Basic Openshift CLI Commands:

oc command follows a generic format, i.e. oc action object-type object-id-or-name
action: get, describe, new_app; login, project, and status etc
object type: to perform action on a type of object, fx: service (svc).
object-id-or-name: fx: a specific service

oc login: login remote openshift cloud.
oc login -u =   -p = : login with user-name and password
oc logout: logout remote openshift cloud.

oc project: showing which project you are in right now.
oc projects: showing all projects.
oc project : switch to a specific project

oc status: showing status at current project.
oc status -v: in a verbose way.

oc get svc : meaning that, get services. it will show all services offered by this project.
oc describe svc : meaning that, return details of services.
oc describe svc pds-test : meaning that returning a specific service in details.

The following URL describes OpenShift Command in Details


Openshift Terms

It needs to know some basic terms as talking about the Openshift.

Image: Packaged together including Application code, dependencies and any supporting system library, and it is identified by a name that can be local to the current cluster or point to a remote docker registry. 

Pod: a set of containers physically running on the same Node and sharing the same IP. 

Route: A route is an external DNS entry(either a top-level domain or a dynamically allocated name), created to point to a service. So it can be accessed from the external cluster. 

Deployment Configuration[DC]: it defines the template for a pod and manages to deploy new images or configuration changes. 

Build: a build running in a container. it converts source code into an image and pushes it into the docker registry.  Post-build test: it doesn't push the image.  Tag: each image may have tags. 

Secrete: 

Source to Image.

Source-To-Image (S2I) is a toolkit and a workflow for building reproducible docker images from source code.

How to make the source into an image?  simply creating a new application and associating it to the git repository.  Use a command, oc new_app, to create a new application, and link it to a git repository.

$ oc new-app jboss-eap70-openshift~https://github.com/PROJECT.git --name=APPLICATION_NAME

How to link bitbucket with Openshift by SSH key?


In windows OS, using Git-bash or Cygwin may generate private and public pairs (ref. to SSH key generation)




How to specify source code to Jenkins


Some terms you may know 


Sunday, 20 May 2018

Handle Exception in SpringBoot

In SpringBoot, how to handle exceptions?

Before 3.2,


1. The controller level @ExceptionHandler

drawback: it is defined within a controller, and it works only for that controller.

2. The HandlerExceptionResolver:

It offers a uniform exception handler, which is enabled by default Dispatch servlet.

@ExceptionHandler: since Spring 3.1
@ResponseStatus: since Spring 3.0, it is a ResponseStatusExceptionResolver, mapping an Exception to a HttpStatus code.

drawback: 1) it maps an exception to an HTTP status code in the response but without a body massage. 2) one ResponseStatus is able to handle only one Exception.

3. The new one:  @ControllerAdvice
Since Spring 3.2 above, it allows scattered handlers resided in one handler, and it allows to customize response body.


Reference:
Error Handling for REST with Spring


Saturday, 19 May 2018

Spring Data Rest

Spring-Data-Rest builds on top of Spring-Data-Repository. it may automatically analyze underlying data model, and bring out a REST API layer in a hypermedia format.  It, implicitly, merged Rest-API within the repository layer. By this way, it removes controller and service layer.

In Spring boot, it is quite simple to initiate a Spring-Data-Rest layer by adding a starter, i.e.
 org.springframework.boot
 spring-boot-starter-data-rest

Certainly, it also needs to include following for repository objects, i.e.
 org.springframework.boot
 spring-boot-starter-data-jpa
 


 
 

Wednesday, 9 May 2018

Checked and Unchecked Exception

Java exceptions are categorized into 3 groups.

(1) checked exception: those are verified during compile time.
(2) unchecked exception: those are not verified during compile time.
(3) error (unchecked)

(1): A Checked exception can be predicted by the programmer.
(2): A Unchecked exception is a runtime exception. it may not be predicted as coding it, fx: index out of bound etc.
(3): Errors made by JVM, for instance, run out of memory etc.

An exception extends a Throwable interface.  All exceptions are throwables.
(1): extends from Exception extends Throwable;  fx:  IOException
(2): extends from RuntimeException extends Throwable; for instance: ArithmeticException
(3): extends from Error extends Throwable

(1) must be handled by a developer by try catch finally, or throws to a caller.
(2) not an error; a developer may handle it or not; whatever it will be handled by the system.
(3) Errors come from the system; they cannot be handled.


Rule in inheritance

class A{
doIt(){ };  //overriden method
}

class B extends A{
@override
doIt() throws RuntimeException{ //overriding method
}
}

In general,  an overriding method cannot throw an exception more generic than the overridden method.  The case above is valid,  only because a RuntimeException is an unchecked exception, so the overriding is valid.

Sunday, 6 May 2018

How to print 1 to 100 without using loop

recursion call works the same as a loop.

/**
 *
 * @author YNZ
 */
public class PrintOneToNWithoutLoop {

    public static void main(String[] args) {
        printN(1);
        System.out.println("");
    }

    public static void printN(int n) {
        if (n <= 100) {
            System.out.print(" " + n);
            n++;
            printN(n);
        }

    }

}



Thursday, 3 May 2018

Using Lombok to create Builder Pattern

Applying the builder pattern to a data model is a trivial task.  Now it is able to use Lombok annotation to auto-generate it.




import lombok.Getter;
import lombok.Setter;
import lombok.NoArgsConstructor;
import java.io.Serializable;
import java.time.ZonedDateTime;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.OneToOne;
import javax.persistence.Table;
import javax.validation.constraints.NotNull;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.EqualsAndHashCode;
import lombok.ToString;
import org.springframework.format.annotation.DateTimeFormat;

/**
 *
 * @author YNZ
 */
@Entity
@Table(name = "SHARE_ORDER")
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@Builder
@ToString
@EqualsAndHashCode
public class Order implements Serializable {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "ORDER_PK")
    protected long id;

    @OneToOne
    @JoinColumn(name = "LISTER_PK_FK", referencedColumnName = "USER_PK")
    @NotNull
    protected Lister lister;

    @OneToOne
    @JoinColumn(name = "SEARCHER_PK_FK", referencedColumnName = "USER_PK")
    protected Searcher searcher;

    @Column(name = "CREATION_TIME")
    @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME)
    @Builder.Default
    protected ZonedDateTime createdDateTime = ZonedDateTime.now();

    @Column(name = "ACTIVATED")
    protected Boolean activated;

    @Column(name = "ORDER_START_TIME")
    @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME)
    protected ZonedDateTime startTime;

    @Column(name = "ORDER_END_TIME")
    @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME)
    protected ZonedDateTime endTime;

}



Can Jackson Deserialize Java Time ZonedDateTime

Yes, but must include JSR310. Thus ZonedDateTime can be deserialized directly from JSON response to POJO field. <dependency> <g...