Java Code Examples for org.apache.mina.common.IoSession#setAttribute()

The following examples show how to use org.apache.mina.common.IoSession#setAttribute() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: MinaChannel.java    From dubbo-2.6.5 with Apache License 2.0 6 votes vote down vote up
static MinaChannel getOrAddChannel(IoSession session, URL url, ChannelHandler handler) {
    if (session == null) {
        return null;
    }
    MinaChannel ret = (MinaChannel) session.getAttribute(CHANNEL_KEY);
    if (ret == null) {
        ret = new MinaChannel(session, url, handler);
        if (session.isConnected()) {
            MinaChannel old = (MinaChannel) session.setAttribute(CHANNEL_KEY, ret);
            if (old != null) {
                session.setAttribute(CHANNEL_KEY, old);
                ret = old;
            }
        }
    }
    return ret;
}
 
Example 2
Source File: MinaChannel.java    From dubbox with Apache License 2.0 6 votes vote down vote up
static MinaChannel getOrAddChannel(IoSession session, URL url, ChannelHandler handler) {
    if (session == null) {
        return null;
    }
    MinaChannel ret = (MinaChannel) session.getAttribute(CHANNEL_KEY);
    if (ret == null) {
        ret = new MinaChannel(session, url, handler);
        if (session.isConnected()) {
            MinaChannel old = (MinaChannel) session.setAttribute(CHANNEL_KEY, ret);
            if (old != null) {
                session.setAttribute(CHANNEL_KEY, old);
                ret = old;
            }
        }
    }
    return ret;
}
 
Example 3
Source File: MinaChannel.java    From dubbox-hystrix with Apache License 2.0 6 votes vote down vote up
static MinaChannel getOrAddChannel(IoSession session, URL url, ChannelHandler handler) {
    if (session == null) {
        return null;
    }
    MinaChannel ret = (MinaChannel) session.getAttribute(CHANNEL_KEY);
    if (ret == null) {
        ret = new MinaChannel(session, url, handler);
        if (session.isConnected()) {
            MinaChannel old = (MinaChannel) session.setAttribute(CHANNEL_KEY, ret);
            if (old != null) {
                session.setAttribute(CHANNEL_KEY, old);
                ret = old;
            }
        }
    }
    return ret;
}
 
Example 4
Source File: MinaChannel.java    From dubbox with Apache License 2.0 6 votes vote down vote up
static MinaChannel getOrAddChannel(IoSession session, URL url, ChannelHandler handler) {
    if (session == null) {
        return null;
    }
    MinaChannel ret = (MinaChannel) session.getAttribute(CHANNEL_KEY);
    if (ret == null) {
        ret = new MinaChannel(session, url, handler);
        if (session.isConnected()) {
            MinaChannel old = (MinaChannel) session.setAttribute(CHANNEL_KEY, ret);
            if (old != null) {
                session.setAttribute(CHANNEL_KEY, old);
                ret = old;
            }
        }
    }
    return ret;
}
 
Example 5
Source File: MinaChannel.java    From dubbox with Apache License 2.0 6 votes vote down vote up
static MinaChannel getOrAddChannel(IoSession session, URL url, ChannelHandler handler) {
    if (session == null) {
        return null;
    }
    MinaChannel ret = (MinaChannel) session.getAttribute(CHANNEL_KEY);
    if (ret == null) {
        ret = new MinaChannel(session, url, handler);
        if (session.isConnected()) {
            MinaChannel old = (MinaChannel) session.setAttribute(CHANNEL_KEY, ret);
            if (old != null) {
                session.setAttribute(CHANNEL_KEY, old);
                ret = old;
            }
        }
    }
    return ret;
}
 
Example 6
Source File: ImageServerIoHandler.java    From javastruct with GNU Lesser General Public License v3.0 5 votes vote down vote up
private String generateString(IoSession session, int length) {
    Integer index = (Integer) session.getAttribute(INDEX_KEY);
    StringBuffer buffer = new StringBuffer(length);
    while (buffer.length() < length) {
        buffer.append(characters.charAt(index));
        index++;
        if (index >= characters.length()) {
            index = 0;
        }
    }
    session.setAttribute(INDEX_KEY, index);
    return buffer.toString();
}
 
