A Java function is another form of metadata that ALDSP can use as a data source. This is perhaps the most powerful metadata, because it allows ALDSP to utilize any data source that can be accessed from Java, such as Enterprise Java Beans, JMS/messaging applications, LDAP and other directory services, text/binary files that can be read through Java I/O, and even DCOM-based applications like Microsoft Excel.
In this lesson, you will access three data sources through Java functions:
WebLogic's embedded LDAP, by importing a Directory Service Markup Language (DSML)-based Java application as a Java function.
Data in a Microsoft Excel spreadsheet, by importing a Java application that uses JCOM to access the MS Excel spreadsheet.
An Enterprise Java Bean that returns customer credit card information using a Java function.
Objectives
After completing this lesson, you will be able to:
Write Java functions and access them from data services.
Overview
When you use ALDSP's Import Source Metadata feature to import user-defined Java functions, the functions are introspected to create the necessary method signatures and parameter metadata. At the same time, a prologue is created that defines the function's signatures and relevant schema type for complex elements such as Java classes and arrays.
In ALDSP, user-defined functions are treated as Java classes. The following are supported:
Java primitive types and single-dimension arrays, such as Boolean, byte, and char.
XMLBean classes corresponding to global elements, complex types, and arrays. The classes generated by XMLBeans can be used as parameters or Return types. The advantage of using XMLBean-generated classes is that you do not need to define a schema for the references complex type or element.
The Metadata Import Wizard supports marshalling and unmarshalling that converts Java token iterators into XML, and vice versa. For example, you start with a Java function, getListGivenMixed, defined as follows:
public static float[] getListGivenMixed(float[] fpList, int size) {
int listLen = ((fpList.length > size) ? size : fpList.length);
float fpListop = new float[listLen];
for (int i =0; i < listLen; i++)
fpListop[i]=fpList[i];
return fpListop;
}
After the function is processed through the Metadata Import Wizard, the following XML-based metadata is generated:
(::pragma function <f:function xmlns:f="urn:annotations.ld.bea.com"
kind="datasource" access="public">
<params>
<param nativeType="[F"/>
<param nativeType="int"/>
</params>
</f:function>::)
declare function f1:getListGivenMixed($x1 as xsd:float*, $x2 as xsd:int)
as xsd:float* external;
The corresponding XQuery for the imported Java function would be as follows:
declare namespace f1 = "ld:javaFunc/float";
let $y := (2.0, 4.0, 6.0, 8.0, 10.0)
let $x := f1:getListGivenMixed($y, 2)
return $x
Note:
To ensure successful importation and usage within ALDSP, the Java function should be static functions and its package and class names should be defined in its namespace. ALDSP recognizes the Java method name as the XQuery function name qualified with the Java function namespace.
For detailed information about using Java functions within ALDSP see the Data Services Developer's Guide.
32.1 Accessing Data Using WebLogic's Embedded LDAP Function
ALDSP enables access to data services, using WebLogic's embedded LDAP function. You will learn how to use this functionality by importing a Directory Service Markup Language (DSML)-based Java application as a Java function.
Objectives
In this exercise, you will:
Set the LDAP security credential for WebLogic's Embedded LDAP.
Create a new user account.
Import JAR files and Java applications that will be used to generate a data service.
Test the data service.
Instructions
In the DataServices project, create a folder and name it Functions. This is where you will place the Java functions that you want to import.
Set the LDAP security credential for WebLogic's Embedded LDAP, by completing the following steps:
Open the WebLogic Server Console from your browser:
http://localhost:7001/console.
Login using the following credentials:
User Name = weblogic
Password = weblogic
Select the Security folder, located under the ldplatform domain.
Click Embedded LDAP.
Enter security in the Credential and Confirm Credential fields.
Click Apply. This allows access to the WebLogic Server LDAP.
Figure 32-1 Setting LDAP Access Credentials
You will need to restart the WebLogic Server now as change to this property does not take effect until the Server is restarted.
Create a new user, by completing the following steps:
Expand the Security Realms myrealm Users folders.
Click Configure a New User, using your name and a password of your choice.
Click Apply.
In WebLogic Workshop right-click the Libraries folder and import all the JAR files located in the samples\liqiddata\EvalGuide\ldap\lib folder into the Libraries folder in Workshop.
Right-click the Functions folder and import DSML.java from the samples\liquiddata\EvalGuide folder.
Build the DataServices project.
Import the Java function metadata for the DSML Java application into the Functions folder by completing the following steps:
Right click the Functions folder and choose Import Source Metadata.
Select Java Function for the Data Source Type and click Next.
In the Class Name field, browse and select DataServices.jar\Functions\DSML.class and click Next.
Select the callDSML() function, click Add, and then click Next.
Note:
Do not select the callDSML procedure as a side-effect procedure.
Accept the default settings in the Summary window and click Finish.
The dsml.ds file and schemas folder are added to the Functions folder.
Build the DataServices project.
Test the DSML data service by completing the following steps:
Open dsml.ds in Test View.
Select callDSML() from the Function drop-down list.
Enter the following arguments (for more information on LDAP arguments and access, see http://dev2dev.bea.com/codelibrary/code/ld_ldap.jsp):
Description
Argument
LDAP URL
ldap://localhost:7001
Principal (Directory Manager)
cn=Admin
Credentials (Password)
security
JNDI (true: use JNDI to access LDAP; false: use native LDAP connection
jndi
Base domain name to search
dc=ldplatform
Filter used to search
cn=<your user name>
Click Execute.
View the results.
Figure 32-2 Results for callDSML()
32.2 Accessing Excel Spreadsheet Data Using JCOM
Data in a Microsoft Excel spreadsheet can be accessed through JCOM.
Objectives
In this exercise, you will:
Import JAR and Java files appropriate that will be used to generate a data service for using JCOM.
Test the results.
Instructions
Right-click the Libraries folder and using the add Add to Library option, add all the JAR files located in the samples\liqiddata\EvalGuide\excel\lib folder.
Right-click the Functions folder and import excel_jcom.java from <beahome>\weblogic81\samples\LiquidData\EvalGuide.
Build the DataServices project.
Import the Java function metadata for the Excel JCOM Java application into the Functions folder, by completing the following steps:
Right-click the Functions project and choose Import Source Metadata.
Select Java Function for the Data Source Type and click Next.
In the Class Name field, browse and select DataServices\Functions\Functions.excel_jcom and then click Next.
Select the getExcel() function, click Add, and then click Next.
Accept the default setting in the Select Side Effect Procedures window and click Next.
Accept the default settings in the Summary window and click Finish. The excel.ds and associated schema files are added to the Functions folder.
Build the DataServices project.
Test the Excel data service, by completing the following steps:
Open excel.ds in Test View.
Select getExcel(x1, x2) from the Function drop-down list.
For more information on Excel access refer to a dev2dev sample illustrating accessing data in an MS-Excel spreadsheet. As of this writing the sample is located at:
This schema will be used for the EJB results, which returns an XML document containing credit card information for a customer.
Build the Schemas project.
Create an EJB Project, by completing the following steps:
Right-click the Evaluation application folder and import the EJB folder as an EJB Project. The folder is located in:
<beahome>\weblogic81\samples\LiquidData\EvalGuide\ejb. This contains:
A container-managed entity bean that maps to the credit card database table.
A stateless session bean that invokes the entity bean finder method returning a list of credit cards for a given customer in the shape of the CREDIT_CARDS XML schema.
Build the EJB project.
Create a Java project, by completing the following steps:
Right-click the Evaluation application folder and import the EJBClient folder as a Java Project. The folder is located in:
This project contains the Java client that connects remotely to the stateless session bean. This will be used as the custom function.
Build the EJBClient project.
Run CreditCardClient.java, which is located in the EJBClient project folder. A list of credit cards for CUSTOMER3 should display in the Output window
Note:
Click OK for the pop-up message. Drag and drop the CreditCardClient.java into the Functions folder.
Build the DataServices project.
Import the Java function metadata for the EJB Client into the DataServices project by completing the following steps.
Right-click the Functions folder and select Import Source Metadata.
Select Java Function as the Data Source Type and click Next.
Browse and select DataServices\Functions.CreditCardClient as the Class Name and click Next.
Select getCreditCards, click Add, and then click Next.
Accept the default settings in the Select Side Effect Procedures window.
Accept the default settings in the Summary window and click Finish. The CREDIT_CARDS.ds file is added to the Functions folder.
Note:
Do not confuse this data service with the CREDIT_CARD.ds created from the relationship database.
Build the DataServices project.
Test the getCreditCards() function within the CREDIT_CARDS data service. Use CUSTOMER3 as the argument. Confirm that you can retrieve credit card information for Britt Pierce.
Figure 32-4 Results for the getCreditCards() function
Lesson Summary
In this lesson, you learned how to import the following sources as Java functions:
WebLogic's embedded LDAP through a Directory Service Markup Language (DSML)-based Java application
Data in a Microsoft Excel spreadsheet through a Java application that uses JCOM to access the MS Excel spreadsheet.
An Enterprise Java Bean that returns customer credit card information.