TALENTA DAILY NEWS

Facebook Coin could be launched much sooner than expected. According to a recently published New York Times (NYT) report, which cites multiple anonymous sources, Facebook employees are already…

Smartphone

独家优惠奖金 100% 高达 1 BTC + 180 免费旋转




Creating a Java web framework from scratch

Why ?

Well, that’s a good enough question to start with, with all the existing solutions, why would I want to create my own ?

After some digging we couldn’t find any suitable solution; that’s where the second arguments comes in!

2. It’s a very interesting project ! Thats it. 🚀

Originally being a Symfony developer, I knew how the framework was working in the background, and some features were in my opinion mandatory:

To be as extensible as possible, each service would be defined by:

Consider the following example :

Service 1 needs Service 4 and Service 2

Service 2 needs Service 4 and Service 3

The natural instantiation order would be; (Service 3 /Service 4 ) then Service 2 and finally Service 1 . Straightforward.

Okay, that was an easy one… What about this one:

Yes, a circular reference, or in other words an infinite loop. ➿

To prevent the Container instantiator from infinite looping over these two poor services, circular references had to be detected before doing anything else.

First of all, a bit of vocabulary:

Definitions inspired from symfony.com

A very cool feature of Symfony is that it allows you to write the following:

What is happening here ?

In the controller MyController the showAction is protected by a condition: is the current user allowed to show the post ?

I thought having such a feature in Java would be great !

Java has out-of-the-box an engine that can run Javascript. Great ! No need to reinvent the wheel !

But, wait, one small issue. In Javascript, when you call a function; just its name matters, not its signature. It’s the opposite from Java. But the way the ScriptEngine binds the Javascript calls to the actual Java code takes into account the signature. 😐

The Java ScriptEngine allows you to inject variables as a HashMap , ie:

But what if you want to inject this behavior :

That’s the problem, you can’t. Or… can you ?!

The solution is to use aFunctionalInterface and Java 8 default methods in interfaces.

This example is a very simple case, but in more complex cases having one method name per signature can be a real nightmare.

That allows me to register the Binding, as they call it, like this:

And to have my expression that looks like this:

Or like that :

Clean 👌. Problem solved ! 🔥

Now the exact same behavior can be created with an @Security annotation on our Routes !

Star the project, Clap the Story and Spread the Word !

Add a comment

Related posts:

The New Cold War

The Cold War dominated global politics between the end of the Second World War and the early 1990s. At the forefront of this war was the threat of thermonuclear war. After the United States developed…

Do The Raptors Have A Shot At Kawhi

The NBA world seems to be at a standstill right now as we all await Kawhi Leonard’s decision on where he’ll sign this off-season. Before this season started Leonard had one destination in mind, Los…

Oche

Silly little dictionary! Don’t you know oche can’t possibly be a word if the New York Times says it ain’t? Those of you who play darts on a regular basis or watch darts tournaments on ESPN5 or just…