Our app runs on a Weblogic server (WLS) and talks to another WLS-hosted app via SOAP-Webservice.
When the other app was patched to a new version, some WS-requests failed indicating that the webservice in the other wanted to JNDI-lookup the previous version of something.
After we restarted our app everything worked fine. But still you don’t want to restart a client when the server was upgraded. Moreover, a spring-boot client did not have these problems.
It turned out that WLS uses a feature called “Context Propagation” that inserts an additional SOAP-Header into the request as well as the response. This header contains a serialized object. It indicated that our app transmits the version of the other app and apperantly the other app somehow uses that information in the JNDI lookup.
How does our app knows about the version number of the other app? Probably because the other apps sends that info in the response. This explains why it worked after we restarted our app: at first it hadn’t that information at all and when it got it, it was about the new version.
What I still can’t explain is that some request were successful before the restart.
The solution is to disable “context propagation” by using a system parameter:
weblogic.wsee.workarea.skipWorkAreaHeader=true
How to split a git repository
Say your git repo consists of a subdirectory “app” and a subdirectory “database” (or “frontend” and “backend” or “team-a” and “team-b”) and you realize each directories content should be in its own repository. Here is how to split the repo.
To create a repo that consists of the content of the “database” dir and its history, execute
git filter-branch --prune-empty --tag-name-filter cat --subdirectory-filter database -- --all
and then push it to a new remote:
git remote rename origin old-origin git remote add origin <new-remote> git push -u origin --all git push -u origin --tags
This is what Gitlab states on a newly created project page. However in git push -u origin –all “-all” should be replaced with “–mirror” because “-all” pushes all local branches only so you would need to checkout all branches you want to keep. “–mirror” pushes remote branches, too.
In the same fashion apply “filter-branches” to the “app” directory.
Java method references recap
In the last post I reviewed Java lambda expressions. They represent a concise syntax to implement functional interfaces.
Enter Java method references. They represent a concise syntax to implement functional interface using existing methods. Like with lambda expressions, referenced methods are not allowed to throw checked exceptions.
Syntax
It’s simply “class-or-instance name” “::” “method name”, like
Function<String, Integer> string2Int = Integer::valueOf;
Types of method references
Reference to a static method
Static methods are referenced using the class name like in the example above.
Reference to an instance method of a particular object
Methods of a particular object are referenced using the variable name of that object:
Map<Integer, String> aMap = new HashMap<>(); Function<Integer, String> getRef = aMap::get; // call it String s = getRef.apply(42);
Reference to an instance method of an arbitary object of a particular type
Instead of using an already existing object you can just state the class and a non-static method. Then the instance is an additional parameter. In the following example toURI is a method with no arguments that returns a String. The function of this method reference takes a File (the object) and returns a String:
Function<File, URI> file2Uri = File::toURI;
Reference to a constructor
Constructors are references using its type and “new”:
Function<String, StringBuffer> bufferFromString = StringBuffer::new;
Here the constructor of StringBuffer with String parameter is referenced. Return type is the type of the constructor, parameters of the function are the parameters of the constructors.
Sprint Goal in Scrum
Sprint Goals are one of the more elusive parts of the Scrum Framework. Most teams know they are important, but few use them - for a variety of reasons. In this post, Barry Overeem and I bust the myth that Sprint Goals are optional in Scrum. And we make an effort to show what makes them so vital in the face of complex work. And more importantly, what you can do to start working with Sprint Goals from now on.
I really like this article because I am in a Scrum team where everyone works in his/her niche area. So far we haven’t used Sprint goals. I agree with the observation in the “What happens without Sprint Goals” – section, especially:
Without Sprint Goals, each Sprint implicitly has the same purpose: complete all the work. This makes all Sprints the same, and can make people (rightfully) complain that they are artificial;
So if you’re in a Scrum team that doesn’t use sprint goals, this is definitely a must read.
Agile back to basics with Robert C. Martin
In this episode we talk to Robert C. Martin, many of you might know him as Uncle Bob, and we're here to talk Agile and taking it back to basics.
A great podcast episode with Robert C. Martin on “Agile – back to the basics”. So what are, in the time of Scrum, LeSS, SaFE etc., the basics of Agile?
Apparently, “Uncle Bob” has a new book in the works, “Clean Agile” (duh!), covering this subject.
On point he makes is that often engineering practices like Pair Programming, Test Driven Design and Refactoring are overlooked. Especially Scrum does not comment on these. But without these practices, technical debt is likely to accumulate (see FlaccidScrum).
Java lambda expression recap
Lambda expressions in Java represent “functions”, something that take a number of parameters and produce at most one return value.
This could be expressed with anonymous classes but lambda expressions offer a more concise syntax.
Syntax
Lambda expression consist of a parameter list, an “arrow” and a body.
(String s1, String s2) -> s1 + "|" + s2
The parameter list is enclosed in round brackets. Types are optional. When the expression has exactly one parameter, the brackets can be omitted.
s -> s!=null && s.length>0
The body can either be an expression (that returns a value) or a block. A block is a sequence of statements, enclosed in curly braces.
n -> { if (n<10) System.out.println(n); }
Lambda expressions and types
In the Java type system, lambda expressions are instances of “functional interfaces”. A functional interface is an interface with exactly one abstract method.
Functional interfaces in java.util.function
The package java.util.function in the JDK contains a number of functional interfaces:
- Function<T,U> represents a function with one parameter of type T and return type U
- Consumer<T> represents a function with one parameter of type T and return type void
- Supplier<T> represents a function with no parameter and return type T
- Predicate<T> represents a function with one parameter of type T and return type boolean
Plus, variants with “Bi” prefix exists that have two parameters, like BiPredicate . More variants exists for using primitive types like DoubleToIntFunction .
User defined function interfaces
Any interface with exactly one abstract method can be used as type of a lambda expression. You mark this interface with @FunctionInterface .
@FunctionalInterface interface SomeInterface { int someBehaviour(String a, String b); } SomeInterface geo = (x,y) -> x.length + y.length;
Benefits
For me, the benefits of lambda expression are
- concise syntax for anonymous classes that represent functional code
- improved readability
- encouragement of a more functional programming style
How static is a static inner class in Java?
Answer: not static at all. A static inner class behaves like a normal class except that it is in the namespace of the outer class (“for packaging convenience”, as the official Java tutorial puts it).
So as an example:
public class Outer { private int x = 0; public int y = 1; static class Inner { //... } }
As opposed to a true inner (nested) class, you do not need an instance of Outer to create an instance of Inner:
Outer.Inner inner = new Outer.Inner();
and Inner instances have no special knowledge about Outer instances. Inner class behaves just like a top-level class, it just has to be qualified as “Outer.Inner”.
Why I am writing about this?
Because I was quite shocked that two of my colleagues (both seasoned Java developers) were not sure if a static inner class was about static members and therefore global state.
Maybe they do not use static inner classes.
When do I use static inner classes?
I use a static inner class
- when it only of use for the outer class and it’s independent of the (private) members of the outer class,
- when it’s conceptionally tied to the outer class (e.g. a Builder class)
- for packaging convenience.
Often, the visibility of the static inner class is not public. In this case there is no big difference whether I create a static inner class or a top-level class in the same source file. An alternative for the first code example therefore is:
public class Outer { // ... } // not really inner any more class Inner { // ... }
An example for (2) is a Builder class:
public class Thing { //... public static class Builder { // ... many withXXX methods public Thing make() // ... } }
If the Inner instance needs access to (private) members of the Outer instance then Inner needs to be non-static.
Checking your project dependencies for vulnerabilites
In the light of the recent case of introducing malicious code through a popular JavaScript module on npm, I like to mention snyk.io .
In a simple, free of charge scenario, snyk.io scans build or dependencies files on your github or gitlab projects and periodically reports vulnerabilities. Snyk supports Node, Ruby, Java, Scala and Python projects.
If you pay for snyk.io, you get a lot more integrations, CLI and API access etc.
In my own trial I found that even for fairly recent spring boot and apache camel dependency tree there a dozen of high-rated vulnerabilities! (Many of them by using “com.fasterxml.jackson.core:jackson-databind@2.9.1”). So the next question is if it’s advisable to upgrade to a secure patch of – say – jackson-databind although I use it only indirectly – in other words: will the depended framework still work with the secure patch version?
An open-source alternative is OWASP-Dependency-Check. It scans Java and .Net dependencies, has experimental support for Python, Ruby, PHP (composer), and Node.js applications. The tool seems to be JVM-based. There is a SonarQube-plugin. I have not tried it myself.
Things I learnt during my latest Javascript Code Kata
Sometimes I do a code kata at codewars.com. That is a fun way to solve computer science related problems, learn on the way to solve them and especially learn from the solutions of others.
Today I completed the kata “Make a spanning tree” using Javascript. I occasionally use Javascript to write an event handler or so but I don’t have much experience in “modern” Javascript. Here is what I learnt from looking at the solutions of others.
Destructuring
I know this from my Scala class and Clojure.
You can assign array elements to variables:
var a, b, rest; [a, b] = [10, 20]; console.log(a); // expected output: 10 console.log(b); // expected output: 20 [a, b, ...rest] = [10, 20, 30, 40, 50]; console.log(rest); // expected output: [30,40,50]
so “…rest” is assign the rest of the array.
This is nice syntactic sugar also when working with nested arrays. Eg when “edges” is an array of pairs:
// sort edges by weight edges.sort(([edge_a, a], [edge_b, b]) => a - b);
There is object destructuring:
var o = {p: 42, q: true}; var {p, q} = o; console.log(p); // 42 console.log(q); // true
and even assigning to new variable names
var o = {p: 42, q: true}; var {p: foo, q: bar} = o; console.log(foo); // 42 console.log(bar); // true
See MDN web docs for more.
Spread operator to create an array using an array literal
Using an array literal to create an array from two other arrays:
const sets = {}; //... // new array with sets[a] elements and sets[b] elements const set = [...sets[a], ...sets[b]];
Objects are associative arrays (aka Maps)
Although I already knew this, kind of, this refreshes my JS
knowledge.
First, you can add properties to Objects without declaring them in
the first place:
let obj = {}; // anonymous object obj.height=2; // create new property "heigth" and assign value console.log(obj.height); // 2
Second, instead of the dot-notation you can use array index
notation using the property name as the index:
let ojb = {}; obj['height'] = 2; console.log(obj['height']); // 2
One solution uses this in order to save the weighted edges in an
object just like i did in the proper Map object:
let set = {}; edges.filter(e => e[0][1] !== e[0][0]).forEach(e => { if (!set[e[0]] || minOrMaxFunc(set[e[0]], e[1])>00) { set[e[0]] = e[1]; } });
Third, methods are kind of properties, too. In the same solution,
“minOrMaxFunc” is cleverly choosen (“minOrMax” argument is either
“min” or “max”):
function makeSpanningTree(edges, minOrMax) { let minOrMaxFunc = { min: (a, b) => a - b, max: (a, b) => b - a }[minOrMax]; // ... }
it creates an objects with two methods: “min” and “max” and then
accesses the one that is given in the argument. If “minOrMax” is
“min”, a reference of the “min” method is returned.
Strings are arrays
Destructuring works with strings:
let [a,b] = 'ABC'; console.log(a); // "A" console.log(b); // "B"
and you can index strings:
const s = "ABC"; s[1]; // "B"
“var” vs. “let”
Of course, the solutions written in “modern” JS use “let” and
“const” all over the place. I just reassured myself about the
difference between let and var:
First, variables declared in a block using “var” are visible
outside that block and are “known” before being declared:
function f() { console.log(v); // undefined { var v = 3; } console.log(v); // 3 }
a block might be a for-loop.
Variables declared using let are not visible outside the block and
are not “known” before declared:
function f() { console.log(v); // Reference error { let v = 3; } console.log(v); // Reference error }
Third, you might not redeclare a variable using let:
var a = 0; var a = 1; // OK let b = 0; let b = 1; // not OK
So basically, “let” is a sane way to declare variables.
Refactor is about knowledge and meaning
Refactoring is more than a technical task. Refactor has business value because its main purpose is to ensure that business language and knowledge are reflected into the code.
Source: Refactor is about knowledge and meaning
Wonderful post by Fran Iglesias at dev.to.