EmberSupport xref

View Javadoc

    1   /***
    2    *
    3    * Copyright 2004 Protique Ltd
    4    *
    5    * Licensed under the Apache License, Version 2.0 (the "License");
    6    * you may not use this file except in compliance with the License.
    7    * You may obtain a copy of the License at
    8    *
    9    * http://www.apache.org/licenses/LICENSE-2.0
    10   *
    11   * Unless required by applicable law or agreed to in writing, software
    12   * distributed under the License is distributed on an "AS IS" BASIS,
    13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    14   * See the License for the specific language governing permissions and
    15   * limitations under the License.
    16   *
    17   **/
    18  package org.codehaus.activemq.transport.ember;
    19
    20  import pyrasun.eio.EIOEvent;
    21  import pyrasun.eio.EIOEventDescriptor;
    22  import pyrasun.eio.EIOGlobalContext;
    23  import pyrasun.eio.EIOPoolingStrategy;
    24  import pyrasun.eio.services.EmberServiceController;
    25  import pyrasun.eio.services.EmberServiceControllerImpl;
    26
    27  import javax.jms.JMSException;
    28  import java.io.IOException;
    29
    30  /***
    31   * An abstract base class useful for implementation inheritence
    32   *
    33   * @version $Revision: 1.7 $
    34   */
    35  public class EmberSupport {
    36
    37      private EIOGlobalContext context;
    38      private String key = "emberIo";
    39      private int maxEm = 1;
    40      private EmberServiceControllerImpl controller;
    41      private EIOPoolingStrategy ioPoolingStrategy;
    42
    43      /***
    44       * TODO when EmberIO can support flushing when in NIO mode
    45       */
    46      //private String poolingStrategyName = "USE_RWP_POOLS";
    47
    48      private String poolingStrategyName = "SELECTOR_READER";
    49
    50      public EmberSupport() {
    51      }
    52
    53      public EmberSupport(EIOGlobalContext context, EIOPoolingStrategy ioPoolingStrategy) {
    54          this.context = context;
    55          this.ioPoolingStrategy = ioPoolingStrategy;
    56      }
    57
    58      public EIOGlobalContext getContext() throws IOException {
    59          if (context == null) {
    60              context = new EIOGlobalContext(maxEm, key);
    61          }
    62          return context;
    63      }
    64
    65      public String getKey() {
    66          return key;
    67      }
    68
    69      protected EmberServiceController getController() throws IOException {
    70          if (controller == null) {
    71              controller = new EmberServiceControllerImpl(getContext());
    72          }
    73          return controller;
    74      }
    75
    76      protected EIOPoolingStrategy getIoPoolingStrategy() {
    77          if (ioPoolingStrategy == null) {
    78              ioPoolingStrategy = getPoolingStrategyByName(poolingStrategyName);
    79          }
    80          return ioPoolingStrategy;
    81      }
    82
    83      protected EIOPoolingStrategy getPoolingStrategyByName(String name) {
    84          EIOPoolingStrategy strategy = EIOPoolingStrategy.getStrategyByName(name);
    85
    86          // lets default some pool sizes
    87          EIOEventDescriptor evRead = strategy.getEventDescriptor(EIOEvent.READ);
    88          evRead.setPoolSize(1);
    89
    90          EIOEventDescriptor evWrite = strategy.getEventDescriptor(EIOEvent.WRITE);
    91          evWrite.setPoolSize(1);
    92
    93          EIOEventDescriptor evProcess = strategy.getEventDescriptor(EIOEvent.PROCESS);
    94          evProcess.setPoolSize(1);
    95          return strategy;
    96      }
    97
    98      /***
    99       * Factory method to create a JMSException which is linked to the base exception
    100      */
    101     protected JMSException createJMSException(String message, Exception ex) {
    102         JMSException jmsEx = new JMSException(message + ex.getMessage());
    103         jmsEx.setLinkedException(ex);
    104         return jmsEx;
    105     }
    106
    107 }