1 /*
2 * EL4J, the Extension Library for the J2EE, adds incremental enhancements to
3 * the spring framework, http://el4j.sf.net
4 * Copyright (C) 2010 by ELCA Informatique SA, Av. de la Harpe 22-24,
5 * 1000 Lausanne, Switzerland, http://www.elca.ch
6 *
7 * EL4J is published under the GNU Lesser General Public License (LGPL)
8 * Version 2.1. See http://www.gnu.org/licenses/
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU Lesser General Public License for more details.
14 *
15 * For alternative licensing, please contact info@elca.ch
16 */
17 package ch.elca.el4j.core.correlationId;
18
19 import org.slf4j.MDC;
20
21 import ch.elca.el4j.services.persistence.generic.primarykey.UuidPrimaryKeyGenerator;
22
23 /**
24 *
25 * Provides an implementation of the {@link CorrelationIdManager} which stores the
26 * current correlation Id as the variable 'correlationId' in SLF4J's MDC.
27 *
28 * @svnLink $Revision: 4253 $;$Date: 2010-12-21 11:08:04 +0100 (Di, 21. Dez 2010) $;$Author: swismer $;$URL: https://el4j.svn.sourceforge.net/svnroot/el4j/branches/el4j_3_1/el4j/framework/modules/core/src/main/java/ch/elca/el4j/core/correlationId/CorrelationIdManagerSlf4jImpl.java $
29 *
30 * @author Simon Stelling (SST)
31 */
32 public class CorrelationIdManagerSlf4jImpl implements CorrelationIdManager {
33
34 /**
35 * The variable name used to store the correlationId in SLF4J's MDC.
36 */
37 private static final String CORRELATION_ID_VARNAME = "correlationId";
38
39 /**
40 * UUID generator used to create new correlation ids.
41 */
42 private static final UuidPrimaryKeyGenerator UUID_KEY_GENERATOR = new UuidPrimaryKeyGenerator();
43
44 private String generateNewId() {
45 return Integer.toString(UUID_KEY_GENERATOR.getPrimaryKey().hashCode());
46 }
47
48 /**
49 * {@inheritDoc}
50 */
51 @Override
52 public void createNewCorrelationId() {
53 String correlationId = generateNewId();
54 setCurrentCorrelationId(correlationId);
55 }
56
57 /**
58 * {@inheritDoc}
59 */
60 @Override
61 public String getCurrentCorrelationId() {
62 return MDC.get(CORRELATION_ID_VARNAME);
63 }
64
65 /**
66 * {@inheritDoc}
67 */
68 @Override
69 public void setCurrentCorrelationId(String correlationId) {
70 if (correlationId != null) {
71 MDC.put(CORRELATION_ID_VARNAME, correlationId);
72 } else {
73 clearCurrentCorrelationId();
74 }
75 }
76
77 /**
78 * {@inheritDoc}
79 */
80 @Override
81 public void clearCurrentCorrelationId() {
82 MDC.remove(CORRELATION_ID_VARNAME);
83 }
84
85 }