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