Skip to content

Commit e8b3dec

Browse files
committed
Remove dependency on google guava
1 parent 749843a commit e8b3dec

30 files changed

Lines changed: 481 additions & 364 deletions

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ Add it as a maven dependency:
5959
<dependency>
6060
<groupId>com.stackify</groupId>
6161
<artifactId>stackify-api-java</artifactId>
62-
<version>2.0.4</version>
62+
<version>2.0.5</version>
6363
</dependency>
6464
```
6565

pom.xml

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
<parent>
1313
<groupId>org.sonatype.oss</groupId>
1414
<artifactId>oss-parent</artifactId>
15-
<version>7</version>
15+
<version>9</version>
1616
</parent>
1717

1818
<licenses>
@@ -48,12 +48,6 @@
4848

4949
<!-- Compile time dependencies -->
5050

51-
<dependency>
52-
<groupId>com.google.guava</groupId>
53-
<artifactId>guava</artifactId>
54-
<version>13.0.1</version>
55-
</dependency>
56-
5751
<dependency>
5852
<groupId>org.slf4j</groupId>
5953
<artifactId>slf4j-api</artifactId>

src/main/java/com/stackify/api/common/ApiConfiguration.java

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
package com.stackify.api.common;
1717

1818
import com.stackify.api.EnvironmentDetail;
19-
import com.google.common.base.Objects;
2019

2120
/**
2221
* ApiConfiguration
@@ -210,17 +209,11 @@ public ApiConfiguration build() {
210209

211210
/**
212211
* @see java.lang.Object#toString()
213-
* @return A string representation of the object
214212
*/
215213
@Override
216214
public String toString() {
217-
return Objects.toStringHelper(this)
218-
.omitNullValues()
219-
.add("apiUrl", apiUrl)
220-
.add("apiKey", apiKey)
221-
.add("application", application)
222-
.add("environment", environment)
223-
.add("envDetail", envDetail)
224-
.toString();
215+
return "ApiConfiguration [apiUrl=" + apiUrl + ", apiKey=" + apiKey
216+
+ ", application=" + application + ", environment="
217+
+ environment + ", envDetail=" + envDetail + "]";
225218
}
226219
}

src/main/java/com/stackify/api/common/AppIdentityService.java

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -19,18 +19,17 @@
1919
import java.util.Hashtable;
2020
import java.util.Map;
2121

22-
import com.stackify.api.EnvironmentDetail;
2322
import org.slf4j.Logger;
2423
import org.slf4j.LoggerFactory;
2524

2625
import com.fasterxml.jackson.core.type.TypeReference;
2726
import com.fasterxml.jackson.databind.ObjectMapper;
2827
import com.fasterxml.jackson.databind.ObjectReader;
29-
import com.google.common.base.Optional;
30-
import com.google.common.base.Preconditions;
3128
import com.stackify.api.AppIdentity;
29+
import com.stackify.api.EnvironmentDetail;
3230
import com.stackify.api.common.http.HttpClient;
3331
import com.stackify.api.common.http.HttpException;
32+
import com.stackify.api.common.util.Preconditions;
3433

3534
/**
3635
* AppIdentityService
@@ -80,11 +79,11 @@ public AppIdentityService(final ApiConfiguration apiConfig, final ObjectMapper o
8079
* Retrieves the application identity given the environment details
8180
* @return The application identity
8281
*/
83-
private Optional<AppIdentity> getAppIdentity(ApiConfiguration apiConfig) {
82+
private AppIdentity getAppIdentity(ApiConfiguration apiConfig) {
8483
final String applicationName = apiConfig.getApplication();
8584

8685
if (applicationName == null)
87-
return Optional.absent();
86+
return null;
8887

8988
// If there's no record create it.
9089
if (!applicationIdentityCache.containsKey(applicationName)) {
@@ -115,7 +114,7 @@ private Optional<AppIdentity> getAppIdentity(ApiConfiguration apiConfig) {
115114
* @param applicationName - name of the application
116115
* @return The application identity
117116
*/
118-
public Optional<AppIdentity> getAppIdentity(final String applicationName) {
117+
public AppIdentity getAppIdentity(final String applicationName) {
119118
if (isCached(applicationName)) {
120119
return applicationIdentityCache.get(applicationName).getAppIdentity();
121120

@@ -138,7 +137,7 @@ public Optional<AppIdentity> getAppIdentity(final String applicationName) {
138137
* getAppIdentity
139138
* @return The application identity
140139
*/
141-
public Optional<AppIdentity> getAppIdentity() {
140+
public AppIdentity getAppIdentity() {
142141
if (isCached(defaultApiConfig.getApplication()))
143142
return applicationIdentityCache.get(defaultApiConfig.getApplication()).getAppIdentity();
144143
else
@@ -170,7 +169,7 @@ private boolean isCached(final String applicationName) {
170169

171170
return
172171
applicationIdentityCache.containsKey(applicationName) &&
173-
applicationIdentityCache.get(applicationName).getAppIdentity().isPresent();
172+
applicationIdentityCache.get(applicationName).getAppIdentity() != null;
174173
}
175174

176175

@@ -190,20 +189,20 @@ private EnvironmentDetail updateEnvironmentDetail(final EnvironmentDetail envDet
190189
*/
191190
private class AppIdentityState {
192191

193-
private Optional<AppIdentity> mayBeAppIdentity = Optional.absent();
192+
private AppIdentity mayBeAppIdentity;
194193
private long lastQueryTimeStamp;
195194

196195
public AppIdentityState() {
197196
this.lastQueryTimeStamp = 0;
198-
this.mayBeAppIdentity = Optional.absent();
197+
this.mayBeAppIdentity = null;
199198
}
200199

201200
public final AppIdentityState updateAppIdentity(final AppIdentity appIdentity) {
202-
mayBeAppIdentity = Optional.fromNullable(appIdentity);
201+
mayBeAppIdentity = appIdentity;
203202
return this;
204203
}
205204

206-
public final Optional<AppIdentity> getAppIdentity() {
205+
public final AppIdentity getAppIdentity() {
207206
return mayBeAppIdentity;
208207
}
209208

src/main/java/com/stackify/api/common/collect/SynchronizedEvictingQueue.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
import java.util.Iterator;
2121
import java.util.Queue;
2222

23-
import com.google.common.base.Preconditions;
23+
import com.stackify.api.common.util.Preconditions;
2424

2525
/**
2626
* SynchronizedEvictingQueue

src/main/java/com/stackify/api/common/concurrent/BackgroundService.java

Lines changed: 150 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,30 +15,168 @@
1515
*/
1616
package com.stackify.api.common.concurrent;
1717

18+
import java.util.concurrent.Callable;
19+
import java.util.concurrent.Executors;
1820
import java.util.concurrent.ScheduledExecutorService;
21+
import java.util.concurrent.ScheduledFuture;
22+
import java.util.concurrent.TimeUnit;
23+
import java.util.concurrent.locks.ReentrantLock;
1924

20-
import com.google.common.util.concurrent.AbstractScheduledService;
21-
import com.google.common.util.concurrent.MoreExecutors;
25+
import org.slf4j.Logger;
26+
import org.slf4j.LoggerFactory;
2227

2328
/**
2429
* BackgroundService
2530
* @author Eric Martin
2631
*/
27-
public abstract class BackgroundService extends AbstractScheduledService
32+
public abstract class BackgroundService
2833
{
2934
/**
30-
* @see com.google.common.util.concurrent.AbstractScheduledService#executor()
35+
* The service logger
3136
*/
32-
@Override
33-
protected ScheduledExecutorService executor() {
37+
private static final Logger LOGGER = LoggerFactory.getLogger(BackgroundService.class);
3438

35-
ScheduledExecutorService executor = super.executor();
39+
/**
40+
* Executor service
41+
*/
42+
private ScheduledExecutorService executorService;
43+
44+
/**
45+
* Next iteration
46+
*/
47+
private ScheduledFuture<Void> currentFuture;
48+
49+
/**
50+
* Lock to protect start/stop/iteration/reschedule
51+
*/
52+
private final ReentrantLock lock = new ReentrantLock();
53+
54+
/**
55+
* Start the background service
56+
* @throws Exception
57+
*/
58+
protected abstract void startUp() throws Exception;
59+
60+
/**
61+
* Run one iteration of the background service
62+
* @throws Exception
63+
*/
64+
protected abstract void runOneIteration() throws Exception;
65+
66+
/**
67+
* @return The next schedule delay (between iterations) in milliseconds
68+
*/
69+
protected abstract long getNextScheduleDelayMilliseconds();
70+
71+
/**
72+
* Shut down the background service
73+
* @throws Exception
74+
*/
75+
protected abstract void shutDown() throws Exception;
76+
77+
/**
78+
* Start the background service/thread
79+
*/
80+
public void start() {
3681

37-
// Add a listener to shutdown the executor after the service is stopped.
38-
// This is to work around a feature in Guava 13.0.1 that does not stop the executor when the service stops.
39-
40-
addListener(new ShutdownListener(executor), MoreExecutors.sameThreadExecutor());
82+
lock.lock();
83+
84+
try {
85+
executorService = Executors.newSingleThreadScheduledExecutor();
86+
87+
try {
88+
startUp();
89+
} catch (Throwable t) {
90+
LOGGER.info("Exception in service start up", t);
91+
}
92+
93+
currentFuture = executorService.schedule(new RunOneIterationAndReschedule(), 0, TimeUnit.MILLISECONDS);
94+
} finally {
95+
lock.unlock();
96+
}
97+
}
98+
99+
/**
100+
* @return True if the background service is running, false otherwise
101+
*/
102+
public boolean isRunning() {
103+
if (executorService != null) {
104+
if (!executorService.isShutdown() && !executorService.isTerminated()) {
105+
return true;
106+
}
107+
}
108+
109+
return false;
110+
}
111+
112+
/**
113+
* Stops the background service/thread
114+
*/
115+
public void stop() {
116+
117+
lock.lock();
118+
119+
try {
120+
currentFuture.cancel(false);
121+
122+
try {
123+
shutDown();
124+
} catch (Throwable t) {
125+
LOGGER.info("Exception in service shut down", t);
126+
}
127+
128+
try {
129+
executorService.shutdown();
130+
executorService.awaitTermination(5, TimeUnit.SECONDS);
131+
} catch (Throwable t) {
132+
LOGGER.info("Exception in service termination", t);
133+
}
134+
} finally {
135+
lock.unlock();
136+
}
137+
}
138+
139+
/**
140+
* RunOneIterationAndReschedule
141+
*/
142+
private class RunOneIterationAndReschedule implements Callable<Void> {
143+
144+
/**
145+
* @see java.util.concurrent.Callable#call()
146+
*/
147+
@Override
148+
public Void call() throws Exception {
149+
150+
// run one iteration
151+
152+
lock.lock();
153+
154+
try {
155+
runOneIteration();
156+
} catch (Throwable t) {
157+
LOGGER.info("Exception in iteration", t);
158+
} finally {
159+
lock.unlock();
160+
}
161+
162+
// reschedule
163+
164+
lock.lock();
165+
166+
try {
167+
if (!currentFuture.isCancelled()) {
168+
long nextDelay = getNextScheduleDelayMilliseconds();
169+
currentFuture = executorService.schedule(this, nextDelay, TimeUnit.MILLISECONDS);
170+
}
171+
} catch (Throwable t) {
172+
LOGGER.info("Exception rescheduling iteration", t);
173+
} finally {
174+
lock.unlock();
175+
}
176+
177+
// done
41178

42-
return executor;
179+
return null;
180+
}
43181
}
44182
}

0 commit comments

Comments
 (0)