View Javadoc
1   /*
2    * Copyright 2012 the original author or authors.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.openehealth.ipf.commons.ihe.core.payload;
17  
18  import java.lang.management.ManagementFactory;
19  import java.lang.management.RuntimeMXBean;
20  import java.text.SimpleDateFormat;
21  import java.util.Date;
22  
23  /**
24   * Evaluation context of expressions for payload log file names. Expression or templating engines being
25   * used should be able to call
26   *
27   * @author Dmytro Rud
28   */
29  public class PayloadLoggingContext {
30      private static final String PROCESS_ID;
31  
32      static {
33          RuntimeMXBean mx = ManagementFactory.getRuntimeMXBean();
34          PROCESS_ID = mx.getName().replace("@", "-");
35      }
36  
37      private final String sequenceId;
38      private final Date currentDate;
39  
40      /**
41       * Initializes this context with a sequence ID
42       *
43       * @param sequenceId sequence ID
44       */
45      public PayloadLoggingContext(Long sequenceId) {
46          this.sequenceId = String.format("%012d", sequenceId);
47          this.currentDate = new Date();
48      }
49  
50      /**
51       * @return the current process ID
52       */
53      public String getProcessId() {
54          return PROCESS_ID;
55      }
56  
57      /**
58       * @return the sequence ID
59       */
60      public String getSequenceId() {
61          return sequenceId;
62      }
63  
64      /**
65       * @param formatSpecification date format specification
66       * @return the current date/time of this context
67       * @see SimpleDateFormat
68       */
69      public String date(String formatSpecification) {
70          return new SimpleDateFormat(formatSpecification).format(currentDate);
71      }
72  }