You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@maven.apache.org by oc...@apache.org on 2007/08/13 13:00:09 UTC

svn commit: r565307 - in /maven/sandbox/trunk/archiva/archiva-consumer-plugin: ./ src/ src/main/ src/main/java/ src/main/java/org/ src/main/java/org/apache/ src/main/java/org/apache/maven/ src/main/java/org/apache/maven/archiva/ src/main/java/org/apach...

Author: oching
Date: Mon Aug 13 04:00:07 2007
New Revision: 565307

URL: http://svn.apache.org/viewvc?view=rev&rev=565307
Log:
[MRM-471]
- Added a sample project that demonstrated how to plugin a component in Archiva
- Created a consumer for discovering new artifacts in the repository being scanned.
- Added a ReadMe.txt file that contains the instructions


Added:
    maven/sandbox/trunk/archiva/archiva-consumer-plugin/
    maven/sandbox/trunk/archiva/archiva-consumer-plugin/ReadMe.txt
    maven/sandbox/trunk/archiva/archiva-consumer-plugin/pom.xml
    maven/sandbox/trunk/archiva/archiva-consumer-plugin/src/
    maven/sandbox/trunk/archiva/archiva-consumer-plugin/src/main/
    maven/sandbox/trunk/archiva/archiva-consumer-plugin/src/main/java/
    maven/sandbox/trunk/archiva/archiva-consumer-plugin/src/main/java/org/
    maven/sandbox/trunk/archiva/archiva-consumer-plugin/src/main/java/org/apache/
    maven/sandbox/trunk/archiva/archiva-consumer-plugin/src/main/java/org/apache/maven/
    maven/sandbox/trunk/archiva/archiva-consumer-plugin/src/main/java/org/apache/maven/archiva/
    maven/sandbox/trunk/archiva/archiva-consumer-plugin/src/main/java/org/apache/maven/archiva/consumer/
    maven/sandbox/trunk/archiva/archiva-consumer-plugin/src/main/java/org/apache/maven/archiva/consumer/plugin/
    maven/sandbox/trunk/archiva/archiva-consumer-plugin/src/main/java/org/apache/maven/archiva/consumer/plugin/DiscoverNewArtifactConsumer.java

Added: maven/sandbox/trunk/archiva/archiva-consumer-plugin/ReadMe.txt
URL: http://svn.apache.org/viewvc/maven/sandbox/trunk/archiva/archiva-consumer-plugin/ReadMe.txt?view=auto&rev=565307
==============================================================================
--- maven/sandbox/trunk/archiva/archiva-consumer-plugin/ReadMe.txt (added)
+++ maven/sandbox/trunk/archiva/archiva-consumer-plugin/ReadMe.txt Mon Aug 13 04:00:07 2007
@@ -0,0 +1,50 @@
+--------------------------------------
+How To Add A New Component in Archiva:
+--------------------------------------
+
+1. Create a project for your component.
+
+2. Declare your class or in this case, consumer as a component as shown in the example below. This
+   should be put at the class level.
+
+   Ex.
+   @plexus.component role="org.apache.maven.archiva.consumers.KnownRepositoryContentConsumer"
+                     role-hint="discover-new-artifact"
+                     instantiation-strategy="per-lookup"
+
+
+   Legend:
+   ** role : the interface that your class implements
+   ** role-hint : the lookup name of the component (your class/consumer)
+   ** instantiation-strategy : how your class will be instantiated
+
+3. Make sure to add the snipper below in the <build> section of the project's pom. This is needed to
+   generate the components.xml.
+
+      <plugin>
+        <groupId>org.codehaus.plexus</groupId>
+        <artifactId>plexus-maven-plugin</artifactId>
+        <version>1.3.5</version>
+        <executions>
+          <execution>
+            <id>generate</id>
+            <goals>
+              <goal>descriptor</goal>
+            </goals>
+          </execution>
+        </executions>
+      </plugin>
+
+4. Package your project by executing 'mvn clean package'
+
+5. Let's say you are using the apache-archiva-1.0-beta-2-SNAPSHOT-bin.tar.gz to run Archiva. Unpack
+   the binaries then go to bin/linux-x86-32/ (assuming you are running on Linux), then execute
+   './run.sh console'. Then stop or shutdown Archiva after it started. (This is necessary to unpack
+   the war file.)
+
+6. Copy the jar file you created in #3 in apache-archiva-1.0-beta-2-SNAPSHOT/apps/archiva/webapp/lib/
+
+7. Add the necessary configurations in archiva.xml (in this case, add 'discover-new-artifact' as a
+   <knownContentConsumer>)
+
+8. Start up Archiva again.
\ No newline at end of file

