Home » Author Archives: Ilias Tsagklis (page 20)

Author Archives: Ilias Tsagklis

Ilias Tsagklis
Ilias is a software developer turned online entrepreneur. He is co-founder and Executive Editor at Java Code Geeks.

Import package in script

In this example we shall show you how to import a package in script. We are using the ScriptEngine interface, that provides methods for basic scripting functionality. To import a package in script one should perform the following steps: Create a StringBuilder to build the script. Append to it all the commands to be executed, along with the import package command. Get ...

Read More »

Invoke specific script function

This is an example of how to invoke a specific script function. We are using the ScriptEngine interface that provides methods for the basic scripting functionality. Invoking a script function with Java implies that you should: Create a new ScriptEngineManager. The ScriptEngineManager implements a discovery and instantiation mechanism for ScriptEngine classes and also maintains a collection of key/value pairs storing state ...

Read More »

Java EE 6 Decorators advanced usage

The example we’ll use is a Social media feed processor. So I have created an interface:   public interface SocialFeedProcessor { Feed process(String feed); } and provided 2 implementations, twitter and google+   public class TwitterFeedProcessor implements SocialFeedProcessor{ @Override public Feed process(String feed) { System.out.println("processing this twitter feed"); // processing logics return new Feed(feed); } } public class GooglePlusFeedProcessor implements ...

Read More »

Implement a shopping cart using the Strategy Pattern

First a definition: in the simplest terms, you can define the Strategy Pattern as telling an object to do a job and to do it using ANOTHER object. To clarify this further I’m going to redesign the ShoppingCart slightly, by giving it a pay()* method: public class ShoppingCart { private final List<Item> items; public ShoppingCart() { items = new ArrayList<Item>(); ...

Read More »

Logging aspect example

Once upon time I was writing a plugin for a closed-source LMS J2EE application having such dependencies that it wasn’t feasible to run it locally. During an API call, a method deep inside the application was failing but the exception didn’t contain enough information to track the cause of the problem. I therefore needed to change the method to log ...

Read More »

Create a simple BasicDataSource object

With this example we are going to demonstrate how to create a simple org.apache.commons.dbcp.BasicDataSource object, that is the basic implementation of javax.sql.DataSource that is configured via JavaBeans properties. In short, to create a simple BasicDataSource object you should: Create a BasicDataSource object and configure the database. Use setDriverClassName(String driverClassName) method to set the jdbc driver class name. Use setUrl(String url) method ...

Read More »

Get list of files from FTP Server

In this example we shall show you how to get a list of files from an FTP Server. We are using the org.apache.commons.net.ftp.FTPClient Class that encapsulates all the functionality necessary to store and retrieve files from an FTP server. To get a list of files from an FTP Server one should perform the following steps: Create a new FTPClient. Use connect() API ...

Read More »

Download file from FTP Server

This is an example of how to download a File from an FTP Server, using the org.apache.commons.net.ftp.FTPClient Class, that encapsulates all the functionality necessary to store and retrieve files from an FTP server. Downloading a File from an FTP Server implies that you should: Create a new FTPClient. Use connect() API method to open a connection to the FTP Server. ...

Read More »

Check if Days and Calendars match

With this example we are going to demonstrate how to check if Days and Calendars match. We are using the org.apache.commons.lang3.time.DateUtils Class, that is a suite of utilities surrounding the use of the Calendar and Date object. DateUtils contains a lot of common methods considering manipulations of Dates or Calendars. In short, to check if Days and Calendars match you should: ...

Read More »

Generate random alphanumeric string

In this example we shall show you how to generate random alphanumeric String objects. We are using the org.apache.commons.lang3.RandomStringUtils class, that offers operations for random Strings. To generate random alphanumeric String objects one should perform the following steps: Use random(int count, boolean letters, boolean numbers) method to create a random string whose length is the number of characters specified. Use randomAlphabetic(int count) ...

Read More »