View Javadoc
1   /*
2    * #%L
3    * wcm.io
4    * %%
5    * Copyright (C) 2015 wcm.io
6    * %%
7    * Licensed under the Apache License, Version 2.0 (the "License");
8    * you may not use this file except in compliance with the License.
9    * You may obtain a copy of the License at
10   *
11   *      http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing, software
14   * distributed under the License is distributed on an "AS IS" BASIS,
15   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   * See the License for the specific language governing permissions and
17   * limitations under the License.
18   * #L%
19   */
20  package io.wcm.dam.assetservice.impl.dataversion;
21  
22  import com.day.cq.dam.api.DamEvent;
23  
24  /**
25   * Simple strategy to generate data versions - on each DAM event a new timestamp is generated and
26   * returned as data version. Please be aware that this does not produce stable data versions
27   * across a cluster of AEM instances. It should only be used if there is only one AEM instance
28   * generating the data version, or some sort of long-stable stickyness is applied on the load balancer.
29   */
30  public class TimestampDataVersionStrategy extends DataVersionStrategy {
31  
32    /**
33     * Data version strategy id for configuration persistence.
34     */
35    public static final String STRATEGY = "timestamp";
36  
37    private volatile String dataVersion;
38  
39    /**
40     * Generate new data version on first instantiation.
41     * @param damPath DAM root path
42     */
43    public TimestampDataVersionStrategy(String damPath) {
44      super(damPath);
45      generateNewDataVersion();
46    }
47  
48    @Override
49    public void handleDamEvent(DamEvent damEvent) {
50      // generate a new data version on any DAM event affecting any of the configured paths
51      generateNewDataVersion();
52    }
53  
54    @Override
55    public String getDataVersion() {
56      return dataVersion;
57    }
58  
59    /**
60     * Generates a new data version based on current timestamp.
61     */
62    private void generateNewDataVersion() {
63      // use timestamp as data version. clashing of versions if two are generated at exactly the same time point
64      // is not the problem, because the data version can then be the same.
65      dataVersion = Long.toString(System.currentTimeMillis());
66      log.debug("{} - Generated new data version: {}", damPath, dataVersion);
67    }
68  
69  }