KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > geronimo > system > repository > AbstractRepository


1 /**
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17 package org.apache.geronimo.system.repository;
18
19 import java.io.File JavaDoc;
20 import java.io.FileInputStream JavaDoc;
21 import java.io.IOException JavaDoc;
22 import java.io.InputStream JavaDoc;
23 import java.net.MalformedURLException JavaDoc;
24 import java.net.URI JavaDoc;
25 import java.net.URISyntaxException JavaDoc;
26 import java.net.URL JavaDoc;
27 import java.net.URLClassLoader JavaDoc;
28 import java.util.Enumeration JavaDoc;
29 import java.util.LinkedHashSet JavaDoc;
30 import java.util.Map JavaDoc;
31 import java.util.HashMap JavaDoc;
32 import java.util.zip.ZipEntry JavaDoc;
33 import java.util.zip.ZipException JavaDoc;
34 import java.util.zip.ZipFile JavaDoc;
35 import javax.xml.parsers.DocumentBuilderFactory JavaDoc;
36 import javax.xml.parsers.ParserConfigurationException JavaDoc;
37
38 import org.apache.commons.logging.Log;
39 import org.apache.commons.logging.LogFactory;
40 import org.apache.geronimo.kernel.repository.Artifact;
41 import org.apache.geronimo.kernel.repository.ArtifactTypeHandler;
42 import org.apache.geronimo.kernel.repository.FileWriteMonitor;
43 import org.apache.geronimo.kernel.repository.WriteableRepository;
44 import org.apache.geronimo.kernel.util.XmlUtil;
45 import org.apache.geronimo.system.serverinfo.ServerInfo;
46 import org.w3c.dom.Document JavaDoc;
47 import org.w3c.dom.Element JavaDoc;
48 import org.w3c.dom.Node JavaDoc;
49 import org.w3c.dom.NodeList JavaDoc;
50 import org.xml.sax.InputSource JavaDoc;
51 import org.xml.sax.SAXException JavaDoc;
52
53 /**
54  * @version $Rev: 476049 $ $Date: 2006-11-16 23:35:17 -0500 (Thu, 16 Nov 2006) $
55  */

