Tre e mezzo [JJRumi’s blog]

October 30th, 2006

TFC - Ubuntu 6.06 & Tomcat 5

Posted by jjrumi in TFC

After doing some UML work, it’s time to install the application server. I’ve choosen Apache Tomcat.

INSTALLATION:

To install tomcat I used:
:~$ sudo apt-get install apache2 tomcat5 tomcat5-webapps tomcat5-admin

With apache2 and tomcat5 I installed the application server.
tomcat5-webapps includes documentation and examples (always useful to have something to look at).
tomcat5-admin provides a web interface for administration tasks.

To use the above command, one needs to add some extra repositories (Multiverse repository). Further information.

To start/restart Tomcat: :~$ sudo /etc/init.d/tomcat5 [start|restart]
Tomcat’s default configuration sets listening port to 8180, so acceding http://localhost:8180 will be enough.
In the page displayed is an important note:
“NOTE: For security reasons, using the administration webapp is restricted to users with role “admin”. The manager webapp is restricted to users with role “manager”. Users are defined in $CATALINA_HOME/conf/tomcat-users.xml.”

The examples (tomcat5-webapps) were installed at /usr/share/tomcat5/webapps/
/etc/default/tomcat5 includes some important variables (such as JAVA_HOME, CATALINA_BASE, LOGFILE_DAYS, etc…)
The configuration files (server.xml y web.xml) are located at /etc/tomcat5/
/var/lib/tomcat5/ is the base directory of my Tomcat installation and /var/lib/webapps/ is where the the web applications should be located.

FIRST WEB APPLICATION:
Now it’s time to add the first webapp.

Tomcat uses the WAR structure.

Standard Directory Layout –> Where are we playing today?

* /WEB-INF/web.xml –> This is an XML file describing the servlets and other components that make up your application.

* /WEB-INF/classes/ –> This directory contains any Java class files (and associated resources) required for your application.
It’s important to note that a Java class named com.mycompany.mypackage.MyServlet would need to be stored in a file named /WEB-INF/classes/com/mycompany/mypackage/MyServlet.class.

* /WEB-INF/lib/ –> This directory contains JAR files that contain Java class files required for your application.
If you want a JAR to be visible for all of your webapps, place it in /var/lib/tomcat5/shared/lib

We can use the webapp http://localhost:8180/manager/html for deploy our applications.

Further information for the different possibilities of this tool.

I hope this will be enough for today ;)

September 29th, 2006

Noam Chomsky

Posted by jjrumi in Digressing

First of all, the presentations: Noam Chomsky

I’ve read this afternoon a column in the ‘Wall Street Journal - Europe’ about Noam Chomsky. The author of the column was very critic with Chomsky, arguing more or less that ‘he’s biting the hand that feeds him’ (refering to America).

I don’t agree with the author, because he used an Utopia to despise Chomky’s efforts to criticize the American foreign policy (since the end of the WWII until now).

The author assured that in America exist an almost recursive liberty and self-criticism that helps everyone to improve (socially and humanly). That’s not true. Chomsky has been shut up in many newspapers, tv programs and so on… just because of what he says.

Aside from the Utopia, he based the other half of his speech in a lie: he sais everybody who isn’t American and is critic with it, want to be American…
I want to tell him neither the American way is the best one, nor the only one.

I must confess I truly enjoy Chomsky’s books, they are really interesting and lets you with a different point of view of our Occidental society.

All this interest in Chomsky comes about because of last Hugo Chaves‘ declarations at the last meeting of the United Nations in New York, where he mentioned Chomsky (recommending a book of him).
I’ll say Chomsky doesn’t need anybody to be known. He belongs to the intelligentzia of our century, and he’s well known because of his ideas.

But well…. WTF I know about America ;)

September 20th, 2006

Java - Instantiating an object dynamically

Posted by jjrumi in Java, Tips

If we ever need to instantiate a bunch of objects, and the order of instantiation needs to be set dynamically, we can take a good use of the class Class and its method forName which receives an String and returns a Class.
For example:

Class i = Class.forName(”java.lang.String”);
String x = (String)i.newInstance();

This would create an String’s instance in x.

But there’s more, you can use this with the Constructor class.

Let’s suppose we have two objects (A,B) implementing an interface like this:

interface IFigure{
  public void setName(String name);
}
class A implements IFigure{
  int idx;
  String name;

  public A(){ idx=20; }
  public A(int i){ idx=i; }
  public String toString(){return “Obj_A(”+idx+”): “+this.name;}
}

class B implements IFigure{
 int idx;
 String name;

 public B(){ idx=10; }
 public B(int i){ idx= (i>0) ? (i*3) : 0; //if i>0 then idx=i*3 else i=0 }
 public void setName(String name){this.name = name;}
 public String toString(){return “Obj_B(”+idx+”): “+this.name;}
}

and we want to instantiate them, we can use this:

//The classical way:
A cA = new A();
cA.setName(”Manolo”);
System.out.println(cA);

//A dynamic way:
try{
  Class generic = Class.forName(”A”);
  Constructor constructor[] = generic.getDeclaredConstructors();

  //Careful with the index of the array!!!!
  IFigure figObj = (IFigure) constructor[1].newInstance(new Object[0]); //equals new A();

  figObj.setName(”Manolo”);

  System.out.println(”A dynamic way: \n”+figObj);
}catch(Exception e){
  e.printStackTrace();
}

In the last example, I’ve used the method getDeclaredConstructors() which returns an array with all the declared constructors of the class without an order.
Because this is very annoying, we can use instead:

Constructor c = generic.getConstructor(new Class[]{int.class})

where we explicitly indicate which constructor we want to use.

Here is a whole example:

/**
* @author Juan Luis Jiménez Rumí (www.jjrumi.com/blog)
*
*/

import java.lang.*;
import java.lang.reflect.Constructor;

interface IFigure{
  public void setName(String name);
}

class A implements IFigure{
  int idx;
  String name;
  public A(){ idx=-999; }
  public A(int i){ idx=i; }
  public void setName(String name){ this.name = name; }
  public String toString() {return “Name_A: “+name+”. Idx: “+idx;}
}

class B implements IFigure{
  int idx;
  String name;
  public B(){ idx=-999; }
  public B(int i){
    idx= (i>0) ? (i*3) : 0; //if i>0 then idx=i*3 else i=0
  }
  public void setName(String name){ this.name = name; }
  public String toString() {return “Name_B: “+name+”. Idx: “+idx;}
}

public class test{

  String ArrayObjects[] = {”A”,”B”,”C”};

  public test(){
    IFigure figObj = null;
    Constructor constructor[] = null;
    Class generic =null;

    int flag=2;
    switch(flag){
      case 1:
        //The classical way:
        A cA = new A();
        cA.setName(”Manolo”);

        //A dynamic way (with same result):
        try{
          generic = Class.forName(ArrayObjects[0]);
          constructor = generic.getDeclaredConstructors();
          figObj = (IFigure) constructor[1].newInstance(new Object[0]); //equals: new A();
          figObj.setName(”Manolo”);
          System.out.println(”A classical way: \n”+cA+”\n”);
          System.out.println(”A dynamic way: \n”+figObj);
        }catch(Exception e){
          e.printStackTrace();
        }
      break;

      case 2:
        //The classical way:
        B cB = new B(23);
        cB.setName(”Manolo”);

        //A dynamic way (with same result):
        try{
          generic = Class.forName(ArrayObjects[1]);
          Constructor c = generic.getConstructor(new Class[]{int.class});
          //equals new B(23);
          figObj = (IFigure) c.newInstance(new Object[]{new Integer(23)});
          figObj.setName(”Manolo”);
          System.out.println(”A classical way: \n”+cB+”\n”);
          System.out.println(”A dynamic way: \n”+figObj);
        }catch(Exception e){
          e.printStackTrace();
        }
        break;
    }
  }

  public static void main(String args[]){
    try{
      //Just instantiating an object from the java.lang.String class and using its *default* constructor.
      Class i = Class.forName(”java.lang.String”);
      String x = (String)i.newInstance();
      x += “some text”;
      System.out.println(”First Example: “+x+”\n———-\n”);

      test t = new test();
    }catch(Exception e){
      e.printStackTrace();
    }
  }
}

Be mad!

September 16th, 2006

Java Sorting Arrays

Posted by jjrumi in Java, Tips

The Java API includes a prefabricated method for sorting arrays. According to the official documentation, this method is a tuned quicksort, so we don’t need to write one.One example:

Long longArray[] = new Long[10];
for(int i=0; i<longArray.length; i++){
    //just a random number between 0 - 9
    longArray[i] = new Long((new Double(Math.random()*i)).longValue());
}

Arrays.sort(longArray);

for(int i=0; i<longArray.length; i++){
   System.out.print(longArray[i].longValue()+”-”);

–> This will output a list of numbers sorted in an Ascending way, (like 0,1,2,3,4…).

If we want to sort it in a reverse order (Descending), we should use: Arrays.sort(longArray,java.util.Collections.reverseOrder());

That simple.

But it isn’t the most interesting part.
We can sort objects according to some of its attributes.
To sort objects, those objects must implements the Comparable interface. For example:


//Declaring the object
class IndexedFreq implements Comparable{
   public int idx;
   public int freq;

   public IndexedFreq(int idx, int freq){
      this idx = idx;
      this.freq = freq;
   }

   public int compareTo(Object obj){
      IndexedFreq idxfreq2 = (IndexedFreq )obj; //We receive the object to compare
      int f1 = freq; //and we can access to the second object which is compared
      int f2 = idxfreq2.freq

      //Here we decide which object must come first
      if(f1<f2){
         return -1;
      }else if(f2>f1){
         return 1;
      }
      return 0; //the freqs are equal
   }
}

//Using the object
public class UseIndexedFreq{
   public static void main(String args[]){
   IndexedFreq idxFreqArray[] = new IndexedFreq[10];
   for(int i=0; i<idxFreqArray.length; i++){
      idxFreqArray.idx = i;
      idxFreqArray.freq = (new Double(Math.random()*i)).intValue();
   }

   Arrays.sort(idxFreqArray,java.util.Collections.reverseOrder());
}

In the last example I’ve created an array of IndexedFreq objects, initializing them with an index (idx) and a random frequency (freq)
As shown above, the Arrays.sort(idxFreqArray,java.util.Collections.reverseOrder()); method will sort the array, in this case according to the freq value, in a Descending order.

Note that we can do the public int compareTo(Object obj) method as complex as we like. For example we could use a new attribute in the object to define if this object must have a freq=0 so it will be allocated at the begining/end of the array (depending of the order of the sort).

Next Page »

Creative Commons License
This work is licensed under a Creative Commons Attribution 2.5 License.