Friday, May 11, 2012

What does the Spring framework do? Should I use it? Why or why not?

What does the Spring framework do? Should I use it? Why or why not?
Spring is a framework that helps you to "wire" different components together. It is most useful in cases where you have a lot of components and you might decide to combine them in different ways, or wish to make it easy to swap out one component for another depending on different settings or environments.
This is what I've always understood "dependency injection" to be.
I would suggest a different definition:
"Dependency injection is where you design your classes so that they expect an outside actor to directly give them the sub-tools they need to do their jobs before anybody starts asking them to do something."
Most of the XML (or annotation-based) stuff is telling spring:
  • When someone asks for a "HammerStore", I want you to create a example.HammerStore and return it result. Cache the result, since there is only one store.
  • When someone asks for a "SomeHammer", I want you to find a "HammerStore" and return the result of its getHammer() method. Do not cache the result, but make a new hammer each time.
  • When someone asks for a "SomeWrench", I want you to create a example.WrenchImpl and take the configuration setting guage and put it into the setWrenchSize() property. Do not cache the result, but create a new one each time.
  • When someone asks for a "OurPlumber", I want to you create an instance of example.PlumberImpl. Put the string "Pedro" into its setName() method, put a "SomeHammer" into its setHammer() method, and put a "SomeHammer" into its setWrench() method. Return the result, but do cache it, since we only have one Pedro.
In this way, Spring lets your connect components, label them, control their lifecycle/caching, and alter behavior based on configuration.
To facilitate [testing] I typically make (at least) two versions of a method : one that uses instance variables, and one that only uses variables that are passed in to the method.
That sounds like a lot of overhead for not a lot of benefit for me. I would suggest that you make your instance variables protected or package-level, and then locate the unit tests inside the same package. That way you can alter or test the instance variables in-place.

http://programmers.stackexchange.com/questions/92393/what-does-the-spring-framework-do-should-i-use-it-why-or-why-not

Why should you use an ORM?

Making data access more abstract and portable. ORM implementation classes know how to write vendor-specific SQL, so you don't have to.

http://stackoverflow.com/questions/448684/why-should-you-use-an-orm

Why do we use hibernate annotation?

It is not something important as in "mandatory". It's a different possibility, with strength and weaknesses.
Advantages:
  • Compile-time checking : writing in Java (instead of Xml) is very user-friendly in the IDE nowadays. No more typos discovered when starting your application (incremental compilation), not that much to remember (completion)...
  • Localized with the code (class level) : instead of having to open two files (java and xml) to get the full story, with one annotated java file, you open only one file. This is less repetitive, faster in the long run.
  • Localized with the code (method or field level) : because the annotation go on a method (or field), there is no need to specify the method it belongs to. That redondant information is not given, which is shorter, and always coherent (even after a code refactoring for example). Maintenance is so much faster.
  • Tools (javadoc, other tools using reflection) can use the annotations for some other requirements.
  • Annotations are newer than the xml, the team used the input they had received at the time to provide better default values. Xml has some, but can't change much for compatibility reasons. Often, with the annotations technology, you write no annotation at all, and it works. Imagine the time-saving, especially during development.
http://stackoverflow.com/questions/1367187/why-do-we-use-hibernate-annotation

Thursday, May 10, 2012

Authentication and Authorization

Authentication
An authentication system is how you identify yourself to the computer. The goal behind an authentication system is to verify that the user is actually who they say they are.
There are many ways of authenticating a user. Any combination of the following are good examples.

