* Fixing ArtifactsByChecksumConstraint for null types.

* Adding anonymous type constructor for ArtifactsByChecksumConstraint.
* Removing DatabaseSearch.  (We don't want a repeat of the mess that exists within continuum!)



git-svn-id: https://svn.apache.org/repos/asf/maven/archiva/trunk@551997 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Joakim Erdfelt 2007-06-29 18:52:49 +00:00
parent cd0a9d9d8b
commit 029c0c9789
6 changed files with 51 additions and 211 deletions

View File

@ -19,6 +19,7 @@ package org.apache.maven.archiva.database.constraints;
* under the License. * under the License.
*/ */
import org.apache.commons.lang.StringUtils;
import org.apache.maven.archiva.database.Constraint; import org.apache.maven.archiva.database.Constraint;
/** /**
@ -33,19 +34,42 @@ public class ArtifactsByChecksumConstraint
{ {
private String whereClause; private String whereClause;
public static final String SHA1_CONDITION = "SHA1"; public static final String SHA1 = "SHA1";
public static final String MD5_CONDITION = "MD5"; public static final String MD5 = "MD5";
public ArtifactsByChecksumConstraint( String desiredChecksum, String condition ) /**
* Create constraint for checksum (without providing type)
*
* @param desiredChecksum the checksum (either SHA1 or MD5)
*/
public ArtifactsByChecksumConstraint( String desiredChecksum )
{ {
if ( !condition.equals( SHA1_CONDITION ) && !condition.equals( MD5_CONDITION ) ) this( desiredChecksum, null );
}
/**
* Create constraint for specific checksum.
*
* @param desiredChecksum the checksum (either SHA1 or MD5)
* @param type the type of checksum (either {@link #SHA1} or {@link #MD5})
*/
public ArtifactsByChecksumConstraint( String desiredChecksum, String type )
{
if( StringUtils.isEmpty( type ) )
{ {
// default for no specified type.
whereClause = "this.checksumSHA1 == desiredChecksum || this.checksumMD5 == desiredChecksum"; whereClause = "this.checksumSHA1 == desiredChecksum || this.checksumMD5 == desiredChecksum";
} }
else if ( condition.equals( SHA1_CONDITION ) || condition.equals( MD5_CONDITION ) ) else if ( !type.equals( SHA1 ) && !type.equals( MD5 ) )
{ {
whereClause = "this.checksum" + condition.trim() + " == desiredChecksum"; // default for type that isn't recognized.
whereClause = "this.checksumSHA1 == desiredChecksum || this.checksumMD5 == desiredChecksum";
}
else if ( type.equals( SHA1 ) || type.equals( MD5 ) )
{
// specific type.
whereClause = "this.checksum" + type.trim() + " == desiredChecksum";
} }
declParams = new String[]{ "String desiredChecksum" }; declParams = new String[]{ "String desiredChecksum" };

View File

@ -1,44 +0,0 @@
package org.apache.maven.archiva.database.search;
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
import org.apache.maven.archiva.database.ObjectNotFoundException;
import org.apache.maven.archiva.database.ArchivaDatabaseException;
import java.util.List;
/**
* Class for searching the database.
*
* @author <a href="mailto:oching@apache.org">Maria Odea Ching</a>
*/
public interface DatabaseSearch
{
/**
* Get artifact(s) with the specified checksum
*
* @param checksum
* @return
*/
public List searchArtifactsByChecksum( String checksum )
throws ObjectNotFoundException, ArchivaDatabaseException;
}

View File

@ -1,58 +0,0 @@
package org.apache.maven.archiva.database.search;
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
import org.apache.maven.archiva.database.ArchivaDAO;
import org.apache.maven.archiva.database.Constraint;
import org.apache.maven.archiva.database.ObjectNotFoundException;
import org.apache.maven.archiva.database.ArchivaDatabaseException;
import org.apache.maven.archiva.database.constraints.ArtifactsByChecksumConstraint;
import org.codehaus.plexus.logging.AbstractLogEnabled;
import java.util.List;
/**
* @author <a href="mailto:oching@apache.org">Maria Odea Ching</a>
* @plexus.component role="org.apache.maven.archiva.database.search.DatabaseSearch" role-hint="default"
*/
public class DefaultDatabaseSearch
extends AbstractLogEnabled
implements DatabaseSearch
{
/**
* @plexus.requirement role-hint="jdo"
*/
private ArchivaDAO dao;
public List searchArtifactsByChecksum( String checksum )
throws ObjectNotFoundException, ArchivaDatabaseException
{
Constraint constraint = new ArtifactsByChecksumConstraint( checksum.toLowerCase().trim(), "" );
List results = dao.getArtifactDAO().queryArtifacts( constraint );
if ( results != null )
{
getLogger().info( "Number of database hits : " + results.size() );
}
return results;
}
}

View File

@ -105,11 +105,11 @@ public class ArtifactsByChecksumConstraintTest
artifactDao.saveArtifact( artifact ); artifactDao.saveArtifact( artifact );
assertConstraint( "Artifacts by SHA1 Checksum", 4, assertConstraint( "Artifacts by SHA1 Checksum", 4,
new ArtifactsByChecksumConstraint( SHA1_HASH1, ArtifactsByChecksumConstraint.SHA1_CONDITION ) ); new ArtifactsByChecksumConstraint( SHA1_HASH1, ArtifactsByChecksumConstraint.SHA1 ) );
assertConstraint( "Artifacts by SHA1 Checksum", 2, assertConstraint( "Artifacts by SHA1 Checksum", 2,
new ArtifactsByChecksumConstraint( SHA1_HASH2, ArtifactsByChecksumConstraint.SHA1_CONDITION ) ); new ArtifactsByChecksumConstraint( SHA1_HASH2, ArtifactsByChecksumConstraint.SHA1 ) );
assertConstraint( "Artifacts by SHA1 Checksum", 1, assertConstraint( "Artifacts by SHA1 Checksum", 1,
new ArtifactsByChecksumConstraint( SHA1_HASH3, ArtifactsByChecksumConstraint.SHA1_CONDITION ) ); new ArtifactsByChecksumConstraint( SHA1_HASH3, ArtifactsByChecksumConstraint.SHA1 ) );
} }
public void testConstraintMD5() public void testConstraintMD5()
@ -146,11 +146,11 @@ public class ArtifactsByChecksumConstraintTest
artifactDao.saveArtifact( artifact ); artifactDao.saveArtifact( artifact );
assertConstraint( "Artifacts by MD5 Checksum", 4, assertConstraint( "Artifacts by MD5 Checksum", 4,
new ArtifactsByChecksumConstraint( MD5_HASH1, ArtifactsByChecksumConstraint.MD5_CONDITION ) ); new ArtifactsByChecksumConstraint( MD5_HASH1, ArtifactsByChecksumConstraint.MD5 ) );
assertConstraint( "Artifacts by MD5 Checksum", 2, assertConstraint( "Artifacts by MD5 Checksum", 2,
new ArtifactsByChecksumConstraint( MD5_HASH2, ArtifactsByChecksumConstraint.MD5_CONDITION ) ); new ArtifactsByChecksumConstraint( MD5_HASH2, ArtifactsByChecksumConstraint.MD5 ) );
assertConstraint( "Artifacts by MD5 Checksum", 1, assertConstraint( "Artifacts by MD5 Checksum", 1,
new ArtifactsByChecksumConstraint( MD5_HASH3, ArtifactsByChecksumConstraint.MD5_CONDITION ) ); new ArtifactsByChecksumConstraint( MD5_HASH3, ArtifactsByChecksumConstraint.MD5 ) );
} }
public void testConstraintOR() public void testConstraintOR()
@ -186,9 +186,9 @@ public class ArtifactsByChecksumConstraintTest
artifact.getModel().setChecksumMD5( MD5_HASH2 ); artifact.getModel().setChecksumMD5( MD5_HASH2 );
artifactDao.saveArtifact( artifact ); artifactDao.saveArtifact( artifact );
assertConstraint( "Artifacts by MD5 Checksum", 4, new ArtifactsByChecksumConstraint( MD5_HASH1, "" ) ); assertConstraint( "Artifacts by MD5 Checksum", 4, new ArtifactsByChecksumConstraint( MD5_HASH1 ) );
assertConstraint( "Artifacts by MD5 Checksum", 2, new ArtifactsByChecksumConstraint( MD5_HASH2, "" ) ); assertConstraint( "Artifacts by MD5 Checksum", 2, new ArtifactsByChecksumConstraint( MD5_HASH2 ) );
assertConstraint( "Artifacts by MD5 Checksum", 1, new ArtifactsByChecksumConstraint( MD5_HASH3, "" ) ); assertConstraint( "Artifacts by MD5 Checksum", 1, new ArtifactsByChecksumConstraint( MD5_HASH3 ) );
} }

