incubator-depot-cvs mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From nickcha...@apache.org
Subject svn commit: rev 10308 - incubator/depot/trunk/update/src/java/org/apache/depot/update/ant
Date Tue, 27 Apr 2004 07:20:45 GMT
Author: nickchalko
Date: Tue Apr 27 00:20:44 2004
New Revision: 10308

Modified:
   incubator/depot/trunk/update/src/java/org/apache/depot/update/ant/CachedResourceSet.java
Log:
Used the old Select methods to download I had used before.
Added a AntUtils static class.

Modified: incubator/depot/trunk/update/src/java/org/apache/depot/update/ant/CachedResourceSet.java
==============================================================================
--- incubator/depot/trunk/update/src/java/org/apache/depot/update/ant/CachedResourceSet.java
(original)
+++ incubator/depot/trunk/update/src/java/org/apache/depot/update/ant/CachedResourceSet.java
Tue Apr 27 00:20:44 2004
@@ -22,10 +22,17 @@
 import java.util.List;
 import org.apache.depot.update.ResourceUpdater;
 import org.apache.depot.update.UpdateException;
+import org.apache.depot.update.impl.RepositorySetWrapper;
+import org.apache.depot.update.impl.ResourceUpdaterContext;
+import org.apache.depot.update.protocols.DefaultProtocolOperationsManager;
+import org.apache.depot.update.query.DefaultQueryEngine;
+import org.apache.depot.update.query.ResourceQuery;
+import org.apache.depot.update.query.ResourceResult;
 import org.apache.depot.update.repository.DefaultRepository;
 import org.apache.depot.update.repository.IRepository;
 import org.apache.depot.update.repository.RepositorySet;
-import org.apache.depot.update.resource.ResourceGroup;
+import org.apache.depot.update.resource.Resource;
+import org.apache.depot.update.resource.compare.VersionComparator;
 import org.apache.depot.update.util.io.ResolvedFile;
 import org.apache.tools.ant.BuildException;
 import org.apache.tools.ant.DirectoryScanner;
@@ -47,8 +54,8 @@
 			this);
 	private final List resourceList = new ArrayList();
 	private ResourceUpdater m_updater = null;
-	private boolean isAlwaysCheckRemote=false;
-	private boolean checkClassPath=true;
+	private boolean isAlwaysCheckRemote = false;
+	private boolean checkClassPath = true;
 	/**
 	 *  
 	 */
