Skip to content

Commit

Permalink
Refactor init process of WebFilter (#37)
Browse files Browse the repository at this point in the history
  • Loading branch information
emre-aydin committed Jan 2, 2017
1 parent c81ae1d commit ff07d03
Show file tree
Hide file tree
Showing 16 changed files with 343 additions and 274 deletions.
21 changes: 4 additions & 17 deletions src/main/java/com/hazelcast/web/ClusteredSessionService.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,10 @@
import com.hazelcast.web.entryprocessor.GetSessionStateEntryProcessor;
import com.hazelcast.web.entryprocessor.SessionUpdateEntryProcessor;

import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import java.util.AbstractMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Properties;
import java.util.Queue;
import java.util.Set;
import java.util.concurrent.Executors;
Expand Down Expand Up @@ -71,8 +69,7 @@ public class ClusteredSessionService {
private volatile SerializationServiceSupport sss;
private volatile HazelcastInstance hazelcastInstance;

private final FilterConfig filterConfig;
private final Properties properties;
private final WebFilterConfig filterConfig;

private final Queue<AbstractMap.SimpleEntry<String, Boolean>> orphanSessions = new
LinkedBlockingQueue<AbstractMap.SimpleEntry<String, Boolean>>();
Expand All @@ -85,26 +82,16 @@ public class ClusteredSessionService {
* Instantiates a new Clustered session service.
*
* @param filterConfig the filter config
* @param properties the properties
*/
public ClusteredSessionService(FilterConfig filterConfig, Properties properties) {
public ClusteredSessionService(WebFilterConfig filterConfig) {
this.filterConfig = filterConfig;
this.properties = properties;
try {
init();
} catch (Exception e) {
ExceptionUtil.rethrow(e);
}
}

public Properties getProperties() {
return properties;
}

public FilterConfig getFilterConfig() {
return filterConfig;
}

public void setFailedConnection(boolean failedConnection) {
this.failedConnection = failedConnection;
}
Expand Down Expand Up @@ -146,8 +133,8 @@ private void ensureInstance() throws Exception {
private void reconnectHZInstance() throws ServletException {
LOGGER.info("Retrying the connection!!");
lastConnectionTry = System.currentTimeMillis();
hazelcastInstance = HazelcastInstanceLoader.createInstance(this);
clusterMap = hazelcastInstance.getMap(properties.getProperty(HazelcastInstanceLoader.MAP_NAME));
hazelcastInstance = HazelcastInstanceLoader.createInstance(this, filterConfig);
clusterMap = hazelcastInstance.getMap(filterConfig.getMapName());
sss = (SerializationServiceSupport) hazelcastInstance;
setFailedConnection(false);
LOGGER.info("Successfully Connected!");
Expand Down
18 changes: 4 additions & 14 deletions src/main/java/com/hazelcast/web/HazelcastHttpSession.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,12 @@
import javax.servlet.ServletContext;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpSessionContext;
import java.util.Collections;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.StringTokenizer;
import java.util.concurrent.ConcurrentHashMap;

/**
Expand All @@ -53,23 +51,15 @@ public class HazelcastHttpSession implements HttpSession {
private Set<String> transientAttributes;

public HazelcastHttpSession(WebFilter webFilter, final String sessionId, final HttpSession originalSession,
final boolean deferredWrite, final boolean stickySession) {
final boolean deferredWrite, final boolean stickySession,
final Set<String> transientAttributes) {
this.webFilter = webFilter;
this.id = sessionId;
this.originalSession = originalSession;
this.deferredWrite = deferredWrite;
this.stickySession = stickySession;
String transientAttributesParam = webFilter.getParam("transient-attributes");
if (transientAttributesParam == null) {
this.transientAttributes = Collections.emptySet();
} else {
this.transientAttributes = new HashSet<String>();
StringTokenizer st = new StringTokenizer(transientAttributesParam, ",");
while (st.hasMoreTokens()) {
String token = st.nextToken();
this.transientAttributes.add(token.trim());
}
}
this.transientAttributes = transientAttributes;

buildLocalCache();
}

Expand Down
68 changes: 12 additions & 56 deletions src/main/java/com/hazelcast/web/HazelcastInstanceLoader.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
import com.hazelcast.client.config.ClientConfig;
import com.hazelcast.client.config.XmlClientConfigBuilder;
import com.hazelcast.config.Config;
import com.hazelcast.config.ConfigLoader;
import com.hazelcast.config.InvalidConfigurationException;
import com.hazelcast.config.ListenerConfig;
import com.hazelcast.config.MapConfig;
Expand All @@ -35,61 +34,36 @@
import com.hazelcast.web.listener.ClientLifecycleListener;
import com.hazelcast.web.listener.ServerLifecycleListener;

import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Properties;
import java.util.logging.Level;

import static java.lang.String.format;

final class HazelcastInstanceLoader {

public static final String INSTANCE_NAME = "instance-name";
public static final String CONFIG_LOCATION = "config-location";
public static final String USE_CLIENT = "use-client";
public static final String CLIENT_CONFIG_LOCATION = "client-config-location";
public static final String STICKY_SESSION_CONFIG = "sticky-session";
public static final String SESSION_TTL_CONFIG = "session-ttl-seconds";
public static final String MAP_NAME = "map-name";

private static final String SESSION_TTL_DEFAULT_SECONDS = "1800";

private static final ILogger LOGGER = Logger.getLogger(HazelcastInstanceLoader.class);

private HazelcastInstanceLoader() {
}

public static HazelcastInstance createInstance(final ClusteredSessionService sessionService)
throws ServletException {
static HazelcastInstance createInstance(final ClusteredSessionService sessionService,
WebFilterConfig filterConfig) throws ServletException {

final Properties properties = sessionService.getProperties();
final String instanceName = properties.getProperty(INSTANCE_NAME);
final String configLocation = properties.getProperty(CONFIG_LOCATION);
final String useClientProp = properties.getProperty(USE_CLIENT);
final String clientConfigLocation = properties.getProperty(CLIENT_CONFIG_LOCATION);
final boolean useClient = !isEmpty(useClientProp) && Boolean.parseBoolean(useClientProp);
URL configUrl = null;
if (useClient && !isEmpty(clientConfigLocation)) {
configUrl = getConfigURL(sessionService.getFilterConfig(), clientConfigLocation);
} else if (!isEmpty(configLocation)) {
configUrl = getConfigURL(sessionService.getFilterConfig(), configLocation);
}
String sessionTTLConfig = properties.getProperty(SESSION_TTL_CONFIG);
if (useClient) {
if (sessionTTLConfig != null) {
throw new InvalidConfigurationException("session-ttl-seconds cannot be used with client/server mode.");
}
boolean isSticky = Boolean.valueOf(properties.getProperty(STICKY_SESSION_CONFIG));
final String instanceName = filterConfig.getInstanceName();
URL configUrl = filterConfig.getConfigUrl();
int sessionTTLConfig = filterConfig.getSessionTtlSeconds();

if (filterConfig.isUseClient()) {
boolean isSticky = filterConfig.isStickySession();
return createClientInstance(sessionService, configUrl, instanceName, isSticky);
}
Config config = getServerConfig(properties.getProperty(MAP_NAME), configUrl, sessionTTLConfig);
Config config = getServerConfig(filterConfig.getMapName(), configUrl, sessionTTLConfig);
return createHazelcastInstance(sessionService, instanceName, config);
}

private static Config getServerConfig(String mapName, URL configUrl, String sessionTTLConfig) throws ServletException {
private static Config getServerConfig(String mapName, URL configUrl, int sessionTTLConfig) throws ServletException {
Config config;
if (configUrl == null) {
config = new XmlConfigBuilder().build();
Expand All @@ -100,13 +74,11 @@ private static Config getServerConfig(String mapName, URL configUrl, String sess
throw new ServletException(e);
}
}
if (sessionTTLConfig == null) {
sessionTTLConfig = SESSION_TTL_DEFAULT_SECONDS;
}
MapConfig mapConfig = config.getMapConfig(mapName);
try {
mapConfig.setMaxIdleSeconds(Integer.parseInt(sessionTTLConfig));
mapConfig.setMaxIdleSeconds(sessionTTLConfig);
} catch (NumberFormatException e) {
//noinspection ThrowableNotThrown
ExceptionUtil.rethrow(new InvalidConfigurationException("session-ttl-seconds must be a numeric value"));
}
return config;
Expand Down Expand Up @@ -164,22 +136,6 @@ private static HazelcastInstance createClientInstance(ClusteredSessionService se
return HazelcastClient.newHazelcastClient(clientConfig);
}

private static URL getConfigURL(final FilterConfig filterConfig, final String configLocation) throws ServletException {
URL configUrl = null;
try {
configUrl = filterConfig.getServletContext().getResource(configLocation);
} catch (MalformedURLException ignore) {
LOGGER.info("ignored MalformedURLException");
}
if (configUrl == null) {
configUrl = ConfigLoader.locateConfig(configLocation);
}
if (configUrl == null) {
throw new ServletException("Could not load configuration '" + configLocation + "'");
}
return configUrl;
}

private static boolean isEmpty(String s) {
return s == null || s.trim().length() == 0;
}
Expand Down
Loading

0 comments on commit ff07d03

Please sign in to comment.