public byte[] getDataInBytes() throws IOException {
final int BUFFER_SIZE = 1024 * 64;
byte[] fileInBytes = new byte[BUFFER_SIZE];
int read = 0;
int w = 0;
int len;
FTPInputStream ftpInStream = null;
try {
final String ftpPoolName = "FTPPoolName";
final String ftpFilePath = "****";
ftpInStream = new FTPInputStream(ftpPoolName, ftpFilePath, "fileName");
do {
w += read;
len = fileInBytes.length - w;
if (len <= 0) {
byte[] b = new byte[fileInBytes.length + BUFFER_SIZE];
System.arraycopy(fileInBytes, 0, b, 0, w);
fileInBytes = b;
len = fileInBytes.length - w;
}
} while ((read = ftpInStream.read(fileInBytes, w, len)) != -1);
} finally {
if (ftpInStream != null) {
ftpInStream.close();
}
}
if (fileInBytes.length > w) {
byte[] b = new byte[w];
System.arraycopy(fileInBytes, 0, b, 0, w);
fileInBytes = b;
}
return fileInBytes;
}
Monday, November 26, 2007
Thursday, May 24, 2007
Webservice using Weblogic
Webservice is a special service (ie., the service written in any programming language can be accessed from any client apllication written in any programming language) which is available in the web, accessed through some standard protocols.
Webservice Standards :
SOAP(Simple Object Access Protocol) protocal is used as a messaging protocol and HTTP is used as an connectin protocol when the client communicating( ie. transmit the data, service invocation call) the web service.
WSDL(WebService Description Language) is an XML speification used to describe the web service to the client. A WSDL document describes Webservice operations, I/O parameters, and how a client application connects to the Web Service.
JAX-RPC(JAVA API for XML based Remote Procedure Call) is used to invoke a webservice in client application.
UDDI(Universal Description, Discvery, and Integeration) is an universal registry used to register and find the service. It contains the details of the service.
Sample Application
1. Create the component
A component can be EJB component, Java Class, etc.. In this sample application we create a simple java class HelloWorld as component. Here the HelloWorld.java file
HelloWorld.java
package com.webservice.sample;
public class HelloWorld {
public String sayHello(final String user) {
return "Hello " + user + "...";
}
public long add(final long number1, final long number2) {
return number1 + number2;
}
}
2.Make the component as a webservice
To make the above class file as webservice we use the servicegen tool provided by weblogic. To setup the environment to create a webserivce component, we run the setWLSEnv.sh file, which is available in the bea installation folder.
cmd:\> setWLSEnv
cmd:\> ant -f build.xml webSerivceEar
After this we will get a deployable component(ws_basic_HelloWorld.ear).
3. Deploy the service
Deploy the .ear component in the weblogic server using the console window. From http://localhost:7001/console, You can access the console window of the bea app server which run on your machine, and deploy the component.
4. Register the service in the registry
Register the service in the UDDI registry. You can register the service and find the registered service, in the URL http://localhost:7001/uddiexplorer. While registering the service you should give the WSDL location (you will get this from the server), to make the service available to others. The WSDL path will be in the form of http://localhost:7001/WebServices<contextURI>/HelloWorld<serviceURI>?WSDL
5. Generate the Client stub to create the client application
By make use of the clientgen tool provided by weblogic, we can create a client stub from the WSDL. The client stub will be created only from the WSDL file, Thats why the WSDL is registered in the UDDI registry.
cmd:\> ant -f build.xml clientStub
After this we will get a clientStub(clientService.jar).
6. Create sample Client application
We create the clientApplication from the clientStub. Here the client application
SampleClient.java
import java.io.IOException;
import javax.xml.rpc.ServiceException;
import webserapp.client.HelloWorld;
import webserapp.client.HelloWorldPort;
import webserapp.client.HelloWorld_Impl;
class SampleClient {
public static void main(String[] args) throws IOException, ServiceException {
String wsdlString = "http://localhost:7001/WebServices/HelloWorld?WSDL";
HelloWorld service = new HelloWorld_Impl(wsdlString);
HelloWorldPort port = service.getHelloWprldPort();
System.out.println(port.sayHello("NAME"));
System.out.println(port.add(100,200));
}
}
Compile and run the client SampleClient.
build.xml
<project name="buildWebservice" default="ear">
<target name="ear">
<servicegen
destEar="ws_basic_helloWorld.ear"
contextURI="WebServices" >
<service
javaClassComponents="com.webservice.sample.HelloWorld"
targetNamespace="http://www.****.com/webservices/basic/helloWorld"
serviceName="HelloWorld"
serviceURI="/HelloWorld"
generateTypes="True"
expandMethods="True"
style="rpc" >
</service>
</servicegen>
</target>
<target name="clientStub">
<clientgen wsdl="http://localhost:7001/WebSservices/HelloWorld?WSDL"
packageName="webserapp.client"
clientJar="clientService.jar">
</clientgen>
</target>
</project>
Webservice Standards :
SOAP(Simple Object Access Protocol) protocal is used as a messaging protocol and HTTP is used as an connectin protocol when the client communicating( ie. transmit the data, service invocation call) the web service.
WSDL(WebService Description Language) is an XML speification used to describe the web service to the client. A WSDL document describes Webservice operations, I/O parameters, and how a client application connects to the Web Service.
JAX-RPC(JAVA API for XML based Remote Procedure Call) is used to invoke a webservice in client application.
UDDI(Universal Description, Discvery, and Integeration) is an universal registry used to register and find the service. It contains the details of the service.
Sample Application
1. Create the component
A component can be EJB component, Java Class, etc.. In this sample application we create a simple java class HelloWorld as component. Here the HelloWorld.java file
HelloWorld.java
package com.webservice.sample;
public class HelloWorld {
public String sayHello(final String user) {
return "Hello " + user + "...";
}
public long add(final long number1, final long number2) {
return number1 + number2;
}
}
2.Make the component as a webservice
To make the above class file as webservice we use the servicegen tool provided by weblogic. To setup the environment to create a webserivce component, we run the setWLSEnv.sh file, which is available in the bea installation folder.
cmd:\> setWLSEnv
cmd:\> ant -f build.xml webSerivceEar
After this we will get a deployable component(ws_basic_HelloWorld.ear).
3. Deploy the service
Deploy the .ear component in the weblogic server using the console window. From http://localhost:7001/console, You can access the console window of the bea app server which run on your machine, and deploy the component.
4. Register the service in the registry
Register the service in the UDDI registry. You can register the service and find the registered service, in the URL http://localhost:7001/uddiexplorer. While registering the service you should give the WSDL location (you will get this from the server), to make the service available to others. The WSDL path will be in the form of http://localhost:7001/WebServices<contextURI>/HelloWorld<serviceURI>?WSDL
5. Generate the Client stub to create the client application
By make use of the clientgen tool provided by weblogic, we can create a client stub from the WSDL. The client stub will be created only from the WSDL file, Thats why the WSDL is registered in the UDDI registry.
cmd:\> ant -f build.xml clientStub
After this we will get a clientStub(clientService.jar).
6. Create sample Client application
We create the clientApplication from the clientStub. Here the client application
SampleClient.java
import java.io.IOException;
import javax.xml.rpc.ServiceException;
import webserapp.client.HelloWorld;
import webserapp.client.HelloWorldPort;
import webserapp.client.HelloWorld_Impl;
class SampleClient {
public static void main(String[] args) throws IOException, ServiceException {
String wsdlString = "http://localhost:7001/WebServices/HelloWorld?WSDL";
HelloWorld service = new HelloWorld_Impl(wsdlString);
HelloWorldPort port = service.getHelloWprldPort();
System.out.println(port.sayHello("NAME"));
System.out.println(port.add(100,200));
}
}
Compile and run the client SampleClient.
build.xml
<project name="buildWebservice" default="ear">
<target name="ear">
<servicegen
destEar="ws_basic_helloWorld.ear"
contextURI="WebServices" >
<service
javaClassComponents="com.webservice.sample.HelloWorld"
targetNamespace="http://www.****.com/webservices/basic/helloWorld"
serviceName="HelloWorld"
serviceURI="/HelloWorld"
generateTypes="True"
expandMethods="True"
style="rpc" >
</service>
</servicegen>
</target>
<target name="clientStub">
<clientgen wsdl="http://localhost:7001/WebSservices/HelloWorld?WSDL"
packageName="webserapp.client"
clientJar="clientService.jar">
</clientgen>
</target>
</project>
Tuesday, April 17, 2007
Returning oracle table object
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
Returning table object from oracle procedure or function
Creating Package and Package body
CREATE OR REPLACE PACKAGE PKG_SAMPLE AS
TYPE TY_TAB_OBJECT IS TABLE OF VARCHAR2(100);
PROCEDURE PR_SAMPLE(TY_TAB_OBJECT_INST OUT TY_TAB_OBJECT);
END PKG_SAMPLE;
/
CREATE OR REPLACE PACKAGE BODY PKG_SAMPLE AS
PROCEDURE PR_SAMPLE(TY_TAB_OBJECT_INST OUT TY_TAB_OBJECT) IS
EMP_OBJ VARCHAR2(100);
BEGIN
TY_TAB_OBJECT_INST := TY_TAB_OBJECT();
FOR SAMPLE_CURSOR IN (SELECT EMPID, EMPNAME, SALARY FROM EMPLOYEE
WHERE DEPTID IN ('0004','0005','0007')
ORDER BY EMPID)
LOOP
TY_TAB_OBJECT_INST.EXTEND;
EMP_OBJ := SAMPLE_CURSOR.EMPID ' -> ' SAMPLE_CURSOR.EMPNAME ' -> ' SAMPLE_CURSOR.SALARY;
TY_TAB_OBJECT_INST(TY_TAB_OBJECT_INST.LAST) := EMP_OBJ;
END LOOP;
END PR_SAMPLE;
END PKG_SAMPLE;
/
Ananymous block which is used to extract the data from the table object returned by the oracle procedure,
DECLARE
TY_TAB_OBJECT_INST PKG_SAMPLE.TY_TAB_OBJECT;
BEGIN
PKG_SAMPLE.PR_SAMPLE(TY_TAB_OBJECT_INST);
IF TY_TAB_OBJECT_INST.COUNT > 0 THEN
FOR I IN TY_TAB_OBJECT_INST.FIRST..TY_TAB_OBJECT_INST.LAST
LOOP
DBMS_OUTPUT.PUT_LINE(TY_TAB_OBJECT_INST(I));
END LOOP;
END IF;
END;
/
Subscribe to:
Posts (Atom)