View File

@ -1,85 +0,0 @@
package org.apache.maven.archiva.database.search;
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
import org.apache.maven.archiva.database.AbstractArchivaDatabaseTestCase;
import org.apache.maven.archiva.database.ArtifactDAO;
import org.apache.maven.archiva.database.ArchivaDAO;
import org.apache.maven.archiva.database.browsing.RepositoryBrowsing;
import org.apache.maven.archiva.model.ArchivaArtifact;
import java.util.Date;
import java.util.List;
/**
* DatabaseSearchTest
*
* @author <a href="mailto:oching@apache.org">Maria Odea Ching</a>
* @version
*/
public class DatabaseSearchTest
extends AbstractArchivaDatabaseTestCase
{
private ArtifactDAO artifactDao;
private static final String MD5_HASH1 = "53e3b856aa1a3f3cb7fe0f7ac6163aaf";
private static final String SHA1_HASH1 = "232f01b24b1617c46a3d4b0ab3415bc9237dcdec";
private DatabaseSearch dbSearch;
protected void setUp()
throws Exception
{
super.setUp();
artifactDao = ( ( ArchivaDAO ) lookup( ArchivaDAO.ROLE, "jdo" ) ).getArtifactDAO();
dbSearch = (DatabaseSearch) lookup( DatabaseSearch.class.getName() );
}
public ArchivaArtifact createArtifact( String groupId, String artifactId, String version )
{
ArchivaArtifact artifact = artifactDao.createArtifact( groupId, artifactId, version, "", "jar" );
artifact.getModel().setLastModified( new Date() );
artifact.getModel().setRepositoryId( "testable_repo" );
return artifact;
}
public void testSearchByChecksum()
throws Exception
{
ArchivaArtifact artifact;
// Setup artifacts in fresh DB.
artifact = createArtifact( "org.apache.maven.test", "test-one", "1.2" );
artifact.getModel().setChecksumMD5( MD5_HASH1 );
artifactDao.saveArtifact( artifact );
artifact = createArtifact( "org.apache.maven.test.foo", "test-two", "1.0" );
artifact.getModel().setChecksumSHA1( SHA1_HASH1 );
artifactDao.saveArtifact( artifact );
List results = dbSearch.searchArtifactsByChecksum( MD5_HASH1 );
assertEquals( 1, results.size() );
results = dbSearch.searchArtifactsByChecksum( SHA1_HASH1 );
assertEquals( 1, results.size() );
}
}

