| 1 |
// |
| 2 |
// ======================================================================== |
| 3 |
// Copyright (c) 1995-2022 Mort Bay Consulting Pty Ltd and others. |
| 4 |
// ------------------------------------------------------------------------ |
| 5 |
// All rights reserved. This program and the accompanying materials |
| 6 |
// are made available under the terms of the Eclipse Public License v1.0 |
| 7 |
// and Apache License v2.0 which accompanies this distribution. |
| 8 |
// |
| 9 |
// The Eclipse Public License is available at |
| 10 |
// http://www.eclipse.org/legal/epl-v10.html |
| 11 |
// |
| 12 |
// The Apache License v2.0 is available at |
| 13 |
// http://www.opensource.org/licenses/apache2.0.php |
| 14 |
// |
| 15 |
// You may elect to redistribute this code under either of these licenses. |
| 16 |
// ======================================================================== |
| 17 |
// |
| 18 |
|
| 19 |
package org.eclipse.jetty.session.infinispan; |
| 20 |
|
| 21 |
import java.util.List; |
| 22 |
import java.util.Set; |
| 23 |
|
| 24 |
import org.eclipse.jetty.server.session.SessionData; |
| 25 |
import org.infinispan.client.hotrod.RemoteCache; |
| 26 |
import org.infinispan.client.hotrod.Search; |
| 27 |
import org.infinispan.query.dsl.Query; |
| 28 |
import org.infinispan.query.dsl.QueryFactory; |
| 29 |
import org.infinispan.query.dsl.QueryResult; |
| 30 |
|
| 31 |
import static java.util.stream.Collectors.toSet; |
| 32 |
|
| 33 |
/** |
| 34 |
* RemoteQueryManager |
| 35 |
* |
| 36 |
* A QueryManager impl that supports doing queries against remote infinispan server. |
| 37 |
*/ |
| 38 |
public class RemoteQueryManager implements QueryManager |
| 39 |
{ |
| 40 |
private RemoteCache<String, SessionData> _cache; |
| 41 |
private QueryFactory _factory; |
| 42 |
|
| 43 |
public RemoteQueryManager(RemoteCache<String, SessionData> cache) |
| 44 |
{ |
| 45 |
_cache = cache; |
| 46 |
_factory = Search.getQueryFactory(_cache); |
| 47 |
} |
| 48 |
|
| 49 |
@Override |
| 50 |
public Set<String> queryExpiredSessions(long time) |
| 51 |
{ |
| 52 |
Query<InfinispanSessionData> expiredQuery = _factory.create("select id from org_eclipse_jetty_session_infinispan.InfinispanSessionData where " + |
| 53 |
"expiry <= :expiry and expiry > 0"); |
| 54 |
expiredQuery.setParameter("expiry", time); |
| 55 |
|
| 56 |
@SuppressWarnings("rawtypes") |
| 57 |
QueryResult result = expiredQuery.execute(); |
| 58 |
List<Object[]> list = result.list(); |
| 59 |
Set<String> ids = list.stream().map(a -> (String)a[0]).collect(toSet()); |
| 60 |
return ids; |
| 61 |
} |
| 62 |
|
| 63 |
@Override |
| 64 |
public Set<String> queryExpiredSessions() |
| 65 |
{ |
| 66 |
return queryExpiredSessions(System.currentTimeMillis()); |
| 67 |
} |
| 68 |
} |