Merge pull request #1372 from jin-z/jetty-9.4.x

Issue #856 Add server/port and auth configuration for mongo sessions
This commit is contained in:
Jan Bartel 2017-03-09 16:43:50 +11:00 committed by GitHub
commit 351a55f494
3 changed files with 30 additions and 13 deletions

View File

@ -36,7 +36,7 @@
<dependency>
<groupId>org.mongodb</groupId>
<artifactId>mongo-java-driver</artifactId>
<version>2.6.1</version>
<version>2.13.2</version>
<type>jar</type>
<scope>compile</scope>
</dependency>

View File

@ -11,7 +11,7 @@ session-store
sessions
[files]
maven://org.mongodb/mongo-java-driver/2.6.1|lib/nosql/mongo-java-driver-2.6.1.jar
maven://org.mongodb/mongo-java-driver/2.13.2|lib/nosql/mongo-java-driver-2.13.2.jar
[lib]
lib/jetty-nosql-${jetty.version}.jar

View File

@ -29,6 +29,7 @@ import org.eclipse.jetty.server.session.SessionDataStore;
import com.mongodb.Mongo;
import com.mongodb.MongoException;
import com.mongodb.MongoURI;
/**
* MongoSessionDataStoreFactory
@ -37,11 +38,12 @@ import com.mongodb.MongoException;
*/
public class MongoSessionDataStoreFactory extends AbstractSessionDataStoreFactory
{
String _dbName;
String _dbName;
String _collectionName;
String _host;
String _connectionString;
int _port = -1;
/**
* @return the host
*/
@ -74,10 +76,6 @@ public class MongoSessionDataStoreFactory extends AbstractSessionDataStoreFactor
_port = port;
}
/**
* @return the dbName
*/
@ -94,6 +92,22 @@ public class MongoSessionDataStoreFactory extends AbstractSessionDataStoreFactor
_dbName = dbName;
}
/**
* @return the connectionString
*/
public String getConnectionString()
{
return _connectionString;
}
/**
* @param connectionString the connection string to set. This has priority over dbHost and port
*/
public void setConnectionString(String connectionString)
{
_connectionString = connectionString;
}
/**
* @return the collectionName
*/
@ -110,10 +124,10 @@ public class MongoSessionDataStoreFactory extends AbstractSessionDataStoreFactor
_collectionName = collectionName;
}
/**
* @throws MongoException
* @throws UnknownHostException
/**
* @throws MongoException
* @throws UnknownHostException
* @see org.eclipse.jetty.server.session.SessionDataStoreFactory#getSessionDataStore(org.eclipse.jetty.server.session.SessionHandler)
*/
@Override
@ -122,7 +136,10 @@ public class MongoSessionDataStoreFactory extends AbstractSessionDataStoreFactor
MongoSessionDataStore store = new MongoSessionDataStore();
store.setGracePeriodSec(getGracePeriodSec());
Mongo mongo;
if (!StringUtil.isBlank(getHost()) && getPort() != -1)
if (!StringUtil.isBlank(getConnectionString()))
mongo = new Mongo(new MongoURI(getConnectionString()));
else if (!StringUtil.isBlank(getHost()) && getPort() != -1)
mongo = new Mongo(getHost(), getPort());
else if (!StringUtil.isBlank(getHost()))
mongo = new Mongo(getHost());