Skip to content

Commit

Permalink
Fixes #625
Browse files Browse the repository at this point in the history
  • Loading branch information
jfarcand committed Jan 8, 2015
1 parent 6e89189 commit 725d526
Show file tree
Hide file tree
Showing 15 changed files with 179 additions and 15 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*

This comment has been minimized.

Copy link
@jfarcand

jfarcand Jan 8, 2015

Author Member

Fix was for #626

* Copyright 2014 Jeanfrancois Arcand
*
* 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 org.atmosphere.annotation;

import org.atmosphere.config.AtmosphereAnnotation;
import org.atmosphere.config.service.UUIDProviderService;
import org.atmosphere.cpr.AtmosphereFramework;
import org.atmosphere.util.UUIDProvider;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@AtmosphereAnnotation(UUIDProviderService.class)
public class UUIDProviderServiceProcessor implements Processor<UUIDProvider> {

private static final Logger logger = LoggerFactory.getLogger(UUIDProviderServiceProcessor.class);

@Override
public void handle(AtmosphereFramework framework, Class<UUIDProvider> annotatedClass) {
try {
framework.uuidProvider(framework.newClassInstance(UUIDProvider.class, annotatedClass));
} catch (Throwable e) {
logger.warn("", e);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import org.atmosphere.cpr.BroadcasterCacheListener;
import org.atmosphere.cpr.BroadcasterConfig;
import org.atmosphere.util.ExecutorsFactory;
import org.atmosphere.util.UUIDProvider;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand All @@ -32,7 +33,6 @@
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.UUID;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.Executors;
Expand Down Expand Up @@ -61,6 +61,7 @@ public class UUIDBroadcasterCache implements BroadcasterCache {
private boolean shared = true;
protected final List<Object> emptyList = Collections.<Object>emptyList();
protected final List<BroadcasterCacheListener> listeners = new LinkedList<BroadcasterCacheListener>();
private UUIDProvider uuidProvider;

/**
* This class wraps all messages to be delivered to a client. The class is thread safe to be accessed in a
Expand Down Expand Up @@ -104,6 +105,8 @@ public void configure(BroadcasterConfig config) {

invalidateCacheInterval = TimeUnit.SECONDS.toMillis(
Long.valueOf(config.getAtmosphereConfig().getInitParameter(ApplicationConfig.UUIDBROADCASTERCACHE_IDLE_CACHE_INTERVAL, "30")));

uuidProvider = config.getAtmosphereConfig().uuidProvider();
}

@Override
Expand Down Expand Up @@ -145,7 +148,7 @@ public CacheMessage addToCache(String broadcasterId, String uuid, BroadcastMessa
logger.trace("Active clients {}", activeClients());
}

String messageId = UUID.randomUUID().toString();
String messageId = uuidProvider.generateUuid();
boolean cache = true;
if (!inspect(message)) {
cache = false;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* Copyright 2014 Jeanfrancois Arcand
*
* 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 org.atmosphere.config.service;

import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/**
* An annotation to use in order to replace the default {@link org.atmosphere.util.UUIDProvider}
*
* @author Jeanfrancois Arcand
*/
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface UUIDProviderService {
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import org.atmosphere.config.service.AtmosphereHandlerService;
import org.atmosphere.config.service.AtmosphereInterceptorService;
import org.atmosphere.config.service.AtmosphereResourceFactoryService;
import org.atmosphere.config.service.AtmosphereResourceListenerService;
import org.atmosphere.config.service.AtmosphereService;
import org.atmosphere.config.service.BroadcasterCacheInspectorService;
import org.atmosphere.config.service.BroadcasterCacheListenerService;
Expand All @@ -18,6 +19,7 @@
import org.atmosphere.config.service.EndpointMapperService;
import org.atmosphere.config.service.ManagedService;
import org.atmosphere.config.service.MeteorService;
import org.atmosphere.config.service.UUIDProviderService;
import org.atmosphere.config.service.WebSocketHandlerService;
import org.atmosphere.config.service.WebSocketProcessorService;
import org.atmosphere.config.service.WebSocketProtocolService;
Expand Down Expand Up @@ -62,7 +64,8 @@
AtmosphereAnnotation.class,
AtmosphereResourceFactoryService.class,
AtmosphereFrameworkListenerService.class,
AtmosphereResourceListener.class
AtmosphereResourceListenerService.class,
UUIDProviderService.class
})
public class AnnotationScanningServletContainerInitializer implements ServletContainerInitializer {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package org.atmosphere.cpr;

import org.atmosphere.config.AtmosphereHandlerConfig;
import org.atmosphere.util.UUIDProvider;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand Down Expand Up @@ -322,6 +323,14 @@ public AtmosphereResourceSessionFactory sessionFactory() {
return framework.sessionFactory();
}

/**
* Return the {@link org.atmosphere.util.UUIDProvider}
*
* @return {@link org.atmosphere.util.UUIDProvider}
*/
public UUIDProvider uuidProvider() {
return framework.uuidProvider();
}

/**
* A shutdown hook that will be called when the {@link AtmosphereFramework#destroy} method gets invoked. An
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,14 @@
import org.atmosphere.interceptor.WebSocketMessageSuspendInterceptor;
import org.atmosphere.util.AtmosphereConfigReader;
import org.atmosphere.util.DefaultEndpointMapper;
import org.atmosphere.util.DefaultUUIDProvider;
import org.atmosphere.util.EndpointMapper;
import org.atmosphere.util.ExecutorsFactory;
import org.atmosphere.util.IOUtils;
import org.atmosphere.util.IntrospectionUtils;
import org.atmosphere.util.ServletContextFactory;
import org.atmosphere.util.ServletProxyFactory;
import org.atmosphere.util.UUIDProvider;
import org.atmosphere.util.Version;
import org.atmosphere.util.analytics.FocusPoint;
import org.atmosphere.util.analytics.JGoogleAnalyticsTracker;
Expand Down Expand Up @@ -93,7 +95,6 @@
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.UUID;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.ExecutorService;
Expand Down Expand Up @@ -231,6 +232,7 @@ public class AtmosphereFramework {
protected String defaultSerializerClassName;
protected Class<Serializer> defaultSerializerClass;
protected final List<AtmosphereFrameworkListener> frameworkListeners = new LinkedList<AtmosphereFrameworkListener>();
private UUIDProvider uuidProvider = new DefaultUUIDProvider();
protected final Class<? extends AtmosphereInterceptor>[] defaultInterceptors = new Class[]{
// Add CORS support
CorsInterceptor.class,
Expand Down Expand Up @@ -2090,7 +2092,7 @@ public AtmosphereFramework configureRequestResponse(AtmosphereRequest req, Atmos
}

if (s == null || s.equals("0")) {
s = UUID.randomUUID().toString();
s = config.uuidProvider().generateUuid();
res.setHeader(HeaderConfig.X_FIRST_REQUEST, "true");
res.setHeader(X_ATMOSPHERE_TRACKING_ID, s);
} else {
Expand Down Expand Up @@ -3238,4 +3240,22 @@ public AtmosphereFramework atmosphereResourceListener(AtmosphereResourceListener
return this;
}

/**
* Set a {@link java.util.UUID} like implementation for generating random UUID String
* @param uuidProvider
* @return this
*/
public AtmosphereFramework uuidProvider(UUIDProvider uuidProvider) {
this.uuidProvider = uuidProvider;
return this;
}

/**
* Return the {@link org.atmosphere.util.UUIDProvider}
* @return {@link org.atmosphere.util.UUIDProvider}
*/
public UUIDProvider uuidProvider(){
return uuidProvider;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ public AtmosphereResource initialize(AtmosphereConfig config, Broadcaster broadc
s = tmp != null && !tmp.equalsIgnoreCase("0") ? tmp : null;
}
}
uuid = s == null ? UUID.randomUUID().toString() : s;
uuid = s == null ? config.uuidProvider().generateUuid() : s;

if (config.isSupportSession()) {
// Keep a reference to an HttpSession in case the associated request get recycled by the underlying container.
Expand Down Expand Up @@ -461,7 +461,8 @@ protected Broadcaster getBroadcaster(boolean autoCreate) {

Broadcaster.SCOPE scope = broadcaster.getScope();
synchronized (this) {
String id = scope != Broadcaster.SCOPE.REQUEST ? broadcaster.getID() : broadcaster.getID() + ".recovered" + UUID.randomUUID();
String id = scope != Broadcaster.SCOPE.REQUEST ? broadcaster.getID() : broadcaster.getID() + ".recovered"
+ config.uuidProvider().generateUuid();

// Another Thread may have added the Broadcaster.
broadcaster = config.getBroadcasterFactory().lookup(id, true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import org.atmosphere.config.service.AtmosphereHandlerService;
import org.atmosphere.config.service.AtmosphereInterceptorService;
import org.atmosphere.config.service.AtmosphereResourceFactoryService;
import org.atmosphere.config.service.AtmosphereResourceListenerService;
import org.atmosphere.config.service.AtmosphereService;
import org.atmosphere.config.service.BroadcasterCacheInspectorService;
import org.atmosphere.config.service.BroadcasterCacheListenerService;
Expand All @@ -33,6 +34,7 @@
import org.atmosphere.config.service.EndpointMapperService;
import org.atmosphere.config.service.ManagedService;
import org.atmosphere.config.service.MeteorService;
import org.atmosphere.config.service.UUIDProviderService;
import org.atmosphere.config.service.WebSocketHandlerService;
import org.atmosphere.config.service.WebSocketProcessorService;
import org.atmosphere.config.service.WebSocketProtocolService;
Expand Down Expand Up @@ -92,7 +94,8 @@ public class DefaultAnnotationProcessor implements AnnotationProcessor {
AtmosphereAnnotation.class,
AtmosphereResourceFactoryService.class,
AtmosphereFrameworkListenerService.class,
AtmosphereResourceListener.class
AtmosphereResourceListenerService.class,
UUIDProviderService.class
};

private AnnotationProcessor delegate;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@
import java.util.LinkedList;
import java.util.List;
import java.util.Set;
import java.util.UUID;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.Callable;
import java.util.concurrent.ConcurrentHashMap;
Expand Down Expand Up @@ -217,7 +216,7 @@ public void setScope(SCOPE scope) {
// Next, we need to create a new broadcaster per resource.
for (AtmosphereResource resource : resources) {
Broadcaster b = config.getBroadcasterFactory()
.get(getClass(), getClass().getSimpleName() + "/" + UUID.randomUUID());
.get(getClass(), getClass().getSimpleName() + "/" + config.uuidProvider().generateUuid());

/**
* REQUEST_SCOPE means one BroadcasterCache per Broadcaster,
Expand Down Expand Up @@ -255,7 +254,7 @@ public SCOPE getScope() {
@Override
public synchronized void setID(String id) {
if (id == null) {
id = getClass().getSimpleName() + "/" + UUID.randomUUID();
id = getClass().getSimpleName() + "/" + config.uuidProvider().generateUuid();
}

if (config.getBroadcasterFactory() == null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
import java.util.Collection;
import java.util.Collections;
import java.util.Enumeration;
import java.util.UUID;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentLinkedQueue;

Expand Down Expand Up @@ -114,7 +113,7 @@ private void configure(String broadcasterLifeCyclePolicy) {

@Override
public synchronized final Broadcaster get() {
return get(clazz.getSimpleName() + "-" + UUID.randomUUID());
return get(clazz.getSimpleName() + "-" + config.uuidProvider().generateUuid());
}

@Override
Expand Down
4 changes: 2 additions & 2 deletions modules/cpr/src/main/java/org/atmosphere/cpr/Meteor.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.UUID;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;

Expand Down Expand Up @@ -132,7 +131,8 @@ public final static Meteor build(HttpServletRequest req, Broadcaster.SCOPE scope
if (scope == Broadcaster.SCOPE.REQUEST) {
try {
BroadcasterFactory f = r.getAtmosphereConfig().getBroadcasterFactory();
b = f.get(DefaultBroadcaster.class, DefaultBroadcaster.class.getSimpleName() + UUID.randomUUID());
b = f.get(DefaultBroadcaster.class, DefaultBroadcaster.class.getSimpleName()
+ r.getAtmosphereConfig().uuidProvider().generateUuid());
} catch (Throwable t) {
throw new RuntimeException(t);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
* Copyright 2014 Jeanfrancois Arcand
*
* 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 org.atmosphere.util;

import java.util.UUID;

public class DefaultUUIDProvider implements UUIDProvider {
@Override
public String generateUuid() {
return UUID.randomUUID().toString();
}
}
22 changes: 22 additions & 0 deletions modules/cpr/src/main/java/org/atmosphere/util/UUIDProvider.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/*
* Copyright 2014 Jeanfrancois Arcand
*
* 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 org.atmosphere.util;

public interface UUIDProvider {

public String generateUuid();

}
Loading

0 comments on commit 725d526

Please sign in to comment.