diff --git a/shield/src/main/java/org/elasticsearch/shield/authc/activedirectory/ActiveDirectoryGroupsResolver.java b/shield/src/main/java/org/elasticsearch/shield/authc/activedirectory/ActiveDirectoryGroupsResolver.java index 17f2721ebf0..18aad11543b 100644 --- a/shield/src/main/java/org/elasticsearch/shield/authc/activedirectory/ActiveDirectoryGroupsResolver.java +++ b/shield/src/main/java/org/elasticsearch/shield/authc/activedirectory/ActiveDirectoryGroupsResolver.java @@ -5,12 +5,12 @@ */ package org.elasticsearch.shield.authc.activedirectory; -import com.google.common.primitives.Ints; import com.unboundid.ldap.sdk.*; import org.elasticsearch.common.Strings; import org.elasticsearch.common.logging.ESLogger; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.unit.TimeValue; +import org.elasticsearch.common.util.primitives.Integers; import org.elasticsearch.shield.authc.ldap.support.LdapSearchScope; import org.elasticsearch.shield.authc.ldap.support.LdapSession.GroupsResolver; import org.elasticsearch.shield.support.Exceptions; @@ -38,7 +38,7 @@ public class ActiveDirectoryGroupsResolver implements GroupsResolver { logger.debug("group SID to DN search filter: [{}]", groupSearchFilter); SearchRequest searchRequest = new SearchRequest(baseDn, scope.scope(), groupSearchFilter, Strings.EMPTY_ARRAY); - searchRequest.setTimeLimitSeconds(Ints.checkedCast(timeout.seconds())); + searchRequest.setTimeLimitSeconds(Integers.checkedCast(timeout.seconds())); SearchResult results; try { results = search(connection, searchRequest, logger); @@ -59,7 +59,7 @@ public class ActiveDirectoryGroupsResolver implements GroupsResolver { static Filter buildGroupQuery(LDAPInterface connection, String userDn, TimeValue timeout, ESLogger logger) { try { SearchRequest request = new SearchRequest(userDn, SearchScope.BASE, OBJECT_CLASS_PRESENCE_FILTER, "tokenGroups"); - request.setTimeLimitSeconds(Ints.checkedCast(timeout.seconds())); + request.setTimeLimitSeconds(Integers.checkedCast(timeout.seconds())); SearchResultEntry entry = searchForEntry(connection, request, logger); Attribute attribute = entry.getAttribute("tokenGroups"); byte[][] tokenGroupSIDBytes = attribute.getValueByteArrays(); diff --git a/shield/src/main/java/org/elasticsearch/shield/authc/activedirectory/ActiveDirectorySessionFactory.java b/shield/src/main/java/org/elasticsearch/shield/authc/activedirectory/ActiveDirectorySessionFactory.java index a7a37ad5296..5c0348f3c3e 100644 --- a/shield/src/main/java/org/elasticsearch/shield/authc/activedirectory/ActiveDirectorySessionFactory.java +++ b/shield/src/main/java/org/elasticsearch/shield/authc/activedirectory/ActiveDirectorySessionFactory.java @@ -5,10 +5,10 @@ */ package org.elasticsearch.shield.authc.activedirectory; -import com.google.common.primitives.Ints; import com.unboundid.ldap.sdk.*; import org.elasticsearch.common.Strings; import org.elasticsearch.common.settings.Settings; +import org.elasticsearch.common.util.primitives.Integers; import org.elasticsearch.shield.ShieldSettingsFilter; import org.elasticsearch.shield.authc.RealmConfig; import org.elasticsearch.shield.authc.ldap.support.LdapSearchScope; @@ -107,7 +107,7 @@ public class ActiveDirectorySessionFactory extends SessionFactory { try { connection.bind(userPrincipal, new String(password.internalChars())); SearchRequest searchRequest = new SearchRequest(userSearchDN, userSearchScope.scope(), createFilter(userSearchFilter, userName), Strings.EMPTY_ARRAY); - searchRequest.setTimeLimitSeconds(Ints.checkedCast(timeout.seconds())); + searchRequest.setTimeLimitSeconds(Integers.checkedCast(timeout.seconds())); SearchResult results = search(connection, searchRequest, logger); int numResults = results.getEntryCount(); if (numResults > 1) { diff --git a/shield/src/main/java/org/elasticsearch/shield/authc/ldap/LdapUserSearchSessionFactory.java b/shield/src/main/java/org/elasticsearch/shield/authc/ldap/LdapUserSearchSessionFactory.java index 13b155b1a46..45f3fe1c310 100644 --- a/shield/src/main/java/org/elasticsearch/shield/authc/ldap/LdapUserSearchSessionFactory.java +++ b/shield/src/main/java/org/elasticsearch/shield/authc/ldap/LdapUserSearchSessionFactory.java @@ -5,12 +5,12 @@ */ package org.elasticsearch.shield.authc.ldap; -import com.google.common.primitives.Ints; import com.unboundid.ldap.sdk.*; import org.elasticsearch.common.Strings; import org.elasticsearch.common.logging.ESLogger; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.unit.TimeValue; +import org.elasticsearch.common.util.primitives.Integers; import org.elasticsearch.shield.ShieldSettingsFilter; import org.elasticsearch.shield.authc.RealmConfig; import org.elasticsearch.shield.authc.ldap.support.LdapSearchScope; @@ -170,7 +170,7 @@ public class LdapUserSearchSessionFactory extends SessionFactory { private String findUserDN(String user) throws Exception { SearchRequest request = new SearchRequest(userSearchBaseDn, scope.scope(), createEqualityFilter(userAttribute, encodeValue(user)), Strings.EMPTY_ARRAY); - request.setTimeLimitSeconds(Ints.checkedCast(timeout.seconds())); + request.setTimeLimitSeconds(Integers.checkedCast(timeout.seconds())); LDAPConnectionPool connectionPool = connectionPool(); SearchResultEntry entry = searchForEntry(connectionPool, request, logger); if (entry == null) { diff --git a/shield/src/main/java/org/elasticsearch/shield/authc/ldap/SearchGroupsResolver.java b/shield/src/main/java/org/elasticsearch/shield/authc/ldap/SearchGroupsResolver.java index eec8e5d620a..917b2c59e75 100644 --- a/shield/src/main/java/org/elasticsearch/shield/authc/ldap/SearchGroupsResolver.java +++ b/shield/src/main/java/org/elasticsearch/shield/authc/ldap/SearchGroupsResolver.java @@ -5,12 +5,12 @@ */ package org.elasticsearch.shield.authc.ldap; -import com.google.common.primitives.Ints; import com.unboundid.ldap.sdk.*; import org.elasticsearch.common.Strings; import org.elasticsearch.common.logging.ESLogger; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.unit.TimeValue; +import org.elasticsearch.common.util.primitives.Integers; import org.elasticsearch.shield.authc.ldap.support.LdapSearchScope; import org.elasticsearch.shield.authc.ldap.support.LdapSession.GroupsResolver; import org.elasticsearch.shield.support.Exceptions; @@ -51,7 +51,7 @@ class SearchGroupsResolver implements GroupsResolver { String userId = userAttribute != null ? readUserAttribute(connection, userDn, timeout, logger) : userDn; try { SearchRequest searchRequest = new SearchRequest(baseDn, scope.scope(), createFilter(filter, userId), Strings.EMPTY_ARRAY); - searchRequest.setTimeLimitSeconds(Ints.checkedCast(timeout.seconds())); + searchRequest.setTimeLimitSeconds(Integers.checkedCast(timeout.seconds())); SearchResult results = search(connection, searchRequest, logger); for (SearchResultEntry entry : results.getSearchEntries()) { groups.add(entry.getDN()); @@ -66,7 +66,7 @@ class SearchGroupsResolver implements GroupsResolver { String readUserAttribute(LDAPInterface connection, String userDn, TimeValue timeout, ESLogger logger) { try { SearchRequest request = new SearchRequest(userDn, SearchScope.BASE, OBJECT_CLASS_PRESENCE_FILTER, userAttribute); - request.setTimeLimitSeconds(Ints.checkedCast(timeout.seconds())); + request.setTimeLimitSeconds(Integers.checkedCast(timeout.seconds())); SearchResultEntry results = searchForEntry(connection, request, logger); Attribute attribute = results.getAttribute(userAttribute); if (attribute == null) { diff --git a/shield/src/main/java/org/elasticsearch/shield/authc/ldap/UserAttributeGroupsResolver.java b/shield/src/main/java/org/elasticsearch/shield/authc/ldap/UserAttributeGroupsResolver.java index 07f9e4e41f5..5f17632f356 100644 --- a/shield/src/main/java/org/elasticsearch/shield/authc/ldap/UserAttributeGroupsResolver.java +++ b/shield/src/main/java/org/elasticsearch/shield/authc/ldap/UserAttributeGroupsResolver.java @@ -5,12 +5,12 @@ */ package org.elasticsearch.shield.authc.ldap; -import com.google.common.primitives.Ints; import com.unboundid.ldap.sdk.*; import org.elasticsearch.ElasticsearchException; import org.elasticsearch.common.logging.ESLogger; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.unit.TimeValue; +import org.elasticsearch.common.util.primitives.Integers; import org.elasticsearch.shield.authc.ldap.support.LdapSession.GroupsResolver; import java.util.Arrays; @@ -39,7 +39,7 @@ class UserAttributeGroupsResolver implements GroupsResolver { public List resolve(LDAPInterface connection, String userDn, TimeValue timeout, ESLogger logger) { try { SearchRequest request = new SearchRequest(userDn, SearchScope.BASE, OBJECT_CLASS_PRESENCE_FILTER, attribute); - request.setTimeLimitSeconds(Ints.checkedCast(timeout.seconds())); + request.setTimeLimitSeconds(Integers.checkedCast(timeout.seconds())); SearchResultEntry result = searchForEntry(connection, request, logger); Attribute attributeReturned = result.getAttribute(attribute); if (attributeReturned == null) { diff --git a/shield/src/main/java/org/elasticsearch/shield/authc/ldap/support/SessionFactory.java b/shield/src/main/java/org/elasticsearch/shield/authc/ldap/support/SessionFactory.java index f575cb444cc..64f1e002187 100644 --- a/shield/src/main/java/org/elasticsearch/shield/authc/ldap/support/SessionFactory.java +++ b/shield/src/main/java/org/elasticsearch/shield/authc/ldap/support/SessionFactory.java @@ -5,7 +5,6 @@ */ package org.elasticsearch.shield.authc.ldap.support; -import com.google.common.primitives.Ints; import com.unboundid.ldap.sdk.LDAPConnectionOptions; import com.unboundid.ldap.sdk.LDAPException; import com.unboundid.ldap.sdk.LDAPURL; @@ -14,6 +13,7 @@ import org.elasticsearch.common.Strings; import org.elasticsearch.common.logging.ESLogger; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.unit.TimeValue; +import org.elasticsearch.common.util.primitives.Integers; import org.elasticsearch.shield.authc.RealmConfig; import org.elasticsearch.shield.authc.support.SecuredString; @@ -94,7 +94,7 @@ public abstract class SessionFactory { protected static LDAPConnectionOptions connectionOptions(Settings settings) { LDAPConnectionOptions options = new LDAPConnectionOptions(); - options.setConnectTimeoutMillis(Ints.checkedCast(settings.getAsTime(TIMEOUT_TCP_CONNECTION_SETTING, TIMEOUT_DEFAULT).millis())); + options.setConnectTimeoutMillis(Integers.checkedCast(settings.getAsTime(TIMEOUT_TCP_CONNECTION_SETTING, TIMEOUT_DEFAULT).millis())); options.setFollowReferrals(settings.getAsBoolean(FOLLOW_REFERRALS_SETTING, true)); options.setResponseTimeoutMillis(settings.getAsTime(TIMEOUT_TCP_READ_SETTING, TIMEOUT_DEFAULT).millis()); options.setAutoReconnect(true); diff --git a/shield/src/main/java/org/elasticsearch/shield/ssl/AbstractSSLService.java b/shield/src/main/java/org/elasticsearch/shield/ssl/AbstractSSLService.java index a6d3d4d5f0e..67f52ce2c01 100644 --- a/shield/src/main/java/org/elasticsearch/shield/ssl/AbstractSSLService.java +++ b/shield/src/main/java/org/elasticsearch/shield/ssl/AbstractSSLService.java @@ -5,13 +5,13 @@ */ package org.elasticsearch.shield.ssl; -import com.google.common.primitives.Ints; import org.elasticsearch.ElasticsearchException; import org.elasticsearch.common.component.AbstractComponent; import org.elasticsearch.common.logging.ESLogger; import org.elasticsearch.common.logging.Loggers; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.unit.TimeValue; +import org.elasticsearch.common.util.primitives.Integers; import org.elasticsearch.env.Environment; import javax.net.ssl.*; @@ -207,7 +207,7 @@ public abstract class AbstractSSLService extends AbstractComponent { SSLContext sslContext = SSLContext.getInstance(sslProtocol); sslContext.init(keyManagers, trustManagers, null); sslContext.getServerSessionContext().setSessionCacheSize(sessionCacheSize); - sslContext.getServerSessionContext().setSessionTimeout(Ints.checkedCast(sessionCacheTimeout.seconds())); + sslContext.getServerSessionContext().setSessionTimeout(Integers.checkedCast(sessionCacheTimeout.seconds())); return sslContext; } catch (Exception e) { throw new ElasticsearchException("failed to initialize the SSLContext", e); diff --git a/shield/src/test/java/org/elasticsearch/shield/authc/activedirectory/ActiveDirectoryGroupsResolverTests.java b/shield/src/test/java/org/elasticsearch/shield/authc/activedirectory/ActiveDirectoryGroupsResolverTests.java index dfadbdb270b..74c58abe94f 100644 --- a/shield/src/test/java/org/elasticsearch/shield/authc/activedirectory/ActiveDirectoryGroupsResolverTests.java +++ b/shield/src/test/java/org/elasticsearch/shield/authc/activedirectory/ActiveDirectoryGroupsResolverTests.java @@ -5,13 +5,13 @@ */ package org.elasticsearch.shield.authc.activedirectory; -import com.google.common.primitives.Ints; import com.unboundid.ldap.sdk.Filter; import com.unboundid.ldap.sdk.LDAPConnection; import com.unboundid.ldap.sdk.LDAPConnectionOptions; import com.unboundid.ldap.sdk.LDAPURL; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.unit.TimeValue; +import org.elasticsearch.common.util.primitives.Integers; import org.elasticsearch.env.Environment; import org.elasticsearch.shield.authc.ldap.support.LdapSearchScope; import org.elasticsearch.shield.authc.ldap.support.SessionFactory; @@ -51,7 +51,7 @@ public class ActiveDirectoryGroupsResolverTests extends ESTestCase { options.setFollowReferrals(true); options.setAutoReconnect(true); options.setAllowConcurrentSocketFactoryUse(true); - options.setConnectTimeoutMillis(Ints.checkedCast(SessionFactory.TIMEOUT_DEFAULT.millis())); + options.setConnectTimeoutMillis(Integers.checkedCast(SessionFactory.TIMEOUT_DEFAULT.millis())); options.setResponseTimeoutMillis(SessionFactory.TIMEOUT_DEFAULT.millis()); ldapConnection = new LDAPConnection(clientSSLService.sslSocketFactory(), options, ldapurl.getHost(), ldapurl.getPort(), BRUCE_BANNER_DN, ActiveDirectorySessionFactoryTests.PASSWORD); } diff --git a/shield/src/test/java/org/elasticsearch/shield/authc/ldap/SearchGroupsResolverTests.java b/shield/src/test/java/org/elasticsearch/shield/authc/ldap/SearchGroupsResolverTests.java index 08b2a515b4a..31a69803ce7 100644 --- a/shield/src/test/java/org/elasticsearch/shield/authc/ldap/SearchGroupsResolverTests.java +++ b/shield/src/test/java/org/elasticsearch/shield/authc/ldap/SearchGroupsResolverTests.java @@ -5,13 +5,13 @@ */ package org.elasticsearch.shield.authc.ldap; -import com.google.common.primitives.Ints; import com.unboundid.ldap.sdk.LDAPConnection; import com.unboundid.ldap.sdk.LDAPConnectionOptions; import com.unboundid.ldap.sdk.LDAPURL; import org.elasticsearch.ElasticsearchSecurityException; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.unit.TimeValue; +import org.elasticsearch.common.util.primitives.Integers; import org.elasticsearch.env.Environment; import org.elasticsearch.shield.authc.ldap.support.LdapSearchScope; import org.elasticsearch.shield.authc.ldap.support.SessionFactory; @@ -51,7 +51,7 @@ public class SearchGroupsResolverTests extends ESTestCase { options.setFollowReferrals(true); options.setAutoReconnect(true); options.setAllowConcurrentSocketFactoryUse(true); - options.setConnectTimeoutMillis(Ints.checkedCast(SessionFactory.TIMEOUT_DEFAULT.millis())); + options.setConnectTimeoutMillis(Integers.checkedCast(SessionFactory.TIMEOUT_DEFAULT.millis())); options.setResponseTimeoutMillis(SessionFactory.TIMEOUT_DEFAULT.millis()); ldapConnection = new LDAPConnection(clientSSLService.sslSocketFactory(), options, ldapurl.getHost(), ldapurl.getPort(), BRUCE_BANNER_DN, OpenLdapTests.PASSWORD); } diff --git a/shield/src/test/java/org/elasticsearch/shield/authc/ldap/UserAttributeGroupsResolverTests.java b/shield/src/test/java/org/elasticsearch/shield/authc/ldap/UserAttributeGroupsResolverTests.java index 7cf803eda47..24945dcbe46 100644 --- a/shield/src/test/java/org/elasticsearch/shield/authc/ldap/UserAttributeGroupsResolverTests.java +++ b/shield/src/test/java/org/elasticsearch/shield/authc/ldap/UserAttributeGroupsResolverTests.java @@ -5,12 +5,12 @@ */ package org.elasticsearch.shield.authc.ldap; -import com.google.common.primitives.Ints; import com.unboundid.ldap.sdk.LDAPConnection; import com.unboundid.ldap.sdk.LDAPConnectionOptions; import com.unboundid.ldap.sdk.LDAPURL; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.unit.TimeValue; +import org.elasticsearch.common.util.primitives.Integers; import org.elasticsearch.env.Environment; import org.elasticsearch.shield.authc.activedirectory.ActiveDirectorySessionFactoryTests; import org.elasticsearch.shield.authc.ldap.support.SessionFactory; @@ -49,7 +49,7 @@ public class UserAttributeGroupsResolverTests extends ESTestCase { options.setFollowReferrals(true); options.setAutoReconnect(true); options.setAllowConcurrentSocketFactoryUse(true); - options.setConnectTimeoutMillis(Ints.checkedCast(SessionFactory.TIMEOUT_DEFAULT.millis())); + options.setConnectTimeoutMillis(Integers.checkedCast(SessionFactory.TIMEOUT_DEFAULT.millis())); options.setResponseTimeoutMillis(SessionFactory.TIMEOUT_DEFAULT.millis()); ldapConnection = new LDAPConnection(clientSSLService.sslSocketFactory(), options, ldapurl.getHost(), ldapurl.getPort(), BRUCE_BANNER_DN, ActiveDirectorySessionFactoryTests.PASSWORD); } diff --git a/watcher/src/main/java/org/elasticsearch/watcher/support/Integers.java b/watcher/src/main/java/org/elasticsearch/watcher/support/Integers.java new file mode 100644 index 00000000000..88b4bf98eba --- /dev/null +++ b/watcher/src/main/java/org/elasticsearch/watcher/support/Integers.java @@ -0,0 +1,40 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License; + * you may not use this file except in compliance with the Elastic License. + */ +package org.elasticsearch.watcher.support; + +import java.util.Arrays; +import java.util.Iterator; +import java.util.NoSuchElementException; +import java.util.Objects; + +public class Integers { + private Integers() { + } + + public static Iterable asIterable(int[] values) { + Objects.requireNonNull(values); + return () -> new Iterator() { + private int position = 0; + @Override + public boolean hasNext() { + return position < values.length; + } + + @Override + public Integer next() { + if (position < values.length) { + return values[position++]; + } else { + throw new NoSuchElementException("position: " + position + ", length: " + values.length); + } + } + }; + } + + public static boolean contains(int[] values, final int value) { + return Arrays.stream(values).anyMatch(v -> v == value); + } +} diff --git a/watcher/src/main/java/org/elasticsearch/watcher/support/Strings.java b/watcher/src/main/java/org/elasticsearch/watcher/support/Strings.java new file mode 100644 index 00000000000..90e5cf348f0 --- /dev/null +++ b/watcher/src/main/java/org/elasticsearch/watcher/support/Strings.java @@ -0,0 +1,29 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License; + * you may not use this file except in compliance with the Elastic License. + */ +package org.elasticsearch.watcher.support; + +import java.util.Objects; + +public class Strings { + private Strings() { + } + + public static String join(String delimiter, int... values) { + Objects.requireNonNull(delimiter); + Objects.requireNonNull(values); + if (values.length == 0) { + return ""; + } + StringBuilder sb = new StringBuilder(4 * values.length); + sb.append(values[0]); + + for (int i = 1; i < values.length; i++) { + sb.append(delimiter).append(values[i]); + } + + return sb.toString(); + } +} diff --git a/watcher/src/main/java/org/elasticsearch/watcher/trigger/schedule/HourlySchedule.java b/watcher/src/main/java/org/elasticsearch/watcher/trigger/schedule/HourlySchedule.java index 012ca776d2e..e523c3bbee6 100644 --- a/watcher/src/main/java/org/elasticsearch/watcher/trigger/schedule/HourlySchedule.java +++ b/watcher/src/main/java/org/elasticsearch/watcher/trigger/schedule/HourlySchedule.java @@ -5,10 +5,10 @@ */ package org.elasticsearch.watcher.trigger.schedule; -import com.google.common.primitives.Ints; import org.elasticsearch.ElasticsearchParseException; import org.elasticsearch.common.ParseField; import org.elasticsearch.common.ParseFieldMatcher; +import org.elasticsearch.common.util.primitives.Integers; import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.common.xcontent.XContentParser; import org.elasticsearch.watcher.trigger.schedule.support.DayTimes; @@ -128,7 +128,7 @@ public class HourlySchedule extends CronnableSchedule { } } - return minutes.isEmpty() ? new HourlySchedule() : new HourlySchedule(Ints.toArray(minutes)); + return minutes.isEmpty() ? new HourlySchedule() : new HourlySchedule(Integers.toArray(minutes)); } } @@ -148,7 +148,7 @@ public class HourlySchedule extends CronnableSchedule { } public HourlySchedule build() { - return minutes.isEmpty() ? new HourlySchedule() : new HourlySchedule(Ints.toArray(minutes)); + return minutes.isEmpty() ? new HourlySchedule() : new HourlySchedule(Integers.toArray(minutes)); } } } diff --git a/watcher/src/main/java/org/elasticsearch/watcher/trigger/schedule/support/DayTimes.java b/watcher/src/main/java/org/elasticsearch/watcher/trigger/schedule/support/DayTimes.java index 066eb5f0ff3..558068d33ee 100644 --- a/watcher/src/main/java/org/elasticsearch/watcher/trigger/schedule/support/DayTimes.java +++ b/watcher/src/main/java/org/elasticsearch/watcher/trigger/schedule/support/DayTimes.java @@ -5,9 +5,9 @@ */ package org.elasticsearch.watcher.trigger.schedule.support; -import com.google.common.primitives.Ints; import org.elasticsearch.ElasticsearchParseException; import org.elasticsearch.common.ParseFieldMatcher; +import org.elasticsearch.common.util.primitives.Integers; import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.common.xcontent.XContentParser; @@ -17,6 +17,7 @@ import java.util.Arrays; import java.util.List; import static org.elasticsearch.watcher.support.Exceptions.illegalArgument; +import static org.elasticsearch.watcher.support.Strings.join; /** * @@ -124,8 +125,8 @@ public class DayTimes implements Times { } public String cron() { - String hrs = Ints.join(",", hour); - String mins = Ints.join(",", minute); + String hrs = join(",", hour); + String mins = join(",", minute); return "0 " + mins + " " + hrs + " * * ?"; } @@ -225,7 +226,7 @@ public class DayTimes implements Times { if (minutes.isEmpty()) { minutes.add(0); } - return new DayTimes(Ints.toArray(hours), Ints.toArray(minutes)); + return new DayTimes(Integers.toArray(hours), Integers.toArray(minutes)); } public static int parseHourValue(XContentParser parser, XContentParser.Token token) throws IOException, ElasticsearchParseException { diff --git a/watcher/src/main/java/org/elasticsearch/watcher/trigger/schedule/support/MonthTimes.java b/watcher/src/main/java/org/elasticsearch/watcher/trigger/schedule/support/MonthTimes.java index ac0da04650f..359df182181 100644 --- a/watcher/src/main/java/org/elasticsearch/watcher/trigger/schedule/support/MonthTimes.java +++ b/watcher/src/main/java/org/elasticsearch/watcher/trigger/schedule/support/MonthTimes.java @@ -6,10 +6,10 @@ package org.elasticsearch.watcher.trigger.schedule.support; import com.google.common.collect.ImmutableSet; -import com.google.common.primitives.Ints; import org.elasticsearch.ElasticsearchParseException; import org.elasticsearch.common.ParseFieldMatcher; import org.elasticsearch.common.Strings; +import org.elasticsearch.common.util.primitives.Integers; import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.common.xcontent.XContentParser; @@ -20,6 +20,7 @@ import java.util.Locale; import java.util.Set; import static org.elasticsearch.watcher.support.Exceptions.illegalArgument; +import static org.elasticsearch.watcher.support.Strings.join; /** * @@ -68,9 +69,9 @@ public class MonthTimes implements Times { public Set crons() { Set crons = new HashSet<>(); for (DayTimes times : this.times) { - String hrsStr = Ints.join(",", times.hour); - String minsStr = Ints.join(",", times.minute); - String daysStr = Ints.join(",", this.days); + String hrsStr = join(",", times.hour); + String minsStr = join(",", times.minute); + String daysStr = join(",", this.days); daysStr = daysStr.replace("32", "L"); crons.add("0 " + minsStr + " " + hrsStr + " " + daysStr + " * ?"); } @@ -103,7 +104,7 @@ public class MonthTimes implements Times { return String.format( Locale.ROOT, "days [%s], times [%s]", - Ints.join(",", days), + join(",", days), Strings.arrayToCommaDelimitedString(times) ); } @@ -185,7 +186,7 @@ public class MonthTimes implements Times { } } } - int[] days = daysSet.isEmpty() ? DEFAULT_DAYS : Ints.toArray(daysSet); + int[] days = daysSet.isEmpty() ? DEFAULT_DAYS : Integers.toArray(daysSet); DayTimes[] times = timesSet.isEmpty() ? new DayTimes[] { new DayTimes(0, 0) } : timesSet.toArray(new DayTimes[timesSet.size()]); return new MonthTimes(days, times); } @@ -220,7 +221,7 @@ public class MonthTimes implements Times { } public Builder on(int... days) { - this.days.addAll(Ints.asList(days)); + Arrays.stream(days).forEach(this.days::add); return this; } @@ -245,7 +246,7 @@ public class MonthTimes implements Times { } public MonthTimes build() { - return new MonthTimes(Ints.toArray(days), times.toArray(new DayTimes[times.size()])); + return new MonthTimes(Integers.toArray(days), times.toArray(new DayTimes[times.size()])); } } } diff --git a/watcher/src/main/java/org/elasticsearch/watcher/trigger/schedule/support/WeekTimes.java b/watcher/src/main/java/org/elasticsearch/watcher/trigger/schedule/support/WeekTimes.java index 63c1e6c3b33..5e9bf53ce99 100644 --- a/watcher/src/main/java/org/elasticsearch/watcher/trigger/schedule/support/WeekTimes.java +++ b/watcher/src/main/java/org/elasticsearch/watcher/trigger/schedule/support/WeekTimes.java @@ -6,7 +6,6 @@ package org.elasticsearch.watcher.trigger.schedule.support; import com.google.common.collect.ImmutableSet; -import com.google.common.primitives.Ints; import org.elasticsearch.ElasticsearchParseException; import org.elasticsearch.common.ParseFieldMatcher; import org.elasticsearch.common.xcontent.XContentBuilder; @@ -15,6 +14,8 @@ import org.elasticsearch.common.xcontent.XContentParser; import java.io.IOException; import java.util.*; +import static org.elasticsearch.watcher.support.Strings.join; + /** * */ @@ -54,8 +55,8 @@ public class WeekTimes implements Times { public Set crons() { Set crons = new HashSet<>(); for (DayTimes times : this.times) { - String hrsStr = Ints.join(",", times.hour); - String minsStr = Ints.join(",", times.minute); + String hrsStr = join(",", times.hour); + String minsStr = join(",", times.minute); String daysStr = DayOfWeek.cronPart(this.days); crons.add("0 " + minsStr + " " + hrsStr + " ? * " + daysStr); } diff --git a/watcher/src/main/java/org/elasticsearch/watcher/trigger/schedule/support/YearTimes.java b/watcher/src/main/java/org/elasticsearch/watcher/trigger/schedule/support/YearTimes.java index 0d39dba167c..c9c3eea8b62 100644 --- a/watcher/src/main/java/org/elasticsearch/watcher/trigger/schedule/support/YearTimes.java +++ b/watcher/src/main/java/org/elasticsearch/watcher/trigger/schedule/support/YearTimes.java @@ -6,10 +6,10 @@ package org.elasticsearch.watcher.trigger.schedule.support; import com.google.common.collect.ImmutableSet; -import com.google.common.primitives.Ints; import org.elasticsearch.ElasticsearchParseException; import org.elasticsearch.common.ParseFieldMatcher; import org.elasticsearch.common.Strings; +import org.elasticsearch.common.util.primitives.Integers; import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.common.xcontent.XContentParser; @@ -17,6 +17,7 @@ import java.io.IOException; import java.util.*; import static org.elasticsearch.watcher.support.Exceptions.illegalArgument; +import static org.elasticsearch.watcher.support.Strings.join; /** * @@ -69,9 +70,9 @@ public class YearTimes implements Times { public Set crons() { Set crons = new HashSet<>(); for (DayTimes times : this.times) { - String hrsStr = Ints.join(",", times.hour); - String minsStr = Ints.join(",", times.minute); - String daysStr = Ints.join(",", this.days); + String hrsStr = join(",", times.hour); + String minsStr = join(",", times.minute); + String daysStr = join(",", this.days); daysStr = daysStr.replace("32", "L"); String monthsStr = Strings.collectionToCommaDelimitedString(months); String expression = "0 " + minsStr + " " + hrsStr + " " + daysStr + " " + monthsStr + " ?"; @@ -109,7 +110,7 @@ public class YearTimes implements Times { Locale.ROOT, "months [%s], days [%s], times [%s]", Strings.collectionToCommaDelimitedString(months), - Ints.join(",", days), + join(",", days), Strings.arrayToCommaDelimitedString(times) ); } @@ -181,7 +182,7 @@ public class YearTimes implements Times { } } EnumSet months = monthsSet.isEmpty() ? DEFAULT_MONTHS : EnumSet.copyOf(monthsSet); - int[] days = daysSet.isEmpty() ? DEFAULT_DAYS : Ints.toArray(daysSet); + int[] days = daysSet.isEmpty() ? DEFAULT_DAYS : Integers.toArray(daysSet); DayTimes[] times = timesSet.isEmpty() ? new DayTimes[] { new DayTimes(0, 0) } : timesSet.toArray(new DayTimes[timesSet.size()]); return new YearTimes(months, days, times); } @@ -211,7 +212,7 @@ public class YearTimes implements Times { } public Builder on(int... days) { - this.days.addAll(Ints.asList(days)); + Arrays.stream(days).forEach(this.days::add); return this; } @@ -236,7 +237,7 @@ public class YearTimes implements Times { } public YearTimes build() { - return new YearTimes(EnumSet.copyOf(months), Ints.toArray(days), times.toArray(new DayTimes[times.size()])); + return new YearTimes(EnumSet.copyOf(months), Integers.toArray(days), times.toArray(new DayTimes[times.size()])); } } } diff --git a/watcher/src/test/java/org/elasticsearch/watcher/trigger/schedule/DailyScheduleTests.java b/watcher/src/test/java/org/elasticsearch/watcher/trigger/schedule/DailyScheduleTests.java index c793eeddc5a..a0fdd138f63 100644 --- a/watcher/src/test/java/org/elasticsearch/watcher/trigger/schedule/DailyScheduleTests.java +++ b/watcher/src/test/java/org/elasticsearch/watcher/trigger/schedule/DailyScheduleTests.java @@ -5,7 +5,6 @@ */ package org.elasticsearch.watcher.trigger.schedule; -import com.google.common.primitives.Ints; import org.elasticsearch.ElasticsearchParseException; import org.elasticsearch.common.bytes.BytesReference; import org.elasticsearch.common.xcontent.XContentBuilder; @@ -15,6 +14,7 @@ import org.elasticsearch.watcher.trigger.schedule.support.DayTimes; import org.junit.Test; import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder; +import static org.elasticsearch.watcher.support.Strings.join; import static org.hamcrest.Matchers.*; /** @@ -36,7 +36,7 @@ public class DailyScheduleTests extends ScheduleTestCase { DailySchedule schedule = new DailySchedule(time); String[] crons = expressions(schedule); assertThat(crons, arrayWithSize(1)); - assertThat(crons, arrayContaining("0 " + Ints.join(",", time.minute()) + " " + Ints.join(",", time.hour()) + " * * ?")); + assertThat(crons, arrayContaining("0 " + join(",", time.minute()) + " " + join(",", time.hour()) + " * * ?")); } @Test(expected = IllegalArgumentException.class) @@ -53,7 +53,7 @@ public class DailyScheduleTests extends ScheduleTestCase { String[] crons = expressions(schedule); assertThat(crons, arrayWithSize(times.length)); for (DayTimes time : times) { - assertThat(crons, hasItemInArray("0 " + Ints.join(",", time.minute()) + " " + Ints.join(",", time.hour()) + " * * ?")); + assertThat(crons, hasItemInArray("0 " + join(",", time.minute()) + " " + join(",", time.hour()) + " * * ?")); } } diff --git a/watcher/src/test/java/org/elasticsearch/watcher/trigger/schedule/HourlyScheduleTests.java b/watcher/src/test/java/org/elasticsearch/watcher/trigger/schedule/HourlyScheduleTests.java index 0c3bcaa5d36..9ddedc96fb8 100644 --- a/watcher/src/test/java/org/elasticsearch/watcher/trigger/schedule/HourlyScheduleTests.java +++ b/watcher/src/test/java/org/elasticsearch/watcher/trigger/schedule/HourlyScheduleTests.java @@ -5,17 +5,20 @@ */ package org.elasticsearch.watcher.trigger.schedule; -import com.google.common.primitives.Ints; import org.elasticsearch.ElasticsearchParseException; import org.elasticsearch.common.bytes.BytesReference; import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.common.xcontent.XContentParser; import org.elasticsearch.common.xcontent.json.JsonXContent; +import org.elasticsearch.watcher.support.Strings; import org.junit.Test; +import java.util.Arrays; import java.util.stream.Collectors; import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder; +import static org.elasticsearch.watcher.support.Integers.asIterable; +import static org.elasticsearch.watcher.support.Integers.contains; import static org.hamcrest.Matchers.*; /** @@ -48,7 +51,7 @@ public class HourlyScheduleTests extends ScheduleTestCase { @Test public void test_MultipleMinutes() throws Exception { int[] minutes = validMinutes(); - String minutesStr = Ints.join(",", minutes); + String minutesStr = Strings.join(",", minutes); HourlySchedule schedule = new HourlySchedule(minutes); String[] crons = expressions(schedule); assertThat(crons, arrayWithSize(1)); @@ -134,7 +137,7 @@ public class HourlyScheduleTests extends ScheduleTestCase { int[] minutes = validMinutes(); XContentBuilder builder = jsonBuilder() .startObject() - .field("minute", Ints.asList(minutes)) + .field("minute", asIterable(minutes)) .endObject(); BytesReference bytes = builder.bytes(); XContentParser parser = JsonXContent.jsonXContent.createParser(bytes); @@ -143,7 +146,8 @@ public class HourlyScheduleTests extends ScheduleTestCase { assertThat(schedule, notNullValue()); assertThat(schedule.minutes().length, is(minutes.length)); for (int i = 0; i < minutes.length; i++) { - assertThat(Ints.contains(schedule.minutes(), minutes[i]), is(true)); + final int minute = minutes[i]; + assertThat(contains(schedule.minutes(), minutes[i]), is(true)); } } @@ -152,7 +156,7 @@ public class HourlyScheduleTests extends ScheduleTestCase { int[] minutes = invalidMinutes(); XContentBuilder builder = jsonBuilder() .startObject() - .field("minute", Ints.asList(minutes)) + .field("minute", asIterable(minutes)) .endObject(); BytesReference bytes = builder.bytes(); XContentParser parser = JsonXContent.jsonXContent.createParser(bytes); @@ -165,7 +169,7 @@ public class HourlyScheduleTests extends ScheduleTestCase { int[] minutes = validMinutes(); XContentBuilder builder = jsonBuilder() .startObject() - .field("minute", Ints.asList(minutes).stream().map(p -> p.toString()).collect(Collectors.toList())) + .field("minute", Arrays.stream(minutes).mapToObj(p -> Integer.toString(p)).collect(Collectors.toList())) .endObject(); BytesReference bytes = builder.bytes(); XContentParser parser = JsonXContent.jsonXContent.createParser(bytes); @@ -174,7 +178,7 @@ public class HourlyScheduleTests extends ScheduleTestCase { assertThat(schedule, notNullValue()); assertThat(schedule.minutes().length, is(minutes.length)); for (int i = 0; i < minutes.length; i++) { - assertThat(Ints.contains(schedule.minutes(), minutes[i]), is(true)); + assertThat(contains(schedule.minutes(), minutes[i]), is(true)); } } @@ -183,7 +187,7 @@ public class HourlyScheduleTests extends ScheduleTestCase { int[] minutes = invalidMinutes(); XContentBuilder builder = jsonBuilder() .startObject() - .field("minute", Ints.asList(minutes).stream().map(p -> p.toString()).collect(Collectors.toList())) + .field("minute", Arrays.stream(minutes).mapToObj(p -> Integer.toString(p)).collect(Collectors.toList())) .endObject(); BytesReference bytes = builder.bytes(); XContentParser parser = JsonXContent.jsonXContent.createParser(bytes); diff --git a/watcher/src/test/java/org/elasticsearch/watcher/trigger/schedule/MonthlyScheduleTests.java b/watcher/src/test/java/org/elasticsearch/watcher/trigger/schedule/MonthlyScheduleTests.java index b65a3bfb7f5..46d391908e5 100644 --- a/watcher/src/test/java/org/elasticsearch/watcher/trigger/schedule/MonthlyScheduleTests.java +++ b/watcher/src/test/java/org/elasticsearch/watcher/trigger/schedule/MonthlyScheduleTests.java @@ -5,7 +5,6 @@ */ package org.elasticsearch.watcher.trigger.schedule; -import com.google.common.primitives.Ints; import org.elasticsearch.ElasticsearchParseException; import org.elasticsearch.common.bytes.BytesReference; import org.elasticsearch.common.xcontent.XContentBuilder; @@ -16,6 +15,7 @@ import org.elasticsearch.watcher.trigger.schedule.support.MonthTimes; import org.junit.Test; import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder; +import static org.elasticsearch.watcher.support.Strings.join; import static org.hamcrest.Matchers.*; /** @@ -38,9 +38,9 @@ public class MonthlyScheduleTests extends ScheduleTestCase { String[] crons = expressions(schedule); assertThat(crons, arrayWithSize(time.times().length)); for (DayTimes dayTimes : time.times()) { - String minStr = Ints.join(",", dayTimes.minute()); - String hrStr = Ints.join(",", dayTimes.hour()); - String dayStr = Ints.join(",", time.days()); + String minStr = join(",", dayTimes.minute()); + String hrStr = join(",", dayTimes.hour()); + String dayStr = join(",", time.days()); dayStr = dayStr.replace("32", "L"); assertThat(crons, hasItemInArray("0 " + minStr + " " + hrStr + " " + dayStr + " * ?")); } @@ -58,9 +58,9 @@ public class MonthlyScheduleTests extends ScheduleTestCase { assertThat(crons, arrayWithSize(count)); for (MonthTimes monthTimes : times) { for (DayTimes dayTimes : monthTimes.times()) { - String minStr = Ints.join(",", dayTimes.minute()); - String hrStr = Ints.join(",", dayTimes.hour()); - String dayStr = Ints.join(",", monthTimes.days()); + String minStr = join(",", dayTimes.minute()); + String hrStr = join(",", dayTimes.hour()); + String dayStr = join(",", monthTimes.days()); dayStr = dayStr.replace("32", "L"); assertThat(crons, hasItemInArray("0 " + minStr + " " + hrStr + " " + dayStr + " * ?")); } diff --git a/watcher/src/test/java/org/elasticsearch/watcher/trigger/schedule/ScheduleTestCase.java b/watcher/src/test/java/org/elasticsearch/watcher/trigger/schedule/ScheduleTestCase.java index 11670073c5d..b4548c1e80d 100644 --- a/watcher/src/test/java/org/elasticsearch/watcher/trigger/schedule/ScheduleTestCase.java +++ b/watcher/src/test/java/org/elasticsearch/watcher/trigger/schedule/ScheduleTestCase.java @@ -5,7 +5,7 @@ */ package org.elasticsearch.watcher.trigger.schedule; -import com.google.common.primitives.Ints; +import org.elasticsearch.common.util.primitives.Integers; import org.elasticsearch.common.xcontent.ToXContent; import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.test.ESTestCase; @@ -174,7 +174,7 @@ public abstract class ScheduleTestCase extends ESTestCase { for (int i = 0; i < count; i++) { days.add(randomIntBetween(1, 31)); } - return Ints.toArray(days); + return Integers.toArray(days); } protected static Object randomDayOfMonth() { diff --git a/watcher/src/test/java/org/elasticsearch/watcher/trigger/schedule/WeeklyScheduleTests.java b/watcher/src/test/java/org/elasticsearch/watcher/trigger/schedule/WeeklyScheduleTests.java index 176d678effb..d60b9b3feeb 100644 --- a/watcher/src/test/java/org/elasticsearch/watcher/trigger/schedule/WeeklyScheduleTests.java +++ b/watcher/src/test/java/org/elasticsearch/watcher/trigger/schedule/WeeklyScheduleTests.java @@ -6,7 +6,6 @@ package org.elasticsearch.watcher.trigger.schedule; -import com.google.common.primitives.Ints; import org.elasticsearch.ElasticsearchParseException; import org.elasticsearch.common.Strings; import org.elasticsearch.common.bytes.BytesReference; @@ -19,6 +18,7 @@ import org.elasticsearch.watcher.trigger.schedule.support.WeekTimes; import org.junit.Test; import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder; +import static org.elasticsearch.watcher.support.Strings.join; import static org.hamcrest.Matchers.*; /** @@ -41,7 +41,7 @@ public class WeeklyScheduleTests extends ScheduleTestCase { String[] crons = expressions(schedule); assertThat(crons, arrayWithSize(time.times().length)); for (DayTimes dayTimes : time.times()) { - assertThat(crons, hasItemInArray("0 " + Ints.join(",", dayTimes.minute()) + " " + Ints.join(",", dayTimes.hour()) + " ? * " + Strings.collectionToCommaDelimitedString(time.days()))); + assertThat(crons, hasItemInArray("0 " + join(",", dayTimes.minute()) + " " + join(",", dayTimes.hour()) + " ? * " + Strings.collectionToCommaDelimitedString(time.days()))); } } @@ -57,7 +57,7 @@ public class WeeklyScheduleTests extends ScheduleTestCase { assertThat(crons, arrayWithSize(count)); for (WeekTimes weekTimes : times) { for (DayTimes dayTimes : weekTimes.times()) { - assertThat(crons, hasItemInArray("0 " + Ints.join(",", dayTimes.minute()) + " " + Ints.join(",", dayTimes.hour()) + " ? * " + Strings.collectionToCommaDelimitedString(weekTimes.days()))); + assertThat(crons, hasItemInArray("0 " + join(",", dayTimes.minute()) + " " + join(",", dayTimes.hour()) + " ? * " + Strings.collectionToCommaDelimitedString(weekTimes.days()))); } } } diff --git a/watcher/src/test/java/org/elasticsearch/watcher/trigger/schedule/YearlyScheduleTests.java b/watcher/src/test/java/org/elasticsearch/watcher/trigger/schedule/YearlyScheduleTests.java index 8348bb0853f..d4eb738e093 100644 --- a/watcher/src/test/java/org/elasticsearch/watcher/trigger/schedule/YearlyScheduleTests.java +++ b/watcher/src/test/java/org/elasticsearch/watcher/trigger/schedule/YearlyScheduleTests.java @@ -6,7 +6,6 @@ package org.elasticsearch.watcher.trigger.schedule; -import com.google.common.primitives.Ints; import org.elasticsearch.ElasticsearchParseException; import org.elasticsearch.common.Strings; import org.elasticsearch.common.bytes.BytesReference; @@ -18,6 +17,7 @@ import org.elasticsearch.watcher.trigger.schedule.support.YearTimes; import org.junit.Test; import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder; +import static org.elasticsearch.watcher.support.Strings.join; import static org.hamcrest.Matchers.*; /** @@ -40,9 +40,9 @@ public class YearlyScheduleTests extends ScheduleTestCase { String[] crons = expressions(schedule); assertThat(crons, arrayWithSize(time.times().length)); for (DayTimes dayTimes : time.times()) { - String minStr = Ints.join(",", dayTimes.minute()); - String hrStr = Ints.join(",", dayTimes.hour()); - String dayStr = Ints.join(",", time.days()); + String minStr = join(",", dayTimes.minute()); + String hrStr = join(",", dayTimes.hour()); + String dayStr = join(",", time.days()); dayStr = dayStr.replace("32", "L"); String monthStr = Strings.collectionToCommaDelimitedString(time.months()); String expression = "0 " + minStr + " " + hrStr + " " + dayStr + " " + monthStr + " ?"; @@ -63,9 +63,9 @@ public class YearlyScheduleTests extends ScheduleTestCase { assertThat(crons, arrayWithSize(count)); for (YearTimes yearTimes : times) { for (DayTimes dayTimes : yearTimes.times()) { - String minStr = Ints.join(",", dayTimes.minute()); - String hrStr = Ints.join(",", dayTimes.hour()); - String dayStr = Ints.join(",", yearTimes.days()); + String minStr = join(",", dayTimes.minute()); + String hrStr = join(",", dayTimes.hour()); + String dayStr = join(",", yearTimes.days()); dayStr = dayStr.replace("32", "L"); String monthStr = Strings.collectionToCommaDelimitedString(yearTimes.months()); assertThat(crons, hasItemInArray("0 " + minStr + " " + hrStr + " " + dayStr + " " + monthStr + " ?"));