Friday, April 11

String is immutable in java


Yes, it true that string is immutable in java which means that the String object doesn't change.
But that can't be true because we have seen many occasions in the program where the String variable is changed.

Yes its true, but we have our understanding of String a bit wrong here. Actually unlike C program, in Java, String is not a primitive variable but a special Class. So in our code  actually we are creating a String object for new character sequence.

For more understanding go to this link String can't be immutable in java

Friday, April 4

Neo4j Community Build Error


To build the neo4j community,  I am following  the steps as done in Travis CI link here.

But i am getting the error below
[ERROR] Failed to execute goal org.neo4j.build.plugins:license-maven-plugin:3:check (check-licenses) on project neo4j-kernel: Some files do not have the expected license header -> [Help 1] 
To solve this "Build the community/license-check submodule first." as suggested in SO link here.

But still i am getting the same error.

Please check whether my steps taken are correct or not.


/$ mvn -version
Apache Maven 3.0.4
Maven home: /usr/share/maven
Java version: 1.6.0_30, vendor: Sun Microsystems Inc.
Java home: /usr/lib/jvm/java-6-openjdk-amd64/jre
Default locale: en_IN, platform encoding: UTF-8
OS name: "linux", version: "3.2.0-60-generic", arch: "amd64", family: "unix"



/$ cd ~/Workspace/neo4jdev/community-experiments/licensecheck-config/
~/Workspace/neo4jdev/community-experiments/licensecheck-config$ mvn install