View File

@ -20,12 +20,14 @@ package org.apache.maven.archiva.web.action;
*/ */
import org.apache.commons.lang.StringUtils; import org.apache.commons.lang.StringUtils;
import org.apache.maven.archiva.database.ArchivaDAO;
import org.apache.maven.archiva.database.Constraint;
import org.apache.maven.archiva.database.constraints.ArtifactsByChecksumConstraint;
import org.apache.maven.archiva.indexer.RepositoryIndexException; import org.apache.maven.archiva.indexer.RepositoryIndexException;
import org.apache.maven.archiva.indexer.RepositoryIndexSearchException; import org.apache.maven.archiva.indexer.RepositoryIndexSearchException;
import org.apache.maven.archiva.indexer.search.CrossRepositorySearch; import org.apache.maven.archiva.indexer.search.CrossRepositorySearch;
import org.apache.maven.archiva.indexer.search.SearchResultLimits; import org.apache.maven.archiva.indexer.search.SearchResultLimits;
import org.apache.maven.archiva.indexer.search.SearchResults; import org.apache.maven.archiva.indexer.search.SearchResults;
import org.apache.maven.archiva.database.search.DatabaseSearch;
import org.codehaus.plexus.xwork.action.PlexusActionSupport; import org.codehaus.plexus.xwork.action.PlexusActionSupport;
import java.net.MalformedURLException; import java.net.MalformedURLException;
@ -44,6 +46,11 @@ public class SearchAction
*/ */
private String q; private String q;
/**
* @plexus.requirement role-hint="jdo"
*/
private ArchivaDAO dao;
/** /**
* The Search Results. * The Search Results.
*/ */
@ -60,11 +67,6 @@ public class SearchAction
private List databaseResults; private List databaseResults;
/**
* @plexus.requirement role-hint="default"
*/
private DatabaseSearch databaseSearch;
public String quickSearch() public String quickSearch()
throws MalformedURLException, RepositoryIndexException, RepositoryIndexSearchException throws MalformedURLException, RepositoryIndexException, RepositoryIndexSearchException
{ {
@ -109,7 +111,8 @@ public class SearchAction
return INPUT; return INPUT;
} }
databaseResults = databaseSearch.searchArtifactsByChecksum( q ); Constraint constraint = new ArtifactsByChecksumConstraint( q );
databaseResults = dao.getArtifactDAO().queryArtifacts( constraint );
if ( databaseResults.isEmpty() ) if ( databaseResults.isEmpty() )
{ {
@ -119,7 +122,7 @@ public class SearchAction
if ( databaseResults.size() == 1 ) if ( databaseResults.size() == 1 )
{ {
// 1 hit? return it's information directly! // 1 hit? return it's information directly!
return ARTIFACT; return ARTIFACT;
} }
else else