Android Open Source - interdroid-vdb Remote Info






From Project

Back to project page interdroid-vdb.

License

The source code is released under:

Copyright (c) 2008-2012 Vrije Universiteit, The Netherlands All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the follo...

If you think the Android project interdroid-vdb listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

/*
 * Copyright (c) 2008-2012 Vrije Universiteit, The Netherlands All rights
 * reserved./*from  w ww  .  j  a  v a  2 s  . com*/
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice, this
 * list of conditions and the following disclaimer.
 *
 * Redistributions in binary form must reproduce the above copyright notice,
 * this list of conditions and the following disclaimer in the documentation
 * and/or other materials provided with the distribution.
 *
 * Neither the name of the Vrije Universiteit nor the names of its contributors
 * may be used to endorse or promote products derived from this software without
 * specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS''
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */
package interdroid.vdb.persistence.api;

import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.List;

import org.eclipse.jgit.lib.Config;
import org.eclipse.jgit.transport.RefSpec;
import org.eclipse.jgit.transport.RemoteConfig;
import org.eclipse.jgit.transport.URIish;

/**
 * Container for all the information we care about a remote device.
 */
public class RemoteInfo {
  /**
   * The section this information is saved under in the preferences.
   */
  public static final String SECTION = "remote";
  /**
   * The key for the description.
   */
  private static final String KEY_DESCRIPTION = "vdb-description";
  /**
   * The key for the local name on the remote.
   */
  private static final String KEY_OUR_NAME_ON_REMOTE =
      "vdb-local-name-on-remote";
  /**
   * The key for the type of remote.
   */
  private static final String KEY_TYPE = "vdb-type";

  /**
   * The types of remotes we suport.
   *
   * @author nick <palmer@cs.vu.nl>
   *
   */
  public enum RemoteType {
    /** A merge point remote. */
    MERGE_POINT,
    /** A hub remote. */
    HUB;
  }

  /**
   * The type of remote this represents.
   */
  private RemoteType mType;
  /**
   * The name of the remote.
   */
  private String mName;
  /**
   * The description of the remote.
   */
  private String mDescription;
  /**
   * The URI for the remote.
   */
  private URIish mRemoteUri;
  /**
   * Our name on the remote when in HUB mode.
   */
  private String mOurNameOnRemote;

  /**
   * Construct a new remote info for a merge point.
   */
  public RemoteInfo() {
    mType = RemoteType.MERGE_POINT;
  }
  /**
   * Construct a new remote info for a merge point
   * with the given name.
   * @param name the name of the remote.
   */
  public RemoteInfo(final String name) {
    mName = name;
    mType = RemoteType.MERGE_POINT;
  }

  /**
   * Load a remote from a configuration.
   * @param rc the configuration to load from
   * @throws URISyntaxException if the Remote URI is invalid
   */
  public final void load(final Config rc) throws URISyntaxException {
    try {
      setType(RemoteType.valueOf(rc.getString(SECTION, mName, KEY_TYPE)));
    } catch (IllegalArgumentException e) {
      setType(RemoteType.HUB);
    } catch (NullPointerException e) {
      setType(RemoteType.HUB);
    }

    RemoteConfig remoteCfg = new RemoteConfig(rc, mName);
    List<URIish> allURIs = remoteCfg.getURIs();
    if (allURIs.size() > 0) {
      setRemoteUri(allURIs.get(0));
    } else {
      setRemoteUri(null);
    }
    setDescription(rc.getString(SECTION, mName, KEY_DESCRIPTION));

    switch (mType) {
    case HUB:
      setOurNameOnRemote(rc.getString(SECTION,
          mName, KEY_OUR_NAME_ON_REMOTE));
      break;
    default:
    case MERGE_POINT:
    }
  }

