Skip to content

Commit

Permalink
WFLY-1815 Distributable SessionManager.getSession(String) implementat…
Browse files Browse the repository at this point in the history
…ion is wrong

Drop more obsolete dependencies.
More code cleanup.
  • Loading branch information
pferraro committed Aug 9, 2013
1 parent 6164704 commit 91c1772
Show file tree
Hide file tree
Showing 24 changed files with 623 additions and 171 deletions.
Expand Up @@ -37,7 +37,6 @@
<module name="io.undertow.core"/>
<module name="io.undertow.servlet"/>
<module name="org.jboss.logging"/>
<module name="org.jboss.metadata"/>
<module name="org.wildfly.clustering.web.spi"/>
</dependencies>
</module>
Expand Up @@ -21,6 +21,7 @@
*/
package org.wildfly.clustering.web.infinispan.session;

import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
Expand All @@ -37,6 +38,7 @@
import org.infinispan.Cache;
import org.infinispan.affinity.KeyAffinityService;
import org.infinispan.affinity.KeyGenerator;
import org.infinispan.context.Flag;
import org.infinispan.distribution.DistributionManager;
import org.infinispan.notifications.Listener;
import org.infinispan.notifications.cachelistener.annotation.CacheEntryActivated;
Expand All @@ -46,7 +48,6 @@
import org.infinispan.notifications.cachelistener.event.CacheEntryPassivatedEvent;
import org.infinispan.notifications.cachelistener.event.CacheEntryRemovedEvent;
import org.infinispan.remoting.transport.Address;
import org.infinispan.util.concurrent.ConcurrentHashSet;
import org.jboss.as.clustering.infinispan.affinity.KeyAffinityServiceFactory;
import org.jboss.as.clustering.registry.Registry;
import org.jboss.metadata.web.jboss.JBossWebMetaData;
Expand Down Expand Up @@ -77,7 +78,6 @@ public class InfinispanSessionManager<V, L> implements SessionManager<L>, KeyGen
private final Registry<String, Void> registry;
private final List<Scheduler<Session<L>>> schedulers = new CopyOnWriteArrayList<>();
private volatile Time defaultMaxInactiveInterval = new Time(30, TimeUnit.MINUTES);
private final Set<String> activeSessions = new ConcurrentHashSet<>();
private final int maxActiveSessions;

