/*
According to JMS 2.0 spec jmsexception is not throwm back
to the caller.Hence any exception will be absorbed and FFDC
will be logged

(non-Javadoc)

@see javax.jms.JMSContext#createProducer()
161388
@Override
    public JMSProducer createProducer() {

        if (TraceComponent.isAnyTracingEnabled() && tc.isEntryEnabled())
            SibTr.entry(this, tc, "createProducer");

        JMSProducer jmsProducer = null;
        try {
            // Create messageproducer first, since we reuse most in jmsproducer
            // destination is passed as null because during send,destination is passed
            // jmssession.createproducer() is not used becuase it stores the list of producers
            // which is not required for simplified API's
            MessageProducer msgProducer = jmsSession.instantiateProducer(null);
            jmsProducer = new JmsJMSProducerImpl(msgProducer);
        } catch (JMSException e) {
            // should never have got here because we pass destination as null so there is no chance 
            // that instantiateProducer() will throw any exception

            FFDCFilter.processException(e,
                                        JmsJMSContextImpl.class.toString() + ".createProducer",
                                        "598", this);
        }
        if (TraceComponent.isAnyTracingEnabled() && tc.isEntryEnabled())
            SibTr.exit(this, tc, "createProducer");

        return jmsProducer;

    }
779967
void removeProducer(JmsMsgProducerImpl producer) {
        if (TraceComponent.isAnyTracingEnabled() && tc.isEntryEnabled())
            SibTr.entry(this, tc, "removeProducer", producer);
        producers.remove(producer);
        if (TraceComponent.isAnyTracingEnabled() && tc.isEntryEnabled())
            SibTr.exit(this, tc, "removeProducer");
    }
1013282
public final ControlFlushed createNewControlFlushed() throws MessageCreateFailedException {
    if (TraceComponent.isAnyTracingEnabled() && tc.isEntryEnabled()) SibTr.entry(tc, "createNewControlFlushed");
    ControlFlushed msg = null;
    try {
      msg = new ControlFlushedImpl(MfpConstants.CONSTRUCTOR_NO_OP);
    }
    catch (MessageDecodeFailedException e) {
      /* No need to FFDC this as JsMsgObject will already have done so */
      // No FFDC code needed
      throw new MessageCreateFailedException(e);
    }
    if (TraceComponent.isAnyTracingEnabled() && tc.isEntryEnabled()) SibTr.exit(tc, "createNewControlFlushed");
    return msg;
  }
874259
public TrmMeConnectReply createNewTrmMeConnectReply() throws MessageCreateFailedException {
    if (TraceComponent.isAnyTracingEnabled() && tc.isEntryEnabled()) SibTr.entry(tc, "createNewTrmMeConnectReply");
    TrmMeConnectReply msg = null;
    try {
      msg = new TrmMeConnectReplyImpl();
    }
    catch (MessageDecodeFailedException e) {
      /* No need to FFDC this as JsMsgObject will already have done so */
      // No FFDC code needed
      throw new MessageCreateFailedException(e);
    }
    if (TraceComponent.isAnyTracingEnabled() && tc.isEntryEnabled()) SibTr.exit(tc, "createNewTrmMeConnectReply");
    return msg;
  }
602857
public void createControlAdapter()
  {
    if (TraceComponent.isAnyTracingEnabled() && tc.isEntryEnabled())
      SibTr.entry(tc, "createControlAdapter");
    
    DestinationHandler dh = null;
    try
    {
      ItemStream is = getItemStream();
      
      // TODO - This method is using the wrong itemstream
  
      dh = ((AOProtocolItemStream) is).getDestinationHandler();
      SIMPMessage msg = (SIMPMessage) is.findById(msgId);
      controlAdapter = new QueuedMessage(msg,dh,is);
    }
    catch(Exception e)
    {
      // FFDC
      FFDCFilter.processException(
          e,
          "com.ibm.ws.sib.processor.impl.store.items.AOValue.createControlAdapter",
          "1:371:1.28.1.5",
          this);  
                  
      SibTr.exception(tc, e); 
    }
         
    if (TraceComponent.isAnyTracingEnabled() && tc.isEntryEnabled())
      SibTr.exit(tc, "createControlAdapter");
  }
