Showing posts with label other. Show all posts
Showing posts with label other. Show all posts

NCDU - How to detect larger directory in linux

If you want search for big files, or big directory..
If you want to do maintenance for you free space,

You have to install NCDU.

I try it! It is wonderful..
It works with a intuitive text gui.

I install it by using
yum install ncdu

and launch by command ncdu after installing.

SSH won't Work - expecting SSH2_MSG_KEX_DH_GEX_GROUP

Short Post for a little-big Problem.

If your firewall is open for ssh, telnet on ssh port works but ssh won't work.

Try to debug the ssh connection by using verbose debug

ssh -v user@yourip

if ssh hangs on

"expecting SSH2_MSG_KEX_DH_GEX_GROUP"

and then ssh returns with the following error: "Read from socket failed: Operation timed out"

Probably you have an MTU/fragmentation problem and you will solve the problem by set correctly a new mtu value for network interface.

So launch by terminal the following command:

sudo ifconfig en1 mtu 576

Where en1 is your active network interface.

Java Hello World

Java Hello World
G.Morreale

Introduzione:

The simplest programming language example!

//define a class HelloWorld
public class HelloWorld 
{ 
    //define a public static method in the class that make an entry point for the application.
    //It takes an array of String parameter
    public static void main(String[] args) 
    { 
        //Use the base System api to print a "Hello World" string in the standard output.
        System.out.println("Hello World"); 
    } 
}

Conclusion:

In a simple Java Hello World example come into play various topics:

  • the class notion
  • the public modifiers
  • the static method modifiers
  • Array type
  • String type
  • a static method call (System.out.println(String a))

So, You have to start with an object oriented programming tutorial within java tutorial in order to start programming in java.

Get the row with a max value in mysql

Get the row with a max value in mysql
G.Morreale

Introduction


Problem: Find the row with the max value of a column, for example 
For each article, find the dealer or dealers with the most expensive price.

The solution is around mysql queries.

Tests Environment

Make an example db with a test table in mysql:

CREATE DATABASE `mytest`;

DROP TABLE IF EXISTS `mytest`.`shop`;
CREATE TABLE  `mytest`.`shop` (
  `article` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `dealer` varchar(45) DEFAULT NULL,
  `price` int(11) DEFAULT NULL,
  PRIMARY KEY (`article`) USING BTREE
) ENGINE=InnoDB AUTO_INCREMENT=87158 DEFAULT CHARSET=latin1;

Insert some rows in the database (I inserted 6800 row)

INSERT INTO mytest.shop (dealer,price) values ('aaa',round(rand()*100));

the test was done on windows xp, mysql 5.1 standard installation

Solutions

  • SELECT price from shop s1 order by price desc limit 1))

It sort the prices and take the first. The best sorting cannot be done in less than O(n log n) time.

  • SELECT price FROM shop s1 WHERE price=(SELECT MAX(s2.price) FROM shop s2))

It take a linear (O(n)) time for max computation and the other linear time for price comparison with max value.

  • SELECT s1.price FROM shop s1 LEFT JOIN shop s2 ON s1.article <> s2.article and s1.price < s2.price WHERE s2.article IS NULL))

This last solution works on the basis that when s1.price is the maximum value there is no s2.price with a greater value so the s2 rows values will be null.
It seems also a linear time computation.

