[FELIX-4368] Work toward Repository 1.0 support
This commit registers the Repository Service in the OSGi service registry and also supports parsing a few more data types from the XML.
git-svn-id: https://svn.apache.org/repos/asf/felix/trunk@1564307 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/bundlerepository/src/main/java/org/apache/felix/bundlerepository/impl/Activator.java b/bundlerepository/src/main/java/org/apache/felix/bundlerepository/impl/Activator.java
index 160f065..0342cfc 100644
--- a/bundlerepository/src/main/java/org/apache/felix/bundlerepository/impl/Activator.java
+++ b/bundlerepository/src/main/java/org/apache/felix/bundlerepository/impl/Activator.java
@@ -20,11 +20,12 @@
import java.util.Hashtable;
+import org.apache.felix.bundlerepository.RepositoryAdmin;
import org.apache.felix.bundlerepository.impl.wrapper.Wrapper;
import org.apache.felix.utils.log.Logger;
import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;
-import org.apache.felix.bundlerepository.RepositoryAdmin;
+import org.osgi.service.repository.Repository;
import org.osgi.service.url.URLConstants;
import org.osgi.service.url.URLStreamHandlerService;
@@ -72,6 +73,11 @@
RepositoryAdmin.class.getName(),
m_repoAdmin, null);
+ // Register the OSGi Repository-spec compliant facade
+ context.registerService(
+ Repository.class.getName(),
+ new OSGiRepositoryImpl(m_repoAdmin), null);
+
try
{
context.registerService(
diff --git a/bundlerepository/src/main/java/org/apache/felix/bundlerepository/impl/FelixPropertyAdapter.java b/bundlerepository/src/main/java/org/apache/felix/bundlerepository/impl/FelixPropertyAdapter.java
new file mode 100644
index 0000000..d2aabe8
--- /dev/null
+++ b/bundlerepository/src/main/java/org/apache/felix/bundlerepository/impl/FelixPropertyAdapter.java
@@ -0,0 +1,78 @@
+/*
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.felix.bundlerepository.impl;
+
+import java.util.HashSet;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+
+import org.apache.felix.bundlerepository.Property;
+import org.osgi.framework.Version;
+
+ class FelixPropertyAdapter implements Property
+{
+ private static Set<?> asSet(List<?> list)
+ {
+ return new HashSet<Object>(list);
+ }
+
+ private final String name;
+ private final Object value;
+
+ public FelixPropertyAdapter(String name, Object value)
+ {
+ if (name == null)
+ throw new NullPointerException("Missing required parameter: name");
+ if (value == null)
+ throw new NullPointerException("Missing required parameter: value");
+ this.name = name;
+ this.value = value;
+ }
+
+ public FelixPropertyAdapter(Map.Entry<String, Object> entry)
+ {
+ this(entry.getKey(), entry.getValue());
+ }
+
+ public Object getConvertedValue()
+ {
+ if (value instanceof List)
+ return asSet((List<?>) value);
+ return value;
+ }
+
+ public String getName()
+ {
+ return name;
+ }
+
+ public String getType()
+ {
+ if (value instanceof Version)
+ return Property.VERSION;
+ if (value instanceof Long)
+ return Property.LONG;
+ if (value instanceof Double)
+ return Property.DOUBLE;
+ if (value instanceof List<?>)
+ return Property.SET;
+ return null;
+ }
+
+ public String getValue()
+ {
+ return String.valueOf(value);
+ }
+}
diff --git a/bundlerepository/src/main/java/org/apache/felix/bundlerepository/impl/OSGiCapabilityAdapter.java b/bundlerepository/src/main/java/org/apache/felix/bundlerepository/impl/OSGiCapabilityAdapter.java
index 21eb02a..78bf33e 100644
--- a/bundlerepository/src/main/java/org/apache/felix/bundlerepository/impl/OSGiCapabilityAdapter.java
+++ b/bundlerepository/src/main/java/org/apache/felix/bundlerepository/impl/OSGiCapabilityAdapter.java
@@ -16,14 +16,10 @@
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
-import java.util.HashSet;
-import java.util.List;
import java.util.Map;
-import java.util.Set;
import org.apache.felix.bundlerepository.Capability;
import org.apache.felix.bundlerepository.Property;
-import org.osgi.framework.Version;
public class OSGiCapabilityAdapter implements Capability
{
@@ -53,10 +49,10 @@
{
if (entry.getKey().equals(capability.getNamespace()))
{
- result.add(new FelixProperty(getName(), entry.getValue()));
+ result.add(new FelixPropertyAdapter(getName(), entry.getValue()));
continue;
}
- result.add(new FelixProperty(entry));
+ result.add(new FelixPropertyAdapter(entry));
}
return result.toArray(new Property[result.size()]);
}
@@ -73,60 +69,4 @@
{
return capability.hashCode();
}
-
- static class FelixProperty implements Property
- {
- private static Set<?> asSet(List<?> list)
- {
- return new HashSet<Object>(list);
- }
-
- private final String name;
- private final Object value;
-
- public FelixProperty(String name, Object value)
- {
- if (name == null)
- throw new NullPointerException("Missing required parameter: name");
- if (value == null)
- throw new NullPointerException("Missing required parameter: value");
- this.name = name;
- this.value = value;
- }
-
- public FelixProperty(Map.Entry<String, Object> entry)
- {
- this(entry.getKey(), entry.getValue());
- }
-
- public Object getConvertedValue()
- {
- if (value instanceof List)
- return asSet((List<?>) value);
- return value;
- }
-
- public String getName()
- {
- return name;
- }
-
- public String getType()
- {
- if (value instanceof Version)
- return Property.VERSION;
- if (value instanceof Long)
- return Property.LONG;
- if (value instanceof Double)
- return Property.DOUBLE;
- if (value instanceof List<?>)
- return Property.SET;
- return null;
- }
-
- public String getValue()
- {
- return String.valueOf(value);
- }
- }
}
diff --git a/bundlerepository/src/main/java/org/apache/felix/bundlerepository/impl/OSGiRepositoryImpl.java b/bundlerepository/src/main/java/org/apache/felix/bundlerepository/impl/OSGiRepositoryImpl.java
index 04473e4..2f75be7 100644
--- a/bundlerepository/src/main/java/org/apache/felix/bundlerepository/impl/OSGiRepositoryImpl.java
+++ b/bundlerepository/src/main/java/org/apache/felix/bundlerepository/impl/OSGiRepositoryImpl.java
@@ -46,7 +46,7 @@
import org.osgi.service.repository.ContentNamespace;
import org.osgi.service.repository.Repository;
-public class OSGiRepositoryImpl implements Repository
+class OSGiRepositoryImpl implements Repository
{
private final RepositoryAdmin repository;
diff --git a/bundlerepository/src/main/java/org/apache/felix/bundlerepository/impl/SpecXMLPullParser.java b/bundlerepository/src/main/java/org/apache/felix/bundlerepository/impl/SpecXMLPullParser.java
index 295f083..3700c17 100644
--- a/bundlerepository/src/main/java/org/apache/felix/bundlerepository/impl/SpecXMLPullParser.java
+++ b/bundlerepository/src/main/java/org/apache/felix/bundlerepository/impl/SpecXMLPullParser.java
@@ -25,6 +25,7 @@
import org.apache.felix.bundlerepository.Capability;
import org.apache.felix.bundlerepository.Requirement;
import org.apache.felix.bundlerepository.Resource;
+import org.osgi.framework.Version;
import org.osgi.framework.namespace.IdentityNamespace;
import org.osgi.resource.Namespace;
import org.osgi.service.repository.ContentNamespace;
@@ -140,7 +141,7 @@
for (Map.Entry<String, Object> entry : attributes.entrySet())
{
- capability.addProperty(entry.getKey(), "" + entry.getValue()); // TODO handle non-string data types
+ capability.addProperty(new FelixPropertyAdapter(entry.getKey(), entry.getValue()));
}
return capability;
@@ -186,9 +187,9 @@
if (ATTRIBUTE.equals(element))
{
String name = reader.getAttributeValue(null, "name");
- String type = reader.getAttributeValue(null, "type"); // TODO handle
+ String type = reader.getAttributeValue(null, "type");
String value = reader.getAttributeValue(null, "value");
- attributes.put(name, value);
+ attributes.put(name, getTypedValue(type, value));
PullParser.sanityCheckEndElement(reader, reader.nextTag(), ATTRIBUTE);
}
else
@@ -199,6 +200,21 @@
PullParser.sanityCheckEndElement(reader, event, parentTag);
}
+ private static Object getTypedValue(String type, String value)
+ {
+ if (type == null)
+ return value;
+
+ type = type.trim();
+ if ("Version".equals(type))
+ return Version.parseVersion(value);
+ else if ("Long".equals(type))
+ return Long.parseLong(value);
+ else if ("Double".equals(type))
+ return Double.parseDouble(value);
+ return value;
+ }
+
private static Requirement parseRequirement(XmlPullParser reader) throws Exception
{
RequirementImpl requirement = new RequirementImpl();