20899
@Override
	public synchronized void start() throws JMSException
    {
    	if (started)
    		return;
    	
    	log.info("Starting listener ["+getName()+"]");
    	
    	stopRequired = false;
    	
    	initServerSocket();
    	listenerThread = new Thread(this,"FFMQ-TCP-Server-"+serverSocket.getLocalPort());
    	listenerThread.start();
    	
    	started = true;
    }
540063
private void createSingleListener(final SibRaMessagingEngineConnection connection) throws ResourceException
    {
        final String methodName = "createSingleListener";
        if (TraceComponent.isAnyTracingEnabled() && TRACE.isEntryEnabled())
        {
            SibTr.entry(this, TRACE, methodName, connection);
        }

        final SIDestinationAddress destination = _endpointConfiguration.getDestination();

        if (TraceComponent.isAnyTracingEnabled() && TRACE.isDebugEnabled())
        {
            SibTr.debug(TRACE, "Creating a consumer to consume from destination "
                               + destination + " on ME " + connection.getConnection().getMeName());
        }

        try
        {
            connection.createListener(destination, _messageEndpointFactory);
            SibTr.info(TRACE, "CONNECTED_CWSIV0777", new Object[] {
                                                                   connection.getConnection().getMeName(),
                                                                   _endpointConfiguration.getDestination()
                                                                                   .getDestinationName(),
                                                                   _endpointConfiguration.getBusName() });
        } catch (final IllegalStateException exception)
        {
            // No FFDC code needed
            if (TraceComponent.isAnyTracingEnabled() && TRACE.isDebugEnabled())
            {
                SibTr.debug(TRACE, "Failed to create a session - blowing away the connection - rethrow the exception");
            }

            _connections.remove(connection.getConnection().getMeUuid());
            connection.close();
            if (TraceComponent.isAnyTracingEnabled() && TRACE.isEntryEnabled())
            {
                SibTr.exit(this, TRACE, methodName);
            }
            throw exception;
        } catch (final ResourceException exception)
        {
            // No FFDC code needed

            // Failed to create a consumer so blow away the connection and try
            // again
            if (TraceComponent.isAnyTracingEnabled() && TRACE.isDebugEnabled())
            {
                SibTr.debug(TRACE, "Failed to create a session - blowing away the connection - a retry should occur");
                SibTr.debug(TRACE, "Exception cause was " + exception.getCause());
            }

            _connections.remove(connection.getConnection().getMeUuid());
            connection.close();
            throw exception;
        }

        if (TraceComponent.isAnyTracingEnabled() && TRACE.isEntryEnabled()) {
            SibTr.exit(this, TRACE, methodName);
        }
    }
921515
public final ControlRequestFlush createNewControlRequestFlush() throws MessageCreateFailedException {
    if (TraceComponent.isAnyTracingEnabled() && tc.isEntryEnabled()) SibTr.entry(tc, "createNewControlRequestFlush");
    ControlRequestFlush msg = null;
    try {
      msg = new ControlRequestFlushImpl(MfpConstants.CONSTRUCTOR_NO_OP);
    }
    catch (MessageDecodeFailedException e) {
      /* No need to FFDC this as JsMsgObject will already have done so */
      // No FFDC code needed
      throw new MessageCreateFailedException(e);
    }
    if (TraceComponent.isAnyTracingEnabled() && tc.isEntryEnabled()) SibTr.exit(tc, "createNewControlRequestFlush");
    return msg;
  }
94556
@Override
	public QueueReceiver createReceiver(Queue queue, String messageSelector) throws JMSException
    {
    	externalAccessLock.readLock().lock();
    	try
		{
	        checkNotClosed();
	        LocalQueueReceiver receiver = new LocalQueueReceiver(engine,this,queue,messageSelector,idProvider.createID());
	        registerConsumer(receiver);
	        receiver.initDestination();
	        return receiver;
		}
    	finally
    	{
    		externalAccessLock.readLock().unlock();
    	}
    }
