Git hooks for automation - java

I have below requirements,
In commit message we have to capture some of the mandatory data like FileName, user story, description etc and later these will be stored in db. Can we create conditional tags so that developer can give either userstory, CR# or defect# based reason for which fix is required?
We have spring boot gradle project. Here we have to automate the static code analysis and Junit Analysis through Pre Commit Hook. Can we write these hooks in java?
Please share some examples.

For your commit messages, there is a customary way to include various pieces of data, which is the trailer system. These are lines of the following format:
Signed-off-by: A U Thor <author#example.com>
Fixes: 1234
It is possible for you to extract data from this either by parsing the commit message yourself or by using git interpret-trailers. This may be able to be done in JGit if you want to do it in Java, but it may or may not have built-in support for parsing trailers. It is strongly recommended that you use the exact same parsing technique that Git does if you need to implement it yourself to avoid creating compatibility problems.
The general rule with hooks on Unix is that they must be either a binary or a script executable by the operating system. Usually Java JARs don't meet that requirement, so you'll probably want to write a shell script wrapper that invokes your Java code. However, this will be very slow, since you'll need to start a full JVM every time.
In addition, you should be aware that, as the Git FAQ outlines, hooks are not an effective tool for controlling policy:
It’s common to try to use pre-commit hooks (or, for commit messages, commit-msg hooks) to check these things, which is great if you’re working as a solo developer and want the tooling to help you. However, using hooks on a developer machine is not effective as a policy control because a user can bypass these hooks with --no-verify without being noticed (among various other ways). Git assumes that the user is in control of their local repositories and doesn’t try to prevent this or tattle on the user.
Therefore, if you want to have effective controls, you need to perform these actions on your CI server. You can still provide hooks for developers who wish to use them, but you cannot rely on them being run. The FAQ also mentions this as another reason why you don't want to mandate hooks:
In addition, some advanced users find pre-commit hooks to be an impediment to workflows that use temporary commits to stage work in progress or that create fixup commits, so it’s better to push these kinds of checks to the server anyway.

Related

Is there an easy way to find all JVMs across all hosts in dynatrace?

Is there an easy way to search dynatrace for all hosts with any java process. Basically trying to find any jvm running on any of our servers. Need to know what server it is, what java vendor, what java version and if possible what technology is using the jvm. I basically need to come up with an inventory of java versions that are out of data and not supported and seems like I should be able to find it easily in dynatrace rather than manually poking around on a hundred servers (many of which I probably dont have direct access to).
If I open an individual host and click on a process (for instance elastic search) I can see this information if I expand properties (EXE=java, JVM vendor=OpenJDK, JVM version=13.0.2, etc). It also seems to know that the process is an elastic search instance vs tomcat or something else (Type=Elasticsearch). I am just not sure how to query for all hosts/processes running a jvm.
You can use the "autotagging" functionality to have a common tag applied to any process with a JVM, see "Settings -> Tags -> Automatically applied tags".
E.g. name it "jvm".
Choose "Rule applies to" - "process groups".
In the rule you can use "Technology" - "Java" as selection.
It seems you need to define at least some condition here although none is required in this case, so any dummy-condition will do, e.g. "Java main class" - "does NOT begin with" - "zzzzz".
You should see the new tag be shown for each JVM process in the UI after a short while when auto-tags are applied.
Then you can use the Dynatrace REST API for "processes" to query for all processes with tag "jvm" and further filter from there.
If you manage to define a condition for what "outdated" means in your case, you may also be able to put a tag "outdated jvm" on such processes and be able to get the actual list of matching jvms directly.

How to refactor procedural start-up code?

I have a class (Android Activity) which handles start-up of my application. The application has some pretty complex start-up rules. Right now it looks like a bunch of spaghetti and I'm looking for strategies for refactoring it.
It's honestly such a mess I'm having problems hacking it down to provides pseudo code. In general there are some rules for start-up that are basically codified in logic:
Steps:
Check for error on last exit and flush local cache if necessary
Download settings file
Parse settings and save settings to local native format
Using the values in settings, do a bunch of 'house keeping'
Using a value in settings, download core data component A
Parse component A and load up local cache
During this logic, its also updating the user interface. All of this is handled in a zig-zagging, single monolithic class. Its very long, its got a bunch of dependencies, the logic is very hard to follow and it seems to touch way too many parts of the application.
Is there a strategy or framework that can be used to break up procedural start-up code?
Hmmm. Based on your steps, I see various different "concerns":
Reading and saving settings.
Downloading settings and components (not sure what a "component" is here) from the server.
Reading and instantiating components.
Flush and read cache.
Housekeeping (not really sure what this all entails).
UI updates (not really sure what this requires either).
You might try splitting up the code into various objects along the lines of the above, for example:
SettingsReader
ServerCommunicationManager (?)
ComponentReader
Cache
Not sure about 5 and 6, since I don't have much to go on there.
Regarding frameworks, well, there are various ones such as the previously mentioned Roboguice, that can help with dependency injection. Those may come in handy, or it may be easier just to do this by hand. I think that before you consider dependency injection, though, you need to untangle the code. All that dependency injection frameworks do is to initialize your objects for you -- you have to make sure that the objects make sense first.
Without any more details, the only suggestion that I can think of is to group the various steps behind well structured functions which do one thing and one thing only.
Your 6 steps look to be a good start for the 6 functions your init function should have. If #2 was synchronous (I doubt it), I would merge #2, #3 into a getSettings function.

