Friday, May 25, 2012

Why BPEL ?


 BPEL is for process/workflow technology what SQL is for relational databases. This is a way to bridge method war from different business school.
If you ahve several web services around the world, you should use BPEL to orchestrate them all into some new service.

Why using Maven is better than Ant?

In Ant you have to set up the classpath and configure and invoke the tool tasks for compilation and packaging.
Maven automatically downloads all dependencies, makes them available to your aplication and plugs them into the build life cycle phase.

Open source Live Profiling of Java ?



There are a lot of tools for profiling Java and the following is a list:

  • HPROF
           http://java.sun.com/developer/technicalArticles/Programming/HPROF.html
           $java -agentlib:hprof[=options] ToBeProfiledClass
           You get a dump AFTER running your application. This is not a live heap watching.

  •  Jmap, jps
         With command line tools  Jmap,jps you do not need to download anything. This comes with JAVA2SE.
         You must know the pid of the Java application to
  • EurekaJ and BTrace
           You can add inside Java code annotations to be gathered by BTrace.

  • AspectJ

         This is the preferred method.

Generally any metric-gathering can take up to 2500 nanoseconds, so use it only when necessary. For example disc access takes 40 milliseconds, servlet processing up to a second so adding 2500 nanoseconds for each of the methods would be negligible.

However the best way is to use AOP for performance monitoring and metric gathering consider JMetrix 
From AspectJ you can read the following:

3. What are some common development aspects?
Aspects for logging, tracing, debugging, profiling or performance monitoring, or testing.

4. What are some common production aspects?
Aspects for performance monitoring and diagnostic systems, display updating or notifications generally, security, context passing, and error handling.

How to restore iexplorer in windows?


If you get problem with iexplorer and can not for example visit sites, just follow the following instruction:
Open Start and write "inetcpl.cpl" in the field. Then reset then iexplorer:

Thursday, May 24, 2012

Cannot find the main class after converting java project to maven



This is because by default, Eclipse puts Java codes in src, but maven conventions are src/main/java. If you do not place or move the Java code to this place Maven will not find any main in your Eclipse when you are trying to run or compile.

Java error: SEVERE: StandardServer.await: create[8005]

Problem:

org.apache.catalina.core.StandardServer await
SEVERE: StandardServer.await: create[8005]:

Explanation:
This is because som application (javaw.exe to be exactly) listening on 8005.
Use tcpview from Sysinternals.com to locate the javaw.exe occupying 8005 and close this and restart the Tomcat server.

Tuesday, May 22, 2012

How to restore your file type association to .bat (batchfile)



If you have associated the extention file of .bat to for example Notepad, you have to remove this manually from Registry. Microsoft obviously did not provide a way to restore back to default program for .bat!
Do as follows:


From a command prompt run the following:
> assoc .bat
that should return with
..bat=batfile
If not run :
> assoc .bat=batfile
to restore the default file type association.

> ftype batfile
should return with
batfile="%1" %*
If not run:
> ftype batfile="%1" %*

Finally remove from your Registry the associated program:
Look for
HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts
and delete the key named and this should looks like below:

Creating a new webservice in Eclipse from Java methods


Following this tutorial will show how this is easy to create a webservices from your existing POJO java mathods. If you have good java application with all methods, simply follow this to expose your methods to the world. If you want to keep it private, use Shiro to keep your webservice available only to your applications.
Use jsSHA to encrypt your Javascript.

Tutorial:
http://www.eclipse.org/webtools/community/tutorials/BottomUpAxis2WebService/bu_tutorial.html

Java Architecture for XML Binding (JAXB)



http://www.oracle.com/technetwork/articles/javase/index-140168.html
PHP example:
http://www.pibx.de/documentation/code-examples/books/

Friday, May 18, 2012

Why Flash is better than Processing when displaying or programming

Display differences between Flash and Processing
In Processing the screen is a bitmap, a grid of pixels where you can draw, similar to the Canvas object in HTML5. Once you draw an ellipse, it becomes pixels. The display has no knowledge of objects. If you want to know if an object was clicked, you have to calculate yourself if the mouse action took place on top of some object in the screen. But you have to keep track yourself of sizes and locations of your objects.


In Flash/ActionScript the display is like a tree. You can add objects to the Stage, and objects inside objects. Each object has x, y, width, height, rotation and alpha properties which can be modified at any time. This makes it very easy to animate objects and also to detect mouse interaction. By default the programmer knows which object in the screen was clicked, rolled over or out, because Flash keeps track of all elements in the screen. If you draw an ellipse, it stays an ellipse. Unless you want to work in a way similar to Processing, in which case you can work with just one bitmap on the Stage and forget about the whole Flash Display Model.

Rotation of objects in Flash is usually also simpler. You can access the .rotation property of any object and change it. By default the center of rotation is the center or corner of each object, but it can be modified. To rotate in Processing you should first translate() the axes, then rotate() them and finally draw your object on the screen.