729768
public JsJmsMapMessage createJmsMapMessage() throws MessageCreateFailedException {
    if (TraceComponent.isAnyTracingEnabled() && tc.isEntryEnabled()) SibTr.entry(tc, "createJmsMapMessage");
    JsJmsMapMessage msg = null;
    try {
      msg = new JsJmsMapMessageImpl(MfpConstants.CONSTRUCTOR_NO_OP);
    }
    catch (MessageDecodeFailedException e) {
      /* No need to FFDC this as JsMsgObject will already have done so */
      // No FFDC code needed
      throw new MessageCreateFailedException(e);
    }
    if (TraceComponent.isAnyTracingEnabled() && tc.isEntryEnabled()) SibTr.exit(tc, "createJmsMapMessage");
    return msg;
  }
643371
@FFDCIgnore(InvocationTargetException.class)
    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        if (TraceComponent.isAnyTracingEnabled() && tc.isEntryEnabled())
            SibTr.entry(this, tc, "invoke", method.toGenericString());

        if (actualMessageObject instanceof JmsMessageImpl) {

            if (((JmsMessageImpl) actualMessageObject).isAsynSendInProgress()) {
                //Async send is in progress... application is trying to access message object
                //throw JMSException

                if (TraceComponent.isAnyTracingEnabled() && tc.isDebugEnabled())
                    SibTr.debug(this, tc, "Application is trying to access message object.. when asysn send is in progress.. throwing JMSException");

                throw (JMSException) JmsErrorUtils.newThrowable(JMSException.class,
                                                                "INVALID_MESSAGE_ACCESS_CWSIA0516E",
                                                                null, // object[] argument
                                                                tc
                                );

            }
        }

        Object retObject = null;

        try {

            retObject = method.invoke(actualMessageObject, args);

        } catch (InvocationTargetException e) {
            throw e.getCause();
        }

        if (TraceComponent.isAnyTracingEnabled() && tc.isEntryEnabled())
            SibTr.exit(this, tc, "invoke", retObject);

        //call the method on actual message object.
        return retObject;

    }
1285978
public final ControlDecisionExpected createNewControlDecisionExpected() throws MessageCreateFailedException {
    if (TraceComponent.isAnyTracingEnabled() && tc.isEntryEnabled()) SibTr.entry(tc, "createNewControlDecisionExpected");
    ControlDecisionExpected msg = null;
    try {
      msg = new ControlDecisionExpectedImpl(MfpConstants.CONSTRUCTOR_NO_OP);
    }
    catch (MessageDecodeFailedException e) {
      /* No need to FFDC this as JsMsgObject will already have done so */
      // No FFDC code needed
      throw new MessageCreateFailedException(e);
    }
    if (TraceComponent.isAnyTracingEnabled() && tc.isEntryEnabled()) SibTr.exit(tc, "createNewControlDecisionExpected");
    return msg;
  }
1052781
public JsJmsTextMessage createJmsTextMessage() throws MessageCreateFailedException {
    if (TraceComponent.isAnyTracingEnabled() && tc.isEntryEnabled()) SibTr.entry(tc, "createJmsTextMessage");
    JsJmsTextMessage msg = null;
    try {
      msg = new JsJmsTextMessageImpl(MfpConstants.CONSTRUCTOR_NO_OP);
    }
    catch (MessageDecodeFailedException e) {
      /* No need to FFDC this as JsMsgObject will already have done so */
      // No FFDC code needed
      throw new MessageCreateFailedException(e);
    }
    if (TraceComponent.isAnyTracingEnabled() && tc.isEntryEnabled()) SibTr.exit(tc, "createJmsTextMessage");
    return msg;
  }
452994
@Override
    public void setProducerBind(String bind) throws JMSException {
        if (TraceComponent.isAnyTracingEnabled() && tc.isEntryEnabled())
            SibTr.entry(this, tc, "setProducerBind", bind);

        // Make sure null and empty are treated equally.
        if ((bind == null) || ("".equals(bind.trim())))
            bind = null;

        if ((bind == null) ||
            (ApiJmsConstants.PRODUCER_BIND_OFF.equals(bind) ||
            (ApiJmsConstants.PRODUCER_BIND_ON.equals(bind)))) {

            updateProperty(PRODUCER_BIND, bind);
        }
        else {
            // bad value, throw exception
            String key = "INVALID_VALUE_CWSIA0281";
            Object[] inserts = new Object[] { PRODUCER_BIND, bind };
            throw (JMSException) JmsErrorUtils.newThrowable(JMSException.class, key, inserts, tc);
        }

        if (TraceComponent.isAnyTracingEnabled() && tc.isEntryEnabled())
            SibTr.exit(this, tc, "setProducerBind");
    }
