Marcel Offermans | e14b342 | 2009-11-25 23:04:32 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | * or more contributor license agreements. See the NOTICE file |
| 4 | * distributed with this work for additional information |
| 5 | * regarding copyright ownership. The ASF licenses this file |
| 6 | * to you under the Apache License, Version 2.0 (the |
| 7 | * "License"); you may not use this file except in compliance |
| 8 | * with the License. You may obtain a copy of the License at |
| 9 | * |
| 10 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | * |
| 12 | * Unless required by applicable law or agreed to in writing, |
| 13 | * software distributed under the License is distributed on an |
| 14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | * KIND, either express or implied. See the License for the |
| 16 | * specific language governing permissions and limitations |
| 17 | * under the License. |
| 18 | */ |
| 19 | package org.apache.felix.dependencymanager.resources; |
| 20 | |
| 21 | import java.io.IOException; |
| 22 | import java.io.InputStream; |
| 23 | import java.net.URL; |
| 24 | import java.util.Enumeration; |
| 25 | import java.util.Properties; |
| 26 | |
| 27 | import org.osgi.framework.Bundle; |
| 28 | import org.osgi.framework.Filter; |
| 29 | import org.osgi.framework.FrameworkUtil; |
| 30 | import org.osgi.framework.InvalidSyntaxException; |
| 31 | import org.osgi.framework.ServiceReference; |
| 32 | |
| 33 | public class BundleResourceRepository { |
| 34 | |
| 35 | private final Bundle bundle; |
| 36 | |
| 37 | public BundleResourceRepository(Bundle bundle) { |
| 38 | this.bundle = bundle; |
| 39 | } |
| 40 | |
| 41 | public synchronized void addHandler(ServiceReference ref, ResourceHandler handler) { |
| 42 | |
| 43 | String filter = (String) ref.getProperty("filter"); // "(&(repository=a)(path=b)(name=*.xml))" |
| 44 | |
| 45 | Filter filterObject = null; |
| 46 | |
| 47 | try { |
| 48 | filterObject = FrameworkUtil.createFilter(filter); |
| 49 | } catch (InvalidSyntaxException e) { |
| 50 | e.printStackTrace(); |
| 51 | return; |
| 52 | } |
| 53 | |
| 54 | Enumeration entries = bundle.findEntries("/", null, true); |
| 55 | while (entries.hasMoreElements()) { |
| 56 | URL entry = (URL) entries.nextElement(); |
| 57 | |
| 58 | Properties props = new Properties(); |
| 59 | props.setProperty(Resource.REPOSITORY, bundle.getSymbolicName() + "_" + bundle.getVersion()); |
| 60 | props.setProperty(Resource.PATH, entry.getPath()); |
| 61 | props.setProperty(Resource.NAME, entry.getFile()); |
| 62 | |
| 63 | if (filterObject.match(props)) |
| 64 | handler.added(new EntryResource(entry)); |
| 65 | |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | public synchronized void removeHandler(ServiceReference ref, ResourceHandler handler) { |
| 70 | |
| 71 | String filter = (String) ref.getProperty("filter"); // "(&(repository=a)(path=b)(name=*.xml))" |
| 72 | |
| 73 | Filter filterObject = null; |
| 74 | |
| 75 | try { |
| 76 | filterObject = FrameworkUtil.createFilter(filter); |
| 77 | } catch (InvalidSyntaxException e) { |
| 78 | e.printStackTrace(); |
| 79 | return; |
| 80 | } |
| 81 | |
| 82 | Enumeration entries = bundle.findEntries("/", null, true); |
| 83 | while (entries.hasMoreElements()) { |
| 84 | URL entry = (URL) entries.nextElement(); |
| 85 | |
| 86 | Properties props = new Properties(); |
| 87 | props.setProperty(Resource.REPOSITORY, bundle.getSymbolicName() + "_" + bundle.getVersion()); |
| 88 | props.setProperty(Resource.PATH, entry.getPath()); |
| 89 | props.setProperty(Resource.NAME, entry.getFile()); |
| 90 | |
| 91 | if (filterObject.match(props)) |
| 92 | handler.removed(new EntryResource(entry)); |
| 93 | |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | class EntryResource implements Resource { |
| 98 | |
| 99 | URL entry; |
| 100 | |
| 101 | EntryResource(URL entry) { |
| 102 | this.entry = entry; |
| 103 | } |
| 104 | |
| 105 | public String getName() { |
| 106 | return entry.getFile(); |
| 107 | } |
| 108 | |
| 109 | public String getPath() { |
| 110 | return entry.getPath(); |
| 111 | } |
| 112 | |
| 113 | public String getRepository() { |
| 114 | |
| 115 | return bundle.getSymbolicName() + "_" + bundle.getVersion(); |
| 116 | } |
| 117 | |
| 118 | public InputStream openStream() throws IOException { |
| 119 | return entry.openStream(); |
| 120 | } |
| 121 | } |
| 122 | } |