Friday, January 18, 2008

Kullandığım firefox eklentileri


  1. del.icio.us buttons.
    İnternette surf yaparken beğendiğim sayfaları del.icio.us profilime eklemek için kullanıyorum. http://del.icio.us/ilkinulas

  2. AdBlockPlus
    Olmazsa olmaz bir plugin.

  3. Tab Mix Plus
    Firefox'un tab özelliğine ilave özellikler ekliyor. Yanlışlıkla kapattığınız bir tab'ı yeniden açmak gibi.

  4. Web Developer Plugin
    Özellikleri saymakla bitmez. Web'de geliştirme yapıyorsanız çok işinize yaracak bir plugin. En beğendiğim özellikleri:
    cookie arayüzü ve sayfa içindeki bütün HTML elemanlarını açıp kapayabilme özelliği.

  5. Firebug
    Özellikle Ajax ile geliştirme yapanlar için çok kullanışlı bir plugin. Firefox'un asenkron yaptığı HTTP request'leri firebug console'dan takip edebilirsiniz.

  6. UnPlug
    Bu plugin'i Youtube videolarını bilgisayarınıza indirmek için kullanabilirsiniz.

  7. YSlow
    Firebug plugin'ine ek olarak kullanılan bu plugin'i Yahoo geliştirmiş. We sayfasını yahoo'nun belirlediği kriterlere göre sınıyor. Bir nevi performans testi uyguluyor.

  8. Greasemonkey
    Bu plugin kısaca, sayfa yüklendikten sonra sayfada post-processing yaparak sayfanın yeniden yapılandırılmasını sağlar. Sadece greasemonkey plugin'ini kurmak yetmez. Greasemonkey sitesinden "user script" denen betikleri kurmanız gerekmektedir.

Sunday, January 13, 2008

A Tutorial on XML Namespaces


XML namespaces are used to avoid element name conflicts. A namespace is a set of names in which all names are unique. An XML namespace can be tought as a Java package name. To distinguish two classes of the same name we put the classes in two different packages. For example; you may choose to put a class that is used to connect to a relational database system in com.foo.sql package (com.foo.sql.Connection) and a class that manages TCP/IP socket connections in com.foo.net package (com.foo.net.Connection).

An XML namespace is used in the same way. Namespaces make it easier to come up with unique names. Without namespaces all XML element names must be unique all over the world. I'll try to explain this with an example.

Xml representation of a java class.

<class>
<name>com.foo.bar.Customer</name>
<method>getName</method>
<method>setName</method>
<superclass>com.foo.bar.Person</superclass>
</class>

Xml representation of a college class.

<class>
<code>MATH101E</code>
<name>Mathematics 1</name>
<language>English</language>
<credits>4</credits>
<class>

If these two "class" XML elements are used in the same XML document there will a name conflict. These name conflicts can be resolved by using namespaces.

<j:class xmlns:j="http://java.sun.com/class">
<j:name>com.foo.bar.Customer</j:name>
<j:method>getName</j:method>
<j:method>setName</j:method>
<j:superclass>com.foo.bar.Person</j:superclass>
</j:class>

<itu:class xmlns:itu="http://java.sun.com/class">
<itu:code>MATH101E</itu:code>
<itu:name>Mathematics 1</itu:name>
<itu:language>English</itu:language>
<itu:credits>4</itu:credits>
</itu:class>

ITU : Istanbul Technical University

XML namespaces are defined with XML Namespace (xmlns) Attribute. The XML namespace attribute is placed in the start tag of an element and has the following syntax:

xmlns:namespace-prefix="namespaceURI"

