blob: 82b103605fe036de1b14db4c467553c5f4f9082c [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 Offermanse14b3422009-11-25 23:04:32 +000035
36 public BundleResourceRepository(Bundle bundle) {
Marcel Offermans54f86e02009-12-21 10:18:28 +000037 this.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 Offermanse14b3422009-11-25 23:04:32 +000043 try {
44 filterObject = FrameworkUtil.createFilter(filter);
Marcel Offermans54f86e02009-12-21 10:18:28 +000045 }
46 catch (InvalidSyntaxException e) {
Marcel Offermanse14b3422009-11-25 23:04:32 +000047 e.printStackTrace();
48 return;
49 }
Marcel Offermans54f86e02009-12-21 10:18:28 +000050 Enumeration entries = m_bundle.findEntries("/", null, true);
Marcel Offermanse14b3422009-11-25 23:04:32 +000051 while (entries.hasMoreElements()) {
Marcel Offermans54f86e02009-12-21 10:18:28 +000052 EntryResource resource = new EntryResource(m_bundle, (URL) entries.nextElement());
53 if (filterObject.match(resource)) {
54 handler.added(resource);
55 }
Marcel Offermanse14b3422009-11-25 23:04:32 +000056 }
57 }
58
59 public synchronized void removeHandler(ServiceReference ref, ResourceHandler handler) {
Marcel Offermanse14b3422009-11-25 23:04:32 +000060 String filter = (String) ref.getProperty("filter"); // "(&(repository=a)(path=b)(name=*.xml))"
Marcel Offermanse14b3422009-11-25 23:04:32 +000061 Filter filterObject = null;
Marcel Offermanse14b3422009-11-25 23:04:32 +000062 try {
63 filterObject = FrameworkUtil.createFilter(filter);
Marcel Offermans54f86e02009-12-21 10:18:28 +000064 }
65 catch (InvalidSyntaxException e) {
Marcel Offermanse14b3422009-11-25 23:04:32 +000066 e.printStackTrace();
67 return;
68 }
Marcel Offermans54f86e02009-12-21 10:18:28 +000069 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 Offermanse14b3422009-11-25 23:04:32 +000076 }
77
Marcel Offermans54f86e02009-12-21 10:18:28 +000078 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 Offermanse14b3422009-11-25 23:04:32 +000086
Marcel Offermans54f86e02009-12-21 10:18:28 +000087 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 Offermanse14b3422009-11-25 23:04:32 +000094 }
95
Marcel Offermans54f86e02009-12-21 10:18:28 +000096 public final String getID() {
97 return m_id;
98 }
99
100 public final String getName() {
101 return m_name;
Marcel Offermanse14b3422009-11-25 23:04:32 +0000102 }
103
Marcel Offermans54f86e02009-12-21 10:18:28 +0000104 public final String getPath() {
105 return m_path;
106 }
107
108 public final String getRepository() {
109 return m_repository;
Marcel Offermanse14b3422009-11-25 23:04:32 +0000110 }
111
Marcel Offermans54f86e02009-12-21 10:18:28 +0000112 public final InputStream openStream() throws IOException {
113 return m_entry.openStream();
Marcel Offermanse14b3422009-11-25 23:04:32 +0000114 }
115
Marcel Offermans54f86e02009-12-21 10:18:28 +0000116 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 Offermanse14b3422009-11-25 23:04:32 +0000178 }
179}