Pages

Monday, December 28, 2015

Repairing a corrupt rar or zip file

There are times when you download a file and that seems to be broken or corrupt.

You get something like "CRC" error.

WinRar is a good tool.... unlike other zip-utilities it has an option (under tools menu) to repair an zip archive.

Try if it works for you!

My first hands at Sonatype Nexus



I am working with Sonatype Nexus (Nexus 2.10.0-02)currently. It appears to be a good repository manager providing facility to proxy a repository. 

For most of the things you would need Maven Central Repository which comes pre-added and once you have that, I think most common dependencies including spring and hibernate are taken care. I did not try spring-io yet.

So how to get going in quick steps with Sonatype Nexus Open Source version... 

1. Download the latest zip/tz file
2. Extract it where you want to have it installed.
3. Start it... mostly you would want to go to bin/jsw and then do a console-* command which is in your 32/64 windows directory.
4. Once you do that, you would see that it internally uses a jetty server (as visible from logs that show up on command prompt)
5. Check http://localhost:8081/nexus

You are done! This is where you would work with... on top-right corner you would see login option, you can login as 
"admin" 
default password "admin123"
to explore more options.

In your pom.xml file of maven projects give these tags...

 <repositories>
<repository>
<id>central</id>
<url>http://localhost:8081/nexus/content/repositories/central/</url>
<releases><enabled>true</enabled><updatePolicy>always</updatePolicy></releases>
</repository>
</repositories>

If there are any dependencies in your pom.xml, then this will try to resolve dependency by trying at this proxy location. If it finds it then no download, if it doesn't find then it will try to download the dependency.

Monday, December 7, 2015

Old Version



Did you ever want to get old version of any of software utilities....

My favorite is Microsoft Paint that came with XP.

Paint version in Vista and recent Windows OS is not that advanced.

Got it from http://www.oldversion.com/




Saturday, October 3, 2015

Tools for testing Restful Services

POSTMAN - this is a google chrome extension.

DHC rest. This is another chrome extension.

Both are good tools for testing your Restful Web Services. Check them up.!

Wednesday, September 16, 2015

Big Data, Data Science and Data Mining

I completed a course from Manipal Global on Hadoop recently. I had opted for an online-course and not instructor led and my experience has been good. The videos were able to cover the fundamentals and were able to build a base from where you can take forward yourself to being a hadoop developer. 
I am also registered to coursera for a specialization course.
I am also registered for a Data Mining course with TalentEdge affiliated to IITRoorkee. 

That is too much learning happening. Sad part is, Coursera wants you to be attentive through out the course, hardly you can skip a week, and should completely dedicated to it. So if you have other course in parallel and are also a working professional you would not be able to manage it all that great. I personally had to drop few course on coursera from in between half-ways. 

Data Science, Data mining and Big data are not completely the same thing.... I now have atleast some idea on Big data and exploring the rest two. I am now thinking to release my another course on Udemy based on my hadoop knowledge that I have gained.

Lessons from past one month

I came across Quora.com. This site is simply doing its job of answering questions. You have a question on any topic, just fire it up there in the right feed category and people will try to answer that. You get opinions and references for your any question on the subject.

I explored Google Analytics, Google Adwords and now going to explore Google AdSense. I did not master any of these but it has been a wonderful experience so far.

August 18th my course "Java Interview Guide" went live on Udemy. And since then I am busy promoting it. It is around a month now and all that I have is $18 earned so far. However, in trying to market my course I have learnt many other things that I would not learn being just a programmer. Words like SEO and Marketing are buzzing in my ears now. :P

Tuesday, August 18, 2015

Java Interview Guide

We know that Java interviews are hard nut to crack. If you know java but find it difficult to appreciate, or you always end up in surprises with core java questions or if you wonder how a java intrrview really looks like, then this course is for you.
Java Interview Guide : Cracking the First Round -video course, is to prepare you for the initial technical rounds for any Java Interview. This is not a Question-Answer guide or a learning material, rather it is a composition of questions and concepts from real interview experiences of the instructor. It is rather a real-walk through to how a typical java interview can be. If you are a person who wants to know how a Java interview looks like, then this video is for you.! It details questions about core java concepts, collections, exception handling, equals and hashcode() etc. and at last some focus on non-java part.

use coupon code -       FIXED10           to avail huge discount.

Monday, August 17, 2015

Canva - A Design Tool



One of the wonderful design tools.

Canva is a free graphic-design tool website. It has an easy to use drag-and-drop interface and provides access to over a million photographs, graphics, and fonts. It is used by non-designers as well as professionals.The tools can be used for both web and print media design and graphics.

https://www.canva.com/

Tuesday, August 4, 2015

Java Versions and Features

OVERVIEW OF VARIOUS JAVA VERSIONS AND THEIR FEATURES


Java SE 8
Java 8 was released on 18 March 2014. The code name culture is dropped with Java 8 and so no official code name going forward from Java 8.

New features in Java SE 8:
  • Lambda Expressions
  • Pipelines and Streams
  • Date and Time API
  • Default Methods
  • Type Annotations
  • Nashhorn JavaScript Engine
  • Concurrent Accumulators
  • Parallel operations
  • PermGen Error Removed
  • TLS SNI

Java Version SE 7
Code named Dolphin and released on July 28, 2011.

New features in Java SE 7:
  • Strings in switch Statement
  • Type Inference for Generic Instance Creation
  • Multiple Exception Handling
  • Support for Dynamic Languages
  • Try with Resources
  • Java nio Package
  • Binary Literals, underscore in literals
  • Diamond Syntax
  • Automatic null Handling

Java Version SE 6
Code named Mustang and released on December 11, 2006.

New features in Java SE 6:
  • Scripting Language Support
  • JDBC 4.0 API
  • Java Compiler API
  • Pluggable Annotations
  • Native PKI, Java GSS, Kerberos and LDAP support.
  • Integrated Web Services.
  • Lot more enhancements.

J2SE Version 5.0
Code named Tiger and released on September 30, 2004.

New features in J2SE 5.0:
  • Generics
  • Enhanced for Loop
  • Autoboxing/Unboxing
  • Typesafe Enums
  • Varargs
  • Static Import
  • Metadata (Annotations)
  • Instrumentation

J2SE Version 1.4
Code named Merlin and released on February 6, 2002 (first release under JCP).

New features in J2SE 1.4:
  • XML Processing
  • Java Print Service
  • Logging API
  • Java Web Start
  • JDBC 3.0 API
  • Assertions
  • Preferences API
  • Chained Exception
  • IPv6 Support
  • Regular Expressions
  • Image I/O API

J2SE Version 1.3
Code named Kestrel and released on May 8, 2000.

New features in J2SE 1.3:
  • Java Sound
  • Jar Indexing

J2SE Version 1.2
Code named Playground and released on December 8, 1998.

New features in J2SE 1.2:
  • Collections framework.
  • Java String memory map for constants.
  • Just In Time (JIT) compiler.
  • Jar Signer for signing Java ARchive (JAR) files.
  • Policy Tool for granting access to system resources.
  • Java Foundation Classes (JFC) which consists of Swing 1.0, Drag and Drop, and Java 2D class libraries.
  • Java Plug-in
  • Scrollable result sets, BLOB, CLOB, batch update, user-defined types in JDBC.
  • Audio support in Applets.

JDK Version 1.1
Released on February 19, 1997

New features in JDK 1.1:
  • JDBC (Java Database Connectivity)
  • Inner Classes
  • Java Beans
  • RMI (Remote Method Invocation)
  • Reflection (introspection only)

JDK Version 1.0
Codenamed Oak and released on January 23, 1996.



Thursday, July 23, 2015

Javascript and JSON Tools

cURL -



curl is an open source command line tool and library
 for transferring data with URL syntax, supporting DICT, FILE, FTP, FTPS, Gopher, HTTP, HTTPS, IMAP, IMAPS, LDAP, LDAPS, POP3, POP3S, RTMP, RTSP, SCP, SFTP, SMB, SMTP, SMTPS, Telnet and TFTP. curl supports SSL certificates, HTTP POST, HTTP PUT, FTP uploading, HTTP form based upload, proxies, HTTP/2, cookies, user+password authentication (Basic, Plain, Digest, CRAM-MD5, NTLM, Negotiate and Kerberos), file transfer resume, proxy tunneling and more.