[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 4.239s
[INFO] Finished at: Fri Apr 04 15:06:07 IST 2014
[INFO] Final Memory: 14M/148M



~/Workspace/neo4jdev/community-experiments/licensecheck-config$ cd ..
~/Workspace/neo4jdev/community-experiments$ MAVEN_OPTS="-Xss2m -Xmx1G"
~/Workspace/neo4jdev/community-experiments$ mvn install -DskipTests

[INFO] --- license-maven-plugin:3:check (check-licenses) @ neo4j-kernel ---
[INFO] Checking licenses...

[INFO] [INFO] Missing header in: /~/Workspace/neo4jdev/community-experiments/kernel/src/test/java/org/neo4j/unsafe/batchinsert/BatchInserterImplTest.java
[INFO] Missing header in: /~/Workspace/neo4jdev/community-experiments/kernel/src/test/java/org/neo4j/kernel/RaceBetweenCommitAndGetMoreRelationshipsIT.java
[INFO] Missing header in: /~/Workspace/neo4jdev/community-experiments/kernel/src/main/java/org/neo4j/tooling/GlobalGraphOperations.java

[INFO] ------------------------------------------------------------------------
[INFO] Reactor Summary:
[INFO] 
[INFO] Neo4j - Graph Database Kernel ..................... FAILURE [1.719s]
[INFO] Neo4j - JMX support ............................... SKIPPED
[INFO] Neo4j - Usage Data Collection ..................... SKIPPED
[INFO] Neo4j - Graphviz generation ....................... SKIPPED
[INFO] Neo4j - Lucene Index .............................. SKIPPED
[INFO] Neo4j - Graph Algorithms .......................... SKIPPED
[INFO] Neo4j - Graph Matching ............................ SKIPPED
[INFO] Neo4j - Cypher .................................... SKIPPED
[INFO] Neo4j Community ................................... SKIPPED
[INFO] Neo4j Community ................................... SKIPPED
[INFO] Neo4j - Generic shell ............................. SKIPPED
[INFO] Neo4j Examples .................................... SKIPPED
[INFO] Neo4j Server API .................................. SKIPPED
[INFO] Neo4j Server ...................................... SKIPPED
[INFO] Neo4j Server Examples ............................. SKIPPED
[INFO] Licensing configuration ........................... SKIPPED
[INFO] Neo4j Community Build ............................. SKIPPED
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 3.427s
[INFO] Finished at: Fri Apr 04 15:08:26 IST 2014
[INFO] Final Memory: 14M/153M
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.neo4j.build.plugins:license-maven-plugin:3:check (check-licenses) on project neo4j-kernel: Some files do not have the expected license header -> [Help 1]
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoExecutionException


Resolved

The license check is failing may because of some repository url may have changed,  which we have no control of so its better to skip the license check.

The following command will help to skip license check and build sucessfully.
$ mvn install -DskipTests -Dlicensing.skip

In linux os, it build successfully but in windows there were some issues in project "neo4j-browser" so i skipped that project to successfully  build in windows too.

Below is the error in windows in project neo4j-browser
[INFO] ------------------------------------------------------------------------
[INFO] Reactor Summary:
[INFO]
[INFO] Licensing configuration ........................... SUCCESS [  2.796 s]
[INFO] Neo4j - Primitive Collections ..................... SUCCESS [  0.794 s]
[INFO] Neo4j - Graph Database Kernel ..................... SUCCESS [01:34 min]
[INFO] Neo4j - JMX support ............................... SUCCESS [  1.362 s]
[INFO] Neo4j - Usage Data Collection ..................... SUCCESS [  3.880 s]
[INFO] Neo4j - Monitor Logging ........................... SUCCESS [  1.120 s]
[INFO] Neo4j - Graphviz generation ....................... SUCCESS [  1.592 s]
[INFO] Neo4j - Lucene Index .............................. SUCCESS [  4.390 s]
[INFO] Neo4j - Graph Algorithms .......................... SUCCESS [  2.070 s]
[INFO] Neo4j - Graph Matching ............................ SUCCESS [  1.230 s]
[INFO] Neo4j Community Cypher Build ...................... SUCCESS [ 11.080 s]
[INFO] Neo4j - Cypher Commons ............................ SUCCESS [ 50.804 s]
[INFO] Neo4j - Cypher Compiler 2.1 ....................... SUCCESS [03:45 min]
[INFO] Neo4j - Cypher .................................... SUCCESS [ 37.636 s]
[INFO] Neo4j Community ................................... SUCCESS [  1.420 s]
[INFO] Neo4j GraphGist ................................... SUCCESS [  6.780 s]
[INFO] Neo4j - Cypher Documentation ...................... SUCCESS [ 36.070 s]
[INFO] Neo4j Community ................................... SUCCESS [  0.750 s]
[INFO] Neo4j - Generic shell ............................. SUCCESS [  3.550 s]
[INFO] Neo4j Examples .................................... SUCCESS [  1.790 s]
[INFO] Neo4j Server API .................................. SUCCESS [  1.920 s]
[INFO] Neo4j Browser ..................................... FAILURE [  0.430 s]
[INFO] Neo4j Server ...................................... SKIPPED
[INFO] Neo4j - Server Plugin Tests ....................... SKIPPED
[INFO] Neo4j Server Examples ............................. SKIPPED
[INFO] Neo4j Community Build ............................. SKIPPED
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 08:15 min
[INFO] Finished at: 2014-04-11T17:08:08+05:30
[INFO] Final Memory: 54M/302M
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-antrun-plugin:1.6:run (default) on project neo4j-browser: An An
t BuildException has occured: unless=cmdsuccess -> [Help 1]

Now if you want to continue by skipping the project "Neo4j Browser", follow the below command
$ mvn install -DskipTests -Dlicensing.skip -e -rf :neo4j-server


Atlas, successfully build
[INFO] ------------------------------------------------------------------------
[INFO] Reactor Summary:
[INFO]
[INFO] Neo4j Server ...................................... SUCCESS [01:55 min]
[INFO] Neo4j - Server Plugin Tests ....................... SUCCESS [  3.482 s]
[INFO] Neo4j Server Examples ............................. SUCCESS [  1.880 s]
[INFO] Neo4j Community Build ............................. SUCCESS [  0.440 s]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 02:05 min
[INFO] Finished at: 2014-04-11T17:20:07+05:30
[INFO] Final Memory: 46M/227M
[INFO] ------------------------------------------------------------------------


Saturday, February 16

Difference between public, private, protected and default



 

  Access Modifier Visibility


Modifier
Class
Package
Subclass
Everywhere
public
Yes
Yes
Yes
Yes
protected
Yes
Yes
Yes
No
default
Yes
Yes
No
No
private
Yes
No
No
No

public  - Members are visible in same class, same package, sub classes and in other classes.

protected - Members are visible in same class, same package, sub classes but not in other classes.

default - Members are visible in same class, same package but not in sub classes and other classes.

private - Members are visible only in same class but not in same package, sub classes and other classes.

  

  Access Modifier Level Applicability


Modifier
Member Field
Member Method
Nested Class
Top Level Class
public
Yes
Yes
Yes
Yes
default
Yes
Yes
Yes
Yes
private
Yes
Yes
Yes
No
protected
Yes
Yes
Yes
No
 
 public, default  - These modifiers can be applied to members of the class and also to the top class. 

 private, protected  - These modifiers can be applied to member of a class only but not to the top class.



Simple Inheritance

parent.java
package r1;
public class parent
{
        int x;
      
       public parent()
       {
              x=10;
       }
             
       public static void main(String args[])
       {
             
              parent p = new parent();
              System.out.println("parent main x="+p.x);
       }
}

Output
parent main x=10


sub1.java
package r1;
public class sub1 extends parent  // a subclass of parent
{
       sub1()
       {
              x=20;
       }
      
       public static void main(String args[])
       {
              sub1 s1 = new sub1();
              System.out.println("In r1 pckg, sub1 class, main fn, sub1.x = "+s1.x);           
       }     
}

Output
In r1 pckg, sub1 class, main fn, sub1.x = 20


Examples of  Access Modifiers

Following are the errors that occurs if access modifiers are not used properly:


private - The field parent.x is not visible

parent.java
package r1;
public class parent
{
        private int x;      // private access modifier
}

sub1.java
package r1;  // same package as the parent
public class sub1 extends parent  // a subclass of parent
{
       sub1()
       {
              x=20;
       }
      
       public static void main(String args[])
       {
              sub1 s1 = new sub1();
              System.out.println("In r1 pckg, sub1 class, main fn, sub1.x = "+s1.x);           
       }     
}
Output
The field parent.x is not visible

Because: sub1.java is not the same class where x is declared.



default - The field parent.x is not visible

parent.java
package r1;
public class parent
{
        int x;  // no access modifier is default
}

sub2.java
// sub2 class is not part of r1 package
import r1.parent;   // so  its imported
public class sub2 extends parent  // a subclass of parent
{
       sub2()
       {
              x=30;
       }
      
       public static void main(String args[])
       {
              sub2 s2 = new sub2();
              System.out.println("In no pckg, sub2 class, main fn, sub2.x = "+s2.x);
       }     
}

Output
The field parent.x is not visible

Because: sub2.java is not in the same package of parent where x is declared.



 protected - The field parent.x is not visible

parent.java
package r1;
public class parent
{
        protected int x;  // protected access modifier
}

some1.java
// some1 class is not part of r1 package
import r1.parent;
public class some1 // not a subclass of parent
{            
       public static void main(String args[])
       {
          parent p = new parent();
          System.out.println("In no pckg, some1 class, main fn, parent.x = "+p.x);
       }     
}

Output
The field parent.x is not visible

Because: some1.java is neither in the same package nor in subclass of parent  where x is declared.

Saturday, October 20

Facebook and AD Campaigns



Is it true, bots are clicking on the ads in facebook?

Yes, its True!

Limited Run Company noticed it could only verify about 20 percent of the clicks that were supposedly being converted to users showing up on its Web site.

80% of clicks it was paying for were coming from bots.

Bots are computer scripts or fake users who simply repeatedly click the ads.
  

What if you complain this to facebook?

If a complaint is send to Facebook, then it demands for the below information:
  1. Server Logs
  2. Aggregated counts of your clicks

What are these?

1. Server Logs

Raw server logs of all clicks coming to your website, or the total amount of all clicks coming from Facebook, with an explanation of how you filtered them.

These server logs must contain:
  1. Timestamp of page load
  2. User agent string
  3. User IP
  4. Exact page loaded, with the parameters passed

 2. Aggregated counts of your clicks (Optional)

If possible, please also include the following:
  1. The total number of clicks you received from Facebook split by day, for the specific time period where you have noticed the click issues.
  2. The total number of clicks you were billed for, by Facebook, also by billable day for the period in question.
  3. A screenshot of your external reporting system showing the total number of clicks received from Facebook.


How to get Server Logs?

For Tomcat Application Server, it can be achieved by setting the value of ‘Access Log Valve’ tag, in pattern attribute as ‘combined’ in server.xml.

<Valve className="org.apache.catalina.valves.AccessLogValve" pattern="combined" directory="logs" prefix="yoursitename_access" suffix=".txt" />
  

Techniques to find or block bots


1. Unique Cookie is one way to find out whether a user has registered multiple times from the campaign or not.

When a user registers from a campaign, server should store a unique cookie value to that user’s browser. So the next time a user registers from that browser, server can find out by looking at the cookie stored.


2. Create a blacklist of user agents of known bots like FeedBurner, Googlebot.

Whenever a request comes, compare their user agent string with the blacklist, to determine whether these are bots or not. Or even a prevention step can also be taken from these identified bots.

This implementation has a performance hit because we have to check each user request user agent to the blacklist.

Also some user agents are real users (hired by ad agency for cheap labour). No blacklist is going to have those real users.


3. Flag bot vs. non-bot.

Log information about browsers which cannot execute Javascript, and then built some sort of system that analyzes these browsers and their trends (like, if they originate from a certain IP range).
 Javascript is something that is on by default and for a user to turn it off is an explicit manual action. No user wants to turn off the javascript, unless for doing any suspicious activity or he is a guy who hates the word ‘java’.

But this means cannot catch the bots until after the analysis.

4.  Whenever user registers, send a verification code to the user’s email or sms to authenticate the identity.
 

5.  Captcha
captcha block bot technique

           But not good for user experience and may even prevent irritated users from registering.  


References: