Development

In the example below we build on the Reading a zip file from java using ZipInputStream page to provide basic filtering. This filtering is provided by the filteredExpandZipFile method taking a Predicate. Every ZipEntry is passed to the predicate, but only ones that match (predicate returns true) are included.

Note that the size of an entry cannot be accurately determined in all cases, so it is not safe to perform validation on the this field.

In this example I demonstrate how to use ConcurrentMap along with Future to generate a lazy cache. Concurrent maps are a good choice in some situations, where absolute atomicity can be traded off for performance, some level of control over ordering of events has been traded for performance.

Following on from one of our popular articles on Reading a zip file from java using ZipInputStream we've put together a new article on how to create a zip archive using Java. The below example uses ZipOutputStream to create a zip file from all the items in a directory.

Zip files are written slightly differently to a normal stream in that each entry is put into the stream one at a time. So the procedure is as follows:

  1. Create a zip archive using ZipOutputStream
  2. Create a new ZipEntry to represent the file to be added
  3. Write the bytes for the file
  4. Repeat steps 2,3 for each file to be added
  5. Close the archive

This is a question as much as a discussion, I can’t find a lot of detail on the state of play in this area and would really welcome any feedback or corrections. Please don’t read this as a negative article as that is not how it is intended; it’s in the optimisation section but I’m not sure what impact it really has. One thing for sure, for 99% of systems you probably don’t have to worry about what’s going on here at all.

Recently I started to wonder how Garbage collection events in large heaps affect cache performance. Earlier today I read an interesting article on the mechanical sympathy blog (Mechanical Sympathy: CPU flushing article ) about the way processor caches work, and it re-enforced a feeling I’ve had for a while; that old generation GC’s on very large heaps could cause a lot of cache misses because each object in the generation has to be marked and swept. This led to more searches where I dug up this page on stack overflow (Stack Exchange: is GC cache friendly).

Over the years there have been no shortage of ways to format a string in java. What with the + operator, StringBuffer, StringBuilder, String.format(..) and various specialised formatters for numbers and dates we sometimes feel a little spoilt for choice. But how do they all work and what are their advantanges / disadvantages?

There are several ways to format dates in Java, but by far the easiest is to use DateFormat. Creating a DateFormat is very similar to NumberFormat that we saw on the previous page. Here are the static factory methods called directly on the DateFormat class:

Following on from Setting up role based security in tomcat, we now switch from using a memory realm to one backed by a database. Memory realms are great for testing but in any real application is would probably not be acceptable. Normally user credentials are stored in a database, so for this purpose there is a realm based on a datasource.

Depending on your view of things, you will either edit server.xml in the tomcat-home/conf directory, or you will edit the context file for the specific application. There are a few choices here, all of which are explained in the tomcat 7.0 documentation. For the purpose of this article we will edit server.xml.

CountDownLatch provides a means of waiting for a number of asynchronous events before proceeding. In order to do this one constructs a latch providing the event count. Then one thread would normally call await Whilst the other thread calls countDown.  Once the count reaches zero the await call returns and the latch is set. If the call to await happens after the latch is set it returns immediately.

In our example we need to wait for a thread to initialise before proceeding. We achieve this by creating a count down latch with a count of 1. Once the thread has done its work, it calls countDown on the latch. In the mean time the main thread has continued to do its longJob and then called await on the CountDownLatch instance. Calling await blocks until countDown has been called enough times (in this case once).

In this entry I show how to use the inbuilt Java XMLStreamReader PULL parser class to read an XML file. The XML stream libraries are PULL based XML parsers that do not load the whole document into a memory structure, so therefore are more suited to large volumes of XML.

Below is an example XML file for a zoo, it contains Animal data types that have both attributes and data. It is kept simple for the sake of example. To run the example, copy this XML into the ROOT of your classpath.

Following on from Setting up role based security in tomcat we now look at accessing the realm security information from code. Although tomcat takes care of authenticating users at the right time, there are still times when we need to programatically access the credential information. For example the following snippet from userProfile.jsp is a mixed mode page In that anyone can view the page, but some users with manager role see more information.

To do this we use a method on the request object. request.isUserInRole(roleName);.Below is an example of its usage from the userProfile page.

Subcategories

This area of the website discusses JVM optimisation and analysis techniques. This subject tends to trigger quite strong reactions in people, and it's not unusual for there to be many differing opinions on this subject.

My aim is to provide articles that are based on evidence and follow best practice.

About Web design 

Web design is a rapidly changing field, both technologies and web-design standards change frequently leaving one with fairly frequent update cycles. In addition to this, any CMS based solution must be updated frequently to ensure that the most recent version. This version will be hardened as much as possible against external attack. If you've not updated your CMS solution in some time, I recommend you do so as soon as you can. If you're running Joomla, there's an article linked from this page to help you do exactly that.

