Exemple #1
0
  /**
   * Constructor.
   *
   * @param model The parent locator model. Must not be <code>null</code>.
   * @param peer The peer. Must not be <code>null</code>.
   */
  public PeerModel(ILocatorModel model, IPeer peer) {
    super();

    Assert.isNotNull(model);
    this.model = model;

    Assert.isNotNull(peer);

    // Set the default properties before enabling the change events.
    // The properties changed listeners should not be called from the
    // constructor.
    setProperty(IPeerModelProperties.PROP_INSTANCE, peer);

    // Initialize the peer id
    peerId = peer.getID();
    Assert.isNotNull(peerId);

    // Peer model nodes can change the node parent at any time
    allowSetParentOnNonNullParent = true;
    // Peer model nodes does not have a parent by default
    //   -> allow change events with null parent
    suppressEventsOnNullParent = false;

    // Enable change events
    setChangeEventsEnabled(true);
  }
  /* (non-Javadoc)
   * @see org.eclipse.tcf.te.runtime.persistence.interfaces.IPersistableURIProvider#getURI(java.lang.Object)
   */
  @Override
  public URI getURI(final Object context) {
    Assert.isNotNull(context);

    URI uri = null;
    final IPeer peer = getPeer(context);

    if (peer != null) {
      // Get the URI the peer model has been created from
      final AtomicReference<URI> nodeURI = new AtomicReference<URI>();
      final AtomicReference<Version> version = new AtomicReference<Version>();
      Runnable runnable =
          new Runnable() {
            @Override
            public void run() {
              String value = peer.getAttributes().get(IPersistableNodeProperties.PROPERTY_URI);
              if (value != null && !"".equals(value.trim())) { // $NON-NLS-1$
                nodeURI.set(URI.create(value.trim()));
              }
              value = peer.getAttributes().get(IPeerProperties.PROP_VERSION);
              version.set(value != null ? new Version(value.trim()) : null);
            }
          };
      if (Protocol.isDispatchThread()) {
        runnable.run();
      } else {
        Protocol.invokeAndWait(runnable);
      }

      if (nodeURI.get() != null) {
        uri = nodeURI.get();
      }

      if (uri == null) {
        String baseName = peer.getName();
        if (baseName == null) {
          baseName = peer.getID();
        }
        String name = makeValidFileSystemName(baseName);
        // Get the URI from the name
        uri = getRoot().append(name + ".peer").toFile().toURI(); // $NON-NLS-1$
        try {
          File file = new File(uri.normalize());
          int i = 0;
          while (file.exists()) {
            name =
                makeValidFileSystemName(
                    baseName
                        + (version.get() != null ? "_" + version.get().toString() : "")
                        + //$NON-NLS-1$ //$NON-NLS-2$
                        (i > 0 ? " (" + i + ")" : "")); // $NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
            uri = getRoot().append(name + ".peer").toFile().toURI(); // $NON-NLS-1$
            file = new File(uri.normalize());
            i++;
          }
        } catch (Exception e) {
        }
      }
    }

    return uri;
  }