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 | */ |
Pierre De Rop | 6f839d1 | 2009-12-04 22:24:01 +0000 | [diff] [blame] | 19 | package org.apache.felix.dm.resources; |
Marcel Offermans | e14b342 | 2009-11-25 23:04:32 +0000 | [diff] [blame] | 20 | |
| 21 | import java.io.IOException; |
| 22 | import java.io.InputStream; |
| 23 | import java.net.URL; |
Marcel Offermans | 54f86e0 | 2009-12-21 10:18:28 +0000 | [diff] [blame] | 24 | import java.util.Dictionary; |
Marcel Offermans | e14b342 | 2009-11-25 23:04:32 +0000 | [diff] [blame] | 25 | import java.util.Enumeration; |
Marcel Offermans | e14b342 | 2009-11-25 23:04:32 +0000 | [diff] [blame] | 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 { |
Marcel Offermans | 54f86e0 | 2009-12-21 10:18:28 +0000 | [diff] [blame] | 34 | private final Bundle m_bundle; |
Marcel Offermans | a66b9bf | 2009-12-21 15:08:31 +0000 | [diff] [blame] | 35 | |
Marcel Offermans | e14b342 | 2009-11-25 23:04:32 +0000 | [diff] [blame] | 36 | public BundleResourceRepository(Bundle bundle) { |
Marcel Offermans | a66b9bf | 2009-12-21 15:08:31 +0000 | [diff] [blame] | 37 | m_bundle = bundle; |
Marcel Offermans | e14b342 | 2009-11-25 23:04:32 +0000 | [diff] [blame] | 38 | } |
| 39 | |
| 40 | public synchronized void addHandler(ServiceReference ref, ResourceHandler handler) { |
Marcel Offermans | e14b342 | 2009-11-25 23:04:32 +0000 | [diff] [blame] | 41 | String filter = (String) ref.getProperty("filter"); // "(&(repository=a)(path=b)(name=*.xml))" |
Marcel Offermans | e14b342 | 2009-11-25 23:04:32 +0000 | [diff] [blame] | 42 | Filter filterObject = null; |
Marcel Offermans | af8fb27 | 2010-05-18 15:25:32 +0000 | [diff] [blame] | 43 | if (filter != null) { |
| 44 | try { |
| 45 | filterObject = FrameworkUtil.createFilter(filter); |
| 46 | } |
| 47 | catch (InvalidSyntaxException e) { |
| 48 | e.printStackTrace(); |
| 49 | return; |
| 50 | } |
Marcel Offermans | e14b342 | 2009-11-25 23:04:32 +0000 | [diff] [blame] | 51 | } |
Marcel Offermans | 54f86e0 | 2009-12-21 10:18:28 +0000 | [diff] [blame] | 52 | Enumeration entries = m_bundle.findEntries("/", null, true); |
Marcel Offermans | e14b342 | 2009-11-25 23:04:32 +0000 | [diff] [blame] | 53 | while (entries.hasMoreElements()) { |
Marcel Offermans | 54f86e0 | 2009-12-21 10:18:28 +0000 | [diff] [blame] | 54 | EntryResource resource = new EntryResource(m_bundle, (URL) entries.nextElement()); |
Marcel Offermans | af8fb27 | 2010-05-18 15:25:32 +0000 | [diff] [blame] | 55 | if (filterObject == null || filterObject.match(resource)) { |
Marcel Offermans | 54f86e0 | 2009-12-21 10:18:28 +0000 | [diff] [blame] | 56 | handler.added(resource); |
| 57 | } |
Marcel Offermans | e14b342 | 2009-11-25 23:04:32 +0000 | [diff] [blame] | 58 | } |
| 59 | } |
| 60 | |
| 61 | public synchronized void removeHandler(ServiceReference ref, ResourceHandler handler) { |
Marcel Offermans | e14b342 | 2009-11-25 23:04:32 +0000 | [diff] [blame] | 62 | String filter = (String) ref.getProperty("filter"); // "(&(repository=a)(path=b)(name=*.xml))" |
Marcel Offermans | e14b342 | 2009-11-25 23:04:32 +0000 | [diff] [blame] | 63 | Filter filterObject = null; |
Marcel Offermans | af8fb27 | 2010-05-18 15:25:32 +0000 | [diff] [blame] | 64 | if (filter != null) { |
| 65 | try { |
| 66 | filterObject = FrameworkUtil.createFilter(filter); |
| 67 | } |
| 68 | catch (InvalidSyntaxException e) { |
| 69 | e.printStackTrace(); |
| 70 | return; |
| 71 | } |
Marcel Offermans | e14b342 | 2009-11-25 23:04:32 +0000 | [diff] [blame] | 72 | } |
Marcel Offermans | 54f86e0 | 2009-12-21 10:18:28 +0000 | [diff] [blame] | 73 | Enumeration entries = m_bundle.findEntries("/", null, true); |
| 74 | while (entries.hasMoreElements()) { |
| 75 | EntryResource resource = new EntryResource(m_bundle, (URL) entries.nextElement()); |
Marcel Offermans | af8fb27 | 2010-05-18 15:25:32 +0000 | [diff] [blame] | 76 | if (filterObject == null || filterObject.match(resource)) { |
Marcel Offermans | 54f86e0 | 2009-12-21 10:18:28 +0000 | [diff] [blame] | 77 | handler.removed(resource); |
| 78 | } |
| 79 | } |
Marcel Offermans | e14b342 | 2009-11-25 23:04:32 +0000 | [diff] [blame] | 80 | } |
| 81 | |
Marcel Offermans | 54f86e0 | 2009-12-21 10:18:28 +0000 | [diff] [blame] | 82 | static class EntryResource extends Dictionary implements Resource { |
| 83 | private final URL m_entry; |
| 84 | private final String m_id; |
| 85 | private final String m_repository; |
| 86 | private final String m_path; |
| 87 | private final String m_name; |
| 88 | private static Object[] m_keys; |
| 89 | private Object[] m_values; |
Marcel Offermans | e14b342 | 2009-11-25 23:04:32 +0000 | [diff] [blame] | 90 | |
Marcel Offermans | 54f86e0 | 2009-12-21 10:18:28 +0000 | [diff] [blame] | 91 | public EntryResource(Bundle bundle, URL entry) { |
| 92 | m_entry = entry; |
| 93 | // TODO is this unique? can we have the same url in more than one repository? |
| 94 | m_id = m_entry.toString(); |
| 95 | m_repository = bundle.getSymbolicName() + "_" + bundle.getHeaders().get("Bundle-Version"); |
Marcel Offermans | a66b9bf | 2009-12-21 15:08:31 +0000 | [diff] [blame] | 96 | String path = entry.getPath(); |
| 97 | int i = path.lastIndexOf('/'); |
| 98 | if (i == -1) { |
| 99 | m_path = "/"; |
| 100 | m_name = path; |
| 101 | } |
| 102 | else { |
| 103 | if (path.length() > (i + 1)) { |
| 104 | m_path = path.substring(0, i); |
| 105 | m_name = path.substring(i + 1); |
| 106 | } |
| 107 | else { |
| 108 | m_path = path; |
| 109 | m_name = ""; |
| 110 | } |
| 111 | } |
Marcel Offermans | e14b342 | 2009-11-25 23:04:32 +0000 | [diff] [blame] | 112 | } |
| 113 | |
Marcel Offermans | 54f86e0 | 2009-12-21 10:18:28 +0000 | [diff] [blame] | 114 | public final String getID() { |
| 115 | return m_id; |
| 116 | } |
| 117 | |
| 118 | public final String getName() { |
| 119 | return m_name; |
Marcel Offermans | e14b342 | 2009-11-25 23:04:32 +0000 | [diff] [blame] | 120 | } |
| 121 | |
Marcel Offermans | 54f86e0 | 2009-12-21 10:18:28 +0000 | [diff] [blame] | 122 | public final String getPath() { |
| 123 | return m_path; |
| 124 | } |
| 125 | |
| 126 | public final String getRepository() { |
| 127 | return m_repository; |
Marcel Offermans | e14b342 | 2009-11-25 23:04:32 +0000 | [diff] [blame] | 128 | } |
| 129 | |
Marcel Offermans | 54f86e0 | 2009-12-21 10:18:28 +0000 | [diff] [blame] | 130 | public final InputStream openStream() throws IOException { |
| 131 | return m_entry.openStream(); |
Marcel Offermans | e14b342 | 2009-11-25 23:04:32 +0000 | [diff] [blame] | 132 | } |
| 133 | |
Marcel Offermans | 54f86e0 | 2009-12-21 10:18:28 +0000 | [diff] [blame] | 134 | public Enumeration elements() { |
| 135 | if (m_values == null) { |
| 136 | m_values = new Object[] { m_id, m_repository, m_path, m_name }; |
| 137 | } |
| 138 | return new ArrayEnumeration(m_values); |
| 139 | } |
| 140 | |
| 141 | public Object get(Object key) { |
| 142 | if (Resource.ID.equals(key)) { |
| 143 | return m_id; |
| 144 | } |
| 145 | else if (Resource.REPOSITORY.equals(key)) { |
| 146 | return m_repository; |
| 147 | } |
| 148 | else if (Resource.PATH.equals(key)) { |
| 149 | return m_path; |
| 150 | } |
| 151 | else if (Resource.NAME.equals(key)) { |
| 152 | return m_name; |
| 153 | } |
| 154 | return null; |
| 155 | } |
| 156 | |
| 157 | public boolean isEmpty() { |
| 158 | return false; |
| 159 | } |
| 160 | |
| 161 | public Enumeration keys() { |
| 162 | if (m_keys == null) { |
| 163 | m_keys = new Object[] { Resource.ID, Resource.REPOSITORY, Resource.PATH, Resource.NAME }; |
| 164 | } |
| 165 | return new ArrayEnumeration(m_keys); |
| 166 | } |
| 167 | |
| 168 | public Object put(Object key, Object value) { |
| 169 | return null; |
| 170 | } |
| 171 | |
| 172 | public Object remove(Object key) { |
| 173 | return null; |
| 174 | } |
| 175 | |
| 176 | public int size() { |
| 177 | return 4; |
| 178 | } |
| 179 | } |
| 180 | |
| 181 | static class ArrayEnumeration implements Enumeration { |
| 182 | private int m_counter = 0; |
| 183 | private Object[] m_elements; |
| 184 | |
| 185 | public ArrayEnumeration(Object[] array) { |
| 186 | m_elements = array; |
| 187 | } |
| 188 | |
| 189 | public boolean hasMoreElements() { |
| 190 | return (m_counter < m_elements.length); |
| 191 | } |
| 192 | |
| 193 | public Object nextElement() { |
| 194 | return m_elements[m_counter++]; |
| 195 | } |
Marcel Offermans | e14b342 | 2009-11-25 23:04:32 +0000 | [diff] [blame] | 196 | } |
| 197 | } |