View Javadoc

1   /*
2      Licensed under the Apache License, Version 2.0 (the "License");
3      you may not use this file except in compliance with the License.
4      You may obtain a copy of the License at
5   
6        http://www.apache.org/licenses/LICENSE-2.0
7   
8      Unless required by applicable law or agreed to in writing, software
9      distributed under the License is distributed on an "AS IS" BASIS,
10     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11     See the License for the specific language governing permissions and
12     limitations under the License.
13  */
14  package uk.nhs.interoperability.service;
15  
16  import java.text.SimpleDateFormat;
17  import java.util.Calendar;
18  import java.util.Date;
19  
20  import uk.nhs.interoperability.capabilities.AuditException;
21  import uk.nhs.interoperability.capabilities.AuditService;
22  import uk.nhs.interoperability.infrastructure.ITKMessageProperties;
23  
24  import com.xmlsolutions.annotation.Requirement;
25  
26  /**
27   * The Class ITKSimpleAudit.
28   *
29   * @author Michael Odling-Smee
30   * @author Nicholas Jones
31   * @since 0.1
32   */
33  @Requirement(traceTo={"IFA-SEC-02"}, status="in-progress")
34  public class ITKSimpleAudit implements AuditService {
35  	
36  	/** The Constant DATE_FORMAT. */
37  	private static final SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
38  	
39  	/** The Constant CAL. */
40  	private static final Calendar CAL = Calendar.getInstance();
41  	
42  	/** The Constant _INSTANCE. */
43  	private static final ITKSimpleAudit _INSTANCE = new ITKSimpleAudit();
44  
45  	/* (non-Javadoc)
46  	 * @see uk.nhs.interoperability.capabilities.AuditService#auditEvent(java.lang.String, long, uk.nhs.interoperability.infrastructure.ITKMessageProperties)
47  	 */
48  	@Override
49  	@Requirement(traceTo={"COR-DEH-04"}, status="in-progress")
50  	public void auditEvent(String event, long eventTime, ITKMessageProperties itkMessageProperties) throws AuditException {
51  		if (itkMessageProperties == null && event == null) {
52  			throw new AuditException("Could not write audit as either the ITKMessageProperties and/or event was not provided");
53  		}
54  		System.out.println(DATE_FORMAT.format(CAL.getTime()) 
55  					+ " [AUDIT] " + event + " at " + DATE_FORMAT.format(new Date(eventTime))
56  					+ " from " + itkMessageProperties.getAuditIdentity());
57  		
58  	}
59  	
60  	/**
61  	 * Gets the single instance of ITKSimpleAudit.
62  	 *
63  	 * @return single instance of ITKSimpleAudit
64  	 */
65  	public static final AuditService getInstance() {
66  		return _INSTANCE;
67  	}
68  
69  }