Good alternative to crontab for java batch programs?

I've been looking for replacements for my companies current batch processing system(java SE + crontab), since there is a lot of java code/shell script duplication, most jobs are ETL and do very similar steps and also i want to provide platform independence instead of relying on crontab, to be more specific with our job role, the current job creation steps are this:
Develop a java program that meets a business requirement.
Test it in a production like enviroment until it meets the business requirement needs.
Pass it to a production server with a shell script that provides file maintenance, java prgram execution and error handling routines(avoid 2 processes of the same name running, mail log to support and developers in case of program error, check output file existence after java program ends if it's relevant for the interface), and specify recurrence data(how often will this program run).
Much of the same logic is being designed and developed into a system that contains generic routines that these programs or "interfaces"(thats how they call it there) do independently(using copy-pasted code usually since most routines are similar), but i am still missing a very important part which i need help with, this concerns the scheduler implementation that i use, and i need it to meet one of these two needs:
-I want to guarantee that whenever i stop the scheduling server for a system update(due to new jobs being added, etc) or whatever other reason, those jobs that could not run due to the system being down(example is 3 jobs that could not run at 3:00 P.M. because the system was down), get to run when the server gets back up, even though their respective scheduling time is gone.
OR in case that the first thing is not possible then:
-I need a way to update the scheduler with new jobs and also update the jars that provide these jobs without restarting the scheduler(sort of like OSGi).
Either of these conditions would satisfy my requirements, and would end my search for the replacement, i've looked into Quartz, Oddjob(theres a scheduler in production with this scheduler, but it needs restarting each time you add new jobs/libraries, does not satisfy my needs) and OSGi using an application server, but i am looking for better suggestions, in case you also know better options, they are also much appreciated.
You might also want to take a look at http://jcrontab.sourceforge.net/
Jcrontab is a scheduler written in Java. The project objective is to provide a fully functional schedules for Java projects.
Alright, found just what i wanted, Quartz does the trick, but i have to develop my own UI Management, FORTUNATELY, there's this project http://code.google.com/p/myschedule/ which contains all that i need(add, remove, resuming jobs), and it is cheap to run the webapp, since you can use tomcat. Now i can focus on designing reusable jobs :), thank god for Quartz!

Java File Wrapper API for Simple Data Structures

I want a simple file format to store and retrieve data from disk in Java.
name=value
list=value1,value2,value3
this is mostly going to be used for initial config settings used at startup of the app. I could invision having a watcher on the file to notify the app if it changes so the new settings can be applied potentially but that would be a nice to have. The first part would be pretty easy to write. I just don't want to reinvent the wheel if something is already out there for this and I'd prefer to avoid something as heavy as spring.
Take a look at the java.util.Properties class.
Properties
You can use the Preferences class. It has a notification system, but alas it doesn't notice changes made outside the running JVM or directly to the underlying configuration store (e.g. the config file). It's a really nice class though.
Have a look at OWNER API.
It incorporates most of the feature of java.util.Properties and adds more.
Version 1.0.4 is under development and it will have:
support for arrays and collections (list, set, arrays). It is already implemented on master branch.
"hot reload", when you change the file the config object gets reloaded (it can be synchronous or asynchronous and it does support event notification for reload). Already implemented in master branch.
a lot of features (variable expansion, type conversion). Available since version 1.0.3 and available on maven central repository.
Also for 1.0.4 is planned a validation mechanism that will check the file to be compliant before discarding the old content of the config file during the reload. (not implemented yet)
If you need some particular feature, just ask on github issues or become a contributor.

Use of AspectJ for debugging Enterprise Java applications

The idea is to utilize AOP for designing applications/tools to debug/view execution flow of an application at runtime. To begin with, a simple data(state) dump at the start and end of method invocation will do the necessary data collection.
The target is not application developers but high level business analyst or high level support people for whom a execution flow could prove helpful. The runtime application flow can also be useful in reducing the learning curve of an application for new developers especially in configuration loaded systems.
I wanted to know if there already exists such tools/applications which could be used. Or better, if this makes sense, then is there a better way to achieve this.
You could start with Spring Insight (http://www.springsource.org/insight) and add your own plugins to collect data appropriate for business analysts/support staff. If that doesn't meet needs, you can write your own custom aspects. It is not that hard.
You could write your own aspects, as suggested by ramnivas, but to prepare for the requests from the users, you may want to just have the aspects compiled into the application, so that you don't have to take a hit at run-time, and then they could just select which execution flows or method groups they are interested in, and you just call the server and set some variable to give them the information desired.
Writing the aspects is easy, but to limit recompiling, you may want to get an idea what the users will want, for example, if they want to have a log of every call made from the time a webservice is called until it gets to the database, then you can build that in, but it would be easier to know this up-front.
Otherwise the aspect does nothing, if the variable is not set, and perhaps unset the variable when finished.
You could also have where they can pick which type of logging and for which user, which may lead to more useful information.

Categories

Resources