View Javadoc

1   /*
2    * Copyright 2013 smartics, Kronseder & Reiner GmbH
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package de.smartics.maven.plugin.jboss.modules.domain;
17  
18  import org.apache.commons.lang.StringUtils;
19  import org.apache.maven.artifact.versioning.ArtifactVersion;
20  import org.apache.maven.artifact.versioning.DefaultArtifactVersion;
21  import org.sonatype.aether.artifact.Artifact;
22  
23  import edu.emory.mathcs.backport.java.util.Arrays;
24  
25  /**
26   * The naming strategy for module slots.
27   */
28  public enum SlotStrategy
29  {
30    // ***************************** Enumeration ******************************
31  
32    // ******************************** Fields ********************************
33  
34    // --- constants ----------------------------------------------------------
35  
36    /**
37     * The module is set to the main slot (default).
38     */
39    MAIN("main"),
40  
41    /**
42     * The module is set to the major artifact version slot.
43     */
44    VERSION_MAJOR("version-major");
45  
46    /**
47     * The main slot.
48     */
49    public static final String MAIN_SLOT = "main";
50  
51    // --- members ------------------------------------------------------------
52  
53    /**
54     * The identifier of the strategy.
55     */
56    private String id;
57  
58    // ***************************** Constructors *****************************
59  
60    private SlotStrategy(final String id)
61    {
62      this.id = id;
63    }
64  
65    // ******************************** Methods *******************************
66  
67    // --- init ---------------------------------------------------------------
68  
69    // --- get&set ------------------------------------------------------------
70  
71    // --- business -----------------------------------------------------------
72  
73    /**
74     * Returns the slot strategy identified by the given {@code id}.
75     *
76     * @param id the identifier of the requested slot strategy.
77     * @return the requested slot strategy.
78     * @throws IllegalArgumentException if {@code id} is not a valid slot
79     *           strategy.
80     */
81    public static SlotStrategy fromString(final String id)
82      throws IllegalArgumentException
83    {
84      for (final SlotStrategy strategy : values())
85      {
86        if (id.equals(strategy.id))
87        {
88          return strategy;
89        }
90      }
91  
92      throw new IllegalArgumentException(String.format(
93          "Invalid slot strategy '%s'. Allowed values are: %s", id,
94          Arrays.toString(values())));
95    }
96  
97    // --- object basics ------------------------------------------------------
98  
99    @Override
100   public String toString()
101   {
102     return id;
103   }
104 
105   /**
106    * Calculates the name for the slot.
107    *
108    * @param artifact the artifact with additional information. If
109    *          <code>null</code>: a static prefix will be assumed.
110    * @param defaultSlot the name of the default slot to use.
111    * @return the name of the slot.
112    */
113   public String calcSlot(final Artifact artifact, final String defaultSlot)
114   {
115     if (this == VERSION_MAJOR)
116     {
117       final String versionString = calcVersion(artifact);
118       final ArtifactVersion version = new DefaultArtifactVersion(versionString);
119       final int majorVersion = version.getMajorVersion();
120       final String slot;
121       if (!(StringUtils.isBlank(defaultSlot) || MAIN_SLOT.equals(defaultSlot)))
122       {
123         slot = defaultSlot + majorVersion;
124       }
125       else
126       {
127         slot = String.valueOf(majorVersion);
128       }
129       return slot;
130     }
131 
132     return StringUtils.isBlank(defaultSlot) ? MAIN_SLOT : defaultSlot;
133   }
134 
135   /**
136    * Calculates the name for the slot.
137    *
138    * @param defaultSlot the name of the default slot. May be blank.
139    * @param moduleSlot the name of the module slot. May be blank.
140    * @param artifact the artifact with additional information. If
141    *          <code>null</code>: a static prefix will be assumed.
142    * @return the name of the slot.
143    */
144   public String calcSlot(final String defaultSlot, final String moduleSlot,
145       final Artifact artifact)
146   {
147     final String fallBackSlot =
148         StringUtils.isBlank(moduleSlot) ? defaultSlot : moduleSlot;
149     final String slot = calcSlot(artifact, fallBackSlot);
150     return slot;
151   }
152 
153   private String calcVersion(final Artifact artifact)
154   {
155     if (artifact != null)
156     {
157       return artifact.getVersion();
158     }
159     else
160     {
161       return "VersionX";
162     }
163   }
164 }