====== Spark | Alpha Preview ======
[[Spark|Project Page]] | **SparkAlpha**
Here are some instructions for getting running with the spark preview.
* SVN: http://svn1.cvsdude.com/osflash/spark/trunk/
* EXPORT: http://dev.codegent.com/spark_export.zip
- checkout from svn or download zip above
- create a mysql db called spark_example
- edit /webapps/example/WEB-INF/spark.properties and change the db connection details
- run start.sh (linux/mac) or start.bat (win) in the /bin/ directory.
- the first time spark loads it will create the db, this is done by the script /webapps/exmaple/WEB-INF/services/PopulateModel.js
- if it looks like everything is working point your browser at http://localhost:8080/example/resources/remoting-test.swf
- now you can call the scripts in /webapps/exmaple/WEB-INF/services/
===== Right so what does the example do? =====
It shows how scripts work in spark, and different ways you can connect to the database using hibernate in spark. The client is pritty simple, something I built quickly to test the remoting methods.
===== Exposing remoting methods =====
To expose a method for remoting you use the **meta** object.
This object is only in the context during compile.
addMethod(String methodName, String paramTypes, String returnType)
addInterface(String className)
setExtends(String className)
Examples:
// add a business interface, all these methods will be exposed to remoting clients
meta.addInterface('some.business.package.MyBusinessObject');
// add a method which will be exposed to remoting clients
meta.addMethod('myRemotingFunc','String,Integer','java.util.List');
function myRemotingFunc(someStr,someNum){
var list = new Packages.java.util.LinkedList();
list.add(someStr);
list.add(someNum);
return list;
}
Why is this needed? Its used by the compiler to generate a interface used to expose the script to remoting.
===== Script object model =====
This is how scripts access beans in the spring application context, access the http session, etc.
This is exposed to scripts as the **spark** object. Here is the api.
getCookie(req,name)
getResources(path)
getResource(file)
getMessageSource()
getApplicationContext()
getBeans()
addCookie(cookie)
createCookie(name, value)
getCookieByName(name)
getCookies()
getSession(create)
getSession()
/*
Note: Need to add access to all or part of http request object. DONE in SVN.
Examples:
*/
var isAuth = spark.session.getAttribute('user_auth');
var someService = spark.beans.getBean('serviceInSpring');
As this java api is called from js you can call get methods as properties so spark.getSession() and spark.session will both work.
===== Logging =====
Each script also has a **log** object. This is just a standard log4j logger. so have all the normal methods. debug(msg), isDebugEnabled() etc. For more info see log4j.
The other option is print() this prints values via System.out.println();. You should try to avoid it.
more info to follow.