POJO handling with mhu-lib

mhu-lib brings a full featured POJO handler. The framework is able to parse POJO objects uses the found attributes to get and set values. Also a toolset to transform JSON or XML structures directly into/from POJO objects. It’s al located in the package ‘de.mhus.lib.core.pojo’.

The base class is the POJO Parser. It’s not working a lot but it brings all together. First important choice is how to parse. Parse strategies are implemented by default looking for attributes (AttributesStrategy) or functions (FunctionsStrategy). The default strategy (DefaultStrategy) combines both but it’s possible to change the strategy object for the parser. Strategies are also looking for the @Embedded annotation and parse deeper inside this attributes. Important: The attribute based Strategy is also able to access ‘private’ declared values! Is no need to declare them all ‘public’.

The strategy creates a PojoModel which can be manipulated by filters. The default filter (DefaultFilter) remove @Hidden tagged attributes. The resulting model allows the coder to access the attributes.

POJO handling with mhu-lib

An example POJO:

public class MyPojo {
  private String name;
  private long id;
  private String displayName;

  public String getName() {
    return name;
  }
  public String getDisplayName() {
    return displayName;
  }
  public void getId() {
    return id;
  }
}

Now use the POJOParser to create the POJOModel:

model = new PojoParser().parse(Myojo.class).getModel();

More complex, use only @Public tagged attributes and concat @Embedded with “_”.:

model = new PojoParser().parse(MyPojo.class, "_", new Class[] {Public.class}).filter(true,false,true,false,true).getModel();

To set the values get the model parameters. Identifiers will be lower case. To access ‘displayName’ the identifier is ‘displayname’:

model.getAttribute("displayname").set(instance, "this is a sample");

The framework is a fast, stable and flexible way to use POJO objects in a common way.

Tipp: Create the model once and use it for all POJOs of the same type.

Parameter Related Classes Tree

Parameter Related Classes Tree

In mhu-lib there is a general attention to properties or attribute related objects. The implementation follows the philosophy that most thirst are attribute related and that is should be handled as the same. Properties and attributes are handled as the same not because they are the same but they have the same behavior.

First of all the IProperties class (since mhu-lib 3.3 a real interface) define a the basic behavior to set and get different value types. All java primitives are supported and the ‘Object’ type. The default implementation (e.g. MProperties) uses the getObject() variant and cast the object to the asked primitive by using the ‘MCast’ utilities. This simple structure is a flat properties store.

The ‘ResourceNode’ and ‘WritableResourceNode’ extends the structure to be a tree. With ‘getNodes()’ or ‘getNode(key)’ or ‘getParent()’ it is possible to traverse thru the tree structure.

An interesting extension of ‘WritableResourceNode’ is ‘IConfig’ with a lot of implementations to load configuration information from different types of sources like XML, JSON and properties files or a memory only variant.

The ‘CaoNode’ from the Content Access Object implementation is also attribute based. The framework enables common access to different tree based content structures like filesystem. (It’s currently recreated and not stable in later versions (mhu-lib 2.x) an EMC Documentum and Adobe AEM/CQ5 driver where available also.)

What is mhus-lib for?

This article is about the need of mhus-lib and a short history to explain. It also describes the different sections shortly (First of all it’s a library for the java VM).

The lib is called by my name because originally it was designed as a tool to solve problems I have in every project in the same way. The first version (in this time called mhu-lib) was a small set of static classes to convert data or to load simple information like the current host name.

In the next years the library was growing also solving more complex problems like how to handle different configurations in a common way. Handle POJOs and database access. It learned to serialize object into a RMDB and how to define forms in a generally way.  Also a common logging framework I really love was implemented. That was version 2.

In version 3 all the things are becoming a more common and integrated touch. Package declarations changed and the library is now OSGi usable.

But why not using one of the common frameworks like apache-commons or spring? The answer is simple – for me – this library implements exactly what I need to develop and after some tries I know it’s more robust, faster and handier than the alternatives. This is really subjective but – for me – true. I have some basic rules to implement functions:

  • Keep it simple: I don’t want to use complex stuff
  • Keep it generic
  • Robustness: It’s most important to think about when/where to handle exceptions
  • Don’t reinvent the wheel

Currently there are the following sections:

Continue reading “What is mhus-lib for?”

Fresh command shell:cut

Just added the command ’shell:cut’ to allow splitting of lines. The command is more flexible as the original shell command and allows spliting by regular expressions.

This is the option list:

  • -r Replace regex
  • -e Regular expression
  • -d Seperate parts by this regex
  • -t Trim every single part
  • -f Fields expression
  • -j Glue between fields
  • -p Positions

Field expressions are a comma separated list of fields or field ranges. The first field is zero ‘0’.

Examples:

bundle:list|cut -d \\\| -t -j ' ' -f 1,3,2
Active 80 0.0.0
...
bundle:list|cut -d \\\| -t -j ' ' -f 1-,0
Active 80 1.4 Commons DBCP 71
...
bundle:list|cut -d \\\| -t -j ' ' -f 1,abc,2
Active abc 80
...

The option -p followed by a list of ranges in the line.

Examples:

bundle:list|cut -j '&' -p 10-20,1-10,abc
ve |  80 |&69 | Acti&abc
...

Every positions definition can’t be out of boundls. In the worst case it will be ignored. An empty line – without written fields – will be ignored.

The options -e and -r must be defined together and change the line content before processing the split mechanism.

Remember: Usage of maven release plugin

Change the current used version numbers:

mvn release:update-versions

Deploy to Sonatype:

