001    /**
002     *  Licensed to the Apache Software Foundation (ASF) under one or more
003     *  contributor license agreements.  See the NOTICE file distributed with
004     *  this work for additional information regarding copyright ownership.
005     *  The ASF licenses this file to You under the Apache License, Version 2.0
006     *  (the "License"); you may not use this file except in compliance with
007     *  the License.  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.j2ee.deployment;
018    
019    import java.util.jar.JarFile;
020    import java.util.Map;
021    import java.util.HashMap;
022    import java.net.URI;
023    
024    import org.apache.xmlbeans.XmlObject;
025    import org.apache.geronimo.kernel.config.ConfigurationModuleType;
026    import org.apache.geronimo.kernel.repository.Environment;
027    import org.apache.geronimo.kernel.repository.Artifact;
028    import org.apache.geronimo.deployment.util.DeploymentUtil;
029    import org.apache.geronimo.gbean.AbstractName;
030    import org.apache.geronimo.j2ee.deployment.annotation.AnnotatedApp;
031    import org.apache.xbean.finder.ClassFinder;
032    
033    /**
034     * @version $Rev: 538730 $ $Date: 2007-05-16 17:07:51 -0400 (Wed, 16 May 2007) $
035     */
036    public abstract class Module {
037        private final boolean standAlone;
038    
039        private final AbstractName moduleName;
040        private final String name;
041        private final Environment environment;
042        private final URI moduleURI;
043        private final JarFile moduleFile;
044        private final String targetPath;
045        private final URI targetPathURI;
046        private final XmlObject vendorDD;
047        private final String namespace;
048    
049        private EARContext earContext;
050        private EARContext rootEarContext;
051        private XmlObject specDD;
052        private String originalSpecDD;
053        private AnnotatedApp annotatedApp;
054        private ClassFinder classFinder;
055    
056        protected final Map sharedContext = new HashMap();
057    
058        protected Module(boolean standAlone, AbstractName moduleName, Environment environment, JarFile moduleFile, String targetPath, XmlObject specDD, XmlObject vendorDD, String originalSpecDD, String namespace, AnnotatedApp annotatedApp) {
059            assert targetPath != null: "targetPath is null";
060            assert moduleName != null: "moduleName is null";
061    
062            this.standAlone = standAlone;
063            this.moduleName = moduleName;
064            this.environment = environment;
065            this.moduleFile = moduleFile;
066            this.targetPath = targetPath;
067            this.specDD = specDD;
068            this.vendorDD = vendorDD;
069            this.originalSpecDD = originalSpecDD;
070            this.namespace = namespace;
071    
072            if (standAlone) {
073                name = environment.getConfigId().toString();
074                moduleURI = URI.create("");
075            } else {
076                name = targetPath;
077                moduleURI = URI.create(targetPath);
078            }
079    
080            targetPathURI = URI.create(targetPath + "/");
081            this.annotatedApp = annotatedApp;
082        }
083    
084        public abstract ConfigurationModuleType getType();
085    
086        public String getName() {
087            return name;
088        }
089    
090        public boolean isStandAlone() {
091            return standAlone;
092        }
093    
094        public AbstractName getModuleName() {
095            return moduleName;
096        }
097    
098        public Environment getEnvironment() {
099            return environment;
100        }
101    
102        public URI getModuleURI() {
103            return moduleURI;
104        }
105    
106        public JarFile getModuleFile() {
107            return moduleFile;
108        }
109    
110        public String getTargetPath() {
111            return targetPath;
112        }
113    
114        public URI getTargetPathURI() {
115            return targetPathURI;
116        }
117    
118        public XmlObject getSpecDD() {
119            return specDD;
120        }
121    
122        public XmlObject getVendorDD() {
123            return vendorDD;
124        }
125    
126        public String getOriginalSpecDD() {
127            return originalSpecDD;
128        }
129    
130        public String getNamespace() {
131            return namespace;
132        }
133    
134        public int hashCode() {
135            return name.hashCode();
136        }
137    
138        public boolean equals(Object obj) {
139            if (obj == this) {
140                return true;
141            }
142            if (obj instanceof Module) {
143                Module module = (Module) obj;
144                return name.equals(module.name);
145            }
146            return false;
147        }
148    
149        public void close() {
150            DeploymentUtil.close(moduleFile);
151        }
152    
153    
154        public EARContext getEarContext() {
155            return earContext;
156        }
157    
158        public void setEarContext(EARContext earContext) {
159            this.earContext = earContext;
160        }
161    
162        public EARContext getRootEarContext() {
163            return rootEarContext;
164        }
165    
166        public void setRootEarContext(EARContext rootEarContext) {
167            this.rootEarContext = rootEarContext;
168        }
169    
170        public Map getSharedContext() {
171            return sharedContext;
172        }
173    
174        public void setSpecDD(XmlObject specDD) {
175            this.specDD = specDD;
176        }
177    
178        public void setOriginalSpecDD(String originalSpecDD) {
179            this.originalSpecDD = originalSpecDD;
180        }
181    
182        public AnnotatedApp getAnnotatedApp() {
183            return annotatedApp;
184        }
185    
186        public void setAnnotatedApp(AnnotatedApp annotatedApp) {
187            this.annotatedApp = annotatedApp;
188        }
189    
190        public ClassFinder getClassFinder() {
191            return classFinder;
192        }
193    
194        public void setClassFinder(ClassFinder classFinder) {
195            this.classFinder = classFinder;
196        }
197    
198        public Artifact[] getConfigId() {
199            if (earContext == null) {
200                throw new NullPointerException("No ear context set");
201            }
202            if (rootEarContext == null || rootEarContext == earContext || rootEarContext.getConfigID().equals(earContext.getConfigID())) {
203                return new Artifact[] {earContext.getConfigID()};
204            }
205            return new Artifact[] {rootEarContext.getConfigID(), earContext.getConfigID()};
206        }
207    
208        /**
209         * Given a path in the ear module, return something that will resolve to that location against the eventual configuration
210         * base uri.  Currently for all modules except wars that is the original path.  If we create separate configurations for
211         * ejb or rar modules, those Module subclasses will need to reimplement this method.
212         *
213         * Example:  if a war is myweb.war, and you pass in myweb.war/WEB-INF/lib/foo.jar, you get WEB-INF/lib/foo.jar
214         * if you pass in myFoo.jar, you get ../myFoo.jar
215         *
216         * @param path a path in the ear config, relative to the ear root.
217         * @return a path to the same location, but relative to the configuration this module represents' base uri.
218         */
219        public String getRelativePath(String path) {
220            return path;
221        }
222        
223    }