56 public abstract class AbstractRepository implements WriteableRepository {
57     protected static final Log log = LogFactory.getLog(AbstractRepository.class);
58     private final static ArtifactTypeHandler DEFAULT_TYPE_HANDLER = new CopyArtifactTypeHandler();
59     protected final File JavaDoc rootFile;
60     private final Map JavaDoc typeHandlers = new HashMap JavaDoc();
61
62     public AbstractRepository(URI JavaDoc root, ServerInfo serverInfo) {
63         this(resolveRoot(root, serverInfo));
64     }
65
66     public AbstractRepository(File JavaDoc rootFile) {
67         if (rootFile == null) throw new NullPointerException JavaDoc("root is null");
68
69         if (!rootFile.exists() || !rootFile.isDirectory() || !rootFile.canRead()) {
70             throw new IllegalStateException JavaDoc("Maven2Repository must have a root that's a valid readable directory (not " + rootFile.getAbsolutePath() + ")");
71         }
72
73         this.rootFile = rootFile;
74         log.debug("Repository root is " + rootFile.getAbsolutePath());
75
76         typeHandlers.put("car", new UnpackArtifactTypeHandler());
77     }
78
79     private static File JavaDoc resolveRoot(URI JavaDoc root, ServerInfo serverInfo) {
80         if (root == null) throw new NullPointerException JavaDoc("root is null");
81
82         if (!root.toString().endsWith("/")) {
83             try {
84                 root = new URI JavaDoc(root.toString() + "/");
85             } catch (URISyntaxException JavaDoc e) {
86                 throw new RuntimeException JavaDoc("Invalid repository root (does not end with / ) and can't add myself", e);
87             }
88         }
89
90         URI JavaDoc resolvedUri;
91         if (serverInfo != null) {
92             resolvedUri = serverInfo.resolve(root);
93         } else {
94             resolvedUri = root;
95         }
96
97         if (!resolvedUri.getScheme().equals("file")) {
98             throw new IllegalStateException JavaDoc("FileSystemRepository must have a root that's a local directory (not " + resolvedUri + ")");
99         }
100
101         File JavaDoc rootFile = new File JavaDoc(resolvedUri);
102         return rootFile;
103     }
104
105     public boolean contains(Artifact artifact) {
106         // Note: getLocation(artifact) does an artifact.isResolved() check - no need to do it here.
107
File JavaDoc location = getLocation(artifact);
108         return location.canRead() && (location.isFile() || new File JavaDoc(location, "META-INF").isDirectory());
109     }
110
111     private static final String JavaDoc NAMESPACE = "http://geronimo.apache.org/xml/ns/deployment-1.2";
112     public LinkedHashSet JavaDoc getDependencies(Artifact artifact) {
113         if(!artifact.isResolved()) {
114             throw new IllegalArgumentException JavaDoc("Artifact "+artifact+" is not fully resolved");
115         }
116         LinkedHashSet JavaDoc dependencies = new LinkedHashSet JavaDoc();
117         URL JavaDoc url;
118         try {
119             File JavaDoc location = getLocation(artifact);
120             url = location.toURL();
121         } catch (MalformedURLException JavaDoc e) {
122             throw (IllegalStateException JavaDoc)new IllegalStateException JavaDoc("Unable to get URL for dependency " + artifact).initCause(e);
123         }
124         ClassLoader JavaDoc depCL = new URLClassLoader JavaDoc(new URL JavaDoc[]{url}, ClassLoader.getSystemClassLoader());
125         InputStream JavaDoc is = depCL.getResourceAsStream("META-INF/geronimo-dependency.xml");
126         try {
127             if (is != null) {
128                 InputSource JavaDoc in = new InputSource JavaDoc(is);
129                 DocumentBuilderFactory JavaDoc dfactory = XmlUtil.newDocumentBuilderFactory();
130                 dfactory.setNamespaceAware(true);
131                 try {
132                     Document JavaDoc doc = dfactory.newDocumentBuilder().parse(in);
133                     Element JavaDoc root = doc.getDocumentElement();
134                     NodeList JavaDoc configs = root.getElementsByTagNameNS(NAMESPACE, "dependency");
135                     for (int i = 0; i < configs.getLength(); i++) {
136                         Element JavaDoc dependencyElement = (Element JavaDoc) configs.item(i);
137                         String JavaDoc groupId = getString(dependencyElement, "groupId");
138                         String JavaDoc artifactId = getString(dependencyElement, "artifactId");
139                         String JavaDoc version = getString(dependencyElement, "version");
140                         String JavaDoc type = getString(dependencyElement, "type");
141                         if (type == null) {
142                             type = "jar";
143                         }
144                         dependencies.add(new Artifact(groupId, artifactId, version, type));
145                     }
146                 } catch (IOException JavaDoc e) {
147                     throw (IllegalStateException JavaDoc)new IllegalStateException JavaDoc("Unable to parse geronimo-dependency.xml file in " + url).initCause(e);
148                 } catch (ParserConfigurationException JavaDoc e) {
149                     throw (IllegalStateException JavaDoc)new IllegalStateException JavaDoc("Unable to parse geronimo-dependency.xml file in " + url).initCause(e);
150                 } catch (SAXException JavaDoc e) {
151                     throw (IllegalStateException JavaDoc)new IllegalStateException JavaDoc("Unable to parse geronimo-dependency.xml file in " + url).initCause(e);
152                 }
153             }
154         } finally {
155             if (is != null) {
156                 try {
157                     is.close();
158                 } catch (IOException JavaDoc ignore) {
159                     // ignore
160
}
161             }
162         }
163         return dependencies;
164     }
165
166     private String JavaDoc getString(Element JavaDoc dependencyElement, String JavaDoc childName) {
167         NodeList JavaDoc children = dependencyElement.getElementsByTagNameNS(NAMESPACE, childName);
168         if (children == null || children.getLength() == 0) {
169         return null;
170         }
171         String JavaDoc value = "";
172         NodeList JavaDoc text = children.item(0).getChildNodes();
173         for (int t = 0; t < text.getLength(); t++) {
174             Node JavaDoc n = text.item(t);
175             if (n.getNodeType() == Node.TEXT_NODE) {
176                 value += n.getNodeValue();
177             }
178         }
179         return value.trim();
180     }
181
182     public void setTypeHandler(String JavaDoc type, ArtifactTypeHandler handler) {
183         typeHandlers.put(type, handler);
184     }
185
186     public void copyToRepository(File JavaDoc source, Artifact destination, FileWriteMonitor monitor) throws IOException JavaDoc {
187         if(!destination.isResolved()) {
188             throw new IllegalArgumentException JavaDoc("Artifact "+destination+" is not fully resolved");
189         }
190         if (!source.exists() || !source.canRead() || source.isDirectory()) {
191             throw new IllegalArgumentException JavaDoc("Cannot read source file at " + source.getAbsolutePath());
192         }
193         int size = 0;
194         try {
195             ZipFile JavaDoc zip = new ZipFile JavaDoc(source);
196             for (Enumeration JavaDoc entries=zip.entries(); entries.hasMoreElements();) {
197                 ZipEntry JavaDoc entry = (ZipEntry JavaDoc)entries.nextElement();
198                 size += entry.getSize();
199             }
200         } catch (ZipException JavaDoc ze) {
201             size = (int)source.length();
202         }
203         FileInputStream JavaDoc is = new FileInputStream JavaDoc(source);
204         try {
205             copyToRepository(is, size, destination, monitor);
206         } finally {
207             try {
208                 is.close();
209             } catch (IOException JavaDoc ignored) {
210                 // ignored
211
}
212         }
213     }
214
215     public void copyToRepository(InputStream JavaDoc source, int size, Artifact destination, FileWriteMonitor monitor) throws IOException JavaDoc {
216         if(!destination.isResolved()) {
217             throw new IllegalArgumentException JavaDoc("Artifact "+destination+" is not fully resolved");
218         }
219         // is this a writable repository
220
if (!rootFile.canWrite()) {
221             throw new IllegalStateException JavaDoc("This repository is not writable: " + rootFile.getAbsolutePath() + ")");
222         }
223
224         // where are we going to install the file
225
File JavaDoc location = getLocation(destination);
226
227         // assure that there isn't already a file installed at the specified location
228
if (location.exists()) {
229             throw new IllegalArgumentException JavaDoc("Destination " + location.getAbsolutePath() + " already exists!");
230         }
231
232         ArtifactTypeHandler typeHandler = (ArtifactTypeHandler) typeHandlers.get(destination.getType());
233         if (typeHandler == null) typeHandler = DEFAULT_TYPE_HANDLER;
234         typeHandler.install(source, size, destination, monitor, location);
235         
236         if (destination.getType().equalsIgnoreCase("car")) {
237             log.debug("Installed module configuration; id=" + destination + "; location=" + location);
238         }
239     }
240 }
241
Popular Tags