Monday, July 23, 2018

SpringBoot2, Update Log Levels At Runtime

I had a requirement where I wanted to create a user interface to dynamically update the spring boot 2 log levels, without having to restart the spring boot service (service)
This can be done by adding the spring boot actuator plugin.
Actuator brings production-ready features to our application.

Heres a good article that explains about the actuator:
http://www.baeldung.com/spring-boot-actuators

More features on how to use the actuator can be found here:

https://docs.spring.io/spring-boot/docs/2.0.x/actuator-api/html/

Note: The actuator service eventually should sit behind a security domain and context so that these services wont be exposed to the public. This article will not cover that right now.


Here's a step by step approach to dynamically set the log levels



1.  Add the actuator library:


     compile("org.springframework.boot:spring-boot-starter-actuator")



2.  In the application properties, add you logging configuration and make sure to enable all the actuator end points.

logging.level.org.springframework.web=ERROR
logging.level.org.hibernate=ERROR
logging.file=testing-features.log
management.endpoints.web.exposure.include=*


3.  Implement a simple controller that will print a log to your log file
@RestController
public class TestController {

    private Logger log = LogManager.getLogger(TestController.class.toString());
    @RequestMapping(value = "/service", method = RequestMethod.GET)
    public ResponseEntity<String> callService(){


        log.info("Service Called");
        return ResponseEntity.ok("Hello World");    }
}

4. View the root loggers log level:
http://localhost:8080/actuator/loggers/root

5. Update the root loggers log level by sending a post request.
Use postman to send a POST request.



Sunday, May 27, 2018

Git Merge Vs Rebase

I have used the below steps to test Git Merge and Git Rebase.

1) Init Master,
2) Init Branch.
3) Commit "1", in Branch. . 
4) Commit "2"  in Master. 
5) Commit "3" in Branch. 

Git Merge:  

"Your repository’s commit history is a record of what actually happened."

git checkout mybranch
git merge master 


Note in the above diagram, I have merge the master into branch.


Note that, the second commit is applied after the third commit in my branch, and a new merge commit is created.


Git Rebase:  

"The repository's commit history is the story of how your project was made."

git checkout mybranch
git rebase master. 

Here I have rebased mybranch onto master

Now here instead of using a merge commit, rebasing re-writes the project history by creating brand new commits for each commit in the original branch.




See that all the branch history is applied on top of master log history.


Way to use

In general the way to get the best of both worlds is to rebase local changes you’ve made but haven’t shared yet before you push them in order to clean up your story, but never rebase anything you’ve pushed somewhere.

Wednesday, May 9, 2018

Dependency Injection, Field, Setter, or Constructor Injection

I was doing some investigation on the different approaches of Dependency Injection in the Spring Framework. Following is a summary of content that I gathered from various websites.


Inversion of Control (IOC)

IoC enables a framework code,  to take control of the flow of a program and make calls to our custom code.


Dependency Injection

Dependency injection is a pattern through which to implement IoC.

The act of connecting objects with other objects, or rather “injecting” objects into other objects, is done by a the framework code  rather than by the objects themselves


Field Injection


Using field injection makes it hard to test , because we do not have a way to inject our dependency into the class we are testing, and we would have to rely on the IOC container to inject this dependency for us.


One of the purposes of Dependency Injection is to decouple code, which in turn should make the code easier to test.

However, it is worthwhile noting that field injection makes the code more elegant than setter or constructor injection.


Setter Injection


In this approach, we can not guarantee that our class would have injected the dependency. However note that, It is good to use setter injection when the dependencies are optional.




This blog Repeat After Me: Setter Injection is a Symptom of Design Problems explains further the disadvantages of using setter injection. 

Another blog explaining why setter injection is not a good idea is Setter injection sucks.

I came across this blog: Constructor vs Setter Injection, happen to explain why setter injection is preferred over constructor injection.


Constructor Injection

Constructor Injection guarantees that the dependency is injected during class creation.





This blog: One correct way to do dependency injection explains why constructor injection is a better option over setter injection.

Tuesday, December 5, 2017

GIT Branching Model

Idea Extracted from this website as I had the liberty to learn from it and apply it to my personal projects as well as in some of the organizations I work for.

The details are summarized as follows:


Overall picture:




Main branches:

  • MASTER 
  • DEVELOP


We consider origin/master to be the main branch where the source code of HEAD always reflects a production-ready state

