Apache HISE + Apache Camel
View more presentations from Rafal Rusin.
This blog covers topics around programming in various languages, for the industry. It includes my insights based on commercial experience from various projects I worked on. I hope it will provide a simple way of doing complex tasks related to programming, as well as an overview of what's going on in software nowadays.
<bean id="jetty-service" class="org.apache.jetty.service.JettyServiceImpl" init-method="init" destroy-method="destroy"/> <osgi:service id="jetty-service-osgi" ref="jetty-service" interface="org.apache.jetty.service.api.JettyService" />
public Handler registerApp(String name, Handler handler) throws Exception; public void unregisterApp(Handler handler) throws Exception;
package org.apache.jetty.service; import org.apache.jetty.service.api.JettyService; import org.mortbay.jetty.Handler; import org.mortbay.jetty.Server; import org.mortbay.jetty.handler.ContextHandler; import org.mortbay.jetty.handler.ContextHandlerCollection; public class JettyServiceImpl implements JettyService { private Server server; private ContextHandlerCollection rootContext; public void init() throws Exception { server = new Server(8080); rootContext = new ContextHandlerCollection(); server.setHandler(rootContext); server.start(); } public void destroy() throws Exception { server.stop(); } public Handler registerApp(String name, Handler handler) throws Exception { server.stop(); ContextHandler h = rootContext.addContext("/" + name, name); h.setHandler(handler); server.start(); return h; } public void unregisterApp(Handler handler) throws Exception { server.stop(); rootContext.removeHandler(handler); server.start(); } }
<osgi:reference id="jetty-service" interface="org.apache.jetty.service.api.JettyService" bean-name="jetty-service"/> <bean class="org.apache.jetty.service.example.HelloWorld" init-method="init" destroy-method="destroy"> <property name="jettyService" ref="jetty-service"/> </bean>
package org.apache.jetty.service.example; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.jetty.service.api.JettyService; import org.apache.jetty.service.util.BundleResource; import org.mortbay.jetty.Handler; import org.mortbay.jetty.HttpConnection; import org.mortbay.jetty.Request; import org.mortbay.jetty.handler.AbstractHandler; import org.mortbay.jetty.handler.ContextHandler; import org.mortbay.jetty.handler.ContextHandlerCollection; import org.mortbay.jetty.handler.ResourceHandler; public class HelloWorld { private JettyService jettyService; private Handler registered; public void setJettyService(JettyService jettyService) { this.jettyService = jettyService; } public void init() throws Exception { ContextHandlerCollection handler = new ContextHandlerCollection(); handler.addContext("/app", "app").setHandler(new AbstractHandler() { public void handle(String target, HttpServletRequest request, HttpServletResponse response, int arg3) throws IOException, ServletException { response.setContentType("text/html"); response.setStatus(HttpServletResponse.SC_OK); response.getWriter().println("<h1>Hello World from Java</h1>" + request.getParameterMap()); Request base_request = (request instanceof Request) ? (Request)request:HttpConnection.getCurrentConnection().getRequest(); base_request.setHandled(true); } }); ResourceHandler resourceHandler = new ResourceHandler(); resourceHandler.setBaseResource(new BundleResource(getClass().getResource("/static"))); ContextHandler contextHandler = handler.addContext("",""); contextHandler.setHandler(resourceHandler); registered = jettyService.registerApp("helloWorld", handler); } public void destroy() throws Exception { jettyService.unregisterApp(registered); } }
<features> <feature name="jetty-service" version="${project.version}"> <bundle>mvn:org.apache.jetty.service/service/${project.version}</bundle> </feature> <feature name="example-jetty-service-helloworld" version="${project.version}"> <feature version="${project.version}">jetty-service</feature> <bundle>mvn:org.apache.jetty.service/example-helloworld/${project.version}</bundle> </feature> </features>
features:addUrl mvn:org.apache.jetty.service/service-karaf/0.1.0-SNAPSHOT/xml/features features:install example-jetty-service-helloworld osgi:list [ 230] [Active ] [ ] [Started] [ 60] Unnamed - org.apache.jetty.service:service:bundle:0.1.0-SNAPSHOT (0.1.0.SNAPSHOT) [ 231] [Active ] [ ] [Started] [ 60] Unnamed - org.apache.jetty.service:example-helloworld:bundle:0.1.0-SNAPSHOT (0.1.0.SNAPSHOT)
SMX4_DIR=os.getenv("SMX4_DIR") SOAPUI_DIR=os.getenv("SOAPUI_DIR")
SMX4_DIR=/some/path SOAPUI_DIR=/some/other/path ./our-python-script
import pexpect import time import sys child = pexpect.spawn("bin/servicemix") child.logfile = sys.stdout child.expect("karaf.*>") time.sleep(3)
child.sendline("features:addUrl mvn:org.apache.hise/hise-karaf/0.3.0-SNAPSHOT/xml/features"); child.expect("karaf.*>") child.sendline("features:install hise-h2-test-example-osgi") child.expect("karaf.*>")
child.sendline("features:addUrl mvn:org.apache.hise/hise-karaf/0.3.0-SNAPSHOT/xml/features"); rep=0 while True: child.sendline("osgi:list|grep -i hise-test-example-osgi") l=child.readline() l=child.readline() if re.match(".*Started", l) != None: break time.sleep(1) child.expect("karaf.*>") rep=rep+1 if rep>30: raise Exception("Bundle not installed")
import os def syscall(c): if os.system(c) != 0: raise Exception("Sys call failed: " + c) syscall(SOAPUI_DIR + "/bin/testrunner.sh -f results hise-soapui-project.xml")
child.sendline("shutdown")
<pmap:replay> <replay> <ns:upgradeInstance>1234</ns:upgradeInstance> </replay> </pmap:replay>
<pmap:getCommunication> <getCommunication> <ns:iid>1234</ns:iid> </getCommunication> </pmap:getCommunication>
<pmap:replay xmlns:ns="http://www.apache.org/ode/pmapi/types/2006/08/02/"> <replay> <ns:restoreInstance> <ns:processType xmlns:p="http://sample.bpel.org/bpel/sample">p:OnEventCorrelation</ns:processType> ... exchanges </ns:restoreInstance> </replay> </pmap:replay>
<beans> <util:properties id="properties"> <prop key="weirdProp">myWeirdPropValue2</prop> </util:properties> </beans>
<bpws:assign> <bpws:copy> <bpws:from>ode:process-property('weirdProp')</bpws:from> <bpws:to>$output.payload</bpws:to> </bpws:copy> </bpws:assign>
<a:article xmlns='http://www.w3.org/1999/xhtml' xmlns:a="urn:article"> <a:l>Some text</a:l> <a:code lang="xml"><![CDATA[ <someXml/> ]]></a:code> </a:article>
./run <input.xml >output.xhtml
declare namespace a="urn:article"; declare default element namespace "http://www.w3.org/1999/xhtml"; declare function a:processLine($l) { for $i in $l/node() return typeswitch ($i) case element(a:link, xs:untyped) return <a href="{$i/text()}">{$i/text()}</a> default return $i }; declare function a:articleItem($i) { typeswitch ($i) case element(a:l, xs:untyped) return (a:processLine($i), <br/>) case element(a:code, xs:untyped) return ( a:highlight($i/text(), $i/@lang)/body/* , <br/>) default return "error;" }; <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>a.xml</title> <link rel="stylesheet" type="text/css" href="highlight.css"/> </head> <body> { for $i in a:article/* return a:articleItem($i) } </body> </html>
public static class Mod { public static Node highlight(final String code, String lang) throws Exception { Validate.notNull(lang); final Process p = new ProcessBuilder("highlight", "-X", "--syntax", lang).start(); Thread t = new Thread(new Runnable() { public void run() { try { OutputStream out = p.getOutputStream(); IOUtils.write(code, out); out.flush(); out.close(); } catch (IOException e) { throw new RuntimeException(e); } } }); t.start(); String result = IOUtils.toString(p.getInputStream()); t.join(); return DOMUtils.parse(result).getDocumentElement(); } }
<employees> <employee> <name>Fred Jones</name> <address location="home"> <street>900 Aurora Ave.</street> <city>Seattle</city> <state>WA</state> <zip>98115</zip> </address> <address location="work"> <street>2011 152nd Avenue NE</street> <city>Redmond</city> <state>WA</state> <zip>98052</zip> </address> <phone location="work">(425)555-5665</phone> <phone location="home">(206)555-5555</phone> <phone location="mobile">(206)555-4321</phone> </employee> </employees>
<names> <name>Fred Jones</name> </names>
<names> {for $name in employees/employee/name/text() return <name>{$name}</name>} </names>
XQueryEvaluator evaluator = new XQueryEvaluator(); Long result = (Long) evaluator.evaluateExpression("5+5", null).get(0); Assert.assertEquals(new Long(10), result);
evaluator.bindVariable(QName.valueOf("myVar"), 123);
public static class MyFunctions { public static String myHello(String arg) { TestEvaluator te = (TestEvaluator) XQueryEvaluator.contextObjectTL.get(); te.id++; return "hello(" + arg + te.id + ")"; } } XQueryEvaluator evaluator = new XQueryEvaluator(); evaluator.setContextObject(this); evaluator.declareJavaClass("http://my.org/employees", MyFunctions.class); }
declare namespace my = 'http://my.org/employees'; my:myHello("hello")
mvn package