When a namespace is defined in the start tag of an element, all child elements with the same prefix are associated with the same namespace.
If this tutorial is not enough for you can read these articles:
  • Xml namespaces from w3schools
  • Understanding XML Namespaces from Micro$oft MSDN library
  • XML Namespaces by Example from xml.com
  • Xml namespaces from wikipedia
  • XML Schema: Understanding Namespaces from Oracle

  • Thursday, December 6, 2007

    Kill A Process By Name

    My favorite shell command is kill. Don't worry I'm not a serial killer.
    If you know the process id and if you have enough permissions you can kill any process you want . It is easier to kill a process by name. Here is the command.

    kill -9 `ps -ef | grep $1 | grep -v grep | awk '{print $2}'`


    kill -9 processId sends SIGKILL signal to specified process. You can use a list of process ids as kill command argument. kill -9 pid1 pid2 pid3
  • ps -ef|grep $1
    Lists process that matches $1
  • ps -ef | grep $1 | grep -v grep
    List process that matches $1 except the process that runs grep command
  • ps -ef | grep $1 | grep -v grep | awk '{print $2}
    Lists only process ids of processes that matches $1 except the process that runs grep command.

    awk '{print $2}' lists the second column of the
    ps -ef | grep $1 | grep -v grep 
    command output.

    Save kill -9 `ps -ef | grep $1 | grep -v grep | awk '{print $2}'` in a file called myKill.sh
    Give execute permission to the myKill.sh file (chmod +x myKill.sh)
    If you want to kill all java processes type
    ./myKill.sh java
    Or you can use pgrep and pkill commands if they are available on your system.
  • Tuesday, December 4, 2007

    Watch multiple log files with "tail"

    Log files are the first place i look while debugging an application. To see the logs in real-time I am using the utility command "tail" with the "-f" option.

    tail -f filename

    When there are multiple log files to watch out, I used to open different xterm console to tail each file.But today I learned that "tail" can watch multiple files for changes. Example command and its output on my PC (Ubuntu 7.10).


    tail -f logs/catalina.out InaWs.log InaWsUserActions.log

    ==> logs/catalina.out <==
    [2007-12-04 09:39:18,746] INFO(ContextManager.java.initialize:56) - Context [vpn] initialized.
    [2007-12-04 09:39:18,760] INFO(ContextManager.java.initialize:56) - Context [prepaid] initialized.
    [2007-12-04 09:39:18,761] INFO(InaWsImpl.java.initialize:116) - Using auhentication factory : com.oksijen.inox.common.ws.auth.PlaintextAuthenticationFactory
    Dec 4, 2007 9:39:19 AM com.sun.xml.ws.transport.http.servlet.WSServletDelegate
    INFO: WSSERVLET14: JAX-WS servlet initializing

    ==> InaWs.log <==
    [2007-12-04 09:39:18,743] INFO [ContainerBackgroundProcessor[StandardEngine[Catalina]]] (InaWsContext.java.addAction:41) - Adding action. actionName[echoVpn]...
    [2007-12-04 09:39:18,744] INFO [ContainerBackgroundProcessor[StandardEngine[Catalina]]] (C3p0Impl.java.initialize:53) - Initializing C3P0 connection pool....
    [2007-12-04 09:39:18,757] INFO [ContainerBackgroundProcessor[StandardEngine[Catalina]]] (InaWsContext.java.addAction:41) - Adding action. actionName[echoPrepaid]...
    [2007-12-04 09:39:18,758] INFO [ContainerBackgroundProcessor[StandardEngine[Catalina]]] (C3p0Impl.java.initialize:53) - Initializing C3P0 connection pool....

    ==> InaWsUserActions.log <==
    20071204093501054 REQ 117787e671a35c5c03e072efbd53bad3cea0ae49 root prepaid echoPrepaid [test]
    20071204093501054 RES 117787e671a35c5c03e072efbd53bad3cea0ae49 1
    20071204093520876 REQ 117787e671a35c5c03e072efbd53bad3cea0ae49 root vpn echoVpn [test]
    20071204093520876 RES 117787e671a35c5c03e072efbd53bad3cea0ae49 1
    20071204093530380 REQ 117787e671a35c5c03e072efbd53bad3cea0ae49 root mpbx echoMpbx []
    20071204093546634 REQ 117787e671a35c5c03e072efbd53bad3cea0ae49 root mpbx listAgents []
    20071204093549223 RES 117787e671a35c5c03e072efbd53bad3cea0ae49 100101

    Monday, November 12, 2007

    My favorite shell commands

    The below one line shell script calculates the number of executions of commands and sorts the result.

    history|awk '{print $2}'|awk 'BEGIN {FS="|"} {print $1}'|sort|uniq -c|sort -rg

    Here is the result of this script on my Ubuntu.

    66 ls
    40 cd
    32 sudo
    32 mvn
    31 ps
    29 clear
    26 more
    22 vi
    19 tail
    18 kill
    16 ssh
    13 rm
    13 eclipse
    11 tomcat_start.sh
    10 ll
    10 find
    9 locate
    9 findJar
    6 ifconfig
    5 ln

    Most frequently used command is "ls". I was surprised to see "mvn" in the 4th place. As you can see I am developing (eclipse), testing-packaging (mvn) and deploying (tomcat_start.sh) web applications written in java these days. I have also "kill"ed a serious number of processes :)

    Try this script to see what commands you use frequently.

    Thursday, October 18, 2007

    Sınır Ötesi Operasyon

    Son günlerde gündemi meşgul eden en önemli haber TBMM'nin hükümete sınır ötesi harekat için yetki vermesi. Sınır ötesi operasyon gerekli mi gereksiz mi tartışmaları sürerken gerçekler gözden kaçırılıyor. TBMM'de bizi temsil eden millet vekilleri arasında PKK'yı bölücü teror örgütü olarak tanımayan millet vekilleri var. Durum böyleyken bizim operasyonu sınır ötesinde değil öncelikle kendi millet meclisimiz içinde yapmamız gerekir. Türkiye Cumhuriyeti vatandaşlarını, devletin bölünmez bütünlüğüne zarar vermek için acımasızca öldüren, binlerce çocuğu anasız babasız bırakan eli kanlı bu terör örgütünü "TERÖR ÖRGÜTÜ" olarak adlandıramayan millet vekilleri meclis çatısı altında nasıl barınır aklım almıyor. Demokrasi böyle birşey olsa gerek.

    Blogged with Flock

    Thursday, October 4, 2007

    Volatile Variables In JAVA



    Java Memory model defines the possible rules for threads and the expected behavior of multi-threaded programs so that programmers can design their programs accordingly. JSR-133 describes the semantics of threads, locks, volatile variables and data races.

    Volatile variables are treated different from other variables. When you mark a variable as volatile, this warns the compiler to get fresh copies of the variable rather than caching the variable in the registers.

    Volatile variables can be used to store shared variables at a lower cost than that of synchronization, but they have limitations. While writes to volatile variables are guaranteed to be immediately visible to other threads (because JVM does not allow threads to cache volatile variables), there is no way to render a read-modify-write sequence of operations atomic, meaning, for example, that a volatile variable cannot be used to reliably implement a mutex (mutual exclusion lock) or a counter.

    Let's demonstrate this with a counter example. In the example a volatile counter variable will be incremented by multiple threads simultaneously. Below is the main class that holds the volatile variable and starts the threads.



    01 package com.oksijen.concurrency.volatiletest;
    02 
    03 import java.util.concurrent.CountDownLatch;
    04 
    05 public class VolatileTest {
    06 
    07     public static volatile long counter = 0L;
    08     private CountDownLatch allThreadsAreDone = null;;
    09     private CountDownLatch allThreadsAreReady = null;
    10     
    11     public static void main(String[] args) {
    12         VolatileTest test = new VolatileTest();
    13         int numberOfThreads = 10;
    14         int incrementCountPerThread = 10000;
    15         CountDownLatch countDownLatch1 = new CountDownLatch(numberOfThreads);
    16         test.setAllThreadsAreDone(countDownLatch1);
    17         CountDownLatch countDownLatch2 = new CountDownLatch(numberOfThreads);
    18         test.setAllThreadsAreReady(countDownLatch2);
    19         test.start(numberOfThreads, incrementCountPerThread);
    20         test.waitForThreadTermination();
    21         System.out.println("Expected counter value : " + numberOfThreads * incrementCountPerThread);
    22         System.out.println("Result : " + VolatileTest.counter);
    23     }
    24     /**
    25      * prints out result after all threads finish execution
    26      */
    27     private void waitForThreadTermination() {
    28         try {
    29             System.out.println(Thread.currentThread().getName() " waiting for other threads...");
    30             this.allThreadsAreDone.await();
    31             System.out.println(Thread.currentThread().getName() " threads finished their work.");
    32         catch (InterruptedException e) {
    33             e.printStackTrace();
    34         }
    35     }
    36     /**
    37      @param numberOfThreads
    38      @param incrementCount
    39      */
    40     private void start(int numberOfThreads, int incrementCount) {
    41         for (int i=0; i<numberOfThreads; i++) {
    42             MyThread thread = new MyThread(incrementCount, this.allThreadsAreDone, this.allThreadsAreReady);
    43             thread.setName("MyThread_"+i);
    44             thread.start();
    45             this.allThreadsAreReady.countDown();
    46         }
    47     }
    48     /**
    49      * synchronized counter increment.
    50      */
    51     public static synchronized void incrementCounter(){
    52         counter++;
    53     }
    54     /**
    55      @param allThreadsAreDone the allThreadsAreDone to set
    56      */
    57     public void setAllThreadsAreDone(CountDownLatch allThreadsAreDone) {
    58         this.allThreadsAreDone = allThreadsAreDone;
    59     }
    60     /**
    61      @param allThreadsAreReady the allThreadsAreReady to set
    62      */
    63     public void setAllThreadsAreReady(CountDownLatch allThreadsAreReady) {
    64         this.allThreadsAreReady = allThreadsAreReady;
    65     }
    66     
    67 }


    Here is our example thread source code:



    01 package com.oksijen.concurrency.volatiletest;
    02 
    03 import java.util.concurrent.CountDownLatch;
    04 
    05 public class MyThread extends Thread {
    06 
    07     private int incrementCount = 0;    
    08     private CountDownLatch done = null;
    09     private CountDownLatch ready = null;
    10     
    11     public MyThread(int incrementCount, CountDownLatch done, CountDownLatch ready){
    12         this.incrementCount = incrementCount;
    13         this.done = done;
    14         this.ready = ready;
    15     }
    16     
    17     public void run() {
    18         try {
    19             System.out.println(Thread.currentThread().getName() " started.");
    20             this.ready.await();
    21             for (int i=0; i<this.incrementCount; i++) {
    22                 VolatileTest.counter++;
    23                 /*
    24                  * if we want to increment the counter synchronously we use the
    25                  * sync incrementCounter() method.
    26                  
    27                  * VolatileTest.incrementCounter();
    28                  */  
    29             }
    30             System.out.println(Thread.currentThread().getName() " ended.");
    31             this.done.countDown();            
    32         catch (InterruptedException e) {
    33             e.printStackTrace();
    34         }
    35     }
    36 }


    I used jdk1.5.0_12 to run this application. I used 10 threads, each increments the variable 10000 times. The result is as expected. Volatile keyword does not guarantie mutual exclusion. Here is the result:

    Expected counter value : 100000
    Result : 85751
    As a result: Volatile variables are best for use when there is only one thread that makes writes (updates volatile variable) and N threads that make reads. Reader threads get the most recent value of the volatile parameter when they perform a read.