Pages

Wednesday, August 6, 2008

Workshop Distribution System

Pada tanggal 5-6 Agustus 2008 diadakan pelatihan Sisterm Terdistribusi dengan nara sumber dari Universitas Indonesia Bpk. Rizal, materi yang disampaikan mulai dari pengertian sister itu sendiri sampai praktikum simulasi Corba dengan menggunakan Java.

Lebih jauh tentang CORBA dapat dilihat di http://java.sun.com/developer/onlineTraining/corba/corba.html .

Contoh aplikasi yang di praktekkan adalah aplikasi akademik berikut ini:
Langkah 1 :
Membuah IDL file dengan nama udinus.idl, source code nya :
module udinus {

struct Mahasiswa {
string nama;
string nomor;
string jurusan;
string angkatan;
string status;
double IPK;
};

struct Kuliah {
string kode;
string nama;
string dosen;
};

interface SistemMahasiswa {
Mahasiswa getMahasiswa(in string nomor);
string getStatusMahasiswa(in string nomor);
double getIPK(in string nomor);
double setIPK(in double IPK);
};

interface SistemKuliah {
Kuliah getKuliah(in string kode);
long SKSMahasiswa(in string nim);
};
};


Langkah 2 :
Membuat file Database.java :
import java.sql.*;
import javax.sql.*;

public class Database {
private static final String accessDBURLPrefix = "jdbc:odbc:Driver={Microsoft Access Driver (*.mdb)};DBQ=";
private static final String accessDBURLSuffix = ";DriverID=22;READONLY=false}";

// Initialize the JdbcOdbc Bridge Driver
static {
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
} catch(ClassNotFoundException e) {
System.err.println("JdbcOdbc Bridge Driver not found!");
}
}

/** Creates a Connection to a Access Database */
public static Connection getAccessDBConnection(String filename) throws SQLException {
filename = filename.replace('\\', '/').trim();
String databaseURL = accessDBURLPrefix + filename + accessDBURLSuffix;
return DriverManager.getConnection(databaseURL, "", "");
}

public static ResultSet doQuery(Connection conn, String query) throws Exception {
Statement stmt= conn.createStatement();
ResultSet rs=stmt.executeQuery(query);
return rs;
}
}


Langkah 3:
Membuat file SistemKuliah.java :
import udinus.*;
import java.sql.*;
import javax.sql.*;

public class SistemKuliah extends SistemKuliahPOA {

org.omg.CORBA.ORB theOrb;
public void setOrb(org.omg.CORBA.ORB theOrb) {
this.theOrb=theOrb;
}

public udinus.Kuliah getKuliah (String kode) {
try {
Connection conn =
Database.getAccessDBConnection("kuliah.mdb");
ResultSet rs=Database.doQuery
(conn,"select kode, nama, dosen from kuliah where kode='"+kode+"'");
Kuliah kul=new Kuliah();
while (rs.next()) {
kul.kode=(String)rs.getString(1);
kul.nama=(String)rs.getString(2);
kul.dosen=(String)rs.getString(3);
}
return kul;

} catch (Exception e) {
return null;
}
}

public int SKSMahasiswa (String nim) {
try {
Connection conn =
Database.getAccessDBConnection("kuliah.mdb");
ResultSet rs=Database.doQuery
(conn,"select sum(k.sks) from kuliahmahasiswa km, kuliah k where km.mahasiswa='"+nim+"' and km.kuliah=k.kode;");
int sks=0;
if (rs.next()) {
sks=rs.getInt(1);
}
return sks;

} catch (Exception e) {
return 0;
}
}

public static void main(String ar[]) {
try {
// Initialize the ORB.
org.omg.CORBA.ORB orb =
org.omg.CORBA.ORB.init(ar,null);

org.omg.PortableServer.POA rootPOA =
org.omg.PortableServer.POAHelper.narrow(orb.resolve_initial_references("RootPOA"));
rootPOA.the_POAManager().activate();

// Create a stock object.
SistemKuliah serv=
new SistemKuliah();

serv.setOrb(orb);
org.omg.CORBA.Object ref=rootPOA.servant_to_reference(serv);
udinus.SistemKuliah saRef=udinus.SistemKuliahHelper.narrow(ref);

org.omg.CORBA.Object objRef=orb.resolve_initial_references("NameService");

org.omg.CosNaming.NamingContextExt ncRef=org.omg.CosNaming.NamingContextExtHelper.narrow(objRef);

org.omg.CosNaming.NameComponent path[]=ncRef.to_name("SistemKuliah");
ncRef.rebind(path,saRef);

System.out.println("Sistem Kuliah Running");
orb.run();

} catch (Exception e) {
System.err.println(
"Stock server error: " + e);
e.printStackTrace(System.out);
}
}
}


Langkah 4 :
Membuat file Client.java :
import udinus.*;
import org.omg.CORBA.*;
import org.omg.CosNaming.*;


public class Client {
public static udinus.SistemKuliah getSistemKuliah(String namaObject, String args[]) throws Exception
{
try
{
// Create an object request broker
ORB orb = ORB.init(args, null);

// Obtain object reference for name service ...
org.omg.CORBA.Object object =
orb.resolve_initial_references("NameService");

// ... and narrow it to a NameContext
NamingContextExt namingContext =
NamingContextExtHelper.narrow(object);

udinus.SistemKuliah sa=udinus.SistemKuliahHelper.narrow(namingContext.resolve_str(namaObject));

return sa;

} catch (Exception e)
{
System.err.println ("CORBA error - " + e);
throw e;
}
}

public static void main(String ar[]) {
try
{
udinus.SistemKuliah sa=getSistemKuliah("SistemKuliah",ar);
System.out.println(sa.getKuliah("1102").nama);
}
catch (Exception e)
{
}
}
}


Langkah 5:
Compile semua file :
untuk udinus.idl perintah compile nya:

idlj -fall udinus.idl

selanjutnya akan terbentuk folder udinus beserta sekeleton file-file yang terentuk otomatis.

setelah selesai semua di compile dan tentu no error, saatnya menjalankan aplikasi client dengan urutan sebagai berikut :
1. jalankan tnameserv
2. jalankan SistemKuliah
3. jalankan Client

Nah sudah selesai deh tes CORBA nya.
Selamat mencoba, semoga bermanfaat.

0 comments:

Post a Comment

Popular Posts