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