Пример #1
0
  @Test
  public void testCacheClearing() throws IOException {
    cleared = false;
    ResourcePool pool =
        new ResourcePool(getCatalog()) {
          @Override
          public void clear(FeatureTypeInfo info) {
            cleared = true;
            super.clear(info);
          }
        };
    FeatureTypeInfo info =
        getCatalog()
            .getFeatureTypeByName(MockData.LAKES.getNamespaceURI(), MockData.LAKES.getLocalPart());

    assertNotNull(pool.getFeatureType(info));
    info.setTitle("changed");

    assertFalse(cleared);
    getCatalog().save(info);
    assertTrue(cleared);

    cleared = false;
    assertNotNull(pool.getFeatureType(info));

    for (LayerInfo l : getCatalog().getLayers(info)) {
      getCatalog().remove(l);
    }
    getCatalog().remove(info);
    assertTrue(cleared);
  }
Пример #2
0
  public ResourcePool createResourcePool(String name, DataFactoryUndoController undo) {
    ResourcePool resourcePool = ResourcePool.createRourcePool(name, undo);
    addPool(resourcePool);
    ((DefaultNodeModel) resourcePool.getResourceOutline()).setDataFactory(resourcePool);
    ((DefaultNodeModel) resourcePool.getResourceOutline())
        .setUndoController(resourcePool.getUndoController());

    return resourcePool;
  }
Пример #3
0
 /**
  * Test that the {@link FeatureType} cache returns the same instance every time. This is assumed
  * by some nasty code in other places that tampers with the CRS. If a new {@link FeatureType} is
  * constructed for the same {@link FeatureTypeInfo}, Bad Things Happen (TM).
  */
 @Test
 public void testFeatureTypeCacheInstance() throws Exception {
   ResourcePool pool = ResourcePool.create(getCatalog());
   FeatureTypeInfo info =
       getCatalog()
           .getFeatureTypeByName(MockData.LAKES.getNamespaceURI(), MockData.LAKES.getLocalPart());
   FeatureType ft1 = pool.getFeatureType(info);
   FeatureType ft2 = pool.getFeatureType(info);
   FeatureType ft3 = pool.getFeatureType(info);
   assertSame(ft1, ft2);
   assertSame(ft1, ft3);
 }
  public void testSend() throws Exception {

    final Acceptor acceptor = new Acceptor("localhost", 0, 1);

    final FanOutServerSender serverSender =
        new FanOutServerSender(acceptor, ConnectionType.AGENT, 3);

    final Socket[] socket = new Socket[5];

    for (int i = 0; i < socket.length; ++i) {
      socket[i] =
          new Connector(
                  InetAddress.getByName(null).getHostName(),
                  acceptor.getPort(),
                  ConnectionType.AGENT)
              .connect();
    }

    // Sleep until we've accepted all connections. Give up after a few
    // seconds.
    final ResourcePool socketSet = acceptor.getSocketSet(ConnectionType.AGENT);

    for (int i = 0; socketSet.countActive() != 5 && i < 10; ++i) {
      Thread.sleep(i * i * 10);
    }

    final SimpleMessage message1 = new SimpleMessage();
    final SimpleMessage message2 = new SimpleMessage();

    serverSender.send(message1);
    serverSender.send(message2);

    for (int i = 0; i < socket.length; ++i) {
      final InputStream socketInput = socket[i].getInputStream();

      final Message m1 = readMessage(socketInput);
      final Message m2 = readMessage(socketInput);

      assertEquals(message1, m1);
      assertEquals(message2, m2);

      assertEquals(0, socketInput.available());

      socket[i].close();
    }

    serverSender.shutdown();
    acceptor.shutdown();
  }
  public void testShutdown() throws Exception {

    final Acceptor acceptor = new Acceptor("localhost", 0, 1);

    final FanOutServerSender serverSender =
        new FanOutServerSender(acceptor, ConnectionType.AGENT, 3);

    assertEquals(1, acceptor.getThreadGroup().activeCount());

    final Socket socket =
        new Connector(
                InetAddress.getByName(null).getHostName(), acceptor.getPort(), ConnectionType.AGENT)
            .connect();

    // Sleep until we've accepted the connection. Give up after a few
    // seconds.
    final ResourcePool socketSet = acceptor.getSocketSet(ConnectionType.AGENT);

    for (int i = 0; socketSet.countActive() != 1 && i < 10; ++i) {
      Thread.sleep(i * i * 10);
    }

    final Message message = new SimpleMessage();
    serverSender.send(message);

    final InputStream socketStream = socket.getInputStream();

    final Object o1 = readMessage(socketStream);
    assertNotNull(o1);

    serverSender.shutdown();

    try {
      serverSender.send(message);
      fail("Expected CommunicationException");
    } catch (CommunicationException e) {
    }

    try {
      final Object o2 = readMessage(socketStream);

      assertTrue(o2 instanceof CloseCommunicationMessage);
    } catch (StreamCorruptedException e) {
      // Occasionally this occurs because the connection is shutdown.
      // Whatever.
    }

    acceptor.shutdown();
  }
