Monday, May 24, 2004

Using java comm api

This is a sample java program that reads call information from the EPBX port and dumps it inot an excel sheet.
/*
* @(#)ReadCall.java 1.12 98/06/25
*

*/

import java.io.*;
import java.util.*;
import javax.comm.*;

public class ReadCall implements Runnable, SerialPortEventListener {
static CommPortIdentifier portId;
static Enumeration portList;

InputStream inputStream;
SerialPort serialPort;
Thread readThread;

public static void main(String[] args) {
portList = CommPortIdentifier.getPortIdentifiers();

while (portList.hasMoreElements()) {
portId = (CommPortIdentifier) portList.nextElement();
if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL) {
System.out.println(" I am here" + portId.getName());
if (portId.getName().equals("COM1")) {
//if (portId.getName().equals("/dev/term/a")) {
ReadCall reader = new ReadCall();
}
}
}
}

public ReadCall() {
try {
serialPort = (SerialPort) portId.open("ReadCallApp", 2000);
} catch (PortInUseException e) {
System.out.println(" Port in use caught");
}
try {
inputStream = serialPort.getInputStream();
} catch (IOException e) {}
try {
serialPort.addEventListener(this);
} catch (TooManyListenersException e) {}
serialPort.notifyOnDataAvailable(true);
try {
// serialPort.setSerialPortParams(9600,
serialPort.setSerialPortParams(1200,
SerialPort.DATABITS_8,
SerialPort.STOPBITS_1,
SerialPort.PARITY_NONE);
} catch (UnsupportedCommOperationException e) {}
readThread = new Thread(this);
readThread.start();
}

public void run() {
try {
Thread.sleep(20000);
} catch (InterruptedException e) {}
}

public void serialEvent(SerialPortEvent event) {
switch(event.getEventType()) {
case SerialPortEvent.BI:
case SerialPortEvent.OE:
case SerialPortEvent.FE:
case SerialPortEvent.PE:
case SerialPortEvent.CD:
case SerialPortEvent.CTS:
case SerialPortEvent.DSR:
case SerialPortEvent.RI:
case SerialPortEvent.OUTPUT_BUFFER_EMPTY:
break;
case SerialPortEvent.DATA_AVAILABLE:
byte[] readBuffer1 = new byte[22];
byte[] readBuffer2 = new byte[38];
byte[] readBuffer3 = new byte[23];
byte[] readBuffer4 = new byte[1];
byte[] readBuffer5 = new byte[16];
byte[] readBuffer6 = new byte[20];
int numBytes = 0;

try {
while (inputStream.available() > 0) {
numBytes = inputStream.read(readBuffer1);
numBytes = inputStream.read(readBuffer2);
numBytes = inputStream.read(readBuffer3);
numBytes = inputStream.read(readBuffer4);
numBytes = inputStream.read(readBuffer5);
numBytes = inputStream.read(readBuffer6);
//System.out.println("Done with x bytes \n" + numBytes);
}
System.out.print(" 1" + new String(readBuffer1));
System.out.print(" 2" + new String(readBuffer2));
System.out.print(" 3" + new String(readBuffer3));
System.out.print(" 4" + new String(readBuffer4));
System.out.print(" 5" + new String(readBuffer5));
System.out.print(" 6" + new String(readBuffer6));
System.out.println("after while Done with x bytes \n" + numBytes);
} catch (IOException e) {}
break;
}
}
}

Basic Security - securing a web service

Ok once you have deployed a web service how do you make it secure.
Put the following snippet in the web.xml file

"
BASIC
Greeting Service


GreetingRole



Greeting Web Service
*


GreetingRole


NONE

"

just before the end tag ""

And in the orion-web.xml file, put the following xml snippet:





Now you are all set. Just deploy the web service again and you would be prompted to provide a user id and password.



As for the java client accessing the wsdl :
In the auto generated constructor, assuming u r using jdeveloper to create and deploy your web services: just provide the properties to the http connection.

Properties props = new Properties();
props.put(OracleSOAPHTTPConnection.AUTH_TYPE, "basic");
props.put(OracleSOAPHTTPConnection.USERNAME, "admin");
props.put(OracleSOAPHTTPConnection.PASSWORD, "welcome");
m_httpConnection.setProperties(props);

OC4J basics

Un deploying web services on OC4J can be a simple command:
java -jar C:\oc4j\j2ee\home\admin.jar ormi://localhost admin welcome
-undeploy

Deployed web services are copied as ear files in the j2ee/home/applications directory and expanded in applications-deployment as any other app server. (META-INF is scrapped from the directory though in deployments directory)