blob: 53b5d678b478af8e8129be973a80222603148db6 [file] [log] [blame]
Marcel Offermanse14b3422009-11-25 23:04:32 +00001/*
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 Rop6f839d12009-12-04 22:24:01 +000019package org.apache.felix.dm.resources;
Marcel Offermanse14b3422009-11-25 23:04:32 +000020
21import java.io.IOException;
22import java.io.InputStream;
23import java.net.URL;
Marcel Offermans54f86e02009-12-21 10:18:28 +000024import java.util.Dictionary;
Marcel Offermanse14b3422009-11-25 23:04:32 +000025import java.util.Enumeration;
Marcel Offermanse14b3422009-11-25 23:04:32 +000026
27import org.osgi.framework.Bundle;
28import org.osgi.framework.Filter;
29import org.osgi.framework.FrameworkUtil;
30import org.osgi.framework.InvalidSyntaxException;
31import org.osgi.framework.ServiceReference;
32
33public class BundleResourceRepository {
Marcel Offermans54f86e02009-12-21 10:18:28 +000034 private final Bundle m_bundle;
Marcel Offermansa66b9bf2009-12-21 15:08:31 +000035
Marcel Offermanse14b3422009-11-25 23:04:32 +000036 public BundleResourceRepository(Bundle bundle) {
Marcel Offermansa66b9bf2009-12-21 15:08:31 +000037 m_bundle = bundle;
Marcel Offermanse14b3422009-11-25 23:04:32 +000038 }
39
40 public synchronized void addHandler(ServiceReference ref, ResourceHandler handler) {
Marcel Offermanse14b3422009-11-25 23:04:32 +000041 String filter = (String) ref.getProperty("filter"); // "(&(repository=a)(path=b)(name=*.xml))"
Marcel Offermanse14b3422009-11-25 23:04:32 +000042 Filter filterObject = null;
Marcel Offermansaf8fb272010-05-18 15:25:32 +000043 if (filter != null) {
44 try {
45 filterObject = FrameworkUtil.createFilter(filter);
46 }
47 catch (InvalidSyntaxException e) {
48 e.printStackTrace();
49 return;
50 }
Marcel Offermanse14b3422009-11-25 23:04:32 +000051 }
Marcel Offermans54f86e02009-12-21 10:18:28 +000052 Enumeration entries = m_bundle.findEntries("/", null, true);
Marcel Offermanse14b3422009-11-25 23:04:32 +000053 while (entries.hasMoreElements()) {
Marcel Offermans54f86e02009-12-21 10:18:28 +000054 EntryResource resource = new EntryResource(m_bundle, (URL) entries.nextElement());
Marcel Offermansaf8fb272010-05-18 15:25:32 +000055 if (filterObject == null || filterObject.match(resource)) {
Marcel Offermans54f86e02009-12-21 10:18:28 +000056 handler.added(resource);
57 }
Marcel Offermanse14b3422009-11-25 23:04:32 +000058 }
59 }
60
61 public synchronized void removeHandler(ServiceReference ref, ResourceHandler handler) {
Marcel Offermanse14b3422009-11-25 23:04:32 +000062 String filter = (String) ref.getProperty("filter"); // "(&(repository=a)(path=b)(name=*.xml))"
Marcel Offermanse14b3422009-11-25 23:04:32 +000063 Filter filterObject = null;
Marcel Offermansaf8fb272010-05-18 15:25:32 +000064 if (filter != null) {
65 try {
66 filterObject = FrameworkUtil.createFilter(filter);
67 }
68 catch (InvalidSyntaxException e) {
69 e.printStackTrace();
70 return;
71 }
Marcel Offermanse14b3422009-11-25 23:04:32 +000072 }
Marcel Offermans54f86e02009-12-21 10:18:28 +000073 Enumeration entries = m_bundle.findEntries("/", null, true);
74 while (entries.hasMoreElements()) {
75 EntryResource resource = new EntryResource(m_bundle, (URL) entries.nextElement());
Marcel Offermansaf8fb272010-05-18 15:25:32 +000076 if (filterObject == null || filterObject.match(resource)) {
Marcel Offermans54f86e02009-12-21 10:18:28 +000077 handler.removed(resource);
78 }
79 }
Marcel Offermanse14b3422009-11-25 23:04:32 +000080 }
81
Marcel Offermans54f86e02009-12-21 10:18:28 +000082 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 Offermanse14b3422009-11-25 23:04:32 +000090
Marcel Offermans54f86e02009-12-21 10:18:28 +000091 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 Offermansa66b9bf2009-12-21 15:08:31 +000096 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 Offermanse14b3422009-11-25 23:04:32 +0000112 }
113
Marcel Offermans54f86e02009-12-21 10:18:28 +0000114 public final String getID() {
115 return m_id;
116 }
117
118 public final String getName() {
119 return m_name;
Marcel Offermanse14b3422009-11-25 23:04:32 +0000120 }
121
Marcel Offermans54f86e02009-12-21 10:18:28 +0000122 public final String getPath() {
123 return m_path;
124 }
125
126 public final String getRepository() {
127 return m_repository;
Marcel Offermanse14b3422009-11-25 23:04:32 +0000128 }
129
Marcel Offermans54f86e02009-12-21 10:18:28 +0000130 public final InputStream openStream() throws IOException {
131 return m_entry.openStream();
Marcel Offermanse14b3422009-11-25 23:04:32 +0000132 }
133
Marcel Offermans54f86e02009-12-21 10:18:28 +0000134 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 Offermanse14b3422009-11-25 23:04:32 +0000196 }
197}