Wednesday, May 16, 2012

Java heap, preformance analysis tool


http://java.sun.com/developer/technicalArticles/Programming/HPROF.html

The best way to analysis the preformance and heap is to use HPROF from Java.

Convert your existing java object model to XML


This tutorial shows how to convert yoyur existing Java Object Model to XML without using metadata:
http://wiki.eclipse.org/EclipseLink/Examples/MOXy/GettingStarted

If you have a scheme and wat to generate Java Object Model, see the following link:
Generating a Java Model from an XML Schema
 EclipseLink MOXy is an extension.

The EclipseLink project delivers a comprehensive open-source Java persistence solution addressing relational, XML, and database web services. More information cab be found on  http://www.eclipse.org/eclipselink/

Monday, May 14, 2012

How to colve "javax.servlet.UnavailableException" or Servlet is not a javax.servlet.Servlet

If you get the following error message:


javax.servlet.UnavailableException: Servlet class com.example.test.myServlet is not a javax.servlet.Servlet

This is because you did not extends the class  com.example.test.myServlet with HttpServler.

The class below will cause javax.servlet.UnavailableException

public class myServlet {
   public void doPost(HttpServletRequest req, HttpServletResponse resp) {
        ...
   }

}

You solve this by extending this class with HttpRequest:
public class myServlet extends HttpServlet  {
   public void doPost(HttpServletRequest req, HttpServletResponse resp) {
        ...
   }

}

Saturday, May 12, 2012

Android Multitouch events using MotionEvent



 ACTION_POINTER_DOWN and ACTION_POINTER_UP are fired whenever a secondary pointer goes down or up.
  • If there is already a pointer on the screen and a new one goes down, you will receive ACTION_POINTER_DOWN instead of ACTION_DOWN. Subsequent pointers will fire  ACTION_POINTER_DOWN too and to get the actual pointer ID us with ACTION_POINTER_INDEX_MASK. 
  • If a pointer goes up but there is still at least one touching the screen, you will receive ACTION_POINTER_UP instead of ACTION_UP.

Friday, May 11, 2012

javax.servlet.UnavailableException at Google APp Engine

If you get the following error:


javax.servlet.ServletContext log: unavailable
javax.servlet.UnavailableException: Servlet class myServlet is not a javax.servlet.Servlet

This is due to version conflict between your Java installed and what Google App Engine is supposed to be supported.

Thursday, May 10, 2012

Best Javascript framework?


Node.js is build upon Chrome Javascript runtime and is widely used by eBay, LinkedIn, Google etc.
See what other said about the node.js:


Uber
Node has allowed us to build a global, real-time logistics system without having to think twice about locking or concurrency issues.
Cloud9 IDE
Node.js allows us to build our real-time cloud IDE with a single language front to back. It makes life easier for both us and our users to write, run, and debug code, anywhere, anytime.
LinkedIn
On the server side, our entire mobile software stack is completely built in Node. One reason was scale. The second is Node showed us huge performance gains.

Transloadit
Node.js allows us to execute our many independent background processes in a non-blocking way. This is essential to make file uploading and encoding the way we do it a great user experience.

Simple webserver using node.js:
This simple web server written in Node responds with "Hello World" for every request.

var http = require('http');
http.createServer(function (req, res) {
  res.writeHead(200, {'Content-Type': 'text/plain'});
  res.end('Hello World\n');
}).listen(1337, '127.0.0.1');
console.log('Server running at http://127.0.0.1:1337/');

Installing in Linux/Unix without sudo permission:

$ git clone https://github.com/joyent/node.git
$ cd node
$ mkdir ~/opt
$ export PREFIX=~/opt; ./configure
$ make
$ make install
$ echo 'export PATH=~/opt/bin:${PATH}' >> ~/.bashrc

Wednesday, May 9, 2012

Securing your RESTful service


There are different options to secure your web service:
Authentication:
      Identify who the user is using the web service.
      PKI, Active Directory is used for authentication.

Authorization:
    What the user can do with the web service.
     authorization service, LDAP is used for authorization.

There are several implementation solution to ecure the web service. Below is a list:
OAuth 1.0  is vulnerable to a session-fixation attack and could result in an attacker stealing the identity of an API end-user.
OAuth is secure API authorization in a simpleand standard. See the specification of OAuth at http://tools.ietf.org/html/rfc5849.


Good to know that HTTPS and HTTP authorization schemes based on HMAC (hash-based message authentication code) are used by Amazon S3 or Windows Azure are some of the measures for greater security.

If your API is free and read only you can use single key-based authentication.

Interesting article about Oauth and security to be read at http://hueniverse.com/oauth/guide/security/.