  /**
   * Save the information to the configuration.
   * @param rc the config to save to
   * @throws URISyntaxException if the URI syntax is invalid
   */
  public final void save(final Config rc) throws URISyntaxException {
    rc.setString(SECTION, mName, KEY_TYPE, mType.name());
    rc.setString(SECTION, mName, KEY_DESCRIPTION, mDescription);

    RemoteConfig remoteCfg = new RemoteConfig(rc, mName);
    switch (mType) {
    case HUB:
      // From hubs we may fetch all the remote references in refs/remotes
      // TODO: test that the wildcard works for multiple directories
      remoteCfg.setFetchRefSpecs(new ArrayList<RefSpec>());
      remoteCfg.addFetchRefSpec(new RefSpec()
          .setSourceDestination("refs/remotes/*", "refs/remotes/*"));

      // And we push our local branches to the refs/remotes/ourname
      remoteCfg.setPushRefSpecs(new ArrayList<RefSpec>());
      if (mOurNameOnRemote != null) {
        String pushPath = "refs/remotes/" + mOurNameOnRemote + "/*";
        remoteCfg.addPushRefSpec(new RefSpec()
            .setSourceDestination("refs/heads/*",  pushPath));
      }
      break;
    case MERGE_POINT:
      // We fetch merge points into refs/remotes/remote-name
      remoteCfg.setFetchRefSpecs(new ArrayList<RefSpec>());
      String fetchPath = "refs/remotes/" + mName +  "/*";
      remoteCfg.addFetchRefSpec(new RefSpec()
          .setSourceDestination("refs/heads/*", fetchPath));

      // We push directly into the heads of the merge-point remote
      remoteCfg.setPushRefSpecs(new ArrayList<RefSpec>());
      remoteCfg.addPushRefSpec(new RefSpec()
          .setSourceDestination("refs/heads/*", "refs/heads/*"));
    default:
    }

    for (URIish uri : remoteCfg.getURIs()) {
      remoteCfg.removeURI(uri);
    }
    if (mRemoteUri != null) {
      remoteCfg.addURI(mRemoteUri);
    }
    remoteCfg.update(rc);

    switch (mType) {
    case HUB:
      rc.setString(SECTION, mName, KEY_OUR_NAME_ON_REMOTE,
          mOurNameOnRemote);
      break;
    case MERGE_POINT:
      rc.unset(SECTION, mName, KEY_OUR_NAME_ON_REMOTE);
    default:
    }
  }

  @Override
  public final RemoteInfo clone() {
    RemoteInfo copy = new RemoteInfo();
    copy.mType = mType;
    copy.mName = mName;
    copy.mOurNameOnRemote = mOurNameOnRemote;
    copy.mDescription = mDescription;
    copy.mRemoteUri = mRemoteUri;
    return copy;
  }

  /**
   * Sets the type for this remote.
   * @param type the type to set it to
   */
  public final void setType(final RemoteType type) {
    mType = type;
    if (mType == null) {
      throw new IllegalArgumentException("Type must not be null.");
    }
    switch (mType) {
    default:
    case HUB:
      break;
    case MERGE_POINT:
      mOurNameOnRemote = null;
      break;
    }
  }

  /**
   * @return the type for this remote
   */
  public final RemoteType getType() {
    return mType;
  }

  /**
   * Sets the name for this remote.
   * @param name the name for this remote
   */
  public final void setName(final String name) {
    mName = name;
  }

  /**
   * @return the name for this remote
   */
  public final String getName() {
    return mName;
  }

  /**
   * Sets the description for this remote.
   * @param description the description
   */
  public final void setDescription(final String description) {
    mDescription = description;
  }

  /**
   * @return the description for this remote
   */
  public final String getDescription() {
    return mDescription;
  }

  /**
   * Set the URI for this remote.
   * @param remoteUri the remote uri
   */
  public final void setRemoteUri(final URIish remoteUri) {
    mRemoteUri = remoteUri;
  }

  /**
   * @return the remote uri
   */
  public final URIish getRemoteUri() {
    return mRemoteUri;
  }