Password based authentication
Requires the user to know some predetermined quantity (their password).


  • Advantages: Easy to impliemnt, requires no special equipemnt.
  • Disadvantages: Easy to forget password. User can tell another user their password. Password can be written down. Password can be reused.
  • Device based authentication
    Requires the user to posses some item such as a key, mag strip, card, s/key device, etc.


  • Advantages: Difficult to copy. Cannot forget password. If used with a PIN is near useless if stolen.
  • Disadvantages: Must have device to use service so the user might forget it at home. Easy target for theft. Still doesn't actually actively identify the user.

  • Biometric Authentication


  • My voice is my passport. Verify me. This is from the movie sneakers and demonstrates one type of biometric authentication device. It identifies some physical charactistic of the user that cannot be seperated from their body.
    Retina Scanners:


  • Advantages: Accurately identifies the user when it works.
  • Disadvantages: New technology that is still evolving. Not perfect yet.

  • Hand Scanners:


  • Advantages: Difficult to seperate from the user. Accurately identifies the user.
  • Disadvantages: Getting your hand stolen to break into a vault sucks a lot more than getting your ID card stolen.
  • Authorization
    Once the system knows who the user is through authentication, authorization is how the system decides what the user can do.
    A good example of this is using group permissions or the difference between a normal user and the superuser on a unix system.
    There are other more compicated ACL (Access Control Lists) available to decide what a user can do and how they can do it. Most unix systems don't impliment this very well (if at all.)

    http://www.acm.uiuc.edu/workshops/security/auth.html

    Error: no 'server' JVM at '...'

    Some workstation or server will not come with server JVM after you install JDK on it. When you run server JVM by 'java -server MyApp', you will get the error such as:
    Error: no `server' JVM at `C:\Program Files\Java\jre1.6.0\bin\server\jvm.dll'.
    There is a little trick to get the server JVM up and running:
    1. Copy 'server' folder from the JDK's JRE's bin folder (example: C:\Program Files\Java\jdk1.6.0\jre\bin\server)
    2. Paste the 'server' folder to JRE's bin folder(example: C:\Program Files\Java\jre1.6.0\bin)
    3. Done
    http://thiamteck.blogspot.com/2007/10/error-no-server-jvm-at.html

    Tuesday, May 8, 2012

    How does Java hashmap work?


    A hashmap works like this (this is a little bit simplified, but it illustrates the basic mechanism):
    It has a number of "buckets" which it uses to store key-value pairs in. Each bucket has a unique number - that's what identifies the bucket. When you put a key-value pair into the map, the hashmap will look at the hash code of the key, and store the pair in the bucket of which the identifier is the hash code of the key. For example: The hash code of the key is 235 -> the pair is stored in bucket number 235. (Note that one bucket can store more then one key-value pair).
    When you lookup a value in the hashmap, by giving it a key, it will first look at the hash code of the key that you gave. The hashmap will then look into the corresponding bucket, and then it will compare the key that you gave with the keys of all pairs in the bucket, by comparing them with equals().
    Now you can see how this is very efficient for looking up key-value pairs in a map: by the hash code of the key the hashmap immediately knows in which bucket to look, so that it only has to test against what's in that bucket.
    Looking at the above mechanism, you can also see what requirements are necessary on thehashCode() and equals() methods of keys:
    • If two keys are the same (equals() returns true when you compare them), their hashCode()method must return the same number. If keys violate this, then keys that are equal might be stored in different buckets, and the hashmap would not be able to find key-value pairs (because it's going to look in the same bucket).
    • If two keys are different, then it doesn't matter if their hash codes are the same or not. They will be stored in the same bucket, but the hashmap will use equals() to tell them apart.

    http://stackoverflow.com/questions/6493605/how-does-java-hashmap-work

    How HashMap works in Java

    HashMap works on principle of hashing, we have put () and get () method for storing and retrieving object form hashMap.When we pass an both key and value to put() method to store on HashMap, it uses key object hashcode() method to calculate hashcode and they by applying hashing on that hashcode it identifies bucket location for storing value object.
    While retrieving it uses key object equals method to find out correct key value pair and return value object associated with that key. HashMap uses linked list in case of collision and object will be stored in next node of linked list.
    Also hashMap stores both key+value tuple in every node of linked list.

    What will happen if two different HashMap key objects have same hashcode?
    They will be stored in same bucket but no next node of linked list. And keys equals () method will be used to identify correct key value pair in HashMap.

    Read more: http://javarevisited.blogspot.com/2011/02/how-hashmap-works-in-java.html#ixzz1uJEyIZJ7