001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common Development
008: * and Distribution License("CDDL") (collectively, the "License"). You
009: * may not use this file except in compliance with the License. You can obtain
010: * a copy of the License at https://glassfish.dev.java.net/public/CDDL+GPL.html
011: * or glassfish/bootstrap/legal/LICENSE.txt. See the License for the specific
012: * language governing permissions and limitations under the License.
013: *
014: * When distributing the software, include this License Header Notice in each
015: * file and include the License file at glassfish/bootstrap/legal/LICENSE.txt.
016: * Sun designates this particular file as subject to the "Classpath" exception
017: * as provided by Sun in the GPL Version 2 section of the License file that
018: * accompanied this code. If applicable, add the following below the License
019: * Header, with the fields enclosed by brackets [] replaced by your own
020: * identifying information: "Portions Copyrighted [year]
021: * [name of copyright owner]"
022: *
023: * Contributor(s):
024: *
025: * If you wish your version of this file to be governed by only the CDDL or
026: * only the GPL Version 2, indicate your decision by adding "[Contributor]
027: * elects to include this software in this distribution under the [CDDL or GPL
028: * Version 2] license." If you don't indicate a single choice of license, a
029: * recipient has the option to distribute your version of this file under
030: * either the CDDL, the GPL Version 2 or to extend the choice of license to
031: * its licensees as provided above. However, if you add GPL Version 2 code
032: * and therefore, elected the GPL Version 2 license, then the option applies
033: * only if the new code is made subject to such option by the copyright
034: * holder.
035: */
036:
037: package com.sun.xml.ws.transport.tcp.server;
038:
039: import java.io.File;
040: import java.io.FilenameFilter;
041: import java.io.IOException;
042: import java.io.InputStream;
043: import java.net.URI;
044: import java.net.URL;
045: import java.util.Collections;
046: import java.util.Enumeration;
047: import java.util.HashMap;
048: import java.util.HashSet;
049: import java.util.Map;
050: import java.util.Set;
051: import java.util.zip.ZipEntry;
052: import java.util.zip.ZipFile;
053:
054: /**
055: * @author Alexey Stashok
056: */
057: public final class TCPStandaloneContext implements TCPContext {
058:
059: private final ClassLoader classloader;
060: private final Map<String, Object> attributes = new HashMap<String, Object>();
061:
062: public TCPStandaloneContext(final ClassLoader classloader) {
063: this .classloader = classloader;
064: }
065:
066: public InputStream getResourceAsStream(final String resource)
067: throws IOException {
068: return classloader.getResourceAsStream(resource);
069: }
070:
071: public Set<String> getResourcePaths(final String path) {
072: try {
073: return populateResourcePaths(path);
074: } catch (Exception ex) {
075: }
076:
077: return Collections.emptySet();
078: }
079:
080: public URL getResource(String resource) {
081: if (resource.charAt(0) == '/') {
082: resource = resource.substring(1, resource.length());
083: }
084:
085: return classloader.getResource(resource);
086: }
087:
088: private Enumeration<URL> getResources(String resource)
089: throws IOException {
090: if (resource.charAt(0) == '/') {
091: resource = resource.substring(1, resource.length());
092: }
093:
094: return classloader.getResources(resource);
095: }
096:
097: private Set<String> populateResourcePaths(final String path)
098: throws Exception {
099: final Set<String> resources = new HashSet<String>();
100:
101: for (final Enumeration<URL> initResources = getResources(path); initResources
102: .hasMoreElements();) {
103: final URI resourceURI = initResources.nextElement().toURI();
104: if (resourceURI.getScheme().equals("file")) {
105: gatherResourcesWithFileMode(path, resourceURI,
106: resources);
107: } else if (resourceURI.getScheme().equals("jar")) {
108: gatherResourcesWithJarMode(path, resourceURI, resources);
109: }
110: }
111:
112: return resources;
113: }
114:
115: private void gatherResourcesWithFileMode(final String path,
116: final URI resourceURI, final Set<String> resources) {
117: final File file = new File(resourceURI);
118: final String[] list = file.list(new FilenameFilter() {
119: public boolean accept(File file, String name) {
120: return name.charAt(0) != '.';
121: }
122: });
123:
124: for (String filename : list) {
125: resources.add(path + filename);
126: }
127: }
128:
129: private void gatherResourcesWithJarMode(final String path,
130: final URI resourceURI, final Set<String> resources) {
131: final String resourceURIAsString = resourceURI.toASCIIString();
132: final int pathDelim = resourceURIAsString.indexOf('!');
133: final String zipFile = resourceURIAsString.substring(
134: "jar:file:/".length(), (pathDelim != -1) ? pathDelim
135: : resourceURIAsString.length());
136: ZipFile file = null;
137:
138: try {
139: file = new ZipFile(zipFile);
140:
141: String pathToCompare = path;
142: if (pathToCompare.charAt(0) == '/') {
143: pathToCompare = pathToCompare.substring(1,
144: pathToCompare.length());
145: }
146: if (!pathToCompare.endsWith("/")) {
147: pathToCompare = pathToCompare + "/";
148: }
149:
150: for (final Enumeration<? extends ZipEntry> e = file
151: .entries(); e.hasMoreElements();) {
152: final ZipEntry entry = e.nextElement();
153: if (entry.getName().startsWith(pathToCompare)
154: && !entry.getName().equals(pathToCompare)) {
155: resources.add("/" + entry.getName());
156: }
157: }
158: } catch (IOException e) {
159: } finally {
160: if (file != null) {
161: try {
162: file.close();
163: } catch (IOException ex) {
164: }
165: }
166: }
167: }
168:
169: public Object getAttribute(final String name) {
170: return attributes.get(name);
171: }
172:
173: public void setAttribute(final String name, final Object value) {
174: attributes.put(name, value);
175: }
176: }
|