1515import javax .enterprise .inject .Produces ;
1616import javax .annotation .PostConstruct ;
1717import javax .inject .Inject ;
18+ import javax .net .ssl .SSLContext ;
1819
1920import com .ibm .websphere .crypto .PasswordUtil ;
2021import com .mongodb .MongoClient ;
@@ -39,9 +40,12 @@ public class MongoProducer {
3940 private static final Logger logger = Logger .getLogger (MongoProducer .class .getCanonicalName ());
4041
4142 private String dbName = null ;
43+ private String authDbName = null ;
4244 private String user = null ;
4345 private String encodedPass = null ;
4446 private String requestedWriteConcern = null ;
47+ private boolean sslEnabled = false ;
48+ private String sslConfig = null ;
4549 private ArrayList <ServerAddress > servers = new ArrayList <ServerAddress >(2 );
4650
4751 @ PostConstruct
@@ -54,10 +58,22 @@ private void readConfig() {
5458 // user and password (optional - if not set, use unauthenticated access)
5559 user = sysprops .getProperty ("lars.mongo.user" );
5660 encodedPass = sysprops .getProperty ("lars.mongo.pass.encoded" );
61+ authDbName = sysprops .getProperty ("lars.mongo.authdb" );
62+ if (authDbName == null ) {
63+ authDbName = dbName ;
64+ }
5765
5866 // writeConcern (optional - if not set use the default "ACKNOWLEDGED")
5967 requestedWriteConcern = sysprops .getProperty ("lars.mongo.writeConcern" );
6068
69+ // sslEnabled (optional - if not set, assume false)
70+ if ("true" .equalsIgnoreCase (sysprops .getProperty ("lars.mongo.sslEnabled" ,"false" ))) {
71+ sslEnabled = true ;
72+ }
73+
74+ // sslConfig (optional, only used if ssl is enabled)
75+ sslConfig = sysprops .getProperty ("lars.mongo.sslConfig" );
76+
6177 // look for all lars.mongo.hostname* properties, in alphabetical order
6278 Enumeration keysEnum = sysprops .keys ();
6379 Vector <String > keyList = new Vector <String >();
@@ -89,7 +105,9 @@ private void readConfig() {
89105
90106 @ Produces
91107 public MongoClient createMongo () {
92- MongoClientOptions opts ;
108+ MongoClientOptions .Builder builder = MongoClientOptions .builder ();
109+
110+ // set the WriteConcern, if specified
93111 if (requestedWriteConcern != null ) {
94112 WriteConcern wc ;
95113 switch (requestedWriteConcern )
@@ -131,26 +149,48 @@ public MongoClient createMongo() {
131149 wc = WriteConcern .ACKNOWLEDGED ;
132150 logger .warning ("No WriteConcern named " + requestedWriteConcern + " found. Using default WriteConcern of ACKNOWLEDGED." );
133151 }
134- opts = new MongoClientOptions . Builder (). writeConcern (wc ). build ( );
152+ builder = builder . writeConcern (wc );
135153 logger .info ("createMongo: using write concern " + requestedWriteConcern );
136154 } else {
137- opts = new MongoClientOptions .Builder ().build ();
138155 logger .info ("createMongo: using default write concern" );
139156 }
140157
141- if (encodedPass == null ) {
142- logger .info ("createMongo: connecting to database " +dbName +" using unauthenticated access" );
158+ // Configure SSL
159+ if (sslEnabled ) {
160+ try {
161+ SSLContext sslContext ;
162+ if (sslConfig == null ) {
163+ sslContext = SSLContext .getDefault ();
164+ } else {
165+ sslContext = com .ibm .websphere .ssl .JSSEHelper .getInstance ().getSSLContext (sslConfig , Collections .emptyMap (), null );
166+ }
167+ logger .info ("createMongo: SSL enabled" );
168+ builder = builder .sslEnabled (sslEnabled ).sslContext (sslContext );
169+ } catch (com .ibm .websphere .ssl .SSLException ex ) {
170+ logger .severe ("createMongo: Failed to initialize SSL: " +ex .getMessage ());
171+ return null ;
172+ } catch (java .security .NoSuchAlgorithmException ex ) {
173+ logger .severe ("createMongo: Failed to initialize SSL: " +ex .getMessage ());
174+ return null ;
175+ }
176+ }
177+ MongoClientOptions opts = builder .build ();
178+
179+ // Configure credentials, and connect
180+ if (encodedPass == null ) {
181+ logger .info ("createMongo: connecting using unauthenticated access" );
143182 return new MongoClient (servers , opts );
144183 } else {
145184 String password = PasswordUtil .passwordDecode (encodedPass );
146- MongoCredential creds = MongoCredential .createCredential (user , dbName , password .toCharArray ());
147- logger .info ("createMongo: connecting to database " +dbName +" as user " + user );
185+ MongoCredential creds = MongoCredential .createCredential (user , authDbName , password .toCharArray ());
186+ logger .info ("createMongo: connecting using user " +user +" and authentication database " + authDbName );
148187 return new MongoClient (servers , creds , opts );
149188 }
150189 }
151190
152191 @ Produces
153192 public DB createDB (MongoClient client ) {
193+ logger .info ("createMongo: connecting to database " +dbName );
154194 return client .getDB (dbName );
155195 }
156196
0 commit comments