diff --git a/bootstrap-core/src/main/java/com/navercorp/pinpoint/bootstrap/plugin/jdbc/ArrayUtils.java b/bootstrap-core/src/main/java/com/navercorp/pinpoint/bootstrap/plugin/jdbc/ArrayUtils.java index 3245c45c0534..98ab65e89990 100644 --- a/bootstrap-core/src/main/java/com/navercorp/pinpoint/bootstrap/plugin/jdbc/ArrayUtils.java +++ b/bootstrap-core/src/main/java/com/navercorp/pinpoint/bootstrap/plugin/jdbc/ArrayUtils.java @@ -17,57 +17,29 @@ package com.navercorp.pinpoint.bootstrap.plugin.jdbc; /** - * @author emeroad + * @deprecated Since 1.6.1. Use {@link com.navercorp.pinpoint.common.util.ArrayUtils} + * @author Woonduk Kang(emeroad) */ +@Deprecated public final class ArrayUtils { private ArrayUtils() { } + /** + * @deprecated Since 1.6.1. Use {@link com.navercorp.pinpoint.common.util.ArrayUtils#abbreviate(byte[])} + */ + @Deprecated public static String dropToString(byte[] bytes) { - return dropToString(bytes, 32); + return com.navercorp.pinpoint.common.util.ArrayUtils.abbreviate(bytes); } - public static String dropToString(byte[] bytes, int limit) { - if (bytes == null) { - return "null"; - } - if (limit < 0) { - throw new IllegalArgumentException("negative limit:" + limit); - } - // TODO handle negative limit - - // Last valid index is length - 1 - int bytesMaxLength = bytes.length - 1; - final int maxLimit = limit - 1; - if (bytesMaxLength > maxLimit) { - bytesMaxLength = maxLimit; - } - if (bytesMaxLength == -1) { - if (bytes.length == 0) { - return "[]"; - } else { - return "[...(" + bytes.length + ")]"; - } - } - - - final StringBuilder sb = new StringBuilder(); - sb.append('['); - for (int i = 0; ; i++) { - sb.append(bytes[i]); - if (i == bytesMaxLength) { - if ((bytes.length - 1) <= maxLimit) { - return sb.append(']').toString(); - } else { - sb.append(", ...("); - sb.append(bytes.length - (i+1)); - sb.append(")]"); - return sb.toString(); - } - } - sb.append(", "); - } + /** + * @deprecated Since 1.6.1. Use {@link com.navercorp.pinpoint.common.util.ArrayUtils#abbreviate(byte[], int)} + */ + @Deprecated + public static String dropToString(byte[] bytes, int maxWidth) { + return com.navercorp.pinpoint.common.util.ArrayUtils.abbreviate(bytes, maxWidth); } diff --git a/bootstrap-core/src/main/java/com/navercorp/pinpoint/bootstrap/plugin/jdbc/bindvalue/BytesConverter.java b/bootstrap-core/src/main/java/com/navercorp/pinpoint/bootstrap/plugin/jdbc/bindvalue/BytesConverter.java index 9e81bf762183..c02eb8f29492 100644 --- a/bootstrap-core/src/main/java/com/navercorp/pinpoint/bootstrap/plugin/jdbc/bindvalue/BytesConverter.java +++ b/bootstrap-core/src/main/java/com/navercorp/pinpoint/bootstrap/plugin/jdbc/bindvalue/BytesConverter.java @@ -16,7 +16,7 @@ package com.navercorp.pinpoint.bootstrap.plugin.jdbc.bindvalue; -import com.navercorp.pinpoint.bootstrap.plugin.jdbc.ArrayUtils; +import com.navercorp.pinpoint.common.util.ArrayUtils; /** * @author emeroad @@ -28,11 +28,11 @@ public String convert(Object[] args) { return "null"; } if (args.length == 2) { - byte[] bytes = (byte[]) args[1]; + final byte[] bytes = (byte[]) args[1]; if (bytes == null) { return "null"; } else { - return ArrayUtils.dropToString(bytes); + return ArrayUtils.abbreviate(bytes); } } return "error"; diff --git a/bootstrap-core/src/main/java/com/navercorp/pinpoint/bootstrap/plugin/jdbc/bindvalue/ObjectConverter.java b/bootstrap-core/src/main/java/com/navercorp/pinpoint/bootstrap/plugin/jdbc/bindvalue/ObjectConverter.java index 1b6c00b7fd12..eb9961c6419b 100644 --- a/bootstrap-core/src/main/java/com/navercorp/pinpoint/bootstrap/plugin/jdbc/bindvalue/ObjectConverter.java +++ b/bootstrap-core/src/main/java/com/navercorp/pinpoint/bootstrap/plugin/jdbc/bindvalue/ObjectConverter.java @@ -22,8 +22,8 @@ import java.sql.Time; import java.sql.Timestamp; -import com.navercorp.pinpoint.bootstrap.plugin.jdbc.ArrayUtils; import com.navercorp.pinpoint.bootstrap.util.StringUtils; +import com.navercorp.pinpoint.common.util.ArrayUtils; /** * @author emeroad @@ -35,11 +35,11 @@ public String convert(Object[] args) { return "null"; } if (args.length == 2) { - Object param = args[1]; + final Object param = args[1]; return getParameter(param); } else if (args.length == 3) { - Object param = args[1]; + final Object param = args[1]; return getParameter(param); } return "error"; @@ -50,33 +50,33 @@ private String getParameter(Object param) { return "null"; } else { if (param instanceof Byte) { - return dropToString(param); + return abbreviate(param); } else if (param instanceof String) { - return dropToString(param); + return abbreviate(param); } else if (param instanceof BigDecimal) { - return dropToString(param); + return abbreviate(param); } else if (param instanceof Short) { - return dropToString(param); + return abbreviate(param); } else if (param instanceof Integer) { - return dropToString(param); + return abbreviate(param); } else if (param instanceof Long) { - return dropToString(param); + return abbreviate(param); } else if (param instanceof Float) { - return dropToString(param); + return abbreviate(param); } else if (param instanceof Double) { - return dropToString(param); + return abbreviate(param); } else if (param instanceof BigInteger) { - return dropToString(param); + return abbreviate(param); } else if (param instanceof java.sql.Date) { - return dropToString(param); + return abbreviate(param); } else if (param instanceof Time) { - return dropToString(param); + return abbreviate(param); } else if (param instanceof Timestamp) { - return dropToString(param); + return abbreviate(param); } else if (param instanceof Boolean) { - return dropToString(param); + return abbreviate(param); } else if (param instanceof byte[]) { - return ArrayUtils.dropToString((byte[]) param); + return ArrayUtils.abbreviate((byte[]) param); } else if (param instanceof InputStream) { return getClassName(param); } else if (param instanceof java.sql.Blob) { @@ -89,7 +89,7 @@ private String getParameter(Object param) { } } - private String dropToString(Object param) { + private String abbreviate(Object param) { return StringUtils.abbreviate(param.toString()); } diff --git a/bootstrap-core/src/test/java/com/navercorp/pinpoint/bootstrap/plugin/jdbc/ArrayUtilsTest.java b/bootstrap-core/src/test/java/com/navercorp/pinpoint/bootstrap/plugin/jdbc/ArrayUtilsTest.java deleted file mode 100644 index 1bbffe0efa14..000000000000 --- a/bootstrap-core/src/test/java/com/navercorp/pinpoint/bootstrap/plugin/jdbc/ArrayUtilsTest.java +++ /dev/null @@ -1,90 +0,0 @@ -/* - * Copyright 2014 NAVER Corp. - * - * Licensed 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 com.navercorp.pinpoint.bootstrap.plugin.jdbc; - -import org.junit.Assert; -import org.junit.Test; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import com.navercorp.pinpoint.bootstrap.plugin.jdbc.ArrayUtils; - -/** - * @author emeroad - */ -public class ArrayUtilsTest { - private final Logger logger = LoggerFactory.getLogger(ArrayUtilsTest.class.getName()); - - @Test - public void dropToStringSmall() { - byte[] bytes = new byte[] {1, 2, 3, 4}; - - String small = ArrayUtils.dropToString(bytes, 3); - Assert.assertEquals("[1, 2, 3, ...(1)]", small); - } - - @Test - public void dropToStringEqual() { - byte[] bytes = new byte[] {1, 2, 3, 4}; - - String equals = ArrayUtils.dropToString(bytes, 4); - Assert.assertEquals("[1, 2, 3, 4]", equals); - - } - - @Test - public void dropToStringLarge() { - byte[] bytes = new byte[] {1, 2, 3, 4}; - - String large = ArrayUtils.dropToString(bytes, 11); - Assert.assertEquals("[1, 2, 3, 4]", large); - - } - - - @Test - public void dropToStringOneAndZero() { - byte[] bytes = new byte[] {1, 2, 3, 4}; - - String one = ArrayUtils.dropToString(bytes, 1); - Assert.assertEquals("[1, ...(3)]", one); - - String zero = ArrayUtils.dropToString(bytes, 0); - Assert.assertEquals("[...(4)]", zero); - } - - - @Test - public void dropToStringSingle() { - byte[] bytes = new byte[] {1}; - - String small = ArrayUtils.dropToString(bytes, 1); - logger.info(small); - Assert.assertEquals("[1]", small); - } - - @Test - public void dropToStringNegative() { - byte[] bytes = new byte[] {1}; - - try { - ArrayUtils.dropToString(bytes, -1); - Assert.fail(); - } catch (Exception ignored) { - } - } -} diff --git a/commons/src/main/java/com/navercorp/pinpoint/common/util/ArrayUtils.java b/commons/src/main/java/com/navercorp/pinpoint/common/util/ArrayUtils.java new file mode 100644 index 000000000000..4c985208c513 --- /dev/null +++ b/commons/src/main/java/com/navercorp/pinpoint/common/util/ArrayUtils.java @@ -0,0 +1,75 @@ +/* + * Copyright 2016 NAVER Corp. + * + * Licensed 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 com.navercorp.pinpoint.common.util; + +/** + * @author Woonduk Kang(emeroad) + */ +public final class ArrayUtils { + + private static final int DEFAULT_ABBREVIATE_MAX_WIDTH = 32; + + private ArrayUtils() { + } + + public static String abbreviate(byte[] bytes) { + return abbreviate(bytes, DEFAULT_ABBREVIATE_MAX_WIDTH); + } + + public static String abbreviate(byte[] bytes, int maxWidth) { + if (bytes == null) { + return "null"; + } + if (maxWidth < 0) { + throw new IllegalArgumentException("negative maxWidth:" + maxWidth); + } + // TODO handle negative limit + + // Last valid index is length - 1 + int bytesMaxLength = bytes.length - 1; + final int maxLimit = maxWidth - 1; + if (bytesMaxLength > maxLimit) { + bytesMaxLength = maxLimit; + } + if (bytesMaxLength == -1) { + if (bytes.length == 0) { + return "[]"; + } else { + return "[...(" + bytes.length + ")]"; + } + } + + + final StringBuilder sb = new StringBuilder(); + sb.append('['); + for (int i = 0; ; i++) { + sb.append(bytes[i]); + if (i == bytesMaxLength) { + if ((bytes.length - 1) <= maxLimit) { + return sb.append(']').toString(); + } else { + sb.append(", ...("); + sb.append(bytes.length - (i+1)); + sb.append(")]"); + return sb.toString(); + } + } + sb.append(", "); + } + } +} diff --git a/commons/src/test/java/com/navercorp/pinpoint/common/util/ArrayUtilsTest.java b/commons/src/test/java/com/navercorp/pinpoint/common/util/ArrayUtilsTest.java new file mode 100644 index 000000000000..a2bf8fe2e9c5 --- /dev/null +++ b/commons/src/test/java/com/navercorp/pinpoint/common/util/ArrayUtilsTest.java @@ -0,0 +1,143 @@ +/* + * Copyright 2016 NAVER Corp. + * + * Licensed 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 com.navercorp.pinpoint.common.util; + +import org.junit.Assert; +import org.junit.Test; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * @author Woonduk Kang(emeroad) + */ +public class ArrayUtilsTest { + + private final Logger logger = LoggerFactory.getLogger(ArrayUtilsTest.class.getName()); + + @Test + public void abbreviateSmall() { + byte[] bytes = new byte[]{1, 2, 3, 4}; + + String small = ArrayUtils.abbreviate(bytes, 3); + Assert.assertEquals("[1, 2, 3, ...(1)]", small); + } + + @Test + public void abbreviateEqual() { + byte[] bytes = new byte[]{1, 2, 3, 4}; + + String equals = ArrayUtils.abbreviate(bytes, 4); + Assert.assertEquals("[1, 2, 3, 4]", equals); + + } + + @Test + public void abbreviateLarge() { + byte[] bytes = new byte[]{1, 2, 3, 4}; + + String large = ArrayUtils.abbreviate(bytes, 11); + Assert.assertEquals("[1, 2, 3, 4]", large); + + } + + + @Test + public void abbreviateOneAndZero() { + byte[] bytes = new byte[]{1, 2, 3, 4}; + + String one = ArrayUtils.abbreviate(bytes, 1); + Assert.assertEquals("[1, ...(3)]", one); + + String zero = ArrayUtils.abbreviate(bytes, 0); + Assert.assertEquals("[...(4)]", zero); + } + + + @Test + public void abbreviateSingle() { + byte[] bytes = new byte[]{1}; + + String small = ArrayUtils.abbreviate(bytes, 1); + logger.info(small); + Assert.assertEquals("[1]", small); + } + + @Test + public void abbreviateNegative() { + byte[] bytes = new byte[]{1}; + + try { + ArrayUtils.abbreviate(bytes, -1); + Assert.fail(); + } catch (Exception ignored) { + } + } + + @Test + public void abbreviate() { + //null test + Assert.assertTrue(ArrayUtils.abbreviate(null).equals("null")); + //zero-sized array test + byte[] bytes_zero = new byte[0]; + Assert.assertEquals("[]", ArrayUtils.abbreviate(bytes_zero)); + //small buffer with default limit + byte[] bytes_short = new byte[4]; + for (int i = 0; i < 4; i++) { + bytes_short[i] = 'A'; + } + Assert.assertEquals("[65, 65, 65, 65]", ArrayUtils.abbreviate(bytes_short)); + //big buffer with small limit + byte[] bytes = new byte[256]; + for (int i = 0; i < 4; i++) { + bytes[i] = 'A'; + } + for (int i = 4; i < 256; i++) { + bytes[i] = 'B'; + } + String answer = "["; + for (int i = 0; i < 4; i++) { + answer = answer + "65, "; + } + for (int i = 4; i < 16; i++) { + answer = answer + "66, "; + } + answer = answer + "...(240)]"; + Assert.assertEquals(answer, ArrayUtils.abbreviate(bytes, 16)); + //big buffer with big limit + answer = "["; + for (int i = 0; i < 4; i++) { + answer = answer + "65, "; + } + for (int i = 4; i < 255; i++) { + answer = answer + "66, "; + } + answer = answer + "66]"; + Assert.assertEquals(answer, ArrayUtils.abbreviate(bytes, 256)); + //big buffer with default limit + answer = "["; + for (int i = 0; i < 4; i++) { + answer = answer + "65, "; + } + for (int i = 4; i < 32; i++) { + answer = answer + "66, "; + } + answer = answer + "...(224)]"; + Assert.assertEquals(answer, ArrayUtils.abbreviate(bytes)); + } + +} \ No newline at end of file diff --git a/profiler/src/main/java/com/navercorp/pinpoint/profiler/util/ArrayUtils.java b/profiler/src/main/java/com/navercorp/pinpoint/profiler/util/ArrayUtils.java index c44819354355..0dd0fc8330a4 100644 --- a/profiler/src/main/java/com/navercorp/pinpoint/profiler/util/ArrayUtils.java +++ b/profiler/src/main/java/com/navercorp/pinpoint/profiler/util/ArrayUtils.java @@ -17,57 +17,29 @@ package com.navercorp.pinpoint.profiler.util; /** - * @author emeroad + * @deprecated Since 1.6.1. Use {@link com.navercorp.pinpoint.common.util.ArrayUtils} + * @author Woonduk Kang(emeroad) */ +@Deprecated public final class ArrayUtils { private ArrayUtils() { } + /** + * @deprecated Since 1.6.1. Use {@link com.navercorp.pinpoint.common.util.ArrayUtils#abbreviate(byte[])} + */ + @Deprecated public static String dropToString(byte[] bytes) { - return dropToString(bytes, 32); + return com.navercorp.pinpoint.common.util.ArrayUtils.abbreviate(bytes); } - public static String dropToString(byte[] bytes, int limit) { - if (bytes == null) { - return "null"; - } - if (limit < 0) { - throw new IllegalArgumentException("negative limit:" + limit); - } - // TODO handle negative limit - - // Last valid index is length - 1 - int bytesMaxLength = bytes.length - 1; - final int maxLimit = limit - 1; - if (bytesMaxLength > maxLimit) { - bytesMaxLength = maxLimit; - } - if (bytesMaxLength == -1) { - if (bytes.length == 0) { - return "[]"; - } else { - return "[...(" + bytes.length + ")]"; - } - } - - - final StringBuilder sb = new StringBuilder(); - sb.append('['); - for (int i = 0; ; i++) { - sb.append(bytes[i]); - if (i == bytesMaxLength) { - if ((bytes.length - 1) <= maxLimit) { - return sb.append(']').toString(); - } else { - sb.append(", ...("); - sb.append(bytes.length - (i+1)); - sb.append(")]"); - return sb.toString(); - } - } - sb.append(", "); - } + /** + * @deprecated Since 1.6.1. Use {@link com.navercorp.pinpoint.common.util.ArrayUtils#abbreviate(byte[], int)} + */ + @Deprecated + public static String dropToString(byte[] bytes, int maxWidth) { + return com.navercorp.pinpoint.common.util.ArrayUtils.abbreviate(bytes, maxWidth); } diff --git a/profiler/src/test/java/com/navercorp/pinpoint/profiler/util/ArrayUtilsTest.java b/profiler/src/test/java/com/navercorp/pinpoint/profiler/util/ArrayUtilsTest.java deleted file mode 100644 index 69af8ceb4547..000000000000 --- a/profiler/src/test/java/com/navercorp/pinpoint/profiler/util/ArrayUtilsTest.java +++ /dev/null @@ -1,69 +0,0 @@ -package com.navercorp.pinpoint.profiler.util; - -import org.junit.Assert; -import org.junit.Test; - -public class ArrayUtilsTest { - - @Test - public void testDropToString() { - //null test - Assert.assertTrue(ArrayUtils.dropToString(null).equals("null")); - //zero-sized array test - byte[] bytes_zero = new byte[0]; - Assert.assertEquals("[]", ArrayUtils.dropToString(bytes_zero)); - //small buffer with default limit - byte[] bytes_short = new byte[4]; - for(int i=0;i<4;i++) - { - bytes_short[i] = 'A'; - } - Assert.assertEquals("[65, 65, 65, 65]", ArrayUtils.dropToString(bytes_short)); - //big buffer with small limit - byte[] bytes = new byte[256]; - for(int i=0;i<4;i++) - { - bytes[i] = 'A'; - } - for(int i=4;i<256;i++) - { - bytes[i] = 'B'; - } - String answer = new String("["); - for(int i=0;i<4;i++) - { - answer = answer + "65, "; - } - for(int i=4;i<16;i++) - { - answer = answer + "66, "; - } - answer = answer + "...(240)]"; - Assert.assertEquals(answer, ArrayUtils.dropToString(bytes, 16)); - //big buffer with big limit - answer = "["; - for(int i=0;i<4;i++) - { - answer = answer + "65, "; - } - for(int i=4;i<255;i++) - { - answer = answer + "66, "; - } - answer = answer + "66]"; - Assert.assertEquals(answer, ArrayUtils.dropToString(bytes, 256)); - //big buffer with default limit - answer = "["; - for(int i=0;i<4;i++) - { - answer = answer + "65, "; - } - for(int i=4;i<32;i++) - { - answer = answer + "66, "; - } - answer = answer + "...(224)]"; - Assert.assertEquals(answer, ArrayUtils.dropToString(bytes)); - } - -}