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.commons.vfs.impl;
018:
019: import org.apache.commons.vfs.FileObject;
020: import org.apache.commons.vfs.FileSystemException;
021: import org.apache.commons.vfs.FileUtil;
022:
023: import java.io.IOException;
024: import java.net.URL;
025: import java.util.jar.Attributes;
026:
027: /**
028: * Helper class for VFSClassLoader. This represents a resource loaded with
029: * the classloader.
030: *
031: * @author <a href="mailto:brian@mmmanager.org">Brian Olsen</a>
032: * @version $Revision: 480428 $ $Date: 2006-11-28 22:15:24 -0800 (Tue, 28 Nov 2006) $
033: * @see VFSClassLoader
034: */
035: class Resource {
036: private final FileObject root;
037: private final FileObject resource;
038: private final FileObject packageFolder;
039: private final String packageName;
040:
041: /**
042: * Creates a new instance.
043: *
044: * @param root The code source FileObject.
045: * @param resource The resource of the FileObject.
046: */
047: public Resource(final String name, final FileObject root,
048: final FileObject resource) throws FileSystemException {
049: this .root = root;
050: this .resource = resource;
051: packageFolder = resource.getParent();
052: final int pos = name.lastIndexOf('/');
053: if (pos == -1) {
054: packageName = null;
055: } else {
056: packageName = name.substring(0, pos).replace('/', '.');
057: }
058: }
059:
060: /**
061: * Returns the URL of the resource.
062: */
063: public URL getURL() throws FileSystemException {
064: return resource.getURL();
065: }
066:
067: /**
068: * Returns the name of the package containing the resource.
069: */
070: public String getPackageName() {
071: return packageName;
072: }
073:
074: /**
075: * Returns an attribute of the package containing the resource.
076: */
077: public String getPackageAttribute(final Attributes.Name attrName)
078: throws FileSystemException {
079: return (String) packageFolder.getContent().getAttribute(
080: attrName.toString());
081: }
082:
083: /**
084: * Returns the folder for the package containing the resource.
085: */
086: public FileObject getPackageFolder() {
087: return packageFolder;
088: }
089:
090: /**
091: * Returns the FileObject of the resource.
092: */
093: public FileObject getFileObject() {
094: return resource;
095: }
096:
097: /**
098: * Returns the code source as an URL.
099: */
100: public URL getCodeSourceURL() throws FileSystemException {
101: return root.getURL();
102: }
103:
104: /**
105: * Returns the data for this resource as a byte array.
106: */
107: public byte[] getBytes() throws IOException {
108: return FileUtil.getContent(resource);
109: }
110: }
|