FileCache – a simple cache of generated static content

Introduction

The FileCache sits at the core of the bankopladerne backend with the following responsibilities:

  • Maintain cache of files in the temporary file system while not using too much space
  • Ensure often-accessed content is cached
  • Ensure an item is only produced once in a high-traffic scenario where the same resource is requested concurrently
Continue reading
Posted in Bankopladerne, Java, Open Source | Tagged , , , | Leave a comment

Bankopladerne.dk upgrade April 2024

Bankopladerne.dk

So I have this “hobby site” called bankopladerne.dk. The main purpose is to expose free bankoplader for everybody to use, but there are also some (hidden) features only available to paying customers.

The “backend” primarily serves PNG’s and PDF’s based on the users needs and there are also “play and check” facilities both for the free cards and for paying customers where the paying customers gets access to their specific cards using a link with an encoded expiring access key.

By the way, a “bankoplade” is a “card” for playing the English version of Bingo: A 9×3 matrix where each column contains 1..3 numbers (increasing downwards) and each row contains exactly 5 numbers, numbers are in the range 1..90 where 1..9 is in the first column, 10..19 in the second … and 80..90 in the last column… Like:

Continue reading
Posted in Bankopladerne, Java, Personal | Tagged , , | Leave a comment

Binary Search

Wow that is ancient technology you might think – can more be written about that? Probably yes 🙂

So Binary Search has been around for as long as we’ve had sorted lists and the need to locate a specific element in the list. If you have studied Computer Science you most probably has come across binary search algorithms.

If you need to locate an element in a list and just start searching from one end you in average will do n/2 comparisons if there are n elements in the list. That is O(n) performance.

That is probably fine if the list always contains a limited number of elements or you don’t have performance as a requirement.

If the elements you are dealing with can be compared you can sort the list and use binary search to locate the element. The algorithm basically starts in the middle of the sorted list and if the element you are searching for is “smaller” than the middle element the algorithm repeats for the middle element in the first half of the list, and if it is “larger” it repeats in the middle for the last half of the list. And so on, until the element is located.

So with n elements in the list the algorithm does on average log2(n) comparisons. That is O(log n) performance. If there are 1 million elements in the sorted list the algorithm will do at most 19 comparisons before the element is found or it is determined that the element does not exist.

So why are we discussing Binary Search again? Doesn’t JAVA have binary search like built-in?

Absolutely for example Collections has a few variants and so does Arrays. But my use-case is a bit special so neither fits and I felt like rolling my own 🙂

So in my case I have a number of ranges given and I need to relatively fast locate a given number in one of the ranges. For example 4455000000 belongs to the range 4450000000..4459999999. In the particular challenge these strings of digits are actually not treated as numbers and they might actually have different lengths, so 44550000 also would belong to the range 4450000000..4459999999…

So I have a list of ranges, they can be sorted, and I have a string of digits and need to know which range it belongs to. That of course could be solved by normalizing my single string of digits and turn it into a short range like 4455000000..4455000000 and the standard Collections.binarySearch() would probably do nicely.

But that is no fun. Also the range contains other stuff than just the low and high values of the range and it would feel unnatural to turn my string of digits into a range. In my “day-time job” implementation (which is not the same as the one presented here) the normalization takes place in the comparison so we never have to bother about which length of the string of digits is the right. It could be 16, it could be 18 or even 19 – we don’t care and do not have to make a decision about that 😉

So I decided to roll my own completely generic BinarySearcher<T> that can do binary searches on sorted lists locating anything that can be compared to the elements in the list. It uses a BiComparator<T, K> because it needs to be able to compare elements of potentially different types. As the BinarySearcherSimpleIntegerTest illustrates it can also be used with simple types.

In my setup the BinarySearcher owns the list of elements to be searched because it makes best sense in my setup. But it can easily be turned into a static method and be used in other setups.

Enjoy 😀

Posted in Java | Tagged , | Leave a comment

Mutable

So you might wonder – “Mutable” – what is that all about?

In general we prefer things to be Immutable. With immutability comes improved performance because we do not have to copy things around and the JIT can do some optimizations. And there are less surprises, less WTFs because referenced things doesn’t suddenly change state.

One of my favorites is Optional. It either has a present value or is empty. Even after a few thousand lines of code a reference to an Optional is either empty or has the value it was initialized with. Love it! No surprises, no WTFs…

It also has a nice stream-like API with map() and filter() and whatnot.

Continue reading
Posted in Java | Tagged , | Leave a comment

Generating garbage…

At JCrete the last couple of years I’ve had the pleasure to be socializing with, among others, HFT people like Peter Lawrey and Martin Thompson.

