1 /*
2  * Copyright 2002-2017 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 
17 module hunt.stomp.support.IdTimestampMessageHeaderInitializer;
18 
19 import hunt.stomp.support.MessageHeaderAccessor;
20 
21 import hunt.stomp.IdGenerator;
22 import hunt.stomp.MessageHeaders;
23 
24 import std.uuid;
25 
26 /**
27  * A {@link hunt.stomp.support.MessageHeaderInitializer MessageHeaderInitializer}
28  * to customize the strategy for ID and TIMESTAMP message header generation.
29  *
30  * @author Rossen Stoyanchev
31  * @since 4.1
32  */
33 class IdTimestampMessageHeaderInitializer : MessageHeaderInitializer {
34 
35 	private __gshared IdGenerator ID_VALUE_NONE_GENERATOR;
36 
37 	shared static this() {
38 		ID_VALUE_NONE_GENERATOR = new class IdGenerator {
39 			UUID generateId() {
40 				return MessageHeaders.ID_VALUE_NONE;
41 			}
42 		};
43 	}
44 	
45 	private IdGenerator idGenerator;
46 
47 	private bool enableTimestamp;
48 
49 	/**
50 	 * Configure the IdGenerator strategy to initialize {@code MessageHeaderAccessor}
51 	 * instances with.
52 	 * <p>By default this property is set to {@code null} in which case the default
53 	 * IdGenerator of {@link hunt.stomp.MessageHeaders} is used.
54 	 * <p>To have no ids generated at all, see {@link #setDisableIdGeneration()}.
55 	 */
56 	public void setIdGenerator(IdGenerator idGenerator) {
57 		this.idGenerator = idGenerator;
58 	}
59 
60 	/**
61 	 * Return the configured {@code IdGenerator}, if any.
62 	 */
63 	
64 	public IdGenerator getIdGenerator() {
65 		return this.idGenerator;
66 	}
67 
68 	/**
69 	 * A shortcut for calling {@link #setIdGenerator} with an id generation strategy
70 	 * to disable id generation completely.
71 	 */
72 	public void setDisableIdGeneration() {
73 		this.idGenerator = ID_VALUE_NONE_GENERATOR;
74 	}
75 
76 	/**
77 	 * Whether to enable the automatic addition of the
78 	 * {@link hunt.stomp.MessageHeaders#TIMESTAMP} header on
79 	 * {@code MessageHeaderAccessor} instances being initialized.
80 	 * <p>By default this property is set to false.
81 	 */
82 	public void setEnableTimestamp(bool enableTimestamp) {
83 		this.enableTimestamp = enableTimestamp;
84 	}
85 
86 	/**
87 	 * Return whether the timestamp header is enabled or not.
88 	 */
89 	public bool isEnableTimestamp() {
90 		return this.enableTimestamp;
91 	}
92 
93 
94 	override
95 	public void initHeaders(MessageHeaderAccessor headerAccessor) {
96 		IdGenerator idGenerator = getIdGenerator();
97 		if (idGenerator !is null) {
98 			headerAccessor.setIdGenerator(idGenerator);
99 		}
100 		headerAccessor.setEnableTimestamp(isEnableTimestamp());
101 	}
102 
103 }