@AdviseWith(adviceClasses = {PropsUtilAdvice.class})
  @Test
  public void testPrepareRequest() throws Exception {
    PropsUtilAdvice.setProps(
        PropsKeys.INTRABAND_MAILBOX_REAPER_THREAD_ENABLED, Boolean.FALSE.toString());
    PropsUtilAdvice.setProps(
        PropsKeys.INTRABAND_MAILBOX_STORAGE_LIFE, String.valueOf(Long.MAX_VALUE));

    Serializer serializer = new Serializer();

    serializer.writeString(_SERVLET_CONTEXT_NAME);
    serializer.writeObject(new SPIAgentRequest(_mockHttpServletRequest));

    Method depositMailMethod =
        ReflectionUtil.getDeclaredMethod(MailboxUtil.class, "depositMail", ByteBuffer.class);

    long receipt = (Long) depositMailMethod.invoke(null, serializer.toByteBuffer());

    byte[] data = new byte[8];

    BigEndianCodec.putLong(data, 0, receipt);

    HttpClientSPIAgent httpClientSPIAgent =
        new HttpClientSPIAgent(
            _spiConfiguration, new MockRegistrationReference(new MockIntraband()));

    MockHttpServletRequest mockHttpServletRequest = new MockHttpServletRequest();

    mockHttpServletRequest.setContent(data);

    HttpServletRequest httpServletRequest =
        httpClientSPIAgent.prepareRequest(mockHttpServletRequest);

    Assert.assertNotNull(httpServletRequest.getAttribute(WebKeys.SPI_AGENT_REQUEST));
  }
Пример #2
0
 static {
   try {
     _findClassMethod =
         ReflectionUtil.getDeclaredMethod(ClassLoader.class, "findClass", String.class);
     _getResourceMethod =
         ReflectionUtil.getDeclaredMethod(ClassLoader.class, "getResource", String.class);
     _getResourcesMethod =
         ReflectionUtil.getDeclaredMethod(ClassLoader.class, "getResources", String.class);
     _loadClassMethod =
         ReflectionUtil.getDeclaredMethod(
             ClassLoader.class, "loadClass", String.class, boolean.class);
   } catch (Exception e) {
     if (_log.isErrorEnabled()) {
       _log.error("Unable to locate required methods", e);
     }
   }
 }
  protected static Class<?> loadClassDirectly(ClassLoader classLoader, String className)
      throws Exception {

    synchronized (classLoader) {
      Method findLoadedClassMethod =
          ReflectionUtil.getDeclaredMethod(ClassLoader.class, "findLoadedClass", String.class);

      Class<?> clazz = (Class<?>) findLoadedClassMethod.invoke(classLoader, className);

      if (clazz == null) {
        Method findClassMethod =
            ReflectionUtil.getDeclaredMethod(ClassLoader.class, "findClass", String.class);

        clazz = (Class<?>) findClassMethod.invoke(classLoader, className);
      }

      Method resolveClassMethod =
          ReflectionUtil.getDeclaredMethod(ClassLoader.class, "resolveClass", Class.class);

      resolveClassMethod.invoke(classLoader, clazz);

      return clazz;
    }
  }
