Ice for Java
Ice for Java is middleware for the practical Java programmer. Written in pure Java and optimized for scalability, Ice offers an efficient integration technology that leverages Java's portability and provides complete interoperability with Ice applications and services written in other languages. With support for secure communication and automatic database persistence, Ice for Java is a stable and robust platform for distributed application development.
Sample Code
To illustrate how Ice is used in various language mappings, consider the following Slice interface definitions:
module Biz {
struct Item {
string sku;
int qty;
};
sequence<Item> ItemSeq;
interface Invoice {
void addItems(ItemSeq items);
void submit();
};
interface InvoiceFactory {
Invoice* create();
};
};
Java code that creates an invoice using the factory is shown below:
// Create a proxy for the invoice factory object.
Ice.ObjectPrx proxy = communicator.stringToProxy("InvoiceFactory:tcp -p 9000");
// Narrow the proxy to the proper type.
Biz.InvoiceFactoryPrx factory = Biz.InvoiceFactoryPrxHelper.checkedCast(proxy);
// Use the factory to obtain a proxy for a new invoice object.
Biz.InvoicePrx invoice = factory.create();
// Add items to the invoice.
Biz.Item[] items = new Biz.Item[1];
items[0] = new Biz.Item();
items[0].sku = "10-6139";
items[0].qty = 3;
invoice.addItems(items);
// Submit the invoice.
invoice.submit();
Flexible Mapping
Ice for Java supports metadata that adjusts how certain Slice types are mapped to Java. For example, the definition of ItemSeq can be changed as shown below:
["java:type:java.util.ArrayList"] sequence<Item> ItemSeq;
The sample code changes accordingly:
// Add items to the invoice. java.util.ArrayList items = new java.util.ArrayList(); Item item = new Biz.Item(); item.sku = "10-6139"; item.qty = 3; items.add(item); invoice.addItems(items);
Similarly, the following metadata changes the mapping to use a LinkedList:
["java:type:java.util.LinkedList"] sequence<Item> ItemSeq;
The metadata directive can also be applied to an operation:
sequence<Item> ItemSeq;
interface Invoice {
void addItems(["java:type:java.util.ArrayList"] ItemSeq items);
};
In this example, ItemSeq is passed as an ArrayList to addItems() and as a native array (the default mapping) in all other cases.