<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Dilan Tharaka on Medium]]></title>
        <description><![CDATA[Stories by Dilan Tharaka on Medium]]></description>
        <link>https://medium.com/@tharakamd-12?source=rss-3ab50117c198------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*MaJuMQmWNoE3N26a</url>
            <title>Stories by Dilan Tharaka on Medium</title>
            <link>https://medium.com/@tharakamd-12?source=rss-3ab50117c198------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Tue, 17 Nov 2026 07:39:52 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@tharakamd-12/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[How to find an IT job with Visa Sponsorship in the UK]]></title>
            <link>https://tharakamd-12.medium.com/how-to-find-an-it-job-with-visa-sponsorship-in-the-uk-7ebd954934a6?source=rss-3ab50117c198------2</link>
            <guid isPermaLink="false">https://medium.com/p/7ebd954934a6</guid>
            <category><![CDATA[visa]]></category>
            <category><![CDATA[uk]]></category>
            <category><![CDATA[software-engineering]]></category>
            <category><![CDATA[jobs]]></category>
            <dc:creator><![CDATA[Dilan Tharaka]]></dc:creator>
            <pubDate>Sat, 21 Oct 2023 05:31:30 GMT</pubDate>
            <atom:updated>2023-10-21T05:31:30.111Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ToN0fqNFbdKXhaDkxZqR0w.jpeg" /><figcaption>Photo by Ron Lach : <a href="https://www.pexels.com/photo/sitting-woman-looking-for-job-in-newspaper-9832698/">https://www.pexels.com/photo/sitting-woman-looking-for-job-in-newspaper-9832698/</a></figcaption></figure><p>This article will give you an idea of how to find a visa sponsored IT job in the UK. Please note that this mainly targets finding a Software Engineer or related jobs. And this is based on my experience, so your journey could differ. Feel free to comment if you have anything to add to this. I will break this journey into a few steps. Even though I have ordered them, there’s no “rule” to follow them in the given order. Plan your journey and use this article as a resource to help you build it up.</p><h3>Step 1 : Build Up Skills</h3><p>Getting a visa sponsorship job is a challenge and a competition. Lots of highly talented and motivated people from all over the world are competing for these jobs. To make yourself highlighted from other candidates, you must have some skills listed on your profile.</p><p>This will start from a formal education qualification like a BSc or MSc. Usually, a BSc degree in the relevant field is more than enough, but an MSc is a plus point.</p><p>Other certifications like AWS and Kubernetes will put you at the top. These won’t just fill up your CV but also will come in handy when you’re facing interviews because studying for them will give you a good knowledge of those systems and, in general, about software development and dev ops best practices.</p><p>And if you’re passionate about writing a blog, doing a YouTube channel, contributing to open-source projects and forums such as StackOverflow will be massive plus points. But don’t do them because everyone else is doing them, do them only if you love to do them. I have met great software engineers who don’t have any of these, but they don’t have any trouble finding a good job.</p><h3>Step 2 : Create a CV</h3><p>CV is the document that will give the first impression of you to the recruiters. It must be brief and organised. Since almost all companies use software to read CVs, your CV has to be well organised. That doesn’t mean you must spend money on a professional person or software to create your CV. Just using a well-designed free template will do the trick. And regarding templates, although many people say to use a “UK CV template”, I don’t think it’s essential. If your CV is well structured, that would be enough. I encourage you to have a shorter CV (around 2 to 3 pages), but again it’s your choice. And make sure to keep your CV up to date and order the points in a section according to the most important or most recent one first. That may not affect the software, but when a recruiter reads it, it’s essential to give them an accurate picture of you.</p><h3>Step 3 : Prepare for Interviews</h3><p>This is a crucial step that most of us don’t spend much time on. It could be boring, but you must do this before applying for any job.</p><p><a href="https://amzn.eu/d/e13bw9G">Cracking the Coding Interview</a> is a highly recommended book for interview preparations. This book covers some theories and questions you will need in the interview.</p><p><a href="https://www.hackerrank.com/interview/interview-preparation-kit">HackerRank Interview Preparation Kit</a> is very helpful in preparing for coding interviews. You might only have time to complete a few questions, but finishing at least a few questions in each section will be extremely valuable for the coding interview.</p><p>System design questions are also very common in these interviews. Refer to YouTube videos and articles about system design interviews and practice to design some of those systems yourself.</p><h3>Step 4 : Search and Apply for Jobs</h3><p>This is the most fun part of the process for most of us. Isn’t that to you? In searching for jobs, our question is where to find job postings. Following are some of the resources that I found effective. Please comment if you know more resources so the others can use them.</p><p><a href="https://companyjobs.co.uk/uk-visa-jobs/">Companyjobs search engine</a> — This is a search engine to find UK jobs with visa sponsorships. A recommended place to start your job search.</p><p><a href="https://uk.indeed.com/">Indeed UK </a>— Indeed is one of the most popular job posting websites in the UK. However, most of the jobs here do not provide a visa sponsorship. Therefore, when searching, using terms like “Software Engineer visa sponsorship” will help you filter the jobs with visa sponsorship.</p><p><a href="https://www.youtube.com/c/YourKnowledgeBuddy">Your Knowledge Buddy YouTube channel</a> — This YouTube channel posts updates about companies providing visa sponsorships these days. And it has much valuable content about finding a visa sponsorship job (not just in the UK). I highly recommend watching videos on this channel.</p><p>Apart from these, most common job listing websites such as LinkedIn and Glassdoor will also help. Remember, don’t apply if the job description doesn’t say that the company provides visa sponsorship for this position. If it doesn’t say, there’s a high chance they are looking for candidates with the right to work in the UK.</p><h3>Step 5 : Improve and Re-Apply</h3><p>I highly discourage applying to a high number of jobs at once. I recommend applying for two or three jobs at once. Then, maintain a spreadsheet of the jobs you applied for and the application status. If you get a rejection email, don’t worry. That is a learning opportunity. You may have reached the wrong job (which doesn’t match your profile or visa status). Or you have a problem with your CV. If you feel like it’s the CV, then update it before applying to any other job.</p><p>If you get an interview, excellent, prepare for it again. Most probably you’ll get the first interview with a recruiter. At this point, research a bit about the company. And prepare answers to common recruiter questions. Questions like “Why do you like to work with us?”. You may get a rejection email after the first interview. Don’t worry; and this is another learning opportunity. This time, it should be either your answers to the recruiters’ questions or something like the first impression. Try to improve in those areas. And keep applying for jobs. Just remember, be slow and improve yourself.</p><p>And a final note about job search. There are many scams in this area. If it’s a genuine job posting, they won’t ask you to pay any money at any stage of the application process. Not even to provide the COS (Certificate of Sponsorship). If they ask for money, there’s an extremely high chance it’s a scam.</p><h3>Step 6: Book Your Tickets</h3><p>If your hard work has finally paid off and you secured a visa sponsorship job in the UK, the next step is to move to the UK. The most exciting part 😃. The visa application process changes from time to time. Therefore, I’m not going to discuss that in this article. But most of the companies will help you with the visa process. Most companies will reimburse you all the money you have to spend on the relocation process, and most will provide you with air tickets, accommodation for the first month and a relocation allowance.</p><p>I hope this article will help you in your job hunting. Please leave a comment if you have anything else to add.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=7ebd954934a6" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Debug Like a Pro in IntelliJ IDEA]]></title>
            <link>https://tharakamd-12.medium.com/debug-like-a-pro-in-intellij-idea-6c3599117297?source=rss-3ab50117c198------2</link>
            <guid isPermaLink="false">https://medium.com/p/6c3599117297</guid>
            <category><![CDATA[intellij]]></category>
            <category><![CDATA[software-development]]></category>
            <category><![CDATA[debugging]]></category>
            <category><![CDATA[java]]></category>
            <category><![CDATA[coding]]></category>
            <dc:creator><![CDATA[Dilan Tharaka]]></dc:creator>
            <pubDate>Tue, 01 Jun 2021 08:02:55 GMT</pubDate>
            <atom:updated>2021-06-01T08:02:55.330Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*WafQa3LzyoYaMxu8DZ4mMA.jpeg" /><figcaption>Photo by <a href="https://www.pexels.com/@hiteshchoudhary?utm_content=attributionCopyText&amp;utm_medium=referral&amp;utm_source=pexels">hitesh choudhary</a> from <a href="https://www.pexels.com/photo/selective-focus-photography-of-man-facing-computer-340152/?utm_content=attributionCopyText&amp;utm_medium=referral&amp;utm_source=pexels">Pexels</a></figcaption></figure><p>IntelliJ IDEA is my favorite IDE in Java development. Might be yours too. When developing Java projects (Not only Java) debugging acts a huge role. IntelliJ IDEA debugger provides powerful tools to debug Java applications. The purpose of this article is not to give you an introduction to the Intellij IDEA debugger, but to give you few tricks to use some of the powerful features. So, let’s get started.</p><h4>Stream Tracing</h4><p>Stream tracing is an interesting feature in the Intellij IDEA debugger. This feature allows us to debug the behavior of a Java Stream in a graphical way. To demonstrate this, I’m using the following sample code. This has a List of Strings and using stream processing to uppercase the list of words and get the count of words where length is greater than 3.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*EYa7OuPx6ElZnIwzY8hkXw.png" /></figure><p>If we place a debug point in the line having the stream procession part, and run the program. When the debugger hits that line, In the debug tools, we can see this button, “Trace Current Stream Chain” is activated.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*MqGH8NbsqqlPJUQWVDxW1w.png" /></figure><p>When we click on that button, a new window named “Stream Trace” is popping up. This window lists out the stream operations we defined. We can click on each operation and see the transformations doing by that step.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*l4EqX-jsnz14WBUYmkBauw.png" /></figure><p>And if we click on the “Flat Mode” button, it gives us a nice representation of the stream procession including all the steps and results of each step.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Nl8HTIzskpVAQ_2LMmv8YA.png" /></figure><p>The following GIF shows the full process,</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*IdGJeuwB0Q5gasRVnEiSKg.gif" /></figure><p>Don’t forget to try this feature the next time debugging a stream 😎</p><h4>Non suspending breakpoints</h4><p>Imagine a situation where you have a loop and you need to check a value of a variable inside the loop in the runtime. What would you do? Console log 🤔 , Nope, since we have the debugger, we can simply place a debug point 😎. But, wait, if that loop iterates thousands of times, the program would stop at the breakpoint thousands of times and you have to observe the value and continue. That is a real pain 😐 Console log then, isn’t is 🙃. Nope, we have this cool feature in the IntelliJ IDEA debugger called non-suspending breakpoints which is really useful in this type of scenario.</p><p>To demonstrate this, I developed the same scenario we used before without using streams.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*xoRxlJzir5PwfkI7BDpjmg.png" /></figure><p>Let’s say, we need to check the word length in line 20. We can simply place a breakpoint. But if our word list is lengthy, this would be a real headache. To avoid this, we can place a breakpoint, right-click on it and untick the Suspend check box.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/990/1*kXD8M3J_mxQbDjYnz-IVxQ.png" /></figure><p>After you untick the Suspend check box, you would get the following popup,</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/894/1*96pZD6JDpt-ASWArXRZP6w.png" /></figure><p>If you check the Evaluate and log check box, then you can write any piece of code in the bellow text box (this can be expanded using the expand button on the right corner).</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/838/1*UEu58ynkWFJYEuIRpZUqhw.png" /></figure><p>Intellij Idea would log the output of the code that we write here. So, in this case, I wrote a line of code to build a String with the word and the word length. Intellij Idea would log this sentence when the debugger hits this line and the debugger won’t stop on that debug point. So, when I debug, the debugger console gives the following output,</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1014/1*24pPCHn3_CK5UetV6dbJTw.png" /></figure><p>As you can see, it has printed all the words and word lengths. Not only for logging but also to change the program status (to change a value of a variable, etc. ) on runtime, we can use this approach. What we have to do is, we have to write the code that we need to change the state of the application in that text box. As an example, if I want to add 1 to the word length at line 21, I can do that in runtime by giving the following code to Elavuate and Log text box.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/833/1*ODa9gg0fcZhnc6ztFxEbGA.png" /></figure><p>Then, when we debugged the program, it gives the following output,</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1000/1*IXlO2GihzQajNkuP_kc5MQ.png" /></figure><p>Note that the last number 4 is from the print statement of the main method. This means all the words have counted since the word length now equals word length + 1. The full process is shown in the following gif,</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*HiOI84Dqo27ourryJQrlvw.gif" /></figure><p>So, using this method you can completely change the program behavior in the runtime (in debug time 😁) without compiling and running the code again.</p><p>That’s all for today guys. Hope you would use these tricks the next time you debug a code in Intellij Idea and save a lot of time. Happy coding 💻 !!!</p><p>And, if you’re a doing frontend development also, check out the following articles to debug like a pro in Chrome Dev Tools,</p><p><a href="https://tharakamd-12.medium.com/debug-like-a-pro-in-chrome-dev-tools-how-to-use-rendering-tab-ba25f5bb6264">Debug like a Pro in Chrome Dev Tools — How to use Rendering tab</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=6c3599117297" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Writing FreeMarker Templates in WSO2 MI]]></title>
            <link>https://tharakamd-12.medium.com/writing-freemarker-templates-in-wso2-mi-28e232ca7077?source=rss-3ab50117c198------2</link>
            <guid isPermaLink="false">https://medium.com/p/28e232ca7077</guid>
            <category><![CDATA[wso2]]></category>
            <category><![CDATA[freemarker]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[integration]]></category>
            <category><![CDATA[template]]></category>
            <dc:creator><![CDATA[Dilan Tharaka]]></dc:creator>
            <pubDate>Thu, 06 May 2021 05:23:45 GMT</pubDate>
            <atom:updated>2021-05-06T05:23:45.047Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Sg51HuYkjI2ohhFG9CuEwQ.jpeg" /><figcaption>Photo by <a href="https://www.pexels.com/@goumbik?utm_content=attributionCopyText&amp;utm_medium=referral&amp;utm_source=pexels">Lukas</a> from <a href="https://www.pexels.com/photo/turned-on-laptop-computer-574073/?utm_content=attributionCopyText&amp;utm_medium=referral&amp;utm_source=pexels">Pexels</a></figcaption></figure><p>In WSO2 Micro Integrator, there are various ways to transform or generate payloads. PayloadFactory mediator, DataMapper, Script mediator, and Smooks mediator are some of the mediators we can use to transform and generate payloads.</p><blockquote>Dont forget to check out this article about the CSV module, which is an easy-to-use module to transform CSV payloads.</blockquote><p><a href="https://tharakamd-12.medium.com/transforming-csv-in-wso2-mi-the-easy-way-dd1588763e7f">Transforming CSV in WSO2  MI— The Easy Way</a></p><p>In the latest MI release (MI 4.0.0), WSO2 introduced a new feature to the PayloadFactory mediator. It’s the Apache FreeMarker templating support.</p><p><a href="https://freemarker.apache.org/">FreeMarker</a> is a general-purpose templating engine developed by Apache. It’s a Java-based template engine that is very popular among Java developers to convert Java objects to HTML, XML, CSV, and other text-based formats.</p><p>To work with FreeMarker templating in PayloadFactory you have to get the latest version of WSO2 Integration Studio. To demonstrate this new feature, I’m doing the following JSON to XML transformation.</p><p>Input-</p><pre>{<br>&quot;first_name&quot;: &quot;John&quot;,<br>&quot;last_name&quot;: &quot;Deo&quot;,<br>&quot;age&quot;: 35,<br>    &quot;location&quot;: {<br>        &quot;state&quot;: {<br>            &quot;code&quot;: &quot;NY&quot;,<br>            &quot;name&quot;: &quot;New York&quot;<br>        },<br>        &quot;city&quot;: &quot;Manhattan&quot;<br>    }<br>}</pre><p>Output-</p><pre>&lt;user&gt;<br>    &lt;Name&gt;John Deo&lt;/Name&gt;<br>    &lt;Age&gt;35&lt;/Age&gt;<br>    &lt;Address&gt;Manhattan, NY&lt;/Address&gt;<br>&lt;/user&gt;</pre><p>I first opened the Integration Studio and created an integration project in that project I created a REST API and the sequence of the REST API is as follows,</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*uHe7sAnegZM2B4qo4MF_Og.png" /></figure><p>If I go to the properties view of the PayloadFactory mediator,</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*fHjgRhH8FQJGvZ5FOT_vYQ.png" /></figure><p>We can see, there’s a new property called Template Type. Using this property we can select the way we are going to write the template. If this is set to Default, then the PayloadFactory would work with the traditional syntaxes (Expressions with $ symbol). But here, I’m selecting the Freemarker template type. Then the PayloadFactory would accept a FreeMarker template as the payload template.</p><p>The FreeMarker template I developed to transform my JSON to XML is as follows,</p><pre>&lt;user&gt;<br>    &lt;Name&gt;${payload.first_name} ${payload.last_name}&lt;/Name&gt;<br>    &lt;Age&gt;${payload.age}&lt;/Age&gt;<br>    &lt;Address&gt;${payload.location.city}, ${payload.location.state.code}&lt;/Address&gt;<br>&lt;/user&gt;</pre><blockquote>💡 You can use online FreeMarker template tester provided by Apache to test the template while development. — <a href="https://try.freemarker.apache.org/">https://try.freemarker.apache.org/</a></blockquote><p>When I add this into the Integration Studio properties view, it shows like this,</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*rWwqicvaVFaXbLwQDxK2sw.png" /></figure><p>As you can see, Integration Studio provides syntax highlighting for FreeMarker templates 😲</p><p>That’s it 😁. We did a transformation in the PayloadFactory mediator using FreeMarker templates. It’s simple as that. Now, we can run our project in embedded Micro Integrator in the Integration Studio and send a request to the REST API and it would give us the desired output.</p><p>You might be wondering how to access payload, properties, and arguments of the mediation flow in the FreeMarker template. To do that, PayloadFactory provides built-in variables for the FreeMarker templates.</p><ul><li>payload — this variable represents the current payload of the mediation sequence.</li><li>ctx — this represents ctx properties</li><li>axis2 — this represents asix2 properties</li><li>trp — this is the variable to refer to transport headers</li><li>arg — using this, we can access defined argument in the PayloadFactory</li></ul><p>We can use these variables as starting points of our template. Following is another example FreeMarker template that is using these variables.</p><pre>{<br>&quot;ctx property&quot; : &quot;${ctx.user_name}&quot;,<br>&quot;axis2 property&quot;: &quot;${axis2.REST_URL_POSTFIX}&quot;,<br>&quot;trp property&quot;: &quot;${trp.Host}&quot;<br>}</pre><p>The output payload generated from this template is as follows,</p><pre>{<br>&quot;ctx property&quot;: &quot;john&quot;,<br>&quot;axis2 property&quot;: &quot;/demo&quot;,<br>&quot;trp property&quot;: &quot;localhost:8290&quot;<br>}</pre><p>The complete synapse code for this implementation is,</p><pre>&lt;property name=&quot;user_name&quot; scope=&quot;default&quot; type=&quot;STRING&quot; value=&quot;john&quot;/&gt;<br>&lt;payloadFactory media-type=&quot;json&quot; template-type=&quot;freemarker&quot;&gt;<br>    &lt;format&gt;&lt;![CDATA[{<br>        &quot;ctx property&quot; : &quot;${ctx.user_name}&quot;,<br>        &quot;axis2 property&quot;: &quot;${axis2.REST_URL_POSTFIX}&quot;,<br>        &quot;trp property&quot;: &quot;${trp.Host}&quot;<br>        }]]&gt;<br>    &lt;/format&gt;<br>&lt;args/&gt;<br>&lt;/payloadFactory&gt;</pre><p>That’s all about how to write FreeMarker templates in PayloadFactory and perform complex transformations. Refer to the official documentation for more details and examples. Happy coding 💻 🎉</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=28e232ca7077" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Transforming CSV in WSO2  MI— The Easy Way]]></title>
            <link>https://tharakamd-12.medium.com/transforming-csv-in-wso2-mi-the-easy-way-dd1588763e7f?source=rss-3ab50117c198------2</link>
            <guid isPermaLink="false">https://medium.com/p/dd1588763e7f</guid>
            <category><![CDATA[transformation]]></category>
            <category><![CDATA[wso2-ei]]></category>
            <category><![CDATA[integration]]></category>
            <category><![CDATA[wso2]]></category>
            <category><![CDATA[data]]></category>
            <dc:creator><![CDATA[Dilan Tharaka]]></dc:creator>
            <pubDate>Sun, 03 Jan 2021 13:20:50 GMT</pubDate>
            <atom:updated>2021-01-03T13:20:50.751Z</atom:updated>
            <content:encoded><![CDATA[<h3>Transforming CSV in WSO2 MI— The Easy Way</h3><p>CSV is a widely used content format in the digital world. In WSO2 MI you can use a number of mediators to transform a CSV payload. DataMapper, PayloadFactory, and Script Mediator are some of them. But now you have an even simpler and powerful way to transform your CSV payloads.</p><p>Previously in the tool pallet of the Integration Studio, you only had some pre-defined mediators. But with this new feature called modules, you can add more tools (Modules) into the tool pallet. Adding a module to Integration Studio is as easy as adding a connector.</p><p>For an easy transformation of CSV, WSO2 introduces a new module called CSV Module. This article is all about how to use the CSV Module.</p><p>The CSV module supports the following transformation,</p><ul><li>CSV to CSV</li><li>CSV to JSON</li><li>CSV to XML</li><li>JSON to CSV</li><li>XML to CSV</li></ul><p>In this article, I’m showing you how to perform a CSV to JSON transformation using the CSV module. To do that, I’m using a simple REST API. The easiest way to create an API in WSO2 MI is to use the Integration Studio getting started page.</p><h3>Generating the REST API</h3><p>First, You have to update the Integration Studio to the latest version. If you want to know how to get the latest updates, check out the <a href="https://ei.docs.wso2.com/en/latest/micro-integrator/develop/installing-WSO2-Integration-Studio/#get-the-latest-updates">docs</a>.</p><p>The Integration Studio latest version comes up with lots of really cool features 😍, The all-new getting started page is one of them.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*rvF0Ze-QXCG490BylSFidw.png" /><figcaption>Integration Studio Getting Started Page</figcaption></figure><p>Now you can easily create the project you need or start with a sample using this getting started page.</p><p>For this example, I’m creating a Rest API using the Hello World Service example in the getting started page.</p><p>To create the API project, click on the <strong>Hello World Service</strong> sample on the getting started page,</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*TgBMQII3x7F5bZ4-lFUC0Q.png" /></figure><p>Give a name for your project and click <strong>Finish.</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ZKAWS1WpaunvmOmBwG0fUA.png" /></figure><p>The <strong>Hello World Service </strong>example is a sample REST API project. You can see the Synapse configs of HelloWorld API in the design view bellow,</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*uv_lbMPnvbppoJQ8CyQ8AA.png" /></figure><p>Now, we’ll see how to import the CSV Module into our workspace.</p><h3><strong>Importing the CSV Module</strong></h3><p>The first step of importing a module is to create a connector exporter project. Right-click on the root project (csvTransformer) and in the menu click on, <strong>new -&gt; Connector Exporter</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*k5Y3HRc7mcJaNhKIblYRQA.png" /></figure><p>In the <strong>New Connector Exporter Project </strong>wizard, give a name for the connector exporter project and click <strong>Finish. </strong>Now, in Project Explorer, you can see the newly added connector exporter project. Then, right-click on the configs project (csvTransformerConfigs) and click on the <strong>Add or Remove Connectors/Modules </strong>from the menu.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/787/1*Gs50FuL3SsjmyFdnDYUGqg.png" /></figure><p>In the <strong>Add or Remove Connectors/Modules </strong>wizard, click on the <strong>Add connector/module </strong>option (this is the default option) and click <strong>Next</strong>. Then, in the search box, type CSV, and click on the search button. Now, you can see the CSV Module in the search results. Click on the download button to add the Module to the project. After the module is downloaded. Close the wizard by clicking the <strong>Finish</strong> button.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/865/1*6dAKpSVtyrsh2DjTwkwrXQ.png" /></figure><p>Then, right-click on the connector exporter project and click on the menu, <strong>New -&gt; Add/Remove Connectors.</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*uLPlgmroNLtPpKb0fhxwJQ.png" /></figure><p>In the <strong>Add or Remove Connectors/Modules </strong>wizard, click on the <strong>Add connector/module </strong>option (this is the default option) and click <strong>Next.</strong></p><p>Then click on the <strong>Workspace </strong>button to import the module from the workspace. In there, select the CSV Module and click <strong>OK</strong>. Then click on <strong>Finish</strong> to finish the wizard.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/862/1*SxrEiLbNa0k708oT4cZl9g.png" /></figure><p>Now, in the Design view, we’ll remove the Payload Factory mediator (since we don’t need that for this scenario) by clicking on it and pressing the <strong>Delete </strong>key in the keyboard.</p><p>Now in the <strong>Tool Palette </strong>you can see the CSV Module group.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*M9OxtKSXQduRxl2rzY4q-Q.png" /></figure><p>This group has all the operations supported by the CSV Module. Now, drag and drop the csvToJson operator into the design view before the Response mediator. After doing that, your design view should look like following,</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*OaX4Aun_0pb5k6QyRTCSxQ.png" /></figure><p>Great, we have successfully added the CSV Module to the project and added the csvToJson operator into the design view. Our next step is to config the operator. Before that, I’ll explain the transformation that I’m trying to do here.</p><h3><strong>CSV to JSON Transformation</strong></h3><p>I have the following CSV input,</p><pre>id,name,email,phone_number<br>1,De witt Hambidge,dwitt0@newsvine.com,true<br>2,Brody Dowthwaite,bdowthwaite1@delicious.com,false<br>3,Catlin Drought,cdrought2@etsy.com,608-510-7991<br>4,Kissiah Douglass,kdouglass3@squarespace.com,true<br>5,Robinette Udey,rudey4@nytimes.com,true</pre><p>And I need to transform this into the following JSON,</p><pre>{<br>    &quot;results&quot;: [<br>        {<br>            &quot;index&quot;: 2.0,<br>            &quot;name&quot;: &quot;Brody Dowthwaite&quot;,<br>            &quot;email&quot;: &quot;bdowthwaite1@delicious.com&quot;<br>        },<br>        {<br>            &quot;index&quot;: 3.0,<br>            &quot;name&quot;: &quot;Catlin Drought&quot;,<br>            &quot;email&quot;: &quot;cdrought2@etsy.com&quot;<br>        },<br>        {<br>            &quot;index&quot;: 4.0,<br>            &quot;name&quot;: &quot;Kissiah Douglass&quot;,<br>            &quot;email&quot;: &quot;kdouglass3@squarespace.com&quot;<br>        },<br>        {<br>            &quot;index&quot;: 5.0,<br>            &quot;name&quot;: &quot;Robinette Udey&quot;,<br>            &quot;email&quot;: &quot;rudey4@nytimes.com&quot;<br>        }<br>    ]<br>}</pre><p>Let’s see how to do this with the CSV to JSON operator in the CSV Module.</p><h3><strong>Configuring the operator</strong></h3><p>Click on the CSV to JSON operator that we added to the design view in the previous step. Now you can do the configurations in the <strong>Properties </strong>view.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*M2C97OMz8LTbQFAU9OkdZg.png" /></figure><p>The first thing we have to configure is the <strong>Header </strong>property under the <strong>CSV Input</strong> group. We need to set this to <strong>Present</strong> using the list box. By setting it to present, we are telling the CSV Module that our CSV has a header. Then it will map that header with the CSV input payload and we can use the header names to specify columns in other configurations.</p><p>Under the <strong>CSV Transformation </strong>group, you can now see the property <strong>Skip Headers </strong>after selecting the value <strong>Present </strong>to the <strong>Header </strong>property. Select value <strong>True </strong>for that property since we don’t need the header row of the input CSV in the output JSON.</p><p>In the output JSON, We don’t need the column <strong>phone_number</strong>. To skip that, we can use the property <strong>Skip Columns. </strong>In that property, we can specify the columns as a list ( 1,2), as a range (1:3), or as a combination of both ( 1,2,5:10). And since we have set the <strong>Header </strong>property to <strong>Present </strong>we can give the column names rather than column index. Therefore, to skip the column <strong>phone_number </strong>we can set the value of the <strong>Skip Columns </strong>as <strong>“phone_number”. </strong>Note that we have to use double quotes if we are giving the names of the columns.</p><p>If you observe the output JSON, not only the header row but the first data row in the CSV is omitted. This can be easily done with the CSV Module using the property <strong>Skip Data Rows. </strong>You can give the number of data rows to skip in this property. In this case, it’s 1.</p><p>Another change that I need in the output is that, Instead of using the CSV headers, I need different keys for the JSON. Since we have said that we have a header in the CSV, the default behavior of the CSV Module is to use those headers as the keys of the output JSON. But if we need our own keys, then we can specify them as a comma-separated list in the <strong>JSON Keys</strong> property. In this case, we are setting it as <strong>index,name,email.</strong></p><p>In CSV, we don’t have a way to define data types. All the values in a CSV payload are string values. But in a JSON we have the ability to use different data types such as string, numbers, and boolean. The CSV Module supports this data type conversion. We can use the <strong>Data Types </strong>property to set the desired data types. If we don’t specify the data types, the CSV Module considers them as String data.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*DGcsZqvtBPJlun9xKUP1dA.png" /></figure><p>As you can see in the above image, the Data types property is a table (You need to have the updated Integrations Studio to have this feature). You can add a new entry to the table by clicking the + button in the upper right corner. For our transformation, we need the <strong>id </strong>column in the CSV to be a number in the output JSON. We can specify that as given below,</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*mq6qL09--OBgz2xl6UnjAQ.png" /></figure><p>Finally, have you notices that the output JSON is not just a transformation of the CSV input. We have to wrap the results in a JSON object and specify the values under a key of<strong> results.</strong> This is a common scenario in most of the transformations. We can do this easily using the CSV Module by specifying the key of the wrapper object in the <strong>Root JSON Key</strong> property.</p><p>When we have set all the properties, the final properties view is like bellow,</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*2I2ED02KVdiPiMS1IKyBNg.png" /></figure><p>You can specify these properties in the source view also. The source view with the above configs are like bellow,</p><pre>&lt;CSV.csvToJson&gt;<br>    &lt;headerPresent&gt;Present&lt;/headerPresent&gt;<br>    &lt;skipHeader&gt;true&lt;/skipHeader&gt;<br>    &lt;columnsToSkip&gt;&quot;phone_number&quot;&lt;/columnsToSkip&gt;<br>    &lt;dataRowsToSkip&gt;1&lt;/dataRowsToSkip&gt;<br>    &lt;csvEmptyValues&gt;Null&lt;/csvEmptyValues&gt;<br>    &lt;jsonKeys&gt;index,name,email&lt;/jsonKeys&gt;<br>    &lt;dataTypes&gt;[{&quot;Column Name Or Index&quot;:&quot;id&quot;,&quot;Is Column Name&quot;:&quot;Yes&quot;,&quot;Data Type&quot;:&quot;Number&quot;}]&lt;/dataTypes&gt;<br>    &lt;rootJsonKey&gt;results&lt;/rootJsonKey&gt;<br>&lt;/CSV.csvToJson&gt;</pre><p>Now we are all set 😎. We can test this by running the project and sending the CSV payload into the API like bellow,</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*DJnrqKzA6gUm__TuPowXJA.png" /></figure><p>That’s how to transform a CSV to JSON in WSO2 MI ( Or EI ) using the CSV Module. The CSV Module can be used to do more transformations as I said before. As you can see, we didn’t use any code. That’s the main advantage of using the CSV Module. You have to write zero code lines to perform a complex transformation. So, consider using the CSV Module for your next payload transformation. Don’t think twice to comment if you have any hesitation 📝</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=dd1588763e7f" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Is it bad to use wildcard imports in Java?]]></title>
            <link>https://tharakamd-12.medium.com/is-it-bad-to-use-wildcard-imports-in-java-1b46a863b2be?source=rss-3ab50117c198------2</link>
            <guid isPermaLink="false">https://medium.com/p/1b46a863b2be</guid>
            <category><![CDATA[software-development]]></category>
            <category><![CDATA[clean-code]]></category>
            <category><![CDATA[code]]></category>
            <category><![CDATA[performance]]></category>
            <category><![CDATA[java]]></category>
            <dc:creator><![CDATA[Dilan Tharaka]]></dc:creator>
            <pubDate>Fri, 07 Aug 2020 14:45:43 GMT</pubDate>
            <atom:updated>2020-08-07T14:45:43.524Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*giKgaGTZOPyjWe6aSYi1RQ.jpeg" /><figcaption>Photo by <a href="https://www.pexels.com/@tomfisk?utm_content=attributionCopyText&amp;utm_medium=referral&amp;utm_source=pexels">Tom Fisk</a> from <a href="https://www.pexels.com/photo/birds-eye-view-photo-of-freight-containers-2226458/?utm_content=attributionCopyText&amp;utm_medium=referral&amp;utm_source=pexels">Pexels</a></figcaption></figure><p>Java developers use the wildcard (*) in import statements to import all the classes in a particular package. But in code reviews, most of you may have asked to remove those wildcard imports and add the actual class name.</p><p><strong><em>Is it really matter having wildcards in import statements in Java?</em></strong></p><p>Purpose of this article is to discuss the above problem. I’m approaching it in 2 ways,</p><ul><li>Performance</li><li>Clean code</li></ul><h3>Performance impact</h3><p>Before moving into the wildcard import performance, we’ll discuss the Java import statement.</p><p>Java import statement is a syntactical sugar. Import statement tells the compiler where to find the names ( Classes, static methods, etc.) used in the source code. Which means, if you use the fully qualified names in the source code, then you don’t need import statements💡</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/ed0ff771cdd2bc0f888e939425d9c0a7/href">https://medium.com/media/ed0ff771cdd2bc0f888e939425d9c0a7/href</a></iframe><p>In the above code block, I’ve used List and ArrayList classes from thejava.util package. We can write the same code without import statements as bellow.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/50e9dffaa24d1ca51083f2a3bfa1bb8f/href">https://medium.com/media/50e9dffaa24d1ca51083f2a3bfa1bb8f/href</a></iframe><p>In the above example, I’ve used fully qualified names for List and ArrayList classes. If we compile and run these codes, the output would be the same.</p><p><strong><em>Then what happens with the wildcard import?</em></strong></p><p>Wildcard imports tell java compiler to search for class names in the given package. Hence, using wild card imports, the compile-time performance may lower a bit. But it won’t be a notable impact in most cases. In run time, there’s no performance issue at all because these import statements are compiler directives and we can’t find them in the byte code. To clarify this we’ll write the above example with wildcard imports.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/591ac3ff1b38ee9b8e04a12f84bf2eb3/href">https://medium.com/media/591ac3ff1b38ee9b8e04a12f84bf2eb3/href</a></iframe><p>If we get the byte code of this class, it would look like following</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/ea3d41ca34e8df55a1059d73b81f6107/href">https://medium.com/media/ea3d41ca34e8df55a1059d73b81f6107/href</a></iframe><p>IF we compare it with the byte code of example 1 (Code with import statements), both have the same byte code. In the runtime, Java uses the byte code, not the source code. In the byte code, there’s no import statements. <strong>That clearly implies that using wildcard imports does not affect the runtime performance of a Java application</strong>.</p><h3>Coding best practices</h3><p>The main drawback of using wildcard imports in Java is possible naming conflicts. Let’s assume that we have developed an ArrayList class ourself which implements the java.util.List interface. But if we use the import statement import java.util.* as in example 3, Compiler would pickup the class java.util.ArrayList instead of our implementation. And sometimes, if we import using the wildcard and the developer of that imported package adds a new class or rename one to a class that we are already using in our code, then the code won’t compile due to class name conflicts.</p><p>Apart from the class name resolving issue, code readability would drop with the wildcard import since the developers don&#39;t get a clear idea of the actual classes using by the code. But if you are using a large number of resources from a package, using the wildcard looks cleaner sometimes.</p><h3>Conclusion</h3><p>Using wildcard imports in Java would not affect runtime performance of a Java program at all, but compile-time performance may be affected a little. In terms of clean coding, you have to use it wisely depending on the situation.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=1b46a863b2be" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[New features in Java 9 Stream API]]></title>
            <link>https://tharakamd-12.medium.com/new-features-in-java-9-stream-api-893c2f6951d8?source=rss-3ab50117c198------2</link>
            <guid isPermaLink="false">https://medium.com/p/893c2f6951d8</guid>
            <category><![CDATA[javastreams]]></category>
            <category><![CDATA[java9]]></category>
            <category><![CDATA[streaming]]></category>
            <category><![CDATA[java]]></category>
            <dc:creator><![CDATA[Dilan Tharaka]]></dc:creator>
            <pubDate>Fri, 26 Jun 2020 13:19:03 GMT</pubDate>
            <atom:updated>2020-06-26T13:19:03.081Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="Photo by ArtHouse Studio from Pexels" src="https://cdn-images-1.medium.com/max/1024/1*dahrfiiDTw-Ny0PBx4AKTQ.jpeg" /><figcaption>Photo by ArtHouse Studio from Pexels</figcaption></figure><p>Java Stream API introduced in Java 8 was a game-changer and almost every Java developer love using Streams. Since Java 8, the Stream API is improving in each Java release. In this article, I’m talking about the new features added to the Java Stream API in Java version 9.</p><h3>iterate</h3><p>Java 8 had this iterate method. In that method, we can give a seed value ( a starting value) and a function to generate the next value given the current value for the stream. But there was no way to give a termination condition for the stream.</p><pre>Stream.<em>iterate</em>(0, i -&gt; i + 1)<br>        .forEach(System.<em>out</em>::println);</pre><p>The above stream can be used as a replacement for a for loop like following,</p><pre>for (int i = 0; ; i++) {<br>    System.<em>out</em>.println(i);<br>}</pre><p>And in the loop, we can specify a termination condition like this,</p><pre>for (int i = 0; i &lt; 10; i++) {<br>    System.<em>out</em>.println(i);<br>}</pre><p>But in Java 8 Stream iterate method there’s no way to give a terminating condition. To replace the above loop in Java 8 Stream, we have to use a hack like following,</p><pre>Stream.<em>iterate</em>(0, i -&gt; i + 1)<br>        .peek(System.<em>out</em>::println)<br>        .allMatch(i -&gt; i &lt; 9);</pre><p>In Java 9, there’s an overload iterate method which takes a function to give terminating condition. Using the new iterate method, we can obtain the same functionality conveniently like following,</p><pre>Stream.<em>iterate</em>(0, i -&gt; i &lt; 10, i -&gt; i + 1)<br>        .forEach(System.<em>out</em>::println);</pre><p>Using the new iterate method, we can replace a for loop easily in Java Stream.</p><h3>takeWhile</h3><p>takeWhile acts like the <strong>break </strong>statement in a loop. Let’s consider the following for loop,</p><pre>int[] numbers = new int[]{1, 2, 10, 3, 7, 345, 55};<br>for (int i = 0; i &lt; numbers.length; i++) {<br>    if (numbers[i] &gt; 5) {<br>        break;<br>    }<br>    System.<em>out</em>.println(numbers[i]);<br>}</pre><pre>// Output<br>// 1<br>// 2</pre><p>This loop uses a condition and a break statement to break the loop when it meets an element that is greater than 5. Achieving this in Java 8 Stream is not an easy task ( this is not filtering. If filter, then number 3 would also be in the output). But in Java 9 we can use takeWhile to get the same result.</p><pre>int[] numbers = new int[]{1, 2, 10, 3, 7, 345, 55};<br>IntStream.<em>of</em>(numbers)<br>        .takeWhile(i -&gt; i &lt; 5)<br>        .forEach(System.<em>out</em>::println);</pre><p>As you can see takeWhile acts the same as the break statement. It continues until the given logic is true and when it gets the first element where the result is false, then the stream ends there.</p><h3>dropWhile</h3><p>dropWhile is somewhat similar to the takeWhile, but the difference is it drops the elements in the stream until the given statement is true, after that it provides all the other elements. See the following code to understand how it works,</p><pre>int[] numbers = new int[]{1, 2, 10, 3, 7, 345, 55};<br>IntStream.<em>of</em>(numbers)<br>        .dropWhile(i -&gt; i &lt; 5)<br>        .forEach(System.<em>out</em>::println);</pre><pre>//        Output<br>//        10<br>//        3<br>//        7<br>//        345<br>//        55</pre><p>In this code, dropWhile drops the first two elements since they are less than five, but the third element (10) is greater than 5. Therefore after the third element, it gives all the elements in the stream.</p><h3>ofNullable</h3><p>The ofNullable method in Java 9 Stream API returns a Stream of a single element if the element is not null. Following is a simple usage of the ofNullable,</p><pre>String value = null;<br>Stream.<em>ofNullable</em>(value)<br>        .forEach(System.<em>out</em>::println);</pre><p>This code would print nothing since the value is null. If we go to a more useful scenario of ofNullable consider following,</p><pre>Stream.<em>of</em>(1, 2, null, 4, 5)<br>        .flatMap(Stream::<em>ofNullable</em>)<br>        .forEach(System.<em>out</em>::println);</pre><p>In the above code, it avoids null values from the stream.</p><p>Those are the new features that came with Java 9 release to the Steam API. Using those features, we can replace almost every loop in Java codes effortlessly. If you have any interesting use case of them, don’t hesitate to leave a comment. Happy Coding 💻</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=893c2f6951d8" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[How to process CSV with WSO2 Class Mediator]]></title>
            <link>https://tharakamd-12.medium.com/how-to-process-csv-with-wso2-class-mediator-7a0fe5280933?source=rss-3ab50117c198------2</link>
            <guid isPermaLink="false">https://medium.com/p/7a0fe5280933</guid>
            <category><![CDATA[wso2]]></category>
            <category><![CDATA[integration]]></category>
            <category><![CDATA[wso2-ei]]></category>
            <dc:creator><![CDATA[Dilan Tharaka]]></dc:creator>
            <pubDate>Wed, 03 Jun 2020 03:53:46 GMT</pubDate>
            <atom:updated>2020-06-03T03:55:12.741Z</atom:updated>
            <content:encoded><![CDATA[<p>When using WSO2 Enterprise Integrator, the Class Mediator is a useful mediator. When there’s a scenario where we can’t use the existing mediators to resolve, we have to write a custom mediator using the Class Mediator. Class Mediator is a custom Java code written by the user, especially for a relevant task.</p><p>Purpose of this article is not to provide details on how to get started with Class mediators. If you’re not familiar with Class Mediator, then I found <a href="https://medium.com/wso2-enterprise-integrator-wso2-dev-studio-wso2/developing-and-debugging-custom-mediators-using-wso2-integration-studio-a3d974d1a2cf">this article</a> helpful to get started from scratch.</p><p>JSON, XML and CSV are the most common payload types that are used today in web services. In this article, I’m discussing how to process a CSV payload using Class Mediator. Following is a ClassMediator I developed to read CSV payload and make the second column of the CSV to upper case. Then set the modified CSV to the payload.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/a1710340c033d72dc6b512d6668ce9ae/href">https://medium.com/media/a1710340c033d72dc6b512d6668ce9ae/href</a></iframe><p>We can send a CSV payload via a web endpoint or using a file connector to read a CSV file. Either way, CSV payload is transferred as a text payload in the message context.</p><p>So, processing a CSV file in the Class Mediator means, we’re processing the text payload in the message context.</p><p>We can use <em>org.apache.synapse.util.PayloadHelper</em>( which is provided in default mediator project) to get and set text payload from the message context.</p><p>To process the payload, we can use any third party library ( in this code I used <a href="https://mvnrepository.com/artifact/com.opencsv/opencsv">OpenCSV</a>). If the library we use is not provided at the runtime of MI, then we would get a ClassNotFoundException. If so, we have to download the Jar file for the library and copy it to the MI_HOME/dropins (MI_HOME is the directory you installed Micro Integrator) directory and restart MI.</p><p>If you want to try out this example, clone following GitHub repository and open it in the <a href="https://wso2.com/integration/integration-studio/">Integration Studio</a>.</p><p><a href="https://github.com/tharakamd/classmediator-csv">tharakamd/classmediator-csv</a></p><h4>Reference</h4><ol><li><a href="https://ei.docs.wso2.com/en/latest/micro-integrator/overview/introduction/">Introduction to WSO2 Enterprise Integrator</a></li><li><a href="https://ei.docs.wso2.com/en/latest/micro-integrator/references/mediators/class-Mediator/">Class mediator documentation</a></li></ol><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=7a0fe5280933" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Get a public URL to your Localhost — The easy way]]></title>
            <link>https://tharakamd-12.medium.com/get-a-public-url-to-your-localhost-the-easy-way-28d70b1c64fe?source=rss-3ab50117c198------2</link>
            <guid isPermaLink="false">https://medium.com/p/28d70b1c64fe</guid>
            <category><![CDATA[development]]></category>
            <category><![CDATA[localhost]]></category>
            <category><![CDATA[web-server]]></category>
            <category><![CDATA[web-development]]></category>
            <category><![CDATA[api]]></category>
            <dc:creator><![CDATA[Dilan Tharaka]]></dc:creator>
            <pubDate>Wed, 08 Apr 2020 05:12:28 GMT</pubDate>
            <atom:updated>2020-04-08T12:57:21.296Z</atom:updated>
            <content:encoded><![CDATA[<h3>Get a public URL to Localhost — The easy way</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*s2Zkbf1r33ZL5AxW_fF-oA.jpeg" /><figcaption>Image by <a href="https://pixabay.com/users/geralt-9301/?utm_source=link-attribution&amp;utm_medium=referral&amp;utm_campaign=image&amp;utm_content=3537401">Gerd Altmann</a> from <a href="https://pixabay.com/?utm_source=link-attribution&amp;utm_medium=referral&amp;utm_campaign=image&amp;utm_content=3537401">Pixabay</a></figcaption></figure><p>If you’re a web developer or an API developer, then you should have wondered how to expose your localhost to the outside world. Some common scenarios are testing chatbots, testing browser notifications, testing your website on a mobile device, and you name it.</p><p>You may have tried techniques like remote port forwarding. But they have drawbacks like cost and security concerns. There’re lots of free services to do this for you. In this article, I’m explaining ngrok. Which I found pretty convenient to use, and it’s free.</p><p>Go to their website. <a href="https://ngrok.com/">https://ngrok.com/</a></p><p>Then create a free account, next go to the <a href="https://dashboard.ngrok.com/get-started">getting started</a> page. It has all the steps you need to follow.</p><p>Step 1 and 2 — Download and install ngrok in your PC.</p><p>Step 3 — Connect your account to ngrok by running following command</p><pre>./ngrok authtoken &lt;&lt;authtoken&gt;&gt;</pre><p>you have to replace &lt;&lt;authtoken&gt;&gt; with your auth token, but you can simply find the full command with your token in the getting started page.</p><p>That’s all set up, and then you’re good to go 😮</p><p>Let’s assume you’re running an Express REST API in port 3003, and you want to make it publicly accessible. Then all you have to do is running following command.</p><pre>./ngrok http 3003</pre><p>You have to replace 3003 with the actual port your service is running. Then you can see the following screen,</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/858/1*2UnGH7xUnHpRlveemaafNQ.png" /></figure><p>In there, I have marked two URLs. First one is the HTTP public URL, and the second one is the HTTPS public URL to your service. Simply copy and paste the URL you need, and that’s it. Your service in localhost now has a public URL 😎</p><p>They have provided a simple UI to monitor the network calls, which is very helpful. You can access it using the following URL</p><pre><a href="http://localhost:4040/">http://localhost:4040/</a></pre><p>But keep one thing in your mind. This public URL is changing every time you restart ngrok. If you want to stop it, then you have to go for a paid version.</p><p>If you require to password protect your URL, that can be done by modifying the above command to following,</p><pre>./ngrok http -auth=&quot;username:password&quot; 3003</pre><p>They provide another cool feature. You can host a fileserver with a public URL to host files in your local PC very easily. Jut run following command then, you would get your public fileserver (replace &lt;&lt;directory&gt;&gt; with the directory you need to share).</p><pre>./ngrok http -auth=&quot;user:password&quot; file://&lt;&lt;directory&gt;&gt;</pre><p>Apart from what I mentioned here, ngrok comes up with lots of other cool features, check out their <a href="https://ngrok.com/docs">documentation</a> page to find all of them.</p><p>Not only ngrok, but there’re several other free services available to do the same. Localhost.run is one of my favourites</p><p><strong>Localhost.run</strong></p><p><a href="http://localhost.run/">localhot.run</a> doesn’t require you to install anything or create an account. Just running a simple command like following in the terminal would do the job,</p><pre>ssh -R 80:localhost:3003 ssh.localhost.run</pre><p>This one is proper if you have a quick demo or a simple thing to do. The main drawback in this is, it doesn’t come up with all the fancy features ngrok offers and lack of configurations available.</p><p>You may find some other free and paid services that would do the same as ngrok. If you found a tool more interesting, feel free to share it by a comment.</p><p>Thank you for reading. Happy Coding 💻</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=28d70b1c64fe" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Debug like a Pro in Chrome Dev Tools — How to use Rendering tab]]></title>
            <link>https://tharakamd-12.medium.com/debug-like-a-pro-in-chrome-dev-tools-how-to-use-rendering-tab-ba25f5bb6264?source=rss-3ab50117c198------2</link>
            <guid isPermaLink="false">https://medium.com/p/ba25f5bb6264</guid>
            <category><![CDATA[performance]]></category>
            <category><![CDATA[web-development]]></category>
            <category><![CDATA[debugging]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[chrome]]></category>
            <dc:creator><![CDATA[Dilan Tharaka]]></dc:creator>
            <pubDate>Sat, 18 Jan 2020 10:29:36 GMT</pubDate>
            <atom:updated>2020-01-21T04:34:45.739Z</atom:updated>
            <content:encoded><![CDATA[<h3>Debug like a Pro in Chrome Dev Tools — How to use Rendering tab</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*RGdF6M45FhjbXBr5h7NxGQ.jpeg" /><figcaption>Printing takes time, even in your browser — Photo by <a href="https://unsplash.com/@bank_phrom?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Bank Phrom</a> on <a href="https://unsplash.com/?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Unsplash</a></figcaption></figure><p>Today frontend applications are becoming more complex. When the apps get bigger, they get slower. One of the main reasons behind a slower app is the large amount of rendering takes place in the browser. But how to find out where the problem is and how to fix it?</p><p>Chrome dev tools have this tab called <strong>Rendering,</strong> which is used to debug rendering related issues in JavaScript applications. To demonstrate its features, I’ve created a small application with Angular 7. It maintains a list of animal names. A new random name can be added by clicking a button. Names are generated using this awesome node package called, Unique Names Generator (<a href="https://www.npmjs.com/package/unique-names-generator">https://www.npmjs.com/package/unique-names-generator</a>) 💌</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/361/1*taDRslI4QL6pyEuBOb_skw.gif" /><figcaption>How the demo app works</figcaption></figure><p>Although this is a small application, There’re lots of renderings going behind the scene. How can we see what’s going on?</p><p>First, we have to open Chrome Dev Tools ( You know how to 😛 ). Then click on the three dots button on the upper right corner. Then in the menu, More Tools -&gt; Rendering.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/653/1*UNflmbJ_mPRYK3lfLwMA0A.png" /><figcaption>Navigate to Rendering tab</figcaption></figure><p>The initial view of the Rendering tab looks like bellow,</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/976/1*veBayADmzasRlHWkrrOD6Q.png" /><figcaption>The initial view of the Rendering tab</figcaption></figure><p>As you can see this has lots of features. But in this article, I’m focusing only on the <strong>Paint Flashing</strong> and <strong>Scrolling Performance Issue</strong> only.</p><p>Let’s first tick the paint flashing checkbox and see what&#39;s gonna happen.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/346/1*qA9KNfQQsz6xdK4wSgHqSg.gif" /><figcaption>Rendering issue</figcaption></figure><p>What’s this 😮</p><p>Basically what paint flash does is, it highlights the areas on the web page which are repainting. In our case. When I click on Add New button. The whole names list is getting repainted. Which affects the performance of the web applications terribly. And in the scrolling also, It renders The whole set of names again. Although this is a small application. This addresses the most common cause of the underperformance of web applications, unnecessary rendering, and bad scrolling.</p><p>What can we do to overcome this issue 😐</p><p>The real solution depends on your own code. But in this case, I can solve this by adding <strong><em>trackBy</em></strong> to Angular <strong><em>ngFor</em></strong>. You can find more details about <strong><em>trackBy</em></strong> in my previous <a href="https://medium.com/@tharakamd.12/avoid-unnecessary-rendering-on-angular-ngfor-with-trackby-f56ae5d79561">article</a>. After fixing that issue. The app looks like follows,</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/361/1*IVq9WHyU8OWIWsgtqYBNjg.gif" /><figcaption>After fixing the rendering issue</figcaption></figure><p>Cools, it no longer re-renders the whole list when adding a new element.</p><p>But the scrolling issue is still there 😫</p><p>In the rendering tab, there is a checkbox named <strong><em>Scrolling performance issues</em></strong>. By clicking it, Chrome gives more details on the scrolling performance issues.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/361/1*Co0OPUamd9ruxKRhnfMLAg.gif" /><figcaption>More insights on Scrolling performances</figcaption></figure><p>It shows a label ‘<strong><em>Repaints on scroll</em></strong>’ and highlights the scrolling area. Then we have to fix the ‘Repaints on scroll’ issue. When I got that I didn’t know how to fix that. So, I googled 👊. And found this article which describes it very precisely (<a href="https://dev.opera.com/articles/css-will-change-property/">https://dev.opera.com/articles/css-will-change-property/</a>). Fortunately, it had a simple fix of adding the CSS property <strong><em>will-change: transform </em></strong>to the scrolling container. And then, my final version looks like follows,</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/361/1*-9oJ6T1qJ4Lvk5k8nkaA6A.gif" /><figcaption>Final optimized version</figcaption></figure><p>Now it just repaints the newly added item and that’s it. It doesn’t repaint on scrolling also. 🆒</p><p>Since this is a small application we can’t see any noticeable improvement in performance after applying these fixes. But when your application gets bigger and bigger these kinds of issues affects significantly to the application performance. In that case, using the Chrome Dev Tools Rendering tab, you could easily find and fix your rendering performance issue.</p><p>I hope this trick would save you some time in developing large scale JavaScript applications. Happy Coding ⌨</p><p>If you want to try this out in your browser, find the code in the following repo,</p><p><a href="https://github.com/tharakamd/chrome-render-tool">tharakamd/chrome-render-tool</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=ba25f5bb6264" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[4 ways to implement a synchronized counter in Java]]></title>
            <link>https://tharakamd-12.medium.com/4-ways-to-implement-a-synchronized-counter-in-java-5540452fb4f7?source=rss-3ab50117c198------2</link>
            <guid isPermaLink="false">https://medium.com/p/5540452fb4f7</guid>
            <category><![CDATA[java]]></category>
            <category><![CDATA[multithread]]></category>
            <category><![CDATA[concurrency]]></category>
            <category><![CDATA[programming]]></category>
            <dc:creator><![CDATA[Dilan Tharaka]]></dc:creator>
            <pubDate>Tue, 27 Aug 2019 10:35:00 GMT</pubDate>
            <atom:updated>2020-07-24T16:04:20.875Z</atom:updated>
            <content:encoded><![CDATA[<h3>Four ways to implement a synchronized counter in Java</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*68fPZL6QCXC19wgqi32-MA.jpeg" /><figcaption>Photo by <a href="https://unsplash.com/@loic?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Djim Loic</a> on <a href="https://unsplash.com/?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Unsplash</a></figcaption></figure><p>A synchronized counter is a counter which can be updated using several threads. You may need to implement a synchronized counter in day to day programming tasks, and this is a common interview question in multithreaded programming. In terms of multithreaded programming Java provides many ways to implement synchronization. Let’s see how we can implement a synchronized counter in Java (For simplicity, we’re implementing a counter which only increments the value, but there’re situations to implement counters with both increment and decrement features).</p><p><strong>Solution 0 ( The wrong way)</strong></p><p>Let’s assume; I don’t know any synchronization methods in Java, then this is how I would implement a multithreaded counter.</p><p>This has a thread pool where each thread is trying to access the counter and increase it. There’s no control over the access to counter variable.</p><p>What would go wrong with this implementation?</p><p>The following is a part of the output generated by the above program.</p><pre>pool-1-thread-1 : 20<br>pool-1-thread-1 : 21<br>pool-1-thread-2 : 21<br>pool-1-thread-2 : 22</pre><p>In here thread 1 and thread 2 both are printing the same value. What happens here is both threads are accessing the counter variable at the same time, and both are updating its value. This is what we call a <strong>race condition</strong> in multithreading. To avoid this, we should synchronize access to the counter variable. Let’s see how to do that.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/a653ef23d1ca8d11d981a43e4d7c3d62/href">https://medium.com/media/a653ef23d1ca8d11d981a43e4d7c3d62/href</a></iframe><p><strong>Solution 1( Using a synchronized block )</strong></p><p>The most popular way of implementing synchronization in Java is using synchronized blocks, synchronized methods or using synchronized variables. In this code example, I’ve used synchronized block, but using the other two methods is also feasible.</p><p>In this code synchronized block is applying on the object lock. Since that object is shared between all the threads, only one thread can be inside of the synchronized block at one time.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/df4a00edc7b8a0794993a27f8dcf0a4f/href">https://medium.com/media/df4a00edc7b8a0794993a27f8dcf0a4f/href</a></iframe><p><strong>Solution 2( Using an atomic variable )</strong></p><p>Atomic variables are just like other variables in Java, but they have atomic operations such as incrementAndGet and compareAndSet . These operations are guaranteed to execute atomically, which means these operations would not get interfered in multithreaded environments. In Java, there’re several types of atomic variables such as AtomicInteger , AtomicBoolean and AtomicIntegerArray.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/172da955773fe3532acdece24b05d183/href">https://medium.com/media/172da955773fe3532acdece24b05d183/href</a></iframe><p><strong>Solution 3( Using a concurrent lock )</strong></p><p>java.util.concurrent.lock the package comes up with several implementations of Lock interface. These Locks are easier to use and more flexible than the synchronized blocks. Following code uses ReentrantLock. But depending on your needs, you may use another lock in your code.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/41308ccb702cd72227da9e44a83789dc/href">https://medium.com/media/41308ccb702cd72227da9e44a83789dc/href</a></iframe><p><strong>Solution 4( Using a Semaphore )</strong></p><p>Semaphores are one of the famous methods of achieving synchronization in programming. They control access to a shared resource using a counter. In the code below, you won’t see much difference in semaphores with locks. But keep in mind that a semaphore can be initiated with an initial count of more than 1 and can be used in scenarios where a thread should wait until the counter becomes zero.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/d6080f23d8e9aa53a5a0d106cc9d1453/href">https://medium.com/media/d6080f23d8e9aa53a5a0d106cc9d1453/href</a></iframe><p>In this article, I’ve discussed 4 different ways of implementing a synchronized counter in Java.</p><ul><li><strong>Synchronized Block</strong></li><li><strong>Atomic Variable</strong></li><li><strong>Concurrent Lock</strong></li><li><strong>Semaphore</strong></li></ul><p>These methods are not just for implementing synchronized counters but useful for all day to day multithreaded programming tasks.</p><p>If you want to learn more about Java multithreaded programming in Java, I recommend reading the following books</p><ul><li><a href="https://www.amazon.com/Java-Concurrency-Practice-Brian-Goetz/dp/0321349601/ref=sr_1_1?keywords=Java+Concurrency+in+Practice&amp;qid=1566891462&amp;s=books&amp;sr=1-1">Java concurrency in practice</a></li><li><a href="https://www.amazon.com/Java-Complete-Reference-Eleventh/dp/1260440230/ref=sr_1_2?keywords=Java&amp;qid=1566891598&amp;s=books&amp;sr=1-2">Java: The complete referenc</a>e (this is not only about multithreading, but it includes all details about concurrency in Java)</li></ul><p><strong>Happy Coding 💻</strong></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=5540452fb4f7" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>