[[writing_first_script]]
== Writing your first Q2 Script

Once you have your Q2 running and checking the +deploy+ directory for new
QBean descriptors (XML files) as well as the +deploy/lib+ directory for new
+jars+, you can try to deploy a QBean.

Just to test the waters, we'll show you how to deploy a +BeanShell+ 
footnote:[ http://beanshell.org/ ] based QBean.

Use your preferred text editor to write an XML file like this:

[source,xml]
----
<script>
  log.info ("Hello jPOS!");
</script>
----

Let's call it +90_hello_jpos.xml+ and save it in a temporary directory.

Now copy that file to your +deploy+ directory and you should see output
like this:

[source,xml]
----
<log realm="Q2.system" at="Sat Oct 19 20:15:48 UYST 2013.237" lifespan="150ms">
  <info>
    deploy: /home/jpos/jpos/build/install/jpos/deploy/90_hello_jpos.xml
  </info>
</log>
<log realm="script" at="Sat Oct 19 20:15:48 UYST 2013.244">
  <info>
    Hello jPOS!
  </info>
</log>
----

That little script is equivalent to:

[source,xml]
----
<qbean name='script' class='org.jpos.q2.qbean.BSH' logger='Q2'>
  log.info ("Hello jPOS!");
</qbean>
----

The reasons this works without specifying the class name, logger name are:

* If there's no +name+ attribute, Q2 uses the root XML element name as
  the bean name, in this case 'script'. 
* If there's no +logger+ attribute, Q2 assigns the default logger name +Q2+.
* If there's no +class+ attribute, the root element name is used to find
  a resource with the mapping. The resource is placed in the 
  link:https://github.com/jpos/jPOS/blob/master/jpos/src/main/resources/org/jpos/q2/QFactory.properties#L3[+QFactory.properties+]

As of this writing mapping, +QFactory.properties+ looks like this:

----
logger=org.jpos.q2.qbean.LoggerAdaptor
shutdown=org.jpos.q2.qbean.Shutdown
script=org.jpos.q2.qbean.BSH
jython=org.jpos.q2.qbean.Jython
spacelet=org.jpos.q2.qbean.SpaceLet
sysmon=org.jpos.q2.qbean.SystemMonitor
txnmgr=org.jpos.transaction.TransactionManager
transaction-manager=org.jpos.transaction.TransactionManager
qmux=org.jpos.q2.iso.QMUX
channel-adaptor=org.jpos.q2.iso.ChannelAdaptor
qexec=org.jpos.q2.qbean.QExec
----

that explains the reason why you can write +<txnmgr>...</txnmgr>+ or
+<qmux>...</qmux>+ without specifying a +class+ attribute.

The previous BeanShell based QBean is very useful to run quick tests or
hot fixes to a running jPOS system. Sometimes the Java code written 
inside the +<script>...</script>+ XML elements need to use some XML
reserved characters (like '<' or '>'). The easiest way to achieve that
is to use a +CDATA+ block, like this:


[source,xml]
----
<qbean name='script' class='org.jpos.q2.qbean.BSH' logger='Q2'><![CDATA[ <1>
  log.info ("Hello jPOS!"); 
]]></qbean>                                                              <2>
----
<1> Note the +<![CDATA[ start+
<2> And its end +]]>+