Example 7
Source File: PooledServerConnectedObjectManagerTest.java    From cougar with Apache License 2.0 5 votes vote down vote up
@Test
public void firstSubscription() throws Exception {
    SocketTransportCommandProcessor commandProcessor = mock(SocketTransportCommandProcessor.class);
    when(commandProcessor.writeSuccessResponse(any(SocketTransportRPCCommand.class), any(ExecutionResult.class), any(DehydratedExecutionContext.class))).thenReturn(true);

    SocketTransportRPCCommand command = mock(SocketTransportRPCCommand.class);
    IoSession session = new MyIoSession(String.valueOf(ioSessionId++));
    session.setAttribute(CougarProtocol.PROTOCOL_VERSION_ATTR_NAME, CougarProtocol.TRANSPORT_PROTOCOL_VERSION_MAX_SUPPORTED);
    when(command.getSession()).thenReturn(session);

    DehydratedExecutionContext requestContext = mock(DehydratedExecutionContext.class);

    MutableHeap heap = new MutableHeap("firstSubscription");
    Subscription sub = mock(Subscription.class);
    ConnectedResponse subscriptionResult = new ConnectedResponseImpl(heap, sub);
    OperationDefinition operationDefinition = mock(OperationDefinition.class);

    subject.addSubscription(commandProcessor, command, subscriptionResult, operationDefinition, requestContext, null);

    ArgumentCaptor<ExecutionResult> resultCaptor = ArgumentCaptor.forClass(ExecutionResult.class);
    verify(commandProcessor).writeSuccessResponse(any(SocketTransportRPCCommand.class), resultCaptor.capture(), any(DehydratedExecutionContext.class));

    ExecutionResult executionResult = resultCaptor.getValue();

    assertTrue(executionResult.getResult() instanceof NewHeapSubscription);
    NewHeapSubscription response = (NewHeapSubscription) executionResult.getResult();
    assertEquals(1, response.getHeapId());
    assertEquals("firstSubscription", response.getUri());
}
 
Example 8
Source File: PooledServerConnectedObjectManagerTest.java    From cougar with Apache License 2.0 5 votes vote down vote up
@Test
public void twoSubscriptionsToDifferentHeaps() throws Exception {
    SocketTransportCommandProcessor commandProcessor = mock(SocketTransportCommandProcessor.class);
    when(commandProcessor.writeSuccessResponse(any(SocketTransportRPCCommand.class), any(ExecutionResult.class), any(DehydratedExecutionContext.class))).thenReturn(true);

    SocketTransportRPCCommand command = mock(SocketTransportRPCCommand.class);
    IoSession session = new MyIoSession(String.valueOf(ioSessionId++));
    session.setAttribute(CougarProtocol.PROTOCOL_VERSION_ATTR_NAME, CougarProtocol.TRANSPORT_PROTOCOL_VERSION_MAX_SUPPORTED);
    when(command.getSession()).thenReturn(session);

    DehydratedExecutionContext requestContext = mock(DehydratedExecutionContext.class);

    OperationDefinition operationDefinition = mock(OperationDefinition.class);

    // 2 subs at about the same time, we're interested in the second..
    Subscription sub = mock(Subscription.class);
    subject.addSubscription(commandProcessor, command, new ConnectedResponseImpl(new MutableHeap("firstHeap"), sub), operationDefinition, requestContext, null);
    subject.addSubscription(commandProcessor, command, new ConnectedResponseImpl(new MutableHeap("secondHeap"), sub), operationDefinition, requestContext, null);

    ArgumentCaptor<ExecutionResult> resultCaptor = ArgumentCaptor.forClass(ExecutionResult.class);
    verify(commandProcessor, times(2)).writeSuccessResponse(any(SocketTransportRPCCommand.class), resultCaptor.capture(), any(DehydratedExecutionContext.class));

    ExecutionResult executionResult0 = resultCaptor.getAllValues().get(0);
    assertTrue(executionResult0.getResult() instanceof NewHeapSubscription);
    NewHeapSubscription response0 = (NewHeapSubscription) executionResult0.getResult();
    assertEquals(1, response0.getHeapId());
    assertEquals("firstHeap", response0.getUri());

    ExecutionResult executionResult1 = resultCaptor.getAllValues().get(1);
    assertTrue(executionResult1.getResult() instanceof NewHeapSubscription);
    NewHeapSubscription response1 = (NewHeapSubscription) executionResult1.getResult();
    assertEquals(2, response1.getHeapId());
    assertEquals("secondHeap", response1.getUri());
}
 