Пример #6
0
  /**
   * Get a set of accepted connections.
   *
   * @param connectionType Identifies the set of connections to return.
   * @return A set of sockets, each wrapped in a {@link SocketWrapper}.
   * @throws ShutdownException If the acceptor is shutdown.
   */
  ResourcePool getSocketSet(final ConnectionType connectionType) throws ShutdownException {

    synchronized (m_socketSets) {
      if (m_isShutdown) {
        throw new ShutdownException("Acceptor has been shut down");
      }

      final ResourcePool original = (ResourcePool) m_socketSets.get(connectionType);

      if (original != null) {
        return original;
      } else {
        final ResourcePool newSocketSet = new ResourcePoolImplementation();

        newSocketSet.addListener(
            new ResourcePool.Listener() {
              public void resourceAdded(ResourcePool.Resource resource) {
                final ConnectionIdentity connection =
                    ((SocketWrapper) resource).getConnectionIdentity();

                getListeners(connectionType)
                    .apply(
                        new ListenerSupport.Informer() {
                          public void inform(Object listener) {
                            ((Listener) listener).connectionAccepted(connectionType, connection);
                          }
                        });
              }

              public void resourceClosed(ResourcePool.Resource resource) {
                final ConnectionIdentity connection =
                    ((SocketWrapper) resource).getConnectionIdentity();

                getListeners(connectionType)
                    .apply(
                        new ListenerSupport.Informer() {
                          public void inform(Object listener) {
                            ((Listener) listener).connectionClosed(connectionType, connection);
                          }
                        });
              }
            });

        m_socketSets.put(connectionType, newSocketSet);
        return newSocketSet;
      }
    }
  }
Пример #7
0
  @Test
  public void testAttributeCache() throws Exception {
    final Catalog catalog = getCatalog();
    ResourcePool pool = ResourcePool.create(catalog);

    // clean up the lakes type
    FeatureTypeInfo oldInfo =
        catalog.getFeatureTypeByName(
            MockData.LAKES.getNamespaceURI(), MockData.LAKES.getLocalPart());
    List<LayerInfo> layers = catalog.getLayers(oldInfo);
    for (LayerInfo layerInfo : layers) {
      catalog.remove(layerInfo);
    }
    catalog.remove(oldInfo);

    // rebuild as new
    CatalogBuilder builder = new CatalogBuilder(catalog);
    builder.setStore(
        catalog.getStoreByName(MockData.CITE_PREFIX, MockData.CITE_PREFIX, DataStoreInfo.class));
    FeatureTypeInfo info =
        builder.buildFeatureType(
            new NameImpl(MockData.LAKES.getNamespaceURI(), MockData.LAKES.getLocalPart()));

    // non persisted state, caching should not occurr
    List<AttributeTypeInfo> att1 = pool.getAttributes(info);
    List<AttributeTypeInfo> att2 = pool.getAttributes(info);
    assertNotSame(att1, att2);
    assertEquals(att1, att2);

    // save it, making it persistent
    catalog.add(info);

    // first check caching actually works against persisted type infos
    List<AttributeTypeInfo> att3 = pool.getAttributes(info);
    List<AttributeTypeInfo> att4 = pool.getAttributes(info);
    assertSame(att3, att4);
    assertNotSame(att1, att3);
    assertEquals(att1, att3);
  }
Пример #8
0
  /**
   * Make sure {@link ResourcePool#clear(DataStoreInfo)} and {@link ResourcePool#dispose()} call
   * {@link DataAccess#dispose()}
   */
  @Test
  public void testDispose() throws IOException {
    disposeCalled = false;
    class ResourcePool2 extends ResourcePool {
      @SuppressWarnings("serial")
      public ResourcePool2(Catalog catalog) {
        super(catalog);
        dataStoreCache =
            new DataStoreCache() {
              @SuppressWarnings("unchecked")
              @Override
              protected void dispose(String name, DataAccess dataStore) {
                disposeCalled = true;
                super.dispose(name, dataStore);
              }
            };
      }
    }

    Catalog catalog = getCatalog();
    ResourcePool pool = new ResourcePool2(catalog);
    catalog.setResourcePool(pool);

    DataStoreInfo info = catalog.getDataStores().get(0);
    // force the datastore to be created
    DataAccess<? extends FeatureType, ? extends Feature> dataStore = pool.getDataStore(info);
    assertNotNull(dataStore);
    assertFalse(disposeCalled);
    pool.clear(info);
    assertTrue(disposeCalled);

    // force the datastore to be created
    dataStore = pool.getDataStore(info);
    assertNotNull(dataStore);
    disposeCalled = false;
    pool.dispose();
    assertTrue(disposeCalled);
  }
  public void testSendAddressedMessage() throws Exception {

    final Acceptor acceptor = new Acceptor("localhost", 0, 1);

    final FanOutServerSender serverSender =
        new FanOutServerSender(acceptor, ConnectionType.AGENT, 3);

    final Socket[] socket = new Socket[5];

    for (int i = 0; i < socket.length; ++i) {
      socket[i] =
          new Connector(
                  InetAddress.getByName(null).getHostName(),
                  acceptor.getPort(),
                  ConnectionType.AGENT)
              .connect(new StubAddress(new Integer(i)));
    }

    // Sleep until we've accepted all connections. Give up after a few
    // seconds.
    final ResourcePool socketSet = acceptor.getSocketSet(ConnectionType.AGENT);

    for (int i = 0; socketSet.countActive() != 5 && i < 10; ++i) {
      Thread.sleep(i * i * 10);
    }

    final SimpleMessage message1 = new SimpleMessage();
    final SimpleMessage message2 = new SimpleMessage();

    serverSender.send(new StubAddress(new Integer(1)), message1);
    serverSender.send(new StubAddress(new Integer(2)), message2);

    for (int i = 0; i < socket.length; ++i) {
      final InputStream socketInput = socket[i].getInputStream();

      if (i == 1) {
        final Message m = readMessage(socketInput);
        assertEquals(message1, m);
      } else if (i == 2) {
        final Message m = readMessage(socketInput);
        assertEquals(message2, m);
      }

      assertEquals(0, socketInput.available());

      socket[i].close();
    }

    serverSender.shutdown();

    try {
      serverSender.send(
          new Address() {
            public boolean includes(Address address) {
              return false;
            }
          },
          message1);
      fail("Expected CommunicationException");
    } catch (CommunicationException e) {
    }

    acceptor.shutdown();
  }