Read also what NSA say about implementation of RestFul service:
http://www.nsa.gov/ia/_files/support/guidelines_implementation_rest.pdf

More readings:
Http Authentication - http://docs.oracle.com/javase/6/docs/technotes/guides/net/http-auth.html
certificate based authority: http://docs.oracle.com/cd/E19316-01/820-2765/gdzeb/index.html

For Apache Shiro working with RestFul service check the blog:
http://blog.xebia.com/2011/04/18/apache-shiro/

Shiro integrating with Spring:
http://shiro.apache.org/spring.html

You can as well take a look at mo_security with Apache.
Oracle has a document on security:
http://www.oracle.com/us/products/middleware/identity-management/059410.pdf
And from Google:
http://lcsd05.cs.tamu.edu/slides/keynote.pdf

From Java.net:
http://weblogs.java.net/blog/gmurray71/archive/2006/08/restricting_acc.html

See how Amazon use Rest Security:
http://docs.amazonwebservices.com/AmazonSimpleDB/latest/DeveloperGuide/HMACAuth.html?r=6357
http://www.thebuzzmedia.com/designing-a-secure-rest-api-without-oauth-authentication/

Tomcat Realm and JASS
http://tomcat.apache.org/tomcat-5.5-doc/realm-howto.html

Also http://www.modsecurity.org/

  • Negative Security Model - looks for known bad, malicious requests. This method is effective at blocking a large number of automated attacks, however it is not the best approach for identifying new attack vectors. Using too many negative rules may also negatively impact performance.
  • Positive Security Model - When positive security model is deployed, only requests that are known to be valid are accepted, with everything else rejected. This approach works best with applications that are heavily used but rarely updated.
  • Virtual Patching - Its rule language makes ModSecurity an ideal external patching tool. External patching is all about reducing the window of opportunity. Time needed to patch application vulnerabilities often runs to weeks in many organizations. With ModSecurity, applications can be patched from the outside, without touching the application source code (and even without any access to it), making your systems secure until a proper patch is produced.
  • Extrusion Detection Model - ModSecurity can also monitor outbound data and identify and block information disclosure issues such as leaking detailed error messages or Social Security Numbers or Credit Card Numers.



Definition of Authentication and Authorization


Sometimes people are confused what Authentication and Authorization are and these two can eb mixed up together.

Read more on: http://www.acm.uiuc.edu/workshops/security/auth.html
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.


Authorization
Once the system knows who the user is through authentication, authorization is how the system decides what the user can do.



Error: Could not find the required version of the Java(TM) 2 Runtime Environment in '(null)'.




This is due to the fact you have not installed the Java package correctly. Just simply download and install again.

Tuesday, May 8, 2012

How to deal with ConcurrentModificationException using iterator


Reason:
You are using ArrayList and trying to iterate thorugh this array after some change in the Collection.

This can be handled by using Synchronized on Classes like the following:


private Object pathlist;
publid void Pathlist() {
      synchronized(pathlist) {

     }
}

This will guranatee a synchronized way of handling the object pathlist, no two concurrent thread will access this in the same time but in Synchronized way.

If you are usinf Iterator somewhere in your code and get ConcurrentModificationException pay special attention to List. Use ListIterator instead of Iterator.

For example the code below will cause a ConcurrentModificationException :

public void removePathline(int pointerID) {
synchronized (this) {
Iterator<Pathline> it=pathlist.iterator();    // 1) This will throw                            
                                                                                      //ConcurrentModificationException 
//ListIterator<Pathline> it=pathlist.listIterator();   // 2) this should be used
       while(it.hasNext())
       {
          Pathline pl = (Pathline) it.next();
          synchronized ( pl ) {
          if ( pl.getPointerID() == pointerID ) {
          pathlist.remove(pl);
          }
          }
       }
}
}


The code above is removing the object while it is iterating. The iterator will check if the List has been modified and if the list has been changed, it throw a ConcurrentModificationException
In solution number 2 will solve the problem.

Java Collection classes are fail-fast which means that if the Collection will be changed while some thread is traversing over it using iterator, the iterator.next() will throw a ConcurrentModificationException.



To Avoid ConcurrentModificationException in multi-threaded environment:

1. You can convert the list to an array and then iterate on the array. This approach works well for small or medium size list but if the list is large then it will affect the performance a lot.

2.Use synchronized block. This approach is not recommended in multithreading environment if you want to use the benefits of multithreading. For the case of iterator, Synchronized approch will not solve your problem. See preferred approch below.

3. Use ConcurrentHashMap and CopyOnWriteArrayList classes if you are using JDK1.5 or higher. It is the recommended approach.







Friday, May 4, 2012

Installing Perl modules in a different directory



If you have various old Perl modules and want to install Perl modules in another directories without root  or administrator privileges use Perlbrew 
For more information check this out:
http://www.cpan.org/modules/INSTALL.html