(I don't know about mysql internals algorithm so take my discussion  with a grain of salt)


Results

select benchmark(800000000,(select price from shop s1 order by price desc limit 1));

37.1 seconds

select benchmark(800000000,(SELECT s1.price FROM shop s1 LEFT JOIN shop s2 ON s1.article <> s2.article and s1.price < s2.price WHERE s2.article IS NULL));

37.3 seconds

select benchmark(800000000,(SELECT price FROM shop s1 WHERE price=(SELECT MAX(s2.price) FROM shop s2)));

38.45 seconds

Conclusion

From the test the fastest query seems to be the first (with the order by), but I'm not sure if it is correct.
Before the tests I was thinking that the other solutions was better.

So if anyone wish to express an opinion, he will be welcome.

Facebook Java Api(ENG)__

Facebook Java Api Example
G.Morreale

Introduction:

This article explain the facebook java api through an example.
First you need a facebook account, and you have to enable "Developer" application.

Then you must configure you account and make a new application configuration in order to obtain 
"api key" and "secret key".
This can be accomplished by reading http://developers.facebook.com/get_started.php


The Server

You need a java web server (tomcat, glassfish, jboss etc.) available by the web.
Localhost server isn't ok to facebook integration purpose.

Facebook Java Api

If you want to interact with facebook platform a client library can be very useful.
Client library are available in different languages:http://wiki.developers.facebook.com/index.php/Client_Libraries

There isn't a officiale Java api but you can choose alternative unofficial ones:


I prefer the last one.
So go to http://code.google.com/p/facebook-java-api/ and download facebook-java-api-2.0.4.bin.zip (or later).
When you download it, extract the jar into a directory and get it available in you facebook example application classpath.

Facebook server make available user data, photos, groups infos etc by rest api:http://wiki.developers.facebook.com/index.php/API

The Facebook Client Project

Make a new Web project, and make a new empty servlet.
The servlet url-pattern configured in web.xml must be the same indicated in facebook application configuration.

The Source Code

public class index extends HttpServlet
{

    //facebook give it!
    String apiKey = "your api key";
    String secretKey = "your secret key";

    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException
    {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try
        {
            out.println("<h2>User information</h2>");

            //facebook login mechanism give you by http parameter the session key
            //needed for client api request.
            String sessionKey = request.getParameter(FacebookParam.SESSION_KEY.toString());

            //initialize a facebook xml client (you can choose different client version: xml, jaxb or json)
            //the init is done by apiKey, secretKey and session key previosly requested
            FacebookXmlRestClient client = new FacebookXmlRestClient(apiKey, secretKey, sessionKey);

            
            //This code line obtain the user logged id
            Long uid = client.users_getLoggedInUser();

            //print user info.
            out.println(printUserInfo(uid, client, sessionKey));
}
 
private String printUserInfo(Long uid, FacebookXmlRestClient client, String sessionKey) throws FacebookException
    {
        StringBuffer ret = new StringBuffer();
        //init array parameter
        ArrayList<Long> uids = new ArrayList<Long>(1);
        uids.add(uid);
        //init field parameter - we choose all profile infos.
        List<ProfileField> fields = Arrays.asList(ProfileField.values());

        //init the client in order to make the xml request
        client = new FacebookXmlRestClient(apiKey, secretKey, sessionKey);
        //get the xml document containing the infos
        Document userInfoDoc = client.users_getInfo(uids, fields);

        //for each info append it to returned string buffer
        for (ProfileField pfield : fields)
        {
            ret.append(pfield.fieldName()).append(" <b>").append(userInfoDoc.getElementsByTagName(pfield.fieldName()).            item(0).getTextContent()).append("</b>");
            ret.append("</br>");
        }
        return ret.toString();
    }

Conclusion 
In this simple manner you can print all logged facebook user info into facebook application.
In order to make a cleary client authentication by using java servlet filter you can read this:
http://www.theliveweb.net/blog/2007/10/31/facebook-authentication-using-java/ 






Android First Example: Hello World

Android First Example: Hello World
G.Morreale

Introduction:

Android is a software stack for mobile devices that includes an operating system, middleware and key applications. 

The Android SDK contains API necessary to begin developing applications on the Android platform using the Java programming language.
In this article I point out the steps for making the hello world example and running it in the emulator.

The Steps:

In order to write the example follow these steps

  • The Android SDK

    • Extract it into your hard drive directory

  • Java Code

    • Make a new java project
    • set in the project class path android.jar (you can find it in sdk zip file)
    • make a new java class into Hello.java
    • copy this source code in Hello.java:

//package name must be composed of at least two java identifiers
package my.android;

import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;

    public class Hello extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //Make a new text view passing Activity object
        TextView tv = new TextView(this);
        //Set a text into view
        tv.setText("Hello World");
        //set the view into activity view container
        setContentView(tv);
    }
}

    • compile Hello.java code

  • Build and install The apps

    • In tools sdk directory you can find activitycreator.bat, launch it by passing the complete package name and class name:
activitycreator -o c:\android my.android.Hello

    • The script prepare the android application putting it into c:\android directory

    • Now you need ant tools in order to build the android application
Download it from: http://ant.apache.org/bindownload.cgi

    • Go in c:\android directory
    • Launch: "ant -f build.xml" command
(note: if you go in "Unable to locate tools.jar" error you must set correctly jdk classpath.)
you'll be left with a file named Hello-debug.apk under the 'bin' directory

    • Go again in tools sdk directory.
Now you can use adb command in order to install the apk file into android emulator

    • First launch the emulator (if you don't adb command fails)
(note: in order to launch emulator you must execute the emulator.exe command in sdk tools directory.)

    • Then launch the follow command: adb install c:\android\bin\hello-debug.apk

  • Run The example

    • Close the emulator
    • Re-launch the emulator

    • Click on the arrow in the bottom of the screen so you open the application list, now you can 

    • find hello application.. click on it to see the hello word string..


Conclusion 

It is only a small example introducing android development.
The next step is to go deep into

Please leave a feedback in the comment to this post.

Java and OpenOffice BASE db through HSQLDB jdbc

Java and OpenOffice BASE db
G.Morreale

Introduction:

A Base document can create an HSQLDB database that is stored inside of the Base document.
OOo documents are stored as zip files and Base documents are no exception.

So HSQLDB is the internal engine of OpenOffice db

In this article we see how can interact by java source code with OpenOffice base db using the hsqldb jdbc driver.

The Steps:

In order to write the example follow these steps

  • Prepare ODB database with openoffice 

    • Create a new office base database(i.e. mydb.odb).
    • Create a new table(i.e. User) into base database.
    • Make the coloumns inside the table(ID,firstname,lastname)
    • Populate it with some rows

  • Prepare HSQLDB extracting it from ODB

    • Rename the mydb.odb file in mydb.zip, extract "database" directory from it, so you can find these files:
      • backup
      • data
      • properities
      • script
    • Copy the files into c:\mydbdir\ location
    • rename all the files by putting the same prefix before the file name, example:
      • mydb.backup
      • mydb.data
      • mydb.properities
      • mydb.script

            The prefix and filename are separated by dot. These files is the HSQLDB.


  • Prepare HSQLDB JDBC API


We are ready for source code!


The Source Code

import java.text.ParseException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.logging.Level;
import java.util.logging.Logger;

public class Main
{

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) throws ParseException
    {
        try
        {            
            String db_file_name_prefix = "c:\\mydbdir\\mydb";
            
            Connection con = null;
            // Load the HSQL Database Engine JDBC driver
            // hsqldb.jar should be in the class path or made part of the current jar
            Class.forName("org.hsqldb.jdbcDriver");

            // connect to the database.   This will load the db files and start the
            // database if it is not alread running.
            // db_file_name_prefix is used to open or create files that hold the state
            // of the db.
            // It can contain directory names relative to the
            // current working directory
            con = DriverManager.getConnection("jdbc:hsqldb:file:" + db_file_name_prefix, // filenames
                    "sa", // username
                    "");  // password

            Statement statement = con.createStatement();
            //look at " for table name
            ResultSet rs = statement.executeQuery("SELECT * FROM \"User\"");

            //print the result set
            while (rs.next())
            {
                System.out.print("ID: " + rs.getString("ID"));
                System.out.print(" first name: " + rs.getString("firstname"));
                System.out.println(" last name: " + rs.getString("lastname"));
            }

            statement.close();
            con.close();

        } catch (SQLException ex)
        {
            Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
            ex.printStackTrace();
        } catch (ClassNotFoundException ex)
        {
            Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}


Conclusion 

The odb is a zip file, so the beginning steps is uncomfortable because you must rename the odb in zip, rename, work on it and then recompress all in order to edit the odb again with openoffice.
In order to avoid this incovenient way it is possible to make the beginning steps automatically by using java.util.zip package:

Please leave a feedback in the comment to this post.

Search and Find a JAR

Search and Find a JAR
G.Morreale

clicca qui per la versione in italiano
Introduction:

Sometimes when you download library, or simply classes the  provided package is not inclusive of all necessary dependencies. 
Therefore the need for this and in other cases, find jar containing a given class. 

The solution:

I found a good online service that can resolve the problem of finding, given the name of a class, the name of the jar file that contains this class in it. 

The service is available from the site www.findjar.com 

It then helps to solve the exceptions: 

  • NoClassDefFoundError
  • ClassNotFoundException

Example 

For example, by typing the name of the class XMLSerializer 

You get the following output: 

[CLASS] org.kxml2.io.K XmlSerializer
[CLASS] org.xmlpull.v1. XmlSerializer
[CLASS] org.kxml2.wap.Wb xmlSerializer
[CLASS] net.sf.json.xml. XMLSerializer
[CLASS] com.idoox.util.xml. XMLSerializer
[CLASS] oracle.xml.binxml.Bin XMLSerializer
[CLASS] org.apache.ws.jaxme.JM XmlSerializer
[CLASS] org.vraptor.remote.xml. XMLSerializer

In this case, not finding a jar containing the class XMLSerializer, the system proposes the complete package name of classes similar to INPUT typed. 

If you click on org.kxml2.io.K XmlSerializer or type it as input, since, to name a complete package and the correct output proposes the names of the JARs that contain the specific class: 


Containing JAR 
files:
kxml2.jar 
kxml2-2.1.8.jar


Conclusion 

A short article to link a simple but sometimes very useful website. 
If you want to point out interesting services and leave a comment to this post. 

Facebook Java Api(ENG)

Facebook Java Api Example
G.Morreale




Introduction:

This article explain the facebook java api through an example.
First you need a facebook account, and you have to enable "Developer" application.

Then you must configure you account and make a new application configuration in order to obtain 
"api key" and "secret key".
This can be accomplished by reading http://developers.facebook.com/get_started.php


The Server

You need a java web server (tomcat, glassfish, jboss etc.) available by the web.
Localhost server isn't ok to facebook integration purpose.

Facebook Java Api

If you want to interact with facebook platform a client library can be very useful.
Client library are available in different languages:http://wiki.developers.facebook.com/index.php/Client_Libraries

There isn't a officiale Java api but you can choose alternative unofficial ones:


I prefer the last one.
So go to http://code.google.com/p/facebook-java-api/ and download facebook-java-api-2.0.4.bin.zip (or later).
When you download it, extract the jar into a directory and get it available in you facebook example application classpath.

Facebook server make available user data, photos, groups infos etc by rest api:http://wiki.developers.facebook.com/index.php/API

The Facebook Client Project

Make a new Web project, and make a new empty servlet.
The servlet url-pattern configured in web.xml must be the same indicated in facebook application configuration.

The Source Code

public class index extends HttpServlet
{

    //facebook give it!
    String apiKey = "your api key";
    String secretKey = "your secret key";

    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException
    {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try
        {
            out.println("<h2>User information</h2>");

            //facebook login mechanism give you by http parameter the session key
            //needed for client api request.
            String sessionKey = request.getParameter(FacebookParam.SESSION_KEY.toString());

            //initialize a facebook xml client (you can choose different client version: xml, jaxb or json)
            //the init is done by apiKey, secretKey and session key previosly requested
            FacebookXmlRestClient client = new FacebookXmlRestClient(apiKey, secretKey, sessionKey);

            
            //This code line obtain the user logged id
            Long uid = client.users_getLoggedInUser();

            //print user info.
            out.println(printUserInfo(uid, client, sessionKey));
}



 

private String printUserInfo(Long uid, FacebookXmlRestClient client, String sessionKey) throws FacebookException
    {
        StringBuffer ret = new StringBuffer();
        //init array parameter
        ArrayList<Long> uids = new ArrayList<Long>(1);
        uids.add(uid);
        //init field parameter - we choose all profile infos.
        List<ProfileField> fields = Arrays.asList(ProfileField.values());
        //init the client in order to make the xml request
        client = new FacebookXmlRestClient(apiKey, secretKey, sessionKey);
        //get the xml document containing the infos
        Document userInfoDoc = client.users_getInfo(uids, fields);
        //for each info append it to returned string buffer
        for (ProfileField pfield : fields)
        {
            ret.append(pfield.fieldName()).append(" <b>").append(userInfoDoc.getElementsByTagName(pfield.fieldName()).            item(0).getTextContent()).append("</b>");
            ret.append("</br>");
        }
        return ret.toString();
    }

Conclusion 
In this simple manner you can print all logged facebook user info into facebook application. In order to make a cleary client authentication by using java servlet filter you can read this:
http://www.theliveweb.net/blog/2007/10/31/facebook-authentication-using-java/ 

TrovaTv Samsung and facebook



 

Iphone File Download (ENG ver)..

Download Servlet implementing http range header
(Needed for IPhone)
G.Morreale

Introduction:

By implementing a system to download 3gp file to be downloaded on mobile devices, you can see how some devices, such as Iphone, making an http request using range header.

The Problem:

In essence, unlike the classic case where the file is provided as an attachment header like this:

  • Content-Disposition:attachment;filename=VIDEO
  • Content-Transfer-Encoding:binary

with the range header, the iphone, or other clients require the content portions, requiring only bytes ranges.

If you want to learn about the specific dell'header range sent by the client simply log on w3c.org (http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html) Section 14:35

The client receiving the content-length header knows how many bytes have to requests, then it requests the various range until different subsets made the entire contents.

ex.

request of bytes ranging from position 500 to 999
range: bytes=500-900

The Solution:

The server must parse range header and only return the portion of the requested data.
Reading the range header specifications you can see that any request may be claimed different range.

ex.
range:bytes=500-900,999-1500

So the parsing must be able to provide this specification.

The Source Code:

As a first step we could build a class that can represent a range:

class ByteRange
{
    long start;
    long end;

    public long getEnd()
    {
        return end;
    }

    public void setEnd(long end)
    {
        this.end = end;
    }

    public long getStart()
    {
        return start;
    }

    public void setStart(long start)
    {
        this.start = start;
    }

    public ByteRange(long start, long end)
    {
        this.start = start;
        this.end = end;
    }

}


e successivamente un metodo, da inserire nella servlet di download, in grado di effettuare il parsing dell'header and a method, to be included in the download servlet, capable of parsing dell'header

     /**
     * Effettua il parsing dell'header http range
     * @param rangeHeader - contenuto dell'header range
     * @param dataLen - dimensione dell'array di byte
     * @return arraylist di oggetti ByteRange rappresentanti i vari range
     */   
    private ArrayList<ByteRange> parseRange(String rangeHeader, int dataLen)
    {
        ArrayList<ByteRange> ranges = null;
        //verifica correttezza dell'header
        if (rangeHeader != null && rangeHeader.startsWith("bytes"))
        {            
            ranges = new ArrayList<ByteRange>(8);
            //split dei diversi range separti da ,
            String[] rangesComma = rangeHeader.split(",");
            //per ogni range si determina la posizione di partenza e quella finale
            for (String r : rangesComma)
            {
                r = r.substring(6);
                int dashPos = r.indexOf('-');
                long end = dataLen - 1;
                long start = Long.parseLong(r.substring(0, dashPos));
                if (dashPos < r.length() - 1)
                {
                    end = Long.parseLong(r.substring(dashPos + 1, r.length()));
                }

                ranges.add(new ByteRange(start, end));
            }
        }
        return ranges;
    }

The final part of the servlet returning requested bytes:

protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException
    {
//-----------DEBUG------------------------
//        Enumeration en = request.getHeaderNames();
//        while (en.hasMoreElements())
//        {
//            String elem = (String) en.nextElement();
//            System.out.println(elem + " " + request.getHeader(elem));
//        }
//-----------FINE DEBUG------------------------

        byte[] data = (byte[]) request.getAttribute("data");
        if (data == null)
        {
            response.sendError(404);
            return;
        }
        response.setContentLength(data.length);

        //data not found
        if (data == null)
        {
            response.setStatus(response.SC_NOT_FOUND);
            return;
        }

        ServletOutputStream sos = response.getOutputStream();

        //extract range header
        String rangeHeader = request.getHeader("range");
        //parse multiple range bytes
        ArrayList<ByteRange> ranges = parseRange(rangeHeader, data.length);
        if (ranges != null)
        {
            long start = -1;
            long end = -1;

            if (ranges.size() == 1)
            {
                ByteRange range = ranges.get(0);
                start = range.getStart();
                end = range.getEnd();
                response.setHeader("Content-Range", "bytes " + start + "-" + end + "/" + data.length);
                response.setStatus(response.SC_PARTIAL_CONTENT);
                for (long j = start; j <= end; j++)
                {
                    sos.write(data[(int) j]);
                }
            }
            else
            {
                response.setStatus(response.SC_NOT_IMPLEMENTED);            
            }
        }
        else
        {            
            sos.write(data);        
        }

    }
The code, I think, is quite simple to understand, only thing to note is http STATUS returned. When you return portions of bytes must respond with code "206" to indicate that the content has been returned yet only in part.

Other related http headers:

In relation to the issue of the "download parts" and the related header range header there are others that may be useful in dealing with these issues:

  • Accept-Ranges: header sent by a server can communicate to the client type range can handle
  • Content-Range:It is sent from the server to indicate the range returned in proportion to the total number of bytes to be returned (used in servlet code!)
  • If-Range:Need to manage the cache of the client when certain portions were already in the cache.


Iphone Specifically.

In the special case of this device, the application of a 3gp download occurs in two stages:
Nella prima il dispositivo fà una normale richiesta proponendo come userAgent quello classi del browser safari: In the first the device makes a normal request proposing that as UserAgent classes safari browser:

Mozilla/5.0 (iPhone; U; CPU iPhone OS 2_2 like Mac OS X; it-it) AppleWebKit/525.18.1 (KHTML, like Gecko) Version/3.1.1 Mobile/5G77 Safari/525.20

Later, once the browser understands that this is a downloadable video 3gp streaming, the client takes over the quicktime that making requests with the range header.
In quest'ultimo caso lo userAgent della richiesta è: In the latter case, the UserAgent of the request is:

Apple iPhone OS v2.1 CoreMedia v1.0.0.5F136


Conclusion:

This article does not cover all aspects range header, as for example, lack of support for end-range (eg range: bytes =- 500)
E' comunque un punto di partenza per capire e implementare il download per parti! It 'still a starting point to understand and implement the download to go!

Per approfondire è possibile dare un occhio alla default servlet di glassfish, ovvero quella servlet che si occupa di servire contenuti statici presenti nelle directory del server. For more you can give a look at the default servlet Glassfish, namely servlet that deals with serving static content in the directory server.
Tale servlet soddisfa le specifiche http. This meets the specific servlet http.