MyArchiBook

Archive for the ‘Uncategorized’ Category

ES6 modules & classes with Webpack: quick setup & usage

with one comment

For setting up and using ES6 Modules with webpack perform the following steps:

  1. quicky setup js-sdk library using webpack
  2. Install babel-loader and its dependencies.
    npm install babel-loader babel-core babel-preset-env --save-dev
    Here babel-loader is the main package for invoking Babel transpilation. It requires babel-core for apis. babel-presets has the plugin for module-loading.
  3. Configure webpack.config.js
  4. vi index.js

    //Class definition
    export default class Imax {
      get movies() {
        return ['Logan', 'Beauty and the Beast'];
      }
      bookMovie(name) {
        return `Thank you for booking with Imax. Booking ID is ASDFFG|${name}`;
      }
    }
    
    module.exports = new Imax();
  5. webpack // build library
  6. webpack-dev-server  // host library. (hosted locally here)
  7. Client side:
    $.getScript('//localhost:8080/assets/imax.js').then(() => {
        let selectedMovie = Imax.movie[0];
        console.log('Movie list: ', Imax.bookMovie(selectedMovie));
     });
    
  8. O/P:
    Thank you for booking with Imax. Booking ID is ASDFFG|Logan

References:

Advertisements

Written by thangaveluaishwarya

March 24, 2017 at 11:17 AM

Autocomplete not working in Netbeans – 8.0.2

with 2 comments

  • Delete everything in the cache directory of netbeans
     rm -rf ~/.cache/netbeans/8.0.2/*
  • Restart Netbeans

Reference:
https://netbeans.org/bugzilla/show_bug.cgi?id=247026
https://forums.netbeans.org/topic61787.html

Written by thangaveluaishwarya

April 4, 2015 at 11:05 PM

Posted in Uncategorized

EJB class able to render REST WebService via Annotation

with 3 comments

Advancements in Java, especially the advent of JEE6 and JEE7  has eased the way we code.
Deployment descriptors for Servlet classes, Entity classes, Web Services are being replaced by annotations, thus advancing towards the concept of Convention over Configuration.

Below is a simple example on how to make a Session Bean class provide a RESTful WebService using annotations.

Here I have used JEE 7 and GlassFish 4 Application Server.
Note: As of now, JEE7 is  compatible only with GlassFish 4.

Here I have considered WebsterPublications example which has,

  •  DictionaryEntity – Entity class
  •  DictionaryResource – EJB+REST Service Provider Class

STEP 1: Create DictionaryEntity.java

@Entity
@Table(name = "WP_DICTIONARY")
public class DictionaryEntity {

    @Id
    @GeneratedValue
    private Long id;

    @NotNull(message = "Enter a word")
    private String word;

    @NotNull(message = "Enter the word's meaning")
    private String meaning;

    //Getters , Setters
}

Attributes can be annotated with constraints like @NotNull, @Pattern and can be made to through message during runtime, when violations occur.

STEP 2: Create DictionaryResource.java

@Path(value = "dictionary")
@Stateless
public class DictionaryResource {

    @PersistenceContext(unitName = "webster")
    private EntityManager em;

    @GET
    @Produces(MediaType.APPLICATION_JSON)
    @Path(value = "wordlist")
    public List<DictionaryEntity> getWordList(){
        Query query=em.createQuery("select d from DictionaryEntity d");
        List<DictionaryEntity> wordList=query.getResultList();
        return wordList;
    }
}

Here we can notice that this class apart from acting as a Stateless Bean also provides a RESTful WebService.
i.e., stateless session bean class providing the output object via REST WebService (Here JSON object got as Output).

Mapping the persistence unit and connection pooling in glassfish server is done (default Derby DB used).

STEP 3: Create WebsterRESTConfig.java

Since DictionaryResource acts like a Servlet by providing WebService, it’s essential to provide its entry in web.xml.
But this can also be done through annotation even.

WebsterRESTConfig class is created that extends Application and adds DictionaryResource as its resource class.Like DictionaryResource various other resource class can also be added.

@javax.ws.rs.ApplicationPath("webresources")
public class WebsterRESTConfig extends Application {

    @Override
    public Set<Class<?>> getClasses() {
        Set<Class<?>> resources = new java.util.HashSet<>();
        addRestResourceClasses(resources);
        return resources;
    }

   private void addRestResourceClasses(Set<Class<?>> resources) {
     resources.add(aish.vaishno.websterpublications.resource.DictionaryResource.class);
    }
}

Here @ApplicationPath is equivalent to the servlet-mapping URL-Pattern that we would specify in web.xml.

Also note, while instantiating Set<Class<?>> resources = new java.util.HashSet<>(); I haven’t used generics in HashSet<> but have just used a plane Diamond notation. This is one of the light-weight changes in Java SE 7.

Now when we give the path as:

http://localhost:8080/WebsterPublications/webresources/dictionary/wordlist
URL

The result is obtained as below.

Output

In my this post I wanted to highlight on the concept of Convention Over Configuration which is taking a great leap as the advancements in JEE versions are happening.Without even taking the pain to write the conventional web.xml deployment descriptor, we are now that able to  configure those settings by using simple annotations at the right place.

You can find the above source code in my GitHub repository. 

I wish you all a BLASTFUL 2014 🙂

Reflection – Dynamic Typing

with 4 comments

Today wanted to add some quick notes on my findings on Dynamic Languages.

To start with, lets look into Reflection.

REFLECTION” –  in common generic terms means a Mirror Image of something.

It carries the same meaning in Java. It means getting the mirror image of a class.
The microscopic details of a class can be explored during the runtime by specifying the class name dynamically. During this process the JVM interacts with the object of unknown type.

Let’s see an example,

public static void main(String[] args) {
	try {
		Class class1=Class.forName("aish.vaishno.musicstore.dao.MusicStoreDaoImpl");
		Package package1=class1.getPackage();
		System.out.println("Package Name with Version : "+package1);

		String canonicalName=class1.getCanonicalName();
		System.out.println("\nCanonical/Standard name of Field class: "+canonicalName);

		Method[] methods=class1.getMethods();
		System.out.println("\nMethods present in Field class:");
		for (int i = 0; i < methods.length; i++) {
			System.out.println(methods[i]);
		}

                Class[] classes=class1.getInterfaces();
                System.out.println("Interface implemented: ");
                for (int i = 0; i < classes.length; i++) {
                System.out.println(classes[i]);
                }
	} catch (ClassNotFoundException e) {
		e.printStackTrace();
	}
}

While specifying the className dynamically, the .class file of that particular class is opened and examined during the runtime .By Reflection, the package ,Method , Interface, modifier informations and etc.,can be retrieved.

But the demerits of this is ,

  • It slows down the performance
  • private fields and methods can be accessed.

Reflection concept is used in ,
Spring – while scanning through the packages.
ORM– when data from database is being mapped to the attributes in Entity Bean.
Likewise in Serialisation/Deserialisation, Remote Method Invocation and etc.,

Dynamic Language plays a major role in Rapid Application Development(RAD).

Groovy and Dynamic Language updates coming up.

 

Reference:

http://javarevisited.blogspot.com/2012/04/how-to-invoke-method-by-name-in-java.html

Written by thangaveluaishwarya

August 9, 2013 at 9:48 PM