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    
041    /**
042     * 
043     * 
044     * @version $Rev: 476049 $ $Date: 2006-11-16 23:35:17 -0500 (Thu, 16 Nov 2006) $
045     */
046    public abstract class AbstractDeployable implements DeployableObject {
047        private final URL moduleURL;
048        private final ModuleType type;
049        private final DDBeanRoot root;
050        private final ClassLoader rootCL;
051        private final List entries;
052    
053        protected AbstractDeployable(ModuleType type, URL moduleURL, String rootDD) throws DDBeanCreateException {
054            this.type = type;
055            this.moduleURL = moduleURL;
056            rootCL = new URLClassLoader(new URL[] {moduleURL}, Thread.currentThread().getContextClassLoader());
057            root = new DDBeanRootImpl(this, rootCL.getResource(rootDD));
058    
059            // @todo make this work with unpacked
060            entries = new ArrayList();
061            InputStream is = null;
062            try {
063                is = moduleURL.openStream();
064                ZipInputStream zis = new ZipInputStream(new BufferedInputStream(is));
065                ZipEntry entry;
066                while ((entry = zis.getNextEntry()) != null) {
067                    entries.add(entry.getName());
068                }
069            } catch (IOException e) {
070                throw (DDBeanCreateException) new DDBeanCreateException("Unable to create list of entries").initCause(e);
071            } finally {
072                if (is != null) {
073                    try {
074                        is.close();
075                    } catch (IOException e1) {
076                        // ignore
077                    }
078                }
079            }
080        }
081    
082        public ModuleType getType() {
083            return type;
084        }
085    
086        public DDBeanRoot getDDBeanRoot() {
087            return root;
088        }
089    
090        public DDBeanRoot getDDBeanRoot(String filename) throws FileNotFoundException, DDBeanCreateException {
091            try {
092                return new DDBeanRootImpl(null, new URL(moduleURL, filename));
093            } catch (MalformedURLException e) {
094                throw (DDBeanCreateException) new DDBeanCreateException("Unable to construct URL for "+filename).initCause(e);
095            }
096        }
097    
098        public DDBean[] getChildBean(String xpath) {
099            return root.getChildBean(xpath);
100        }
101    
102        public String[] getText(String xpath) {
103            return root.getText(xpath);
104        }
105    
106        public Enumeration entries() {
107            return Collections.enumeration(entries);
108        }
109    
110        public InputStream getEntry(String name) {
111            return rootCL.getResourceAsStream(name);
112        }
113    
114        protected ClassLoader getModuleLoader() {
115            return rootCL;
116        }
117    
118        public Class getClassFromScope(String className) {
119            try {
120                return getModuleLoader().loadClass(className);
121            } catch (ClassNotFoundException e) {
122                // spec does not allow an Exception
123                return null;
124            }
125        }
126    
127        public String getModuleDTDVersion() {
128            throw new UnsupportedOperationException();
129        }
130    }