Skip to main content

Posts

Showing posts from March, 2017

Angular : How to Inject Nested Service in Component

How to Inject Nested Services in Component Let's create a service url.service.ts that will will be injected in other services. import { Injectable } from '@angular/core' ; @Injectable () export class UrlService{ // business logic } Now lets create another service http.service.ts that will Inject this service. import { UrlService } from './url.service.ts' ; export class HttpService extends UrlService{ // business logic } Now, we need to inject the HttpService in some component to make http calls. So now we can inject the HttpService in component as shown below. import { Component ,OnInit } from '@angular/core' ; import { UrlService } from './url.service.ts' ; import { HttpService } from './http.service.ts' ; @Component ({ selector : 'some-selector' , templateUrl : 'some.html' , providers: [HttpService, UrlService] }) export class SomeComponent implements OnInit{ // busin

Angular : Getting started with Angular with Angular CLI, Installation and Hello World Example

Install and Setup Angular Before we start, lets install the angular cli with the following command npm install -g @angular/cli Angular CLI is a tool that can make a new project, compile your typescript files, configure your typescript compiler run your code with live preview, build and package your project. Note Make sure you have latest node and npm installed, at least node version 6.9.x and npm version 3.x.x . To see the versions you can run commands node -v and npm -v for node and npm versions respectively. Warning While installation you might get the following warning npm WARN optional SKIPPING OPTIONAL DEPENDENCY: fsevents@^1.0.0 (node_modules/@angular/cli/node_modules/chokidar/node_modules/fsevents): npm WARN notsup SKIPPING OPTIONAL DEPENDENCY: Unsupported platform for fsevents@1.1.1: wanted {"os":"darwin","arch":"any"} (current: {"os":"linux","arch":"x64"}) You can sk

Node : How to uninstall a nodejs global dependency

Uninstall global node dependency To uninstall a node dependency installed with -g command you can use the following command. npm uninstall -g <dependency> The above command will remove that dependency from your system. Uninstall node dependency To remove a dependency from your project you can use the following command. npm uninstall <dependency> This will remove the dependency and remove its entry from the package.json file.

Node : How to Upgrade NodeJs and Npm and How to install latest NodeJs on Linux, Ubuntu

Points to Remember If you are installing node for the first time then, use the NodeSource PPA and first choose the version of node you want to install. Following are the sources for the node versions for Node.js v4 curl -sL https://deb.nodesource.com/setup_4.x | sudo -E bash - for Node.js v5 curl -sL https://deb.nodesource.com/setup_5.x | sudo -E bash - for Node.js v6 curl -sL https://deb.nodesource.com/setup_6.x | sudo -E bash - for Node.js v7 curl -sL https://deb.nodesource.com/setup_7.x | sudo -E bash - Once this is done, then update your repositories by command sudo apt-get update Now, finally install NodeJs and Npm using the following command sudo apt-get install nodejs sudo apt-get install npm To check the version of node, try command node -v and npm version by command npm -v Upgrading NodeJs and Npm If you already have node and npm install and you want to upgrade the version then you should first run the following command to remove them. sudo apt-get purge n

Groovy : What is Identity Operator and difference between == in Java and Groovy

What is Identity == operator In Java, == operator is used for checking the reference of objects. In Groovy, it is used to check if the object are equals, i.e it is the implementation of equals() method in java. == operator in In java, checks the reference of the objects. In groovy, == is equivalent to .equals() and checks if the two objects are equal is operator In java, does not exist In groovy, checks the reference of the objects which is == in java equals() method In java, checks if the two objects are equal In groovy, checks if the two objects are equal Let's have a look at the following examples class A { String name; boolean equals(A a){ this .name == a.name } } A a1 = new A( name : 'ekiras' ) A a2 = new A( name : 'ekiras' ) println (a1 == a2) println (a1.equals(a2)) The above output will give the following output. Output true true

Groovy : What is Coercion operator and how to use it

