Sunday, April 21, 2019

SImple Spring Boot and React working example

https://developer.okta.com/blog/2018/07/19/simple-crud-react-and-spring-boot

Matt Raible is a boss and he is so straight to the point and precise.

Remember that if you have in your pom.xml the dependency spring-boot-starter-security, then Spring Boot will enable security by default with username "user" and a dynamically generated password:

https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-security.html

so for the time being I have excluded this dependency.

This https://www.baeldung.com/spring-security-login should explain more how to handle web security with the great fluent api provided by Spring.

Friday, April 19, 2019

Intellij and WebLogic

https://www.jetbrains.com/help/idea/configuring-and-managing-application-server-integration.html



Wednesday, April 17, 2019

never copy/paste from outlook or Word

Microsoft sucks




The command on the left was copied from an email (Outlook). And it was failing, returning EVERYTHING, not only config.xml.

Then I have typed all over again (right pane) and it was working

Visually they look absolutely the same, even in Notepad++. But a closer inspection reveals hidden chars.

Copying and pasting from Outlook or Word has caused more victims than the 1919 Spanish Flu.

I remember a production delivery failed because someone copied a command from a Word document where Word had capitalized a property name.



CompletableFuture

https://www.callicoder.com/java-8-completablefuture-tutorial/

package com.example.demo;

import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.TimeUnit;

@SpringBootApplication
public class DemoApplication implements CommandLineRunner {

    public static void main(String[] args) {
        SpringApplication.run(com.example.demo.DemoApplication.class, args);
    }

    @Override
    public void run(String... args) throws Exception {
        CompletableFuture<String> completableFuture = new CompletableFuture<String>();
        completableFuture.complete("Future's Result");

        String result = completableFuture.get();
        System.out.println(result);


        CompletableFuture<Void> future = CompletableFuture.runAsync(new Runnable() {
            @Override
            public void run() {
                // Simulate a long-running Job
                try {
                    TimeUnit.SECONDS.sleep(1);
                } catch (InterruptedException e) {
                    throw new IllegalStateException(e);
                }
                System.out.println("I'll run in a separate thread than the main thread.");
            }
        });
        // Block and wait for the future to complete
        future.get();
    }

}







Ruminations about Jenkins

The jobs are defined in here:

$JENKINS_HOME/jobs

For every project (item), there is a folder.

In every folder there is a config.xml file which hopefully should contain the entire project definition.

My assumption is that one should simply save the config.xml, and this is enough to recreate the project elsewhere.

I am worried because the Project definitions in Jenkins are not saved in bitbucket,
and we don’t have an automated way to export them from PROD and import them to a UAT instance.

One COULD tar the $JENKINS_HOME/jobs folder, but it’s very bulky.


[jobs]$ cd myproject/

[myproject]$ ls -ltra

drwxr-xr-x. 3 pippo pippogroup 4096 Jul 18 2016 modules
-rw-r--r--. 1 pippo pippogroup 6 Apr 25 2018 nextBuildNumber
lrwxrwxrwx. 1 pippo pippogroup 26 Apr 25 2018 lastSuccessful -> builds/lastSuccessfulBuild
lrwxrwxrwx. 1 pippo pippogroup 22 Apr 25 2018 lastStable -> builds/lastStableBuild
drwxr-xr-x. 5 pippo pippogroup 4096 Apr 25 2018 workspace
drwxr-xr-x. 24 pippo pippogroup 4096 Apr 25 2018 builds
-rw-r--r--. 1 pippo pippogroup 7257 Apr 25 2018 config.xml
drwxr-xr-x. 5 pippo pippogroup 4096 Apr 25 2018 .
drwxr-xr-x. 360 pippo pippogroup 20480 Mar 18 15:06 ..


especially the workspace is a PIG

So for the time being I will simply tar up all the config.xml and untar them in UAT:


cd $JENKINS_HOME/jobs