Those HFT dudes really makes me thinking when I’m implementing stuff. Thinking about how much garbage I create.

In High Frequency Trading systems you cannot afford garbage collection and therefore they are doing lots of tricks to get around generating garbage.

With the stuff I’m doing a gc pause is generally not critical, so generating garbage is no big deal. Writing easy to read- and maintain code is more important.

But then again, it does not harm to think about garbage.
Continue reading

Posted in Fun, Java | Tagged , | 1 Comment

SafeEquals

I’m currently designing a user-authentication OAuth2 based service.

I’m trying very hard never ever to reveal anything about users or passwords. Credential-lookup by userid is always done twice. If the user is not found a known dummy-user is looked up instead. If the user is found, a “by guarantee not existing” user is looked up. Just to try to make the timings of existing and not-existing users the same.

Internally a lot of hashing takes place with random and known stuff. Valid and invalid users and passwords all take exactly the same “route”.

At the end, byte arrays are compared, and if they contain the same binary information, the user is authenticated.

In order to ensure this check timing-wise is independent of the outcome, a special equals(byte[], byte[]) method is implemented. Also, a rather special counterpart, the neverEquals(byte[], byte[]) is implemented. This does the exact same comparisons as the equals method, just the outcome is always false, to be used when the user is invalid. Continue reading

Posted in Java, Open Source | Tagged , | Leave a comment

YN

Yeah, I know, this isn’t rocket science. But it is rather nice 🙂

So, I’m working on this JEE project, backed by an Oracle database. Some of my work involves refactoring CMP 2.1 entities into JPA entities.

Here I’m faced with the issue, that sometimes true/false columns are modeled using CHAR(1) as ‘Y’ vs ‘N’. And at other times they are modeled using INTEGER as 1 vs 0. Sometimes both versions are using in the same table.

The natural datatypes in JPA are String and int. So I’m having code doing “Y”.equals(colum) and column == 1 etc. For the String I’ve generally been using an enum called YN having 2 constants, Y and N. So I can do column == YN.Y. Still a bit annoying though…

Just today I realized I could fix both issues with a simple version of my YN enum below. This works both for the CHAR ‘Y’ / ‘N’ case and the INTEGER 1 / 0 case.

And it’s damn simple containing 2 simple convenience methods that does make life simpler:

/**
 * Being used in JPA entities as @Enumerated(EnumType.STRING) or @Enumerated(EnumType.ORDINAL) (N=0, Y=1)
 */
public enum YN {
    N, Y;
 
    public final boolean bool() {
        return this == Y;
    }
    public static final YN bool(boolean b) {
        return b ? Y : N;
    }
}

Now I can do column.bool() and I can do YN.bool(something) the other way. And the mapping in the JPA entity takes care of the rest.

Nice and simple, definitely not rocket science.

Posted in Databases, Java | Tagged , , , , , , | Leave a comment

Unit testing really simple implementations

As an independent subcontractor (freelance consultant) I get to work in various very different organisations, with very different approaches towards testing in general and unit-testing in particular.

Some places unit-testing is mandatory and there might even be a code-coverage tool where you must strive to meet a specific threshold.

At other places unit-tests are more like integration tests, where the unit-testing is considered implicit in the integration-unit-tests.

And then there are places where unit-tests are of your own choice, as long as you do not spend too much (measurable) time on them.

In the latter cases you as an experienced developer might feel that unit-testing very simple stuff is superfluous.

Not so! In my experience, it is when you do these simple no-brainer implementations that you make mistakes, simply because you do not have to think. Also, unit-testing simple utility stuff demonstrates corner-cases that might suddenly be used or mis-used in the code where changing even the simplest implementation might break important business logic

In this post I will go through a few very simple utility methods that I’ve worked on recently and talk about some of the finer details – and why unit-testing even the simplest is really important.

Continue reading

Posted in Java | Tagged , | Leave a comment

Bad sample code…

Okay, I just HATE bad code.

I’m not religious about how you express your business logic in the code. I don’t mind “different” indentation or long methods although I try to keep my own code as short and simple as possible. I prefer lots of small classes over a few huge, and I prefer a bunch of short methods (“a screenfull”) over a few huge ones. Complex IF-clauses formed as simple methods etc.

Whenever I’m maintaining code developed by others, I accept the previous developers different habits. I might rewrite (also known as refactoring) parts, especially if there are unit-tests. But only if I think it would benefit the quality and maintainability of the code.

But, when reading books or articles I get really REALLY annoyed when I encounter bad code and bad habits in samples etc. Whenever I come across these things, where the quality is as if the developer is not really a JAVA developer, absentminded or just plain drunk, I loose interest in the rest of the content. As if, if the code is really bad (and untrustworthy) how can I then trust the rest of the content?

