SHARE
TWEET

Untitled

a guest May 6th, 2015 282 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package io.vertx.ext.mail.impl;
  2.  
  3. import io.vertx.core.Context;
  4. import io.vertx.core.Handler;
  5. import io.vertx.core.Vertx;
  6. import io.vertx.core.impl.NoStackTraceThrowable;
  7. import io.vertx.core.logging.Logger;
  8. import io.vertx.core.logging.impl.LoggerFactory;
  9. import io.vertx.core.net.NetClient;
  10. import io.vertx.core.net.NetClientOptions;
  11. import io.vertx.ext.mail.MailConfig;
  12.  
  13. import java.util.ArrayList;
  14. import java.util.List;
  15.  
  16. class ConnectionPool {
  17.  
  18.   private static final Logger log = LoggerFactory.getLogger(ConnectionPool.class);
  19.  
  20.   private final List<SMTPConnection> connections;
  21.   private final Vertx vertx;
  22.   private NetClient netClient = null;
  23.   private final boolean stopped = false;
  24.   private final MailConfig config;
  25.   private final Context context;
  26.  
  27.   ConnectionPool(Vertx vertx, MailConfig config, Context context) {
  28.     this.vertx = vertx;
  29.     connections = new ArrayList<SMTPConnection>();
  30.     this.context = context;
  31.     this.config = config;
  32.   }
  33.  
  34.   private void createNetclient(Handler<Void> finishedHandler) {
  35.     NetClientOptions netClientOptions = new NetClientOptions().setSsl(config.isSsl());
  36.     context.runOnContext(v -> {
  37.       netClient = vertx.createNetClient(netClientOptions);
  38.       finishedHandler.handle(null);
  39.     });
  40.   }
  41.  
  42.   void getConnection(MailConfig config, Handler<SMTPConnection> resultHandler, Handler<Throwable> errorHandler) {
  43.     log.debug("getConnection()");
  44.     if (stopped) {
  45.       errorHandler.handle(new NoStackTraceThrowable("connection pool is stopped"));
  46.     } else {
  47.       if(connections.size()==0) {
  48.         createNewConnection(config, resultHandler, errorHandler);
  49.       } else {
  50.         findUsableConnection(connections, config, 0, resultHandler, v -> {
  51.           log.debug("no usable connection found, createNewConnection()");
  52.           createNewConnection(config, resultHandler, errorHandler);
  53.         });
  54.       }
  55.     }
  56.   }
  57.  
  58.   /**
  59.    * set up NetClient and create a connection.
  60.    *
  61.    * @param config
  62.    * @param resultHandler
  63.    * @param errorHandler
  64.    */
  65.   private void createNewConnection(MailConfig config, Handler<SMTPConnection> resultHandler,
  66.       Handler<Throwable> errorHandler) {
  67.     log.debug("creating new connection");
  68.     // if we have not yet created the netclient, do that first
  69.     if(netClient == null) {
  70.       createNetclient(v -> {
  71.         createConnection(resultHandler, errorHandler);
  72.       });
  73.     } else {
  74.       createConnection(resultHandler, errorHandler);
  75.     }
  76.   }
  77.  
  78.   /**
  79.    * really open the connection.
  80.    *
  81.    * @param resultHandler
  82.    * @param errorHandler
  83.    */
  84.   private void createConnection(Handler<SMTPConnection> resultHandler,
  85.       Handler<Throwable> errorHandler) {
  86.     SMTPConnection conn = new SMTPConnection(netClient, context);
  87.     connections.add(conn);
  88.     new SMTPStarter(vertx, conn, config, v -> resultHandler.handle(conn), errorHandler).connect();
  89.   }
  90.  
  91.   private void findUsableConnection(List<SMTPConnection> connections, MailConfig config, int i,
  92.       Handler<SMTPConnection> foundHandler, Handler<Void> notFoundHandler) {
  93.     if (i == connections.size()) {
  94.       notFoundHandler.handle(null);
  95.     } else {
  96.       log.debug("findUsableConnection(" + i + ")");
  97.       SMTPConnection conn = connections.get(i);
  98.       if (!conn.isBroken() && conn.isIdle()) {
  99.         conn.useConnection();
  100.         new SMTPReset(conn, config, v -> foundHandler.handle(conn), v -> {
  101.           // make sure we do not get confused by a close event later
  102.           conn.setBroken();
  103.           findUsableConnection(connections, config, i + 1, foundHandler, notFoundHandler);
  104.         }).rsetCmd();
  105.       } else {
  106.         findUsableConnection(connections, config, i + 1, foundHandler, notFoundHandler);
  107.       }
  108.     }
  109.   }
  110.  
  111.   void stop() {
  112.     stop(v -> {
  113.     });
  114.   }
  115.  
  116.   void stop(Handler<Void> finishedHandler) {
  117.     shutdownConnections(0, finishedHandler);
  118.   }
  119.  
  120.   private void shutdownConnections(int i, Handler<Void> finishedHandler) {
  121.     if(netClient != null) {
  122.       netClient.close();
  123.     }
  124.     if (i == connections.size()) {
  125.       finishedHandler.handle(null);
  126.     } else {
  127.       log.debug("STMPConnection.shutdown(" + i + ")");
  128.       SMTPConnection connection = connections.get(i);
  129.       // TODO: have to wait for active connections still running
  130.       // they will shut down when the operation is finished
  131.       if (connection.isIdle()) {
  132.         if (connection.isBroken()) {
  133.           connection.shutdown();
  134.           shutdownConnections(i + 1, finishedHandler);
  135.         } else {
  136.           connection.setBroken();
  137.           log.debug("shutting down connection");
  138.           new SMTPQuit(connection, v -> {
  139.             connection.shutdown();
  140.             log.debug("connection is shut down");
  141.             shutdownConnections(i + 1, finishedHandler);
  142.           }).quitCmd();
  143.         }
  144.       } else {
  145.         shutdownConnections(i + 1, finishedHandler);
  146.       }
  147.     }
  148.   }
  149.  
  150. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top