find . –maxdepth 2 –name config.xml | tar cvf /var/tmp/alljenkinsconfig.tar –T –

(maxdepth is important to avoid picking up files coming from the workspaces)

However we should really really IMHO push all those config.xml to bitbucket, regularly – ideally automatically whenever someone changes a config.xml:

git init
#set origin and remote
find . –maxdepth 2 –name config.xml –exec git add {} \;
git commit -m "blablabla"
git push


Incidentally, many project folders contain spaces, which makes it much trickier to write scripts to manipulate them.

I am not in favor of Capital Punishment, apart from cases when people create folders containing spaces.


Jenkins sucks anyway. Design is from Napoleonian Era, UI made by a freak, configuration freakishly XML based without fluent Administration Groovy API,
actually in the old times they were much better at designing stuff, think of the Tour Eiffel or the Pyramids and the Coliseum.
Nowadays any idiotic monkey can code freakish products like Maven or Jenkins and become a celebrity.



Thread dumps analysis

this post provides useful insights on how to detect issues from a Thread Dump:

http://allthingsmdw.blogspot.com/2012/02/analyzing-thread-dumps-in-middleware_08.html

For a quick thread dump consolidation one can use https://spotify.github.io/threaddump-analyzer/ or https://www.jetbrains.com/help/idea/analyzing-external-stacktraces.html (both are quite equivalent)....

Problem is that these tools are Application Server agnostic and don't tell you what is normal and what is not. I think some AI should be added to the tool, plus some graphical rendering for instance of the lock analysis.

When I have time I want to look into https://fastthread.io/

Anyway advanced thread analysis is a skill in itself and should be done with proper tooling... manually sorting stuff can be overwhelming for the regular human being.

Saturday, April 13, 2019

Mockito revisited

https://github.com/mockito/mockito


I have used Mockito in 2010. Then I never did SERIOUS Java development any more (shame on me...)

Yet I strongly believe that a solid implementation of Mocks is the foundation of healthy, fully testable software.
If you can't entirely automate your application tests, you are playing with your life.


https://search.maven.org/artifact/org.mockito/mockito-core/2.26.0/jar

<dependency>
    <groupId>org.mockito</groupId>
    <artifactId>mockito-core</artifactId>
    <version>2.26.0</version>
</dependency>









https://github.com/in28minutes/MockitoTutorialForBeginners



https://www.baeldung.com/mockito-annotations

source code is here https://github.com/eugenp/tutorials/tree/master/testing-modules/mockito


https://static.javadoc.io/org.mockito/mockito-core/2.27.0/org/mockito/Mock.html

@Mock
List mockedList;


https://static.javadoc.io/org.mockito/mockito-core/2.27.0/org/mockito/Spy.html

@Spy
List spiedList = new ArrayList();


https://static.javadoc.io/org.mockito/mockito-core/2.27.0/org/mockito/ArgumentCaptor.html

@Captor
ArgumentCaptor argCaptor;


I am getting : Illegal reflective access by org.mockito.internal.util.reflection.AccessibilityChanger
Hopefully Mockito folks will fix this one da.







Wednesday, April 10, 2019

Gradle plugin for Jenkins

Maven Sucks, so we welcome Gradle - it can't possibly be worse than Maven!

https://wiki.jenkins.io/display/JENKINS/Gradle+Plugin

it seems that Gradle Plugin is installed as part of the "common plugins". I use an old Jenkins 2.138.


export GRADLE_HOME=/home/centos/gradle/gradle-4.8
#make sure $GRADLE_HOME/bin is in $PATH
cd /home/centos/gitclones
git clone https://github.com/jitpack/gradle-simple
cd gradle-simple/
gradle clean
gradle build
#check if jar was built
ls -ltra build/libs


I configure a freestyle Jenkins item to checkout from Github and "clean build" as gradle tasks...
Then I inspect /home/centos/.jenkins/workspace/gradletest/build/libs/ and the jar has been built there... great!


