org.eclipse.equinox.p2.directorywatcher_1.0.300.v20110808-1657

   File[] list = directory.listFiles();
      if (list != null) {
        for (int i = 0; i < list.length; i++)
        {
          File file = list[i];
          if (list[i].getPath().endsWith(".del"))
          {
            File target = new File(file.getPath().substring(0, file.getPath().length() - 4));
            removals.add(target);
            pendingDeletions.add(target);
          }
          else
          {
            scannedFiles.add(file);
            removals.remove(file);
            for (Iterator localIterator = listeners.iterator(); localIterator.hasNext();)
            {
              DirectoryChangeListener listener = (DirectoryChangeListener)localIterator.next();
              if (isInterested(listener, file)) {
                processFile(file, listener);
              }
            }
          }
        }
      }
    }
  }
  
  private void stopPoll()
  {
    notifyRemovals();
    removals = scannedFiles;
    for (Iterator localIterator = listeners.iterator(); localIterator.hasNext();)
    {
      DirectoryChangeListener listener = (DirectoryChangeListener)localIterator.next();
      listener.stopPoll();
    }
    processPendingDeletions();
  }
  
  private boolean isInterested(DirectoryChangeListener listener, File file)
  {
    return listener.isInterested(file);
  }
  
  private void notifyRemovals()
  {
    Set<File> removed = removals;
    Iterator localIterator2;
    for (Iterator localIterator1 = listeners.iterator(); localIterator1.hasNext(); localIterator2.hasNext())
    {
      DirectoryChangeListener listener = (DirectoryChangeListener)localIterator1.next();
      localIterator2 = removed.iterator(); continue;File file = (File)localIterator2.next();
      if (isInterested(listener, file)) {
        listener.removed(file);
      }
    }
  }
  
  private void processFile(File file, DirectoryChangeListener listener)
  {
    try
    {
      Long oldTimestamp = listener.getSeenFile(file);
      if (oldTimestamp == null)
      {
        listener.added(file);
      }
      else
      {
        long lastModified = file.lastModified();
        if (oldTimestamp.longValue() != lastModified) {
          listener.changed(file);
        }
      }
    }
    catch (Exception e)
    {
      log(NLS.bind(Messages.error_processing, listener), e);
    }
  }
  
  private void processPendingDeletions()
  {
    for (Iterator<File> iterator = pendingDeletions.iterator(); iterator.hasNext();)
    {
      File file = (File)iterator.next();
      if ((!file.exists()) || (file.delete())) {
        iterator.remove();
      }
      new File(file.getPath() + ".del").delete();
    }
  }
}

/* Location:
 * Qualified Name:     org.eclipse.equinox.internal.provisional.p2.directorywatcher.DirectoryWatcher
 * Java Class Version: 1.4 (48.0)
 * JD-Core Version:    0.7.1
 */
package org.eclipse.equinox.internal.provisional.p2.directorywatcher;

import java.io.File;
import java.net.URI;
import java.util.HashMap;
import java.util.Map;
import org.eclipse.equinox.p2.metadata.IInstallableUnit;
import org.eclipse.equinox.p2.metadata.MetadataFactory.InstallableUnitDescription;
import org.eclipse.equinox.p2.metadata.Version;
import org.eclipse.equinox.p2.publisher.actions.IPropertyAdvice;
import org.eclipse.equinox.p2.repository.artifact.IArtifactDescriptor;

public class EntryAdvice
  implements IPropertyAdvice
{
  private Map<String, String> metadataProps = new HashMap();
  private Map<String, String> artifactProps = new HashMap();
  
  public boolean isApplicable(String configSpec, boolean includeDefault, String id, Version version)
  {
    return true;
  }
  
  void setProperties(File location, long timestamp, URI reference)
  {
    setProperties(location, timestamp, reference, null);
  }
  
  void setProperties(File location, long timestamp, URI reference, String linkFile)
  {
    if (reference == null) {
      artifactProps.remove("artifact.reference");
    } else {
      artifactProps.put("artifact.reference", reference.toString());
    }
    if (location.isDirectory()) {
      artifactProps.put("artifact.folder", Boolean.TRUE.toString());
    } else {
      artifactProps.remove("artifact.folder");
    }
    artifactProps.put("file.name", location.getAbsolutePath());
    metadataProps.put("file.name", location.getAbsolutePath());
    metadataProps.put("file.lastModified", Long.toString(timestamp));
    if (linkFile != null) {
      metadataProps.put("org.eclipse.update.site.linkFile", linkFile);
    }
  }
  
  public Map<String, String> getArtifactProperties(IInstallableUnit iu, IArtifactDescriptor descriptor)
  {
    return artifactProps;
  }
  
  public Map<String, String> getInstallableUnitProperties(MetadataFactory.InstallableUnitDescription iu)
  {
    return metadataProps;
  }
}