Added: maven/sandbox/trunk/archiva/archiva-consumer-plugin/pom.xml
URL: http://svn.apache.org/viewvc/maven/sandbox/trunk/archiva/archiva-consumer-plugin/pom.xml?view=auto&rev=565307
==============================================================================
--- maven/sandbox/trunk/archiva/archiva-consumer-plugin/pom.xml (added)
+++ maven/sandbox/trunk/archiva/archiva-consumer-plugin/pom.xml Mon Aug 13 04:00:07 2007
@@ -0,0 +1,56 @@
+<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
+  <modelVersion>4.0.0</modelVersion>
+  <groupId>org.apache.maven.archiva</groupId>
+  <artifactId>archiva-consumer-plugin</artifactId>
+  <version>1.0-beta-2-SNAPSHOT</version>
+  <packaging>jar</packaging>
+  <name>archiva-consumer-plugin</name>
+  <url>http://maven.apache.org</url>
+  <description>
+    This is a sample Consumer component which demonstrates how a component can be plugged in Archiva.
+  </description>
+  <dependencies>
+    <dependency>
+      <groupId>org.apache.maven.archiva</groupId>
+      <artifactId>archiva-consumer-api</artifactId>
+      <version>1.0-beta-2-SNAPSHOT</version>
+    </dependency>
+    <dependency>
+      <groupId>org.apache.maven.archiva</groupId>
+      <artifactId>archiva-configuration</artifactId>
+      <version>1.0-beta-2-SNAPSHOT</version>
+    </dependency>
+    <dependency>
+      <groupId>org.apache.maven.archiva</groupId>
+      <artifactId>archiva-repository-layer</artifactId>
+      <version>1.0-beta-2-SNAPSHOT</version>
+    </dependency>
+    <dependency>
+      <groupId>org.apache.maven.archiva</groupId>
+      <artifactId>archiva-indexer</artifactId>
+      <version>1.0-beta-2-SNAPSHOT</version>
+    </dependency>
+  </dependencies>     
+  <build>
+    <plugins>
+      <!--
+      This is required to be declared in the pom.
+      Generates the components.xml file which is needed for the consumer to be looked up by Archiva
+      -->
+      <plugin>
+        <groupId>org.codehaus.plexus</groupId>
+        <artifactId>plexus-maven-plugin</artifactId>
+        <version>1.3.5</version>
+        <executions>
+          <execution>
+            <id>generate</id>
+            <goals>
+              <goal>descriptor</goal>
+            </goals>
+          </execution>
+        </executions>
+      </plugin>
+    </plugins>
+  </build>
+</project>

