View Javadoc

1   /**
2    * Copyright 2005-2006 the original author or authors.
3    *
4    * Licensed under the Gnu General Pubic License, Version 2.0 (the
5    * "License"); you may not use this file except in compliance with
6    * the License. You may obtain a copy of the License at
7    *
8    *      http://www.opensource.org/licenses/gpl-license.php
9    *
10   * This program is distributed in the hope that it will be useful,
11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13   * See the Gnu General Public License for more details.
14   */
15  package org.figure8.join.services.repository;
16  
17  import org.figure8.join.businessobjects.artifact.Artifact;
18  import org.figure8.join.util.AntHelper;
19  
20  import java.util.Properties;
21  /**
22   * This is a base class for structured repository, ie: repository having
23   * a root (path, URL or whatever ...) and storing artifacts using an internal
24   * tree-styled structure.
25   * <br/>
26   * This abstract implementation proposes to express structure using Ant-styled
27   * properties that will be replaced at runtime by artifact properties. Such
28   * properties are : <i>${release}</i>, <i>${uniqueId}</i>, <i>${versionInfo}</i>
29   * and <i>${categoryInfo}</i>. The default structure is: ${categoryInfo}/${release}.
30   * @author <a href="mailto:laurent.broudoux@free.fr">Laurent Broudoux</a>
31   * @version $Revision: 1.2 $
32   */
33  public abstract class AbstractStructuredRepository implements Repository{
34  
35     // Static -------------------------------------------------------------------
36  
37     /** Repository default structure definition. */
38     public static final String DEFAULT_STRUCTURE = "${categoryInfo}/${release}";
39  
40     /** Structure property relating to the Artifact unique id. */
41     public static final String ID_PROPERTY = "uniqueId";
42     /** Structure property relating to the Artifact Release name. */
43     public static final String RELEASE_PROPERTY = "release";
44     /** Structure property relating to the Artifact category info. */
45     public static final String CATEGORY_PROPERTY = "categoryInfo";
46     /** Structure property relating to the Artifact version info. */
47     public static final String VERSION_PROPERTY = "versionInfo";
48  
49  
50     // Attributes ---------------------------------------------------------------
51  
52     /** Repository structure definition using Artifact properties. */
53     private String structureDefinition = DEFAULT_STRUCTURE;
54  
55  
56     // Public -------------------------------------------------------------------
57  
58     /** @param definition Repository internal structure definition */
59     public void setStructureDefinition(String definition){
60        this.structureDefinition = definition;
61     }
62     /** @return Repository internal structure definition */
63     public String getStructureDefinition(){
64        return structureDefinition;
65     }
66  
67  
68     // Protected ----------------------------------------------------------------
69  
70     /**
71      * Return the relative path of Artifact within repository structure
72      * according to the structure definition.
73      * @param artifact The artifact for whom getting relative path
74      * @return The relative path of artifact within repotisoty
75      */
76     protected String getStructurePath(Artifact artifact){
77        // Create artifact properties.
78        Properties properties = new Properties();
79        properties.setProperty(ID_PROPERTY, artifact.getUniqueId());
80        properties.setProperty(RELEASE_PROPERTY, artifact.getRelease().getName());
81        properties.setProperty(VERSION_PROPERTY, artifact.getVersionInfo());
82        properties.setProperty(CATEGORY_PROPERTY, artifact.getCategoryInfo());
83  
84        // Compute structure path for this artifact
85        String result = structureDefinition;
86        try {result = AntHelper.replaceProperties(result, properties);}
87        catch (Exception e){
88           // Do nothing here. Best efforts have been done...
89        }
90        return result;
91     }
92  }