/* Location:
 * Qualified Name:     org.eclipse.equinox.internal.provisional.p2.directorywatcher.EntryAdvice
 * Java Class Version: 1.4 (48.0)
 * JD-Core Version:    0.7.1
 */
package org.eclipse.equinox.internal.provisional.p2.directorywatcher;

import org.eclipse.osgi.util.NLS;

public class Messages
  extends NLS
{
  private static final String BUNDLE_NAME = "org.eclipse.equinox.internal.provisional.p2.directorywatcher.messages";
  public static String artifact_repo_manager_not_registered;
  public static String error_main_loop;
  public static String error_processing;
  public static String failed_create_artifact_repo;
  public static String failed_create_metadata_repo;
  public static String filename_missing;
  public static String metadata_repo_manager_not_registered;
  public static String null_folder;
  public static String thread_not_started;
  public static String thread_started;
  
  static
  {
    NLS.initializeMessages("org.eclipse.equinox.internal.provisional.p2.directorywatcher.messages", Messages.class);
  }
}

/* Location:
 * Qualified Name:     org.eclipse.equinox.internal.provisional.p2.directorywatcher.Messages
 * Java Class Version: 1.4 (48.0)
 * JD-Core Version:    0.7.1
 */
package org.eclipse.equinox.internal.provisional.p2.directorywatcher;

import java.io.File;
import java.net.URI;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.core.runtime.Status;
import org.eclipse.equinox.internal.p2.artifact.repository.simple.SimpleArtifactDescriptor;
import org.eclipse.equinox.internal.p2.core.helpers.LogHelper;
import org.eclipse.equinox.internal.p2.core.helpers.Tracing;
import org.eclipse.equinox.p2.core.ProvisionException;
import org.eclipse.equinox.p2.metadata.IInstallableUnit;
import org.eclipse.equinox.p2.publisher.IPublisherAction;
import org.eclipse.equinox.p2.publisher.IPublisherResult;
import org.eclipse.equinox.p2.publisher.PublisherInfo;
import org.eclipse.equinox.p2.publisher.PublisherResult;
import org.eclipse.equinox.p2.publisher.eclipse.BundlesAction;
import org.eclipse.equinox.p2.publisher.eclipse.FeaturesAction;
import org.eclipse.equinox.p2.query.IQuery;
import org.eclipse.equinox.p2.query.IQueryResult;
import org.eclipse.equinox.p2.query.IQueryable;
import org.eclipse.equinox.p2.query.QueryUtil;
import org.eclipse.equinox.p2.repository.artifact.ArtifactDescriptorQuery;
import org.eclipse.equinox.p2.repository.artifact.IArtifactDescriptor;
import org.eclipse.equinox.p2.repository.artifact.IArtifactRepository;
import org.eclipse.equinox.p2.repository.artifact.IArtifactRepositoryManager;
import org.eclipse.equinox.p2.repository.metadata.IMetadataRepository;
import org.eclipse.equinox.p2.repository.metadata.IMetadataRepositoryManager;
import org.eclipse.osgi.service.resolver.BundleDescription;
import org.eclipse.osgi.util.NLS;

