001 /* 002 * Licensed to the Apache Software Foundation (ASF) under one 003 * or more contributor license agreements. See the NOTICE file 004 * distributed with this work for additional information 005 * regarding copyright ownership. The ASF licenses this file 006 * to you under the Apache License, Version 2.0 (the 007 * "License"); you may not use this file except in compliance 008 * with the License. You may obtain a copy of the License at 009 * 010 * http://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, 013 * software distributed under the License is distributed on an 014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 015 * KIND, either express or implied. See the License for the 016 * specific language governing permissions and limitations 017 * under the License. 018 */ 019 package org.apache.felix.framework.cache; 020 021 import org.apache.felix.moduleloader.*; 022 import java.io.IOException; 023 import java.io.InputStream; 024 import java.util.Enumeration; 025 import java.util.NoSuchElementException; 026 027 public class ContentDirectoryContent implements IContent 028 { 029 private IContent m_content = null; 030 private String m_rootPath = null; 031 032 public ContentDirectoryContent(IContent content, String path) 033 { 034 m_content = content; 035 // Add a '/' to the end if not present. 036 m_rootPath = (path.length() > 0) && (path.charAt(path.length() - 1) != '/') 037 ? path + "/" : path; 038 } 039 040 public synchronized void close() 041 { 042 // We do not actually close the associated content 043 // from which we are filtering our directory because 044 // we assume that this will be close manually by 045 // the owner of that content. 046 m_content = null; 047 } 048 049 public synchronized boolean hasEntry(String name) throws IllegalStateException 050 { 051 if ((name.length() > 0) && (name.charAt(0) == '/')) 052 { 053 name = name.substring(1); 054 } 055 056 return m_content.hasEntry(m_rootPath + name); 057 } 058 059 public synchronized Enumeration getEntries() 060 { 061 return new EntriesEnumeration(m_content.getEntries(), m_rootPath); 062 } 063 064 public synchronized byte[] getEntryAsBytes(String name) throws IllegalStateException 065 { 066 if ((name.length() > 0) && (name.charAt(0) == '/')) 067 { 068 name = name.substring(1); 069 } 070 071 return m_content.getEntryAsBytes(m_rootPath + name); 072 } 073 074 public synchronized InputStream getEntryAsStream(String name) 075 throws IllegalStateException, IOException 076 { 077 if ((name.length() > 0) && (name.charAt(0) == '/')) 078 { 079 name = name.substring(1); 080 } 081 082 return m_content.getEntryAsStream(m_rootPath + name); 083 } 084 085 public IContent getEntryAsContent(String name) 086 { 087 if ((name.length() > 0) && (name.charAt(0) == '/')) 088 { 089 name = name.substring(1); 090 } 091 092 return m_content.getEntryAsContent(m_rootPath + name); 093 } 094 095 public String getEntryAsNativeLibrary(String name) 096 { 097 if ((name.length() > 0) && (name.charAt(0) == '/')) 098 { 099 name = name.substring(1); 100 } 101 102 return m_content.getEntryAsNativeLibrary(m_rootPath + name); 103 } 104 105 public String toString() 106 { 107 return "CONTENT DIR " + m_rootPath + " (" + m_content + ")"; 108 } 109 110 private static class EntriesEnumeration implements Enumeration 111 { 112 private Enumeration m_enumeration = null; 113 private String m_rootPath = null; 114 private String m_nextEntry = null; 115 116 public EntriesEnumeration(Enumeration enumeration, String rootPath) 117 { 118 m_enumeration = enumeration; 119 m_rootPath = rootPath; 120 m_nextEntry = findNextEntry(); 121 } 122 123 public boolean hasMoreElements() 124 { 125 return (m_nextEntry != null); 126 } 127 128 public Object nextElement() 129 { 130 if (m_nextEntry == null) 131 { 132 throw new NoSuchElementException("No more elements."); 133 } 134 String currentEntry = m_nextEntry; 135 m_nextEntry = findNextEntry(); 136 return currentEntry; 137 } 138 139 private String findNextEntry() 140 { 141 // Find next entry that is inside the root directory. 142 while (m_enumeration.hasMoreElements()) 143 { 144 String next = (String) m_enumeration.nextElement(); 145 if (next.startsWith(m_rootPath) && !next.equals(m_rootPath)) 146 { 147 // Strip off the root directory. 148 return next.substring(m_rootPath.length()); 149 } 150 } 151 return null; 152 } 153 } 154 }