59206
@Override
    public BytesMessage createBytesMessage() throws JMSRuntimeException {

        if (TraceComponent.isAnyTracingEnabled() && tc.isEntryEnabled())
            SibTr.entry(this, tc, "createBytesMessage");

        BytesMessage bytesMessage = null;

        try {
            bytesMessage = jmsSession.createBytesMessage();
        } catch (JMSException e) {
            throw (JMSRuntimeException) JmsErrorUtils.getJMS2Exception(e, JMSRuntimeException.class);
        } finally {
            if (TraceComponent.isAnyTracingEnabled() && tc.isEntryEnabled())
                SibTr.exit(this, tc, "createBytesMessage");
        }
        return bytesMessage;
    }
1069578
private void createListener(final SibRaMessagingEngineConnection connection)
            throws ResourceException {

        final String methodName = "createListener";
        if (TraceComponent.isAnyTracingEnabled() && TRACE.isEntryEnabled()) {
            SibTr.entry(this, TRACE, methodName, connection);
        }

        final SIDestinationAddress destination = _endpointConfiguration
                .getDestination();

        try {

            connection.createListener(destination, _messageEndpointFactory);

        } catch (final IllegalStateException exception) {

            // No FFDC code needed
            throw exception;

        } catch (final ResourceException exception) {

            // No FFDC code needed

            if (_remoteDestination) {

                SibTr.warning(TRACE, SibTr.Suppressor.ALL_FOR_A_WHILE_SIMILAR_INSERTS, "ATTACH_FAILED_CWSIV0769", new Object[] {
                        destination.getDestinationName(),
                        _endpointConfiguration.getBusName(), this, exception });

                /*
                 * Try again 30 seconds later
                 */

                _timer.schedule(new TimerTask() {

                    public void run() {

                        try {

                            createListener(connection);

                        } catch (final ResourceException retryException) {
                            // No FFDC code needed
                        }

                    }
                }, RETRY_INTERVAL);

            } else {

                throw exception;

            }

        }

        if (TraceComponent.isAnyTracingEnabled() && TRACE.isEntryEnabled()) {
            SibTr.exit(this, TRACE, methodName);
        }

    }
927264
@Override
    public void createControlAdapter()
    {
        if (TraceComponent.isAnyTracingEnabled() && tc.isEntryEnabled())
            SibTr.entry(this, tc, "createControlAdapter");

        DestinationHandler dh = null;
        try
        {
            ItemStream is = getItemStream();
            if (is instanceof PubSubMessageItemStream)
            {
                dh = ((PubSubMessageItemStream) is).getDestinationHandler();
                controlAdapter = new QueuedMessage(this, dh, is);
            }
            else if (is instanceof MQLinkMessageItemStream)
            {
                dh = ((MQLinkMessageItemStream) is).getDestinationHandler();
                controlAdapter = new MQLinkQueuedMessage(this, dh, is);
            }
            else if (is instanceof PtoPMessageItemStream)
            {
                dh = ((PtoPMessageItemStream) is).getDestinationHandler();
                controlAdapter = new QueuedMessage(this, dh, is);
            }
            else
            {
                if (TraceComponent.isAnyTracingEnabled() && tc.isDebugEnabled())
                    SibTr.debug(this, tc, "Unknown item stream type " + is);
            }
        } catch (Exception e)
        {
            FFDCFilter.processException(
                                        e,
                                        "com.ibm.ws.sib.processor.impl.store.items.MessageItem.createControlAdapter",
                                        "1:2585:1.244.1.40",
                                        this);

            SibTr.exception(tc, e);
        }

        if (TraceComponent.isAnyTracingEnabled() && tc.isEntryEnabled())
            SibTr.exit(this, tc, "createControlAdapter");
    }