public class RepositoryListener
  extends DirectoryChangeListener
{
  public static final String ARTIFACT_FOLDER = "artifact.folder";
  public static final String ARTIFACT_REFERENCE = "artifact.reference";
  public static final String FILE_LAST_MODIFIED = "file.lastModified";
  public static final String FILE_NAME = "file.name";
  private final IMetadataRepository metadataRepository;
  private final CachingArtifactRepository artifactRepository;
  private final Map<File, Long> currentFiles = new HashMap();
  private final Collection<File> polledSeenFiles = new HashSet();
  private EntryAdvice advice = new EntryAdvice();
  private PublisherInfo info;
  private IPublisherResult iusToAdd;
  private IPublisherResult iusToChange;
  
  public RepositoryListener(String repositoryName, Map<String, String> properties)
  {
    URI location = Activator.getDefaultRepositoryLocation(this, repositoryName);
    metadataRepository = initializeMetadataRepository(repositoryName, location, properties);
    artifactRepository = initializeArtifactRepository(repositoryName, location, properties);
    initializePublisher();
  }
  
  public RepositoryListener(IMetadataRepository metadataRepository, IArtifactRepository artifactRepository)
  {
    this.artifactRepository = new CachingArtifactRepository(artifactRepository);
    this.metadataRepository = metadataRepository;
    initializePublisher();
  }
  
  private void initializePublisher()
  {
    info = new PublisherInfo();
    info.setArtifactRepository(artifactRepository);
    info.setMetadataRepository(metadataRepository);
    info.addAdvice(advice);
    info.setArtifactOptions(9);
  }
  
  protected CachingArtifactRepository initializeArtifactRepository(String name, URI repositoryLocation, Map<String, String> properties)
  {
    IArtifactRepositoryManager manager = Activator.getArtifactRepositoryManager();
    if (manager == null) {
      throw new IllegalStateException(Messages.artifact_repo_manager_not_registered);
    }
    try
    {
      IArtifactRepository result = manager.loadRepository(repositoryLocation, null);
      return result == null ? null : new CachingArtifactRepository(result);
    }
    catch (ProvisionException localProvisionException1)
    {
      try
      {
        IArtifactRepository result = manager.createRepository(repositoryLocation, name, "org.eclipse.equinox.p2.artifact.repository.simpleRepository", properties);
        return result == null ? null : new CachingArtifactRepository(result);
      }
      catch (ProvisionException e)
      {
        LogHelper.log(e);
        throw new IllegalStateException(NLS.bind(Messages.failed_create_artifact_repo, repositoryLocation));
      }
    }
  }
  
  protected IMetadataRepository initializeMetadataRepository(String name, URI repositoryLocation, Map<String, String> properties)
  {
    IMetadataRepositoryManager manager = Activator.getMetadataRepositoryManager();
    if (manager == null) {
      throw new IllegalStateException(Messages.metadata_repo_manager_not_registered);
    }
    try
    {
      return manager.loadRepository(repositoryLocation, null);
    }
    catch (ProvisionException localProvisionException1)
    {
      try
      {
        return manager.createRepository(repositoryLocation, name, "org.eclipse.equinox.p2.metadata.repository.simpleRepository", properties);
      }
      catch (ProvisionException e)
      {
        LogHelper.log(e);
        throw new IllegalStateException(NLS.bind(Messages.failed_create_metadata_repo, repositoryLocation));
      }
    }
  }
  
  public boolean added(File file)
  {
    return process(file, true);
  }
  
  public boolean changed(File file)
  {
    return process(file, false);
  }
  
  public boolean removed(File file)
  {
    return currentFiles.containsKey(file);
  }
  
  private boolean process(File file, boolean isAddition)
  {
    boolean isDirectory = file.isDirectory();
    if ((isDirectory) && (file.getParentFile() != null) && (file.getParentFile().getName().equals("features")) && (new File(file, "feature.xml").exists())) {
      return processFeature(file, isAddition);
    }
    if ((isDirectory) || (file.getName().endsWith(".jar"))) {
      return processBundle(file, isDirectory, isAddition);
    }
    return false;
  }
  
  private boolean processBundle(File file, boolean isDirectory, boolean isAddition)
  {
    BundleDescription bundleDescription = BundlesAction.createBundleDescription(file);
    if (bundleDescription == null) {
      return false;
    }
    advice.setProperties(file, file.lastModified(), file.toURI());
    return publish(new BundlesAction(new BundleDescription[] { bundleDescription }), isAddition);
  }
  
  private boolean processFeature(File file, boolean isAddition)
  {
    String link = (String)metadataRepository.getProperties().get("org.eclipse.update.site.linkFile");
    advice.setProperties(file, file.lastModified(), file.toURI(), link);
    return publish(new FeaturesAction(new File[] { file }), isAddition);
  }
  
  private boolean publish(IPublisherAction action, boolean isAddition)
  {
    IPublisherResult result = isAddition ? iusToAdd : iusToChange;
    return action.perform(info, result, new NullProgressMonitor()).isOK();
  }
  
  public boolean isInterested(File file)
  {
    return true;
  }
  
  public Long getSeenFile(File file)
  {
    Long lastSeen = (Long)currentFiles.get(file);
    if (lastSeen != null) {
      polledSeenFiles.add(file);
    }
    return lastSeen;
  }
  
  public void startPoll()
  {
    iusToAdd = new PublisherResult();
    iusToChange = new PublisherResult();
    synchronizeCurrentFiles();
  }
  
  public void stopPoll()
  {
    Set<File> filesToRemove = new HashSet(currentFiles.keySet());
    filesToRemove.removeAll(polledSeenFiles);
    polledSeenFiles.clear();
    
    synchronizeMetadataRepository(filesToRemove);
    synchronizeArtifactRepository(filesToRemove);
    iusToAdd = null;
    iusToChange = null;
  }
  
  private void synchronizeMetadataRepository(Collection<File> removedFiles)
  {
    if (metadataRepository == null) {
      return;
    }
    Collection<IInstallableUnit> changes = iusToChange.getIUs(null, null);
    if ((!removedFiles.isEmpty()) || (!changes.isEmpty()))
    {
      metadataRepository.removeInstallableUnits(changes);
      
      Collection<String> paths = new HashSet(removedFiles.size());
      for (Iterator localIterator = removedFiles.iterator(); localIterator.hasNext();)
      {
        File file = (File)localIterator.next();
        paths.add(file.getAbsolutePath());
      }
      IQuery<IInstallableUnit> removeQuery = QueryUtil.createMatchQuery(
        "$1.exists(x | properties[$0] == x)", new Object[] { "file.name", paths });
      Object toRemove = metadataRepository.query(removeQuery, null);
      metadataRepository.removeInstallableUnits(((IQueryResult)toRemove).toUnmodifiableSet());
    }
    Collection<IInstallableUnit> additions = iusToAdd.getIUs(null, null);
    additions.addAll(changes);
    if (!additions.isEmpty()) {
      metadataRepository.addInstallableUnits(additions);
    }
  }
  
  private void synchronizeArtifactRepository(Collection<File> removedFiles)
  {
    if (artifactRepository == null) {
      return;
    }
    if (!removedFiles.isEmpty())
    {
      IArtifactDescriptor[] descriptors = (IArtifactDescriptor[])artifactRepository.descriptorQueryable().query(ArtifactDescriptorQuery.ALL_DESCRIPTORS, null).toArray(IArtifactDescriptor.class);
      IArtifactDescriptor[] arrayOfIArtifactDescriptor1;
      int j = (arrayOfIArtifactDescriptor1 = descriptors).length;
      for (int i = 0; i < j; i++)
      {
        IArtifactDescriptor d = arrayOfIArtifactDescriptor1[i];
        SimpleArtifactDescriptor descriptor = (SimpleArtifactDescriptor)d;
        String filename = descriptor.getRepositoryProperty("file.name");
        if (filename == null)
        {
          if (Tracing.DEBUG)
          {
            String message = NLS.bind(Messages.filename_missing, "artifact", descriptor.getArtifactKey());
            LogHelper.log(new Status(4, "org.eclipse.equinox.p2.directorywatcher", message, null));
          }
        }
        else
        {
          File artifactFile = new File(filename);
          if (removedFiles.contains(artifactFile)) {
            artifactRepository.removeDescriptor(descriptor);
          }
        }
      }
    }
    artifactRepository.save();
  }
  
  private void synchronizeCurrentFiles()
  {
    currentFiles.clear();
    if (metadataRepository != null)
    {
      IQueryResult<IInstallableUnit> ius = metadataRepository.query(QueryUtil.createIUAnyQuery(), null);
      for (Iterator<IInstallableUnit> it = ius.iterator(); it.hasNext();)
      {
        IInstallableUnit iu = (IInstallableUnit)it.next();
        String filename = iu.getProperty("file.name");
        if (filename == null)
        {
          if (Tracing.DEBUG)
          {
            String message = NLS.bind(Messages.filename_missing, "installable unit", iu.getId());
            LogHelper.log(new Status(4, "org.eclipse.equinox.p2.directorywatcher", message, null));
          }
        }
        else
        {
          File iuFile = new File(filename);
          Long iuLastModified = new Long(iu.getProperty("file.lastModified"));
          currentFiles.put(iuFile, iuLastModified);
        }
      }
    }
  }
  
  public IMetadataRepository getMetadataRepository()
  {
    return metadataRepository;
  }
  
  public IArtifactRepository getArtifactRepository()
  {
    return artifactRepository;
  }
}

/* Location:
 * Qualified Name:     org.eclipse.equinox.internal.provisional.p2.directorywatcher.RepositoryListener
 * Java Class Version: 1.4 (48.0)
 * JD-Core Version:    0.7.1
 */
1 2

Further reading...

For more information on Java 1.5 Tiger, you may find Java 1.5 Tiger, A developer's Notebook by D. Flanagan and B. McLaughlin from O'Reilly of interest.

New!JAR listings


Copyright 2006-2019. Infinite Loop Ltd