http://curl.haxx.se/

Reading and writing JSON

 

 Source: http://www.mkyong.com/java/json-simple-example-read-and-write-json/

JSON.simple example – Read and write JSON

JSON.simple, is a simple Java library for JSON processing, read and write JSON data and full compliance with JSON specification (RFC4627).
Note
To convert object to / from JSON, you should consider Jackson or Gson.
In this tutorial, we show you how to use JSON.simple to read and write JSON data from / to a file.

1. JSON.simple Dependency

JSON.simple is available at Maven central repository, just declares following dependency in your pom.xml file.
  <dependency>
 <groupId>com.googlecode.json-simple</groupId>
 <artifactId>json-simple</artifactId>
 <version>1.1</version>
  </dependency>

2. Write JSON to file

In below example, it write JSON data via JSONObject and JSONArray, and save it into a file named “test.json“.
import java.io.FileWriter;
import java.io.IOException;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
 
public class JsonSimpleExample {
     public static void main(String[] args) {
 
 JSONObject obj = new JSONObject();
 obj.put("name", "mkyong.com");
 obj.put("age", new Integer(100));
 
 JSONArray list = new JSONArray();
 list.add("msg 1");
 list.add("msg 2");
 list.add("msg 3");
 
 obj.put("messages", list);
 
 try {
 
  FileWriter file = new FileWriter("c:\\test.json");
  file.write(obj.toJSONString());
  file.flush();
  file.close();
 
 } catch (IOException e) {
  e.printStackTrace();
 }
 
 System.out.print(obj);
 
     }
 
}
Output – See content of file named “test.json“.
{
 "age":100,
 "name":"mkyong.com",
 "messages":["msg 1","msg 2","msg 3"]
}

3. Read JSON from file

Use JSONParser to read above generated JSON file “test.json“, and display each of the values.
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.Iterator;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;
 
public class JsonSimpleExample {
     public static void main(String[] args) {
 
 JSONParser parser = new JSONParser();
 
 try {
 
  Object obj = parser.parse(new FileReader("c:\\test.json"));
 
  JSONObject jsonObject = (JSONObject) obj;
 
  String name = (String) jsonObject.get("name");
  System.out.println(name);
 
  long age = (Long) jsonObject.get("age");
  System.out.println(age);
 
  // loop array
  JSONArray msg = (JSONArray) jsonObject.get("messages");
  Iterator<String> iterator = msg.iterator();
  while (iterator.hasNext()) {
   System.out.println(iterator.next());
  }
 
 } catch (FileNotFoundException e) {
  e.printStackTrace();
 } catch (IOException e) {
  e.printStackTrace();
 } catch (ParseException e) {
  e.printStackTrace();
 }
 
     }
 
}
Output
mkyong.com
100
msg 1
msg 2
msg 3

References

  1. JSON.simple official website
  2. JSON.simple encoding JSON example
  3. JSON.simple decoding JSON example

Friday, July 3, 2015

Why dependency Injection ?



Source : http://www.javacreed.com/why-should-we-use-dependency-injection/

In this article we will explore some of the benefits of dependency injection and how this can be used in all types of programming, including small and simple programs. Some of the example shown below make use of Guice (Homepage), a dependency injection framework by Google. The same concept applies for any other dependency injection framework such as Spring (Homepage).
Please note that this article does not provide detailed description of Guice or any other dependency injection framework, but it only focus on the benefits of dependency injection and design methods that improve modularity, extendibility and testing. The main idea behind this article is to understand why dependency injection should be used rather than how to implement it.
All code listed below is available at: http://java-creed-examples.googlecode.com/svn/dependency-injection/WhyUseDependencyInjection/. Most of the examples will not contain the whole code and may omit fragments which are not relevant to the example being discussed. The readers can download or view all code from the above link.

What is dependency injection?

