#git alias gc='git clone' alias gp='echo "-> pulling from git" && git pull' #gradle and idea alias gw='./gradlew' alias gwci='echo "-> cleaning idea" && ./gradlew clean idea --refresh-dependencies' alias gwcb='echo "-> clean build" && ./gradlew clean build --refresh-dependencies' alias gwca='echo "-> clean assemble" && ./gradlew clean assemble --refresh-dependencies' alias oi='echo "-> opening intellij" && open $(find ./ -iname "*.ipr")' alias gwcio='gwci && oi' #Other alias gpci='gp && gwcio'
Simply put, this blog is a dumping ground for things I find interesting or worth remembering relating to Java, programming, web, lean, agile, etc, etc.
Showing posts with label java. Show all posts
Showing posts with label java. Show all posts
Friday, March 9, 2018
Some Java Dev Bash Aliases
Below are some aliases that can be added to your ~/.bash_profile that I've found useful. These aliases would be used for Git, Gradle, and IntelliJ, and for completeness are run on a mac.
Wednesday, January 6, 2016
Get a Resource File's Contents
Here is a little snippet on how to get the contents of a resource file.
And for reference here are the imports used
And for reference here are the imports used
import java.nio.charset.StandardCharsets; import java.nio.file.Files; import java.nio.file.Paths;
Thursday, September 24, 2015
Java 8 Predicate Chaining
The other day I got it in my head that it would be fun to chain together Java 8's Predicate lambdas in an object-oriented sort of way. Sort of like predicate1.and(predicate2).
It was a fun little exercise, but I don't think it would be incredibly useful, unless you are conditionally chaining them together in a for loop. It really wouldnt be any different than using similar syntax as predicate1 && predicate2.
But, with that being said, below is the PredicateChain class as well as the test that demonstrates how it could be used.
It was a fun little exercise, but I don't think it would be incredibly useful, unless you are conditionally chaining them together in a for loop. It really wouldnt be any different than using similar syntax as predicate1 && predicate2.
But, with that being said, below is the PredicateChain class as well as the test that demonstrates how it could be used.
Wednesday, February 18, 2015
Spring Data Custom Batch Save Repository
We use Spring Data JPA for our data access framework and generally we use JpaRepository to create our repos, which is a really handy way of doing it. Spring uses the SimpleJpaRepository for its implementation and it does have a save that allows an Iterable, but if you look at the code it just loops over the entities and calls the save method on each one.
So, if we want to save, or update, in a batch we need to use something else. Luckily, Spring JDBC has a pretty handy way of doing it and you can add custom repos to your repository interface.
Like so:
To see the collection util that splits out our collection into batches see Split a Java List Into a List of Sublists
So, if we want to save, or update, in a batch we need to use something else. Luckily, Spring JDBC has a pretty handy way of doing it and you can add custom repos to your repository interface.
Like so:
To see the collection util that splits out our collection into batches see Split a Java List Into a List of Sublists
Wednesday, February 4, 2015
Dynamic Where Clause in JPA
Every once in a while you'll come across a scenario where you want to dynamically add parameters to a query's where clause. For dynamic filtering I created a query generator that will simply add a segment to the where clause and the parameter to the query, and it works pretty well.
However, there are times when the scenario is a bit more complex and you'll need some more advanced logic. For example given this entity:
Let's say we're given a list of similar data objects that have an optional userId, the last name, and the last 4 digits of the ssn. We want to match these to our User object. The rules to match up to the User is thus: use the userId if there is one, but if not match with the lastName and the last 4 digits of the ssn.
The SQL Query would look something like this:
In order to generate a query we need some conditional logic to decide if we should add the userId to a list to query the :userIds parameter or if we should add an OR clause to find a match of lastname and ssn.
As you can see it is pretty verbose but the flexibility to basically do whatever we want is pretty powerful.
However, there are times when the scenario is a bit more complex and you'll need some more advanced logic. For example given this entity:
Let's say we're given a list of similar data objects that have an optional userId, the last name, and the last 4 digits of the ssn. We want to match these to our User object. The rules to match up to the User is thus: use the userId if there is one, but if not match with the lastName and the last 4 digits of the ssn.
The SQL Query would look something like this:
In order to generate a query we need some conditional logic to decide if we should add the userId to a list to query the :userIds parameter or if we should add an OR clause to find a match of lastname and ssn.
The Criteria API
I've generally tried to avoid using the criteria API for my dynamic queries because it is fairly verbose, but it is an effective tool for the complex logic we're looking at. Below is an example of using the criteria API to generate the query we need.As you can see it is pretty verbose but the flexibility to basically do whatever we want is pretty powerful.
Monday, January 26, 2015
Some Useful Tomcat VM Arguments
Here are some common VM arguments for Tomcat that I find myself looking up again and again. Especially when setting up a new dev environment. This is mostly based on Tomcat 7 but should also work for Tomcat 8, for the most part.
Another note: The configuration I'm using here is for my eclipse dev environment, but it could be adapted for any server.
More Memory
-Xms (Initial Heap size)
-Xmx (Maximum Heap Size)
-XX:MaxPermSize (Max Perm Size; tomcat 7 only)
Example:
-Xms512m -Xmx1024m -XX:MaxPermSize=512m
JMX Remote Access
-Dcom.sun.management.jmxremote -Dcom.sun.management.jmxremote.port=1234 -Dcom.sun.management.jmxremote.authenticate=false -Dcom.sun.management.jmxremote.ssl=false -Djava.rmi.server.hostname=127.0.0.1
Then you can then connect to your local process with JConsole with localhost:1234
Some useful links:
Monday, October 20, 2014
Creating a JSON Schema from a POJO
If you're documenting your REST API it might be useful to show the schema definition behind the JSON objects being used. The JSON Schema format provides a definition similar to XML's XSD.
It isn't very difficult to generate a JSON schema with Jackson. Given a simple POJO with some properties which includes a list of another simpler POJO:
We can use the Jackson JsonSchema Module to generate the Schema with code similar to this:
This code would generate this json schema:
The only downside is if you're using a different version of jackson (we were using one from the codehaus repo) you'll need to change your json annotations so it will print out correctly.
All the code can be found on My Github Account
It isn't very difficult to generate a JSON schema with Jackson. Given a simple POJO with some properties which includes a list of another simpler POJO:
public class SimplePojo { private Integer id; private String name; private String description; private Listchildren; // getters and setters ommitted }
public class SimplePojoChild { private Integer id; @JsonProperty("cName") private String childName; @JsonProperty("cDescription") private String childDescription; // getters and setters ommitted }
We can use the Jackson JsonSchema Module to generate the Schema with code similar to this:
public class JsonSchemaGenerator { public static void main(String[] args) throws JsonProcessingException { ObjectMapper mapper = new ObjectMapper(); SchemaFactoryWrapper visitor = new SchemaFactoryWrapper(); mapper.acceptJsonFormatVisitor(SimplePojo.class, visitor); JsonSchema schema = visitor.finalSchema(); System.out.println(mapper.writerWithDefaultPrettyPrinter().writeValueAsString(schema)); } }
This code would generate this json schema:
{ "type" : "object", "id" : "urn:jsonschema:com:bmchild:model:SimplePojo", "properties" : { "children" : { "type" : "array", "items" : { "type" : "object", "id" : "urn:jsonschema:com:bmchild:model:SimplePojoChild", "properties" : { "cName" : { "type" : "string" }, "id" : { "type" : "integer" }, "cDescription" : { "type" : "string" } } } }, "name" : { "type" : "string" }, "description" : { "type" : "string" }, "id" : { "type" : "integer" } } }
The only downside is if you're using a different version of jackson (we were using one from the codehaus repo) you'll need to change your json annotations so it will print out correctly.
All the code can be found on My Github Account
Monday, August 18, 2014
Splitting a String by Length or Position
Here is a simple method to split a string into a list of substrings based on length. For example, given the string
Here is the method and tests:
"12345678901"
, if we were to split by 2 we would expect a result like {"12", "34", "56", "78", "90", "1"}
.Here is the method and tests:
Friday, May 9, 2014
Manually Execute a Spring @Scheduled method
I have to admit I'm a little disappointed that there isn't an easy way to hook into Spring's @Scheduled jobs like there is with Quartz jobs. I expected there to be some way to get the list of scheduled jobs but after digging through the ScheduledAnnotationBeanPostProcessor it doesn't look like they make that data visible.
So, here is a way to execute a method annotated with the @Scheduled annotation. And, for the record, I don't like having to manually create Spring beans, loop through methods, or any of the other smelly things that are going on here.
The BaseAjaxResponse is simply a pojo that gets converted into json (see SpringMVC and jackson)
So, here is a way to execute a method annotated with the @Scheduled annotation. And, for the record, I don't like having to manually create Spring beans, loop through methods, or any of the other smelly things that are going on here.
The BaseAjaxResponse is simply a pojo that gets converted into json (see SpringMVC and jackson)
Friday, May 2, 2014
A Simple CAS Java Rest Client Example
Here are some notes from creating a simple Java Client to access a REST endpoint behind CAS authentication.
Resourcs for enabling CAS Rest Access:
And here is my simple implementation:
Resourcs for enabling CAS Rest Access:
- CAS REST API: https://wiki.jasig.org/display/casum/restful+api
- POST to protected resource: https://groups.google.com/forum/#!searchin/jasig-cas-user/post$20to$20rest$20resource/jasig-cas-user/NWmFahj9usk/YBECPJULN3sJ
(make sure redirectAfterValidation = false for the server using CAS to authenticate, but could have unintended consequences for browsers) - More redirectAfterValidation: https://wiki.jasig.org/display/CASC/Configuring+the+Jasig+CAS+Client+for+Java+in+the+web.xml
- CAS on GitHub: https://github.com/Jasig/cas
And here is my simple implementation:
- Main.java contains a runnable main method and makes the calls to our REST service
- CasLogin.java holds our user's credentials and makes the call to get the TicketGrantingTicket and the ServiceGrantingTicket. You might be able to store the TGT in order to avoid passing the credentials over the wire for each request.
- RestClient.java holds some helper methods to make http calls
Monday, April 21, 2014
Create a Static Code Analysis Tool
- 39% of developers don't monitor their code for quality
- Good code quality improves predictability (more accurate guesstimations)
- There are a lot of good and free code quality tools
- Sonar
- PMD
- Checkstyle
- Findbugs
- Setting up a code analysis server will take some time to tweak and get the rules just right
- Making sweeping changes to tweak minor violations will probably piss off other developers
Here are the links. Html Version | PDF Version
We've been using Sonar for a number of years and it has pretty good integration with the other code analysis tools as well as with Eclipse so you can run a local analysis as well as see any violations as you're viewing the source. The only gripe I have is that you can't run an analysis against just one file. It runs against the entire project.
Tuesday, March 18, 2014
Stamping Text on Existing Pdf Files
I recently created a framework for stamping text on existing pdf files by using the iText project. It is available here on github.
The workflow goes something like this:
The workflow goes something like this:
- Create a PdfForm
- Add your PdfFormStamps (location, alignment, and rotation and defined in the PdfFormPlaceholder
- Run it through the PdfFormServiceImpl.stampPdf
- Then you can save the bytes wherever you like
Seeing the results is kind of clunky since it's kind of a guessing game where your stamps will show up, but better than nothing I suppose.
Monday, February 10, 2014
Spring MVC 3: Property referenced in indexed property path is neither an array nor a List nor a Map
JQuery's $.ajax does an excellent job mapping a json object to parameters, but when you start getting into more complex objects Spring MVC doesn't know how to interpret it sometimes.
For example if you have a json object like:
The problem is that Spring MVC is expecting a parameter format like
In order to get that you can do it in 1 of two ways. You can do the quick and dirty way, which changes the way you're building your json object. Or, the other way is to extend the jQuery plugin to build parameters differently.
To change the javascript code was pretty simple and looked something like this
To modify the jquery plugin I would suggest taking a look here.
And for reference here are the pojos I was mapping to.
For example if you have a json object like:
{
"beans":[
{
"agreemendId":1,
"answerId":2
}
]
}
JQuery will map your parameters like
beans[0][agreementId]:1
beans[0][answerId]:2
The problem is that Spring MVC is expecting a parameter format like
beans[0].agreementId:1
beans[0].answerId:2
In order to get that you can do it in 1 of two ways. You can do the quick and dirty way, which changes the way you're building your json object. Or, the other way is to extend the jQuery plugin to build parameters differently.
To change the javascript code was pretty simple and looked something like this
var answers = {};
answers['beans[' + index +'].agreementId'] = agreementId;
answers['beans[' + index +'].answerId'] = value;
To modify the jquery plugin I would suggest taking a look here.
And for reference here are the pojos I was mapping to.
Friday, January 3, 2014
Split a Java List Into a List of Sublists
Often times I'm updating a large amount of records with a batch SQL script, and a lot of times I run into this error: DataIntegrityViolationException: Prepared or callable statement has more than 2000 parameter.
So to help me to update with only the allowed number of parameters I've created a method that will split a List of objects into sublists based on the max number of records you want. And then I would pass each sublist to the batch update method.
So to help me to update with only the allowed number of parameters I've created a method that will split a List of objects into sublists based on the max number of records you want. And then I would pass each sublist to the batch update method.
Wednesday, December 18, 2013
How to Not Run Integration Tests With the Eclipse JUnit Plugin
Here is the scenario. A Maven multi-module project being developed in Eclipse, the m2e plugin keeps the different modules in sync in eclipse, integration and unit tests mixed throughout each project, and integration tests using the naming convention *IntegrationTest.java.
Here is the problem. Using maven to run unit tests for the entire project kind of sucks. I mean it's easy to limit the tests being run with the maven sure fire plugin's exclude, but the problems are 1) it takes too long to build the project before the tests are run and 2) if you have altered a upstream project it won't show any compile problems in eclipse because m2e keeps them in sync, but the maven build doesn't seem to know about it so it fails to build.
Here is the problem. Using maven to run unit tests for the entire project kind of sucks. I mean it's easy to limit the tests being run with the maven sure fire plugin's exclude, but the problems are 1) it takes too long to build the project before the tests are run and 2) if you have altered a upstream project it won't show any compile problems in eclipse because m2e keeps them in sync, but the maven build doesn't seem to know about it so it fails to build.
Half of the solution
The first half of the solution is the JUnit plugin that comes with eclipse. You can right-click on a project and Run As a JUnit Test, or ctrl+alt+shift+T, and it will run through all the tests really fast.
This is only half of the solution because it also runs through the integration tests, which generally take a long time.
To work around running the integration tests you can create a test suite. The test suite is a pretty good way to limit what tests are run, but out of the box JUnit 4 requires you to either annotate each test with a @Category annotation on each test and then to use the @IncludeCategory or @ExcludeCategory on the test suite. Or, the other less friendly version would be to use the @SuiteClasses annotation on the test suite and list out each test class individually. When you have hundreds of test classes this is a lot of work.
The other half of the solution
What we really need is a way to use some wild cards to limit the tests we want to run. Similar to the surefire plugin's exclude. The solution I've come across is something called the JUnit Toolbox. The cool thing about the project is that it has a couple of custom runners that allow you to specify wildcards in the classes to run. What that means is that we can write a test suite like this
import org.junit.runner.RunWith;
import com.googlecode.junittoolbox.ParallelSuite;
import com.googlecode.junittoolbox.SuiteClasses;
@RunWith(ParallelSuite.class)
@SuiteClasses({"**/*Test.class", "!**/*IntegrationTest.class"})
public class AllUnitTestsTestSuite {}
and it will only run the tests in that project that don't have IntegrationTest.class.
Now the only downside I've come across is that if you have classes that match the naming convention and they don't have tests in them you will get a initialization error, but you can use categories to restrict those ones. Also if you're looking for something to run tests for all of your projects this doesn't seem to address that either. All in all though, I've gone from having to use a fairly manual process that takes 20-30 seconds with maven to a pretty automatic one that takes about 9.
Tuesday, December 3, 2013
Spring @Scheduled method found on bean target class
I received this error while wiring up a method with the @Scheduled annotation on it. The method was a concrete implementation of an interface so at runtime it was a proxy. Apparently, the @Scheduled annotation has a hard time being on a proxy, but the workaround seems to be to put the annotation on the interface and the concrete impl.
Here is a bug logged here.
And more information on @Scheduled.
Here is a bug logged here.
And more information on @Scheduled.
Friday, November 15, 2013
Java and Mockito: Patterns that Make Testing Hard
This article isn't meant to convince anyone of the importance of unit tests. There's already a ton of information out there for that. This article looks at a few common practices that make testing harder and might tempt a developer to skip testing altogether. I'm in no way saying the solutions presented here are the best solutions. Every case is different and every developer is different too. If these solutions don't fit your style I'd love to hear of ones that do.
Complex Private Methods
In development we're often taught that we should keep related method calls inside of the class. This is good, but it can often lead to private methods with a lot of logic.
Consider the following code.
The problem: We can't isolate each piece of logic in our tests. Our tests need to account for each private method scenario and makes testing a pain to write and maintain. In each test we also have to account for the logic in the executeSomeLogic method. If we refactor any of the private methods it could possibly cascade to forcing us to refactor all of our tests.
A solution: Pull the private methods out into a separate service. That way we can mock them in our original test and test each private method in isolation. Like this:
The problem: We can't isolate each piece of logic in our tests. Our tests need to account for each private method scenario and makes testing a pain to write and maintain. In each test we also have to account for the logic in the executeSomeLogic method. If we refactor any of the private methods it could possibly cascade to forcing us to refactor all of our tests.
A solution: Pull the private methods out into a separate service. That way we can mock them in our original test and test each private method in isolation. Like this:
Static Util Methods
Static methods can be very handy to use since we don't need to worry about instantiating. And, generally every static method will include its own test suite so we can be confident the static method will execute how we expect. But, because of their static nature we must always execute that logic, even when we're testing something else.
Consider the following code.
The problem: Logic that uses static methods will be executed during the test and can lead to more complex setup. Basically, you would be testing your logic and the logic of the static method.
A solution: Wrap the static methods in an interface that can then be mocked. Like this:
Obviously (I hope), if the logic is very simple then this would not be worth it.
Using @Before to setup for every test
Tests can get pretty complicated and the junit @Before annotation can be abused to the point where the test becomes hard to maintain.
Consider the following code.
The problem: The setup up becomes too complicated so that when we refactor some existing logic and we need to update the test we find ourselves taking a lot of unnecessary time understanding how the test is setup before we can alter it.
A solution: Write more setup code in each test and keep the code in the @Before block to a minimum. This allows you to be flexible and it is easier to modify without any unforeseen consequences. Like this:
The problem: The setup up becomes too complicated so that when we refactor some existing logic and we need to update the test we find ourselves taking a lot of unnecessary time understanding how the test is setup before we can alter it.
A solution: Write more setup code in each test and keep the code in the @Before block to a minimum. This allows you to be flexible and it is easier to modify without any unforeseen consequences. Like this:
And if you find yourself repeating a lot of code create little helper methods.
Long if...else or switch clauses
If..else and switch clauses are a great way to easily step through a logical ladder to get the desired behavior. But with that quickness comes more complex testing.
Consider the following code:
The problem: A long logical selector forces us to write a lot of tests in order to cover each possible scenario.
A solution: Refactor to use a common interface with an implementation for each scenario. This allows us to isolate our tests and to refactor without affecting any other code. Like this:
Understandably your particular case probably isn't going to fall into one of the above problems exactly and you probably won't be able to make your tests and business logic 100% simple. That's okay in my opinion. My goal here is only to get people thinking about alternatives to some common practices and why some of these practices cause problems.
Dive Deeper with some light reading
Tuesday, September 24, 2013
Spring Mobile: Spring MVC XML Config
I integrated Spring Mobile(version 1.0.2.RELEASE) for the first time into a Spring MVC project (version 3.2.4.RELEASE). It went relatively smoothly with a couple of exceptions.
First, the Spring Mobile Project page assumes you're using configuration classes. And I swear the argument resolver is wrong since that class doesn't exist, but whatever. Here is the XML I used for our config:
And Second, Spring Mobile seems to be a version behind Spring MVC so instead of using the maven dependency XML shown on the site I used this:
Beside the project page, here is some other useful documentation.
First, the Spring Mobile Project page assumes you're using configuration classes. And I swear the argument resolver is wrong since that class doesn't exist, but whatever. Here is the XML I used for our config:
And Second, Spring Mobile seems to be a version behind Spring MVC so instead of using the maven dependency XML shown on the site I used this:
Beside the project page, here is some other useful documentation.
Monday, August 26, 2013
Use Spring to Load a Resource From the Classpath
The architecure of our projects is such that when I tried to load a resource via a common method call with the traditional
I knew the resource was on the classpath, but even so I could only load resources from the main project. I'm sure if I played around with it long enough I could get it to work(maybe this), but I never really liked having to get a class loader from an existing class anyways.
In my searching I eventually came across a useful class called the ClassPathResource provided in Spring. Simply by instantiating a ClassPathResource it seamlessly grabbed the resource I knew was there.
This allowed me to change from
this.getClass().getClassLoader().getResourceAsStream(resourcename)
it was unable to do so. The resource was in another project which was wired up as a dependency in the webapp.
I knew the resource was on the classpath, but even so I could only load resources from the main project. I'm sure if I played around with it long enough I could get it to work(maybe this), but I never really liked having to get a class loader from an existing class anyways.
In my searching I eventually came across a useful class called the ClassPathResource provided in Spring. Simply by instantiating a ClassPathResource it seamlessly grabbed the resource I knew was there.
This allowed me to change from
reportInputStream = this.getClass().getClassLoader().getResourceAsStream(reportPath);
to reportInputStream = new ClassPathResource(reportPath).getInputStream();
Tuesday, August 20, 2013
Create XML With Only XPath and Values
Today I found myself needing to create a pretty straight forward XML document. The catch was I was taking values from a database that included the xpath address and a value, and from there I had to create the document. It's not incredibly complicated but briefly googling it didn't turn up any results so here is my solution, using the dom4j (1.6.1) API. (Other dependencies include Jaxen, Apache Commons, and log4j).
The real meat of this solution is the
This example will create this XML even though we only really tell it to create 5 elements. (the elements depend on the
The real meat of this solution is the
addElementToParent
method. What it does it searches for the parent element of the provided xpath. If it doesn't find the parent in the document it will create it via a recursive call. Another trick is it will create any siblings that are needed in case the parent has an index (like /root/parent[3]
). Once all the prerequisite elements are created it will then create the element we originally asked for.
This example will create this XML even though we only really tell it to create 5 elements. (the elements depend on the
data
element so it is created automatically.
Subscribe to:
Posts (Atom)