001    /**
002     *
003     * Copyright 2005 The Apache Software Foundation
004     *
005     *  Licensed under the Apache License, Version 2.0 (the "License");
006     *  you may not use this file except in compliance with the License.
007     *  You may obtain a copy of the License at
008     *
009     *     http://www.apache.org/licenses/LICENSE-2.0
010     *
011     *  Unless required by applicable law or agreed to in writing, software
012     *  distributed under the License is distributed on an "AS IS" BASIS,
013     *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     *  See the License for the specific language governing permissions and
015     *  limitations under the License.
016     */
017    package org.apache.geronimo.deployment;
018    
019    import org.apache.geronimo.kernel.repository.Artifact;
020    import org.apache.geronimo.kernel.repository.Version;
021    import org.apache.geronimo.kernel.repository.Environment;
022    
023    /**
024     * A utility class to flesh out any incomplete Module IDs (formerly known as
025     * config IDs) encountered during the course of a deployment.  For example,
026     * an EAR may have a module ID with only an artifactId, and contain a web
027     * app with no Geronimo plan and an EJB JAR with a module ID with no version.
028     *
029     * @version $Rev: 410741 $ $Date: 2006-05-31 21:35:48 -0700 (Wed, 31 May 2006) $
030     */
031    public class ModuleIDBuilder {
032        private Version defaultVersion;
033        private String defaultGroup;
034    
035        public ModuleIDBuilder() {
036            defaultVersion = new Version(Long.toString(System.currentTimeMillis()));
037            defaultGroup = Artifact.DEFAULT_GROUP_ID;
038        }
039    
040        /**
041         * If an EAR is going to pass this module ID builder to its children, it
042         * can use this to set the default groupId to be its own.
043         */
044        public void setDefaultGroup(String defaultGroup) {
045            this.defaultGroup = defaultGroup;
046        }
047    
048    
049        /**
050         * If an EAR is going to pass this module ID builder to its children, it
051         * can use this to set the default version to be its own.
052         */
053        public void setDefaultVersion(Version defaultVersion) {
054            this.defaultVersion = defaultVersion;
055        }
056    
057        /**
058         * Translates the argument Artifact to a fully-resolved Artifact, which is
059         * returned.  If the argument was fully-resolved to begin with it is
060         * returned as is.  Otherwise, a new Artifact is returned with any missing
061         * values populated.
062         *
063         * @param argument     The artifact to review
064         * @param defaultType  The type to use if the artifact to review has no
065         *                     type specified
066         *
067         * @return A fully resolved Artifact
068         *
069         * @throws IllegalArgumentException Occurs when the argument artifact does
070         *                                  not have an artifactId
071         */
072        public Artifact resolve(Artifact argument, String defaultType) {
073            if(argument.isResolved()) {
074                return argument;
075            }
076            if(argument.getArtifactId() == null || argument.getArtifactId().equals("")) {
077                throw new IllegalArgumentException("Incoming Artifact must have an ArtifactID (not "+argument+")");
078            }
079            return new Artifact(argument.getGroupId() == null || argument.getGroupId().equals("") ? defaultGroup : argument.getGroupId(),
080                    argument.getArtifactId(),
081                    argument.getVersion() == null ? defaultVersion : argument.getVersion(),
082                    argument.getType() == null || argument.getType().equals("") ? defaultType : argument.getType());
083        }
084    
085        /**
086         * Creates a new artifact using entirely default values.
087         *
088         * @param defaultArtifact  The artifactId to use for the new Artifact
089         * @param defaultType      The type to use for the new Artifact
090         */
091        public Artifact createDefaultArtifact(String defaultArtifact, String defaultType) {
092            return new Artifact(defaultGroup, defaultArtifact, defaultVersion, defaultType);
093        }
094    
095        /**
096         * Guarantees that the argument Environment will have a present and fully
097         * qualified module ID when this method returns. If the Environment is
098         * missing a module ID, or has a partial module ID (isResolved() == false)
099         * 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    }