If I want to copy the plugin to another machine, it's in /home/centos/.jenkins/plugins/gradle.jpi

The MANIFEST.MF says:

Manifest-Version: 1.0
Plugin-Dependencies: structs:1.3
Long-Name: Gradle Plugin
Compatible-Since-Version: 1.0
Plugin-Developers: Stefan Wolf:wolfs:
Group-Id: org.jenkins-ci.plugins
Extension-Name: gradle
Plugin-Version: 1.31
Jenkins-Version: 1.651.3
Url: https://wiki.jenkins.io/display/JENKINS/Gradle+Plugin
Short-Name: gradle







Tuesday, April 9, 2019

Effective Java third edition


the source code:

https://github.com/jbloch/effective-java-3e-source-code






*clone (p. 86)
copy constructor/ copy factory
compareTo()
java.util.Comparator.comparingInt + thenComparingInt


immutable objects

composition over inheritance

default methods

generics and collections

unbounded wildcard types (<?>)

PECS producer extends consumer super

isAnnotationPresent
@ExceptionTest
@Repeatable


java.util.function
Operator
Predicate
Function
Supplier
Consumer



Monday, April 8, 2019

Generics and collections

this one has no compilation errors:

Set a;
Set<Object> b;
Set<? super Object> c;

a = new HashSet();
b = new HashSet<>();
c = new HashSet<>();

c = a;
c = b;
b = a;
b = c;
a = c;
a = b;



but if I use

Set<Object> b;
Set<?> c;

then "c = b" gives compilation error

Error:(26, 13) java: incompatible types: java.util.Set cannot be converted to java.util.Set


This is interesting, since ? means "any Object and subclasses"... while "? super Object" means "Object and its superclasses"
but Object has no superclasses, so effectively it's only Object.


If I use:
b = new HashSet<>();
Set<?? extends Object> c;

then
b = c;
fails with the same error:

Error:(26, 13) java: incompatible types: java.util.Set cannot be converted to java.util.Set


The raw type a can be assigned all the time to all the others.


similarly, collections are INVARIANT:
Set<Number> numset = new HashSet<>();
Set<Integer> intset = new HashSet<>();
Set<?> allset = new HashSet<>();

numset = intset; // INVALID
intset = numset; // INVALID
allset = numset; // VALID
numset = allset; // INVALID



but arrays are COVARIANT:

Number[] numarr = new Number[1];
Integer[] intarr = new Integer[1];
numarr = intarr; // VALID
intarr = numarr; // INVALID










Sunday, April 7, 2019

Amazing brushes by Reto




a colleague of mine, Reto, is a passionate photographer and Photoshop artist , here some links to his creations:

https://creativemarket.com/xresch/2642460-1500-Brushes-Megapack

https://creativemarket.com/xresch

https://affinity.serif.com/en-gb/store/product/180-smoke-and-cloud-brushes/




Intellij , maven and the source or target java language level

I create a new Java "maven" project, set it to Java 12 (ctrl-alt-shift-S, MOdules, Language level), but when I build I get:

"Warning:java: source value 1.5 is obsolete and will be removed in a future release"


Apparently this is due to the prehistoric, nonsensical Maven Compiler Plugin using Java 1.5 by default

https://stackoverflow.com/questions/27037657/stop-intellij-idea-to-switch-java-language-level-every-time-the-pom-is-reloaded


you can either insert in the pom.xml :
<properties>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
    </properties>



or
<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.8.0</version>
            <configuration>
                <source>1.11</source>
                <target>1.11</target>
            </configuration>
        </plugin>

    </plugins>

</build>



Remember also to make sure that ctrl-alt-S (Settings) Settings -> Build, Execution, Deployment -> Build Tools -> Maven -> Importing is set to use JDK 12. So complicated. So brittle. So Maven.

Working with Maven is like traveling back to prehistoric times, like the Mural de la Prehistoria in Cuba, https://en.wikipedia.org/wiki/Vi%C3%B1ales_Valley



