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    
018    package org.apache.geronimo.deployment.tools.loader;
019    
020    import java.io.BufferedInputStream;
021    import java.io.FileNotFoundException;
022    import java.io.IOException;
023    import java.io.InputStream;
024    import java.net.MalformedURLException;
025    import java.net.URL;
026    import java.net.URLClassLoader;
027    import java.util.ArrayList;
028    import java.util.Collections;
029    import java.util.Enumeration;
030    import java.util.List;
031    import java.util.zip.ZipEntry;
032    import java.util.zip.ZipInputStream;
033    import javax.enterprise.deploy.model.DDBean;
034    import javax.enterprise.deploy.model.DDBeanRoot;
035    import javax.enterprise.deploy.model.DeployableObject;
036    import javax.enterprise.deploy.model.exceptions.DDBeanCreateException;
037    import javax.enterprise.deploy.shared.ModuleType;
038    
039    import org.apache.geronimo.deployment.tools.DDBeanRootImpl;
040    import org.apache.geronimo.kernel.classloader.UrlResourceFinder;
041    import org.apache.geronimo.kernel.config.MultiParentClassLoader;
042    
043    /**
044     * 
045     * 
046     * @version $Rev: 706640 $ $Date: 2008-10-21 14:44:05 +0000 (Tue, 21 Oct 2008) $
047     */
048    public abstract class AbstractDeployable implements DeployableObject {
049        private final URL moduleURL;
050        private final ModuleType type;
051        private final DDBeanRoot root;
052        private final ClassLoader rootCL;
053        private final List entries;
054    
055        protected AbstractDeployable(ModuleType type, URL moduleURL, String rootDD) throws DDBeanCreateException {
056            this.type = type;
057            this.moduleURL = moduleURL;
058            rootCL = new URLClassLoader(new URL[] {moduleURL}, Thread.currentThread().getContextClassLoader());
059            UrlResourceFinder resourceFinder = new UrlResourceFinder(new URL[] {moduleURL});
060            root = new DDBeanRootImpl(this, resourceFinder.findResource(rootDD));                 
061    
062            // @todo make this work with unpacked
063            entries = new ArrayList();
064            InputStream is = null;
065            try {
066                is = moduleURL.openStream();
067                ZipInputStream zis = new ZipInputStream(new BufferedInputStream(is));
068                ZipEntry entry;
069                while ((entry = zis.getNextEntry()) != null) {
070                    entries.add(entry.getName());
071                }
072            } catch (IOException e) {
073                throw (DDBeanCreateException) new DDBeanCreateException("Unable to create list of entries").initCause(e);
074            } finally {
075                if (is != null) {
076                    try {
077                        is.close();
078                    } catch (IOException e1) {
079                        // ignore
080                    }
081                }
082            }
083        }
084    
085        public ModuleType getType() {
086            return type;
087        }
088    
089        public DDBeanRoot getDDBeanRoot() {
090            return root;
091        }
092    
093        public DDBeanRoot getDDBeanRoot(String filename) throws FileNotFoundException, DDBeanCreateException {
094            try {
095                return new DDBeanRootImpl(null, new URL(moduleURL, filename));
096            } catch (MalformedURLException e) {
097                throw (DDBeanCreateException) new DDBeanCreateException("Unable to construct URL for "+filename).initCause(e);
098            }
099        }
100    
101        public DDBean[] getChildBean(String xpath) {
102            return root.getChildBean(xpath);
103        }
104    
105        public String[] getText(String xpath) {
106            return root.getText(xpath);
107        }
108    
109        public Enumeration entries() {
110            return Collections.enumeration(entries);
111        }
112    
113        public InputStream getEntry(String name) {
114            return rootCL.getResourceAsStream(name);
115        }
116    
117        protected ClassLoader getModuleLoader() {
118            return rootCL;
119        }
120    
121        public Class getClassFromScope(String className) {
122            try {
123                return getModuleLoader().loadClass(className);
124            } catch (ClassNotFoundException e) {
125                // spec does not allow an Exception
126                return null;
127            }
128        }
129    
130        public String getModuleDTDVersion() {
131            throw new UnsupportedOperationException();
132        }
133    }