Beispiel #1
0
  /**
   * Process one message and either place it into the <code>responseMap</code> for a waiting thread
   * or send it to the dispatcher.
   */
  private void processOneMessage() {
    IProbeMessage message;

    try {
      message = serializer.readMessage();
    } catch (ChannelException e) {
      interrupt();
      return;
    } catch (Exception e) {
      logger.error("Error reading message", e);
      return;
    }

    if (message.isResponse()) {

      synchronized (responseMap) {
        if (isAbandoned(message.getSequenceNumber())) {
          return;
        }

        responseMap.put(message.getSequenceNumber(), message);
        responseMap.notifyAll();
        return;
      }
    }

    if (dispatcher != null) {
      try {
        dispatcher.dispatch(messenger, message);
      } catch (MessengerException e) {
        e.printStackTrace();
      }
    }
  }
  protected void closeFile() {
    synchronized (locked) {
      if (lock != null && lock.isValid()) {
        try {

          lock.release();
        } catch (IOException e) {
          LOG.log(Level.WARNING, "", e);
        }
      }
      try {
        if (fos != null) {
          fos.close();
        }
        if (fis != null) {
          fis.close();
        }

        if (channel != null) {
          channel.close();
        }

      } catch (IOException e) {
        LOG.log(Level.WARNING, "", e);
      }
      channel = null;
      fis = null;
      fos = null;
      lock = null;
      locked.put(filename, false);
      locked.notifyAll();
    }
  }
 /**
  * Releases the lock of the given key. The lock is only released if the calling thread owns the
  * lock for the given key
  *
  * @param key The key
  */
 public final void unlock(final Object key) {
   synchronized (keys) {
     if (keys.get(key) == Thread.currentThread()) {
       keys.remove(key);
       keys.notifyAll();
     }
   }
 }
  /* (non-Javadoc)
   * @see org.eclipse.wst.jsdt.debug.transport.TransportService#accept(org.eclipse.wst.jsdt.debug.transport.ListenerKey, long, long)
   */
  public Connection accept(ListenerKey key, long attachTimeout, long handshakeTimeout)
      throws IOException {
    long timeout = attachTimeout;
    if (timeout > 0) {
      if (timeout > Integer.MAX_VALUE) {
        timeout = Integer.MAX_VALUE; // approximately 25 days!
      }
    }

    synchronized (listeners) {
      if (!listeners.containsKey(key))
        throw new IllegalStateException("not listening"); // $NON-NLS-1$

      if (listeners.get(key) != null)
        throw new IllegalStateException(
            "PipedTransport only accepts one accept at a time"); //$NON-NLS-1$

      PipedInputStream serveris = new PipedInputStream();
      PipedOutputStream clientos = new PipedOutputStream();
      PipedOutputStream serveros = new PipedOutputStream();
      PipedInputStream clientis = new PipedInputStream();
      serveris.connect(clientos);
      serveros.connect(clientis);

      PipedConnection clientConnection = new PipedConnection(clientis, clientos);
      PipedConnection serverConnection = new PipedConnection(serveris, serveros);

      listeners.put(key, clientConnection);
      listeners.notifyAll();
      long startTime = System.currentTimeMillis();
      while (true) {
        try {
          listeners.wait(timeout);
        } catch (InterruptedException e) {
          clientConnection.close(); // Close unused client connection (and its streams);
          serverConnection.close(); // Close unused server connection (and its streams);
          throw new IOException("accept failed: interrupted"); // $NON-NLS-1$
        }
        if (!listeners.containsKey(key)) {
          clientConnection.close(); // Close unused client connection (and its streams);
          serverConnection.close(); // Close unused server connection (and its streams);
          throw new IOException("accept failed: stopped listening"); // $NON-NLS-1$
        }

        if (listeners.get(key) != null) {
          if (System.currentTimeMillis() - startTime > timeout) {
            listeners.put(key, null);
            clientConnection.close(); // Close unused client connection (and its streams);
            serverConnection.close(); // Close unused server connection (and its streams);
            throw new IOException("accept failed: timed out"); // $NON-NLS-1$
          }
          continue;
        }
        return serverConnection; // From this point both, the server and the client,
        // are responsible to close their connections
      }
    }
  }
