You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@brooklyn.apache.org by he...@apache.org on 2020/12/15 13:34:48 UTC

[brooklyn-server] branch master updated: change timestamp to be a timestamp class for better serialization support

This is an automated email from the ASF dual-hosted git repository.

heneveld pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/brooklyn-server.git


The following commit(s) were added to refs/heads/master by this push:
     new 6863296  change timestamp to be a timestamp class for better serialization support
6863296 is described below

commit 6863296c32227c2dc86ad641c8089748f1644e4c
Author: Alex Heneveld <al...@cloudsoftcorp.com>
AuthorDate: Tue Dec 15 12:55:24 2020 +0000

    change timestamp to be a timestamp class for better serialization support
---
 .../camp/brooklyn/ConfigParametersYamlTest.java    | 46 +++++++-----------
 .../core/flags/BrooklynTypeNameResolution.java     |  3 +-
 .../coerce/CommonAdaptorTypeCoercions.java         | 20 +++++---
 .../org/apache/brooklyn/util/time/Timestamp.java   | 55 ++++++++++++++++++++++
 4 files changed, 87 insertions(+), 37 deletions(-)

diff --git a/camp/camp-brooklyn/src/test/java/org/apache/brooklyn/camp/brooklyn/ConfigParametersYamlTest.java b/camp/camp-brooklyn/src/test/java/org/apache/brooklyn/camp/brooklyn/ConfigParametersYamlTest.java
index 49a451c..3d701f6 100644
--- a/camp/camp-brooklyn/src/test/java/org/apache/brooklyn/camp/brooklyn/ConfigParametersYamlTest.java
+++ b/camp/camp-brooklyn/src/test/java/org/apache/brooklyn/camp/brooklyn/ConfigParametersYamlTest.java
@@ -18,30 +18,18 @@
  */
 package org.apache.brooklyn.camp.brooklyn;
 
+import com.google.common.base.Joiner;
 import static com.google.common.base.Preconditions.checkNotNull;
+import com.google.common.base.Predicate;
+import com.google.common.base.Predicates;
+import com.google.common.collect.ImmutableList;
+import com.google.common.collect.ImmutableMap;
+import com.google.common.collect.ImmutableSet;
+import com.google.common.collect.Iterables;
 import com.google.common.reflect.TypeToken;
+import java.util.*;
 import java.util.function.BiConsumer;
-import java.util.function.BiFunction;
-import org.apache.brooklyn.core.effector.AddChildrenEffector;
-import org.apache.brooklyn.core.resolve.jackson.BeanWithTypePlanTransformer;
-import org.apache.brooklyn.core.resolve.jackson.BrooklynRegisteredTypeJacksonSerializationTest.SampleBean;
-import org.apache.brooklyn.core.typereg.BasicBrooklynTypeRegistry;
-import org.apache.brooklyn.core.typereg.BasicTypeImplementationPlan;
-import org.apache.brooklyn.core.typereg.RegisteredTypes;
-import org.apache.brooklyn.entity.stock.BasicEntity;
-import org.apache.brooklyn.util.collections.MutableMap;
-import static org.testng.Assert.assertEquals;
-import static org.testng.Assert.assertNotNull;
-import static org.testng.Assert.assertTrue;
-
-import java.util.Collection;
-import java.util.Collections;
-import java.util.Date;
-import java.util.List;
-import java.util.Map;
-import java.util.Set;
 import java.util.stream.Collectors;
-
 import org.apache.brooklyn.api.catalog.CatalogConfig;
 import org.apache.brooklyn.api.entity.Entity;
 import org.apache.brooklyn.api.entity.EntitySpec;
@@ -68,28 +56,26 @@ import org.apache.brooklyn.core.test.entity.TestEntityImpl;
 import org.apache.brooklyn.entity.software.base.EmptySoftwareProcess;
 import org.apache.brooklyn.entity.software.base.VanillaSoftwareProcess;
 import org.apache.brooklyn.entity.stock.BasicApplication;
+import org.apache.brooklyn.entity.stock.BasicEntity;
 import org.apache.brooklyn.location.ssh.SshMachineLocation;
 import org.apache.brooklyn.test.Asserts;
 import org.apache.brooklyn.util.collections.MutableList;
+import org.apache.brooklyn.util.collections.MutableMap;
 import org.apache.brooklyn.util.core.internal.ssh.ExecCmdAsserts;
 import org.apache.brooklyn.util.core.internal.ssh.RecordingSshTool;
 import org.apache.brooklyn.util.core.internal.ssh.RecordingSshTool.ExecCmd;
 import org.apache.brooklyn.util.time.Duration;
+import org.apache.brooklyn.util.time.Timestamp;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.testng.Assert;
+import static org.testng.Assert.assertEquals;
+import static org.testng.Assert.assertNotNull;
+import static org.testng.Assert.assertTrue;
 import org.testng.annotations.AfterMethod;
 import org.testng.annotations.BeforeMethod;
 import org.testng.annotations.Test;
 