developers on the right about to be eaten by Maven on the left


Priceless String manipulation Intellij plugin

ctrl-alt-S, then "Plugins" search for "String Manipulations"; install and reboot.

for instance, if you need to "escape XML" (that is transform reserve chars in "% bla ;" sequences :

File/new scratch file (or ctrl-alt-shift-ins), paste your XML, select all, right click "String manipulation", "escape xml"


Normally I used Notepad++ and the "XML Tools Plugin", but this seems more useful.





Wednesday, April 3, 2019

git synchronize repositories

It happens that an external Git repository has to be mirrored internally in a corporate git repository. This cloning and mirroring should include branches, tags, commit history etc. and be one-way (internal changes are lot upon synchronization)

Luckily you don't have to write special scripts, all is provided by git:


https://help.github.com/en/articles/duplicating-a-repository



Let's make an experiment

in github I create a "gitclonesource" and a "gitclonedestination" empty repositories

mkdir gitclonetests
cd gitclonetests


#let's prepare the source
mkdir gitclonesource
cd gitclonesource
git init
echo "ciao" > README.md
git add README.md
git commit -m "first commit"
git remote add origin https://github.com/vernetto/gitclonesource.git
git push -u origin master

#create branch
git checkout -b mybranch
echo "hello" >> mybranch.txt
git add mybranch.txt
git commit -am "added mybranch.txt"
git push --set-upstream origin mybranch

#create tag
git checkout master
git tag -a v1.4 -m "my version 1.4"
git push --tags



#now clone source and push to destination with all branches and tags
cd ..
#this will create a folder gitclonesource.git
git clone --bare https://github.com/vernetto/gitclonesource.git
#when you cd, you will see a message BARE:master if using Git bash
cd gitclonesource.git/
git push --mirror https://github.com/vernetto/gitclonedestination

Counting objects: 7, done.
Delta compression using up to 8 threads.
Compressing objects: 100% (4/4), done.
Writing objects: 100% (7/7), 600 bytes | 200.00 KiB/s, done.
Total 7 (delta 0), reused 0 (delta 0)
To https://github.com/vernetto/gitclonedestination
 * [new branch]      master -> master
 * [new branch]      mybranch -> mybranch
 * [new tag]         v1.4 -> v1.4




and, lo and behold, in the gitclonedestination I can find my branch and the tag! All cloned in one go!

at this point you can cleanup the gitclonedestination.git:

cd ..
rm -rf gitclonedestination.git








Monday, April 1, 2019

WebLogic Security documentation

https://docs.oracle.com/middleware/12213/wls/INTRO/security.htm#INTRO232



https://docs.oracle.com/cd/E19798-01/821-1841/girbe/index.html JACC "Java Authorization Contract for Containers"

https://docs.oracle.com/javase/8/docs/api/java/security/Permission.html java.security.Permission

https://docs.oracle.com/javase/8/docs/api/java/lang/SecurityManager.html java.lang.SecurityManager





Java Authentication Service Provider Interface for Containers (JASPIC)




Training Material:

https://www.udemy.com/how-to-secure-oracle-weblogic-12c/


https://www.udemy.com/oracle-weblogic-12c-for-administrators/


Old book https://www.amazon.com/Securing-Weblogic-Server-Luca-Masini/dp/1849687781





Best Spring books

reblogging for my own reference, I am planning to read all those 5 books...

https://javarevisited.blogspot.com/2018/04/5-spring-framework-books-experienced-Java-developers-2018.html

- Spring in Action 5
- Cloud Native Java
- Learning Spring Boot 2.0
- Spring 5 Recipes
- Spring Microservices in Action



I am already going through https://www.udemy.com/spring-framework-5-beginner-to-guru/ but I find it incredibly non-concise - lot of repeated stuff, very lengthy and boring coding sessions where you don't really see much new, lot of unnecessary verbosity. Not my style, I like crisp, focused, simple examples and just fundamental facts and concepts.