Archive for the ‘Java’ Category

Generate random UUID token that starts with what you want

May 31, 2018

I wanted to personalize my environments by assigning them unique tokens which will be used by consumer APIs. To generate those tokens, I wrote a simple program on Java:

import java.util.UUID;

public class RandomStringUUID {
    public static void main(String[] args) {

        while(true) {
            // Creating a random UUID (Universally unique identifier).
            UUID uuid = UUID.randomUUID();
            String randomUUIDString = uuid.toString();

            if (randomUUIDString.startsWith("c001")) {

                System.out.println("UUID token = " + randomUUIDString);
                System.out.println("UUID version       = " + uuid.version());
                System.out.println("UUID variant       = " + uuid.variant());

                break;
            }
        }
    }
}

Output:

UUID token = c001ab8b-4224-4530-bb93-0fab6ad3f83b
UUID version = 4
UUID variant = 2

Process finished with exit code 0

Now, looking at the token I can tell to which environment it belongs. And my log analyzer too 🙂

Java and Big Data

March 22, 2016

Recently I came across an interesting question at Quora. Someone was asking which is better to learn as a fresher, Java or Big Data. Most of the answers were about comparing apples to apples and oranges to oranges, which really makes perfect sense but doesn’t help the inquirer.

Over my 10+ years of experience with Java, I used it across many platforms and domains and believe there are many more applications of it I can not even imagine. On the other hand, Big Data is promising and fancy. I’m pretty sure it’s possible to have it all and learn them both.

I think the question is more about how soon you can earn money with your knowledge. I believe, earning money in IT doesn’t differ much from any other R&D industry: first you invest your time and your mind, as much as you can, and then convince an employer to pay you rather than your competitors.

Talking about competition, Java as a buzzword is much older than Big Data. There are crowds of people having Java in their resumes for 10-15 years and, what’s more important, deep and extensive experience with it. Alternatively, I personally know few people with a PhD degree in Big Data whom I wouldn’t dare to compete with. Luckily, they are not looking for a job, because they are booked out for few years ahead with sophisticated projects for banks, insurers and government.

So my opinion is, to be able to do the job in these days – study them both and a platform like Hadoop as well. But put the Big Data first in your resume.

Calling Spring web services from jQuery

November 11, 2011

I love both frameworks because they are awesome. So here is how you can make Spring 3.0 and jQuery 1.5 work smoothly together, letting you forget about integration pain and concentrate on actual functionality.

Annotate your Spring service like:

@Controller
public class BankController extends BaseJSONController implements BankControllerIntf {
	@Autowired
	ConfigurationDaoIntf dao;
	@Override
	@SessionRequired
	@RoleRequired(UserRole.user)
	@RequestMapping("/ebank/paymentList")
	public @ResponseBody Map<String, String> getList(Session session,
			@RequestParam("type") String type,
			@RequestParam(value="report", required = false) String report,
			HttpServletResponse response) {
               // backend functionality here
......}
...}

BTW don’t forget to instruct Spring to resolve the @Controller-annotatated services in the applicationContext.xml:

<context:component-scan ....>
    <context:include-filter expression="org.springframework.stereotype.Controller" type="annotation"/>
...

and include Jsonp filter to the server configuration, just next to the Hibernate Open Session In View Filter.

Then make jQuery call it:

$(document).ready(function(){
		    $.getJSON("/appcontext/ebank/paymentList?callback=?",
					{ "type": $("input[name=type]").val(),
					  "report": $("input[name=report]").val()
					},
				    function(data) {
						// UI manipulation here
						var options = $("#portfolioId");
						$.each(data, function(i, object) {
							options.append($("<option />").val(i).text(object));
						});
						$("#imageLoading").hide();
						$("#portfolioId").fadeIn();
			        });
	    });

and it will deliver the data straight to your web page. That’s it!

Spring 3 dependency management issue

September 28, 2010

In 2010, Spring 3 was released and it was a significant event for all Java devs.

An open issue in Spring 3 is dependency (jar) management. Doc says to use external framework like Maven, ivy or OSGi. Having no experience with ivy, I started digging towards the other twos. Maven is what I love and successfully use for the last 2 years.