Let say we want to make some tea. How would we do it and what do we need? We need to boil some water, a clean mug, tea bags, sugar and milk (at least that’s what I put into mine). In order to make tea we need to provide all these ingredients and we need to manage all these ourselves. We have to fill the kettle with water and heat it up, get the milk from the refrigerator and get the rest.
Now assume that we can make tea, by simply asking someone else to do it for us. Someone that knows how to do the tea just the way we want it. Would that be better than making the tea ourselves? If yes, then welcome to dependency injection.
Dependency injection is a framework that takes care of creating objects for us without having to worry about providing the right ingredients so to say.

A Simple Example

Let say we have a class, Person, and this class needs to send a message. The Person class requires the aid of some other class, Email, in order to send a message. Following is a simple way of doing this.
package com.javacreed.examples.di.part1;

public class Email {
  public void sendEmail(String subject, String message){
    // Send the email
  }
}
package com.javacreed.examples.di.part1;

public class Person {
  private Email email = new Email();

  public void greetFriend(){
    email.sendEmail("Hello", "Hello my friend :)");
  }
}
We all agree that this is a very simple and straightforward example that involves two simple Java classes. Nevertheless, the above has some limitations as described below.
  • The Person class is dependent (has a strong/tight dependency) on the Email class. There is a hard connection between these two classes. Let say we have a new and better version of email class, FastEmail, in order for us to use it, we need to go in each and every class that depends on the Email class, such as the Person class, and replace it with the new version.
  • Let say we parameterise the Email‘s constructor. Again we have to go in each and every class that is initialising the Email class, such as the Person class, and change it.
  • A design decision is taken to make the Email class Singleton (Wiki). Similar to above we need to modify all instances where it is used.
  • In order to improve the notifications/messages system, we decide to add different message delivery systems such as SMS or tweets. The Person class and others like it, need to all be modified in order for it to use the new implementations.
  • Another developer needs to use the Person class, but would like to use a different notification/message system. This cannot be achieved with the current version of the Person class as it is hardwired to the Email class. What generally happens is that the other developer duplicates the Person class and modifies it as he/she needs. The projects ends up with two versions of the Person class.
  • In the above points we mentioned many scenarios where code has to be changed. All changes made, need to and should be tested. How can we test the Person class without including the message delivery class such as the Email? Testing, in many cases, is left as an afterthought. The way we have the Person class constructed makes it hard to test it without involving the Email class. Furthermore, how would we automate such test? How can we use JUnit (Homepage), or the like, to automate out tests?
  • Moving forward in the project, the Person class starts to depend on another class that allow this object to write a letter using the Pen class for example. The Person class can use other ways to write a letter, such as Pencil class or Typewriter class, but this approach does not allow that.
These limitations can be improved by changing the way we think and refactor our code in a modular way. This is independent from dependency injection and the dependency injection framework as we will see in the following section.

Change the way we think

