keep it httpsJekyll2023-10-11T11:35:56+01:00//fabriziofortino.github.io/Fabrizio Fortino//fabriziofortino.github.io/fabrizio.fortino@gmail.com//fabriziofortino.github.io/articles/fixer-io-swagger-schema-and-code-generation2016-08-14T00:00:00+01:002016-08-14T00:00:00+01:00Fabrizio Fortino//fabriziofortino.github.iofabrizio.fortino@gmail.com
<p><a href="http://fixer.io/" target="_blank">Fixer.io</a> is a free API that provides current
and historical foreign exchange rates published by the <a href="https://www.ecb.europa.eu/home/html/index.en.html" target="_blank">European Central Bank</a> (rates are updated every day around 4PM CET).</p>
<p>Although the API is very easy to use, if you want to integrate it in your application you need to
write a bit of code. Googling around I have found clients for <a href="https://pypi.python.org/pypi/pixer/0.0.2" target="_blank">Python</a> and <a href="http://www.phpclasses.org/package/9799-PHP-Fetch-currency-exchange-rates-from-fixer-io.html" target="_blank">PHP</a>.</p>
<h3 id="schema-definition">Schema Definition</h3>
<p>Since I need to use it in different applications (mainly Java and Go) I have decided to map the API calls
and response models in a <a href="http://swagger.io/specification/" target="_blank">Swagger Schema</a>
using the YAML format.</p>
<script src="https://gist.github.com/fabriziofortino/5d0482c72a7b6b5bbe2c65d978f9f2f7.js?file=fixer.yml"> </script>
<p>The <strong>definitions</strong> section contains the <code class="language-plaintext highlighter-rouge">Rates</code> model: an object with 2 properties (base & date)
and a dictionary of exchanges where the key (string) is the currency and the value (double) the rate.</p>
<p><strong>paths</strong> is where the API endpoints are mapped: the former gets the latest rates, the latter the historical ones.</p>
<p>I have published the schema as a public API on Swagger hub <a href="https://swaggerhub.com/api/fabriziofortino/fixer-io/1.0" target="_blank">here</a>. The right hand side
of the page shows the API in a nicely formatted style. Unfortunately the <strong>Try this operation</strong> button does not
to work due to a javascript bug in the website. As a workaround copy-paste the schema definition in
<a href="http://editor.swagger.io/" target="_blank">http://editor.swagger.io/</a> where everything seems to work.</p>
<h3 id="code-generation">Code Generation</h3>
<p>One of the most interesting feature of Swagger is the ability to generate code for the most common
languages through the <a href="https://github.com/swagger-api/swagger-codegen" target="_blank">Swagger Codegen project</a>.</p>
<p>The code generation feature is also integrated in Swagger hub. To create a client: click on the
download icon on the top-right menu, then Client and finally select a language from the list. In a matter of
seconds a zip file with the generated client will be downloaded. It is possible to customize the generated code
from the menu <strong>Edit Codegen Options</strong>.</p>
<h3 id="how-to-use-the-clients">How to use the clients</h3>
<p>Every client implementation is language dependent though they present similarities in term of naming and structure.
The following code snippet uses the Java <a href="https://github.com/OpenFeign/feign" target="_blank">Feign</a> client to access the latest AUD exhange rate for USD.</p>
<script src="https://gist.github.com/fabriziofortino/5d0482c72a7b6b5bbe2c65d978f9f2f7.js?file=Rates.java"> </script>
<p>The Go client provides the functions <code class="language-plaintext highlighter-rouge">NewRatesApi</code> and <code class="language-plaintext highlighter-rouge">GetLatest</code> to respectively get an handler to the API
and invoke the GET request to retrieve the latest rates.</p>
<script src="https://gist.github.com/fabriziofortino/5d0482c72a7b6b5bbe2c65d978f9f2f7.js?file=main.go"> </script>
<p><a href="//fabriziofortino.github.io/articles/fixer-io-swagger-schema-and-code-generation/">Multi-language Fixer.io clients with Swagger</a> was originally published by Fabrizio Fortino at <a href="//fabriziofortino.github.io">keep it https</a> on August 14, 2016.</p>
//fabriziofortino.github.io/articles/recursive-watchservice-java82016-06-05T00:00:00+01:002016-06-05T00:00:00+01:00Fabrizio Fortino//fabriziofortino.github.iofabrizio.fortino@gmail.com
<p><a href="https://docs.oracle.com/javase/8/docs/api/java/nio/file/WatchService.html" target="_blank">WatchService</a> is a
class included in the standard Java nio package since version 7. WatchService is extremely useful when we need to trigger an action after an event happens to an object in a specific folder or set of folders.</p>
<p>In the following code I have implemented a watch service registered to a root folder and all its subfolders.</p>
<p>Most of the examples around are based on Java 7. Since I am using Java 8 I have decided to re-implement it
using streams and lambdas:</p>
<ul>
<li>The code responsible to recursively register the folders has been implemented as a
<a href="https://docs.oracle.com/javase/8/docs/api/java/util/function/Consumer.html" target="_blank">Consumer</a>
Functional Interface (lines 67-84) and gets called to register the root folder (line 86) and the folders created at
successive stages (line 109).</li>
<li>A lambda with no arguments (or thunk) is used at line 88 to create and start a separate thread to execute
the WatchService logic.</li>
<li>The iteration of the pending events has been implemented in a functional style using Java 8 streams (lines 103-114).</li>
</ul>
<script src="https://gist.github.com/fabriziofortino/83eb36c7b48e9b900c1da1d8508245cd.js?file=RecursiveWatcherService.java"> </script>
<p>The code makes use of Spring but it’s extremely easy to remove it and adapt to other needs.</p>
<p><a href="//fabriziofortino.github.io/articles/recursive-watchservice-java8/">Java 8 Recursive WatchService</a> was originally published by Fabrizio Fortino at <a href="//fabriziofortino.github.io">keep it https</a> on June 05, 2016.</p>
//fabriziofortino.github.io/articles/orientdb-unlock-the-value-of-document-data-relationships2016-04-13T00:00:00+01:002016-04-13T00:00:00+01:00Fabrizio Fortino//fabriziofortino.github.iofabrizio.fortino@gmail.com
<p>The <a href="http://www.meetup.com/hadoop-user-group-ireland/events/229509552/" target="_blank">Ireland Hadoop User Group</a> organized an event themed on Graph Databases. I was invited as <a href="http://www.orientechnologies.com/orientdb/" target="_blank">OrientDB</a> contributor. My talk “OrientDB: Unlock the Value of Document Data Relationships” was focused on how to combine the flexibility of documents with the power of graphs while incorporating object oriented paradigm concepts. I shared my experience with OrientDB presenting a real use case I worked on in the last year.</p>
<iframe src="//www.slideshare.net/slideshow/embed_code/key/26Jj8znoVJX8gF" width="595" height="485" frameborder="0" marginwidth="0" marginheight="0" scrolling="no" style="border:1px solid #CCC; border-width:1px; margin-bottom:5px; max-width: 100%;" allowfullscreen=""> </iframe>
<div style="margin-bottom:5px"> <strong> <a href="//www.slideshare.net/FabrizioFortino1/orientdb-unlock-the-value-of-document-data-relationships" title="OrientDB: Unlock the Value of Document Data Relationships" target="_blank">OrientDB: Unlock the Value of Document Data Relationships</a> </strong> from <strong><a href="//www.slideshare.net/FabrizioFortino1" target="_blank">Fabrizio Fortino</a></strong> </div>
<p>For other details on the RTE Investigation Unit case study <a href="http://orientdb.com/rte-iu_case-study/" target="_blank">click here</a>.</p>
<p>It has been a great experience for me (my first public talk in front of such a big audience). I had the pleasure to meet a lot of passionate people, share ideas and discuss different point of views. Looking forward to the next event!!!</p>
<p><a href="//fabriziofortino.github.io/articles/orientdb-unlock-the-value-of-document-data-relationships/">OrientDB: Unlock the Value of Document Data Relationships</a> was originally published by Fabrizio Fortino at <a href="//fabriziofortino.github.io">keep it https</a> on April 13, 2016.</p>
//fabriziofortino.github.io/articles/running-orientdb-in-service-mode-with-jsw2014-08-11T00:00:00+01:002014-08-11T00:00:00+01:00Fabrizio Fortino//fabriziofortino.github.iofabrizio.fortino@gmail.com
<p>Many companies heavily rely on the functions of database that their daily business operations can not be executed if the database is not available, making database management and maintenance a critical component of their business models.</p>
<p><a href="http://www.orientechnologies.com/orientdb/" target="_blank">OrientDB</a> is an Open Source NOSQL database, written in Java, which nicely mixes Document and Graph features in a unique solution.</p>
<p><a href="http://wrapper.tanukisoftware.com/doc/english/product-overview.html" target="_blank">Java Service Wrapper</a> is a configurable tool which allows Java-based applications to be installed, controlled and monitored as native Unix/Linux/MacOSX/Windows services in a painless way.</p>
<p>On a past project I have integrated JSW Community Edition for a critical application needed 24x7. Since the result was excellent I have decided to do the same with OrientDB. The complete integration scripts and related documentation on how to install and use it are available on <a href="https://github.com/fabriziofortino/orientdb-servicewrapper" target="_blank">https://github.com/fabriziofortino/orientdb-servicewrapper</a></p>
<h3 id="install-orientdb-servicewrapper">Install orientdb-servicewrapper</h3>
<script src="https://gist.github.com/fabriziofortino/bcafd9ef3e953fe24fb4.js?file=install"> </script>
<h3 id="setup-orientdb-home">Setup OrientDB home</h3>
<p>To setup the integration between OrientDB and Java Service Wrapper, edit the file <em>$ORIENTDB-HOME/bin/service/orientdb.conf</em> and replace the placeholder for the variable <em>set.default.ORIENTDB_HOME</em> with the OrientDB installation full path.</p>
<h3 id="ready-to-go">Ready to go</h3>
<p>Then, at this point the OrientDB service wrapper is ready to go, you can test it by directly invoking the service:</p>
<script src="https://gist.github.com/fabriziofortino/bcafd9ef3e953fe24fb4.js?file=execute"> </script>
<h3 id="daemonize-and-monitor-orientdb">Daemonize and monitor OrientDB</h3>
<p>As you can see, install or uninstall of the OrientDB daemon / NT service or retrieving useful information like the JVM dump are easy as pie. But JSW Community Edition gives us even more. Basically the wrapper will ping the OrientDB JVM every 5 seconds (you can change the polling time in orientdb.conf) to make sure the process has not frozen up. If the JVM does not respond the process will be automatically restarted. Moreover, it is possible to intercept any string coming from the OrientDB JVM output and execute any number of actions. By default, I have included in orientdb-servicewrapper a RESTART action triggered when an OutOfMemory exception occurs. You can easily add your custom trigger / actions just modifying the configuration file.</p>
<p><a href="//fabriziofortino.github.io/articles/running-orientdb-in-service-mode-with-jsw/">OrientDB in service mode with Java Service Wrapper</a> was originally published by Fabrizio Fortino at <a href="//fabriziofortino.github.io">keep it https</a> on August 11, 2014.</p>
//fabriziofortino.github.io/articles/hibernate-json-usertype2014-06-07T00:00:00+01:002014-06-07T00:00:00+01:00Fabrizio Fortino//fabriziofortino.github.iofabrizio.fortino@gmail.com
<p><a href="http://hibernate.org" target="_blank">Hibernate</a> provides a wide range of mappings to convert SQL data to Java objects and the other way around. In some circumstances, developers may need to define custom types to handle the mapping of a column to a specific type.
In the last years, along with the exploding growth of JavaScript, JSON has become the most popular data-interchange format with lots of supporting libraries that make it easy to read and write models in JSON.
<a href="http://jackson.codehaus.org/" target="_blank">Jackson</a> is a widely used high performance Java JSON data processing library.
Here I will show you how to create a custom Hibernate UserType that uses Jackson to map generic POJOs to a database column in a transparent fashion.</p>
<h3 id="add-jackson-as-dependency-to-your-project">Add Jackson as dependency to your project</h3>
<script src="https://gist.github.com/fabriziofortino/d33e6cf0e06545fcba18.js?file=pom.xml"> </script>
<h3 id="include-the-jackson-usertype-implementation">Include the Jackson UserType implementation</h3>
<p>Here is the code in charge of handling the marshall/unmarshall of JSON to Java and vice versa. It implements <em>ParameterizedType</em> to allow parameterization of the mapping file. This implementation supports LONGVARCHAR, CLOB and BLOB.</p>
<script src="https://gist.github.com/fabriziofortino/d33e6cf0e06545fcba18.js?file=JSONUserType.java"> </script>
<h3 id="define-the-hbm-file-to-instruct-hibernate-how-to-map-the-defined-class-to-the-database-table">Define the hbm file to instruct Hibernate how to map the defined class to the database table</h3>
<p>Below is an example of mapping file for the Person java class. As you can see it contains the <em>status</em> property of type EnumType (provided by Hibernate) and our JSONUserType <em>config</em> property. Parameter <em>classType</em> is the mapped POJO and <em>type</em> is the CLOB column type constant (see class <a href="http://docs.oracle.com/javase/6/docs/api/java/sql/Types.html" target="_blank">java.sql.Types</a> for more details on constant field values).
<script src="https://gist.github.com/fabriziofortino/d33e6cf0e06545fcba18.js?file=mapping.xml"> </script></p>
<p><a href="//fabriziofortino.github.io/articles/hibernate-json-usertype/">Hibernate JSON UserType: simple handling of JSON objects with Jackson</a> was originally published by Fabrizio Fortino at <a href="//fabriziofortino.github.io">keep it https</a> on June 07, 2014.</p>