Point to Remember Coercion Operator is used for casting Custom Coercion rules can be applied to a class by defining asType method in a class. Syntax of coercion operator is as What is Coercion Operator Coercion operator is used to do type casting of objects in groovy. It converts objects of one type to another type. Lets take a look at the following examples int a = 123 String s = (String) a The the above example, the assignment of a to s will give a ClassCastException in java but when you run the same in groovy it will not give an error since Groovy as defined how to typecast integer to string. Coercion version of the above example is as follows int a = 123 String s = a as String How to Define Custom Coercion Rules. Let's take an example where we want to convert an object of class A to class B. class A { String name } class B { String name String email def asType(Class target){ if (target == A) return new A( name : this .name

Groovy : What is Direct field access operator and how to use it

Direct field access operator In groovy when we access the property of any object as object.property then groovy under the hood calls the getter method of the property even if we have not specified it. The default getter and setter methods return the property and set the property respectively. Let's have a look at the example. ​ class User { String name String email String getName(){ return "Name is $name" } } User u = new User( name : 'ekiras' , email : 'ekansh@ekiras.com' ) println u.name When you execute the above code you will get the output Name is ekiras . Note What happens under the hood is u.name will internally call the getter method of instance variable name . Here calling u.getName()' is equivalent to calling u.name Also, all the classes and their instance variables are by default public in nature. So you can access the instance variable without calling the getter method byb using the Direct Field Access Operator S

Groovy : What is Elvis Operator and difference b/w Elvis and Ternary operator

Ternary Operator Ternary Operator is the short hand of the if-else block , where a condition is evaluated to true or false and following statement is executed. Syntax of the ternary operator is below < boolean expression> ? <executed when condition is true > : <executed when condition is false > A typical example of if-else block is shown below where we check if a variable is null or not. String s = "hello" if ( s != null ) println s else println "not found" We can replace the above code with the ternary operator as shown below String s = "hello" println s != null ? s : "not found" Here, the variable s is assigned a string hello and condition s!=null evaluates to true hence the statement after ? is executed which returns s itself. Elvis Operator Elvis operator is the short hand of the Ternary operator explained above. In this operator we do not specify the true condition. The true condition is will

Groovy : What is Safe Navigation operator or Null check Operator

Safe navigation operator The safe navigation Operator is used to avoid null pointer exceptions. Many a times you come across the need to access the value of an object if it is not null. Syntax for the Safe Navigation Operator is <object>?.<property> Here the could be any object and could be any property of the object. Let's have a look at the below example, class User { String name; } User u = new User(name : "ekiras" ) if (u != null ) println u.name // prints ekiras In the above code we have to write the if block to check if the user object is null or not. So, with the Safe navigation operator we do not need to write the if-else blocks. We can rewrite the above code as shown below class User { String name; } User u = new User( name : "ekiras" ) println u?.name // prints ekiras

Gradle : How to upload the Jar or War file to custom path in local machine

Points to Remember Default location for gradle dependencies is ~/.gradle or /home/{user}/.gradle . We can specify the location from where to gradle should find dependencies and where it should upload the created artifacts. How to upload an Artifact to a custom location in file system buildscript { repositories { maven { url uri( "$buildDir/repo" ) } } } group 'com.ekiras' version '1.0-SNAPSHOT' apply plugin: 'groovy' apply plugin: 'maven' repositories { mavenCentral() } dependencies { // dependencies } uploadArchives { repositories { mavenDeployer { repository(url: uri( "$buildDir/repo" )) } } } In the above example, uploadArchives { repositories { mavenDeployer { repository(url: uri( "$buildDir/repo" )) } } } This tells gradle that it will upload the created artifact (jar or war ) to

Gradle : How to make a custom War file

Points to Remember War task extends Jar You can create war files with any configuration defined in configurations { } closure You can also add files to an existing war file. You can select the files that needs to be included or excluded while creating a war file How to create a War file in Gradle To create a war file you have to create a task of type War as shown below // include java plugin apply plugin : 'java' task createWar(type : War){ destinationDir = file ( "$buildDir" ) baseName = "my-war" version = "1.1" caseSensitive = true classifier = "SNAPSHOT" from "src" } Run the above task with command gradle -q createWar , this will create a war file named my-war-1.1-SNAPSHOT.war in the build folder. See Full Documentation of War Task How to create a War file and exclude some files Now if we want to exclude some files from a war file we can use the exclude method which takes