mvn clean install
PASS=
USER=
mvn release:clean
(Store the project into the repository ... svn ci ... git commit)
mvn release:prepare -Dusername=$USER -Dpassword=$PASS
mvn release:perform
  • Visit https://oss.sonatype.org/
  • Login
  • Open ‘Staging Repositories’
  • Search (in my case ‘mhus’)
  • Select and Close Repository, Message: ‘Ready to release’
  • Refresh and wait until closed
  • Select and Release Repository, Message: ‘Ready to release’
  • Refresh until it’s dropped

 

Scripting karaf gogo shell with mhu-osgi

Scripting karaf shell could be very constricted. Therefor an extension could help to work more with the possibilities of the karaf engine.

The main reason to enable more scripting are the possibility to find easy maintenance solutions without starting a software project for every small task. (The same reason bash become popular.)

The main extensions are ’shell:bash’ and ’shell:run’.

shell:bash

This extension only works for unix systems. It clones the ’shell:exec’ command and makes it easier to execute bash scripts and bring it together with gogo shell.

To use it you also need the commands ’shell:read’ and ’shell:write’ to work with files. The commands will be discussed later.

The following example writes the output of bundle:list to a file and executes bash script.

bundle:list|write test.txt
bash "cat test.txt|cut -d \\| -f 5"

shell:run

More helpful will be the ‘run’ command. You can run scripts that can be extended with block instructions.

Use the -c option to use the inline version. Use -d to debug the execution. Example:

bundle:list|run -c “for i in read *;echo Start \$i End;done”

An instruction must be in a separate line to be interpreted. The following instructions are possible:

  • if <condition> : start of a conditional block
  • elseif <condition> : another condition block if the condition before was not true
  • else : otherwise block
  • endif : end of the condition block
  • while <condition> : start of a while loop
  • for <var> in <list> : start of a for loop. The list could be a iterable object or a string. The string will be split by New Line.
  • done : end of a loop block
  • label <label> : label declaration
  • goto <label> : jump to a label
  • exit : exit execution

The following special instructions are:

# commend

#–
commend block
–#

bash:read

Reading from a file or stdin. Use ‘read <filename>’ to read the content of a file. The content will send to the pipe. Use the ‘-o’ option to set it as variable content.

If you want to read the hole content from the pipe and send it to the next pipe or set it as variable content use a star ‘*’ instead of the file name.

If you want to read only one line from the user input do not set a file name. The content of the line will be send to the pipe or set the variable with ‘-o’.

Examples:

karaf@root()> read -p prompt> 
prompt>Hello World!
Hello World!
karaf@root()> read -p secure> -s
secure>************
Hello World!
karaf@root()> read -p secure> -s -o secure
secure>***********
karaf@root()> echo ${secure}
Secure HeHe
karaf@root()>

bash:write

Write the pipe into a file. Use a star ‘*’ instead of the file name to send it to the next pipe consumer.

Example:

package:exports | write exports.txt

 

Installing mhu tools for karaf

The best choice for developers to install mhu tools is to use the current snapshot. With the snapshot the most of the discussed topics are working. I will make a note of the used SNAPSHOT you can use it or use the following release. If you use the relese skip the following description and start with the karaf installation.

Build the current Snapshot

Get the Sources

git clone https://github.com/mhus/mhus-lib.git
git clone https://github.com/mhus/mhus-osgi-tools.git

Compile it into the local repository

cd mhus-lib
mvn install
cd ..
cd mhus-osgi-tools
mvn install

Karaf installation

Setup Karaf

Download Karaf 3.0.5 from the Website https://karaf.apache.org/index/community/download.html#Karaf3.0.5 and unpack the zip bundle. Now change directory into the karaf directory ‘apache-karaf-3.0.5’ and start karaf for the first time ./bin/karaf and dtop it with ‘logout’.

If you need help with proxys or other problems then ask duckduckgo.

Basic Setup

Route the logging to the console for a better feedback. Open the file etc/org.ops4j.pax.logging.cfg and change the line

log4j.rootLogger=INFO, out, osgi:*

into

log4j.rootLogger=INFO, out, osgi:*, stdout

Start karaf again.

Install mhu-osgi-tools

Insert the following commands:

mhulib_version=3.2.9-SNAPSHOT
mhuosgi_version=1.0.5-SNAPSHOT
feature:repo-add activemq 5.12.1
feature:repo-add cxf 2.7.11
feature:install jdbc
feature:install openjpa
feature:install scr
feature:install activemq-broker

install -s mvn:mysql/mysql-connector-java/5.1.18
install -s mvn:org.codehaus.jackson/jackson-core-asl/1.9.5
install -s mvn:org.codehaus.jackson/jackson-mapper-asl/1.9.5
install -s mvn:org.apache.httpcomponents/httpcore-osgi/4.2.1
install -s mvn:org.apache.httpcomponents/httpclient-osgi/4.2.1
install -s mvn:de.mhus.lib/mhu-lib-annotations/${mhulib_version}
install -s mvn:de.mhus.lib/mhu-lib-core/${mhulib_version}
install -s mvn:de.mhus.lib/mhu-lib-jms/${mhulib_version}
install -s mvn:de.mhus.lib/mhu-lib-logging/${mhulib_version}
install -s mvn:de.mhus.lib/mhu-lib-persistence/${mhulib_version}
install -s mvn:de.mhus.lib/mhu-lib-karaf/${mhulib_version}
install -s mvn:de.mhus.osgi/jms-commands/${mhuosgi_version}
install -s mvn:de.mhus.osgi/mhus-osgi-commands/${mhuosgi_version}
install -s mvn:de.mhus.osgi/mailosgi/${mhuosgi_version}
install -s mvn:de.mhus.osgi/mailkaraf/${mhuosgi_version}

Maybe you need to change the versions.

If you type ‘list’ you should see all installed bundles.

Now you are ready to use mhu osgi tools.