Example 9
Source File: PooledServerConnectedObjectManagerTest.java    From cougar with Apache License 2.0 5 votes vote down vote up
@Test
public void subscribeToTerminatedHeap() throws Exception {
    SocketTransportCommandProcessor commandProcessor = mock(SocketTransportCommandProcessor.class);
    when(commandProcessor.writeSuccessResponse(any(SocketTransportRPCCommand.class), any(ExecutionResult.class), any(DehydratedExecutionContext.class))).thenReturn(true);

    SocketTransportRPCCommand command = mock(SocketTransportRPCCommand.class);
    IoSession session = new MyIoSession("1");
    session.setAttribute(CougarProtocol.PROTOCOL_VERSION_ATTR_NAME, CougarProtocol.TRANSPORT_PROTOCOL_VERSION_MAX_SUPPORTED);
    when(command.getSession()).thenReturn(session);

    DehydratedExecutionContext requestContext = mock(DehydratedExecutionContext.class);

    MutableHeap heap = new MutableHeap("subscribeToTerminatedHeap");
    heap.beginUpdate();
    heap.terminateHeap();
    heap.endUpdate();
    Subscription sub = mock(Subscription.class);
    ConnectedResponse subscriptionResult = new ConnectedResponseImpl(heap, sub);
    OperationDefinition operationDefinition = mock(OperationDefinition.class);

    subject.addSubscription(commandProcessor, command, subscriptionResult, operationDefinition, requestContext, null);

    verify(commandProcessor).writeErrorResponse(any(SocketTransportCommand.class), any(DehydratedExecutionContext.class), any(CougarFrameworkException.class), eq(true));

    assertNull(subject.getHeapsByClient().get(session));
    assertEquals(0, subject.getHeapStates().size());

    verify(sub, never()).close();
    verify(sub, never()).close(any(Subscription.CloseReason.class));
}
 
Example 10
Source File: PooledServerConnectedObjectManagerTest.java    From cougar with Apache License 2.0 5 votes vote down vote up
@Test
public void secondSubscribeToTerminatedHeap() throws Exception {
    SocketTransportCommandProcessor commandProcessor = mock(SocketTransportCommandProcessor.class);
    when(commandProcessor.writeSuccessResponse(any(SocketTransportRPCCommand.class), any(ExecutionResult.class), any(DehydratedExecutionContext.class))).thenReturn(true);

    SocketTransportRPCCommand command = mock(SocketTransportRPCCommand.class);
    IoSession session = new MyIoSession(String.valueOf(ioSessionId++));
    session.setAttribute(CougarProtocol.PROTOCOL_VERSION_ATTR_NAME, CougarProtocol.TRANSPORT_PROTOCOL_VERSION_MAX_SUPPORTED);
    when(command.getSession()).thenReturn(session);

    DehydratedExecutionContext requestContext = mock(DehydratedExecutionContext.class);

    MutableHeap heap = new MutableHeap("subscribeToTerminatedHeap");
    Subscription sub1 = mock(Subscription.class);
    ConnectedResponse subscriptionResult = new ConnectedResponseImpl(heap, sub1);
    OperationDefinition operationDefinition = mock(OperationDefinition.class);

    subject.addSubscription(commandProcessor, command, subscriptionResult, operationDefinition, requestContext, null);
    verify(commandProcessor).writeSuccessResponse(any(SocketTransportRPCCommand.class), any(ExecutionResult.class), any(DehydratedExecutionContext.class));

    heap.beginUpdate();
    heap.terminateHeap();
    heap.endUpdate();

    Subscription sub2 = mock(Subscription.class);
    subscriptionResult = new ConnectedResponseImpl(heap, sub2);
    subject.addSubscription(commandProcessor, command, subscriptionResult, operationDefinition, requestContext, null);

    verify(commandProcessor).writeErrorResponse(any(SocketTransportCommand.class), any(DehydratedExecutionContext.class), any(CougarFrameworkException.class), eq(true));

    assertNull(subject.getHeapsByClient().get(session));
    assertEquals(0, subject.getHeapStates().size());

    // sub should have been closed
    verify(sub1).close(Subscription.CloseReason.REQUESTED_BY_PUBLISHER);
    verify(sub2, never()).close();
    verify(sub2, never()).close(any(Subscription.CloseReason.class));
}
 
Example 11
Source File: NioUtils.java    From cougar with Apache License 2.0 5 votes vote down vote up
public static String getSessionId(IoSession session) {
    String sessionId = (String) session.getAttribute("COUGAR_SESSION_ID");
    if (sessionId == null) {
        sessionId = String.format("%07d", sessionIdent.incrementAndGet());
        session.setAttribute("COUGAR_SESSION_ID", sessionId);
    }
    return sessionId;
}
 
Example 12
Source File: CougarProtocol3.java    From cougar with Apache License 2.0 5 votes vote down vote up
@Override
public void sessionOpened(NextFilter nextFilter, IoSession session) throws Exception {
    if (!isServer) {
        ClientHandshake clientHandshake = new ClientHandshake();
        session.setAttribute(ClientHandshake.HANDSHAKE, clientHandshake);
        session.write(new ConnectMessage(getClientAcceptableVersions()));
    }
    super.sessionOpened(nextFilter, session);
}
 
Example 13
Source File: ImageServerIoHandler.java    From javastruct with GNU Lesser General Public License v3.0 4 votes vote down vote up
public void sessionOpened(IoSession session) throws Exception {
    session.setAttribute(INDEX_KEY, new Integer(0));
}