Beispiel #5
0
 public void tmBegin() throws Exception {
   log.info(tid() + " TM_BEGIN (" + id + ")");
   getTM().begin();
   Transaction tx = getTM().getTransaction();
   synchronized (transactions) {
     transactions.put(id, tx);
     transactions.notifyAll();
   }
 }
Beispiel #6
0
 public synchronized void receiveNotification(String senderId, Message msg) {
   String mtype = msg.getMType();
   if (REG_MTYPE.equals(mtype)) {
     String id = (String) msg.getParam("id");
     if (!clientMap_.containsKey(id)) {
       clientMap_.put(id, null);
       clientMap_.notifyAll();
     }
   } else if (UNREG_MTYPE.equals(mtype)) {
     clientMap_.remove(msg.getParam("id"));
     clientMap_.notifyAll();
   } else if (METADATA_MTYPE.equals(mtype)) {
     clientMap_.put(msg.getParam("id"), msg.getParam("metadata"));
     clientMap_.notifyAll();
   } else {
     throw new UnsupportedOperationException("MType " + mtype + "??");
   }
 }
 void placeResponse(final int sequence, final ProtocolFrame response) {
   synchronized (myResponseQueue) {
     if (response == null || myResponseQueue.containsKey(sequence)) {
       myResponseQueue.put(sequence, response);
     }
     if (response != null) {
       myResponseQueue.notifyAll();
     }
   }
 }
  public CreateCoordinationContextResponseType createCoordinationContext(
      final CreateCoordinationContextType createCoordinationContext,
      final MAP map,
      boolean isSecure) {
    final String messageId = map.getMessageID();
    synchronized (messageIdMap) {
      messageIdMap.put(
          messageId, new CreateCoordinationContextDetails(createCoordinationContext, map));
      messageIdMap.notifyAll();
    }
    String coordinationType = createCoordinationContext.getCoordinationType();
    if (TestUtil.INVALID_CREATE_PARAMETERS_COORDINATION_TYPE.equals(coordinationType)) {
      try {
        SOAPFactory factory = SOAPFactory.newInstance();
        SOAPFault soapFault =
            factory.createFault(
                SoapFaultType.FAULT_SENDER.getValue(),
                CoordinationConstants.WSCOOR_ERROR_CODE_INVALID_PARAMETERS_QNAME);
        soapFault
            .addDetail()
            .addDetailEntry(CoordinationConstants.WSCOOR_ERROR_CODE_INVALID_PARAMETERS_QNAME)
            .addTextNode("Invalid create parameters");
        throw new SOAPFaultException(soapFault);
      } catch (Throwable th) {
        throw new ProtocolException(th);
      }
    }

    // we have to return a value so lets cook one up

    CreateCoordinationContextResponseType createCoordinationContextResponseType =
        new CreateCoordinationContextResponseType();
    CoordinationContext coordinationContext = new CoordinationContext();
    coordinationContext.setCoordinationType(coordinationType);
    coordinationContext.setExpires(createCoordinationContext.getExpires());
    String identifier = nextIdentifier();
    CoordinationContextType.Identifier identifierInstance =
        new CoordinationContextType.Identifier();
    identifierInstance.setValue(identifier);
    coordinationContext.setIdentifier(identifierInstance);
    W3CEndpointReferenceBuilder builder = new W3CEndpointReferenceBuilder();
    builder.serviceName(CoordinationConstants.REGISTRATION_SERVICE_QNAME);
    builder.endpointName(CoordinationConstants.REGISTRATION_ENDPOINT_QNAME);
    builder.address(TestUtil.PROTOCOL_COORDINATOR_SERVICE);
    W3CEndpointReference registrationService = builder.build();
    coordinationContext.setRegistrationService(TestUtil11.getRegistrationEndpoint(identifier));
    createCoordinationContextResponseType.setCoordinationContext(coordinationContext);

    return createCoordinationContextResponseType;
  }
  @SuppressWarnings("resource")
  protected void openFile() throws IOException {
    synchronized (locked) {
      while (locked.containsKey(filename) && locked.get(filename)) {
        try {
          locked.wait();
        } catch (InterruptedException e) {
        }
      }
      locked.put(filename, true);

      File file = new File(this.filename);
      if (!file.exists()) {
        locked.put(filename, false);
        locked.notifyAll();
        throw new IllegalStateException(
            "Warning: File doesn't exist (anymore):'" + this.filename + "'");
      }

      channel = new RandomAccessFile(file, "rw").getChannel();
      try {
        // TODO: add support for shared locks, allowing parallel reading
        // operations.
        lock = channel.lock();

      } catch (Exception e) {
        channel.close();
        channel = null;
        lock = null;
        locked.put(filename, false);
        locked.notifyAll();
        throw new IllegalStateException("error, couldn't obtain file lock on:" + filename, e);
      }
      fis = new BufferedInputStream(Channels.newInputStream(channel));
      fos = new BufferedOutputStream(Channels.newOutputStream(channel));
    }
  }
  /**
   * Containers use this method to report whenever they run out of messages in their queue.
   *
   * <p>To be called from the thread of the reporting container.
   *
   * @pre threads > 0.
   * @param reportingContainer the container reporting the emptiness of its message queue.
   * @param clock the value of the container's clock when reporting.
   * @param localTerminationCriteria the latest clock values this container has received from other
   *     containers since the last time it reported termination.
   */
  void reportLocalUpdateTermination(
      ReteContainer reportingContainer,
      long clock,
      Map<ReteContainer, Long> localTerminationCriteria) {
    synchronized (globalTerminationCriteria) {
      for (Entry<ReteContainer, Long> criterion : localTerminationCriteria.entrySet()) {
        terminationCriterion(criterion.getKey(), criterion.getValue());
      }

      reportedClocks.put(reportingContainer, clock);
      Long criterion = globalTerminationCriteria.get(reportingContainer);
      if (criterion != null && criterion < clock)
        globalTerminationCriteria.remove(reportingContainer);

      if (globalTerminationCriteria.isEmpty()) globalTerminationCriteria.notifyAll();
    }
  }
 /**
  * Release lock for given directory.
  *
  * @param directory
  */
 private void releaseUpdate(String directory) {
   synchronized (updatingBundles) {
     // check that we are the owner
     if (updatingBundles.containsKey(directory)) {
       if (updatingBundles.get(directory) == Thread.currentThread()) {
         // we have the rights to do this
       } else {
         LOG.error("Unlocking DPU from no-owner thread!");
       }
       // in every case .. unlock as we rely on programmers
       // that they use the lock properly
       updatingBundles.remove(directory);
     } else {
       // no record at all
     } // in every case notify everyone
     updatingBundles.notifyAll();
   }
 }
 /* (non-Javadoc)
  * @see org.eclipse.wst.jsdt.debug.transport.TransportService#attach(java.lang.String, long, long)
  */
 public Connection attach(String address, long attachTimeout, long handshakeTimeout)
     throws IOException {
   ListenerKey key = new PipedListenerKey(address == null ? Constants.EMPTY_STRING : address);
   Connection connection;
   long startTime = System.currentTimeMillis();
   synchronized (listeners) {
     connection = (Connection) listeners.get(key);
     while (connection == null) {
       if (System.currentTimeMillis() - startTime > attachTimeout)
         throw new IOException("attach failed: timed out"); // $NON-NLS-1$
       try {
         listeners.wait(attachTimeout);
       } catch (InterruptedException e) {
         throw new IOException("attach failed: interrupted"); // $NON-NLS-1$
       }
       connection = (Connection) listeners.get(key);
     }
     listeners.put(key, null);
     listeners.notifyAll();
   }
   return connection;
 }
Beispiel #13
0
 public void notifyall() {
   observables.notifyAll();
 }
Beispiel #14
0
 public static void pushEvent(String event, Object o) {
   events.put(event, o);
   events.notifyAll();
 }
Beispiel #15
0
 void releaseParticipant(final Participant p) {
   synchronized (participants) {
     participants.remove(p);
     participants.notifyAll();
   }
 }