  /**
   * Set our name on the remote. This can only be done for remotes
   * which are in HUB mode.
   * @param ourNameOnRemote the name to set
   */
  public final void setOurNameOnRemote(final String ourNameOnRemote) {
    if (mType != RemoteType.HUB) {
      throw new IllegalArgumentException(
          "ourNameOnRemote is only valid for hubs.");
    }
    mOurNameOnRemote = ourNameOnRemote;
  }

  /**
   * @return our name on the remote or null.
   */
  public final String getOurNameOnRemote() {
    return mOurNameOnRemote;
  }
}




Java Source Code List

interdroid.util.CryptoUtil.java
interdroid.util.DbUtil.java
interdroid.util.FSUtil.java
interdroid.util.StrictUtil.java
interdroid.util.ToastOnUI.java
interdroid.vdb.Actions.java
interdroid.vdb.Authority.java
interdroid.vdb.content.ContentChangeHandler.java
interdroid.vdb.content.CrossProcessCursorWrapper.java
interdroid.vdb.content.DatabaseInitializer.java
interdroid.vdb.content.EntityUriBuilder.java
interdroid.vdb.content.EntityUriMatcher.java
interdroid.vdb.content.GenericContentProvider.java
interdroid.vdb.content.VdbConfig.java
interdroid.vdb.content.VdbMainContentProvider.java
interdroid.vdb.content.VdbProviderRegistry.java
interdroid.vdb.content.avro.AvroContentProviderProxy.java
interdroid.vdb.content.avro.AvroContentProvider.java
interdroid.vdb.content.avro.AvroEntityInfo.java
interdroid.vdb.content.avro.AvroFieldInfo.java
interdroid.vdb.content.avro.AvroMetadata.java
interdroid.vdb.content.avro.AvroProviderRegistry.java
interdroid.vdb.content.avro.AvroSchemaRegistrationHandler.java
interdroid.vdb.content.avro.SchemaEvolutionValidator.java
interdroid.vdb.content.avro.package-info.java
interdroid.vdb.content.metadata.DatabaseFieldType.java
interdroid.vdb.content.metadata.EntityInfo.java
interdroid.vdb.content.metadata.FieldInfo.java
interdroid.vdb.content.metadata.Metadata.java
interdroid.vdb.content.metadata.package-info.java
interdroid.vdb.content.orm.DbEntity.java
interdroid.vdb.content.orm.DbField.java
interdroid.vdb.content.orm.ORMEntityInfo.java
interdroid.vdb.content.orm.ORMFieldInfo.java
interdroid.vdb.content.orm.ORMGenericContentProvider.java
interdroid.vdb.content.orm.ORMMetadata.java
interdroid.vdb.content.orm.package-info.java
interdroid.vdb.content.package-info.java
interdroid.vdb.persistence.api.DirtyCheckoutException.java
interdroid.vdb.persistence.api.MergeInProgressException.java
interdroid.vdb.persistence.api.MergeInfo.java
interdroid.vdb.persistence.api.RemoteInfo.java
interdroid.vdb.persistence.api.VdbCheckout.java
interdroid.vdb.persistence.api.VdbInitializer.java
interdroid.vdb.persistence.api.VdbRepositoryRegistry.java
interdroid.vdb.persistence.api.VdbRepository.java
interdroid.vdb.persistence.api.package-info.java
interdroid.vdb.persistence.impl.MergeHelper.java
interdroid.vdb.persistence.impl.ThreeWayDiffCursor.java
interdroid.vdb.persistence.impl.VdbCheckoutImpl.java
interdroid.vdb.persistence.impl.VdbRepositoryImpl.java
interdroid.vdb.persistence.impl.package-info.java
interdroid.vdb.transport.SmartSocketsDaemonClient.java
interdroid.vdb.transport.SmartSocketsDaemon.java
interdroid.vdb.transport.SmartSocketsTransport.java
interdroid.vdb.transport.SmartsocketsDaemonService.java
interdroid.vdb.transport.VdbRepositoryResolver.java
interdroid.vdb.transport.package-info.java
interdroid.vdb.package-info.java