@@ -130,8 +137,7 @@
 	 * @throws UpdateException
 	 */
 	private File getLocalFile(ResourceElement r) throws UpdateException {
-		// TODO respect the ant build.classpath  property.
-		
+		// TODO respect the ant build.classpath property.
 		File file = null;
 		if (!isAlwaysCheckRemote) {
 			file = getCachedFile(r);
@@ -146,49 +152,61 @@
 	 * @param r
 	 */
 	private void cacheFile(ResourceElement r) {
-		// TODO Auto-generated method stub
+		try {
+			ResourceResult res = query(r, r.getIRepository());
+			if (res.size() == 0) {
+				return;
+			} else if (res.size() == 1) {
+				downLoadRemoteResult(res);
+				return;
+			} else {
+				log("Found " + res.size() + " matches for " + r
+						+ " returning first", Project.MSG_VERBOSE);
+				downLoadRemoteResult(res);
+				return;
+			}
+		} catch (Exception e) {
+			throw new BuildException("Unable to get local copy of " + r, e);
+		}
 	}
 	/**
 	 * @param r
 	 */
-	private File getCachedFile(ResourceElement r) {
-		// TODO Auto-generated method stub
-		return null;
-	}
-	/**
-	 * @return
-	 */
-	private IRepository getLocalRepository() {
-		DefaultRepository repository = new DefaultRepository(
-				LOCAL_REPOSITORY_ID, ResolvedFile.resolve(getRepoDir()));
-		return repository;
+	private File getCachedFile(ResourceElement r) throws UpdateException {
+		try {
+			ResourceResult res = query(r, getLocalRepository());
+			if (res.size() == 0) {
+				return null;
+			} else if (res.size() == 1) {
+				return getFirstFile(res);
+			} else {
+				log("Found " + res.size() + " matches for " + r
+						+ " returning first", Project.MSG_VERBOSE);
+				return getFirstFile(res);
+			}
+		} catch (Exception e) {
+			throw new BuildException("Error trying to get local copy of " + r,
+					e);
+		}
 	}
 	/**
 	 * @return
 	 */
 	private File getRepoDir() {
-		String repoDir = project.getProperty(REPOSITORY_DIR_PROPERTY);
-		if (repoDir == null) {
-			repoDir = getDepotHome() + "/" + DEFAULT_LOCAL_REPOSITORY_DIR_NAME;
-		}
-		File repo = new File(repoDir);
-		if (!repo.exists()) {
-			repo.mkdirs();
+		// TODO cache this.
+		File repo = AntUtils.findAndCreateDirFromProperty(getProject(),
+				REPOSITORY_DIR_PROPERTY);
+		if (repo == null) {
+			repo = new File(AntUtils.getDepotHome(getProject()),
+					DEFAULT_LOCAL_REPOSITORY_DIR_NAME);
 		}
+		AntUtils.createAndValidateDir(getProject(), repo,
+				"Local Repository Cache", true);
 		return repo;
 	}
 	/**
-	 * @return
-	 */
-	private String getDepotHome() {
-		final String property = project.getProperty("depot.home");
-		if (property == null) {
-			throw new BuildException("The property depot.home must be set");
-		}
-		return property;
-	}
-	/**
 	 * Should the cached set always look the remore repository for updates
+	 * 
 	 * @return Returns the isAlwaysCheckRemote.
 	 */
 	public boolean isAlwaysCheckRemote() {
@@ -196,9 +214,69 @@
 	}
 	/**
 	 * Should the cached set always look the remore repository for updates
-	 * @param isAlwaysCheckRemote The isAlwaysCheckRemote to set.
+	 * 
+	 * @param isAlwaysCheckRemote
+	 *            The isAlwaysCheckRemote to set.
 	 */
 	public void setAlwaysCheckRemote(boolean isAlwaysCheckRemote) {
 		this.isAlwaysCheckRemote = isAlwaysCheckRemote;
+	}
+	/**
+	 * @param r
+	 * @param res
+	 * @return
+	 */
+	private File getFirstFile(ResourceResult res) throws Exception {
+		Resource resource = getFirstResource(res);
+		return resource.getLocation().getFile();
+	}
+	/**
+	 * @param res
+	 * @return
+	 */
+	private Resource getFirstResource(ResourceResult res) {
+		List l = (List) res.get(0);
+		Resource resource = (Resource) l.get(0);
+		return resource;
+	}
+	private ResourceResult query(ResourceElement r, IRepository repo) { //TODO
+		// experiment
+		log("Looking for " + r, Project.MSG_DEBUG);
+		try {
+			DefaultQueryEngine dqe = new DefaultQueryEngine();
+			ResourceQuery query = new ResourceQuery(r.getResourceGroup(), r
+					.getResourceTypeSelector(), null, VersionComparator.REVERSE);
+			//TODO need to be a better way to sort by.
+			ResourceUpdaterContext context = new ResourceUpdaterContext();
+			context.setProtocolManager(new DefaultProtocolOperationsManager(
+					context));
+			RepositorySet repositorySet = RepositorySet.getRepositorySet(
+					getId(), true);
+			repositorySet.addRepository(repo);
+			RepositorySetWrapper rw = new RepositorySetWrapper(repositorySet,
+					context);
+			ResourceResult result = dqe.queryRepositories(rw, query);
+			return result;
+		} catch (UpdateException e) {
+			throw new BuildException("Unable to get " + r, e);
+		}
+	}
+	/**
+	 * @return
+	 */
+	private IRepository getLocalRepository() {
+		DefaultRepository repository = new DefaultRepository(
+				LOCAL_REPOSITORY_ID, ResolvedFile.resolve(getRepoDir()));
+		return repository;
+	}
+	/**
+	 * @param res
+	 * @throws RuperException
+	 */
+	private void downLoadRemoteResult(ResourceResult res)
+			throws UpdateException {
+		Resource resource = getFirstResource(res);
+		ResourceUpdater ru = new ResourceUpdater();
+		ru.downloadResource(resource, getLocalRepository());
 	}
 }

Mime
View raw message