diff options
author | Joakim Erdfelt <joakime@apache.org> | 2007-05-01 13:53:14 +0000 |
---|---|---|
committer | Joakim Erdfelt <joakime@apache.org> | 2007-05-01 13:53:14 +0000 |
commit | 42ced36dd2b918a6229b371d18740254ad813fd4 (patch) | |
tree | 5f8d1b31b1e8b46c278bc2979b971835b40408d2 /archiva-database | |
parent | 9a692f3cc7acd9ad51e4fda442b2a4c858efa679 (diff) | |
parent | 4c9789b2284a3d8bde67405fe8651fbf674dbca8 (diff) | |
download | archiva-42ced36dd2b918a6229b371d18740254ad813fd4.tar.gz archiva-42ced36dd2b918a6229b371d18740254ad813fd4.zip |
Making archiva-jpox-database-refactor branch the new trunk.
See archiva-dev mailing list thread ...
http://www.nabble.com/-VOTE--Merge-Archiva-Database-Branch-to-Trunk-tf3648867.html
git-svn-id: https://svn.apache.org/repos/asf/maven/archiva/trunk@534090 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'archiva-database')
64 files changed, 5002 insertions, 1237 deletions
diff --git a/archiva-database/pom.xml b/archiva-database/pom.xml index f978fdd2f..25b1fbd3a 100755 --- a/archiva-database/pom.xml +++ b/archiva-database/pom.xml @@ -22,7 +22,7 @@ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> <parent> <groupId>org.apache.maven.archiva</groupId> - <artifactId>archiva</artifactId> + <artifactId>archiva-parent</artifactId> <version>1.0-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> @@ -30,30 +30,44 @@ <name>Archiva Database</name> <dependencies> <dependency> - <groupId>org.apache.ibatis</groupId> - <artifactId>ibatis-sqlmap</artifactId> - <version>2.3.0</version> + <groupId>org.apache.maven.archiva</groupId> + <artifactId>archiva-consumer-api</artifactId> </dependency> <dependency> - <groupId>org.apache.maven</groupId> - <artifactId>maven-artifact-manager</artifactId> + <groupId>org.apache.maven.archiva</groupId> + <artifactId>archiva-configuration</artifactId> </dependency> <dependency> - <groupId>org.apache.maven</groupId> - <artifactId>maven-project</artifactId> - </dependency> - <dependency> - <groupId>org.apache.maven</groupId> - <artifactId>maven-repository-metadata</artifactId> + <groupId>org.apache.maven.archiva</groupId> + <artifactId>archiva-model</artifactId> </dependency> <dependency> <groupId>org.codehaus.plexus</groupId> - <artifactId>plexus-ibatis</artifactId> - <version>1.0-SNAPSHOT</version> + <artifactId>plexus-jdo2</artifactId> + <version>1.0-alpha-8</version> + <exclusions> + <exclusion> + <groupId>xerces</groupId> + <artifactId>xercesImpl</artifactId> + </exclusion> + <exclusion> + <groupId>xerces</groupId> + <artifactId>xmlParserAPIs</artifactId> + </exclusion> + </exclusions> </dependency> <dependency> - <groupId>org.codehaus.plexus</groupId> - <artifactId>plexus-utils</artifactId> + <groupId>jpox</groupId> + <artifactId>jpox</artifactId> + <version>1.1.7</version> + <scope>compile</scope> + <exclusions> + <!-- targeting JDK 1.4 we don't need this --> + <exclusion> + <groupId>javax.sql</groupId> + <artifactId>jdbc-stdext</artifactId> + </exclusion> + </exclusions> </dependency> <dependency> <groupId>org.codehaus.plexus</groupId> @@ -83,12 +97,17 @@ <artifactId>log4j</artifactId> <version>1.2.8</version> </dependency> - <!-- TEST DEPS --> <dependency> <groupId>org.apache.derby</groupId> <artifactId>derby</artifactId> <version>10.2.1.6</version> - <!--<scope>test</scope>--> + </dependency> + <!-- TEST DEPS --> + <dependency> + <groupId>hsqldb</groupId> + <artifactId>hsqldb</artifactId> + <version>1.8.0.7</version> + <scope>test</scope> </dependency> <dependency> <groupId>org.apache.derby</groupId> @@ -98,11 +117,11 @@ </dependency> </dependencies> <build> - <plugins> - <plugin> + <plugins> + <plugin> <groupId>org.codehaus.plexus</groupId> <artifactId>plexus-maven-plugin</artifactId> - </plugin> - </plugins> + </plugin> + </plugins> </build> </project> diff --git a/archiva-database/src/main/java/org/apache/maven/archiva/database/AbstractIbatisStore.java b/archiva-database/src/main/java/org/apache/maven/archiva/database/AbstractIbatisStore.java deleted file mode 100644 index 0e25a028c..000000000 --- a/archiva-database/src/main/java/org/apache/maven/archiva/database/AbstractIbatisStore.java +++ /dev/null @@ -1,180 +0,0 @@ -package org.apache.maven.archiva.database; - -/* - * 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 com.ibatis.sqlmap.client.SqlMapClient; - -import org.codehaus.plexus.ibatis.PlexusIbatisHelper; -import org.codehaus.plexus.logging.AbstractLogEnabled; -import org.codehaus.plexus.personality.plexus.lifecycle.phase.Initializable; -import org.codehaus.plexus.personality.plexus.lifecycle.phase.InitializationException; - -import java.sql.Connection; -import java.sql.DatabaseMetaData; -import java.sql.ResultSet; -import java.sql.SQLException; - -/** - * AbstractIbatisStore - * - * @author <a href="mailto:joakim@erdfelt.com">Joakim Erdfelt</a> - * @version $Id$ - */ -public abstract class AbstractIbatisStore - extends AbstractLogEnabled - implements Initializable -{ - /** - * @plexus.requirement - */ - protected PlexusIbatisHelper ibatisHelper; - - /** - * @plexus.configuration default-value="create" - */ - private String createPrefix; - - /** - * @plexus.configuration default-value="drop" - */ - private String dropPrefix; - - protected abstract String[] getTableNames(); - - public void initialize() - throws InitializationException - { - try - { - String tableNames[] = getTableNames(); - for ( int i = 0; i < tableNames.length; i++ ) - { - String tableName = tableNames[i]; - initializeTable( tableName ); - } - } - catch ( ArchivaDatabaseException e ) - { - throw new InitializationException( "Unable to initialize the database: " + e.getMessage(), e ); - } - } - - protected void initializeTable( String tableName ) - throws ArchivaDatabaseException - { - SqlMapClient sqlMap = ibatisHelper.getSqlMapClient(); - - try - { - sqlMap.startTransaction(); - - Connection con = sqlMap.getCurrentConnection(); - - DatabaseMetaData databaseMetaData = con.getMetaData(); - - ResultSet rs = databaseMetaData.getTables( con.getCatalog(), null, null, null ); - - // check if the index database exists in the database - while ( rs.next() ) - { - String dbTableName = rs.getString( "TABLE_NAME" ); - - // if it does then we are already initialized - if ( dbTableName.toLowerCase().equals( tableName.toLowerCase() ) ) - { - return; - } - } - - // Create the tables - - getLogger().info( "Creating table: " + tableName ); - sqlMap.update( createPrefix + tableName, null ); - - sqlMap.commitTransaction(); - } - catch ( SQLException e ) - { - getLogger().error( "Error while initializing database, showing all linked exceptions in SQLException." ); - - while ( e != null ) - { - getLogger().error( e.getMessage(), e ); - - e = e.getNextException(); - } - - throw new ArchivaDatabaseException( "Error while setting up database.", e ); - } - finally - { - try - { - sqlMap.endTransaction(); - } - catch ( SQLException e ) - { - e.printStackTrace(); - } - } - } - - protected void dropTable( String tableName ) - throws ArchivaDatabaseException - { - SqlMapClient sqlMap = ibatisHelper.getSqlMapClient(); - - try - { - sqlMap.startTransaction(); - - getLogger().info( "Dropping table: " + tableName ); - sqlMap.update( dropPrefix + tableName, null ); - - sqlMap.commitTransaction(); - } - catch ( SQLException e ) - { - getLogger().error( "Error while dropping database, showing all linked exceptions in SQLException." ); - - while ( e != null ) - { - getLogger().error( e.getMessage(), e ); - - e = e.getNextException(); - } - - throw new ArchivaDatabaseException( "Error while dropping database.", e ); - } - finally - { - try - { - sqlMap.endTransaction(); - } - catch ( SQLException e ) - { - e.printStackTrace(); - } - } - } - - -} diff --git a/archiva-database/src/main/java/org/apache/maven/archiva/database/AbstractMetadataKeyDatabase.java b/archiva-database/src/main/java/org/apache/maven/archiva/database/AbstractMetadataKeyDatabase.java deleted file mode 100644 index dda52114a..000000000 --- a/archiva-database/src/main/java/org/apache/maven/archiva/database/AbstractMetadataKeyDatabase.java +++ /dev/null @@ -1,288 +0,0 @@ -package org.apache.maven.archiva.database; - - -/* - * 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 com.ibatis.sqlmap.client.SqlMapClient; - -import org.apache.maven.archiva.database.key.MetadataKey; -import org.apache.maven.artifact.repository.metadata.Metadata; -import org.codehaus.plexus.ibatis.PlexusIbatisHelper; -import org.codehaus.plexus.logging.AbstractLogEnabled; -import org.codehaus.plexus.personality.plexus.lifecycle.phase.Initializable; -import org.codehaus.plexus.personality.plexus.lifecycle.phase.InitializationException; - -import java.sql.Connection; -import java.sql.DatabaseMetaData; -import java.sql.ResultSet; -import java.sql.SQLException; - - -/** - * - * IbatisMetadataStore - * - * @author <a href="mailto:jmcconnell@apache.com">Jesse McConnell</a> - * @version $Id$ - * - */ -public class AbstractMetadataKeyDatabase - extends AbstractLogEnabled - implements Initializable -{ - /** - * @plexus.requirement - */ - protected PlexusIbatisHelper ibatisHelper; - - /** - * @plexus.configuration default-value="create" - */ - private String createPrefix; - - /** - * @plexus.configuration default-value="drop" - */ - private String dropPrefix; - - public MetadataKey getMetadataKey( Metadata metadata ) - throws ArchivaDatabaseException - { - SqlMapClient sqlMap = ibatisHelper.getSqlMapClient(); - - try - { - sqlMap.startTransaction(); - - getLogger().info( "Getting metadata key" ); - MetadataKey newMetadataKey = (MetadataKey) sqlMap.queryForObject( "getMetadataKey", metadata ); - - if ( newMetadataKey == null ) - { - getLogger().info( "added new metadata" ); - sqlMap.update( "addMetadataKey", metadata ); - - newMetadataKey = (MetadataKey) sqlMap.queryForObject( "getMetadataKey", metadata ); - - if ( newMetadataKey == null ) - { - throw new ArchivaDatabaseException( "unable to create new MetadataKeys" ); - } - } - - return newMetadataKey; - - } - catch ( SQLException e ) - { - getLogger().error( "Error while adding metadata, showing all linked exceptions in SQLException." ); - - while ( e != null ) - { - getLogger().error( e.getMessage(), e ); - - e = e.getNextException(); - } - - throw new ArchivaDatabaseException ( "Error while interacting with the database.", e ); - } - finally - { - try - { - sqlMap.endTransaction(); - } - catch ( SQLException e ) - { - e.printStackTrace(); - } - } - } - - - protected void initializeTable( String tableName ) - throws ArchivaDatabaseException - { - SqlMapClient sqlMap = ibatisHelper.getSqlMapClient(); - - try - { - sqlMap.startTransaction(); - - Connection con = sqlMap.getCurrentConnection(); - - DatabaseMetaData databaseMetaData = con.getMetaData(); - - ResultSet rs = databaseMetaData.getTables( con.getCatalog(), null, null, null ); - - // check if the index database exists in the database - while ( rs.next() ) - { - String dbTableName = rs.getString( "TABLE_NAME" ); - - // if it does then we are already initialized - if ( dbTableName.toLowerCase().equals( tableName.toLowerCase() ) ) - { - return; - } - } - - // Create the tables - - getLogger().info( "Creating table: " + tableName ); - sqlMap.update( createPrefix + tableName, null ); - - sqlMap.commitTransaction(); - } - catch ( SQLException e ) - { - getLogger().error( "Error while initializing database, showing all linked exceptions in SQLException." ); - - while ( e != null ) - { - getLogger().error( e.getMessage(), e ); - - e = e.getNextException(); - } - - throw new ArchivaDatabaseException( "Error while setting up database.", e ); - } - finally - { - try - { - sqlMap.endTransaction(); - } - catch ( SQLException e ) - { - e.printStackTrace(); - } - } - } - - protected void dropTable( String tableName ) - throws ArchivaDatabaseException - { - SqlMapClient sqlMap = ibatisHelper.getSqlMapClient(); - - try - { - sqlMap.startTransaction(); - - getLogger().info( "Dropping table: " + tableName ); - sqlMap.update( dropPrefix + tableName, null ); - - sqlMap.commitTransaction(); - } - catch ( SQLException e ) - { - getLogger().error( "Error while dropping database, showing all linked exceptions in SQLException." ); - - while ( e != null ) - { - getLogger().error( e.getMessage(), e ); - - e = e.getNextException(); - } - - throw new ArchivaDatabaseException( "Error while dropping database.", e ); - } - finally - { - try - { - sqlMap.endTransaction(); - } - catch ( SQLException e ) - { - e.printStackTrace(); - } - } -} - - public void initialize() - throws InitializationException - { - try - { - initializeTable( "MetadataKeys" ); - } - catch ( ArchivaDatabaseException ade ) - { - throw new InitializationException( "unable to initialize metadata keys database" ); - } - } - - - protected boolean tableExists( String tableName ) - throws ArchivaDatabaseException -{ - SqlMapClient sqlMap = ibatisHelper.getSqlMapClient(); - - try - { - sqlMap.startTransaction(); - - Connection con = sqlMap.getCurrentConnection(); - - DatabaseMetaData databaseMetaData = con.getMetaData(); - - ResultSet rs = databaseMetaData.getTables( con.getCatalog(), null, null, null ); - - // check if the index database exists in the database - while ( rs.next() ) - { - String dbTableName = rs.getString( "TABLE_NAME" ); - - // if it does then we are already initialized - if ( dbTableName.toLowerCase().equals( tableName.toLowerCase() ) ) - { - return true; - } - } - return false; - } - catch ( SQLException e ) - { - getLogger().error( "Error while check database, showing all linked exceptions in SQLException." ); - - while ( e != null ) - { - getLogger().error( e.getMessage(), e ); - - e = e.getNextException(); - } - - throw new ArchivaDatabaseException( "Error while checking database.", e ); - } - finally - { - try - { - sqlMap.endTransaction(); - } - catch ( SQLException e ) - { - e.printStackTrace(); - } - } -} - -} diff --git a/archiva-database/src/main/java/org/apache/maven/archiva/database/ArchivaDAO.java b/archiva-database/src/main/java/org/apache/maven/archiva/database/ArchivaDAO.java new file mode 100644 index 000000000..bd64fb5da --- /dev/null +++ b/archiva-database/src/main/java/org/apache/maven/archiva/database/ArchivaDAO.java @@ -0,0 +1,58 @@ +package org.apache.maven.archiva.database; + +/* + * 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 java.io.Serializable; +import java.util.List; + +/** + * ArchivaDAO - The interface for all content within the database. + * + * @author <a href="mailto:joakim@erdfelt.com">Joakim Erdfelt</a> + * @version $Id$ + */ +public interface ArchivaDAO +{ + public static final String ROLE = ArchivaDAO.class.getName(); + + /** + * Perform a simple query against the database. + * + * @param constraint the constraint to use. + * @return the List of results. + */ + List query( SimpleConstraint constraint ); + + /** + * Perform a simple save of a peristable object to the database. + * + * @param o the serializable (persistable) object to save. + * @return the post-serialized object. + */ + Object save( Serializable obj ); + + ArtifactDAO getArtifactDAO(); + + ProjectModelDAO getProjectModelDAO(); + + RepositoryDAO getRepositoryDAO(); + + RepositoryProblemDAO getRepositoryProblemDAO(); +} diff --git a/archiva-database/src/main/java/org/apache/maven/archiva/database/ArtifactDAO.java b/archiva-database/src/main/java/org/apache/maven/archiva/database/ArtifactDAO.java new file mode 100644 index 000000000..146cb78e0 --- /dev/null +++ b/archiva-database/src/main/java/org/apache/maven/archiva/database/ArtifactDAO.java @@ -0,0 +1,70 @@ +package org.apache.maven.archiva.database; + +/* + * 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.model.ArchivaArtifact; + +import java.util.List; + +/** + * ArtifactDAO + * + * @author <a href="mailto:joakim@erdfelt.com">Joakim Erdfelt</a> + * @version $Id$ + */ +public interface ArtifactDAO +{ + /* NOTE TO ARCHIVA DEVELOPERS. + * + * Please keep this interface clean and lean. + * We don't want a repeat of the Continuum Store. + * You should have the following methods per object type ... + * + * (Required Methods) + * + * DatabaseObject .createDatabaseObject( Required Params ) ; + * List .queryDatabaseObject( Constraint ) throws ObjectNotFoundException, DatabaseException; + * DatabaseObject .saveDatabaseObject( DatabaseObject ) throws DatabaseException; + * + * (Optional Methods) + * + * DatabaseObject .getDatabaseObject( Id ) throws ObjectNotFoundException, DatabaseException; + * List .getDatabaseObjects() throws ObjectNotFoundException, DatabaseException; + * void .deleteDatabaseObject( DatabaseObject ) throws DatabaseException; + * + * This is the only list of options created in this DAO. + */ + + public ArchivaArtifact createArtifact( String groupId, String artifactId, String version, String classifier, + String type ); + + public ArchivaArtifact getArtifact( String groupId, String artifactId, String version, String classifier, + String type ) + throws ObjectNotFoundException, ArchivaDatabaseException; + + public List /*<ArchivaArtifact>*/queryArtifacts( Constraint constraint ) + throws ObjectNotFoundException, ArchivaDatabaseException; + + public ArchivaArtifact saveArtifact( ArchivaArtifact artifact ) + throws ArchivaDatabaseException; + + public void deleteArtifact( ArchivaArtifact artifact ) + throws ArchivaDatabaseException; +} diff --git a/archiva-database/src/main/java/org/apache/maven/archiva/database/Constraint.java b/archiva-database/src/main/java/org/apache/maven/archiva/database/Constraint.java new file mode 100644 index 000000000..d0a02b248 --- /dev/null +++ b/archiva-database/src/main/java/org/apache/maven/archiva/database/Constraint.java @@ -0,0 +1,43 @@ +package org.apache.maven.archiva.database; + +/* + * 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. + */ + + +/** + * Constraint - a generic object for dealing with database constraints. + * + * @author <a href="mailto:joakim@erdfelt.com">Joakim Erdfelt</a> + * @version $Id$ + * @see DeclarativeConstraint + * @see SimpleConstraint + */ +public interface Constraint +{ + public static final String ASCENDING = "ascending"; + + public static final String DESCENDING = "descending"; + + /** + * Get the fetch limits on the object. + * + * @return the fetch limits on the object. (can be null) (O/RM specific) + */ + public String getFetchLimits(); +} diff --git a/archiva-database/src/main/java/org/apache/maven/archiva/database/DeclarativeConstraint.java b/archiva-database/src/main/java/org/apache/maven/archiva/database/DeclarativeConstraint.java new file mode 100644 index 000000000..54b0b4724 --- /dev/null +++ b/archiva-database/src/main/java/org/apache/maven/archiva/database/DeclarativeConstraint.java @@ -0,0 +1,77 @@ +package org.apache.maven.archiva.database; + +/* + * 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. + */ + +/** + * DeclarativeConstraint + * + * @author <a href="mailto:joakim@erdfelt.com">Joakim Erdfelt</a> + * @version $Id$ + */ +public interface DeclarativeConstraint extends Constraint +{ + /** + * Get the declared imports used for this query. (optional) + * + * NOTE: This is DAO implementation specific. + * + * @return the imports. (can be null) + */ + public abstract String[] getDeclaredImports(); + + /** + * Get the declared parameters used for this query. (optional) + * + * NOTE: This is DAO implementation specific. + * + * @return the parameters. (can be null) + */ + public abstract String[] getDeclaredParameters(); + + /** + * Get the parameters used for this query. (required if using {@link #getDeclaredParameters()} ) + * + * NOTE: This is DAO implementation specific. + * + * @return the parameters. (can be null) + */ + public abstract Object[] getParameters(); + + /** + * Get the sort direction name. + * + * @return the sort direction name. ("ASC" or "DESC") (only valid if {@link #getSortColumn()} is specified.) + */ + public abstract String getSortDirection(); + + /** + * Get the sort column name. + * + * @return the sort column name. (can be null) + */ + public abstract String getSortColumn(); + + /** + * Get the SELECT WHERE (condition) value for the constraint. + * + * @return the equivalent of the SELECT WHERE (condition) value for this constraint. (can be null) + */ + public abstract String getWhereCondition(); +}
\ No newline at end of file diff --git a/archiva-database/src/main/java/org/apache/maven/archiva/database/ObjectNotFoundException.java b/archiva-database/src/main/java/org/apache/maven/archiva/database/ObjectNotFoundException.java new file mode 100644 index 000000000..e9dfc01d0 --- /dev/null +++ b/archiva-database/src/main/java/org/apache/maven/archiva/database/ObjectNotFoundException.java @@ -0,0 +1,53 @@ +package org.apache.maven.archiva.database; + +/* + * 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. + */ + +/** + * ObjectNotFoundException + * + * @author <a href="mailto:joakim@erdfelt.com">Joakim Erdfelt</a> + * @version $Id$ + */ +public class ObjectNotFoundException + extends ArchivaDatabaseException +{ + private Object id; + + public ObjectNotFoundException( String message, Throwable cause, Object id ) + { + super( message, cause ); + this.id = id; + } + + public ObjectNotFoundException( String message, Throwable cause ) + { + super( message, cause ); + } + + public ObjectNotFoundException( String message ) + { + super( message ); + } + + public Object getId() + { + return id; + } +} diff --git a/archiva-database/src/main/java/org/apache/maven/archiva/database/ProjectModelDAO.java b/archiva-database/src/main/java/org/apache/maven/archiva/database/ProjectModelDAO.java new file mode 100644 index 000000000..210c18982 --- /dev/null +++ b/archiva-database/src/main/java/org/apache/maven/archiva/database/ProjectModelDAO.java @@ -0,0 +1,68 @@ +package org.apache.maven.archiva.database; + +/* + * 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.model.ArchivaProjectModel; + +import java.util.List; + +/** + * ProjectModelDAO + * + * @author <a href="mailto:joakim@erdfelt.com">Joakim Erdfelt</a> + * @version $Id$ + */ +public interface ProjectModelDAO +{ + /* NOTE TO ARCHIVA DEVELOPERS. + * + * Please keep this interface clean and lean. + * We don't want a repeat of the Continuum Store. + * You should have the following methods per object type ... + * + * (Required Methods) + * + * DatabaseObject .createDatabaseObject( Required Params ) ; + * List .queryDatabaseObject( Constraint ) throws ObjectNotFoundException, DatabaseException; + * DatabaseObject .saveDatabaseObject( DatabaseObject ) throws DatabaseException; + * + * (Optional Methods) + * + * DatabaseObject .getDatabaseObject( Id ) throws ObjectNotFoundException, DatabaseException; + * List .getDatabaseObjects() throws ObjectNotFoundException, DatabaseException; + * void .deleteDatabaseObject( DatabaseObject ) throws DatabaseException; + * + * This is the only list of options created in this DAO. + */ + + public ArchivaProjectModel createProjectModel( String groupId, String artifactId, String version ); + + public ArchivaProjectModel getProjectModel( String groupId, String artifactId, String version ) + throws ObjectNotFoundException, ArchivaDatabaseException; + + public List /*<ArchivaProjectModel>*/queryProjectModels( Constraint constraint ) + throws ObjectNotFoundException, ArchivaDatabaseException; + + public ArchivaProjectModel saveProjectModel( ArchivaProjectModel model ) + throws ArchivaDatabaseException; + + public void deleteProjectModel( ArchivaProjectModel model ) + throws ArchivaDatabaseException; +} diff --git a/archiva-database/src/main/java/org/apache/maven/archiva/database/RepositoryDAO.java b/archiva-database/src/main/java/org/apache/maven/archiva/database/RepositoryDAO.java new file mode 100644 index 000000000..baaaaa574 --- /dev/null +++ b/archiva-database/src/main/java/org/apache/maven/archiva/database/RepositoryDAO.java @@ -0,0 +1,71 @@ +package org.apache.maven.archiva.database; + +/* + * 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.model.ArchivaRepository; + +import java.util.List; + +/** + * RepositoryDAO + * + * @author <a href="mailto:joakim@erdfelt.com">Joakim Erdfelt</a> + * @version $Id$ + */ +public interface RepositoryDAO +{ + /* NOTE TO ARCHIVA DEVELOPERS. + * + * Please keep this interface clean and lean. + * We don't want a repeat of the Continuum Store. + * You should have the following methods per object type ... + * + * (Required Methods) + * + * DatabaseObject .createDatabaseObject( Required Params ) ; + * List .queryDatabaseObject( Constraint ) throws ObjectNotFoundException, DatabaseException; + * DatabaseObject .saveDatabaseObject( DatabaseObject ) throws DatabaseException; + * + * (Optional Methods) + * + * DatabaseObject .getDatabaseObject( Id ) throws ObjectNotFoundException, DatabaseException; + * List .getDatabaseObjects() throws ObjectNotFoundException, DatabaseException; + * void .deleteDatabaseObject( DatabaseObject ) throws DatabaseException; + * + * This is the only list of options created in this DAO. + */ + + public ArchivaRepository createRepository( String id, String name, String url ); + + public List /*<ArchivaRepository>*/getRepositories() + throws ObjectNotFoundException, ArchivaDatabaseException; + + public ArchivaRepository getRepository( String id ) + throws ObjectNotFoundException, ArchivaDatabaseException; + + public List /*<ArchivaRepository>*/queryRepositories( Constraint constraint ) + throws ObjectNotFoundException, ArchivaDatabaseException; + + public ArchivaRepository saveRepository( ArchivaRepository repository ) + throws ArchivaDatabaseException; + + public void deleteRepository( ArchivaRepository repository ) + throws ArchivaDatabaseException; +} diff --git a/archiva-database/src/main/java/org/apache/maven/archiva/database/RepositoryMetadataDatabase.java b/archiva-database/src/main/java/org/apache/maven/archiva/database/RepositoryMetadataDatabase.java deleted file mode 100644 index 5434e6071..000000000 --- a/archiva-database/src/main/java/org/apache/maven/archiva/database/RepositoryMetadataDatabase.java +++ /dev/null @@ -1,232 +0,0 @@ -package org.apache.maven.archiva.database; - -/* - * 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 com.ibatis.sqlmap.client.SqlMapClient; - -import org.apache.maven.archiva.database.key.MetadataKey; -import org.apache.maven.artifact.repository.metadata.RepositoryMetadata; -import org.codehaus.plexus.personality.plexus.lifecycle.phase.InitializationException; - -import java.sql.SQLException; - -/** - * RepositoryMetadataDatabase - * - * @author <a href="mailto:joakim@erdfelt.com">Joakim Erdfelt</a> - * @version $Id$ - * - * @plexus.component role="org.apache.maven.archiva.database.RepositoryMetadataDatabase" role-hint="default" - */ -public class RepositoryMetadataDatabase extends AbstractMetadataKeyDatabase -{ - public void create( RepositoryMetadata metadata ) - throws ArchivaDatabaseException - { - - SqlMapClient sqlMap = ibatisHelper.getSqlMapClient(); - - try - { - sqlMap.startTransaction(); - - getLogger().info( "Adding repository metadata" ); - sqlMap.update( "addRepositoryMetadata", metadata ); - - sqlMap.commitTransaction(); - } - catch ( SQLException e ) - { - getLogger().error( "Error while executing statement, showing all linked exceptions in SQLException." ); - - while ( e != null ) - { - getLogger().error( e.getMessage(), e ); - - e = e.getNextException(); - } - - throw new ArchivaDatabaseException( "Error while executing statement.", e ); - } - finally - { - try - { - sqlMap.endTransaction(); - } - catch ( SQLException e ) - { - e.printStackTrace(); - } - } - } - - - public RepositoryMetadata read( String groupId, String artifactId, String version ) - throws ArchivaDatabaseException - { - - SqlMapClient sqlMap = ibatisHelper.getSqlMapClient(); - - try - { - sqlMap.startTransaction(); - - getLogger().info( "Reading repository metadata" ); - RepositoryMetadata repositoryMetadata = (RepositoryMetadata) sqlMap.queryForObject( "getRepositoryMetadata", new MetadataKey( groupId, artifactId, version ) ); - - return repositoryMetadata; - } - catch ( SQLException e ) - { - getLogger().error( "Error while executing statement, showing all linked exceptions in SQLException." ); - - while ( e != null ) - { - getLogger().error( e.getMessage(), e ); - - e = e.getNextException(); - } - - throw new ArchivaDatabaseException( "Error while executing statement.", e ); - } - finally - { - try - { - sqlMap.endTransaction(); - } - catch ( SQLException e ) - { - e.printStackTrace(); - } - } - } - - /** - * not implemented yet - * - * @param metadata - * @throws ArchivaDatabaseException - */ - public void update( RepositoryMetadata metadata ) - throws ArchivaDatabaseException - { - - SqlMapClient sqlMap = ibatisHelper.getSqlMapClient(); - - try - { - sqlMap.startTransaction(); - - getLogger().info( "Updating repository metadata" ); - sqlMap.update( "updateRepositoryMetadata", metadata ); - - sqlMap.commitTransaction(); - } - catch ( SQLException e ) - { - getLogger().error( "Error while executing statement, showing all linked exceptions in SQLException." ); - - while ( e != null ) - { - getLogger().error( e.getMessage(), e ); - - e = e.getNextException(); - } - - throw new ArchivaDatabaseException( "Error while executing statement.", e ); - } - finally - { - try - { - sqlMap.endTransaction(); - } - catch ( SQLException e ) - { - e.printStackTrace(); - } - } - } - - public void delete( RepositoryMetadata metadata ) - throws ArchivaDatabaseException - { - // FIXME is this right? baseVersion seems wrong but I don't know enough about the metadata to say - delete( metadata.getGroupId(), metadata.getArtifactId(), metadata.getBaseVersion() ); - } - - public void delete( String groupId, String artifactId, String version ) - throws ArchivaDatabaseException - { - SqlMapClient sqlMap = ibatisHelper.getSqlMapClient(); - - try - { - sqlMap.startTransaction(); - - getLogger().info( "Removing repository metadata" ); - sqlMap.update( "removeRepositoryMetadata", new MetadataKey( groupId, artifactId, version ) ); - - sqlMap.commitTransaction(); - } - catch ( SQLException e ) - { - getLogger().error( "Error while executing statement, showing all linked exceptions in SQLException." ); - - while ( e != null ) - { - getLogger().error( e.getMessage(), e ); - - e = e.getNextException(); - } - - throw new ArchivaDatabaseException( "Error while executing statement.", e ); - } - finally - { - try - { - sqlMap.endTransaction(); - } - catch ( SQLException e ) - { - e.printStackTrace(); - } - } - } - - public void initialize() - throws InitializationException - { - super.initialize(); - try - { - initializeTable( "RepositoryMetadata" ); - } - catch ( ArchivaDatabaseException ade ) - { - throw new InitializationException( "unable to initialize repository metadata table", ade ); - } - } - - -} diff --git a/archiva-database/src/main/java/org/apache/maven/archiva/database/RepositoryProblemDAO.java b/archiva-database/src/main/java/org/apache/maven/archiva/database/RepositoryProblemDAO.java new file mode 100644 index 000000000..f4aecd2f4 --- /dev/null +++ b/archiva-database/src/main/java/org/apache/maven/archiva/database/RepositoryProblemDAO.java @@ -0,0 +1,63 @@ +package org.apache.maven.archiva.database; + +/* + * 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.model.RepositoryProblem; + +import java.util.List; + +/** + * RepositoryProblemDAO + * + * @author <a href="mailto:joakim@erdfelt.com">Joakim Erdfelt</a> + * @version $Id$ + */ +public interface RepositoryProblemDAO +{ + /* NOTE TO ARCHIVA DEVELOPERS. + * + * Please keep this interface clean and lean. + * We don't want a repeat of the Continuum Store. + * You should have the following methods per object type ... + * + * (Required Methods) + * + * List .queryDatabaseObject( Constraint ) throws ObjectNotFoundException, DatabaseException; + * DatabaseObject .saveDatabaseObject( DatabaseObject ) throws DatabaseException; + * + * (Optional Methods) + * + * DatabaseObject .createDatabaseObject( Required Params ) ; + * DatabaseObject .getDatabaseObject( Id ) throws ObjectNotFoundException, DatabaseException; + * List .getDatabaseObjects() throws ObjectNotFoundException, DatabaseException; + * void .deleteDatabaseObject( DatabaseObject ) throws DatabaseException; + * + * This is the only list of options created in this DAO. + */ + + public List /*<RepositoryProblem>*/queryRepositoryProblems( Constraint constraint ) + throws ObjectNotFoundException, ArchivaDatabaseException; + + public RepositoryProblem saveRepositoryProblem( RepositoryProblem problem ) + throws ArchivaDatabaseException; + + public void deleteRepositoryProblem( RepositoryProblem problem ) + throws ArchivaDatabaseException; +} diff --git a/archiva-database/src/main/java/org/apache/maven/archiva/database/SimpleConstraint.java b/archiva-database/src/main/java/org/apache/maven/archiva/database/SimpleConstraint.java new file mode 100644 index 000000000..d08c22541 --- /dev/null +++ b/archiva-database/src/main/java/org/apache/maven/archiva/database/SimpleConstraint.java @@ -0,0 +1,60 @@ +package org.apache.maven.archiva.database; + +/* + * 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. + */ + +/** + * SimpleConstraint + * + * @author <a href="mailto:joakim@erdfelt.com">Joakim Erdfelt</a> + * @version $Id$ + */ +public interface SimpleConstraint extends Constraint +{ + /** + * Get the parameters used for this query. (required if using parameterized SQL) + * + * NOTE: This is DAO implementation specific. + * + * @return the parameters. (can be null) + */ + public Object[] getParameters(); + + /** + * Get the SELECT query value for the constraint. + * + * @return the SELECT value for this constraint. (can be null) + */ + public abstract String getSelectSql(); + + /** + * For simple Constraints the results class must be specified. + * + * @return the result class. + */ + public Class getResultClass(); + + /** + * When working with result classes that are not persistable, + * it is advisable to tell the underlying DAO to not do the persistable related efforts. + * + * @return true if result classes are persistable. + */ + public boolean isResultsPersistable(); +}
\ No newline at end of file diff --git a/archiva-database/src/main/java/org/apache/maven/archiva/database/artifact/ArtifactKey.java b/archiva-database/src/main/java/org/apache/maven/archiva/database/artifact/ArtifactKey.java deleted file mode 100644 index e1f00ab98..000000000 --- a/archiva-database/src/main/java/org/apache/maven/archiva/database/artifact/ArtifactKey.java +++ /dev/null @@ -1,101 +0,0 @@ -package org.apache.maven.archiva.database.artifact; - -/* - * 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. - */ - -/** - * ArtifactKey - * - * @author <a href="mailto:joakim@erdfelt.com">Joakim Erdfelt</a> - * @version $Id$ - */ -public class ArtifactKey -{ - private String groupId; - - private String artifactId; - - private String version; - - private String classifier; - - private String type; - - private long id; - - public String getArtifactId() - { - return artifactId; - } - - public void setArtifactId( String artifactId ) - { - this.artifactId = artifactId; - } - - public String getClassifier() - { - return classifier; - } - - public void setClassifier( String classifier ) - { - this.classifier = classifier; - } - - public String getGroupId() - { - return groupId; - } - - public void setGroupId( String groupId ) - { - this.groupId = groupId; - } - - public long getId() - { - return id; - } - - public void setId( long id ) - { - this.id = id; - } - - public String getType() - { - return type; - } - - public void setType( String type ) - { - this.type = type; - } - - public String getVersion() - { - return version; - } - - public void setVersion( String version ) - { - this.version = version; - } -} diff --git a/archiva-database/src/main/java/org/apache/maven/archiva/database/artifact/ArtifactPersistence.java b/archiva-database/src/main/java/org/apache/maven/archiva/database/artifact/ArtifactPersistence.java deleted file mode 100644 index d59cbf9cc..000000000 --- a/archiva-database/src/main/java/org/apache/maven/archiva/database/artifact/ArtifactPersistence.java +++ /dev/null @@ -1,137 +0,0 @@ -package org.apache.maven.archiva.database.artifact; - -/* - * 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 com.ibatis.sqlmap.client.SqlMapClient; - -import org.apache.maven.archiva.database.AbstractIbatisStore; -import org.apache.maven.archiva.database.ArchivaDatabaseException; -import org.apache.maven.artifact.Artifact; - -import java.sql.SQLException; - -/** - * ArtifactPersistence - * - * @author <a href="mailto:joakim@erdfelt.com">Joakim Erdfelt</a> - * @version $Id$ - * - * @plexus.component role="org.apache.maven.archiva.database.artifact.ArtifactPersistence" - */ -public class ArtifactPersistence - extends AbstractIbatisStore -{ - protected String[] getTableNames() - { - return new String[] { "ArtifactKeys" }; - } - - private ArtifactKey toKey( Artifact artifact ) - { - ArtifactKey key = new ArtifactKey(); - key.setGroupId( artifact.getGroupId() ); - key.setArtifactId( artifact.getArtifactId() ); - key.setVersion( artifact.getVersion() ); - key.setClassifier( artifact.getClassifier() ); - key.setType( artifact.getType() ); - return key; - } - - public void create( Artifact artifact ) - throws ArchivaDatabaseException - { - SqlMapClient sqlMap = ibatisHelper.getSqlMapClient(); - - try - { - sqlMap.startTransaction(); - - getLogger().info( "Adding artifact." ); - sqlMap.update( "addArtifact", artifact ); - - sqlMap.commitTransaction(); - } - catch ( SQLException e ) - { - getLogger().error( "Error while executing statement, showing all linked exceptions in SQLException." ); - - while ( e != null ) - { - getLogger().error( e.getMessage(), e ); - - e = e.getNextException(); - } - - throw new ArchivaDatabaseException( "Error while executing statement.", e ); - } - finally - { - try - { - sqlMap.endTransaction(); - } - catch ( SQLException e ) - { - e.printStackTrace(); - } - } - } - - public Artifact read( String groupId, String artifactId, String version ) - { - return null; - } - - public Artifact read( String groupId, String artifactId, String version, String type ) - { - return null; - } - - public Artifact read( String groupId, String artifactId, String version, String classifier, String type ) - { - return null; - } - - public void update( Artifact artifact ) - { - - } - - public void delete( Artifact artifact ) - { - - } - - public void delete( String groupId, String artifactId, String version ) - { - - } - - public void delete( String groupId, String artifactId, String version, String type ) - { - - } - - public void delete( String groupId, String artifactId, String version, String classifier, String type ) - { - - } - -} diff --git a/archiva-database/src/main/java/org/apache/maven/archiva/database/browsing/BrowsingResults.java b/archiva-database/src/main/java/org/apache/maven/archiva/database/browsing/BrowsingResults.java new file mode 100644 index 000000000..118c9e1d4 --- /dev/null +++ b/archiva-database/src/main/java/org/apache/maven/archiva/database/browsing/BrowsingResults.java @@ -0,0 +1,114 @@ +package org.apache.maven.archiva.database.browsing; + +/* + * 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.commons.collections.CollectionUtils; + +import java.util.List; + +/** + * BrowsingResults + * + * @author <a href="mailto:joakim@erdfelt.com">Joakim Erdfelt</a> + * @version $Id$ + */ +public class BrowsingResults +{ + private String selectedGroupId; + + private String selectedArtifactId; + + private List groupIds = null; + + private List artifacts = null; + + private List versions = null; + + public BrowsingResults() + { + /* do nothing, this is the results of the root */ + } + + public BrowsingResults( String groupId ) + { + this.selectedGroupId = groupId; + } + + public BrowsingResults( String groupId, String artifactId ) + { + this.selectedGroupId = groupId; + this.selectedArtifactId = artifactId; + } + + public List getArtifacts() + { + return artifacts; + } + + public List getGroupIds() + { + return groupIds; + } + + public String getSelectedArtifactId() + { + return selectedArtifactId; + } + + public String getSelectedGroupId() + { + return selectedGroupId; + } + + public List getVersions() + { + return versions; + } + + public boolean hasArtifacts() + { + return CollectionUtils.isNotEmpty( artifacts ); + } + + public boolean hasGroupIds() + { + return CollectionUtils.isNotEmpty( groupIds ); + } + + public boolean hasVersions() + { + return CollectionUtils.isNotEmpty( versions ); + } + + public void setArtifacts( List artifacts ) + { + this.artifacts = artifacts; + } + + public void setGroupIds( List groupIds ) + { + this.groupIds = groupIds; + } + + public void setVersions( List versions ) + { + this.versions = versions; + } +} diff --git a/archiva-database/src/main/java/org/apache/maven/archiva/database/browsing/DefaultRepositoryBrowsing.java b/archiva-database/src/main/java/org/apache/maven/archiva/database/browsing/DefaultRepositoryBrowsing.java new file mode 100644 index 000000000..ad8b71932 --- /dev/null +++ b/archiva-database/src/main/java/org/apache/maven/archiva/database/browsing/DefaultRepositoryBrowsing.java @@ -0,0 +1,98 @@ +package org.apache.maven.archiva.database.browsing; + +/* + * 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.ArchivaDatabaseException; +import org.apache.maven.archiva.database.ObjectNotFoundException; +import org.apache.maven.archiva.database.constraints.UniqueArtifactIdConstraint; +import org.apache.maven.archiva.database.constraints.UniqueGroupIdConstraint; +import org.apache.maven.archiva.database.constraints.UniqueVersionConstraint; +import org.apache.maven.archiva.model.ArchivaProjectModel; +import org.codehaus.plexus.logging.AbstractLogEnabled; + +import java.util.List; + +/** + * DefaultRepositoryBrowsing + * + * @author <a href="mailto:joakim@erdfelt.com">Joakim Erdfelt</a> + * @version $Id$ + * + * @plexus.component role="org.apache.maven.archiva.database.browsing.RepositoryBrowsing" + * role-hint="default" + */ +public class DefaultRepositoryBrowsing + extends AbstractLogEnabled + implements RepositoryBrowsing +{ + /** + * @plexus.requirement role-hint="jdo" + */ + private ArchivaDAO dao; + + public BrowsingResults getRoot() + { + List groups = dao.query( new UniqueGroupIdConstraint() ); + + BrowsingResults results = new BrowsingResults(); + + results.setGroupIds( GroupIdFilter.filterGroups( groups ) ); + + return results; + } + + public BrowsingResults selectArtifactId( String groupId, String artifactId ) + { + // List groups = dao.query( new UniqueGroupIdConstraint( groupId ) ); + // List artifacts = dao.query( new UniqueArtifactIdConstraint( groupId ) ); + List versions = dao.query( new UniqueVersionConstraint( groupId, artifactId ) ); + + BrowsingResults results = new BrowsingResults( groupId, artifactId ); + + // results.setGroupIds( groups ); + // results.setArtifacts( artifacts ); + results.setArtifacts( versions ); + + return results; + } + + public BrowsingResults selectGroupId( String groupId ) + { + List groups = dao.query( new UniqueGroupIdConstraint( groupId ) ); + List artifacts = dao.query( new UniqueArtifactIdConstraint( groupId ) ); + + BrowsingResults results = new BrowsingResults( groupId ); + results.setGroupIds( groups ); + results.setArtifacts( artifacts ); + + return results; + } + + public ArchivaProjectModel selectVersion( String groupId, String artifactId, String version ) + throws ObjectNotFoundException, ArchivaDatabaseException + { + ArchivaProjectModel model = dao.getProjectModelDAO().getProjectModel( groupId, artifactId, version ); + + // TODO: if the model isn't found. load it from disk, insert into DB, and then return it. + + return model; + } +} diff --git a/archiva-database/src/main/java/org/apache/maven/archiva/database/browsing/GroupIdFilter.java b/archiva-database/src/main/java/org/apache/maven/archiva/database/browsing/GroupIdFilter.java new file mode 100644 index 000000000..a102e9c7e --- /dev/null +++ b/archiva-database/src/main/java/org/apache/maven/archiva/database/browsing/GroupIdFilter.java @@ -0,0 +1,176 @@ +package org.apache.maven.archiva.database.browsing; + +/* + * 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 java.util.ArrayList; +import java.util.Iterator; +import java.util.List; +import java.util.Map; +import java.util.StringTokenizer; +import java.util.TreeMap; + +/** + * GroupIdFilter - utility methods for filtering groupIds. + * + * @author <a href="mailto:joakim@erdfelt.com">Joakim Erdfelt</a> + * @version $Id$ + */ +public class GroupIdFilter +{ + private static final String GROUP_SEPARATOR = "."; + + /** + * <p> + * Filter out excessive groupId naming. (to provide a tree-ish view of the list of groupIds). + * </p> + * + * <pre> + * // Input List + * commons-lang + * com.jsch + * org.apache.apache + * org.apache.maven + * org.codehaus.modello + * // Filtered List + * commons-lang + * com.jsch + * org + * </pre> + * + * <pre> + * // Input List + * commons-lang + * commons-io + * commons-pool + * com.jsch + * com.jsch.lib + * com.jsch.providers + * org.apache.apache + * org.apache.maven + * org.apache.maven.archiva + * org.apache.maven.shared + * // Filtered List + * commons-lang + * commons-io + * commons-pool + * com.jsch + * org.apache + * </pre> + * + * @param groups the list of groupIds. + * @return + */ + public static List filterGroups( List groups ) + { + GroupTreeNode tree = buildGroupTree( groups ); + return collateGroups( tree ); + } + + public static GroupTreeNode buildGroupTree( List groups ) + { + GroupTreeNode rootNode = new GroupTreeNode(); + + // build a tree structure + for ( Iterator i = groups.iterator(); i.hasNext(); ) + { + String groupId = (String) i.next(); + + StringTokenizer tok = new StringTokenizer( groupId, GROUP_SEPARATOR ); + + GroupTreeNode node = rootNode; + + while ( tok.hasMoreTokens() ) + { + String part = tok.nextToken(); + + if ( !node.getChildren().containsKey( part ) ) + { + GroupTreeNode newNode = new GroupTreeNode( part, node ); + node.addChild( newNode ); + node = newNode; + } + else + { + node = (GroupTreeNode) node.getChildren().get( part ); + } + } + } + + return rootNode; + } + + private static List collateGroups( GroupTreeNode rootNode ) + { + List groups = new ArrayList(); + for ( Iterator i = rootNode.getChildren().values().iterator(); i.hasNext(); ) + { + GroupTreeNode node = (GroupTreeNode) i.next(); + + while ( node.getChildren().size() == 1 ) + { + node = (GroupTreeNode) node.getChildren().values().iterator().next(); + } + + groups.add( node.getFullName() ); + } + return groups; + } + + private static class GroupTreeNode + { + private final String name; + + private final String fullName; + + private final Map children = new TreeMap(); + + GroupTreeNode() + { + name = null; + fullName = null; + } + + GroupTreeNode( String name, GroupTreeNode parent ) + { + this.name = name; + this.fullName = parent.fullName != null ? parent.fullName + GROUP_SEPARATOR + name : name; + } + + public String getName() + { + return name; + } + + public String getFullName() + { + return fullName; + } + + public Map getChildren() + { + return children; + } + + public void addChild( GroupTreeNode newNode ) + { + children.put( newNode.name, newNode ); + } + } +} diff --git a/archiva-database/src/main/java/org/apache/maven/archiva/database/browsing/RepositoryBrowsing.java b/archiva-database/src/main/java/org/apache/maven/archiva/database/browsing/RepositoryBrowsing.java new file mode 100644 index 000000000..ead8a7913 --- /dev/null +++ b/archiva-database/src/main/java/org/apache/maven/archiva/database/browsing/RepositoryBrowsing.java @@ -0,0 +1,42 @@ +package org.apache.maven.archiva.database.browsing; + +/* + * 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.ArchivaDatabaseException; +import org.apache.maven.archiva.database.ObjectNotFoundException; +import org.apache.maven.archiva.model.ArchivaProjectModel; + +/** + * RepositoryBrowsing + * + * @author <a href="mailto:joakim@erdfelt.com">Joakim Erdfelt</a> + * @version $Id$ + */ +public interface RepositoryBrowsing +{ + public BrowsingResults getRoot(); + + public BrowsingResults selectGroupId( String groupId ); + + public BrowsingResults selectArtifactId( String groupId, String artifactId ); + + public ArchivaProjectModel selectVersion( String groupId, String artifactId, String version ) + throws ObjectNotFoundException, ArchivaDatabaseException; +} diff --git a/archiva-database/src/main/java/org/apache/maven/archiva/database/constraints/AbstractDeclarativeConstraint.java b/archiva-database/src/main/java/org/apache/maven/archiva/database/constraints/AbstractDeclarativeConstraint.java new file mode 100644 index 000000000..48a4a3d21 --- /dev/null +++ b/archiva-database/src/main/java/org/apache/maven/archiva/database/constraints/AbstractDeclarativeConstraint.java @@ -0,0 +1,64 @@ +package org.apache.maven.archiva.database.constraints; + +/* + * 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.Constraint; +import org.apache.maven.archiva.database.DeclarativeConstraint; + +/** + * AbstractDeclarativeConstraint + * + * @author <a href="mailto:joakim@erdfelt.com">Joakim Erdfelt</a> + * @version $Id$ + */ +public abstract class AbstractDeclarativeConstraint + implements DeclarativeConstraint +{ + protected String[] declImports; + + protected String[] declParams; + + protected Object[] params; + + public String getFetchLimits() + { + return null; + } + + public String[] getDeclaredImports() + { + return declImports; + } + + public String[] getDeclaredParameters() + { + return declParams; + } + + public Object[] getParameters() + { + return params; + } + + public String getSortDirection() + { + return Constraint.ASCENDING; + } +} diff --git a/archiva-database/src/main/java/org/apache/maven/archiva/database/constraints/AbstractSimpleConstraint.java b/archiva-database/src/main/java/org/apache/maven/archiva/database/constraints/AbstractSimpleConstraint.java new file mode 100644 index 000000000..b9e76ffb4 --- /dev/null +++ b/archiva-database/src/main/java/org/apache/maven/archiva/database/constraints/AbstractSimpleConstraint.java @@ -0,0 +1,49 @@ +package org.apache.maven.archiva.database.constraints; + +/* + * 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.SimpleConstraint; + +/** + * Simple Constraint abstract for working with nearly-raw SQL strings. + * + * @author <a href="mailto:joakim@erdfelt.com">Joakim Erdfelt</a> + * @version $Id$ + */ +public abstract class AbstractSimpleConstraint + implements SimpleConstraint +{ + protected Object[] params; + + public Object[] getParameters() + { + return params; + } + + public String getFetchLimits() + { + return null; + } + + public boolean isResultsPersistable() + { + return false; + } +} diff --git a/archiva-database/src/main/java/org/apache/maven/archiva/database/constraints/ArchivaRepositoryByUrlConstraint.java b/archiva-database/src/main/java/org/apache/maven/archiva/database/constraints/ArchivaRepositoryByUrlConstraint.java new file mode 100644 index 000000000..9ad4de605 --- /dev/null +++ b/archiva-database/src/main/java/org/apache/maven/archiva/database/constraints/ArchivaRepositoryByUrlConstraint.java @@ -0,0 +1,31 @@ +package org.apache.maven.archiva.database.constraints; + +import org.apache.maven.archiva.database.Constraint; + +/** + * ArchivaRepositoryByUrlConstraint + * + * @author <a href="mailto:joakim@erdfelt.com">Joakim Erdfelt</a> + * @version $Id$ + */ +public class ArchivaRepositoryByUrlConstraint + extends AbstractDeclarativeConstraint + implements Constraint +{ + private String whereCondition; + + public ArchivaRepositoryByUrlConstraint( String url ) + { + whereCondition = "this.url == '" + url + "'"; + } + + public String getWhereCondition() + { + return whereCondition; + } + + public String getSortColumn() + { + return "url"; + } +} diff --git a/archiva-database/src/main/java/org/apache/maven/archiva/database/constraints/ArtifactsBySha1ChecksumConstraint.java b/archiva-database/src/main/java/org/apache/maven/archiva/database/constraints/ArtifactsBySha1ChecksumConstraint.java new file mode 100644 index 000000000..e8ac7a539 --- /dev/null +++ b/archiva-database/src/main/java/org/apache/maven/archiva/database/constraints/ArtifactsBySha1ChecksumConstraint.java @@ -0,0 +1,52 @@ +package org.apache.maven.archiva.database.constraints; + +/* + * 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.Constraint; + +/** + * ArtifactsBySha1ChecksumConstraint + * + * @author <a href="mailto:joakim@erdfelt.com">Joakim Erdfelt</a> + * @version $Id$ + */ +public class ArtifactsBySha1ChecksumConstraint + extends AbstractDeclarativeConstraint + implements Constraint +{ + private String whereClause; + + public ArtifactsBySha1ChecksumConstraint( String desiredChecksum ) + { + whereClause = "this.checksumSHA1 == desiredChecksum"; + declParams = new String[] { "String desiredChecksum" }; + params = new Object[] { desiredChecksum }; + } + + public String getSortColumn() + { + return "groupId"; + } + + public String getWhereCondition() + { + return whereClause; + } +} diff --git a/archiva-database/src/main/java/org/apache/maven/archiva/database/constraints/ArtifactsProcessedConstraint.java b/archiva-database/src/main/java/org/apache/maven/archiva/database/constraints/ArtifactsProcessedConstraint.java new file mode 100644 index 000000000..a8b389c5c --- /dev/null +++ b/archiva-database/src/main/java/org/apache/maven/archiva/database/constraints/ArtifactsProcessedConstraint.java @@ -0,0 +1,72 @@ +package org.apache.maven.archiva.database.constraints; + +/* + * 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.Constraint; + +import java.util.Date; + +/** + * ArtifactsProcessedConstraint + * + * @author <a href="mailto:joakim@erdfelt.com">Joakim Erdfelt</a> + * @version $Id$ + */ +public class ArtifactsProcessedConstraint + extends AbstractDeclarativeConstraint + implements Constraint +{ + private String whereClause; + + public ArtifactsProcessedConstraint( boolean isProcessed ) + { + if ( isProcessed ) + { + whereClause = "whenProcessed != null"; + } + else + { + whereClause = "whenProcessed == null"; + } + } + + /** + * A Constraint showing artifacts processed since date provided. + * @param since + */ + public ArtifactsProcessedConstraint( Date since ) + { + whereClause = "whenProcessed > since"; + declImports = new String[] { "import java.util.Date" }; + declParams = new String[] { "Date since" }; + params = new Object[] { since }; + } + + public String getSortColumn() + { + return "groupId"; + } + + public String getWhereCondition() + { + return whereClause; + } + +} diff --git a/archiva-database/src/main/java/org/apache/maven/archiva/database/constraints/ArtifactsRelatedConstraint.java b/archiva-database/src/main/java/org/apache/maven/archiva/database/constraints/ArtifactsRelatedConstraint.java new file mode 100644 index 000000000..6f544ad59 --- /dev/null +++ b/archiva-database/src/main/java/org/apache/maven/archiva/database/constraints/ArtifactsRelatedConstraint.java @@ -0,0 +1,53 @@ +package org.apache.maven.archiva.database.constraints; + +/* + * 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.commons.lang.StringEscapeUtils; +import org.apache.maven.archiva.database.Constraint; + +/** + * ArtifactsRelatedConstraint + * + * @author <a href="mailto:joakim@erdfelt.com">Joakim Erdfelt</a> + * @version $Id$ + */ +public class ArtifactsRelatedConstraint + extends AbstractDeclarativeConstraint + implements Constraint +{ + private String whereClause; + + public ArtifactsRelatedConstraint( String groupId, String artifactId, String version ) + { + whereClause = "groupId == '" + StringEscapeUtils.escapeSql( groupId ) + "' AND artifactId == '" + + StringEscapeUtils.escapeSql( artifactId ) + "' AND version == '" + StringEscapeUtils.escapeSql( version ) + + "'"; + } + + public String getSortColumn() + { + return "classifier"; + } + + public String getWhereCondition() + { + return whereClause; + } +} diff --git a/archiva-database/src/main/java/org/apache/maven/archiva/database/constraints/MostRecentRepositoryScanStatistics.java b/archiva-database/src/main/java/org/apache/maven/archiva/database/constraints/MostRecentRepositoryScanStatistics.java new file mode 100644 index 000000000..496f4f2af --- /dev/null +++ b/archiva-database/src/main/java/org/apache/maven/archiva/database/constraints/MostRecentRepositoryScanStatistics.java @@ -0,0 +1,54 @@ +package org.apache.maven.archiva.database.constraints; + +/* + * 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.model.RepositoryContentStatistics; + +/** + * MostRecentRepositoryScanStatistics + * + * @author <a href="mailto:joakim@erdfelt.com">Joakim Erdfelt</a> + * @version $Id$ + */ +public class MostRecentRepositoryScanStatistics + extends AbstractSimpleConstraint +{ + private String sql; + + public MostRecentRepositoryScanStatistics( String repoId ) + { + sql = "SELECT FROM " + RepositoryContentStatistics.class.getName() + + " WHERE repositoryId == repoId PARAMETERS String repoId" + + " ORDER BY whenGathered DESCENDING" + + " RANGE 1,1"; + + super.params = new Object[] { repoId }; + } + + public Class getResultClass() + { + return RepositoryContentStatistics.class; + } + + public String getSelectSql() + { + return sql; + } +} diff --git a/archiva-database/src/main/java/org/apache/maven/archiva/database/constraints/OlderArtifactsByAgeConstraint.java b/archiva-database/src/main/java/org/apache/maven/archiva/database/constraints/OlderArtifactsByAgeConstraint.java new file mode 100644 index 000000000..55d8cbee8 --- /dev/null +++ b/archiva-database/src/main/java/org/apache/maven/archiva/database/constraints/OlderArtifactsByAgeConstraint.java @@ -0,0 +1,60 @@ +package org.apache.maven.archiva.database.constraints; + +/* + * 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.Constraint; + +import java.util.Calendar; +import java.util.Date; + +/** + * Constraint for artifacts that are of a certain age (in days) or older. + * + * @author <a href="mailto:joakim@erdfelt.com">Joakim Erdfelt</a> + * @version $Id$ + */ +public class OlderArtifactsByAgeConstraint + extends AbstractDeclarativeConstraint + implements Constraint +{ + private String whereClause; + + public OlderArtifactsByAgeConstraint( int daysOld ) + { + Calendar cal = Calendar.getInstance(); + cal.add( Calendar.DAY_OF_MONTH, ( -1 ) * daysOld ); + Date cutoffDate = cal.getTime(); + + whereClause = "this.lastModified <= cutoffDate"; + declImports = new String[] { "import java.util.Date" }; + declParams = new String[] { "java.util.Date cutoffDate" }; + params = new Object[] { cutoffDate }; + } + + public String getSortColumn() + { + return "groupId"; + } + + public String getWhereCondition() + { + return whereClause; + } +} diff --git a/archiva-database/src/main/java/org/apache/maven/archiva/database/constraints/OlderSnapshotArtifactsByAgeConstraint.java b/archiva-database/src/main/java/org/apache/maven/archiva/database/constraints/OlderSnapshotArtifactsByAgeConstraint.java new file mode 100644 index 000000000..311410463 --- /dev/null +++ b/archiva-database/src/main/java/org/apache/maven/archiva/database/constraints/OlderSnapshotArtifactsByAgeConstraint.java @@ -0,0 +1,60 @@ +package org.apache.maven.archiva.database.constraints; + +/* + * 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.Constraint; + +import java.util.Calendar; +import java.util.Date; + +/** + * Constraint for snapshot artifacts that are of a certain age (in days) or older. + * + * @author <a href="mailto:joakim@erdfelt.com">Joakim Erdfelt</a> + * @version $Id$ + */ +public class OlderSnapshotArtifactsByAgeConstraint + extends AbstractDeclarativeConstraint + implements Constraint +{ + private String whereClause; + + public OlderSnapshotArtifactsByAgeConstraint( int daysOld ) + { + Calendar cal = Calendar.getInstance(); + cal.add( Calendar.DAY_OF_MONTH, ( -1 ) * daysOld ); + Date cutoffDate = cal.getTime(); + + whereClause = "this.lastModified <= cutoffDate && this.snapshot == true"; + declImports = new String[] { "import java.util.Date" }; + declParams = new String[] { "java.util.Date cutoffDate" }; + params = new Object[] { cutoffDate }; + } + + public String getSortColumn() + { + return "groupId"; + } + + public String getWhereCondition() + { + return whereClause; + } +} diff --git a/archiva-database/src/main/java/org/apache/maven/archiva/database/constraints/RecentArtifactsByAgeConstraint.java b/archiva-database/src/main/java/org/apache/maven/archiva/database/constraints/RecentArtifactsByAgeConstraint.java new file mode 100644 index 000000000..3f2e101b5 --- /dev/null +++ b/archiva-database/src/main/java/org/apache/maven/archiva/database/constraints/RecentArtifactsByAgeConstraint.java @@ -0,0 +1,61 @@ +package org.apache.maven.archiva.database.constraints; + +/* + * 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.Constraint; + +import java.util.Calendar; +import java.util.Date; + +/** + * Constraint for artifacts that are of a certain age (in days) or newer. + * + * @author <a href="mailto:joakim@erdfelt.com">Joakim Erdfelt</a> + * @version $Id$ + */ +public class RecentArtifactsByAgeConstraint + extends AbstractDeclarativeConstraint + implements Constraint +{ + private String whereClause; + + public RecentArtifactsByAgeConstraint( int daysOld ) + { + Calendar cal = Calendar.getInstance(); + // Extra subtraction of 1 done to allow for lastModified that occur on the day represented by 'daysOld'. + cal.add( Calendar.DAY_OF_MONTH, (( -1 ) * daysOld) - 1 ); + Date cutoffDate = cal.getTime(); + + whereClause = "this.lastModified >= cutoffDate"; + declImports = new String[] { "import java.util.Date" }; + declParams = new String[] { "java.util.Date cutoffDate" }; + params = new Object[] { cutoffDate }; + } + + public String getSortColumn() + { + return "groupId"; + } + + public String getWhereCondition() + { + return whereClause; + } +} diff --git a/archiva-database/src/main/java/org/apache/maven/archiva/database/constraints/RepositoryProblemByTypeConstraint.java b/archiva-database/src/main/java/org/apache/maven/archiva/database/constraints/RepositoryProblemByTypeConstraint.java new file mode 100644 index 000000000..6be5f8362 --- /dev/null +++ b/archiva-database/src/main/java/org/apache/maven/archiva/database/constraints/RepositoryProblemByTypeConstraint.java @@ -0,0 +1,52 @@ +package org.apache.maven.archiva.database.constraints; + +/* + * 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.Constraint; + +/** + * RepositoryProblemByTypeConstraint + * + * @author <a href="mailto:joakim@erdfelt.com">Joakim Erdfelt</a> + * @version $Id$ + */ +public class RepositoryProblemByTypeConstraint + extends AbstractDeclarativeConstraint + implements Constraint +{ + private String whereClause; + + public RepositoryProblemByTypeConstraint( String desiredType ) + { + whereClause = "type == desiredType"; + declParams = new String[] { "String desiredType" }; + params = new Object[] { desiredType }; + } + + public String getSortColumn() + { + return "groupId"; + } + + public String getWhereCondition() + { + return whereClause; + } +} diff --git a/archiva-database/src/main/java/org/apache/maven/archiva/database/constraints/UniqueArtifactIdConstraint.java b/archiva-database/src/main/java/org/apache/maven/archiva/database/constraints/UniqueArtifactIdConstraint.java new file mode 100644 index 000000000..d8a6e3862 --- /dev/null +++ b/archiva-database/src/main/java/org/apache/maven/archiva/database/constraints/UniqueArtifactIdConstraint.java @@ -0,0 +1,60 @@ +package org.apache.maven.archiva.database.constraints; + +/* + * 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.Constraint; +import org.apache.maven.archiva.model.ArchivaArtifactModel; + +/** + * Obtain a set of unique ArtifactIds for the specified groupId. + * + * @author <a href="mailto:joakim@erdfelt.com">Joakim Erdfelt</a> + * @version $Id$ + */ +public class UniqueArtifactIdConstraint + extends AbstractSimpleConstraint + implements Constraint +{ + private String sql; + + /** + * Obtain a set of unique ArtifactIds for the specified groupId. + * + * @param groupId the groupId to search for artifactIds within. + */ + public UniqueArtifactIdConstraint( String groupId ) + { + sql = "SELECT artifactId FROM " + ArchivaArtifactModel.class.getName() + + " WHERE groupId == selectedGroupId PARAMETERS String selectedGroupId" + + " GROUP BY artifactId ORDER BY artifactId ASCENDING"; + + super.params = new Object[] { groupId }; + } + + public Class getResultClass() + { + return String.class; + } + + public String getSelectSql() + { + return sql; + } +} diff --git a/archiva-database/src/main/java/org/apache/maven/archiva/database/constraints/UniqueGroupIdConstraint.java b/archiva-database/src/main/java/org/apache/maven/archiva/database/constraints/UniqueGroupIdConstraint.java new file mode 100644 index 000000000..246e3e78d --- /dev/null +++ b/archiva-database/src/main/java/org/apache/maven/archiva/database/constraints/UniqueGroupIdConstraint.java @@ -0,0 +1,62 @@ +package org.apache.maven.archiva.database.constraints; + +/* + * 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.Constraint; +import org.apache.maven.archiva.model.ArchivaArtifactModel; + +/** + * UniqueGroupIdConstraint + * + * @author <a href="mailto:joakim@erdfelt.com">Joakim Erdfelt</a> + * @version $Id$ + */ +public class UniqueGroupIdConstraint + extends AbstractSimpleConstraint + implements Constraint +{ + private String sql; + + public UniqueGroupIdConstraint() + { + /* this assumes search for no groupId prefix */ + sql = "SELECT groupId FROM " + ArchivaArtifactModel.class.getName() + + " GROUP BY groupId ORDER BY groupId ASCENDING"; + } + + public UniqueGroupIdConstraint( String groupIdPrefix ) + { + sql = "SELECT groupId FROM " + ArchivaArtifactModel.class.getName() + + " WHERE groupId.startsWith(groupIdPrefix) PARAMETERS String groupIdPrefix" + + " GROUP BY groupId ORDER BY groupId ASCENDING"; + + super.params = new Object[] { groupIdPrefix }; + } + + public Class getResultClass() + { + return String.class; + } + + public String getSelectSql() + { + return sql; + } +} diff --git a/archiva-database/src/main/java/org/apache/maven/archiva/database/constraints/UniqueVersionConstraint.java b/archiva-database/src/main/java/org/apache/maven/archiva/database/constraints/UniqueVersionConstraint.java new file mode 100644 index 000000000..f39c603a3 --- /dev/null +++ b/archiva-database/src/main/java/org/apache/maven/archiva/database/constraints/UniqueVersionConstraint.java @@ -0,0 +1,73 @@ +package org.apache.maven.archiva.database.constraints; + +/* + * 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.commons.lang.StringUtils; +import org.apache.maven.archiva.database.Constraint; +import org.apache.maven.archiva.model.ArchivaArtifactModel; + +/** + * Obtain the list of version's for specific GroupId and ArtifactId. + * + * @author <a href="mailto:joakim@erdfelt.com">Joakim Erdfelt</a> + * @version $Id$ + */ +public class UniqueVersionConstraint + extends AbstractSimpleConstraint + implements Constraint +{ + private String sql; + + /** + * Obtain the list of version's for specific GroupId and ArtifactId. + * + * @param groupId the selected groupId. + * @param artifactId the selected artifactId. + */ + public UniqueVersionConstraint( String groupId, String artifactId ) + { + if ( StringUtils.isBlank( groupId ) ) + { + throw new IllegalArgumentException( "A blank groupId is not allowed." ); + } + + if ( StringUtils.isBlank( artifactId ) ) + { + throw new IllegalArgumentException( "A blank artifactId is not allowed." ); + } + + sql = "SELECT version FROM " + ArchivaArtifactModel.class.getName() + + " WHERE groupId == selectedGroupId && artifactId == selectedArtifactId" + + " PARAMETERS String selectedGroupId, String selectedArtifactId" + + " GROUP BY version ORDER BY version ASCENDING"; + + super.params = new Object[] { groupId, artifactId }; + } + + public Class getResultClass() + { + return String.class; + } + + public String getSelectSql() + { + return sql; + } +} diff --git a/archiva-database/src/main/java/org/apache/maven/archiva/database/jdo/JdoAccess.java b/archiva-database/src/main/java/org/apache/maven/archiva/database/jdo/JdoAccess.java new file mode 100644 index 000000000..75e895360 --- /dev/null +++ b/archiva-database/src/main/java/org/apache/maven/archiva/database/jdo/JdoAccess.java @@ -0,0 +1,560 @@ +package org.apache.maven.archiva.database.jdo; + +/* + * 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.commons.lang.StringUtils; +import org.apache.maven.archiva.database.ArchivaDatabaseException; +import org.apache.maven.archiva.database.Constraint; +import org.apache.maven.archiva.database.DeclarativeConstraint; +import org.apache.maven.archiva.database.ObjectNotFoundException; +import org.apache.maven.archiva.database.SimpleConstraint; +import org.apache.maven.archiva.database.constraints.AbstractSimpleConstraint; +import org.apache.maven.archiva.model.CompoundKey; +import org.codehaus.plexus.jdo.JdoFactory; +import org.codehaus.plexus.personality.plexus.lifecycle.phase.Initializable; +import org.codehaus.plexus.personality.plexus.lifecycle.phase.InitializationException; + +import java.io.PrintStream; +import java.util.ArrayList; +import java.util.List; + +import javax.jdo.Extent; +import javax.jdo.JDOException; +import javax.jdo.JDOHelper; +import javax.jdo.JDOObjectNotFoundException; +import javax.jdo.JDOUserException; +import javax.jdo.PersistenceManager; +import javax.jdo.PersistenceManagerFactory; +import javax.jdo.Query; +import javax.jdo.Transaction; +import javax.jdo.datastore.DataStoreCache; +import javax.jdo.listener.InstanceLifecycleEvent; +import javax.jdo.listener.InstanceLifecycleListener; +import javax.jdo.listener.StoreLifecycleListener; +import javax.jdo.spi.Detachable; +import javax.jdo.spi.PersistenceCapable; + +/** + * JdoAccess + * + * @author <a href="mailto:joakim@erdfelt.com">Joakim Erdfelt</a> + * @version $Id$ + * @plexus.component role="org.apache.maven.archiva.database.jdo.JdoAccess" role-hint="archiva" + */ +public class JdoAccess + implements Initializable, InstanceLifecycleListener, StoreLifecycleListener +{ + /** + * @plexus.requirement role-hint="archiva" + */ + private JdoFactory jdoFactory; + + private PersistenceManagerFactory pmf; + + public void initialize() + throws InitializationException + { + pmf = jdoFactory.getPersistenceManagerFactory(); + + pmf.addInstanceLifecycleListener( this, null ); + } + + public static void dumpObjectState( PrintStream out, Object o ) + { + final String STATE = "[STATE] "; + final String INDENT = " "; + + if ( o == null ) + { + out.println( STATE + "Object is null." ); + return; + } + + out.println( STATE + "Object " + o.getClass().getName() ); + + if ( !( o instanceof PersistenceCapable ) ) + { + out.println( INDENT + "is NOT PersistenceCapable (not a jdo object?)" ); + return; + } + + out.println( INDENT + "is PersistenceCapable." ); + if ( o instanceof Detachable ) + { + out.println( INDENT + "is Detachable" ); + } + + out.println( INDENT + "is new : " + Boolean.toString( JDOHelper.isNew( o ) ) ); + out.println( INDENT + "is transactional : " + Boolean.toString( JDOHelper.isTransactional( o ) ) ); + out.println( INDENT + "is deleted : " + Boolean.toString( JDOHelper.isDeleted( o ) ) ); + out.println( INDENT + "is detached : " + Boolean.toString( JDOHelper.isDetached( o ) ) ); + out.println( INDENT + "is dirty : " + Boolean.toString( JDOHelper.isDirty( o ) ) ); + out.println( INDENT + "is persistent : " + Boolean.toString( JDOHelper.isPersistent( o ) ) ); + + out.println( INDENT + "object id : " + JDOHelper.getObjectId( o ) ); + } + + public PersistenceManager getPersistenceManager() + { + PersistenceManager pm = pmf.getPersistenceManager(); + + pm.getFetchPlan().setMaxFetchDepth( -1 ); + + return pm; + } + + public void enableCache( Class clazz ) + { + DataStoreCache cache = pmf.getDataStoreCache(); + cache.pinAll( clazz, false ); // Pin all objects of type clazz from now on + } + + public Object saveObject( Object object ) + { + return saveObject( object, null ); + } + + public Object saveObject( Object object, String[] fetchGroups ) + { + PersistenceManager pm = getPersistenceManager(); + Transaction tx = pm.currentTransaction(); + + try + { + tx.begin(); + + if ( ( JDOHelper.getObjectId( object ) != null ) && !JDOHelper.isDetached( object ) ) + { + // This is a fatal error that means we need to fix our code. + // Leave it as a JDOUserException, it's intentional. + throw new JDOUserException( "Existing object is not detached: " + object, object ); + } + + if ( fetchGroups != null ) + { + for ( int i = 0; i >= fetchGroups.length; i++ ) + { + pm.getFetchPlan().addGroup( fetchGroups[i] ); + } + } + + pm.makePersistent( object ); + + object = pm.detachCopy( object ); + + tx.commit(); + + return object; + } + finally + { + rollbackIfActive( tx ); + } + } + + public List getAllObjects( Class clazz ) + { + return queryObjects( clazz, null ); + } + + public List queryObjects( Class clazz, Constraint constraint ) + { + PersistenceManager pm = getPersistenceManager(); + Transaction tx = pm.currentTransaction(); + + try + { + tx.begin(); + + List result = null; + + if ( constraint != null ) + { + if ( constraint instanceof DeclarativeConstraint ) + { + result = processConstraint( pm, clazz, (DeclarativeConstraint) constraint ); + } + else if ( constraint instanceof AbstractSimpleConstraint ) + { + result = processConstraint( pm, (SimpleConstraint) constraint ); + } + else + { + result = processUnconstrained( pm, clazz ); + } + } + else + { + result = processUnconstrained( pm, clazz ); + } + + result = (List) pm.detachCopyAll( result ); + + tx.commit(); + + return result; + } + finally + { + rollbackIfActive( tx ); + } + } + + public List queryObjects( SimpleConstraint constraint ) + { + PersistenceManager pm = getPersistenceManager(); + Transaction tx = pm.currentTransaction(); + + try + { + tx.begin(); + + List result = processConstraint( pm, constraint ); + + // Only detach if results are known to be persistable. + if ( constraint.isResultsPersistable() ) + { + result = (List) pm.detachCopyAll( result ); + } + else + { + List copiedResults = new ArrayList(); + copiedResults.addAll( result ); + result = copiedResults; + } + + tx.commit(); + + return result; + } + finally + { + rollbackIfActive( tx ); + } + } + + private List processUnconstrained( PersistenceManager pm, Class clazz ) + { + Extent extent = pm.getExtent( clazz, true ); + Query query = pm.newQuery( extent ); + return (List) query.execute(); + } + + private List processConstraint( PersistenceManager pm, SimpleConstraint constraint ) + { + Query query = pm.newQuery( constraint.getSelectSql() ); + + if ( constraint.getResultClass() == null ) + { + throw new IllegalStateException( "Unable to use a SimpleConstraint with a null result class." ); + } + + query.setResultClass( constraint.getResultClass() ); + + if ( constraint.getFetchLimits() != null ) + { + pm.getFetchPlan().addGroup( constraint.getFetchLimits() ); + } + + if ( constraint.getParameters() != null ) + { + return processParameterizedQuery( query, constraint.getParameters() ); + } + + return (List) query.execute(); + } + + private List processConstraint( PersistenceManager pm, Class clazz, DeclarativeConstraint constraint ) + { + Extent extent = pm.getExtent( clazz, true ); + Query query = pm.newQuery( extent ); + + if ( constraint.getSortColumn() != null ) + { + String ordering = constraint.getSortColumn(); + + if ( constraint.getSortDirection() != null ) + { + ordering += " " + constraint.getSortDirection(); + } + + query.setOrdering( ordering ); + } + + if ( constraint.getFetchLimits() != null ) + { + pm.getFetchPlan().addGroup( constraint.getFetchLimits() ); + } + + if ( constraint.getWhereCondition() != null ) + { + query.setFilter( constraint.getWhereCondition() ); + } + + if ( constraint.getDeclaredImports() != null ) + { + for ( int i = 0; i < constraint.getDeclaredImports().length; i++ ) + { + String qimport = constraint.getDeclaredImports()[i]; + query.declareImports( qimport ); + } + } + + if ( constraint.getDeclaredParameters() != null ) + { + if ( constraint.getParameters() == null ) + { + throw new JDOException( "Unable to use query, there are declared parameters, " + + "but no parameter objects to use." ); + } + + if ( constraint.getParameters().length != constraint.getDeclaredParameters().length ) + { + throw new JDOException( "Unable to use query, there are <" + constraint.getDeclaredParameters().length + + "> declared parameters, yet there are <" + constraint.getParameters().length + + "> parameter objects to use. This should be equal." ); + } + + for ( int i = 0; i < constraint.getDeclaredParameters().length; i++ ) + { + String declaredParam = constraint.getDeclaredParameters()[i]; + query.declareParameters( declaredParam ); + } + + return processParameterizedQuery( query, constraint.getParameters() ); + } + else + { + return (List) query.execute(); + } + } + + private List processParameterizedQuery( Query query, Object parameters[] ) + { + switch ( parameters.length ) + { + case 1: + return (List) query.execute( parameters[0] ); + case 2: + return (List) query.execute( parameters[0], parameters[1] ); + case 3: + return (List) query.execute( parameters[0], parameters[1], parameters[2] ); + default: + throw new JDOException( "Unable to use more than 3 parameters." ); + } + } + + public Object getObjectById( Class clazz, Object id, String fetchGroup ) + throws ObjectNotFoundException, ArchivaDatabaseException + { + if ( id == null ) + { + throw new ObjectNotFoundException( "Unable to get object '" + clazz.getName() + "' from jdo using null id." ); + } + + PersistenceManager pm = getPersistenceManager(); + Transaction tx = pm.currentTransaction(); + + try + { + tx.begin(); + + if ( fetchGroup != null ) + { + pm.getFetchPlan().addGroup( fetchGroup ); + } + + Object objectId = null; + + if ( id instanceof CompoundKey ) + { + objectId = pm.newObjectIdInstance( clazz, id.toString() ); + } + else + { + objectId = pm.newObjectIdInstance( clazz, id ); + } + + Object object = pm.getObjectById( objectId ); + + object = pm.detachCopy( object ); + + tx.commit(); + + return object; + } + catch ( JDOObjectNotFoundException e ) + { + throw new ObjectNotFoundException( "Unable to find Database Object [" + id + "] of type " + clazz.getName() + + " using " + ( ( fetchGroup == null ) ? "no fetch-group" : "a fetch-group of [" + fetchGroup + "]" ), + e, id ); + } + catch ( JDOException e ) + { + throw new ArchivaDatabaseException( "Error in JDO during get of Database object id [" + id + "] of type " + + clazz.getName() + " using " + + ( ( fetchGroup == null ) ? "no fetch-group" : "a fetch-group of [" + fetchGroup + "]" ), e ); + } + finally + { + rollbackIfActive( tx ); + } + } + + public Object getObjectById( Class clazz, String id, String fetchGroup ) + throws ObjectNotFoundException, ArchivaDatabaseException + { + if ( StringUtils.isEmpty( id ) ) + { + throw new ObjectNotFoundException( "Unable to get object '" + clazz.getName() + + "' from jdo using null/empty id." ); + } + + return getObjectById( clazz, (Object) id, fetchGroup ); + } + + public boolean objectExists( Object object ) + { + return ( JDOHelper.getObjectId( object ) != null ); + } + + public boolean objectExistsById( Class clazz, String id ) + throws ArchivaDatabaseException + { + try + { + Object o = getObjectById( clazz, id, null ); + return ( o != null ); + } + catch ( ObjectNotFoundException e ) + { + return false; + } + } + + public void removeObject( Object o ) + throws ArchivaDatabaseException + { + if ( o == null ) + { + throw new ArchivaDatabaseException( "Unable to remove null object '" + o.getClass().getName() + "'" ); + } + + PersistenceManager pm = getPersistenceManager(); + Transaction tx = pm.currentTransaction(); + + try + { + tx.begin(); + + o = pm.getObjectById( pm.getObjectId( o ) ); + + pm.deletePersistent( o ); + + tx.commit(); + } + finally + { + rollbackIfActive( tx ); + } + } + + public void rollbackIfActive( Transaction tx ) + { + PersistenceManager pm = tx.getPersistenceManager(); + + try + { + if ( tx.isActive() ) + { + tx.rollback(); + } + } + finally + { + closePersistenceManager( pm ); + } + } + + public void closePersistenceManager( PersistenceManager pm ) + { + try + { + pm.close(); + } + catch ( JDOUserException e ) + { + // ignore + } + } + + public void postDelete( InstanceLifecycleEvent evt ) + { + PersistenceCapable obj = ( (PersistenceCapable) evt.getSource() ); + + if ( obj == null ) + { + // Do not track null objects. + // These events are typically a product of an internal lifecycle event. + return; + } + } + + public void preDelete( InstanceLifecycleEvent evt ) + { + // ignore + } + + public void postStore( InstanceLifecycleEvent evt ) + { + // PersistenceCapable obj = ( (PersistenceCapable) evt.getSource() ); + } + + public void preStore( InstanceLifecycleEvent evt ) + { + // ignore + } + + public void removeAll( Class aClass ) + { + PersistenceManager pm = getPersistenceManager(); + Transaction tx = pm.currentTransaction(); + + try + { + tx.begin(); + + Query query = pm.newQuery( aClass ); + query.deletePersistentAll(); + + tx.commit(); + } + finally + { + rollbackIfActive( tx ); + } + } + + public JdoFactory getJdoFactory() + { + return jdoFactory; + } + +} diff --git a/archiva-database/src/main/java/org/apache/maven/archiva/database/jdo/JdoArchivaDAO.java b/archiva-database/src/main/java/org/apache/maven/archiva/database/jdo/JdoArchivaDAO.java new file mode 100644 index 000000000..7fbf79ad5 --- /dev/null +++ b/archiva-database/src/main/java/org/apache/maven/archiva/database/jdo/JdoArchivaDAO.java @@ -0,0 +1,99 @@ +package org.apache.maven.archiva.database.jdo; + +/* + * 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.ArtifactDAO; +import org.apache.maven.archiva.database.ProjectModelDAO; +import org.apache.maven.archiva.database.RepositoryDAO; +import org.apache.maven.archiva.database.RepositoryProblemDAO; +import org.apache.maven.archiva.database.SimpleConstraint; +import org.codehaus.plexus.logging.AbstractLogEnabled; + +import java.io.Serializable; +import java.util.List; + +/** + * JdoArchivaDAO + * + * @author <a href="mailto:joakim@erdfelt.com">Joakim Erdfelt</a> + * @version $Id$ + * + * @plexus.component role-hint="jdo" + */ +public class JdoArchivaDAO + extends AbstractLogEnabled + implements ArchivaDAO +{ + /** + * @plexus.requirement role-hint="archiva" + */ + private JdoAccess jdo; + + /** + * @plexus.requirement role-hint="jdo" + */ + private ArtifactDAO artifactDAO; + + /** + * @plexus.requirement role-hint="jdo" + */ + private ProjectModelDAO projectModelDAO; + + /** + * @plexus.requirement role-hint="jdo" + */ + private RepositoryDAO repositoryDAO; + + /** + * @plexus.requirement role-hint="jdo" + */ + private RepositoryProblemDAO repositoryProblemDAO; + + public List query( SimpleConstraint constraint ) + { + return jdo.queryObjects( constraint ); + } + + public Object save( Serializable obj ) + { + return jdo.saveObject( obj ); + } + + public ArtifactDAO getArtifactDAO() + { + return artifactDAO; + } + + public ProjectModelDAO getProjectModelDAO() + { + return projectModelDAO; + } + + public RepositoryDAO getRepositoryDAO() + { + return repositoryDAO; + } + + public RepositoryProblemDAO getRepositoryProblemDAO() + { + return repositoryProblemDAO; + } +} diff --git a/archiva-database/src/main/java/org/apache/maven/archiva/database/jdo/JdoArtifactDAO.java b/archiva-database/src/main/java/org/apache/maven/archiva/database/jdo/JdoArtifactDAO.java new file mode 100644 index 000000000..33bdbe2fc --- /dev/null +++ b/archiva-database/src/main/java/org/apache/maven/archiva/database/jdo/JdoArtifactDAO.java @@ -0,0 +1,124 @@ +package org.apache.maven.archiva.database.jdo; + +/* + * 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.ArchivaDatabaseException; +import org.apache.maven.archiva.database.ArtifactDAO; +import org.apache.maven.archiva.database.Constraint; +import org.apache.maven.archiva.database.ObjectNotFoundException; +import org.apache.maven.archiva.model.ArchivaArtifact; +import org.apache.maven.archiva.model.ArchivaArtifactModel; +import org.apache.maven.archiva.model.jpox.ArchivaArtifactModelKey; +import org.codehaus.plexus.logging.AbstractLogEnabled; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.List; + +/** + * JdoArtifactDAO + * + * @author <a href="mailto:joakim@erdfelt.com">Joakim Erdfelt</a> + * @version $Id$ + * + * @plexus.component role-hint="jdo" + */ +public class JdoArtifactDAO + extends AbstractLogEnabled + implements ArtifactDAO +{ + /** + * @plexus.requirement role-hint="archiva" + */ + private JdoAccess jdo; + + /* .\ Archiva Artifact \. _____________________________________________________________ */ + + public ArchivaArtifact createArtifact( String groupId, String artifactId, String version, String classifier, + String type ) + { + ArchivaArtifact artifact; + + try + { + artifact = getArtifact( groupId, artifactId, version, classifier, type ); + } + catch ( ArchivaDatabaseException e ) + { + artifact = new ArchivaArtifact( groupId, artifactId, version, classifier, type ); + } + + return artifact; + } + + public ArchivaArtifact getArtifact( String groupId, String artifactId, String version, String classifier, + String type ) + throws ObjectNotFoundException, ArchivaDatabaseException + { + ArchivaArtifactModelKey key = new ArchivaArtifactModelKey(); + key.setGroupId( groupId ); + key.setArtifactId( artifactId ); + key.setVersion( version ); + key.setClassifier( classifier ); + key.setType( type ); + + ArchivaArtifactModel model = (ArchivaArtifactModel) jdo.getObjectById( ArchivaArtifactModel.class, key, null ); + + return new ArchivaArtifact( model ); + } + + public List queryArtifacts( Constraint constraint ) + throws ObjectNotFoundException, ArchivaDatabaseException + { + List results = jdo.queryObjects( ArchivaArtifactModel.class, constraint ); + if ( ( results == null ) || results.isEmpty() ) + { + return results; + } + + List ret = new ArrayList(); + Iterator it = results.iterator(); + while ( it.hasNext() ) + { + ArchivaArtifactModel model = (ArchivaArtifactModel) it.next(); + ret.add( new ArchivaArtifact( model ) ); + } + + return ret; + } + + public ArchivaArtifact saveArtifact( ArchivaArtifact artifact ) + throws ArchivaDatabaseException + { + ArchivaArtifactModel model = (ArchivaArtifactModel) jdo.saveObject( artifact.getModel() ); + if ( model == null ) + { + return null; + } + + return new ArchivaArtifact( model ); + } + + public void deleteArtifact( ArchivaArtifact artifact ) + throws ArchivaDatabaseException + { + jdo.removeObject( artifact.getModel() ); + } +} diff --git a/archiva-database/src/main/java/org/apache/maven/archiva/database/jdo/JdoProjectModelDAO.java b/archiva-database/src/main/java/org/apache/maven/archiva/database/jdo/JdoProjectModelDAO.java new file mode 100644 index 000000000..873f1b44a --- /dev/null +++ b/archiva-database/src/main/java/org/apache/maven/archiva/database/jdo/JdoProjectModelDAO.java @@ -0,0 +1,94 @@ +package org.apache.maven.archiva.database.jdo; + +/* + * 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.ArchivaDatabaseException; +import org.apache.maven.archiva.database.Constraint; +import org.apache.maven.archiva.database.ObjectNotFoundException; +import org.apache.maven.archiva.database.ProjectModelDAO; +import org.apache.maven.archiva.model.ArchivaProjectModel; +import org.apache.maven.archiva.model.jpox.ArchivaProjectModelKey; + +import java.util.List; + +/** + * JdoProjectModelDAO + * + * @author <a href="mailto:joakim@erdfelt.com">Joakim Erdfelt</a> + * @version $Id$ + * + * @plexus.component role-hint="jdo" + */ +public class JdoProjectModelDAO + implements ProjectModelDAO +{ + /** + * @plexus.requirement role-hint="archiva" + */ + private JdoAccess jdo; + + public ArchivaProjectModel createProjectModel( String groupId, String artifactId, String version ) + { + ArchivaProjectModel model; + + try + { + model = getProjectModel( groupId, artifactId, version ); + } + catch ( ArchivaDatabaseException e ) + { + model = new ArchivaProjectModel(); + model.setGroupId( groupId ); + model.setArtifactId( artifactId ); + model.setVersion( version ); + } + + return model; + } + + public ArchivaProjectModel getProjectModel( String groupId, String artifactId, String version ) + throws ObjectNotFoundException, ArchivaDatabaseException + { + ArchivaProjectModelKey key = new ArchivaProjectModelKey(); + key.groupId = groupId; + key.artifactId = artifactId; + key.version = version; + + return (ArchivaProjectModel) jdo.getObjectById( ArchivaProjectModel.class, key, null ); + } + + public List queryProjectModels( Constraint constraint ) + throws ObjectNotFoundException, ArchivaDatabaseException + { + return jdo.queryObjects( ArchivaProjectModel.class, constraint ); + } + + public ArchivaProjectModel saveProjectModel( ArchivaProjectModel model ) + throws ArchivaDatabaseException + { + return (ArchivaProjectModel) jdo.saveObject( model ); + } + + public void deleteProjectModel( ArchivaProjectModel model ) + throws ArchivaDatabaseException + { + jdo.removeObject( model ); + } +} diff --git a/archiva-database/src/main/java/org/apache/maven/archiva/database/jdo/JdoRepositoryDAO.java b/archiva-database/src/main/java/org/apache/maven/archiva/database/jdo/JdoRepositoryDAO.java new file mode 100644 index 000000000..0774c1c1d --- /dev/null +++ b/archiva-database/src/main/java/org/apache/maven/archiva/database/jdo/JdoRepositoryDAO.java @@ -0,0 +1,118 @@ +package org.apache.maven.archiva.database.jdo; + +/* + * 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.ArchivaDatabaseException; +import org.apache.maven.archiva.database.Constraint; +import org.apache.maven.archiva.database.ObjectNotFoundException; +import org.apache.maven.archiva.database.RepositoryDAO; +import org.apache.maven.archiva.model.ArchivaRepository; +import org.apache.maven.archiva.model.ArchivaRepositoryModel; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.List; + +/** + * JdoRepositoryDAO + * + * @author <a href="mailto:joakim@erdfelt.com">Joakim Erdfelt</a> + * @version $Id$ + * + * @plexus.component role-hint="jdo" + */ +public class JdoRepositoryDAO + implements RepositoryDAO +{ + /** + * @plexus.requirement role-hint="archiva" + */ + private JdoAccess jdo; + + /* .\ Archiva Repository \.____________________________________________________________ */ + + public ArchivaRepository createRepository( String id, String name, String url ) + { + ArchivaRepository repo; + + try + { + repo = getRepository( id ); + } + catch ( ArchivaDatabaseException e ) + { + repo = new ArchivaRepository( id, name, url ); + } + + return repo; + } + + public List getRepositories() + throws ObjectNotFoundException, ArchivaDatabaseException + { + return queryRepositories( null ); + } + + public ArchivaRepository getRepository( String id ) + throws ObjectNotFoundException, ArchivaDatabaseException + { + ArchivaRepositoryModel model = (ArchivaRepositoryModel) jdo.getObjectById( ArchivaRepositoryModel.class, id, + null ); + return new ArchivaRepository( model ); + } + + public List queryRepositories( Constraint constraint ) + throws ObjectNotFoundException, ArchivaDatabaseException + { + List results = jdo.queryObjects( ArchivaRepositoryModel.class, constraint ); + + if ( ( results == null ) || results.isEmpty() ) + { + return results; + } + + List ret = new ArrayList(); + Iterator it = results.iterator(); + while ( it.hasNext() ) + { + ArchivaRepositoryModel model = (ArchivaRepositoryModel) it.next(); + ret.add( new ArchivaRepository( model ) ); + } + + return ret; + } + + public ArchivaRepository saveRepository( ArchivaRepository repository ) + { + ArchivaRepositoryModel model = (ArchivaRepositoryModel) jdo.saveObject( repository.getModel() ); + if ( model == null ) + { + return null; + } + + return new ArchivaRepository( model ); + } + + public void deleteRepository( ArchivaRepository repository ) + throws ArchivaDatabaseException + { + jdo.removeObject( repository.getModel() ); + } +} diff --git a/archiva-database/src/main/java/org/apache/maven/archiva/database/jdo/JdoRepositoryProblemDAO.java b/archiva-database/src/main/java/org/apache/maven/archiva/database/jdo/JdoRepositoryProblemDAO.java new file mode 100644 index 000000000..ee3ed6291 --- /dev/null +++ b/archiva-database/src/main/java/org/apache/maven/archiva/database/jdo/JdoRepositoryProblemDAO.java @@ -0,0 +1,63 @@ +package org.apache.maven.archiva.database.jdo; + +/* + * 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.ArchivaDatabaseException; +import org.apache.maven.archiva.database.Constraint; +import org.apache.maven.archiva.database.ObjectNotFoundException; +import org.apache.maven.archiva.database.RepositoryProblemDAO; +import org.apache.maven.archiva.model.RepositoryProblem; + +import java.util.List; + +/** + * JdoRepositoryProblemDAO + * + * @author <a href="mailto:joakim@erdfelt.com">Joakim Erdfelt</a> + * @version $Id$ + * + * @plexus.component role-hint="jdo" + */ +public class JdoRepositoryProblemDAO + implements RepositoryProblemDAO +{ + /** + * @plexus.requirement role-hint="archiva" + */ + private JdoAccess jdo; + + public List queryRepositoryProblems( Constraint constraint ) + throws ObjectNotFoundException, ArchivaDatabaseException + { + return jdo.queryObjects( RepositoryProblem.class, constraint ); + } + + public RepositoryProblem saveRepositoryProblem( RepositoryProblem problem ) + throws ArchivaDatabaseException + { + return (RepositoryProblem) jdo.saveObject( problem ); + } + + public void deleteRepositoryProblem( RepositoryProblem problem ) + throws ArchivaDatabaseException + { + jdo.removeObject( problem ); + } +} diff --git a/archiva-database/src/main/java/org/apache/maven/archiva/database/key/MetadataKey.java b/archiva-database/src/main/java/org/apache/maven/archiva/database/key/MetadataKey.java deleted file mode 100644 index b06e03b17..000000000 --- a/archiva-database/src/main/java/org/apache/maven/archiva/database/key/MetadataKey.java +++ /dev/null @@ -1,46 +0,0 @@ -package org.apache.maven.archiva.database.key; - -public class MetadataKey { - - private String groupId; - private String artifactId; - private String version; - private int metadataKey; - - public MetadataKey( String groupId, String artifactId, String version ) - { - this.groupId = groupId; - this.artifactId = artifactId; - this.version = version; - } - - public MetadataKey() {} - - public String getArtifactId() { - return artifactId; - } - public void setArtifactId(String artifactId) { - this.artifactId = artifactId; - } - public String getGroupId() { - return groupId; - } - public void setGroupId(String groupId) { - this.groupId = groupId; - } - public int getMetadataKey() { - return metadataKey; - } - public void setMetadataKey(int id) { - this.metadataKey = id; - } - public String getVersion() { - return version; - } - public void setVersion(String version) { - this.version = version; - } - - - -} diff --git a/archiva-database/src/main/java/org/apache/maven/archiva/database/updater/DatabaseUpdater.java b/archiva-database/src/main/java/org/apache/maven/archiva/database/updater/DatabaseUpdater.java new file mode 100644 index 000000000..7dd27c3af --- /dev/null +++ b/archiva-database/src/main/java/org/apache/maven/archiva/database/updater/DatabaseUpdater.java @@ -0,0 +1,79 @@ +package org.apache.maven.archiva.database.updater; + +/* + * 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.ArchivaDatabaseException; +import org.apache.maven.archiva.model.ArchivaArtifact; + +/** + * The database update component. + * + * @author <a href="mailto:joakim@erdfelt.com">Joakim Erdfelt</a> + * @version $Id$ + */ +public interface DatabaseUpdater +{ + /** + * Execute the {@link #updateAllUnprocessed()} and {@link #updateAllProcessed()} + * tasks in one go. + * + * @throws ArchivaDatabaseException + */ + public void update() + throws ArchivaDatabaseException; + + /** + * Update all unprocessed content. + * + * @throws ArchivaDatabaseException if there was a fatal error with the database. + */ + public void updateAllUnprocessed() + throws ArchivaDatabaseException; + + /** + * Update specific unprocessed content. + * + * @throws ArchivaDatabaseException if there was a fatal error with the database. + */ + public void updateUnprocessed( ArchivaArtifact artifact ) + throws ArchivaDatabaseException; + + /** + * Update all previously processed content. + * + * This is done to allow archiva to remove content from the database that + * may have been removed from the filesystem too. + * + * @throws ArchivaDatabaseException if there was a fatal error with the database. + */ + public void updateAllProcessed() + throws ArchivaDatabaseException; + + /** + * Update specific processed content. + * + * Example: This is done to allow a specific artifact to be removed from the + * database if it no longer exists on the filesystem. + * + * @throws ArchivaDatabaseException if there was a fatal error with the database. + */ + public void updateProcessed( ArchivaArtifact artifact ) + throws ArchivaDatabaseException; +} diff --git a/archiva-database/src/main/java/org/apache/maven/archiva/database/updater/JdoDatabaseUpdater.java b/archiva-database/src/main/java/org/apache/maven/archiva/database/updater/JdoDatabaseUpdater.java new file mode 100644 index 000000000..98a5d5635 --- /dev/null +++ b/archiva-database/src/main/java/org/apache/maven/archiva/database/updater/JdoDatabaseUpdater.java @@ -0,0 +1,274 @@ +package org.apache.maven.archiva.database.updater; + +/* + * 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.configuration.ArchivaConfiguration; +import org.apache.maven.archiva.configuration.DatabaseScanningConfiguration; +import org.apache.maven.archiva.consumers.ArchivaArtifactConsumer; +import org.apache.maven.archiva.consumers.ConsumerException; +import org.apache.maven.archiva.database.ArchivaDAO; +import org.apache.maven.archiva.database.ArchivaDatabaseException; +import org.apache.maven.archiva.database.constraints.ArtifactsProcessedConstraint; +import org.apache.maven.archiva.model.ArchivaArtifact; +import org.codehaus.plexus.logging.AbstractLogEnabled; +import org.codehaus.plexus.personality.plexus.lifecycle.phase.Initializable; +import org.codehaus.plexus.personality.plexus.lifecycle.phase.InitializationException; +import org.codehaus.plexus.registry.Registry; +import org.codehaus.plexus.registry.RegistryListener; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.Date; +import java.util.Iterator; +import java.util.List; +import java.util.Map; + +/** + * JdoDatabaseUpdater + * + * @author <a href="mailto:joakim@erdfelt.com">Joakim Erdfelt</a> + * @version $Id$ + * + * @plexus.component role="org.apache.maven.archiva.database.updater.DatabaseUpdater" + * role-hint="jdo" + */ +public class JdoDatabaseUpdater + extends AbstractLogEnabled + implements DatabaseUpdater, RegistryListener, Initializable +{ + /** + * @plexus.requirement role-hint="jdo" + */ + private ArchivaDAO dao; + + /** + * @plexus.requirement + */ + private ArchivaConfiguration configuration; + + /** + * The collection of available consumers. + * @plexus.requirement role="org.apache.maven.archiva.consumers.ArchivaArtifactConsumer" + */ + private Map availableConsumers; + + /** + * The list of active consumers for unprocessed content. + */ + private List activeUnprocessedConsumers = new ArrayList(); + + /** + * The list of active consumers for processed content. + */ + private List activeProcessedConsumers = new ArrayList(); + + /** + * The list of registry (configuration) property names that will trigger a refresh of the activeConsumers list. + */ + private List propertyNameTriggers = new ArrayList(); + + public void update() + throws ArchivaDatabaseException + { + updateAllUnprocessed(); + updateAllProcessed(); + } + + public void updateAllUnprocessed() + throws ArchivaDatabaseException + { + List unprocessedArtifacts = dao.getArtifactDAO().queryArtifacts( new ArtifactsProcessedConstraint( false ) ); + + beginConsumerLifecycle( this.activeUnprocessedConsumers ); + + try + { + // Process each consumer. + Iterator it = unprocessedArtifacts.iterator(); + while ( it.hasNext() ) + { + ArchivaArtifact artifact = (ArchivaArtifact) it.next(); + + if ( !artifact.getModel().isProcessed() ) + { + updateUnprocessed( artifact ); + } + } + } + finally + { + consumerConsumerLifecycle( this.activeUnprocessedConsumers ); + } + } + + public void updateAllProcessed() + throws ArchivaDatabaseException + { + List processedArtifacts = dao.getArtifactDAO().queryArtifacts( new ArtifactsProcessedConstraint( true ) ); + + beginConsumerLifecycle( this.activeProcessedConsumers ); + + try + { + // Process each consumer. + Iterator it = processedArtifacts.iterator(); + while ( it.hasNext() ) + { + ArchivaArtifact artifact = (ArchivaArtifact) it.next(); + + if ( !artifact.getModel().isProcessed() ) + { + updateProcessed( artifact ); + } + } + } + finally + { + consumerConsumerLifecycle( this.activeProcessedConsumers ); + } + } + + private void consumerConsumerLifecycle( List consumers ) + { + Iterator it = consumers.iterator(); + while ( it.hasNext() ) + { + ArchivaArtifactConsumer consumer = (ArchivaArtifactConsumer) it.next(); + consumer.completeScan(); + } + } + + private void beginConsumerLifecycle( List consumers ) + { + Iterator it = consumers.iterator(); + while ( it.hasNext() ) + { + ArchivaArtifactConsumer consumer = (ArchivaArtifactConsumer) it.next(); + consumer.beginScan(); + } + } + + public void updateUnprocessed( ArchivaArtifact artifact ) + throws ArchivaDatabaseException + { + Iterator it = this.activeUnprocessedConsumers.iterator(); + while ( it.hasNext() ) + { + ArchivaArtifactConsumer consumer = (ArchivaArtifactConsumer) it.next(); + try + { + consumer.processArchivaArtifact( artifact ); + } + catch ( ConsumerException e ) + { + getLogger().warn( "Unable to consume (unprocessed) artifact: " + artifact ); + } + } + + artifact.getModel().setWhenProcessed( new Date() ); + dao.getArtifactDAO().saveArtifact( artifact ); + } + + public void updateProcessed( ArchivaArtifact artifact ) + throws ArchivaDatabaseException + { + Iterator it = this.activeProcessedConsumers.iterator(); + while ( it.hasNext() ) + { + ArchivaArtifactConsumer consumer = (ArchivaArtifactConsumer) it.next(); + try + { + consumer.processArchivaArtifact( artifact ); + } + catch ( ConsumerException e ) + { + getLogger().warn( "Unable to consume (processed) artifact: " + artifact ); + } + } + } + + private void updateActiveConsumers() + { + this.activeUnprocessedConsumers.clear(); + this.activeProcessedConsumers.clear(); + + DatabaseScanningConfiguration dbScanning = configuration.getConfiguration().getDatabaseScanning(); + if ( dbScanning == null ) + { + getLogger().error( "No Database Consumers found!" ); + return; + } + + this.activeUnprocessedConsumers.addAll( getActiveConsumerList( dbScanning.getUnprocessedConsumers() ) ); + this.activeProcessedConsumers.addAll( getActiveConsumerList( dbScanning.getProcessedConsumers() ) ); + } + + private List getActiveConsumerList( List potentialConsumerList ) + { + if ( ( potentialConsumerList == null ) || ( potentialConsumerList.isEmpty() ) ) + { + return Collections.EMPTY_LIST; + } + + List ret = new ArrayList(); + + Iterator it = potentialConsumerList.iterator(); + while ( it.hasNext() ) + { + String consumerName = (String) it.next(); + if ( !availableConsumers.containsKey( consumerName ) ) + { + getLogger().warn( "Requested Consumer [" + consumerName + "] does not exist. Disabling." ); + continue; + } + + ret.add( consumerName ); + } + + return ret; + } + + public void initialize() + throws InitializationException + { + propertyNameTriggers = new ArrayList(); + propertyNameTriggers.add( "databaseScanning" ); + propertyNameTriggers.add( "unprocessedConsumers" ); + propertyNameTriggers.add( "unprocessedConsumer" ); + propertyNameTriggers.add( "processedConsumers" ); + propertyNameTriggers.add( "processedConsumer" ); + + configuration.addChangeListener( this ); + updateActiveConsumers(); + } + + public void afterConfigurationChange( Registry registry, String propertyName, Object propertyValue ) + { + if ( propertyNameTriggers.contains( propertyName ) ) + { + updateActiveConsumers(); + } + } + + public void beforeConfigurationChange( Registry registry, String propertyName, Object propertyValue ) + { + /* nothing to do here */ + } +} diff --git a/archiva-database/src/test/java/org/apache/maven/archiva/database/AbstractArchivaDatabaseTestCase.java b/archiva-database/src/test/java/org/apache/maven/archiva/database/AbstractArchivaDatabaseTestCase.java index 6033a6924..4deedfc9e 100644 --- a/archiva-database/src/test/java/org/apache/maven/archiva/database/AbstractArchivaDatabaseTestCase.java +++ b/archiva-database/src/test/java/org/apache/maven/archiva/database/AbstractArchivaDatabaseTestCase.java @@ -20,9 +20,20 @@ package org.apache.maven.archiva.database; */ import org.codehaus.plexus.PlexusTestCase; -import org.codehaus.plexus.util.FileUtils; +import org.codehaus.plexus.jdo.DefaultConfigurableJdoFactory; +import org.codehaus.plexus.jdo.JdoFactory; +import org.jpox.SchemaTool; import java.io.File; +import java.net.URL; +import java.text.SimpleDateFormat; +import java.util.Date; +import java.util.Iterator; +import java.util.Map; +import java.util.Properties; + +import javax.jdo.PersistenceManager; +import javax.jdo.PersistenceManagerFactory; /** * AbstractArchivaDatabaseTestCase @@ -33,15 +44,100 @@ import java.io.File; public class AbstractArchivaDatabaseTestCase extends PlexusTestCase { + private static final String TIMESTAMP = "yyyy/MM/dd HH:mm:ss"; + + protected ArchivaDAO dao; + protected void setUp() throws Exception { - File derbyDbDir = new File( "target/plexus-home/testdb" ); - if ( derbyDbDir.exists() ) + super.setUp(); + + DefaultConfigurableJdoFactory jdoFactory = (DefaultConfigurableJdoFactory) lookup( JdoFactory.ROLE, "archiva" ); + assertEquals( DefaultConfigurableJdoFactory.class.getName(), jdoFactory.getClass().getName() ); + + jdoFactory.setPersistenceManagerFactoryClass( "org.jpox.PersistenceManagerFactoryImpl" ); + + /* derby version + File derbyDbDir = new File( "target/plexus-home/testdb" ); + if ( derbyDbDir.exists() ) + { + FileUtils.deleteDirectory( derbyDbDir ); + } + + jdoFactory.setDriverName( System.getProperty( "jdo.test.driver", "org.apache.derby.jdbc.EmbeddedDriver" ) ); + jdoFactory.setUrl( System.getProperty( "jdo.test.url", "jdbc:derby:" + derbyDbDir.getAbsolutePath() + ";create=true" ) ); + */ + + jdoFactory.setDriverName( System.getProperty( "jdo.test.driver", "org.hsqldb.jdbcDriver" ) ); + jdoFactory.setUrl( System.getProperty( "jdo.test.url", "jdbc:hsqldb:mem:" + getName() ) ); + + jdoFactory.setUserName( System.getProperty( "jdo.test.user", "sa" ) ); + + jdoFactory.setPassword( System.getProperty( "jdo.test.pass", "" ) ); + + jdoFactory.setProperty( "org.jpox.transactionIsolation", "READ_COMMITTED" ); + + jdoFactory.setProperty( "org.jpox.poid.transactionIsolation", "READ_COMMITTED" ); + + jdoFactory.setProperty( "org.jpox.autoCreateSchema", "true" ); + + jdoFactory.setProperty( "javax.jdo.option.RetainValues", "true" ); + + jdoFactory.setProperty( "javax.jdo.option.RestoreValues", "true" ); + + // jdoFactory.setProperty( "org.jpox.autoCreateColumns", "true" ); + + jdoFactory.setProperty( "org.jpox.validateTables", "true" ); + + jdoFactory.setProperty( "org.jpox.validateColumns", "true" ); + + jdoFactory.setProperty( "org.jpox.validateConstraints", "true" ); + + Properties properties = jdoFactory.getProperties(); + + for ( Iterator it = properties.entrySet().iterator(); it.hasNext(); ) { - FileUtils.deleteDirectory( derbyDbDir ); + Map.Entry entry = (Map.Entry) it.next(); + + System.setProperty( (String) entry.getKey(), (String) entry.getValue() ); } - super.setUp(); + URL jdoFileUrls[] = new URL[] { getClass().getResource( "/org/apache/maven/archiva/model/package.jdo" ) }; + + if ( ( jdoFileUrls == null ) || ( jdoFileUrls[0] == null ) ) + { + fail( "Unable to process test " + getName() + " - missing package.jdo." ); + } + + File propsFile = null; // intentional + boolean verbose = true; + + SchemaTool.deleteSchemaTables( jdoFileUrls, new URL[] {}, propsFile, verbose ); + SchemaTool.createSchemaTables( jdoFileUrls, new URL[] {}, propsFile, verbose, null ); + + PersistenceManagerFactory pmf = jdoFactory.getPersistenceManagerFactory(); + + assertNotNull( pmf ); + + PersistenceManager pm = pmf.getPersistenceManager(); + + pm.close(); + + this.dao = (ArchivaDAO) lookup( ArchivaDAO.class.getName(), "jdo" ); + } + + protected Date toDate( String txt ) + throws Exception + { + SimpleDateFormat sdf = new SimpleDateFormat( TIMESTAMP ); + return sdf.parse( txt ); + } + + protected String fromDate( Date date ) + throws Exception + { + SimpleDateFormat sdf = new SimpleDateFormat( TIMESTAMP ); + return sdf.format( date ); } } diff --git a/archiva-database/src/test/java/org/apache/maven/archiva/database/RepositoryMetadataDatabaseTest.java b/archiva-database/src/test/java/org/apache/maven/archiva/database/RepositoryMetadataDatabaseTest.java deleted file mode 100644 index 070e54958..000000000 --- a/archiva-database/src/test/java/org/apache/maven/archiva/database/RepositoryMetadataDatabaseTest.java +++ /dev/null @@ -1,87 +0,0 @@ -package org.apache.maven.archiva.database; - -/* - * 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.key.MetadataKey; -import org.apache.maven.artifact.repository.metadata.Metadata; -import org.codehaus.plexus.PlexusTestCase; -import org.codehaus.plexus.ibatis.PlexusIbatisHelper; - -/** - * RepositoryMetadataDatabaseTest - * - * @author <a href="mailto:joakim@erdfelt.com">Joakim Erdfelt</a> - * @version $Id$ - */ -public class RepositoryMetadataDatabaseTest - extends PlexusTestCase -{ - /** - * @plexus.requirement - */ - protected PlexusIbatisHelper ibatisHelper; - - protected void setUp() - throws Exception - { - super.setUp(); - } - - public void testRepositoryMetadataCreationAndDeletion() throws Exception - { - RepositoryMetadataDatabase db = (RepositoryMetadataDatabase) lookup( "org.apache.maven.archiva.database.RepositoryMetadataDatabase", "default" ); - - assertNotNull( db ); - assertTrue( db.tableExists( "RepositoryMetadata" ) ); - assertTrue( db.tableExists( "MetadataKeys" ) ); - - db.dropTable( "RepositoryMetadata" ); - db.dropTable( "MetadataKeys" ); - - assertFalse( db.tableExists( "RepositoryMetadata" ) ); - assertFalse( db.tableExists( "MetadataKeys" ) ); - } - - public void testMetadataKeyRetrieval() throws Exception - { - RepositoryMetadataDatabase db = (RepositoryMetadataDatabase) lookup( "org.apache.maven.archiva.database.RepositoryMetadataDatabase", "default" ); - - Metadata metadata = new Metadata(); - metadata.setArtifactId( "testArtifactId" ); - metadata.setGroupId( "testGroupId" ); - metadata.setVersion( "testVersion" ); - - MetadataKey metadataKey = db.getMetadataKey( metadata ); - - assertTrue( metadataKey.getMetadataKey() > 0 ); - assertEquals( metadataKey.getArtifactId(), metadata.getArtifactId() ); - assertEquals( metadataKey.getGroupId(), metadata.getGroupId() ); - assertEquals( metadataKey.getVersion(), metadata.getVersion() ); - - db.dropTable( "RepositoryMetadata" ); - db.dropTable( "MetadataKeys" ); - - assertFalse( db.tableExists( "RepositoryMetadata" ) ); - assertFalse( db.tableExists( "MetadataKeys" ) ); - - } - - -} diff --git a/archiva-database/src/test/java/org/apache/maven/archiva/database/artifact/ArtifactPersistenceTest.java b/archiva-database/src/test/java/org/apache/maven/archiva/database/artifact/ArtifactPersistenceTest.java deleted file mode 100644 index 007e027b2..000000000 --- a/archiva-database/src/test/java/org/apache/maven/archiva/database/artifact/ArtifactPersistenceTest.java +++ /dev/null @@ -1,70 +0,0 @@ -package org.apache.maven.archiva.database.artifact; - -/* - * 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.ArchivaDatabaseException; -import org.apache.maven.artifact.Artifact; -import org.apache.maven.artifact.factory.ArtifactFactory; - -/** - * ArtifactPersistenceTest - * - * @author <a href="mailto:joakim@erdfelt.com">Joakim Erdfelt</a> - * @version $Id$ - */ -public class ArtifactPersistenceTest - extends AbstractArchivaDatabaseTestCase -{ - private ArtifactFactory artifactFactory; - - private ArtifactPersistence db; - - protected void setUp() - throws Exception - { - super.setUp(); - - artifactFactory = (ArtifactFactory) lookup( ArtifactFactory.ROLE ); - db = (ArtifactPersistence) lookup( ArtifactPersistence.class.getName() ); - } - - public void testLookup() - { - assertNotNull( db ); - } - - public void testAddArtifact() throws ArchivaDatabaseException - { - String groupId = "org.apache.maven.archiva"; - String artifactId = "archiva-test-artifact"; - String version = "1.0"; - - Artifact artifact = artifactFactory - .createArtifact( groupId, artifactId, version, Artifact.SCOPE_COMPILE, "jar" ); - - db.create( artifact ); - - Artifact fetched = db.read( groupId, artifactId, version ); - - assertNotNull( "Should have fetched an Artifact.", fetched ); - assertEquals( "Should have fetched the expected Artifact.", artifact, fetched ); - } -} diff --git a/archiva-database/src/test/java/org/apache/maven/archiva/database/browsing/RepositoryBrowsingTest.java b/archiva-database/src/test/java/org/apache/maven/archiva/database/browsing/RepositoryBrowsingTest.java new file mode 100644 index 000000000..11d62cd48 --- /dev/null +++ b/archiva-database/src/test/java/org/apache/maven/archiva/database/browsing/RepositoryBrowsingTest.java @@ -0,0 +1,115 @@ +package org.apache.maven.archiva.database.browsing; + +/* + * 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.ArchivaDAO; +import org.apache.maven.archiva.database.ArtifactDAO; +import org.apache.maven.archiva.model.ArchivaArtifact; + +import java.util.Date; + +/** + * RepositoryBrowsingTest + * + * @author <a href="mailto:joakim@erdfelt.com">Joakim Erdfelt</a> + * @version $Id$ + */ +public class RepositoryBrowsingTest + extends AbstractArchivaDatabaseTestCase +{ + private ArtifactDAO artifactDao; + + protected void setUp() + throws Exception + { + super.setUp(); + + ArchivaDAO dao = (ArchivaDAO) lookup( ArchivaDAO.ROLE, "jdo" ); + artifactDao = dao.getArtifactDAO(); + } + + public ArchivaArtifact createArtifact( String groupId, String artifactId, String version ) + { + ArchivaArtifact artifact = artifactDao.createArtifact( groupId, artifactId, version, "", "jar" ); + artifact.getModel().setLastModified( new Date() ); // mandatory field. + artifact.getModel().setRepositoryId( "testable_repo" ); + return artifact; + } + + public void saveTestData() + throws Exception + { + ArchivaArtifact artifact; + + // Setup artifacts in fresh DB. + artifact = createArtifact( "commons-lang", "commons-lang", "2.0" ); + artifactDao.saveArtifact( artifact ); + + artifact = createArtifact( "commons-lang", "commons-lang", "2.1" ); + artifactDao.saveArtifact( artifact ); + + artifact = createArtifact( "org.apache.maven.test", "test-one", "1.2" ); + artifactDao.saveArtifact( artifact ); + + artifact = createArtifact( "org.apache.maven.test.foo", "test-two", "1.0" ); + artifactDao.saveArtifact( artifact ); + + artifact = createArtifact( "org.apache.maven.shared", "test-two", "2.0" ); + artifactDao.saveArtifact( artifact ); + + artifact = createArtifact( "org.apache.maven.shared", "test-two", "2.1-SNAPSHOT" ); + artifactDao.saveArtifact( artifact ); + + artifact = createArtifact( "org.apache.maven.shared", "test-two", "2.1.1" ); + artifactDao.saveArtifact( artifact ); + + artifact = createArtifact( "org.apache.maven.shared", "test-two", "2.1-alpha-1" ); + artifactDao.saveArtifact( artifact ); + + artifact = createArtifact( "org.apache.maven.shared", "test-bar", "2.1" ); + artifactDao.saveArtifact( artifact ); + + artifact = createArtifact( "org.codehaus.modello", "modellong", "3.0" ); + artifactDao.saveArtifact( artifact ); + } + + public RepositoryBrowsing lookupBrowser() + throws Exception + { + RepositoryBrowsing browser = (RepositoryBrowsing) lookup( RepositoryBrowsing.class.getName(), "default" ); + assertNotNull( "RepositoryBrowsing should not be null.", browser ); + return browser; + } + + public void testSimpleBrowse() + throws Exception + { + saveTestData(); + + RepositoryBrowsing browser = lookupBrowser(); + BrowsingResults results = browser.getRoot(); + assertNotNull( "Browsing Results should not be null.", results ); + + String expectedRootGroupIds[] = new String[] { "commons-lang", "org" }; + + assertEquals( "Browsing Results: groupIds on root.", expectedRootGroupIds.length, results.getGroupIds().size() ); + } +} diff --git a/archiva-database/src/test/java/org/apache/maven/archiva/database/constraints/AllTests.java b/archiva-database/src/test/java/org/apache/maven/archiva/database/constraints/AllTests.java new file mode 100644 index 000000000..c7fc265bc --- /dev/null +++ b/archiva-database/src/test/java/org/apache/maven/archiva/database/constraints/AllTests.java @@ -0,0 +1,48 @@ +package org.apache.maven.archiva.database.constraints; + +/* + * 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 junit.framework.Test; +import junit.framework.TestSuite; + +/** + * IDE Provided Utility Class for all tests. + * + * @author <a href="mailto:joakim@erdfelt.com">Joakim Erdfelt</a> + * @version $Id$ + */ +public class AllTests +{ + + public static Test suite() + { + TestSuite suite = new TestSuite( "Test for org.apache.maven.archiva.database.constraints" ); + //$JUnit-BEGIN$ + suite.addTestSuite( ArtifactsProcessedConstraintTest.class ); + suite.addTestSuite( ArtifactsBySha1ChecksumConstraintTest.class ); + suite.addTestSuite( OlderArtifactsByAgeConstraintTest.class ); + suite.addTestSuite( UniqueGroupIdConstraintTest.class ); + suite.addTestSuite( OlderSnapshotArtifactsByAgeConstraintTest.class ); + suite.addTestSuite( RecentArtifactsByAgeConstraintTest.class ); + //$JUnit-END$ + return suite; + } + +} diff --git a/archiva-database/src/test/java/org/apache/maven/archiva/database/constraints/ArtifactsBySha1ChecksumConstraintTest.java b/archiva-database/src/test/java/org/apache/maven/archiva/database/constraints/ArtifactsBySha1ChecksumConstraintTest.java new file mode 100644 index 000000000..f958384ae --- /dev/null +++ b/archiva-database/src/test/java/org/apache/maven/archiva/database/constraints/ArtifactsBySha1ChecksumConstraintTest.java @@ -0,0 +1,112 @@ +package org.apache.maven.archiva.database.constraints; + +/* + * 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.ArchivaDAO; +import org.apache.maven.archiva.database.ArtifactDAO; +import org.apache.maven.archiva.model.ArchivaArtifact; + +import java.util.Date; +import java.util.List; + +/** + * ArtifactsBySha1ChecksumConstraintTest + * + * @author <a href="mailto:joakim@erdfelt.com">Joakim Erdfelt</a> + * @version $Id$ + */ +public class ArtifactsBySha1ChecksumConstraintTest + extends AbstractArchivaDatabaseTestCase +{ + private static final String HASH3 = "f3f653289f3217c65324830ab3415bc92feddefa"; + + private static final String HASH2 = "a49810ad3eba8651677ab57cd40a0f76fdef9538"; + + private static final String HASH1 = "232f01b24b1617c46a3d4b0ab3415bc9237dcdec"; + + private ArtifactDAO artifactDao; + + protected void setUp() + throws Exception + { + super.setUp(); + + ArchivaDAO dao = (ArchivaDAO) lookup( ArchivaDAO.ROLE, "jdo" ); + artifactDao = dao.getArtifactDAO(); + } + + public ArchivaArtifact createArtifact( String artifactId, String version ) + { + ArchivaArtifact artifact = artifactDao.createArtifact( "org.apache.maven.archiva.test", artifactId, version, + "", "jar" ); + artifact.getModel().setLastModified( new Date() ); + artifact.getModel().setRepositoryId( "testable_repo" ); + return artifact; + } + + public void testConstraint() + throws Exception + { + ArchivaArtifact artifact; + + // Setup artifacts in fresh DB. + artifact = createArtifact( "test-one", "1.0" ); + artifact.getModel().setChecksumSHA1( HASH1 ); + artifactDao.saveArtifact( artifact ); + + artifact = createArtifact( "test-one", "1.1" ); + artifact.getModel().setChecksumSHA1( HASH1 ); + artifactDao.saveArtifact( artifact ); + + artifact = createArtifact( "test-one", "1.2" ); + artifact.getModel().setChecksumSHA1( HASH1 ); + artifactDao.saveArtifact( artifact ); + + artifact = createArtifact( "test-two", "1.0" ); + artifact.getModel().setChecksumSHA1( HASH1 ); + artifactDao.saveArtifact( artifact ); + + artifact = createArtifact( "test-two", "2.0" ); + artifact.getModel().setChecksumSHA1( HASH3 ); + artifactDao.saveArtifact( artifact ); + + artifact = createArtifact( "test-two", "2.1" ); + artifact.getModel().setChecksumSHA1( HASH2 ); + artifactDao.saveArtifact( artifact ); + + artifact = createArtifact( "test-two", "3.0" ); + artifact.getModel().setChecksumSHA1( HASH2 ); + artifactDao.saveArtifact( artifact ); + + assertConstraint( "Artifacts by SHA1 Checksum", 4, new ArtifactsBySha1ChecksumConstraint( HASH1 ) ); + assertConstraint( "Artifacts by SHA1 Checksum", 2, new ArtifactsBySha1ChecksumConstraint( HASH2 ) ); + assertConstraint( "Artifacts by SHA1 Checksum", 1, new ArtifactsBySha1ChecksumConstraint( HASH3 ) ); + } + + private void assertConstraint( String msg, int count, ArtifactsBySha1ChecksumConstraint constraint ) + throws Exception + { + List results = artifactDao.queryArtifacts( constraint ); + assertNotNull( msg + ": Not Null", results ); + assertEquals( msg + ": Results.size", count, results.size() ); + } + +} diff --git a/archiva-database/src/test/java/org/apache/maven/archiva/database/constraints/ArtifactsProcessedConstraintTest.java b/archiva-database/src/test/java/org/apache/maven/archiva/database/constraints/ArtifactsProcessedConstraintTest.java new file mode 100644 index 000000000..a76d9689b --- /dev/null +++ b/archiva-database/src/test/java/org/apache/maven/archiva/database/constraints/ArtifactsProcessedConstraintTest.java @@ -0,0 +1,136 @@ +package org.apache.maven.archiva.database.constraints; + +/* + * 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.model.ArchivaArtifact; + +import java.util.Date; +import java.util.Iterator; +import java.util.List; + +/** + * ArtifactsProcessedConstraintTest + * + * @author <a href="mailto:joakim@erdfelt.com">Joakim Erdfelt</a> + * @version $Id$ + */ +public class ArtifactsProcessedConstraintTest + extends AbstractArchivaDatabaseTestCase +{ + public ArchivaArtifact createArtifact( String groupId, String artifactId, String version, String whenProcessed ) + throws Exception + { + ArchivaArtifact artifact = dao.getArtifactDAO().createArtifact( groupId, artifactId, version, "", "jar" ); + assertNotNull( "Artifact should not be null.", artifact ); + Date dateWhenProcessed = null; + + if ( whenProcessed != null ) + { + dateWhenProcessed = toDate( whenProcessed ); + } + + artifact.getModel().setWhenProcessed( dateWhenProcessed ); + + // Satisfy table / column requirements. + artifact.getModel().setLastModified( new Date() ); + + return artifact; + } + + public void assertResults( String type, List results, String expectedArtifacts[] ) + { + assertNotNull( "Results[" + type + "] should not be null.", results ); + assertEquals( "Results[" + type + "].size", expectedArtifacts.length, results.size() ); + + for ( int i = 0; i < expectedArtifacts.length; i++ ) + { + String artifactId = expectedArtifacts[i]; + + int found = 0; + Iterator it = results.iterator(); + while ( it.hasNext() ) + { + ArchivaArtifact artifact = (ArchivaArtifact) it.next(); + if ( artifactId.equals( artifact.getArtifactId() ) ) + { + found++; + } + } + + if ( found <= 0 ) + { + fail( "Results[" + type + "] - Did not find expected artifact ID [" + artifactId + "]" ); + } + + if ( found > 1 ) + { + fail( "Results[" + type + "] - Expected to find 1 copy of artifact ID [" + artifactId + + "], yet found <" + found + "> instead." ); + } + } + } + + protected void setUp() + throws Exception + { + super.setUp(); + + ArtifactDAO adao = dao.getArtifactDAO(); + assertNotNull( "Artifact DAO should not be null.", adao ); + + adao.saveArtifact( createArtifact( "org.apache.maven.archiva", "archiva-common", "1.0-SNAPSHOT", null ) ); + adao.saveArtifact( createArtifact( "org.apache.maven.archiva", "archiva-utils", "1.0-SNAPSHOT", + "2006/08/22 19:01:00" ) ); + adao.saveArtifact( createArtifact( "org.apache.maven.archiva", "archiva-old", "0.1", "2004/02/15 9:01:00" ) ); + adao.saveArtifact( createArtifact( "org.apache.maven.archiva", "archiva-database", "1.0-SNAPSHOT", null ) ); + } + + public void testNotProcessed() + throws Exception + { + List results = dao.getArtifactDAO().queryArtifacts( new ArtifactsProcessedConstraint( false ) ); + assertResults( "not-processed", results, new String[] { "archiva-common", "archiva-database" } ); + } + + public void testProcessed() + throws Exception + { + List results = dao.getArtifactDAO().queryArtifacts( new ArtifactsProcessedConstraint( true ) ); + assertResults( "processed", results, new String[] { "archiva-utils", "archiva-old" } ); + } + + public void testSinceRecent() + throws Exception + { + Date since = toDate( "2006/01/01 12:00:00" ); + List results = dao.getArtifactDAO().queryArtifacts( new ArtifactsProcessedConstraint( since ) ); + assertResults( "processed", results, new String[] { "archiva-utils" } ); + } + + public void testSinceOld() + throws Exception + { + Date since = toDate( "2001/01/01 12:00:00" ); + List results = dao.getArtifactDAO().queryArtifacts( new ArtifactsProcessedConstraint( since ) ); + assertResults( "processed", results, new String[] { "archiva-utils", "archiva-old" } ); + } +} diff --git a/archiva-database/src/test/java/org/apache/maven/archiva/database/constraints/MostRecentRepositoryScanStatisticsTest.java b/archiva-database/src/test/java/org/apache/maven/archiva/database/constraints/MostRecentRepositoryScanStatisticsTest.java new file mode 100644 index 000000000..3ef84e8d2 --- /dev/null +++ b/archiva-database/src/test/java/org/apache/maven/archiva/database/constraints/MostRecentRepositoryScanStatisticsTest.java @@ -0,0 +1,86 @@ +package org.apache.maven.archiva.database.constraints; + +/* + * 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.model.RepositoryContentStatistics; + +import java.util.List; + +/** + * MostRecentRepositoryScanStatisticsTest + * + * @author <a href="mailto:joakim@erdfelt.com">Joakim Erdfelt</a> + * @version $Id$ + */ +public class MostRecentRepositoryScanStatisticsTest + extends AbstractArchivaDatabaseTestCase +{ + private RepositoryContentStatistics createStats( String repoId, String timestamp, long duration, long totalfiles, + long newfiles ) + throws Exception + { + RepositoryContentStatistics stats = new RepositoryContentStatistics(); + stats.setRepositoryId( repoId ); + stats.setDuration( duration ); + stats.setNewFileCount( newfiles ); + stats.setTotalFileCount( totalfiles ); + stats.setWhenGathered( toDate( timestamp ) ); + + return stats; + } + + protected void setUp() + throws Exception + { + super.setUp(); + + dao.save( createStats( "internal", "2007/02/21 10:00:00", 20000, 12000, 400 ) ); + dao.save( createStats( "internal", "2007/02/20 10:00:00", 20000, 11800, 0 ) ); + dao.save( createStats( "internal", "2007/02/19 10:00:00", 20000, 11800, 100 ) ); + dao.save( createStats( "internal", "2007/02/18 10:00:00", 20000, 11700, 320 ) ); + } + + public void testNotProcessedYet() + throws Exception + { + List results = dao.query( new MostRecentRepositoryScanStatistics( "central" ) ); + assertNotNull( "Not Processed Yet", results ); + assertTrue( "Not Processed Yet", results.isEmpty() ); + } + + public void testStats() + throws Exception + { + List results = dao.query( new MostRecentRepositoryScanStatistics( "internal" ) ); + assertNotNull( "Stats: results (not null)", results ); + assertEquals( "Stats: results.size", 1, results.size() ); + + Object o = results.get( 0 ); + assertTrue( "Stats: result[0] instanceof RepositoryScanStatistics", o instanceof RepositoryContentStatistics ); + RepositoryContentStatistics stats = (RepositoryContentStatistics) o; + assertEquals( "Stats: id", "internal", stats.getRepositoryId() ); + assertEquals( "Stats: when gathered", "2007/02/21 10:00:00", fromDate( stats.getWhenGathered() ) ); + assertEquals( "Stats: duration", 20000, stats.getDuration() ); + assertEquals( "Stats: total file count", 12000, stats.getTotalFileCount() ); + assertEquals( "Stats: new file count", 400, stats.getNewFileCount() ); + } + +} diff --git a/archiva-database/src/test/java/org/apache/maven/archiva/database/constraints/OlderArtifactsByAgeConstraintTest.java b/archiva-database/src/test/java/org/apache/maven/archiva/database/constraints/OlderArtifactsByAgeConstraintTest.java new file mode 100644 index 000000000..a306b7ba4 --- /dev/null +++ b/archiva-database/src/test/java/org/apache/maven/archiva/database/constraints/OlderArtifactsByAgeConstraintTest.java @@ -0,0 +1,103 @@ +package org.apache.maven.archiva.database.constraints; + +/* + * 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.ArchivaDAO; +import org.apache.maven.archiva.database.ArtifactDAO; +import org.apache.maven.archiva.database.Constraint; +import org.apache.maven.archiva.model.ArchivaArtifact; + +import java.util.Calendar; +import java.util.List; + +/** + * OlderArtifactsByAgeConstraintTest + * + * @author <a href="mailto:joakim@erdfelt.com">Joakim Erdfelt</a> + * @version $Id$ + */ +public class OlderArtifactsByAgeConstraintTest + extends AbstractArchivaDatabaseTestCase +{ + private ArtifactDAO artifactDao; + + protected void setUp() + throws Exception + { + super.setUp(); + + ArchivaDAO dao = (ArchivaDAO) lookup( ArchivaDAO.ROLE, "jdo" ); + artifactDao = dao.getArtifactDAO(); + } + + public ArchivaArtifact createArtifact( String artifactId, String version, int daysOld ) + { + ArchivaArtifact artifact = artifactDao.createArtifact( "org.apache.maven.archiva.test", artifactId, version, + "", "jar" ); + Calendar cal = Calendar.getInstance(); + cal.add( Calendar.DAY_OF_MONTH, ( -1 ) * daysOld ); + artifact.getModel().setLastModified( cal.getTime() ); + artifact.getModel().setRepositoryId( "testable_repo" ); + return artifact; + } + + public void testConstraint() + throws Exception + { + ArchivaArtifact artifact; + + // Setup artifacts in fresh DB. + artifact = createArtifact( "test-one", "1.0", 200 ); + artifactDao.saveArtifact( artifact ); + + artifact = createArtifact( "test-one", "1.1", 100 ); + artifactDao.saveArtifact( artifact ); + + artifact = createArtifact( "test-one", "1.2", 50 ); + artifactDao.saveArtifact( artifact ); + + artifact = createArtifact( "test-two", "1.0", 200 ); + artifactDao.saveArtifact( artifact ); + + artifact = createArtifact( "test-two", "2.0", 150 ); + artifactDao.saveArtifact( artifact ); + + artifact = createArtifact( "test-two", "2.1", 100 ); + artifactDao.saveArtifact( artifact ); + + artifact = createArtifact( "test-two", "3.0", 5 ); + artifactDao.saveArtifact( artifact ); + + assertConstraint( 6, new OlderArtifactsByAgeConstraint( 7 ) ); + assertConstraint( 5, new OlderArtifactsByAgeConstraint( 90 ) ); + assertConstraint( 5, new OlderArtifactsByAgeConstraint( 100 ) ); + assertConstraint( 3, new OlderArtifactsByAgeConstraint( 150 ) ); + assertConstraint( 0, new OlderArtifactsByAgeConstraint( 9000 ) ); + } + + private void assertConstraint( int expectedHits, Constraint constraint ) + throws Exception + { + List results = artifactDao.queryArtifacts( constraint ); + assertNotNull( "Older Artifacts By Age: Not Null", results ); + assertEquals( "Older Artifacts By Age: Results.size", expectedHits, results.size() ); + } +} diff --git a/archiva-database/src/test/java/org/apache/maven/archiva/database/constraints/OlderSnapshotArtifactsByAgeConstraintTest.java b/archiva-database/src/test/java/org/apache/maven/archiva/database/constraints/OlderSnapshotArtifactsByAgeConstraintTest.java new file mode 100644 index 000000000..88838ea72 --- /dev/null +++ b/archiva-database/src/test/java/org/apache/maven/archiva/database/constraints/OlderSnapshotArtifactsByAgeConstraintTest.java @@ -0,0 +1,118 @@ +package org.apache.maven.archiva.database.constraints; + +/* + * 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.ArchivaDAO; +import org.apache.maven.archiva.database.ArtifactDAO; +import org.apache.maven.archiva.database.Constraint; +import org.apache.maven.archiva.model.ArchivaArtifact; + +import java.util.Calendar; +import java.util.List; + +/** + * OlderArtifactsByAgeConstraintTest + * + * @author <a href="mailto:joakim@erdfelt.com">Joakim Erdfelt</a> + * @version $Id$ + */ +public class OlderSnapshotArtifactsByAgeConstraintTest + extends AbstractArchivaDatabaseTestCase +{ + private ArtifactDAO artifactDao; + + protected void setUp() + throws Exception + { + super.setUp(); + + ArchivaDAO dao = (ArchivaDAO) lookup( ArchivaDAO.ROLE, "jdo" ); + artifactDao = dao.getArtifactDAO(); + } + + public ArchivaArtifact createArtifact( String artifactId, String version, int daysOld ) + { + ArchivaArtifact artifact = artifactDao.createArtifact( "org.apache.maven.archiva.test", artifactId, version, + "", "jar" ); + Calendar cal = Calendar.getInstance(); + cal.add( Calendar.DAY_OF_MONTH, ( -1 ) * daysOld ); + artifact.getModel().setLastModified( cal.getTime() ); + artifact.getModel().setRepositoryId( "testable_repo" ); + return artifact; + } + + public void testConstraint() + throws Exception + { + ArchivaArtifact artifact; + + // Setup artifacts in fresh DB. + artifact = createArtifact( "test-one", "1.0", 200 ); + artifactDao.saveArtifact( artifact ); + + artifact = createArtifact( "test-one", "1.1-SNAPSHOT", 110 ); + artifactDao.saveArtifact( artifact ); + + artifact = createArtifact( "test-one", "1.1", 100 ); + artifactDao.saveArtifact( artifact ); + + artifact = createArtifact( "test-one", "1.2-20060923.005752-2", 55 ); + artifactDao.saveArtifact( artifact ); + + artifact = createArtifact( "test-one", "1.2-SNAPSHOT", 52 ); + artifactDao.saveArtifact( artifact ); + + artifact = createArtifact( "test-one", "1.2", 50 ); + artifactDao.saveArtifact( artifact ); + + artifact = createArtifact( "test-two", "1.0-20060828.144210-1", 220 ); + artifactDao.saveArtifact( artifact ); + + artifact = createArtifact( "test-two", "1.0-SNAPSHOT", 210 ); + artifactDao.saveArtifact( artifact ); + + artifact = createArtifact( "test-two", "1.0", 200 ); + artifactDao.saveArtifact( artifact ); + + artifact = createArtifact( "test-two", "2.0", 150 ); + artifactDao.saveArtifact( artifact ); + + artifact = createArtifact( "test-two", "2.1", 100 ); + artifactDao.saveArtifact( artifact ); + + artifact = createArtifact( "test-two", "3.0", 5 ); + artifactDao.saveArtifact( artifact ); + + assertConstraint( 5, new OlderSnapshotArtifactsByAgeConstraint( 7 ) ); + assertConstraint( 3, new OlderSnapshotArtifactsByAgeConstraint( 90 ) ); + assertConstraint( 3, new OlderSnapshotArtifactsByAgeConstraint( 100 ) ); + assertConstraint( 2, new OlderSnapshotArtifactsByAgeConstraint( 150 ) ); + assertConstraint( 0, new OlderSnapshotArtifactsByAgeConstraint( 500 ) ); + } + + private void assertConstraint( int expectedHits, Constraint constraint ) + throws Exception + { + List results = artifactDao.queryArtifacts( constraint ); + assertNotNull( "Older Snapshot Artifacts By Age: Not Null", results ); + assertEquals( "Older Snapshot Artifacts By Age: Results.size", expectedHits, results.size() ); + } +} diff --git a/archiva-database/src/test/java/org/apache/maven/archiva/database/constraints/RecentArtifactsByAgeConstraintTest.java b/archiva-database/src/test/java/org/apache/maven/archiva/database/constraints/RecentArtifactsByAgeConstraintTest.java new file mode 100644 index 000000000..4512264f3 --- /dev/null +++ b/archiva-database/src/test/java/org/apache/maven/archiva/database/constraints/RecentArtifactsByAgeConstraintTest.java @@ -0,0 +1,104 @@ +package org.apache.maven.archiva.database.constraints; + +/* + * 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.ArchivaDAO; +import org.apache.maven.archiva.database.ArtifactDAO; +import org.apache.maven.archiva.database.Constraint; +import org.apache.maven.archiva.model.ArchivaArtifact; + +import java.util.Calendar; +import java.util.List; + +/** + * RecentArtifactsByAgeConstraintTest + * + * @author <a href="mailto:joakim@erdfelt.com">Joakim Erdfelt</a> + * @version $Id$ + */ +public class RecentArtifactsByAgeConstraintTest + extends AbstractArchivaDatabaseTestCase +{ + private ArtifactDAO artifactDao; + + protected void setUp() + throws Exception + { + super.setUp(); + + ArchivaDAO dao = (ArchivaDAO) lookup( ArchivaDAO.ROLE, "jdo" ); + artifactDao = dao.getArtifactDAO(); + } + + public ArchivaArtifact createArtifact( String artifactId, String version, int daysOld ) + { + ArchivaArtifact artifact = artifactDao.createArtifact( "org.apache.maven.archiva.test", artifactId, version, + "", "jar" ); + Calendar cal = Calendar.getInstance(); + cal.add( Calendar.DAY_OF_MONTH, ( -1 ) * daysOld ); + artifact.getModel().setLastModified( cal.getTime() ); + artifact.getModel().setRepositoryId( "testable_repo" ); + return artifact; + } + + public void testConstraint() + throws Exception + { + ArchivaArtifact artifact; + + // Setup artifacts in fresh DB. + artifact = createArtifact( "test-one", "1.0", 200 ); + artifactDao.saveArtifact( artifact ); + + artifact = createArtifact( "test-one", "1.1", 100 ); + artifactDao.saveArtifact( artifact ); + + artifact = createArtifact( "test-one", "1.2", 50 ); + artifactDao.saveArtifact( artifact ); + + artifact = createArtifact( "test-two", "1.0", 200 ); + artifactDao.saveArtifact( artifact ); + + artifact = createArtifact( "test-two", "2.0", 150 ); + artifactDao.saveArtifact( artifact ); + + artifact = createArtifact( "test-two", "2.1", 100 ); + artifactDao.saveArtifact( artifact ); + + artifact = createArtifact( "test-two", "3.0", 5 ); + artifactDao.saveArtifact( artifact ); + + assertConstraint( 0, new RecentArtifactsByAgeConstraint( 2 ) ); + assertConstraint( 1, new RecentArtifactsByAgeConstraint( 7 ) ); + assertConstraint( 2, new RecentArtifactsByAgeConstraint( 90 ) ); + assertConstraint( 4, new RecentArtifactsByAgeConstraint( 100 ) ); + assertConstraint( 5, new RecentArtifactsByAgeConstraint( 150 ) ); + assertConstraint( 7, new RecentArtifactsByAgeConstraint( 9000 ) ); + } + + private void assertConstraint( int expectedHits, Constraint constraint ) + throws Exception + { + List results = artifactDao.queryArtifacts( constraint ); + assertNotNull( "Recent Artifacts By Age: Not Null", results ); + assertEquals( "Recent Artifacts By Age: Results.size", expectedHits, results.size() ); + } +} diff --git a/archiva-database/src/test/java/org/apache/maven/archiva/database/constraints/UniqueArtifactIdConstraintTest.java b/archiva-database/src/test/java/org/apache/maven/archiva/database/constraints/UniqueArtifactIdConstraintTest.java new file mode 100644 index 000000000..bd8a9a2c3 --- /dev/null +++ b/archiva-database/src/test/java/org/apache/maven/archiva/database/constraints/UniqueArtifactIdConstraintTest.java @@ -0,0 +1,119 @@ +package org.apache.maven.archiva.database.constraints; + +/* + * 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.commons.lang.StringUtils; +import org.apache.maven.archiva.database.AbstractArchivaDatabaseTestCase; +import org.apache.maven.archiva.database.ArchivaDAO; +import org.apache.maven.archiva.database.ArtifactDAO; +import org.apache.maven.archiva.database.SimpleConstraint; +import org.apache.maven.archiva.model.ArchivaArtifact; + +import java.util.Arrays; +import java.util.Date; +import java.util.Iterator; +import java.util.List; + +/** + * UniqueArtifactIdConstraintTest + * + * @author <a href="mailto:joakim@erdfelt.com">Joakim Erdfelt</a> + * @version $Id$ + */ +public class UniqueArtifactIdConstraintTest + extends AbstractArchivaDatabaseTestCase +{ + private ArtifactDAO artifactDao; + + protected void setUp() + throws Exception + { + super.setUp(); + + ArchivaDAO dao = (ArchivaDAO) lookup( ArchivaDAO.ROLE, "jdo" ); + artifactDao = dao.getArtifactDAO(); + } + + public ArchivaArtifact createArtifact( String groupId, String artifactId, String version ) + { + ArchivaArtifact artifact = artifactDao.createArtifact( groupId, artifactId, version, "", "jar" ); + artifact.getModel().setLastModified( new Date() ); // mandatory field. + artifact.getModel().setRepositoryId( "testable_repo" ); + return artifact; + } + + public void testConstraint() + throws Exception + { + ArchivaArtifact artifact; + + // Setup artifacts in fresh DB. + artifact = createArtifact( "commons-lang", "commons-lang", "2.0" ); + artifactDao.saveArtifact( artifact ); + + artifact = createArtifact( "commons-lang", "commons-lang", "2.1" ); + artifactDao.saveArtifact( artifact ); + + artifact = createArtifact( "org.apache.maven.test", "test-one", "1.2" ); + artifactDao.saveArtifact( artifact ); + + artifact = createArtifact( "org.apache.maven.test.foo", "test-two", "1.0" ); + artifactDao.saveArtifact( artifact ); + + artifact = createArtifact( "org.apache.maven.shared", "test-two", "2.0" ); + artifactDao.saveArtifact( artifact ); + + artifact = createArtifact( "org.apache.maven.shared", "test-two", "2.1" ); + artifactDao.saveArtifact( artifact ); + + artifact = createArtifact( "org.apache.maven.shared", "test-bar", "2.1" ); + artifactDao.saveArtifact( artifact ); + + artifact = createArtifact( "org.codehaus.modello", "modellong", "3.0" ); + artifactDao.saveArtifact( artifact ); + + assertConstraint( new String[] {}, new UniqueArtifactIdConstraint( "org.apache" ) ); + assertConstraint( new String[] { "commons-lang" }, new UniqueArtifactIdConstraint( "commons-lang" ) ); + assertConstraint( new String[] { "test-one" }, new UniqueArtifactIdConstraint( "org.apache.maven.test" ) ); + assertConstraint( new String[] { "test-two", "test-bar" }, + new UniqueArtifactIdConstraint( "org.apache.maven.shared" ) ); + assertConstraint( new String[] { "modellong" }, new UniqueArtifactIdConstraint( "org.codehaus.modello" ) ); + } + + private void assertConstraint( String[] artifactIds, SimpleConstraint constraint ) + { + String prefix = "Unique Artifact IDs: "; + + List results = dao.query( constraint ); + assertNotNull( prefix + "Not Null", results ); + assertEquals( prefix + "Results.size", artifactIds.length, results.size() ); + + List expectedArtifactIds = Arrays.asList( artifactIds ); + + Iterator it = results.iterator(); + while ( it.hasNext() ) + { + String actualArtifactId = (String) it.next(); + assertTrue( prefix + "artifactId result should not be blank.", StringUtils.isNotBlank( actualArtifactId ) ); + assertTrue( prefix + " artifactId result <" + actualArtifactId + "> exists in expected artifactIds.", + expectedArtifactIds.contains( actualArtifactId ) ); + } + } +} diff --git a/archiva-database/src/test/java/org/apache/maven/archiva/database/constraints/UniqueGroupIdConstraintTest.java b/archiva-database/src/test/java/org/apache/maven/archiva/database/constraints/UniqueGroupIdConstraintTest.java new file mode 100644 index 000000000..e032d236d --- /dev/null +++ b/archiva-database/src/test/java/org/apache/maven/archiva/database/constraints/UniqueGroupIdConstraintTest.java @@ -0,0 +1,132 @@ +package org.apache.maven.archiva.database.constraints; + +/* + * 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.commons.lang.StringUtils; +import org.apache.maven.archiva.database.AbstractArchivaDatabaseTestCase; +import org.apache.maven.archiva.database.ArchivaDAO; +import org.apache.maven.archiva.database.ArtifactDAO; +import org.apache.maven.archiva.database.SimpleConstraint; +import org.apache.maven.archiva.model.ArchivaArtifact; + +import java.util.Arrays; +import java.util.Date; +import java.util.Iterator; +import java.util.List; + +/** + * UniqueGroupIdConstraintTest + * + * @author <a href="mailto:joakim@erdfelt.com">Joakim Erdfelt</a> + * @version $Id$ + */ +public class UniqueGroupIdConstraintTest + extends AbstractArchivaDatabaseTestCase +{ + private ArtifactDAO artifactDao; + + protected void setUp() + throws Exception + { + super.setUp(); + + ArchivaDAO dao = (ArchivaDAO) lookup( ArchivaDAO.ROLE, "jdo" ); + artifactDao = dao.getArtifactDAO(); + } + + public ArchivaArtifact createArtifact( String groupId, String artifactId, String version ) + { + ArchivaArtifact artifact = artifactDao.createArtifact( groupId, artifactId, version, "", "jar" ); + artifact.getModel().setLastModified( new Date() ); // mandatory field. + artifact.getModel().setRepositoryId( "testable_repo" ); + return artifact; + } + + public void testConstraint() + throws Exception + { + ArchivaArtifact artifact; + + // Setup artifacts in fresh DB. + artifact = createArtifact( "commons-lang", "commons-lang", "2.0" ); + artifactDao.saveArtifact( artifact ); + + artifact = createArtifact( "commons-lang", "commons-lang", "2.1" ); + artifactDao.saveArtifact( artifact ); + + artifact = createArtifact( "org.apache.maven.test", "test-one", "1.2" ); + artifactDao.saveArtifact( artifact ); + + artifact = createArtifact( "org.apache.maven.test.foo", "test-two", "1.0" ); + artifactDao.saveArtifact( artifact ); + + artifact = createArtifact( "org.apache.maven.shared", "test-two", "2.0" ); + artifactDao.saveArtifact( artifact ); + + artifact = createArtifact( "org.apache.maven.shared", "test-two", "2.1" ); + artifactDao.saveArtifact( artifact ); + + artifact = createArtifact( "org.codehaus.modello", "test-two", "3.0" ); + artifactDao.saveArtifact( artifact ); + + assertConstraint( new String[] { + "commons-lang", + "org.apache.maven.test", + "org.apache.maven.test.foo", + "org.apache.maven.shared", + "org.codehaus.modello" }, new UniqueGroupIdConstraint() ); + assertConstraint( new String[] { "commons-lang" }, new UniqueGroupIdConstraint( "commons-lang" ) ); + assertConstraint( new String[] { + "org.apache.maven.test", + "org.apache.maven.test.foo", + "org.apache.maven.shared" }, new UniqueGroupIdConstraint( "org.apache.maven" ) ); + assertConstraint( new String[] { + "org.apache.maven.test", + "org.apache.maven.test.foo", + "org.apache.maven.shared" }, new UniqueGroupIdConstraint( "org.apache" ) ); + assertConstraint( new String[] { + "org.apache.maven.test", + "org.apache.maven.test.foo", + "org.apache.maven.shared", + "org.codehaus.modello" }, new UniqueGroupIdConstraint( "org" ) ); + } + + private void assertConstraint( String[] expectedGroupIds, SimpleConstraint constraint ) + throws Exception + { + String prefix = "Unique Group IDs: "; + + List results = dao.query( constraint ); + assertNotNull( prefix + "Not Null", results ); + assertEquals( prefix + "Results.size", expectedGroupIds.length, results.size() ); + + List groupIdList = Arrays.asList( expectedGroupIds ); + + Iterator it = results.iterator(); + while ( it.hasNext() ) + { + String actualGroupId = (String) it.next(); + assertTrue( prefix + "groupId result should not be blank.", StringUtils.isNotBlank( actualGroupId ) ); + assertTrue( prefix + " groupId result <" + actualGroupId + "> exists in expected GroupIds.", + groupIdList.contains( actualGroupId ) ); + } + } + +} diff --git a/archiva-database/src/test/java/org/apache/maven/archiva/database/constraints/UniqueVersionConstraintTest.java b/archiva-database/src/test/java/org/apache/maven/archiva/database/constraints/UniqueVersionConstraintTest.java new file mode 100644 index 000000000..d5d47aa84 --- /dev/null +++ b/archiva-database/src/test/java/org/apache/maven/archiva/database/constraints/UniqueVersionConstraintTest.java @@ -0,0 +1,128 @@ +package org.apache.maven.archiva.database.constraints; + +/* + * 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.commons.lang.StringUtils; +import org.apache.maven.archiva.database.AbstractArchivaDatabaseTestCase; +import org.apache.maven.archiva.database.ArchivaDAO; +import org.apache.maven.archiva.database.ArtifactDAO; +import org.apache.maven.archiva.database.SimpleConstraint; +import org.apache.maven.archiva.model.ArchivaArtifact; + +import java.util.Arrays; +import java.util.Date; +import java.util.Iterator; +import java.util.List; + +/** + * UniqueVersionConstraintTest + * + * @author <a href="mailto:joakim@erdfelt.com">Joakim Erdfelt</a> + * @version $Id$ + */ +public class UniqueVersionConstraintTest + extends AbstractArchivaDatabaseTestCase +{ + private ArtifactDAO artifactDao; + + protected void setUp() + throws Exception + { + super.setUp(); + + ArchivaDAO dao = (ArchivaDAO) lookup( ArchivaDAO.ROLE, "jdo" ); + artifactDao = dao.getArtifactDAO(); + } + + public ArchivaArtifact createArtifact( String groupId, String artifactId, String version ) + { + ArchivaArtifact artifact = artifactDao.createArtifact( groupId, artifactId, version, "", "jar" ); + artifact.getModel().setLastModified( new Date() ); // mandatory field. + artifact.getModel().setRepositoryId( "testable_repo" ); + return artifact; + } + + public void testConstraint() + throws Exception + { + ArchivaArtifact artifact; + + // Setup artifacts in fresh DB. + artifact = createArtifact( "commons-lang", "commons-lang", "2.0" ); + artifactDao.saveArtifact( artifact ); + + artifact = createArtifact( "commons-lang", "commons-lang", "2.1" ); + artifactDao.saveArtifact( artifact ); + + artifact = createArtifact( "org.apache.maven.test", "test-one", "1.2" ); + artifactDao.saveArtifact( artifact ); + + artifact = createArtifact( "org.apache.maven.test.foo", "test-two", "1.0" ); + artifactDao.saveArtifact( artifact ); + + artifact = createArtifact( "org.apache.maven.shared", "test-two", "2.0" ); + artifactDao.saveArtifact( artifact ); + + artifact = createArtifact( "org.apache.maven.shared", "test-two", "2.1-SNAPSHOT" ); + artifactDao.saveArtifact( artifact ); + + artifact = createArtifact( "org.apache.maven.shared", "test-two", "2.1.1" ); + artifactDao.saveArtifact( artifact ); + + artifact = createArtifact( "org.apache.maven.shared", "test-two", "2.1-alpha-1" ); + artifactDao.saveArtifact( artifact ); + + artifact = createArtifact( "org.apache.maven.shared", "test-bar", "2.1" ); + artifactDao.saveArtifact( artifact ); + + artifact = createArtifact( "org.codehaus.modello", "modellong", "3.0" ); + artifactDao.saveArtifact( artifact ); + + assertConstraint( new String[] {}, new UniqueVersionConstraint( "org.apache", "invalid" ) ); + assertConstraint( new String[] {}, new UniqueVersionConstraint( "org.apache.test", "invalid" ) ); + assertConstraint( new String[] {}, new UniqueVersionConstraint( "invalid", "test-two" ) ); + + assertConstraint( new String[] { "2.0", "2.1" }, new UniqueVersionConstraint( "commons-lang", "commons-lang" ) ); + assertConstraint( new String[] { "1.2" }, new UniqueVersionConstraint( "org.apache.maven.test", "test-one" ) ); + assertConstraint( new String[] { "2.0", "2.1-SNAPSHOT", "2.1.1", "2.1-alpha-1" }, + new UniqueVersionConstraint( "org.apache.maven.shared", "test-two" ) ); + assertConstraint( new String[] { "3.0" }, new UniqueVersionConstraint( "org.codehaus.modello", "modellong" ) ); + } + + private void assertConstraint( String[] versions, SimpleConstraint constraint ) + { + String prefix = "Unique Versions: "; + + List results = dao.query( constraint ); + assertNotNull( prefix + "Not Null", results ); + assertEquals( prefix + "Results.size", versions.length, results.size() ); + + List expectedVersions = Arrays.asList( versions ); + + Iterator it = results.iterator(); + while ( it.hasNext() ) + { + String actualVersion = (String) it.next(); + assertTrue( prefix + "version result should not be blank.", StringUtils.isNotBlank( actualVersion ) ); + assertTrue( prefix + "version result <" + actualVersion + "> exists in expected versions.", + expectedVersions.contains( actualVersion ) ); + } + } +} diff --git a/archiva-database/src/test/java/org/apache/maven/archiva/database/jdo/JdoArchivaDAOTest.java b/archiva-database/src/test/java/org/apache/maven/archiva/database/jdo/JdoArchivaDAOTest.java new file mode 100644 index 000000000..a97492c24 --- /dev/null +++ b/archiva-database/src/test/java/org/apache/maven/archiva/database/jdo/JdoArchivaDAOTest.java @@ -0,0 +1,38 @@ +package org.apache.maven.archiva.database.jdo; + +/* + * 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; + +/** + * JdoArchivaDAOTest + * + * @author <a href="mailto:joakim@erdfelt.com">Joakim Erdfelt</a> + * @version $Id$ + */ +public class JdoArchivaDAOTest + extends AbstractArchivaDatabaseTestCase +{ + public void testSubDAOs() + { + assertNotNull( "Artifact DAO", dao.getArtifactDAO() ); + assertNotNull( "Repository DAO", dao.getRepositoryDAO() ); + } +} diff --git a/archiva-database/src/test/java/org/apache/maven/archiva/database/jdo/JdoArtifactDAOTest.java b/archiva-database/src/test/java/org/apache/maven/archiva/database/jdo/JdoArtifactDAOTest.java new file mode 100644 index 000000000..899d77518 --- /dev/null +++ b/archiva-database/src/test/java/org/apache/maven/archiva/database/jdo/JdoArtifactDAOTest.java @@ -0,0 +1,115 @@ +package org.apache.maven.archiva.database.jdo; + +/* + * 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.model.ArchivaArtifact; +import org.apache.maven.archiva.model.ArchivaArtifactModel; +import org.apache.maven.archiva.model.jpox.ArchivaArtifactModelKey; + +import java.util.Date; +import java.util.List; + +import javax.jdo.JDOHelper; +import javax.jdo.spi.JDOImplHelper; + +/** + * JdoArtifactDAOTest + * + * @author <a href="mailto:joakim@erdfelt.com">Joakim Erdfelt</a> + * @version $Id$ + */ +public class JdoArtifactDAOTest + extends AbstractArchivaDatabaseTestCase +{ + public void testArtifactKey() + { + Object o = JDOImplHelper.getInstance().newObjectIdInstance( ArchivaArtifactModel.class, "foo:bar:1.0::jar" ); + assertNotNull( "Key should not be null.", o ); + assertTrue( "Key should be an instance of " + ArchivaArtifactModelKey.class.getName(), + ( o instanceof ArchivaArtifactModelKey ) ); + + ArchivaArtifactModelKey key = (ArchivaArtifactModelKey) o; + assertEquals( "foo", key.groupId ); + assertEquals( "bar", key.artifactId ); + assertEquals( "1.0", key.version ); + assertEquals( "", key.classifier ); + assertEquals( "jar", key.type ); + } + + public void testArtifactCRUD() + throws Exception + { + ArtifactDAO artiDao = dao.getArtifactDAO(); + + // Create it + ArchivaArtifact artifact = artiDao.createArtifact( "org.apache.maven.archiva", "archiva-test-module", "1.0", + "", "jar" ); + assertNotNull( artifact ); + + // Set some mandatory values + artifact.getModel().setLastModified( new Date() ); + artifact.getModel().setOrigin( "test" ); + + // Save it. + ArchivaArtifact savedArtifact = artiDao.saveArtifact( artifact ); + assertNotNull( savedArtifact ); + String savedKeyId = JDOHelper.getObjectId( savedArtifact.getModel() ).toString(); + assertEquals( "org.apache.maven.archiva:archiva-test-module:1.0::jar", savedKeyId ); + + // Test that something has been saved. + List artifacts = artiDao.queryArtifacts( null ); + assertNotNull( artifacts ); + assertEquals( 1, artifacts.size() ); + + // Test that retrieved object is what we expect. + ArchivaArtifact firstArtifact = (ArchivaArtifact) artifacts.get( 0 ); + assertNotNull( firstArtifact ); + assertEquals( "org.apache.maven.archiva", firstArtifact.getGroupId() ); + assertEquals( "archiva-test-module", firstArtifact.getArtifactId() ); + assertEquals( "1.0", firstArtifact.getVersion() ); + assertEquals( "", firstArtifact.getClassifier() ); + assertEquals( "jar", firstArtifact.getType() ); + + // Change value and save. + savedArtifact.getModel().setOrigin( "changed" ); + artiDao.saveArtifact( savedArtifact ); + + // Test that only 1 object is saved. + assertEquals( 1, artiDao.queryArtifacts( null ).size() ); + + // Get the specific artifact. + ArchivaArtifact actualArtifact = artiDao.getArtifact( "org.apache.maven.archiva", "archiva-test-module", "1.0", + null, "jar" ); + assertNotNull( actualArtifact ); + + // Test expected values. + assertEquals( "archiva-test-module", actualArtifact.getArtifactId() ); + assertEquals( "changed", actualArtifact.getModel().getOrigin() ); + + // Test that only 1 object is saved. + assertEquals( 1, artiDao.queryArtifacts( null ).size() ); + + // Delete object. + artiDao.deleteArtifact( actualArtifact ); + assertEquals( 0, artiDao.queryArtifacts( null ).size() ); + } +} diff --git a/archiva-database/src/test/java/org/apache/maven/archiva/database/jdo/JdoProjectModelDAOTest.java b/archiva-database/src/test/java/org/apache/maven/archiva/database/jdo/JdoProjectModelDAOTest.java new file mode 100644 index 000000000..edb13129d --- /dev/null +++ b/archiva-database/src/test/java/org/apache/maven/archiva/database/jdo/JdoProjectModelDAOTest.java @@ -0,0 +1,111 @@ +package org.apache.maven.archiva.database.jdo; + +/* + * 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.ProjectModelDAO; +import org.apache.maven.archiva.model.ArchivaProjectModel; +import org.apache.maven.archiva.model.jpox.ArchivaProjectModelKey; + +import java.util.Date; +import java.util.List; + +import javax.jdo.JDOHelper; +import javax.jdo.spi.JDOImplHelper; + +/** + * JdoProjectModelDAOTest + * + * @author <a href="mailto:joakim@erdfelt.com">Joakim Erdfelt</a> + * @version $Id$ + */ +public class JdoProjectModelDAOTest + extends AbstractArchivaDatabaseTestCase +{ + public void testProjectModelKey() + { + Object o = JDOImplHelper.getInstance().newObjectIdInstance( ArchivaProjectModel.class, "foo:bar:1.0" ); + assertNotNull( "Key should not be null.", o ); + assertTrue( "Key should be an instance of " + ArchivaProjectModelKey.class.getName(), + ( o instanceof ArchivaProjectModelKey ) ); + + ArchivaProjectModelKey key = (ArchivaProjectModelKey) o; + assertEquals( "foo", key.groupId ); + assertEquals( "bar", key.artifactId ); + assertEquals( "1.0", key.version ); + } + + public void testProjectModelCRUD() + throws Exception + { + ProjectModelDAO projectDao = dao.getProjectModelDAO(); + + // Create it + ArchivaProjectModel model = projectDao.createProjectModel( "org.apache.maven.archiva", "archiva-test-module", + "1.0" ); + assertNotNull( model ); + + // Set some mandatory values + model.setPackaging( "pom" ); + model.setWhenIndexed( new Date() ); + model.setOrigin( "test" ); + + // Save it. + ArchivaProjectModel savedModel = projectDao.saveProjectModel( model ); + assertNotNull( savedModel ); + String savedKeyId = JDOHelper.getObjectId( savedModel ).toString(); + assertEquals( "org.apache.maven.archiva:archiva-test-module:1.0", savedKeyId ); + + // Test that something has been saved. + List projects = projectDao.queryProjectModels( null ); + assertNotNull( projects ); + assertEquals( 1, projects.size() ); + + // Test that retrieved object is what we expect. + ArchivaProjectModel firstModel = (ArchivaProjectModel) projects.get( 0 ); + assertNotNull( firstModel ); + assertEquals( "org.apache.maven.archiva", firstModel.getGroupId() ); + assertEquals( "archiva-test-module", firstModel.getArtifactId() ); + assertEquals( "1.0", firstModel.getVersion() ); + + // Change value and save. + savedModel.setOrigin( "changed" ); + projectDao.saveProjectModel( savedModel ); + + // Test that only 1 object is saved. + assertEquals( 1, projectDao.queryProjectModels( null ).size() ); + + // Get the specific artifact. + ArchivaProjectModel actualModel = projectDao.getProjectModel( "org.apache.maven.archiva", + "archiva-test-module", "1.0" ); + assertNotNull( actualModel ); + + // Test expected values. + assertEquals( "archiva-test-module", actualModel.getArtifactId() ); + assertEquals( "changed", actualModel.getOrigin() ); + + // Test that only 1 object is saved. + assertEquals( 1, projectDao.queryProjectModels( null ).size() ); + + // Delete object. + projectDao.deleteProjectModel( actualModel ); + assertEquals( 0, projectDao.queryProjectModels( null ).size() ); + } +} diff --git a/archiva-database/src/test/java/org/apache/maven/archiva/database/jdo/JdoRepositoryDAOTest.java b/archiva-database/src/test/java/org/apache/maven/archiva/database/jdo/JdoRepositoryDAOTest.java new file mode 100644 index 000000000..dfdc383ec --- /dev/null +++ b/archiva-database/src/test/java/org/apache/maven/archiva/database/jdo/JdoRepositoryDAOTest.java @@ -0,0 +1,96 @@ +package org.apache.maven.archiva.database.jdo; + +/* + * 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.ArchivaDatabaseException; +import org.apache.maven.archiva.database.RepositoryDAO; +import org.apache.maven.archiva.model.ArchivaRepository; + +import java.util.List; + +import javax.jdo.JDOHelper; + +/** + * JdoRepositoryDAOTest + * + * @author <a href="mailto:joakim@erdfelt.com">Joakim Erdfelt</a> + * @version $Id$ + */ +public class JdoRepositoryDAOTest + extends AbstractArchivaDatabaseTestCase +{ + public void testRepositoryCRUD() + throws ArchivaDatabaseException + { + RepositoryDAO repoDao = dao.getRepositoryDAO(); + + // Create it + ArchivaRepository repo = repoDao.createRepository( "testRepo", "Test Repository", + "http://localhost:8080/repository/foo" ); + assertNotNull( repo ); + + // Set some mandatory values + repo.getModel().setCreationSource( "Test Case" ); + repo.getModel().setLayoutName( "default" ); + + // Save it. + ArchivaRepository repoSaved = repoDao.saveRepository( repo ); + assertNotNull( repoSaved ); + assertNotNull( repoSaved.getModel() ); + assertEquals( "testRepo", JDOHelper.getObjectId( repoSaved.getModel() ).toString() ); + + // Test that something has been saved. + List repos = repoDao.getRepositories(); + assertNotNull( repos ); + assertEquals( 1, repos.size() ); + + // Test that retreived object is what we expect. + ArchivaRepository firstRepo = (ArchivaRepository) repos.get( 0 ); + assertNotNull( firstRepo ); + assertEquals( "testRepo", repo.getId() ); + assertEquals( "Test Repository", repo.getModel().getName() ); + assertEquals( "Test Case", repo.getModel().getCreationSource() ); + assertEquals( "default", repo.getModel().getLayoutName() ); + + // Change value and save. + repoSaved.getModel().setCreationSource( "Changed" ); + repoDao.saveRepository( repoSaved ); + + // Test that only 1 object is saved. + assertEquals( 1, repoDao.getRepositories().size() ); + + // Get the specific repo. + ArchivaRepository actualRepo = repoDao.getRepository( "testRepo" ); + assertNotNull( actualRepo ); + + // Test expected values. + assertEquals( "testRepo", actualRepo.getId() ); + assertEquals( "http://localhost:8080/repository/foo", actualRepo.getUrl().toString() ); + assertEquals( "Changed", actualRepo.getModel().getCreationSource() ); + + // Test that only 1 object is saved. + assertEquals( 1, repoDao.getRepositories().size() ); + + // Delete object. + repoDao.deleteRepository( actualRepo ); + assertEquals( 0, repoDao.getRepositories().size() ); + } +} diff --git a/archiva-database/src/test/resources/META-INF/plexus/components.xml b/archiva-database/src/test/resources/META-INF/plexus/components.xml index 9be2e75bc..cd924e95c 100644 --- a/archiva-database/src/test/resources/META-INF/plexus/components.xml +++ b/archiva-database/src/test/resources/META-INF/plexus/components.xml @@ -1,30 +1,24 @@ -<plexus> +<component-set> <components> <component> - <role>org.codehaus.plexus.ibatis.PlexusIbatisHelper</role> - <implementation>org.codehaus.plexus.ibatis.DefaultPlexusIbatisHelper</implementation> - <role-hint>metadata</role-hint> + <role>org.codehaus.plexus.jdo.JdoFactory</role> + <role-hint>archiva</role-hint> + <implementation>org.codehaus.plexus.jdo.DefaultConfigurableJdoFactory</implementation> <configuration> - <resource>ibatis-config.xml</resource> - <properties> + <persistenceManagerFactoryClass>org.jpox.PersistenceManagerFactoryImpl</persistenceManagerFactoryClass> + <otherProperties> <property> - <name>jdbc.driver</name> - <value>org.apache.derby.jdbc.EmbeddedDriver</value> + <name>javax.jdo.PersistenceManagerFactoryClass</name> + <value>org.jpox.PersistenceManagerFactoryImpl</value> </property> - <property> - <name>jdbc.url</name> - <value>jdbc:derby:${plexus.home}/testdb;create=true</value> - </property> - <property> - <name>jdbc.username</name> - <value>app</value> - </property> - <property> - <name>jdbc.password</name> - <value></value> - </property> - </properties> + </otherProperties> </configuration> </component> + + <component> + <role>org.codehaus.plexus.logging.LoggerManager</role> + <implementation>org.codehaus.plexus.logging.slf4j.Slf4jLoggerManager</implementation> + </component> </components> -</plexus>
\ No newline at end of file +</component-set> + diff --git a/archiva-database/src/test/resources/ibatis-config.xml b/archiva-database/src/test/resources/ibatis-config.xml deleted file mode 100644 index b7013e013..000000000 --- a/archiva-database/src/test/resources/ibatis-config.xml +++ /dev/null @@ -1,37 +0,0 @@ -<?xml version="1.0" encoding="UTF-8" ?> -<!DOCTYPE sqlMapConfig - PUBLIC "-//ibatis.apache.org//DTD SQL Map Config 2.0//EN" - "http://ibatis.apache.org/dtd/sql-map-config-2.dtd"> - -<sqlMapConfig> - <settings - cacheModelsEnabled="true" - enhancementEnabled="true" - lazyLoadingEnabled="false" - maxRequests="32" - maxSessions="10" - maxTransactions="5" - useStatementNamespaces="false" - /> - - <transactionManager type="JDBC"> - <dataSource type="SIMPLE"> - <property name="JDBC.Driver" value="${jdbc.driver}"/> - <property name="JDBC.ConnectionURL" value="${jdbc.url}"/> - <property name="JDBC.Username" value="${jdbc.username}"/> - <property name="JDBC.Password" value="${jdbc.password}"/> - </dataSource> - </transactionManager> - - <!-- - <resultObjectFactory type="org.codehaus.plexus.ibatis.PlexusResultObjectFactory" > - <property name="foo" value="bar"/> - </resultObjectFactory> - --> - - <sqlMap resource="org/apache/maven/archiva/database/ManageTables.xml"/> - <sqlMap resource="org/apache/maven/archiva/database/MetadataKey.xml"/> - <sqlMap resource="org/apache/maven/archiva/database/ArtifactKey.xml"/> - <sqlMap resource="org/apache/maven/archiva/database/RepositoryMetadata.xml"/> - -</sqlMapConfig>
\ No newline at end of file diff --git a/archiva-database/src/test/resources/log4j.properties b/archiva-database/src/test/resources/log4j.properties deleted file mode 100644 index a30a9049a..000000000 --- a/archiva-database/src/test/resources/log4j.properties +++ /dev/null @@ -1,10 +0,0 @@ -log4j.rootCategory=DEBUG, root - -## Define the destination and format of our logging -log4j.appender.root=org.apache.log4j.ConsoleAppender -log4j.appender.root.layout=org.apache.log4j.PatternLayout -log4j.appender.root.layout.ConversionPattern=%d{HH:mm:ss,SSS} %-5p [%c] - %m%n - -# SqlMap logging configuration... -log4j.logger.com.ibatis=INFO -log4j.logger.java.sql=INFO diff --git a/archiva-database/src/test/resources/log4j.xml b/archiva-database/src/test/resources/log4j.xml new file mode 100644 index 000000000..395941ac3 --- /dev/null +++ b/archiva-database/src/test/resources/log4j.xml @@ -0,0 +1,76 @@ +<?xml version="1.0" encoding="UTF-8" ?> +<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd"> + +<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/"> + + <appender name="console" class="org.apache.log4j.ConsoleAppender"> + <param name="Target" value="System.out"/> + <layout class="org.apache.log4j.PatternLayout"> + <param name="ConversionPattern" value="%d [%t] %-5p %-30c{1} - %m%n"/> + </layout> + </appender> + + <!-- Help identify bugs during testing --> + <logger name="org.apache.maven"> + <level value="info"/> + </logger> + + <logger name="org.codehaus.plexus.security"> + <level value="info"/> + </logger> + + <!-- squelch noisy objects (for now) --> + <logger name="org.codehaus.plexus.mailsender.MailSender"> + <level value="info"/> + </logger> + + <logger name="org.quartz"> + <level value="info"/> + </logger> + + <logger name="org.apache.jasper"> + <level value="info"/> + </logger> + + <logger name="com.opensymphony.xwork"> + <level value="info"/> + </logger> + + <logger name="com.opensymphony.webwork"> + <level value="info"/> + </logger> + + <logger name="org.codehaus.plexus.PlexusContainer"> + <level value="info"/> + </logger> + + <logger name="JPOX"> + <level value="warn"/> + </logger> + + <logger name="JPOX.MetaData"> + <level value="error"/> + </logger> + + <logger name="JPOX.RDBMS.SQL"> + <level value="error"/> + </logger> + + <logger name="SQL"> + <level value="error"/> + </logger> + + <logger name="freemarker"> + <level value="warn"/> + </logger> + + <logger name="org.codehaus.plexus.component.manager.ClassicSingletonComponentManager"> + <level value="error"/> + </logger> + + <root> + <priority value ="info" /> + <appender-ref ref="console" /> + </root> + +</log4j:configuration> |