Mahela Jayawardene's 91 helped Sri Lanka recover from 50-5 against New Zealand to 247 all out on day two in Galle.