The Email class provides a service, that is, sending of messages over the Internet using the mail protocol. Instead of having the Person class initialising an instance of the Email class, we first create an interface, MessageService, and make the Person class using this interface instead. This removes the dependency that the Person class has on the Email and replaces it with an abstract message delivery interface.
The following three steps: define, implement and use, show how we can develop modular and extendable code. This approach also improves testing as we will see at the end of this article.
  1. Define Interfaces Many developers do not use interfaces as they see them as additional, non-required, code. This may be true (I said maybe as the System class makes use of interfaces) for the famous hello world (example) program, definitely not true for the rest. Like with everything else, we have to see things in context and there will be cases when we can do without interfaces. Nevertheless, developing code using interfaces produce far more modular and extendable code as illustrated in the following examples. The example discussed in the previous section was quite simple, but it included several pitfalls which can be easily avoided by using interfaces instead of concrete classes to define services. Changing code at a later stage involves more work than having it right in the first place.
    We start by defining the MessageService interface that includes one method, sendMessage(String subject, String message). For simplicity we assume that no exceptions are thrown.
    package com.javacreed.examples.di.part2;
    
    public interface MessageService {
      void sendMessage(String subject, String message);
    }
    
  2. Implement Interfaces In the list of limitations we mentioned four possible methods of sending a message: email, fast email, SMS and tweet. Let’s create four classes that handle each message delivery method and have all these classes implement the interface created above.
    package com.javacreed.examples.di.part2;
    
    public class EmailService implements MessageService {
      @Override
      public void sendMessage(String subject, String message){
        System.out.printf("Email: %s, %s%n", subject, message);
      }
    }
    
    package com.javacreed.examples.di.part2;
    
    public class FastEmailService implements MessageService {
      @Override
      public void sendMessage(String subject, String message){
        System.out.printf("Fast Email: %s, %s%n", subject, message);
      }
    }
    
    package com.javacreed.examples.di.part2;
    
    public class SmsService implements MessageService {
      @Override
      public void sendMessage(String subject, String message){
        System.out.printf("SMS: %s, %s%n", subject, message);
      }
    }
    
    package com.javacreed.examples.di.part2;
    
    public class TweetService implements MessageService {
      @Override
      public void sendMessage(String subject, String message){
        System.out.printf("Tweet: %s, %s%n", subject, message);
      }
    }
    
  3. Use Interfaces Finally, instead of using classes, we use interfaces. In the Person class, we replace the Email field with the MessageService service interface as highlighted below.
    public class Person {
      private MessageService messageService;
    
      public Person(MessageService messageService){
        this.messageService = messageService;
      }
    
      public void greetFriend(){
        messageService.sendMessage("Hello", "Hello my friend :)");
      }
    }
    
    Note that the Person class is not initialising the message service but it is expecting it as a parameter of its constructor. This is a key element in the design. It improves modularity, extendibility and testing. The Person class is not dependent on any implementation, but on a service defined by an interface. This means that we can use the Person class without having to worry about the underlying implementation of the message service. Furthermore, different Person instances can be instantiated using different message services.
One can argue that the new version of Person class became more complex to instantiate as it requires parameters. This is a fair statement and here is when dependency injection comes into play.

Using Dependency Injection

As mentioned in the introduction, dependency injection can help us initialising objects and provide these objects all the necessary resources (ingredients). For example, the Person class requires an instance of MessageService. The dependency injection framework will provide that for us. So to create an instance of Person class, all we need to do is call something like:
dependecyFramework.getInstance(Person.class)
Magically, (not really), the dependency injection framework will create an instance of the Person class and provide an instance of the MessageService to the Person object.
The next natural question will be, how does the dependency injection framework knows how to initialise the MessageService? We need to tell the dependency injection framework how to create an instance of MessageService. With Guice we do that by creating a module (a class that extends AbstractModule class) as illustrated below.
package com.javacreed.examples.di.part2;

import com.google.inject.AbstractModule;

public class ProjectModule extends AbstractModule {

  @Override
  protected void configure() {
    bind(MessageService.class).to(EmailService.class);
  }
}
Here we are telling the dependency injection framework (Guice) how to create an instance of the MessageService class. We also need to add an annotation to the Person class in order to allow the dependency injection framework to inject the necessary parameters.
package com.javacreed.examples.di.part2;

import com.google.inject.Inject;

public class Person {
  private MessageService messageService;

  @Inject
  public Person(MessageService messageService){
    this.messageService = messageService;
  }

  public void greetFriend(){
    messageService.sendMessage("Hello", "Hello my friend :)");
  }
}
Note that the same concept can be applies to Spring or other dependency injection frameworks. Each dependency injection framework has its way of configuration and the above only applies to Guice. Spring and the others dependencies injection frameworks make use of different configuration methods.
With everything set, we create an instance of the Person class using the dependency injection framework.
package com.javacreed.examples.di.part2;

import com.google.inject.Guice;
import com.google.inject.Injector;

public class Main {
  public static void main(String[] args) {
    Injector injector = Guice.createInjector(new ProjectModule());
    Person person = injector.getInstance(Person.class);
    person.greetFriend();
  }
}
We replaced a couple of lines of code with many others. What’s the buzz about this? In the next section we will see some of the benefits of dependency injection and how this can be used to simplify our coding life.

Benefits of Dependency Injection