Mentioning here OSGi was quite a surprise for me. But the more I learned about the framework, the more I liked the approach it offers. Actually publish-find-bind services model is not new (there was UDDI 10 years ago) but keeping it in line with Java 5 and EJB 3 and being supported by Eclipse, SpringSource, Glassfish, JBoss, Weblogic and Websphere makes it a powerful competitor to Maven.

Also, there’s something interesting gonna happen around web UI in the next versions of Spring. They ditched support for Struts 1 so they have to offer something instead to take care for C in MVC. Webflow seems pretty perspective but  heaps of XML config must go to make the development less painful. It longs to be replaced with annotations – and they started to move towards it with @MVC. Web UI components should be managed and reused somehow and it’s absolutely unclear how to do it with screens, namespaces, templates, .js, .css, etc.

Configuring project properties in Spring

July 6, 2009

It is useful to have all project properties in a text file and make Spring care of objects initialisation, especially if you have multiple environments (dev, stage, prod, etc). For this, I use placeholders like ${mybean.itsproperty} in spring-config.xml – just add a couple of configurers there:

< bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">

< property name='location'>< value>config/common.properties< /value>< /property>

< /bean>

< bean class="org.springframework.beans.factory.config.PropertyOverrideConfigurer">

< property name='location'>< value>config/env.properties< /value>< /property>

< /bean>

Then I can use placeholders like:

< bean id="coolDS" class="org.springframework.jdbc.datasource.DriverManagerDataSource">

< property name="driverClassName">< value>${coolDS.driverClassName}< /value>< /property>

...

< /bean>

< bean id="config" class="com.mycompany.Config" scope="singleton">

< property name="incoming_directory">< value>${config.incoming_directory}< /value>< /property>

...

< /bean>

Note that placeholders should be in format beanName.propertyName if you want to override some environment-specific properties.

Top code comments

May 9, 2009

The best code comments I’ve ever seen:

// I dedicate all this code, all my work, to my wife, Darlene, who will
// have to support me and our three children and the dog once it gets
// released into the public.

/*
* You may think you know what the following code does.
* But you dont. Trust me.
* Fiddle with it, and youll spend many a sleepless
* night cursing the moment you thought youd be clever
* enough to “optimize” the code below.
* Now close this file and go play with something else.
*/

//When I wrote this, only God and I understood what I was doing
//Now, God only knows

Long John; //Silver

//Don’t edit — This configuration file is automatically generated by magic…

.xlsx format (MS Excel 2007)

February 6, 2009

Today I’ve sorted out parsing of the new MS Excel 2007 .xlsx format. It turned out to be just a .zip archive with a bunch of .xml files inside:

\_rels\.rels
\docProps\core.xml
\docProps\app.xml
\xl\_rels\workbook.xml.rels
\xl\externalLinks\_rels\externalLink1.xml.rels
\xl\externalLinks\externalLink1.xml
\xl\printerSettings\printerSettings1.bin
\xl\theme\theme1.xml
\xl\worksheets\_rels
\xl\worksheets\sheet1.xml
\xl\worksheets\_rels\sheet1.xml.rels
\xl\calcChain.xml
\xl\workbook.xml
\xl\sharedStrings.xml
\xl\styles.xml
[Content_Types].xml

As it’s expected, the data I need are in \xl\worksheets\sheet1.xml file, which is ordinary XML file:

<?xml version=”1.0″ encoding=”UTF-8″ standalone=”yes” ?>
– <worksheet xmlns=”http://schemas.openxmlformats.org/spreadsheetml/2006/main&#8221; xmlns:r=”http://schemas.openxmlformats.org/officeDocument/2006/relationships”&gt;
………
– <cols>
<col width=”20.7109375″ />
…….
</cols>
– <sheetData>
– &lt row r=”2″ spans=”2:12″ s=”9″ customFormat=”1″ ht=”23.25″>
– <c r=”B2″ s=”22″ t=”s”>
<v>16< /v>
</c>

So I think parsing this in Java will be easier than parsing old .xls format using Apache POI

SVN client for Eclipse update

January 12, 2009

After updating the project from repository using Ant script (and upper version of svn client – 1.5), I came through the problem:

org.tigris.subversion.javahl.ClientException: svn: This client is too old to work with working copy XXXXX; please get a newer Subversion client

The cause is that svn client 1.5 changes the svn setup files so svn client 1.4 can not read them anymore. The remedy is installing new version of Subclipse plugin, here the process is described:
subclipse: Installation

Exception interception

November 14, 2008

Sometimes you need to perform a specific action on some kind of Exception. For example, if you have multiple calls of domainRegistryService, and in case of unsuccessful registration it needs to write the reason to the database. For this task you can use Spring to create an interceptor.

This is the interceptor class. afterThrowing implements ThrowsAdvice and is called in case of Exception, afterReturning implements AfterReturningAdvice and is called in case when method is completed successfully. getOrder is needed if you have more than one interceptor

public class DomainRegistryInterceptor implements ThrowsAdvice,
		AfterReturningAdvice, Ordered {

	private static final long serialVersionUID = 1L;
	private static final Logger logger = Logger
			.getLogger(TransactionDescriptionInterceptor.class);

	public void afterThrowing(Method method, Object[] args,
			DomainRegistryService target, Exception ex) throws Exception {
		logger.debug("=== TransactionDescriptionInterceptor.afterThrowing ===");

		logger.debug("=== target === " + target.getClass().getName());
		logger.debug("=== method === " + method.getName());

		if (args != null && args.length > 0 && args[0] instanceof Domain
				&& ((Domain) args[0]).getTransactionId() != null)

			target.updateTransactionDescription(((Domain) args[0])
					.getTransactionId(), ex.getMessage());
		throw ex;
	}

	@Override
	public void afterReturning(Object arg0, Method method, Object[] args,
			Object target) throws Throwable {

		logger.debug("=== TransactionDescriptionInterceptor.afterReturning ===");

		logger.debug("=== target === " + target.getClass().getName());
		logger.debug("=== method === " + method.getName());

		if (target instanceof DomainRegistryService) {
			DomainRegistryService domainRegistryService = (DomainRegistryService) target;

			if (args != null && args.length > 0 && args[0] instanceof Domain
					&& ((Domain) args[0]).getTransactionId() != null)

				domainRegistryService.updateTransactionDescription(
						((Domain) args[0]).getTransactionId(),
						"Command completed successfully");
		}
	}

	@Override
	public int getOrder() {
		return 3;
	}

}

Then you have to bind this interceptor to your service (or services). Here’s a Spring configuration file:

	< !-- Service-->
	< bean id="service.domain.domainRegistryService"
		class="com.yourcompany.service.domain.impl.DomainRegistryServiceImpl">
	< /bean>

	< !-- INTERCEPTOR -->
	< bean id="domainRegistryInterceptor"
		class="com.yourcompany.spring.DomainRegistryInterceptor">
	< /bean>

	< !-- Auto-Proxy -->
	< bean id="domainRegistryProxyCreator"
		class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
		< property name="beanNames">
			< list>
				< idref bean="service.domain.domainRegistryService" />
			< /list>
		< /property>
		< property name="interceptorNames">
			< list>
				< idref bean="txInterceptor" />
				< idref bean="methodCallAccessInterceptor" />
				< idref bean="domainRegistryInterceptor" />
			< /list>
		< /property>
		< property name="order" value="3" />
	< /bean>

The linking is performed in the Auto-proxy part. Service is listed under beanNames property, interceptor goes to interceptorNames property, order defines the order of this proxy among other proxies in the application.

Background of Spring auto-proxy is explained here.

Initializing an object by reference

October 21, 2008

As everyone knows, in Java objects are passed to methods by reference (the only exclusion is String). But if the reference is null, the method does not know where to return the object.

So following code produces ‘Exception in thread “main” java.lang.NullPointerException’:

	public static void main(String[] args) {
		User u = null;
		initializeUser(u);
		System.out.println(u.getId());
	}

	private static void initializeUser(User u) {
		u = new User(1L);
	}

more, if you change the code to

	public static void main(String[] args) {
		User u = new User(2L);
		initializeUser(u);
		System.out.println(u.getId());
	}

	private static void initializeUser(User u) {
		u = new User(1L);
	}

The main method prints 2. Why the user isn’t initialized?