View Javadoc

1   /**
2    *
3    * Copyright 2005 The Apache Software Foundation
4    *
5    *  Licensed under the Apache License, Version 2.0 (the "License");
6    *  you may not use this file except in compliance with the License.
7    *  You may obtain a copy of the License at
8    *
9    *     http://www.apache.org/licenses/LICENSE-2.0
10   *
11   *  Unless required by applicable law or agreed to in writing, software
12   *  distributed under the License is distributed on an "AS IS" BASIS,
13   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   *  See the License for the specific language governing permissions and
15   *  limitations under the License.
16   */
17  package org.apache.geronimo.deployment;
18  
19  import org.apache.geronimo.kernel.repository.Artifact;
20  import org.apache.geronimo.kernel.repository.Version;
21  import org.apache.geronimo.kernel.repository.Environment;
22  
23  /**
24   * A utility class to flesh out any incomplete Module IDs (formerly known as
25   * config IDs) encountered during the course of a deployment.  For example,
26   * an EAR may have a module ID with only an artifactId, and contain a web
27   * app with no Geronimo plan and an EJB JAR with a module ID with no version.
28   *
29   * @version $Rev: 410741 $ $Date: 2006-05-31 21:35:48 -0700 (Wed, 31 May 2006) $
30   */
31  public class ModuleIDBuilder {
32      private Version defaultVersion;
33      private String defaultGroup;
34  
35      public ModuleIDBuilder() {
36          defaultVersion = new Version(Long.toString(System.currentTimeMillis()));
37          defaultGroup = Artifact.DEFAULT_GROUP_ID;
38      }
39  
40      /**
41       * If an EAR is going to pass this module ID builder to its children, it
42       * can use this to set the default groupId to be its own.
43       */
44      public void setDefaultGroup(String defaultGroup) {
45          this.defaultGroup = defaultGroup;
46      }
47  
48  
49      /**
50       * If an EAR is going to pass this module ID builder to its children, it
51       * can use this to set the default version to be its own.
52       */
53      public void setDefaultVersion(Version defaultVersion) {
54          this.defaultVersion = defaultVersion;
55      }
56  
57      /**
58       * Translates the argument Artifact to a fully-resolved Artifact, which is
59       * returned.  If the argument was fully-resolved to begin with it is
60       * returned as is.  Otherwise, a new Artifact is returned with any missing
61       * values populated.
62       *
63       * @param argument     The artifact to review
64       * @param defaultType  The type to use if the artifact to review has no
65       *                     type specified
66       *
67       * @return A fully resolved Artifact
68       *
69       * @throws IllegalArgumentException Occurs when the argument artifact does
70       *                                  not have an artifactId
71       */
72      public Artifact resolve(Artifact argument, String defaultType) {
73          if(argument.isResolved()) {
74              return argument;
75          }
76          if(argument.getArtifactId() == null || argument.getArtifactId().equals("")) {
77              throw new IllegalArgumentException("Incoming Artifact must have an ArtifactID (not "+argument+")");
78          }
79          return new Artifact(argument.getGroupId() == null || argument.getGroupId().equals("") ? defaultGroup : argument.getGroupId(),
80                  argument.getArtifactId(),
81                  argument.getVersion() == null ? defaultVersion : argument.getVersion(),
82                  argument.getType() == null || argument.getType().equals("") ? defaultType : argument.getType());
83      }
84  
85      /**
86       * Creates a new artifact using entirely default values.
87       *
88       * @param defaultArtifact  The artifactId to use for the new Artifact
89       * @param defaultType      The type to use for the new Artifact
90       */
91      public Artifact createDefaultArtifact(String defaultArtifact, String defaultType) {
92          return new Artifact(defaultGroup, defaultArtifact, defaultVersion, defaultType);
93      }
94  
95      /**
96       * Guarantees that the argument Environment will have a present and fully
97       * qualified module ID when this method returns. If the Environment is
98       * missing a module ID, or has a partial module ID (isResolved() == false)
99       * then this method will fill in any missing values.  If the module ID is
100      * present and resolved, then this method does nothing.
101      *
102      * @param environment        The Environment to check and populate
103      * @param defaultArtifactId  The artifactId to use if the Envrionment does
104      *                           not have a module ID at all
105      * @param defaultType        The type to use if the Environment is lacking
106      *                           a module ID or the module ID is lacking a type
107      */
108     public void resolve(Environment environment, String defaultArtifactId, String defaultType) {
109         if(environment.getConfigId() == null) {
110             environment.setConfigId(resolve(new Artifact(null, defaultArtifactId, (Version)null, defaultType), defaultType));
111         } else if(!environment.getConfigId().isResolved()) {
112             environment.setConfigId(resolve(environment.getConfigId(), defaultType));
113         }
114     }
115 }