In this section we will see some key benefits of dependency injection
  • Changing the message service Let’s change the delivery method from email to SMS. How would we do that?
    We only need to change the ProjectModule class to map the MessageService class to the SmsService class as highlighted in the following example.
    package com.javacreed.examples.di.part3;
    
    import com.google.inject.AbstractModule;
    
    public class ProjectModule extends AbstractModule {
    
      @Override
      protected void configure() {
        bind(MessageService.class).to(SmsService.class);
      }
    }
    
    This one change will affect all classes initialised with the dependency injection framework without having to change any of these classes. This leads us to another advantage: testing.
  • Testing and Mocking the message service We can create a MockMessageService class which can be using in JUnit test case as shown next.
    package com.javacreed.examples.di.part3;
    
    public class MockMessageService implements MessageService {
    
      public String subject;
      public String message;
    
      @Override
      public void sendMessage(String subject, String message) {
        this.subject = subject;
        this.message = message;
      }
    }
    
    The above mock message service simply stores the parameters into two public fields. These fields can be used to retrieve the values of the parameters and used for testing ass illustrated next.
    package com.javacreed.examples.di.part3;
    
    import org.junit.Assert;
    import org.junit.Before;
    import org.junit.Test;
    
    import com.google.inject.AbstractModule;
    import com.google.inject.Guice;
    import com.google.inject.Injector;
    import com.google.inject.Singleton;
    
    public class TestPerson {
    
      private Injector injector;
    
      @Before
      public void init() {
        injector = Guice.createInjector(new AbstractModule() {
          @Override
          protected void configure() {
            bind(MessageService.class).to(MockService.class);
          }
        });
      }
    
      @Test
      public void testGreetFriend() {
        Person person = injector.getInstance(Person.class);
        person.greetFriend();
    
        MockService mockService = injector.getInstance(MockService.class);
        Assert.assertEquals("Greet", mockService.subject);
        Assert.assertEquals("Hello my friend", mockService.message);
      }
    }
    
    This may require some explanation. So here we go. In the init() method we created a dependency injection context just for this testing and provided a custom module (as an inner anonymous class). The custom module wires the MessageService with the MockMessageService instead. After the greetFriend() method is invoked, we ascertain that the correct parameters are being passed to the message service instance.
    This design setup allows us to test the Person class independent from the other classes that it depends on in an automated manner.
  • Changing the signature of the Person constructor As we mentioned in the list of limitations, the Person class may evolve and include more functionality. Changing the signature of the Person constructor will not affect us as long as the injector knows how to provide the required parameters.
    package com.javacreed.examples.di.part3;
    
    import com.google.inject.Inject;
    
    public class Person {
    
      private final MessageService messageService;
      private final WritingService writingService;
    
      @Inject
      private Person(final MessageService messageService, WritingService writeService) {
        this.messageService = messageService;
        this.writingService = writeService;
      }
    
      public void greetFriend() {
        messageService.sendMessage("Hello", "Hello my friend :)");
      }
      
      public void writeToFriend() {
        writingService.write("Hello my friend :)");
      }
    }
    
    The Person class will still be initialised in the same way as it is now. Thus changing the Person constructor signature will not affect the other classes that make us of it.
    Person person = injector.getInstance(Person.class);
    
    The same applies for anything that is initialised and handled through the dependency injection framework.
  • Passing the Injector as parameter In a project we can have one instance shared throughout the project. This can be achieved by passing the Injector as a parameter to other objects that need it. We setup the Injector at the beginning (in a main() method for example), and then have it set as a constructor parameter in all the classes that require an instance of the injector. Like that, one injector will server all classes in the project.

Conclusion

This ended up to be quite a long article. Dependency injection is quite simple to use and it has quite a “shallow” learning curve. This article did not explain how to use the actual dependency injection framework (such as Guice or Spring). Articles about these are easily found. This article described key benefits of using a dependency injection framework, even in small projects.

Thursday, July 2, 2015

CCDH CCD - 410

I gathered some preparation map or material for CCDH -(Cloudera Certified Developer for Apache Hadoop )

  • http://www.cloudera.com/content/cloudera/en/training/certification/ccdh.html

  • Study Guide / Objectives   - http://www.cloudera.com/content/cloudera/en/training/certification/ccdh/prep.html


Preparation may require:
  • Installation of Cloudera and Hortonworks VMs
  • Book - Hadoop The Definitive Guide - by Tom White
  • I am planning to do hadoop course from following
http://intellipaat.com/
manipalglobal.com/