Continue reading

Posted in Fun, Java | 1 Comment

Handing over JAVA components to L3 support…

Java Specialist13:13

Hi

Udby, Jesper13:13

Hi

Java Specialist13:13

we are unable to build with existing set up

can you give us ear file to me

Udby, Jesper13:14

no, unfortunately not, I’m working from home today and do not have access to my dev env

Java Specialist13:14

i will put in weblogic server

Udby, Jesper13:14

I suggest you configure you’r maven setup to use the proper repo so you can build it. It has to be done anyways…

Java Specialist13:15

can you share your maven to me

_

folder

Udby, Jesper13:15

no… its on m dev env too…

the settings.xml linked from the document i sent you contains information about the repo – you should be able to use that?

which weblogic server btw?

Java Specialist13:18

weblogic server 11gR1

Udby, Jesper13:18

you have a local setup?

Java Specialist13:18

yes

Udby, Jesper13:18

nice:)

you got a local db setup too?

Java Specialist13:19

datasouce

Udby, Jesper13:20

yes, datasource. does it point to a local db or one of our dev environments?

Java Specialist13:20

yes

Udby, Jesper13:21

do you have a local db?

Java Specialist13:21

yes

Udby, Jesper13:21

nice:)

how’s the schema setup?

Java Specialist13:21

there

Udby, Jesper13:21

there?

Java Specialist13:21

yes

Udby, Jesper13:22

ok, i rephrase: how are the users/schemas, tables, sequences etc setup?

DDL

Java Specialist13:22

i will give schema name

usern and password

yes

Udby, Jesper13:23

i don’t know if you are aware of it, but “we” are using what is called “database migration” scripts to do the DDL

Java Specialist13:24

1 min

Udby, Jesper13:24

1 min what?

Java Specialist13:25

wait

database migration is no there with me

Udby, Jesper13:27

well thats not important if you are able to setup the schema/users, tables etc yourself

I was just wondering where you got the details from

Java Specialist13:28

webservice setup document

Udby, Jesper13:29

ok

Java Specialist13:31

Do you have this type of document with you_

Udby, Jesper13:32

nope, not here, might have it in the office…

Java Specialist13:32

ok

is there anu body who will share information_

Udby, Jesper13:33

what information?

Java Specialist13:34

regadring configuartion of java compenent with oSB

Udby, Jesper13:34

what component and what kind of configuration?

Java Specialist13:36

two ear file ie two java componet and after that i will confuge jMS queue with SFDC 

and OSB

Udby, Jesper13:36

first, there are more than two components, you need to tell me which components you need information about

then I can tell you which datasouces and jms queues are necessary

but you can see that in the code, if you like?

take the xxx-feed-parser (from memory, you can see the proper name in the poms/mail/source etc)

Java Specialist13:38

ok

Udby, Jesper13:38

it needs a jms queue, can’t remember the jndi name, but it’s in the annotation in the MDB

and it needs a datasource, can’t remember the jndi name, but its in the persistence.xml file

Java Specialist13:39

i have JMSQUEue is there with me

Udby, Jesper13:39

what?

Java Specialist13:40

while i am doing yYYY project

Udby, Jesper13:40

ok, good, but its probably not the same queue:)

Java Specialist13:41

ok

Udby, Jesper13:41

you can see the jndi name of the jms queue in the annotation in the mdb – message driven bean

Java Specialist13:44

1min

wait

i am opening weblogic server

Java Specialist13:53

yes

Udby, Jesper13:54

yes?

Java Specialist13:55

i am able to jndi nameof the JMS queue

for YYYY

Udby, Jesper13:55

ok

I wasn’t referring to the jndi name of the jms queue in yyyy

you do know what an annotation is, right?

Java Specialist14:00

no

Udby, Jesper14:00

ok, you are in trouble

do you know what a message driven bean is?

Java Specialist14:01

yes

Udby, Jesper14:03

good

in the mdb (aka message driven bean) there is an annotation that tells it which jms queue it should connect to

i can’t remember the proper name of it, but somewhere in the xxx-feed-ejb (i think it is) there is the java source of a message driven bean – I even think it is called something like Xxx…Mdb or similar. In that source you can find the annotation containing the name of the jms queue

also, in that project, there is a persistence.xml – you do know what that is?

Java Specialist14:06

jesper please share me the ear file to deploy javacomponents in weblogic server

Udby, Jesper14:06

sorry, i cant

you should configure your maven to use the proper repo, then you can build it yourself

btw: there is more than one ear

one component = one ear

 

Posted in Fun, Java | Leave a comment