Added: maven/sandbox/trunk/archiva/archiva-consumer-plugin/src/main/java/org/apache/maven/archiva/consumer/plugin/DiscoverNewArtifactConsumer.java
URL: http://svn.apache.org/viewvc/maven/sandbox/trunk/archiva/archiva-consumer-plugin/src/main/java/org/apache/maven/archiva/consumer/plugin/DiscoverNewArtifactConsumer.java?view=auto&rev=565307
==============================================================================
--- maven/sandbox/trunk/archiva/archiva-consumer-plugin/src/main/java/org/apache/maven/archiva/consumer/plugin/DiscoverNewArtifactConsumer.java (added)
+++ maven/sandbox/trunk/archiva/archiva-consumer-plugin/src/main/java/org/apache/maven/archiva/consumer/plugin/DiscoverNewArtifactConsumer.java Mon Aug 13 04:00:07 2007
@@ -0,0 +1,221 @@
+package org.apache.maven.archiva.consumer.plugin;
+
+/*
+ * 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.consumers.AbstractMonitoredConsumer;
+import org.apache.maven.archiva.consumers.KnownRepositoryContentConsumer;
+import org.apache.maven.archiva.consumers.ConsumerException;
+import org.apache.maven.archiva.configuration.ArchivaConfiguration;
+import org.apache.maven.archiva.configuration.FileTypes;
+import org.apache.maven.archiva.repository.layout.BidirectionalRepositoryLayoutFactory;
+import org.apache.maven.archiva.repository.layout.BidirectionalRepositoryLayout;
+import org.apache.maven.archiva.repository.layout.LayoutException;
+import org.apache.maven.archiva.model.ArchivaRepository;
+import org.apache.maven.archiva.model.ArchivaArtifact;
+import org.apache.maven.archiva.indexer.search.CrossRepositorySearch;
+import org.apache.maven.archiva.indexer.search.SearchResults;
+import org.apache.maven.archiva.indexer.search.SearchResultLimits;
+import org.apache.maven.archiva.indexer.search.SearchResultHit;
+import org.codehaus.plexus.registry.RegistryListener;
+import org.codehaus.plexus.registry.Registry;
+import org.codehaus.plexus.personality.plexus.lifecycle.phase.Initializable;
+import org.codehaus.plexus.personality.plexus.lifecycle.phase.InitializationException;
+
+import java.util.List;
+import java.util.ArrayList;
+import java.util.Iterator;
+
+/**
+ * @author <a href="mailto:oching@apache.org">Maria Odea Ching</a>
+ * @plexus.component role="org.apache.maven.archiva.consumers.KnownRepositoryContentConsumer"
+ * role-hint="discover-new-artifact"
+ * instantiation-strategy="per-lookup"
+ */
+public class DiscoverNewArtifactConsumer
+    extends AbstractMonitoredConsumer
+    implements KnownRepositoryContentConsumer, RegistryListener, Initializable
+{
+    /**
+     * @plexus.configuration default-value="discover-new-artifact"
+     */
+    private String id;
+
+    /**
+     * @plexus.configuration default-value="Discover new artifacts in the repository."
+     */
+    private String description;
+
+    /**
+     * @plexus.requirement
+     */
+    private FileTypes filetypes;
+
+    /**
+     * @plexus.requirement role-hint="default"
+     */
+    private CrossRepositorySearch repoSearch;
+
+    /**
+     * @plexus.requirement
+     */
+    private BidirectionalRepositoryLayoutFactory layoutFactory;
+
+    /**
+     * @plexus.requirement
+     */
+    private ArchivaConfiguration configuration;
+
+    private List propertyNameTriggers = new ArrayList();
+
+    private List includes = new ArrayList();
+
+    private ArchivaRepository repository;
+
+    private List newArtifacts = new ArrayList();
+
+    private BidirectionalRepositoryLayout repositoryLayout;
+
+    public String getId()
+    {
+        return this.id;
+    }
+
+    public String getDescription()
+    {
+        return this.description;
+    }
+
+    public boolean isPermanent()
+    {
+        return false;
+    }
+
+    public List getExcludes()
+    {
+        return null;
+    }
+
+    public List getIncludes()
+    {
+        return this.includes;
+    }
+
+    public void beginScan( ArchivaRepository repository )
+        throws ConsumerException
+    {
+        if ( !repository.isManaged() )
+        {
+            throw new ConsumerException( "Consumer requires managed repository." );
+        }
+
+        this.repository = repository;
+
+        try
+        {
+            this.repositoryLayout = layoutFactory.getLayout( this.repository.getLayoutType() );
+        }
+        catch ( LayoutException e )
+        {
+            throw new ConsumerException(
+                "Unable to initialize consumer due to unknown repository layout: " + e.getMessage(), e );
+        }
+    }
+
+    public void processFile( String path )
+        throws ConsumerException
+    {
+        // @todo needs to be tested!
+        SearchResults results =
+            repoSearch.searchForTerm( repository.getId() + "/" + path, new SearchResultLimits( 0 ) );
+        List hits = results.getHits();
+        boolean found = false;
+        for ( Iterator iter = hits.iterator(); iter.hasNext(); )
+        {
+            SearchResultHit hit = (SearchResultHit) iter.next();
+            if ( ( repository.getId() + "/" + path ).equalsIgnoreCase( hit.getUrl() ) )
+            {
+                found = true;
+                break;
+            }
+        }
+
+        if ( found )
+        {
+            try
+            {
+                ArchivaArtifact artifact = this.repositoryLayout.toArtifact( path );
+                newArtifacts.add( artifact );
+            }
+            catch ( LayoutException e )
+            {
+                // Not an artifact.
+            }
+        }
+    }
+
+    public void completeScan()
+    {
+        // @todo dump into file
+        for ( Iterator iter = newArtifacts.iterator(); iter.hasNext(); )
+        {
+            ArchivaArtifact artifact = (ArchivaArtifact) iter.next();
+            //System.out.println( "\n %%%%%% NEW ARTIFACT == " + artifact.getGroupId() + ":" +
+            //  artifact.getArtifactId() + ":" + artifact.getVersion() + ":" + artifact.getType() );
+        }
+
+        /* do nothing */
+    }
+
+    public void afterConfigurationChange( Registry registry, String propertyName, Object propertyValue )
+    {
+        if ( propertyNameTriggers.contains( propertyName ) )
+        {
+            initIncludes();
+        }
+    }
+
+    public void beforeConfigurationChange( Registry registry, String propertyName, Object propertyValue )
+    {
+        /* do nothing */
+    }
+
+    private void initIncludes()
+    {
+        includes.clear();
+
+        includes.addAll( filetypes.getFileTypePatterns( FileTypes.INDEXABLE_CONTENT ) );
+    }
+
+    public void initialize()
+        throws InitializationException
+    {
+        propertyNameTriggers = new ArrayList();
+        propertyNameTriggers.add( "repositoryScanning" );
+        propertyNameTriggers.add( "fileTypes" );
+        propertyNameTriggers.add( "fileType" );
+        propertyNameTriggers.add( "patterns" );
+        propertyNameTriggers.add( "pattern" );
+
+        configuration.addChangeListener( this );
+
+        initIncludes();
+    }
+
+}