We consider origin/develop to be the main branch where the source code of HEAD always reflects a state with the latest delivered development changes for the next release. Some would call this the “integration branch”. This is where any automatic nightly builds are built from

When the source code in the develop branch reaches a stable point and is ready to be released, all of the changes should be merged back into master somehow and then tagged with a release number.

Other Branches

  • Feature branches
  • Release branches
  • Hotfix branches


Unlike the main branches, these branches always have a limited life time, since they will be removed eventually.

Feature:
May branch off from: develop Must merge back into: develop

Finished features may be merged into the develop branch to definitely add them to the upcoming release.

Release:
May branch off from: develop Must merge back into: develop and master
It is exactly at the start of a release branch that the upcoming release gets assigned a version number—not any earlier.
This new branch may exist there for a while, until the release may be rolled out definitely. During that time, bug fixes may be applied in this branch (rather than on the develop branch).

When the state of the release branch is ready to become a real release, some actions need to be carried out. First, the release branch is merged into master (since every commit on master is a new release by definition, remember). Next, that commit on master must be tagged for easy future reference to this historical version

Finally, the changes made on the release branch need to be merged back into develop, so that future releases also contain these bug fixes.

Hotfix:
May branch off from: master Must merge back into: develop and master

When finished, the bugfix needs to be merged back into master, but also needs to be merged back into develop, in order to safeguard that the bugfix is included in the next release as well. 

Saturday, December 2, 2017

React Redux Concepts

Redux is a tool for managing both data-state and UI-state in JavaScript applications.

Redux attempts to make state mutations predictable by imposing certain restrictions on how and when updates can happen.

Core principals:



  1. The state of your whole application is stored in an object tree within a single store.
  2. The only way to change the state is to emit an action, an object describing what happened.
  3. To specify how the state tree is transformed by actions, you write pure reducers. Reducers are just pure functions that take the previous state and an action, and return the next state. 


What React Solves:



Component to Component communication is hard in react, as data flows from top to bottom.




For communication between two components that don't have a parent-child relationship, you can set up your own global event system. ... Flux pattern is one of the possible ways to arrange this.


React solves the above problem by introducing a global store. Components then "dispatch" state changes to the store, not directly to other components. The components that need to be aware of state changes can "subscribe" to the store:


npm install --save redux react-redux  

Monday, July 3, 2017

Wildfly 10.1 Webservice Security with Custom Login Module

source code:   https://github.com/byorn/wildfly-webservice-security


A Sample EAR extracted from the Wildfly Quickstarts can be found from the above source.


It contains a @Webservice and @SecurityDomain, protecting the @Webmethod with defined roles.

In the project's EJB Section, is the Module DelegationLoginModule.

This is a custom Login Module. All the information on how to authentication the user name and password is described in the source comments.


The Standalone.xml in Wildfly10 should have this configuration:


<security-domain name="other" cache-type="default">
                    <authentication>
                        <login-module code="byorns.com.login.module.DelegationLoginModule" flag="required">
                            <module-option name="password-stacking" value="useFirstPass"/>
                        </login-module>
                    
                    </authentication>
                </security-domain>

web.xml should have
<login-config>
   <auth-method>BASIC</auth-method>   <realm-name>RealmUsersRoles</realm-name></login-config>


Test with SOAP UI, Basic Authentication:





For more security related debugs:

Add the logger in Standalone.xml

 <logger category="org.jboss.security">
                <level name="TRACE"/>
</logger>

Sunday, May 14, 2017

Starting React Development

The quickest way to get started with react is to follow the official react tutorial.

Firstly you would want to make sure you have Node installed in your machine.
To download Node, go to: https://nodejs.org/en/download/

If you already have node installed, make sure it is updated to its latest version:

> npm install npm@latest -g

Next create the skeleton react app using create-react-app program

> npm install -g create-react-app
> create-react-app my-app

> cd my-app
> npm start  


To work with React Projects, I use ATOM with added plugin Language-Babel package.

To Build React for Production use:
> npm run build

This will create a build folder and the index.html and its contents can be hosted in production.

To run it, you can install the serve program

> npm install -g serve
> serve -s build

Note: 
create-react-app uses Webpack underneath it to  bundle your code and all its dependencies, It also uses also uses Babel to Tranform next generation java script (llike es6) to browser compatible java scripts