For new websites I recommend starting with a Fluid layout based on twitter bootstrap or similar technology; it may feel a little bit restrictive at first, but it will size dynamically onto almost any device as repayment for the slightly more complex design phase. In terms of CMS I prefer Joomla, it is easy to manage, keeps itself up to date and is freely available under an opensource license.

Do we offer web-design services?

Yes, and it can be cheaper than you think. Our sites are generally based on Joomla content engine, mobile ready by default and will include training to ensure you know how to manage the instance going forwards. Contact us using the link at the top of the page, or see the consulting area for more information.

Can you build your own site?

 

It is possible to build your own site, as long as you are quite technical and can understand HTML and CSS, there is no reason not to have a go. Bear in mind this is not a quick process, if you are not used to web development, allow a few weeks to read about CSS, HTML and hosting. Personally, I would recommend Joomla as the content management solution and managed webhosting, from any reputable provider. A good starting point for developers that are unfamiliar with web-development is w3 schools.

Questions to ask before starting designing a web site

  • What you want the site to look like? Remember the more complex, the greater the cost will be. Draw some simple story boards of your website to get started.
  • Check who is going to own the copyright for the work once complete, and then ensure you are happy with that. It's not always a problem for someone else to own the copyright and give a 100 year lease, but it's best to know this upfront.
  • Is the site going to be static, or based on a content management solution? Static sites are more difficult to change, but use less resources and there are no server components to keep up to date. However, a dynamic solution is easier to change and is normally driven from a template; which makes wholesale change easier.
  • Do you need an ecommerce / shop presence? If so, which shopping cart and payment system are you going to use. Does your company have a long enough track record to meet the card payment credit checks.
  • If you are building an e-shop, do you have an existing stock management system? If so you will need a shop solution that is compatible with that stock system.
  • Mobile support is now very important, Google actively check if a site is mobile friendly (makes up > 10% of visitors) even the technology pages on this site.
  • Have you yet registered the domain? If not think long and hard about what domain to choose as once you're on a domain, it's difficult to change it.

 

Importance of social media

social media

These days, you can have a great site, but unless you've connected all the dots, and integrated your site into a valid social media strategy, its unlikely to be enough.

I learn't this lesson the hard way nearly ten years ago when we first built the nutricherry site, we spent all our effort building a site that looked great, spending countless hours on user testing, to ensure the site worked properly.

We did all this only to find we had not worked out who was going to visit the site, and how they were going to find it. Needless to say I would not make this costly mistake again.

Linking your site to social platforms

There are many, many platforms in the social media domain, and not all options will be good for every site. For example, some may want to put video content onto youtube and other video aggregators, where others may want to put professional contacts onto linkedIN. Nearly everyone would want to have a facebook and twitter presence. Think carefully about who may want to link with you and which types of social media they may be using. Don't forget that many businesses block most social media sites, so for professionals, this may severely limit what they have access to.

Blogs and building links socially

Most people who have followed search engine strategies will be aware that recently google have changed focus, and gone against traditional link farms. To be honest, I think that thier decision was right. Paying for links on farms or questionable sites was never a good policy.

Further, I believe that the best strategy is to slowly build credibility by blogging, writing good content and making it known via social networks. Also, when appropriate using a PR company to get some extra exposure.

SEO (Search Engine Optimisation)

seo board

Optimising a site, so that it places well for the chosen search terms is a tricky business. For a start which google searches do you want to do well in? Locally or globally? These are but two questions of many for anybody wanting to optimise a site.

First, decide which search phrases you want to target, and help you to ensure that your site does well for these phrases. Think what you would search for if you were looking for your products and services. Take a look at current google searches using googles webmaster tools.

ONLY USE SAFE techniques to optimise your site, ones that will ensure you have a good relationship with all search engines. Recently google have cut up heavily on sites that were using an underhand technique for linking sites together, personally I always frowned upon this activity, and looks like I was proved right recently.

Have you been hit by googles changes recently

If your organic search results from google have dropped suddenly, you could have been hit by one of two updates by google, Project Panda or Project Pengiun. If this is the case, you'll have to take stock of your inbound links from other sites, and try to clean them up. It's often harder to remove and clean up links than to create them in the first place.

Do you know your incoming links?

If you had some SEO done a few years ago, do you know where your links are coming from? Have you got a fairly normal link profile? If the answer is don't know, get onto it right away as google are cutting up on this, and their algorithms improve with every release. Open up a browser and do a search something like the following

allinanchor:yourdomain.ext -site:yourdomain.ext

What the above query does is to find out who is linking to you, exluding results from your own site. This will give you a starting point to finding out where your links come from.

Don't forget mobile

Ensure your site is mobile friendly, if not you could lose a good percentage of your visitors. More people than ever search from phones and tables, and id your site is not mobile friendly, it may not rank as well. Webmaster tools now reports on sites that don't look right on mobile devices, and is warning that failure to act may result in reduce relevance for mobile users.