public InfinispanSessionManager(SessionContext context, SessionIdentifierFactory idFactory, Cache<String, V> cache, SessionFactory<V, L> factory, KeyAffinityServiceFactory affinityFactory, Registry<String, Void> registry, JBossWebMetaData metaData) {
Expand Down Expand Up @@ -189,7 +189,6 @@ public Session<L> findSession(String id) {
for (Scheduler<Session<L>> scheduler: this.schedulers) {
scheduler.cancel(session);
}
this.activeSessions.add(id);
return new SchedulableSession<>(session, this.schedulers);
}

Expand All @@ -198,15 +197,38 @@ public Session<L> createSession(String id) {
Session<L> session = this.factory.createSession(id, this.factory.createValue(id));
final Time time = this.defaultMaxInactiveInterval;
session.getMetaData().setMaxInactiveInterval(time.getValue(), time.getUnit());
this.activeSessions.add(id);
return new SchedulableSession<>(session, this.schedulers);
}

@Override
public int size() {
return this.activeSessions.size();
public ImmutableSession viewSession(String id) {
V value = this.factory.findValue(id);
return (value != null) ? new SimpleImmutableSession(this.factory.createImmutableSession(id, value)) : null;
}

@Override
public Set<String> getActiveSessions() {
// Omit remote (i.e. when using DIST mode) and passivated sessions
return this.getSessions(Flag.CACHE_MODE_LOCAL, Flag.SKIP_CACHE_LOAD);
}

@Override
public Set<String> getLocalSessions() {
// Omit remote sessions (i.e. when using DIST mode)
return this.getSessions(Flag.CACHE_MODE_LOCAL);
}

private Set<String> getSessions(Flag... flags) {
Set<String> result = new HashSet<>();
for (Object key: this.cache.getAdvancedCache().withFlags(flags).keySet()) {
if (key instanceof String) {
result.add((String) key);
}
}
return result;
}

@SuppressWarnings("cast")
@CacheEntryActivated
public void activated(CacheEntryActivatedEvent<String, ?> event) {
// Cache may contain non-string keys, so ignore any others
Expand All @@ -227,12 +249,12 @@ public void activated(CacheEntryActivatedEvent<String, ?> event) {
}
}

@SuppressWarnings("cast")
@CacheEntryPassivated
public void passivated(CacheEntryPassivatedEvent<String, ?> event) {
// Cache may contain non-string keys, so ignore any others
if (event.isPre() && (event.getKey() instanceof String)) {
String id = event.getKey();
this.activeSessions.remove(id);
if (event.isOriginLocal()) {
InfinispanWebLogger.ROOT_LOGGER.tracef("Session %s will be passivated", id);
ImmutableSession session = this.factory.createSession(id, this.factory.findValue(id));
Expand All @@ -250,12 +272,12 @@ public void passivated(CacheEntryPassivatedEvent<String, ?> event) {
}
}

@SuppressWarnings("cast")
@CacheEntryRemoved
public void removed(CacheEntryRemovedEvent<String, ?> event) {
// Cache may contain non-string keys, so ignore any others
if (event.isPre() && event.isOriginLocal() && (event.getKey() instanceof String)) {
String id = event.getKey();
this.activeSessions.remove(id);
if (event.isOriginLocal()) {
InfinispanWebLogger.ROOT_LOGGER.tracef("Session %s will be removed", id);
ImmutableSession session = this.factory.createImmutableSession(id, this.factory.findValue(id));
Expand Down
@@ -0,0 +1,73 @@
/*
* JBoss, Home of Professional Open Source.
* Copyright 2013, Red Hat, Inc., and individual contributors
* as indicated by the @author tags. See the copyright.txt file in the
* distribution for a full listing of individual contributors.
*
* This is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this software; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
*/
package org.wildfly.clustering.web.infinispan.session;

import org.wildfly.clustering.web.session.ImmutableSession;
import org.wildfly.clustering.web.session.ImmutableSessionAttributes;
import org.wildfly.clustering.web.session.ImmutableSessionMetaData;
import org.wildfly.clustering.web.session.SessionContext;

/**
* An immutable "snapshot" of a session which can be accessed outside the scope of a transaction.
* @author Paul Ferraro
*/
public class SimpleImmutableSession implements ImmutableSession {

private final String id;
private final boolean valid;
private final ImmutableSessionMetaData metaData;
private final ImmutableSessionAttributes attributes;
private final SessionContext context;

public SimpleImmutableSession(ImmutableSession session) {
this.id = session.getId();
this.valid = session.isValid();
this.metaData = new SimpleImmutableSessionMetaData(session.getMetaData());
this.attributes = new SimpleImmutableSessionAttributes(session.getAttributes());
this.context = session.getContext();
}

@Override
public String getId() {
return this.id;
}

@Override
public ImmutableSessionMetaData getMetaData() {
return this.metaData;
}

@Override
public boolean isValid() {
return this.valid;
}

@Override
public ImmutableSessionAttributes getAttributes() {
return this.attributes;
}

@Override
public SessionContext getContext() {
return this.context;
}
}
@@ -0,0 +1,56 @@
/*
* JBoss, Home of Professional Open Source.
* Copyright 2013, Red Hat, Inc., and individual contributors
* as indicated by the @author tags. See the copyright.txt file in the
* distribution for a full listing of individual contributors.
*
* This is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this software; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
*/
package org.wildfly.clustering.web.infinispan.session;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

import org.wildfly.clustering.web.session.ImmutableSessionAttributes;

/**
* An immutable "snapshot" of a session's attributes which can be accessed outside the scope of a transaction.
* @author Paul Ferraro
*/
public class SimpleImmutableSessionAttributes implements ImmutableSessionAttributes {

private final Map<String, Object> attributes;

public SimpleImmutableSessionAttributes(ImmutableSessionAttributes attributes) {
Map<String, Object> map = new HashMap<>();
for (String name: attributes.getAttributeNames()) {
this.attributes.put(name, attributes.getAttribute(name));
}
this.attributes = Collections.unmodifiableMap(map);
}

@Override
public Set<String> getAttributeNames() {
return this.attributes.keySet();
}

@Override
public Object getAttribute(String name) {
return this.attributes.get(name);
}
}
@@ -0,0 +1,73 @@
/*
* JBoss, Home of Professional Open Source.
* Copyright 2013, Red Hat, Inc., and individual contributors
* as indicated by the @author tags. See the copyright.txt file in the
* distribution for a full listing of individual contributors.
*
* This is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this software; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
*/
package org.wildfly.clustering.web.infinispan.session;

import java.util.Date;
import java.util.concurrent.TimeUnit;

import org.wildfly.clustering.web.session.ImmutableSessionMetaData;

/**
* An immutable "snapshot" of a session's meta-data which can be accessed outside the scope of a transaction.
* @author Paul Ferraro
*/
public class SimpleImmutableSessionMetaData implements ImmutableSessionMetaData {

private final boolean isNew;
private final boolean expired;
private final Date creationTime;
private final Date lastAccessedTime;
private final long maxInactiveInterval;

public SimpleImmutableSessionMetaData(ImmutableSessionMetaData metaData) {
this.isNew = metaData.isNew();
this.expired = metaData.isExpired();
this.creationTime = metaData.getCreationTime();
this.lastAccessedTime = metaData.getLastAccessedTime();
this.maxInactiveInterval = metaData.getMaxInactiveInterval(TimeUnit.MILLISECONDS);
}

@Override
public boolean isNew() {
return this.isNew;
}

@Override
public boolean isExpired() {
return this.expired;
}

@Override
public Date getCreationTime() {
return this.creationTime;
}

@Override
public Date getLastAccessedTime() {
return this.lastAccessedTime;
}

@Override
public long getMaxInactiveInterval(TimeUnit unit) {
return unit.convert(this.maxInactiveInterval, TimeUnit.MILLISECONDS);
}
}
Expand Up @@ -48,7 +48,7 @@ public void writeObject(ObjectOutput output, SimpleSessionMetaData metaData) thr
}

@Override
public SimpleSessionMetaData readObject(ObjectInput input) throws IOException, ClassNotFoundException {
public SimpleSessionMetaData readObject(ObjectInput input) throws IOException {
Date creationTime = new Date(input.readLong());
Date lastAccessedTime = new Date(input.readLong());
Time maxInactiveInterval = new Time(input.readInt(), TimeUnit.SECONDS);
Expand Down
Expand Up @@ -53,7 +53,7 @@ public void writeObject(ObjectOutput output, CoarseSessionCacheEntry<Object> ent
}

@Override
public CoarseSessionCacheEntry<Object> readObject(ObjectInput input) throws IOException, ClassNotFoundException {
public CoarseSessionCacheEntry<Object> readObject(ObjectInput input) throws IOException {
return new CoarseSessionCacheEntry<>(this.externalizer.readObject(input));
}
}
Expand Up @@ -44,7 +44,7 @@ public void writeObject(ObjectOutput output, SessionAttributesCacheKey key) thro
}

@Override
public SessionAttributesCacheKey readObject(ObjectInput input) throws IOException, ClassNotFoundException {
public SessionAttributesCacheKey readObject(ObjectInput input) throws IOException {
return new SessionAttributesCacheKey(input.readUTF());
}
}
Expand Up @@ -59,7 +59,7 @@ public void writeObject(ObjectOutput output, FineSessionCacheEntry<Object> entry
}

@Override
public FineSessionCacheEntry<Object> readObject(ObjectInput input) throws IOException, ClassNotFoundException {
public FineSessionCacheEntry<Object> readObject(ObjectInput input) throws IOException {
FineSessionCacheEntry<Object> entry = new FineSessionCacheEntry<>(this.externalizer.readObject(input));
Set<String> attributes = entry.getAttributes();
int size = input.readInt();
Expand Down
Expand Up @@ -45,7 +45,7 @@ public void writeObject(ObjectOutput output, SessionAttributeCacheKey key) throw
}

@Override
public SessionAttributeCacheKey readObject(ObjectInput input) throws IOException, ClassNotFoundException {
public SessionAttributeCacheKey readObject(ObjectInput input) throws IOException {
return new SessionAttributeCacheKey(input.readUTF(), input.readUTF());
}
}
Expand Up @@ -59,7 +59,7 @@ public void writeObject(ObjectOutput output, CoarseSSOCacheEntry<?> entry) throw
}

@Override
public CoarseSSOCacheEntry<?> readObject(ObjectInput input) throws IOException, ClassNotFoundException {
public CoarseSSOCacheEntry<?> readObject(ObjectInput input) throws IOException {
CoarseSSOCacheEntry<?> entry = new CoarseSSOCacheEntry<>();
entry.setAuthenticationType(AuthenticationType.values()[input.readByte()]);
entry.setUser(input.readUTF());
Expand Down

0 comments on commit 91c1772

Please sign in to comment.