• Post Reply Bookmark Topic Watch Topic
  • New Topic

Open Source Java library with stacktrace filtering, Silent String parsing and Version comparison  RSS feed

 
Michael Gantman
Greenhorn
Posts: 6
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Over some time in different jobs I came across a need for several utilities that I couldn't find available at the time. And I saw that I needed them several times over and over again. So I wrote my own small library that I found very useful. So I just published it as an open source java library. Here is the link

https://github.com/michaelgantman/Mgnt/releases

Also this library is available on Maven Central. Here are the Maven artifacts (the version 1.03 is the latest at the time of writing of this article but might change to check for the latest version search for artifact "MgntUtils" at http://search.maven.org/):

<dependency>
<groupId>com.github.michaelgantman</groupId>
<artifactId>MgntUtils</artifactId>
<version>1.03</version>
</dependency>

<dependency>
<groupId>com.github.michaelgantman</groupId>
<artifactId>MgntUtils</artifactId>
<version>1.03</version>
<classifier>javadoc</classifier>
</dependency>

<dependency>
<groupId>com.github.michaelgantman</groupId>
<artifactId>MgntUtils</artifactId>
<version>1.03</version>
<classifier>sources</classifier>
</dependency>

Below is just a short explanation what is there. The library comes with a nicely written (I hope) javadoc with detailed description. So here are the list of features:

Stacktrace noise filter
In my experience this feature was the most generic and useful for me. Stacktrace is a life saver when debugging or trying to figure out what went wrong in your application. However, When working with logs on the server side you can come across huge stacktrace that contains the longest useless tail of various frameworks and Application Server related packages. And somewhere in this pile there are several lines of a relevant trace and they may be in different segments separated by useless information. It becomes a nightmare to search for a relevant stuff. Here is a link that describes the same problem with real life examples (not for the fainthearted ) https://dzone.com/articles/filtering-stack-trace-hell. So In my library There is class is called TextUtils and it has method getStacktrace() with several overridden signatures. It takes a Throwable instance and allows to set a package prefix of the packages that are relevant. Let's say your company's code always resides in packages that start with "com.plain.*" So you set such a prefix and do this



this will filter out very smartly all the useless parts of the trace leaving you with very concise stacktrace. Also I found it very convinient to pre-set the prefix and then just use the convinience method


It will do the same. To preset the prefix just use method



Also if you use Spring environment you can add the following segment to your Spring configuration and then you all set:

<beanclass=
"org.springframework.beans.factory.config.MethodInvokingFactoryBean">
<property name="targetClass" value="com.mgnt.utils.TextUtils"/>
<property name="targetMethod" value="setRelevantPackage"/>
<property name="arguments" value="com.plain."/>
</bean>


Javadoc explains everything in detail. But here is a little teaser: you will get a following stacktrace:

at com.plain.BookService.listBooks()
at com.plain.BookService$$FastClassByCGLIB$$e7645040.invoke()
at net.sf.cglib.proxy.MethodProxy.invoke()
...
at com.plain.LoggingAspect.logging()
at sun.reflect.NativeMethodAccessorImpl.invoke0()
...
at com.plain.BookService$$EnhancerByCGLIB$$7cb147e4.listBooks()
at com.plain.web.BookController.listBooks()


instead of

at com.plain.BookService.listBooks()
at com.plain.BookService$$FastClassByCGLIB$$e7645040.invoke()
at net.sf.cglib.proxy.MethodProxy.invoke()
at org.springframework.aop.framework.Cglib2AopProxy$CglibMethodInvocation.invokeJoinpoint()
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed()
at org.springframework.aop.aspectj.MethodInvocationProceedingJoinPoint.proceed()
at com.plain.LoggingAspect.logging()
at sun.reflect.NativeMethodAccessorImpl.invoke0()
at sun.reflect.NativeMethodAccessorImpl.invoke()
at sun.reflect.DelegatingMethodAccessorImpl.invoke()
at java.lang.reflect.Method.invoke()
at org.springframework.aop.aspectj.AbstractAspectJAdvice.invokeAdviceMethodWithGivenArgs()
at org.springframework.aop.aspectj.AbstractAspectJAdvice.invokeAdviceMethod()
at org.springframework.aop.aspectj.AspectJAroundAdvice.invoke()
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed()
at org.springframework.aop.interceptor.AbstractTraceInterceptor.invoke()
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed()
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke()
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed()
at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke()
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed()
at org.springframework.aop.framework.Cglib2AopProxy$DynamicAdvisedInterceptor.intercept()
at com.plain.BookService$$EnhancerByCGLIB$$7cb147e4.listBooks()
at com.plain.web.BookController.listBooks()


Silent String parsing into Integer, Long etc
The same class TextUtils provides methods to parse String silently (without exception ever being thrown) into types of Double, Long, Integer, Short and Byte. The methods are called parseStringToLong() parseStringToInteger() etc. They all take 4 arguments:

  • A String to be parsed
    An implementation of Number (Double, Long, Integer, Short or Byte) to serve as default value if any parsing occurred
    A String to be printed as an error message into log if the first argument is null (may be null, and then no error is printed)
    A String to be printed as an error message if NumberFormatException occurred (may be null, and then no error is printed)


  • Compare Versions
    This utility allows to convert String to version and vise-versa and to compare versions and version ranges properly. Often if you need to compare versions you just compare Strings. So lets say version "1.5.3" will be greater then version "1.3.1". However if you will compare Strings "1.4.2" and "1.12.3" the String "1.4.2" will erroneously be greater. So this Utility takes care of such problem and in addition introduces VersionRange class and allows operations on version ranges. (See methods compareVersions in class TextUtils and class VersionRange)

    String Unicode converter
    Class StringUnicodeEncoderDecoder has methods that can convert a String (in any language) into a sequence of Unicode characters and vise-versa. For example a String "Hello World" will be converted into

    "\u0048\u0065\u006c\u006c\u006f\u0020
    \u0057\u006f\u0072\u006c\u0064"

    and may be restored back.

    Web Utils
    Class WebUtils provides a method to read a binary information from http request. This method may be useful in particular if a client is uploading a large size of binary information and the server side reading is faster then client writing. This Utility allows server to throttle down and adjust server to client speed. Also may be convenient to read a client input as byte array in one go.

    File Utils
    This utility allows to read file as a String with or without specifying character set or just read it as byte array. This utility uses nio package and Buffers for faster performance. Max file size for this Utility is 2G.

    Time Util
    This is just a single convenience method that provides the same functionality as Thread.sleep() but it is silient. I.e. it "swallows" IterruptedException. Also it takes 2 arguments: time to sleep and measurement unit. So instead of



    You can just write:





    I thought it was cute

    So this is it. Feel free to download this library. It comes with MIT licence - one of the most permissive licences I know of. Source code, Javadoc and binary version is available at the link above. The code was compiled by jdk 8 but has no features of version 8 in it. It should work with java 7 most definitely, I suspect it will work with java 6 and even 5, but haven't tested it with those versions. Feel free to use it as you see fit, tell others about it if you like it and feel free to drop me a note at michael_gantman@yahoo.com if you have any feedback.
     
    Campbell Ritchie
    Marshal
    Posts: 56518
    172
    • Likes 1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    That looks useful stuff Shall drive some beef your way as thanks, only it will be very fresh.

    And welcome to the Ranch
     
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!