Пример #4
0
  public static Properties loadJDK6(Reader reader) throws IOException {
    try {
      Properties properties = new Properties();

      if (_jdk6LoadMethod == null) {
        _jdk6LoadMethod = ReflectionUtil.getDeclaredMethod(Properties.class, "load", Reader.class);
      }

      _jdk6LoadMethod.invoke(properties, reader);

      return properties;
    } catch (Exception e) {
      Throwable cause = e.getCause();

      if (cause instanceof IOException) {
        throw (IOException) cause;
      }

      throw new IllegalStateException(
          "Failed to invoke java.util.Properties.load(Reader reader)", e);
    }
  }
    @Override
    public Datagram sendSyncDatagram(RegistrationReference registrationReference, Datagram datagram)
        throws IOException {

      if (_ioException != null) {
        throw _ioException;
      }

      try {
        Method depositMailMethod =
            ReflectionUtil.getDeclaredMethod(MailboxUtil.class, "depositMail", ByteBuffer.class);

        long receipt = (Long) depositMailMethod.invoke(null, datagram.getDataByteBuffer());

        _receiptData = new byte[8];

        BigEndianCodec.putLong(_receiptData, 0, receipt);

        return Datagram.createResponseDatagram(datagram, ByteBuffer.wrap(_receiptData));
      } catch (Exception e) {
        throw new IOException(e);
      }
    }
  @Test
  public void testService() throws Exception {

    // Unable to borrow a socket

    HttpClientSPIAgent httpClientSPIAgent =
        new HttpClientSPIAgent(
            _spiConfiguration, new MockRegistrationReference(new MockIntraband()));

    try {
      httpClientSPIAgent.service(null, null);

      Assert.fail();
    } catch (PortalResiliencyException pre) {
      Throwable throwable = pre.getCause();

      Assert.assertSame(ConnectException.class, throwable.getClass());
    }

    // Unable to send, successfully close

    httpClientSPIAgent =
        new HttpClientSPIAgent(
            _spiConfiguration,
            new MockRegistrationReference(new DirectMailboxIntraBand(new IOException())));

    ServerSocketChannel serverSocketChannel =
        SocketUtil.createServerSocketChannel(
            InetAddressUtil.getLoopbackInetAddress(), _spiConfiguration.getConnectorPort(), null);

    serverSocketChannel.configureBlocking(true);

    SocketChannel socketChannel = SocketChannel.open(httpClientSPIAgent.socketAddress);

    Socket socket = socketChannel.socket();

    Queue<Socket> socketBlockingQueue = httpClientSPIAgent.socketBlockingQueue;

    socketBlockingQueue.add(socket);

    MockHttpServletRequest mockHttpServletRequest = new MockHttpServletRequest();

    mockHttpServletRequest.setAttribute(WebKeys.SPI_AGENT_PORTLET, _portlet);

    try {
      httpClientSPIAgent.service(mockHttpServletRequest, null);

      Assert.fail();
    } catch (PortalResiliencyException pre) {
      Throwable throwable = pre.getCause();

      Assert.assertSame(IOException.class, throwable.getClass());
    }

    ServerSocket serverSocket = serverSocketChannel.socket();

    closePeers(socket, serverSocket);

    // Unable to send, unable to close, without log

    List<LogRecord> logRecords =
        JDKLoggerTestUtil.configureJDKLogger(HttpClientSPIAgent.class.getName(), Level.OFF);

    socket =
        new Socket(InetAddressUtil.getLoopbackInetAddress(), _spiConfiguration.getConnectorPort());

    SocketImpl socketImpl = swapSocketImpl(socket, null);

    DirectMailboxIntraBand directMailboxIntraBand = new DirectMailboxIntraBand(new IOException());

    httpClientSPIAgent =
        new HttpClientSPIAgent(
            _spiConfiguration, new MockRegistrationReference(directMailboxIntraBand));

    socketBlockingQueue = httpClientSPIAgent.socketBlockingQueue;

    socketBlockingQueue.add(socket);

    try {
      httpClientSPIAgent.service(mockHttpServletRequest, null);

      Assert.fail();
    } catch (PortalResiliencyException pre) {
      Throwable throwable = pre.getCause();

      Assert.assertSame(IOException.class, throwable.getClass());
    }

    Assert.assertTrue(logRecords.isEmpty());

    swapSocketImpl(socket, socketImpl);

    closePeers(socket, serverSocket);

    // Unable to send, unable to close, with log

    logRecords =
        JDKLoggerTestUtil.configureJDKLogger(HttpClientSPIAgent.class.getName(), Level.WARNING);

    socket =
        new Socket(InetAddressUtil.getLoopbackInetAddress(), _spiConfiguration.getConnectorPort());

    socketImpl = swapSocketImpl(socket, null);

    directMailboxIntraBand = new DirectMailboxIntraBand(new IOException());

    httpClientSPIAgent =
        new HttpClientSPIAgent(
            _spiConfiguration, new MockRegistrationReference(directMailboxIntraBand));

    socketBlockingQueue = httpClientSPIAgent.socketBlockingQueue;

    socketBlockingQueue.add(socket);

    try {
      httpClientSPIAgent.service(mockHttpServletRequest, null);

      Assert.fail();
    } catch (PortalResiliencyException pre) {
      Throwable throwable = pre.getCause();

      Assert.assertSame(IOException.class, throwable.getClass());
    }

    Assert.assertEquals(1, logRecords.size());

    LogRecord logRecord = logRecords.get(0);

    Throwable throwable = logRecord.getThrown();

    Assert.assertSame(IOException.class, throwable.getClass());

    swapSocketImpl(socket, socketImpl);

    closePeers(socket, serverSocket);

    // Successfully send

    socketChannel = SocketChannel.open(httpClientSPIAgent.socketAddress);

    socketChannel.configureBlocking(true);

    httpClientSPIAgent =
        new HttpClientSPIAgent(
            _spiConfiguration, new MockRegistrationReference(new DirectMailboxIntraBand(null)));

    socketBlockingQueue = httpClientSPIAgent.socketBlockingQueue;

    socket = socketChannel.socket();

    socketBlockingQueue.add(socket);

    Serializer serializer = new Serializer();

    serializer.writeString(_SERVLET_CONTEXT_NAME);
    serializer.writeObject(new SPIAgentResponse(_SERVLET_CONTEXT_NAME));

    Method depositMailMethod =
        ReflectionUtil.getDeclaredMethod(MailboxUtil.class, "depositMail", ByteBuffer.class);

    long receipt = (Long) depositMailMethod.invoke(null, serializer.toByteBuffer());

    Socket remoteSocket = serverSocket.accept();

    OutputStream outputStream = remoteSocket.getOutputStream();

    outputStream.write("HTTP/1.1 200 OK\n\n".getBytes("US-ASCII"));

    byte[] receiptData = new byte[8];

    BigEndianCodec.putLong(receiptData, 0, receipt);

    outputStream.write(receiptData);

    outputStream.flush();

    httpClientSPIAgent.service(mockHttpServletRequest, new MockHttpServletResponse());

    socket.close();
    remoteSocket.close();
    serverSocket.close();
  }
  protected Servlet reloadDependants(String path, Servlet servlet, ServletInfo servletInfo) {

    if (!_reloadDependants) {
      return servlet;
    }

    try {
      Method method = ReflectionUtil.getDeclaredMethod(servlet.getClass(), "getDependants");

      Collection<String> dependants = null;

      if (JasperVersionDetector.hasJspServletDependantsMap()) {
        Map<String, ?> dependantsMap = (Map<String, ?>) method.invoke(servlet);

        if (dependantsMap != null) {
          dependants = dependantsMap.keySet();
        }
      } else {
        dependants = (List<String>) method.invoke(servlet);
      }

      if (dependants == null) {
        return servlet;
      }

      boolean reloadServlet = false;

      for (String dependant : dependants) {
        long lastModified = getFileLastModified(dependant, servlet);

        Long previousLastModified = _dependantTimestamps.get(dependant);

        if (previousLastModified == null) {
          _dependantTimestamps.put(dependant, lastModified);

          previousLastModified = lastModified;
        }

        if ((lastModified == 0) || (lastModified != previousLastModified.longValue())) {

          reloadServlet = true;

          _dependantTimestamps.put(dependant, lastModified);

          if (_log.isDebugEnabled()) {
            _log.debug("Reload dependant " + dependant);
          }
        }
      }

      if (reloadServlet) {
        _servletInfos.remove(path);

        updateFileLastModified(path, servlet);

        servlet = null;
      }
    } catch (NoSuchMethodException nsme) {
      if (_log.isWarnEnabled()) {
        _log.warn(
            "Reloading of dependant JSP is disabled because your "
                + "Servlet container is not a variant of Jasper");
      }

      _reloadDependants = false;
    } catch (Exception e) {
      _log.error(e, e);
    }

    return servlet;
  }