-import com.google.common.base.Joiner;
-import com.google.common.base.Predicate;
-import com.google.common.base.Predicates;
-import com.google.common.collect.ImmutableList;
-import com.google.common.collect.ImmutableMap;
-import com.google.common.collect.ImmutableSet;
-import com.google.common.collect.Iterables;
-
 public class ConfigParametersYamlTest extends AbstractYamlRebindTest {
 	
     private static final Logger LOG = LoggerFactory.getLogger(ConfigParametersYamlTest.class);
@@ -812,8 +798,8 @@ public class ConfigParametersYamlTest extends AbstractYamlRebindTest {
                 .put("String", String.class)
                 .put("duration", Duration.class)
                 .put("Duration", Duration.class)
-                .put("timestamp", Date.class)
-                .put("Timestamp", Date.class)
+                .put("timestamp", Timestamp.class)
+                .put("Timestamp", Timestamp.class)
                 .put("port", PortRange.class)
                 .put("Port", PortRange.class)
                 .build();
diff --git a/core/src/main/java/org/apache/brooklyn/util/core/flags/BrooklynTypeNameResolution.java b/core/src/main/java/org/apache/brooklyn/util/core/flags/BrooklynTypeNameResolution.java
index a69601c..066c454 100644
--- a/core/src/main/java/org/apache/brooklyn/util/core/flags/BrooklynTypeNameResolution.java
+++ b/core/src/main/java/org/apache/brooklyn/util/core/flags/BrooklynTypeNameResolution.java
@@ -40,6 +40,7 @@ import org.apache.brooklyn.util.collections.MutableSet;
 import org.apache.brooklyn.util.guava.Maybe;
 import org.apache.brooklyn.util.text.Strings;
 import org.apache.brooklyn.util.time.Duration;
+import org.apache.brooklyn.util.time.Timestamp;
 import org.apache.commons.lang3.reflect.TypeUtils;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
@@ -70,7 +71,7 @@ public class BrooklynTypeNameResolution {
             .put("wrappedvalue", WrappedValue.class)
 
             .put("duration", Duration.class)
-            .put("timestamp", Date.class)
+            .put("timestamp", Timestamp.class)
             .put("port", PortRange.class)
             .build();
 
diff --git a/utils/common/src/main/java/org/apache/brooklyn/util/javalang/coerce/CommonAdaptorTypeCoercions.java b/utils/common/src/main/java/org/apache/brooklyn/util/javalang/coerce/CommonAdaptorTypeCoercions.java
index ede2afb..dafd705 100644
--- a/utils/common/src/main/java/org/apache/brooklyn/util/javalang/coerce/CommonAdaptorTypeCoercions.java
+++ b/utils/common/src/main/java/org/apache/brooklyn/util/javalang/coerce/CommonAdaptorTypeCoercions.java
@@ -23,12 +23,7 @@ import java.math.BigInteger;
 import java.net.InetAddress;
 import java.net.URI;
 import java.net.URL;
-import java.util.Collection;
-import java.util.Date;
-import java.util.List;
-import java.util.Map;
-import java.util.Set;
-import java.util.TimeZone;
+import java.util.*;
 import java.util.concurrent.atomic.AtomicInteger;
 import java.util.concurrent.atomic.AtomicLong;
 
@@ -48,6 +43,7 @@ import org.apache.brooklyn.util.text.StringEscapes.JavaStringEscapes;
 import org.apache.brooklyn.util.text.Strings;
 import org.apache.brooklyn.util.time.Duration;
 import org.apache.brooklyn.util.time.Time;
+import org.apache.brooklyn.util.time.Timestamp;
 import org.apache.brooklyn.util.yaml.Yamls;
 
 import com.google.common.base.Function;
@@ -259,6 +255,18 @@ public class CommonAdaptorTypeCoercions {
                 return Time.parseDate(input);
             }
         });
+        registerAdapter(String.class, Timestamp.class, new Function<String,Timestamp>() {
+            @Override
+            public Timestamp apply(final String input) {
+                return new Timestamp(input);
+            }
+        });
+        registerAdapter(Date.class, Timestamp.class, new Function<Date,Timestamp>() {
+            @Override
+            public Timestamp apply(final Date input) {
+                return new Timestamp(input);
+            }
+        });
         registerAdapter(String.class, QuorumCheck.class, new Function<String,QuorumCheck>() {
             @Override
             public QuorumCheck apply(final String input) {
diff --git a/utils/common/src/main/java/org/apache/brooklyn/util/time/Timestamp.java b/utils/common/src/main/java/org/apache/brooklyn/util/time/Timestamp.java
new file mode 100644
index 0000000..4ba3e33
--- /dev/null
+++ b/utils/common/src/main/java/org/apache/brooklyn/util/time/Timestamp.java
@@ -0,0 +1,55 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.brooklyn.util.time;
+
+import java.util.Calendar;
+import java.util.Date;
+
+/** This wrapper around Date making it better suited to serialization etc */
+public class Timestamp extends Date {
+
+    public Timestamp(Date d) { super(d.getTime()); }
+    public Timestamp(Calendar d) { this(d.getTime()); }
+    /** takes milliseconds since epoch */
+    public Timestamp(long l) {
+        this(new Calendar.Builder().setInstant(l).build());
+    }
+    /** parses many common formats, as per {@link Time#parseCalendar(String)} */
+    public Timestamp(String s) {
+        this(Time.parseCalendar(s));
+    }
+
+
+    /** json constructor */
+    private Timestamp() {
+        this(Calendar.getInstance());
+    }
+
+    public static Timestamp fromString(String s) { return new Timestamp(s); }
+
+    public Calendar toCalendar() {
+        return new Calendar.Builder().setInstant(this).build();
+    }
+
+    @Override
+    public String toString() {
+        return Time.makeDateString(this);
+    }
+
+}