Friday, December 17, 2010

Old Post - asterisk for java - installation

07-05-2008

asterisk for java - installation

Now a days i am playing with Asterisk, and now I needed to integrate a Java based application with asterisk.

I started searching, and found about JAsterisk, but later i found that JAsterisk was made with Asterisk 1.0.0 while I am running with Asterisk 1.4, and 1.2 installations.
Then I found 
Asterisk for Java ; this package is built on top of Maven [a project management API for Linux],  the pre-requisites were as follows

1. JDK above version 1.5
2. Maven

Now, JDK has a significant style of it :) ... but As I am a .net developer I'll say it sucks....I tumbled a lot with installation sof JDK's and JVM' and environment variables, let me simplify it for you....

1. Download JDK from 
http://java.sun.com/javase/downloads/   you need to download the self extracting binary .. with a name somewhat like this jdk-6u6-linux-i586-rpm.bin
2. Copy it into a directory; I copied it in /opt , extracted it there by command
./jdk-6u6-linux-i586-rpm.bin

Note - it should install automatically, but if it doesn't or it says some Java Package is already installed you'll have to remove it
yum remove <packagetoremove>

3. Now we need to create environment variables
My JAVA is installed in directory /usr/java/jdk1.6.0_06
this is how you can persist your Java environment variables for good in your linux machine, be it on root OR on user
           open the directory /etc
          Find the file profile open it type these lines, one for PATH, one for JAVA_HOME, and one for CLASSPATH
Note - remove my comments from here before copy pasting these lines, also put them at end of file
       
//for the directory path where JAVA imnstallation resides
export JAVA_HOME=/usr/java/jdk1.6.0_06            

//For the path where compiler resides, the bin dir contains javac which is the compiler
export PATH=PATH=$PATH:/usr/java/jdk1.6.0_06/bin

//For the PATH where classes may reside,m in our case they may reside in any directory
export CLASSPATH=$CLASSPATH:.

Now, you should restart your machine, raise the terminal window [command prompt] and cross check
type these commands, and you may see the results like this
[root@localhost ~]# java -version
java version "1.7.0"
IcedTea Runtime Environment (build 1.7.0-b21)
IcedTea Client VM (build 1.7.0-b21, mixed mode)
[root@localhost ~]# which java
/usr/bin/java
[root@localhost ~]# which javac
/usr/bin/javac
[root@localhost ~]# echo $JAVA_HOME
/usr/java/jdk1.6.0_06
[root@localhost ~]# echo $PATH
/usr/local/apache-maven-2.0.9/bin:PATH=/usr/local/apache-maven-2.0.9/bin:PATH=/usr/local/apache-maven-2.0.9/bin:PATH=/usr/kerberos/sbin:/usr/kerberos/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/usr/X11R6/bin:/usr/java/jdk1.6.0_06/bin:/usr/java/jdk1.6.0_06/bin:/root/bin:/usr/java/jdk1.6.0_06/bin
[root@localhost ~]# echo $CLASSPATH
:.:.:.

Ok , Now lets get the runtime of Asterisk for Java, As I don't have any plans to play with source of asterisk for Java

download the Asterisk-java-03.jar from http://sourceforge.net/projects/asterisk-java/

for testing create a directory and copy this Asterisk-java-03.jar file in it
Now create a file HelloAGIScript.java and copy paste this code

import org.asteriskjava.fastagi.AgiChannel;
import org.asteriskjava.fastagi.AgiException;
import org.asteriskjava.fastagi.AgiRequest;
import org.asteriskjava.fastagi.BaseAgiScript;

public class HelloAGIScript extends BaseAgiScript
{
    public void service(AgiRequest request, AgiChannel channel)
            throws AgiException
    {
        // Answer the channel...
        answer();
                
        // ...say hello...
        streamFile("tt-monkeysintro");
                
        // ...and hangup.
        hangup();
    }
}


Save it in the same folder, I have saved both of these files at /root/MyAgiScript, now go to terminal window, and compile it
[root@localhost MyAgiScripts]# javac -g -cp asterisk-java-0.3.jar HelloAGIScript.java
Now, create a file fastagi-mapping.properties copy and paste this code
//for mapping the agi script to the Java code we created
hello.agi = HelloAGIScript 

now go to your dialplan in asterisk  /etc/asterisk/extensions..conf
I have a dialplan like this, which I can dial from a SIP phone

[sip]
;
exten => 1,1,Answer()
exten => 1,2,Wait(2)
exten => 1,3,Record(asterisk-recording%d:ulaw)
exten => 1,4,Wait(2)
exten => 1,5,Playback(${RECORDED_FILE}) 
exten => 1,6,Agi(agi://localhost/hello.agi)
exten => 1,6,Hangup()


Now open another terminal window, and run your AGI server
java -cp asterisk-java-0.3.jar:. org.asteriskjava.fastagi.DefaultAgiServer


and it should run like this
May 7, 2008 5:38:29 PM org.asteriskjava.fastagi.DefaultAgiServer startup
INFO: Thread pool started.
May 7, 2008 5:38:30 PM org.asteriskjava.fastagi.DefaultAgiServer startup
INFO: Listening on *:4573.
May 7, 2008 5:39:25 PM org.asteriskjava.fastagi.DefaultAgiServer startup
INFO: Received connection from /127.0.0.1
May 7, 2008 5:39:25 PM org.asteriskjava.fastagi.ResourceBundleMappingStrategy loadResourceBundle
INFO: Added mapping for 'hello.agi' to class HelloAGIScript
May 7, 2008 5:39:25 PM org.asteriskjava.fastagi.internal.AgiConnectionHandler runScript
INFO: Begin AgiScript HelloAGIScript on Asterisk-Java DaemonPool-1-thread-1
May 7, 2008 5:39:25 PM org.asteriskjava.fastagi.internal.AgiConnectionHandler runScript
INFO: End AgiScript HelloAGIScript on Asterisk-Java DaemonPool-1-thread-1

This  output- comes when I dial a call from my sip-phone to the asterisk server; and it plays a sound file for me......

No comments:

There was an error in this gadget