6.6 Introduction: Using Java in Actionscript
The Server Side Framework is not only limited by the provided API, but it
can easily access the entire Java2 Framework and any other
Java classes, providing
an unlimited amount of possibilities for developers.
Mixing Java classes in your Actionscript code is really very simple. Have
a look at the example below:
var list = new java.util.LinkedList()
for (var i = 0; i < 10; i++)
{
list.add("Item " + i)
}
// Use a Java iterator to cycle through the list
for (var it = list.iterator(); it.hasNext();)
{
trace(it.next())
}
As you can see from the code, Java classes can be instantiated and accessed
using their fully qualified name.
With this approach there's really no limit to what could be achieved
with SmartFoxServer PRO: parsing and writing XML, using XSLT and XPath, writing
web services etc...
Not only Java classes can extend the possibilities of what you can do on the
server side, but they may also help in obtaining better
performance in critical
parts of your code. For example in a complex 2D array loop you may want to
use Java native collections instead of Actionscript arrays to get faster code
execution.
» The Packages object
A special object called Packages
is available to access Java classes outside of the J2SE standard packages.
In order to access your custom classes you should add them to the classpath in
the start.bat (Win) or start.sh (Linux) scripts.
Here follows an example that shows how to create an instance of the User class
inside the it.gotoandplay.smartfoxserver.data package
var data = Packages.it.gotoandplay.smartfoxserver.data
var user = new data.User()
A complete Extension example is provided with SmartFoxServer
PRO showing how
to integrate Java libraries in Actionscript, you can read it in chapter 8.7