You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@felix.apache.org by gn...@apache.org on 2014/11/27 16:14:40 UTC
svn commit: r1642172 [2/4] - in /felix/trunk/connect: ./
src/main/java/org/apache/felix/connect/
src/main/java/org/apache/felix/connect/felix/framework/
src/main/java/org/apache/felix/connect/felix/framework/capabilityset/
src/main/java/org/apache/feli...
Modified: felix/trunk/connect/src/main/java/org/apache/felix/connect/PojoSRBundle.java
URL: http://svn.apache.org/viewvc/felix/trunk/connect/src/main/java/org/apache/felix/connect/PojoSRBundle.java?rev=1642172&r1=1642171&r2=1642172&view=diff
==============================================================================
--- felix/trunk/connect/src/main/java/org/apache/felix/connect/PojoSRBundle.java (original)
+++ felix/trunk/connect/src/main/java/org/apache/felix/connect/PojoSRBundle.java Thu Nov 27 15:14:39 2014
@@ -22,6 +22,7 @@ import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
+import java.security.cert.X509Certificate;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
@@ -29,7 +30,6 @@ import java.util.Collections;
import java.util.Dictionary;
import java.util.Enumeration;
import java.util.HashMap;
-import java.util.Iterator;
import java.util.List;
import java.util.Locale;
import java.util.Map;
@@ -51,21 +51,23 @@ import org.osgi.framework.wiring.BundleR
import org.osgi.framework.wiring.BundleRevisions;
import org.osgi.framework.wiring.BundleWire;
import org.osgi.framework.wiring.BundleWiring;
-
+import org.osgi.resource.Capability;
+import org.osgi.resource.Requirement;
+import org.osgi.resource.Wire;
import org.apache.felix.connect.felix.framework.ServiceRegistry;
import org.apache.felix.connect.felix.framework.util.EventDispatcher;
import org.apache.felix.connect.felix.framework.util.MapToDictionary;
import org.apache.felix.connect.felix.framework.util.StringMap;
-class PojoSRBundle implements Bundle, BundleRevisions, BundleRevision
+class PojoSRBundle implements Bundle, BundleRevisions
{
private final Revision m_revision;
- private final Map<String, String> m_manifest;
+ private final Map<String, String> m_headers;
private final Version m_version;
private final String m_location;
private final Map<Long, Bundle> m_bundles;
- private final ServiceRegistry m_reg;
+ private final ServiceRegistry m_registry;
private final String m_activatorClass;
private final long m_id;
private final String m_symbolicName;
@@ -73,45 +75,62 @@ class PojoSRBundle implements Bundle, Bu
volatile int m_state = Bundle.RESOLVED;
volatile BundleContext m_context = null;
private final EventDispatcher m_dispatcher;
- private final ClassLoader m_loader;
+ private final ClassLoader m_classLoader;
+ private final Map<Class, Object> m_services;
private final Map m_config;
- Revision getRevision()
- {
- return m_revision;
- }
-
- public PojoSRBundle(Revision revision, Map<String, String> manifest,
- Version version, String location, ServiceRegistry reg,
- EventDispatcher dispatcher, String activatorClass, long id,
- String symbolicName, Map<Long, Bundle> bundles, ClassLoader loader, Map config)
+ public PojoSRBundle(ServiceRegistry registry,
+ EventDispatcher dispatcher,
+ Map<Long, Bundle> bundles,
+ String location,
+ long id,
+ String symbolicName,
+ Version version,
+ Revision revision,
+ ClassLoader classLoader,
+ Map<String, String> headers,
+ Map<Class, Object> services,
+ Map<? extends Object, ? extends Object> config)
{
m_revision = revision;
- m_manifest = manifest;
+ m_headers = headers;
m_version = version;
m_location = location;
- m_reg = reg;
+ m_registry = registry;
m_dispatcher = dispatcher;
- m_activatorClass = activatorClass;
+ m_activatorClass = headers.get(Constants.BUNDLE_ACTIVATOR);
m_id = id;
m_symbolicName = symbolicName;
- bundles.put(m_id, this);
m_bundles = bundles;
- m_loader = loader;
+ m_classLoader = classLoader;
+ m_services = services;
m_config = config;
+ if (classLoader instanceof BundleAware) {
+ ((BundleAware) classLoader).setBundle(this);
+ }
+ if (services != null) {
+ for (Object o : services.values()) {
+ if (o instanceof BundleAware) {
+ ((BundleAware) o).setBundle(this);
+ }
+ }
+ }
}
+ @Override
public int getState()
{
return m_state;
}
+ @Override
public void start(int options) throws BundleException
{
// TODO: lifecycle - fix this
start();
}
+ @Override
public synchronized void start() throws BundleException
{
if (m_state != Bundle.RESOLVED)
@@ -126,36 +145,33 @@ class PojoSRBundle implements Bundle, Bu
{
m_state = Bundle.STARTING;
- m_context = new PojoSRBundleContext(this, m_reg, m_dispatcher,
- m_bundles, m_config);
- m_dispatcher.fireBundleEvent(new BundleEvent(BundleEvent.STARTING,
- this));
+ m_context = new PojoSRBundleContext(this, m_registry, m_dispatcher, m_bundles, m_config);
+ m_dispatcher.fireBundleEvent(new BundleEvent(BundleEvent.STARTING, this));
if (m_activatorClass != null)
{
- m_activator = (BundleActivator) m_loader.loadClass(
- m_activatorClass).newInstance();
+ m_activator = (BundleActivator) m_classLoader.loadClass(m_activatorClass).newInstance();
m_activator.start(m_context);
}
m_state = Bundle.ACTIVE;
- m_dispatcher.fireBundleEvent(new BundleEvent(BundleEvent.STARTED,
- this));
+ m_dispatcher.fireBundleEvent(new BundleEvent(BundleEvent.STARTED, this));
}
catch (Throwable ex)
{
m_state = Bundle.RESOLVED;
m_activator = null;
- m_dispatcher.fireBundleEvent(new BundleEvent(BundleEvent.STOPPED,
- this));
+ m_dispatcher.fireBundleEvent(new BundleEvent(BundleEvent.STOPPED, this));
throw new BundleException("Unable to start bundle", ex);
}
}
+ @Override
public void stop(int options) throws BundleException
{
// TODO: lifecycle - fix this
stop();
}
+ @Override
public synchronized void stop() throws BundleException
{
if (m_state != Bundle.ACTIVE)
@@ -182,7 +198,7 @@ class PojoSRBundle implements Bundle, Bu
}
finally
{
- m_reg.unregisterServices(this);
+ m_registry.unregisterServices(this);
m_dispatcher.removeListeners(m_context);
m_activator = null;
m_context = null;
@@ -192,72 +208,83 @@ class PojoSRBundle implements Bundle, Bu
}
}
+ @Override
public void update(InputStream input) throws BundleException
{
throw new BundleException("pojosr bundles can't be updated");
}
+ @Override
public void update() throws BundleException
{
throw new BundleException("pojosr bundles can't be updated");
}
+ @Override
public void uninstall() throws BundleException
{
throw new BundleException("pojosr bundles can't be uninstalled");
}
- public Dictionary getHeaders()
+ @Override
+ public Dictionary<String, String> getHeaders()
{
return getHeaders(Locale.getDefault().toString());
}
+ @Override
public long getBundleId()
{
return m_id;
}
+ @Override
public String getLocation()
{
return m_location;
}
- public ServiceReference[] getRegisteredServices()
+ @Override
+ public ServiceReference<?>[] getRegisteredServices()
{
- return m_reg.getRegisteredServices(this);
+ return m_registry.getRegisteredServices(this);
}
- public ServiceReference[] getServicesInUse()
+ @Override
+ public ServiceReference<?>[] getServicesInUse()
{
- return m_reg.getServicesInUse(this);
+ return m_registry.getServicesInUse(this);
}
+ @Override
public boolean hasPermission(Object permission)
{
// TODO: security - fix this
return true;
}
+ @Override
public URL getResource(String name)
{
// TODO: module - implement this based on the revision
- URL result = m_loader.getResource(name);
+ URL result = m_classLoader.getResource(name);
return result;
}
- public Dictionary getHeaders(String locale)
+ @Override
+ public Dictionary<String, String> getHeaders(String locale)
{
- return new MapToDictionary(getCurrentLocalizedHeader(locale));
+ return new MapToDictionary<String, String>(getCurrentLocalizedHeader(locale));
}
- Map getCurrentLocalizedHeader(String locale)
+ Map<String, String> getCurrentLocalizedHeader(String locale)
{
- Map result = null;
+ Map<String, String> result = null;
// Spec says empty local returns raw headers.
if ((locale == null) || (locale.length() == 0))
{
- result = new StringMap(m_manifest, false);
+ result = new StringMap<String>(m_headers, false);
}
// If we have no result, try to get it from the cached headers.
@@ -270,7 +297,7 @@ class PojoSRBundle implements Bundle, Bu
// the time of uninstall, so just return that.
if (getState() == Bundle.UNINSTALLED)
{
- result = (Map) m_cachedHeaders.values().iterator().next();
+ result = m_cachedHeaders.values().iterator().next();
}
// If the bundle has been updated, clear the cached headers.
else if (getLastModified() > m_cachedHeadersTimestamp)
@@ -280,12 +307,8 @@ class PojoSRBundle implements Bundle, Bu
// Otherwise, returned the cached headers if they exist.
else
{
- // Check if headers for this locale have already been
- // resolved
- if (m_cachedHeaders.containsKey(locale))
- {
- result = (Map) m_cachedHeaders.get(locale);
- }
+ // Check if headers for this locale have already been resolved
+ result = m_cachedHeaders.get(locale);
}
}
}
@@ -294,18 +317,18 @@ class PojoSRBundle implements Bundle, Bu
if (result == null)
{
// Get a modifiable copy of the raw headers.
- Map headers = new StringMap(m_manifest, false);
+ Map<String, String> headers = new StringMap<String>(m_headers, false);
// Assume for now that this will be the result.
result = headers;
// Check to see if we actually need to localize anything
boolean localize = false;
- for (Iterator it = headers.values().iterator(); !localize
- && it.hasNext();)
+ for (String s : headers.values())
{
- if (((String) it.next()).startsWith("%"))
+ if ((s).startsWith("%"))
{
localize = true;
+ break;
}
}
@@ -319,31 +342,28 @@ class PojoSRBundle implements Bundle, Bu
else
{
// Do localization here and return the localized headers
- String basename = (String) headers
- .get(Constants.BUNDLE_LOCALIZATION);
+ String basename = headers.get(Constants.BUNDLE_LOCALIZATION);
if (basename == null)
{
basename = Constants.BUNDLE_LOCALIZATION_DEFAULT_BASENAME;
}
// Create ordered list of files to load properties from
- List resourceList = createLocalizationResourceList(basename,
- locale);
+ List<String> resourceList = createLocalizationResourceList(basename, locale);
// Create a merged props file with all available props for this
// locale
boolean found = false;
Properties mergedProperties = new Properties();
- for (Iterator it = resourceList.iterator(); it.hasNext();)
+ for (String aResourceList : resourceList)
{
- URL temp = m_revision.getEntry(it.next() + ".properties");
+ URL temp = m_revision.getEntry(aResourceList + ".properties");
if (temp != null)
{
found = true;
try
{
- mergedProperties.load(temp.openConnection()
- .getInputStream());
+ mergedProperties.load(temp.openConnection().getInputStream());
}
catch (IOException ex)
{
@@ -357,24 +377,20 @@ class PojoSRBundle implements Bundle, Bu
// return the default localization.
if (!found && !locale.equals(Locale.getDefault().toString()))
{
- result = getCurrentLocalizedHeader(Locale.getDefault()
- .toString());
+ result = getCurrentLocalizedHeader(Locale.getDefault().toString());
}
// Otherwise, perform the localization based on the discovered
// properties and cache the result.
else
{
// Resolve all localized header entries
- for (Iterator it = headers.entrySet().iterator(); it
- .hasNext();)
+ for (Map.Entry<String, String> entry : headers.entrySet())
{
- Map.Entry entry = (Map.Entry) it.next();
- String value = (String) entry.getValue();
+ String value = entry.getValue();
if (value.startsWith("%"))
{
String newvalue;
- String key = value
- .substring(value.indexOf("%") + 1);
+ String key = value.substring(value.indexOf("%") + 1);
newvalue = mergedProperties.getProperty(key);
if (newvalue == null)
{
@@ -392,7 +408,7 @@ class PojoSRBundle implements Bundle, Bu
return result;
}
- private void updateHeaderCache(String locale, Map localizedHeaders)
+ private void updateHeaderCache(String locale, Map<String, String> localizedHeaders)
{
synchronized (m_cachedHeaders)
{
@@ -401,16 +417,15 @@ class PojoSRBundle implements Bundle, Bu
}
}
- private final Map m_cachedHeaders = new HashMap();
+ private final Map<String, Map<String, String>> m_cachedHeaders = new HashMap<String, Map<String, String>>();
private long m_cachedHeadersTimestamp;
- private static List createLocalizationResourceList(String basename,
- String locale)
+ private static List<String> createLocalizationResourceList(String basename, String locale)
{
- List result = new ArrayList(4);
+ List<String> result = new ArrayList<String>(4);
StringTokenizer tokens;
- StringBuffer tempLocale = new StringBuffer(basename);
+ StringBuilder tempLocale = new StringBuilder(basename);
result.add(tempLocale.toString());
@@ -433,64 +448,73 @@ class PojoSRBundle implements Bundle, Bu
public Class<?> loadClass(String name) throws ClassNotFoundException
{
- return m_loader.loadClass(name);
+ return m_classLoader.loadClass(name);
}
+ @Override
public Enumeration<URL> getResources(String name) throws IOException
{
// TODO: module - implement this based on the revision
- return m_loader.getResources(name);
+ return m_classLoader.getResources(name);
}
+ @Override
public Enumeration<String> getEntryPaths(String path)
{
return new EntryFilterEnumeration<String>(m_revision, false, path, null, false,
false);
}
+ @Override
public URL getEntry(String path)
{
URL result = m_revision.getEntry(path);
return result;
}
+ @Override
public long getLastModified()
{
return m_revision.getLastModified();
}
- public Enumeration<URL> findEntries(String path, String filePattern,
- boolean recurse)
+ @Override
+ public Enumeration<URL> findEntries(String path, String filePattern, boolean recurse)
{
// TODO: module - implement this based on the revision
- return new EntryFilterEnumeration<URL>(m_revision, false, path, filePattern,
- recurse, true);
+ return new EntryFilterEnumeration<URL>(m_revision, true, path, filePattern, recurse, true);
}
+ @Override
public BundleContext getBundleContext()
{
return m_context;
}
- public Map getSignerCertificates(int signersType)
+ @Override
+ public Map<X509Certificate, List<X509Certificate>> getSignerCertificates(int signersType)
{
// TODO: security - fix this
- return new HashMap();
+ return new HashMap<X509Certificate, List<X509Certificate>>();
}
+ @Override
public Version getVersion()
{
return m_version;
}
- public boolean equals(Object o)
- {
- if (o instanceof PojoSRBundle) {
- return ((PojoSRBundle) o).m_id == m_id;
- }
- return false;
- }
+ @Override
+ public boolean equals(Object o)
+ {
+ if (o instanceof PojoSRBundle)
+ {
+ return ((PojoSRBundle) o).m_id == m_id;
+ }
+ return false;
+ }
+ @Override
public int compareTo(Bundle o)
{
long thisBundleId = this.getBundleId();
@@ -498,45 +522,28 @@ class PojoSRBundle implements Bundle, Bu
return (thisBundleId < thatBundleId ? -1 : (thisBundleId == thatBundleId ? 0 : 1));
}
+ @SuppressWarnings("unchecked")
public <A> A adapt(Class<A> type)
{
- if (type == BundleStartLevel.class)
+ if (m_services != null && m_services.containsKey(type))
{
- return (A) new BundleStartLevel() {
-
- public Bundle getBundle()
- {
- return PojoSRBundle.this;
- }
-
- public int getStartLevel()
- {
- // TODO Implement this?
- return 1;
- }
-
- public void setStartLevel(int startlevel)
- {
- // TODO Implement this?
- }
-
- public boolean isPersistentlyStarted()
- {
- return true;
- }
-
- public boolean isActivationPolicyUsed()
- {
- return false;
- }};
+ return (A) m_services.get(type);
}
- else if (type == BundleRevisions.class)
+ if (type.isInstance(this))
{
return (A) this;
}
- else if (type == BundleWiring.class)
+ if (type == BundleWiring.class)
+ {
+ return (A) new BundleWiringImpl(this, m_classLoader);
+ }
+ if (type == BundleRevision.class)
{
- return (A) this.getWiring();
+ return (A) new BundleRevisionImpl(this);
+ }
+ if (type == BundleStartLevel.class)
+ {
+ return (A) new BundleStartLevelImpl(this);
}
return null;
}
@@ -551,109 +558,248 @@ class PojoSRBundle implements Bundle, Bu
String sym = getSymbolicName();
if (sym != null)
{
- return sym + " [" + getBundleId() +"]";
+ return sym + " [" + getBundleId() + "]";
}
- return "[" + getBundleId() +"]";
- }
-
- public Bundle getBundle()
- {
- return this;
+ return "[" + getBundleId() + "]";
}
+ @Override
public List<BundleRevision> getRevisions()
{
- return Arrays.asList((BundleRevision) this);
+ return Arrays.asList(adapt(BundleRevision.class));
}
- public List<BundleCapability> getDeclaredCapabilities(String namespace)
+ @Override
+ public Bundle getBundle()
{
- return Collections.emptyList();
+ return this;
}
- public List<BundleRequirement> getDeclaredRequirements(String namespace)
- {
- return Collections.emptyList();
- }
- public int getTypes()
+ public static class BundleStartLevelImpl implements BundleStartLevel
{
- if (getHeaders().get(Constants.FRAGMENT_HOST) != null) {
- return BundleRevision.TYPE_FRAGMENT;
+ private final Bundle bundle;
+
+ public BundleStartLevelImpl(Bundle bundle)
+ {
+ this.bundle = bundle;
+ }
+
+ public int getStartLevel()
+ {
+ // TODO Implement this?
+ return 1;
+ }
+
+ public void setStartLevel(int startlevel)
+ {
+ // TODO Implement this?
+ }
+
+ public boolean isPersistentlyStarted()
+ {
+ return true;
+ }
+
+ public boolean isActivationPolicyUsed()
+ {
+ return false;
+ }
+
+ @Override
+ public Bundle getBundle()
+ {
+ return bundle;
}
- return 0;
}
- public BundleWiring getWiring()
+ public static class BundleRevisionImpl implements BundleRevision
{
- return new BundleWiring()
+ private final Bundle bundle;
+
+ public BundleRevisionImpl(Bundle bundle)
{
+ this.bundle = bundle;
+ }
- public Bundle getBundle()
- {
- return PojoSRBundle.this;
- }
+ @Override
+ public String getSymbolicName()
+ {
+ return bundle.getSymbolicName();
+ }
- public Collection<String> listResources(String path, String filePattern, int options)
- {
- Collection<String> result = new ArrayList<String>();
- for (URL u : findEntries(path, filePattern, options))
- {
- result.add(u.toString());
- }
- // TODO: implement this
- return result;
- }
+ @Override
+ public Version getVersion()
+ {
+ return bundle.getVersion();
+ }
- public boolean isInUse()
- {
- return true;
- }
+ @Override
+ public List<BundleCapability> getDeclaredCapabilities(String namespace)
+ {
+ return Collections.emptyList();
+ }
- public boolean isCurrent()
- {
- return true;
- }
+ @Override
+ public List<BundleRequirement> getDeclaredRequirements(String namespace)
+ {
+ return Collections.emptyList();
+ }
- public BundleRevision getRevision()
+ @Override
+ public int getTypes()
+ {
+ if (bundle.getHeaders().get(Constants.FRAGMENT_HOST) != null)
{
- return PojoSRBundle.this;
+ return BundleRevision.TYPE_FRAGMENT;
}
+ return 0;
+ }
- public List<BundleRequirement> getRequirements(String namespace)
- {
- return getDeclaredRequirements(namespace);
- }
+ @Override
+ public BundleWiring getWiring()
+ {
+ return bundle.adapt(BundleWiring.class);
+ }
- public List<BundleWire> getRequiredWires(String namespace)
- {
- return Collections.emptyList();
- }
+ @Override
+ public List<Capability> getCapabilities(String namespace)
+ {
+ return Collections.emptyList();
+ }
- public List<BundleWire> getProvidedWires(String namespace)
- {
- return Collections.emptyList();
- }
+ @Override
+ public List<Requirement> getRequirements(String namespace)
+ {
+ return Collections.emptyList();
+ }
- public ClassLoader getClassLoader()
- {
- return getClass().getClassLoader();
- }
+ @Override
+ public Bundle getBundle()
+ {
+ return bundle;
+ }
+ }
+
+ public static class BundleWiringImpl implements BundleWiring
+ {
+
+ private final Bundle bundle;
+ private final ClassLoader classLoader;
+
+ public BundleWiringImpl(Bundle bundle, ClassLoader classLoader)
+ {
+ this.bundle = bundle;
+ this.classLoader = classLoader;
+ }
+
+ @Override
+ public boolean isInUse()
+ {
+ return true;
+ }
+
+ @Override
+ public boolean isCurrent()
+ {
+ return true;
+ }
- public List<BundleCapability> getCapabilities(String namespace)
+ @Override
+ public BundleRevision getRevision()
+ {
+ return bundle.adapt(BundleRevision.class);
+ }
+
+ @Override
+ public List<BundleRequirement> getRequirements(String namespace)
+ {
+ return Collections.emptyList();
+ }
+
+ @Override
+ public List<BundleWire> getRequiredWires(String namespace)
+ {
+ return Collections.emptyList();
+ }
+
+ @Override
+ public List<BundleWire> getProvidedWires(String namespace)
+ {
+ return Collections.emptyList();
+ }
+
+ @Override
+ public ClassLoader getClassLoader()
+ {
+ return classLoader;
+ }
+
+ @Override
+ public List<BundleCapability> getCapabilities(String namespace)
+ {
+ return Collections.emptyList();
+ }
+
+ @Override
+ public List<Capability> getResourceCapabilities(String namespace)
+ {
+ return Collections.emptyList();
+ }
+
+ @Override
+ public List<Requirement> getResourceRequirements(String namespace)
+ {
+ return Collections.emptyList();
+ }
+
+ @Override
+ public List<Wire> getProvidedResourceWires(String namespace)
+ {
+ return Collections.emptyList();
+ }
+
+ @Override
+ public List<Wire> getRequiredResourceWires(String namespace)
+ {
+ return Collections.emptyList();
+ }
+
+ @Override
+ public BundleRevision getResource()
+ {
+ return getRevision();
+ }
+
+ @Override
+ public Bundle getBundle()
+ {
+ return bundle;
+ }
+
+ @Override
+ public List<URL> findEntries(String path, String filePattern, int options)
+ {
+ List<URL> result = new ArrayList<URL>();
+ for (Enumeration<URL> e = bundle.findEntries(path, filePattern, options == BundleWiring.FINDENTRIES_RECURSE); e.hasMoreElements(); )
{
- return Collections.emptyList();
+ result.add(e.nextElement());
}
+ return result;
+ }
- public List<URL> findEntries(String path, String filePattern, int options)
+ @Override
+ public Collection<String> listResources(String path, String filePattern, int options)
+ {
+ // TODO: this is wrong, we should return the resource names
+ Collection<String> result = new ArrayList<String>();
+ for (URL u : findEntries(path, filePattern, options))
{
- List<URL> result = new ArrayList<URL>();
- for (Enumeration<URL> e = PojoSRBundle.this.findEntries(path, filePattern, options == BundleWiring.FINDENTRIES_RECURSE); e.hasMoreElements();)
- {
- result.add(e.nextElement());
- }
- return result;
+ result.add(u.toString());
}
- };
+ return result;
+ }
+
}
+
}
Modified: felix/trunk/connect/src/main/java/org/apache/felix/connect/PojoSRBundleContext.java
URL: http://svn.apache.org/viewvc/felix/trunk/connect/src/main/java/org/apache/felix/connect/PojoSRBundleContext.java?rev=1642172&r1=1642171&r2=1642172&view=diff
==============================================================================
--- felix/trunk/connect/src/main/java/org/apache/felix/connect/PojoSRBundleContext.java (original)
+++ felix/trunk/connect/src/main/java/org/apache/felix/connect/PojoSRBundleContext.java Thu Nov 27 15:14:39 2014
@@ -25,8 +25,9 @@ import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.Dictionary;
-import java.util.List;
+import java.util.Iterator;
import java.util.Map;
+import java.util.Set;
import org.osgi.framework.Bundle;
import org.osgi.framework.BundleContext;
@@ -39,10 +40,13 @@ import org.osgi.framework.InvalidSyntaxE
import org.osgi.framework.ServiceListener;
import org.osgi.framework.ServiceReference;
import org.osgi.framework.ServiceRegistration;
+import org.osgi.framework.hooks.service.FindHook;
import org.apache.felix.connect.felix.framework.ServiceRegistry;
import org.apache.felix.connect.felix.framework.capabilityset.SimpleFilter;
import org.apache.felix.connect.felix.framework.util.EventDispatcher;
+import org.apache.felix.connect.felix.framework.util.ShrinkableCollection;
+import org.apache.felix.connect.felix.framework.util.Util;
class PojoSRBundleContext implements BundleContext
{
@@ -50,10 +54,10 @@ class PojoSRBundleContext implements Bun
private final ServiceRegistry m_reg;
private final EventDispatcher m_dispatcher;
private final Map<Long, Bundle> m_bundles;
- private final Map m_config;
+ private final Map<String, Object> m_config;
public PojoSRBundleContext(Bundle bundle, ServiceRegistry reg,
- EventDispatcher dispatcher, Map<Long, Bundle> bundles, Map config)
+ EventDispatcher dispatcher, Map<Long, Bundle> bundles, Map<String, Object> config)
{
m_bundle = bundle;
m_reg = reg;
@@ -70,7 +74,7 @@ class PojoSRBundleContext implements Bun
public void removeServiceListener(ServiceListener listener)
{
m_dispatcher.removeListener(this, ServiceListener.class,
- listener);
+ listener);
}
public void removeFrameworkListener(FrameworkListener listener)
@@ -85,14 +89,14 @@ class PojoSRBundleContext implements Bun
}
public ServiceRegistration registerService(String clazz, Object service,
- Dictionary properties)
+ Dictionary properties)
{
- return m_reg.registerService(m_bundle, new String[] { clazz }, service,
+ return m_reg.registerService(m_bundle, new String[]{clazz}, service,
properties);
}
public ServiceRegistration registerService(String[] clazzes,
- Object service, Dictionary properties)
+ Object service, Dictionary properties)
{
return m_reg.registerService(m_bundle, clazzes, service, properties);
}
@@ -109,17 +113,17 @@ class PojoSRBundleContext implements Bun
throw new BundleException("pojosr can't do that");
}
- public ServiceReference[] getServiceReferences(String clazz, String filter)
+ public ServiceReference<?>[] getServiceReferences(String clazz, String filter)
throws InvalidSyntaxException
{
- return getAllServiceReferences(clazz, filter);
+ return getServiceReferences(clazz, filter, true);
}
- public ServiceReference getServiceReference(String clazz)
+ public ServiceReference<?> getServiceReference(String clazz)
{
try
{
- return getBestServiceReference(getAllServiceReferences(clazz, null));
+ return getBestServiceReference(getServiceReferences(clazz, null));
}
catch (InvalidSyntaxException e)
{
@@ -127,7 +131,7 @@ class PojoSRBundleContext implements Bun
}
}
- private ServiceReference getBestServiceReference(ServiceReference[] refs)
+ private ServiceReference<?> getBestServiceReference(ServiceReference<?>[] refs)
{
if (refs == null)
{
@@ -153,7 +157,7 @@ class PojoSRBundleContext implements Bun
return bestRef;
}
- public Object getService(ServiceReference reference)
+ public <S> S getService(ServiceReference<S> reference)
{
return m_reg.getService(m_bundle, reference);
}
@@ -202,24 +206,90 @@ class PojoSRBundleContext implements Bun
}
public ServiceReference[] getAllServiceReferences(String clazz,
- String filter) throws InvalidSyntaxException
+ String filter) throws InvalidSyntaxException
{
- SimpleFilter simple = null;
- if (filter != null)
+ return getServiceReferences(clazz, filter, false);
+ }
+
+ /**
+ * Retrieves an array of {@link ServiceReference} objects based on calling bundle,
+ * service class name, and filter expression. Optionally checks for isAssignable to
+ * make sure that the service can be cast to the
+ * @param className Service Classname or <code>null</code> for all
+ * @param expr Filter Criteria or <code>null</code>
+ * @return Array of ServiceReference objects that meet the criteria
+ * @throws InvalidSyntaxException
+ */
+ ServiceReference[] getServiceReferences(
+ final String className,
+ final String expr, final boolean checkAssignable)
+ throws InvalidSyntaxException
+ {
+ // Define filter if expression is not null.
+ SimpleFilter filter = null;
+ if (expr != null)
{
try
{
- simple = SimpleFilter.parse(filter);
+ filter = SimpleFilter.parse(expr);
}
catch (Exception ex)
{
- throw new InvalidSyntaxException(ex.getMessage(), filter);
+ throw new InvalidSyntaxException(ex.getMessage(), expr);
}
}
- List<ServiceReference> result = m_reg.getServiceReferences(clazz,
- simple);
- return result.isEmpty() ? null : result
- .toArray(new ServiceReference[result.size()]);
+
+ // Ask the service registry for all matching service references.
+ final Collection<ServiceReference<?>> refList = m_reg.getServiceReferences(className, filter);
+
+ // Filter on assignable references
+ if (checkAssignable)
+ {
+ for (Iterator<ServiceReference<?>> it = refList.iterator(); it.hasNext();)
+ {
+ // Get the current service reference.
+ ServiceReference ref = it.next();
+ // Now check for castability.
+ if (!Util.isServiceAssignable(m_bundle, ref))
+ {
+ it.remove();
+ }
+ }
+ }
+
+ // activate findhooks
+ Set<ServiceReference<FindHook>> findHooks = m_reg.getHooks(org.osgi.framework.hooks.service.FindHook.class);
+ for (ServiceReference<org.osgi.framework.hooks.service.FindHook> sr : findHooks)
+ {
+ org.osgi.framework.hooks.service.FindHook fh = m_reg.getService(getBundle(0), sr);
+ if (fh != null)
+ {
+ try
+ {
+ fh.find(this,
+ className,
+ expr,
+ !checkAssignable,
+ new ShrinkableCollection<ServiceReference<?>>(refList));
+ }
+ catch (Throwable th)
+ {
+ System.err.println("Problem invoking service registry hook");
+ th.printStackTrace();
+ }
+ finally
+ {
+ m_reg.ungetService(getBundle(0), sr);
+ }
+ }
+ }
+
+ if (refList.size() > 0)
+ {
+ return refList.toArray(new ServiceReference[refList.size()]);
+ }
+
+ return null;
}
public Filter createFilter(String filter) throws InvalidSyntaxException
@@ -240,10 +310,10 @@ class PojoSRBundleContext implements Bun
}
}
- public void addServiceListener(final ServiceListener listener, String filter)
+ public void addServiceListener(final ServiceListener listener, String filter)
throws InvalidSyntaxException
{
- m_dispatcher.addListener(this, ServiceListener.class, listener,
+ m_dispatcher.addListener(this, ServiceListener.class, listener,
filter == null ? null : FrameworkUtil.createFilter(filter));
}
@@ -259,18 +329,21 @@ class PojoSRBundleContext implements Bun
.addListener(this, BundleListener.class, listener, null);
}
+ @SuppressWarnings("unchecked")
public <S> ServiceRegistration<S> registerService(Class<S> clazz, S service, Dictionary<String, ?> properties)
{
return (ServiceRegistration<S>) registerService(clazz.getName(), service, properties);
}
+ @SuppressWarnings("unchecked")
public <S> ServiceReference<S> getServiceReference(Class<S> clazz)
{
return (ServiceReference<S>) getServiceReference(clazz.getName());
}
+ @SuppressWarnings("unchecked")
public <S> Collection<ServiceReference<S>> getServiceReferences(Class<S> clazz, String filter)
- throws InvalidSyntaxException
+ throws InvalidSyntaxException
{
ServiceReference<S>[] refs = (ServiceReference<S>[]) getServiceReferences(clazz.getName(), filter);
if (refs == null)
Modified: felix/trunk/connect/src/main/java/org/apache/felix/connect/PojoServiceRegistryFactoryImpl.java
URL: http://svn.apache.org/viewvc/felix/trunk/connect/src/main/java/org/apache/felix/connect/PojoServiceRegistryFactoryImpl.java?rev=1642172&r1=1642171&r2=1642172&view=diff
==============================================================================
--- felix/trunk/connect/src/main/java/org/apache/felix/connect/PojoServiceRegistryFactoryImpl.java (original)
+++ felix/trunk/connect/src/main/java/org/apache/felix/connect/PojoServiceRegistryFactoryImpl.java Thu Nov 27 15:14:39 2014
@@ -22,9 +22,11 @@ import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
+import java.security.cert.X509Certificate;
import java.util.Dictionary;
import java.util.Enumeration;
import java.util.HashMap;
+import java.util.List;
import java.util.Map;
import org.osgi.framework.Bundle;
@@ -42,183 +44,227 @@ import org.apache.felix.connect.launch.C
import org.apache.felix.connect.launch.PojoServiceRegistry;
import org.apache.felix.connect.launch.PojoServiceRegistryFactory;
-public class PojoServiceRegistryFactoryImpl implements
- PojoServiceRegistryFactory, FrameworkFactory {
+public class PojoServiceRegistryFactoryImpl implements PojoServiceRegistryFactory, FrameworkFactory
+{
- public PojoServiceRegistry newPojoServiceRegistry(Map configuration)
- throws Exception {
- return new PojoSR(configuration);
- }
-
- public Framework newFramework(Map configuration) {
- return new FrameworkImpl((String) configuration.get("pojosr.filter"));
- }
-
- private static final class FrameworkImpl implements Framework {
- private final String m_filter;
- private volatile Bundle m_bundle = null;
- private volatile PojoServiceRegistry m_reg = null;
-
- public FrameworkImpl(String filter) {
- m_filter = filter;
- }
-
- public void init() throws BundleException {
- try {
- m_reg = new PojoServiceRegistryFactoryImpl()
- .newPojoServiceRegistry(new HashMap());
- m_bundle = m_reg.getBundleContext()
- .getBundle();
- } catch (Exception ex) {
- throw new BundleException("Unable to scan classpath", ex);
- }
- }
-
- public int getState() {
- return (m_bundle == null) ? Bundle.INSTALLED : m_bundle.getState();
- }
-
- public void start(int options) throws BundleException {
- start();
- }
-
- public void start() throws BundleException {
- try {
- m_reg.startBundles((m_filter != null) ? new ClasspathScanner()
- .scanForBundles(m_filter)
- : new ClasspathScanner().scanForBundles());
- } catch (Exception e) {
- throw new BundleException("Error starting framework", e);
- }
- }
-
- public void stop(int options) throws BundleException {
- m_bundle.stop(options);
- }
-
- public void stop() throws BundleException {
- m_bundle.stop();
- }
-
- public void update(InputStream input) throws BundleException {
- m_bundle.update(input);
- }
-
- public void update() throws BundleException {
- m_bundle.update();
- }
-
- public void uninstall() throws BundleException {
- m_bundle.uninstall();
- }
-
- public Dictionary getHeaders() {
- return m_bundle.getHeaders();
- }
-
- public long getBundleId() {
- return m_bundle.getBundleId();
- }
-
- public String getLocation() {
- return m_bundle.getLocation();
- }
-
- public ServiceReference[] getRegisteredServices() {
- return m_bundle.getRegisteredServices();
- }
-
- public ServiceReference[] getServicesInUse() {
- return m_bundle.getServicesInUse();
- }
-
- public boolean hasPermission(Object permission) {
- return m_bundle.hasPermission(permission);
- }
-
- public URL getResource(String name) {
- return m_bundle.getResource(name);
- }
-
- public Dictionary getHeaders(String locale) {
- return m_bundle.getHeaders(locale);
- }
-
- public String getSymbolicName() {
- return m_bundle.getSymbolicName();
- }
-
- public Class loadClass(String name) throws ClassNotFoundException {
- return m_bundle.loadClass(name);
- }
-
- public Enumeration getResources(String name) throws IOException {
- return m_bundle.getResources(name);
- }
-
- public Enumeration getEntryPaths(String path) {
- return m_bundle.getEntryPaths(path);
- }
-
- public URL getEntry(String path) {
- return m_bundle.getEntry(path);
- }
-
- public long getLastModified() {
- return m_bundle.getLastModified();
- }
-
- public Enumeration findEntries(String path, String filePattern,
- boolean recurse) {
- return m_bundle.findEntries(path, filePattern, recurse);
- }
-
- public BundleContext getBundleContext() {
- return m_bundle.getBundleContext();
- }
-
- public Map getSignerCertificates(int signersType) {
- return m_bundle.getSignerCertificates(signersType);
- }
-
- public Version getVersion() {
- return m_bundle.getVersion();
- }
-
- public FrameworkEvent waitForStop(long timeout)
- throws InterruptedException {
- final Object lock = new Object();
-
- m_bundle.getBundleContext().addBundleListener(new SynchronousBundleListener() {
-
- public void bundleChanged(BundleEvent event) {
- if ((event.getBundle() == m_bundle) && (event.getType() == BundleEvent.STOPPED)) {
- synchronized (lock) {
- lock.notifyAll();
- }
- }
- }
- });
- synchronized (lock) {
- while (m_bundle.getState() != Bundle.RESOLVED) {
- if (m_bundle.getState() == Bundle.STOPPING ) {
- lock.wait(100);
- }
- else {
- lock.wait();
- }
- }
- }
- return new FrameworkEvent(FrameworkEvent.STOPPED, m_bundle, null);
- }
-
- public File getDataFile(String filename) {
- return m_bundle.getDataFile(filename);
- }
+ public PojoServiceRegistry newPojoServiceRegistry(Map<String, Object> configuration) throws Exception
+ {
+ return new PojoSR(configuration);
+ }
+
+ public Framework newFramework(Map<String, String> configuration)
+ {
+ return new FrameworkImpl(configuration.get("pojosr.filter"));
+ }
+
+ private static final class FrameworkImpl implements Framework
+ {
+ private final String m_filter;
+ private volatile Bundle m_bundle = null;
+ private volatile PojoServiceRegistry m_reg = null;
+
+ public FrameworkImpl(String filter)
+ {
+ m_filter = filter;
+ }
+
+ public void init() throws BundleException
+ {
+ try
+ {
+ m_reg = new PojoServiceRegistryFactoryImpl()
+ .newPojoServiceRegistry(new HashMap<String, Object>());
+ m_bundle = m_reg.getBundleContext().getBundle();
+ }
+ catch (Exception ex)
+ {
+ throw new BundleException("Unable to scan classpath", ex);
+ }
+ }
+
+ public int getState()
+ {
+ return (m_bundle == null) ? Bundle.INSTALLED : m_bundle.getState();
+ }
+
+ public void start(int options) throws BundleException
+ {
+ start();
+ }
+
+ public void start() throws BundleException
+ {
+ try
+ {
+ m_reg.startBundles((m_filter != null) ? new ClasspathScanner()
+ .scanForBundles(m_filter)
+ : new ClasspathScanner().scanForBundles());
+ }
+ catch (Exception e)
+ {
+ throw new BundleException("Error starting framework", e);
+ }
+ }
+
+ public void stop(int options) throws BundleException
+ {
+ m_bundle.stop(options);
+ }
+
+ public void stop() throws BundleException
+ {
+ m_bundle.stop();
+ }
+
+ public void update(InputStream input) throws BundleException
+ {
+ m_bundle.update(input);
+ }
+
+ public void update() throws BundleException
+ {
+ m_bundle.update();
+ }
+
+ public void uninstall() throws BundleException
+ {
+ m_bundle.uninstall();
+ }
+
+ public Dictionary<String, String> getHeaders()
+ {
+ return m_bundle.getHeaders();
+ }
+
+ public long getBundleId()
+ {
+ return m_bundle.getBundleId();
+ }
+
+ public String getLocation()
+ {
+ return m_bundle.getLocation();
+ }
+
+ public ServiceReference[] getRegisteredServices()
+ {
+ return m_bundle.getRegisteredServices();
+ }
+
+ public ServiceReference[] getServicesInUse()
+ {
+ return m_bundle.getServicesInUse();
+ }
+
+ public boolean hasPermission(Object permission)
+ {
+ return m_bundle.hasPermission(permission);
+ }
+
+ public URL getResource(String name)
+ {
+ return m_bundle.getResource(name);
+ }
+
+ public Dictionary<String, String> getHeaders(String locale)
+ {
+ return m_bundle.getHeaders(locale);
+ }
+
+ public String getSymbolicName()
+ {
+ return m_bundle.getSymbolicName();
+ }
+
+ public Class loadClass(String name) throws ClassNotFoundException
+ {
+ return m_bundle.loadClass(name);
+ }
+
+ public Enumeration<URL> getResources(String name) throws IOException
+ {
+ return m_bundle.getResources(name);
+ }
+
+ public Enumeration<String> getEntryPaths(String path)
+ {
+ return m_bundle.getEntryPaths(path);
+ }
+
+ public URL getEntry(String path)
+ {
+ return m_bundle.getEntry(path);
+ }
+
+ public long getLastModified()
+ {
+ return m_bundle.getLastModified();
+ }
+
+ public Enumeration<URL> findEntries(String path, String filePattern, boolean recurse)
+ {
+ return m_bundle.findEntries(path, filePattern, recurse);
+ }
+
+ public BundleContext getBundleContext()
+ {
+ return m_bundle.getBundleContext();
+ }
+
+ public Map<X509Certificate, List<X509Certificate>> getSignerCertificates(int signersType)
+ {
+ return m_bundle.getSignerCertificates(signersType);
+ }
+
+ public Version getVersion()
+ {
+ return m_bundle.getVersion();
+ }
+
+ public FrameworkEvent waitForStop(long timeout)
+ throws InterruptedException
+ {
+ final Object lock = new Object();
+
+ m_bundle.getBundleContext().addBundleListener(new SynchronousBundleListener()
+ {
+
+ public void bundleChanged(BundleEvent event)
+ {
+ if ((event.getBundle() == m_bundle) && (event.getType() == BundleEvent.STOPPED))
+ {
+ synchronized (lock)
+ {
+ lock.notifyAll();
+ }
+ }
+ }
+ });
+ synchronized (lock)
+ {
+ while (m_bundle.getState() != Bundle.RESOLVED)
+ {
+ if (m_bundle.getState() == Bundle.STOPPING)
+ {
+ lock.wait(100);
+ }
+ else
+ {
+ lock.wait();
+ }
+ }
+ }
+ return new FrameworkEvent(FrameworkEvent.STOPPED, m_bundle, null);
+ }
+
+ public File getDataFile(String filename)
+ {
+ return m_bundle.getDataFile(filename);
+ }
public int compareTo(Bundle o)
{
- if (o == this)
+ if (o == this)
{
return 0;
}
@@ -230,5 +276,5 @@ public class PojoServiceRegistryFactoryI
return m_bundle.adapt(type);
}
- }
+ }
}
Added: felix/trunk/connect/src/main/java/org/apache/felix/connect/RequiredBundleImpl.java
URL: http://svn.apache.org/viewvc/felix/trunk/connect/src/main/java/org/apache/felix/connect/RequiredBundleImpl.java?rev=1642172&view=auto
==============================================================================
--- felix/trunk/connect/src/main/java/org/apache/felix/connect/RequiredBundleImpl.java (added)
+++ felix/trunk/connect/src/main/java/org/apache/felix/connect/RequiredBundleImpl.java Thu Nov 27 15:14:39 2014
@@ -0,0 +1,79 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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.connect;
+
+import java.util.HashSet;
+import java.util.Set;
+
+import org.osgi.framework.Bundle;
+import org.osgi.framework.Version;
+import org.osgi.framework.namespace.BundleNamespace;
+import org.osgi.framework.wiring.BundleWire;
+import org.osgi.framework.wiring.BundleWiring;
+import org.osgi.service.packageadmin.RequiredBundle;
+
+public class RequiredBundleImpl implements RequiredBundle
+{
+
+ private final Bundle m_bundle;
+
+ public RequiredBundleImpl(Bundle bundle)
+ {
+ m_bundle = bundle;
+ }
+
+ public String getSymbolicName()
+ {
+ return m_bundle.getSymbolicName();
+ }
+
+ public Bundle getBundle()
+ {
+ return m_bundle;
+ }
+
+ public Bundle[] getRequiringBundles()
+ {
+ Set<Bundle> set = new HashSet<Bundle>();
+ for (BundleWire wire : m_bundle.adapt(BundleWiring.class).getProvidedWires(null))
+ {
+ if (BundleNamespace.BUNDLE_NAMESPACE.equals(wire.getCapability().getNamespace()))
+ {
+ set.add(wire.getRequirer().getBundle());
+ }
+ }
+ return set.toArray(new Bundle[set.size()]);
+ }
+
+ public Version getVersion()
+ {
+ return m_bundle.getVersion();
+ }
+
+ public boolean isRemovalPending()
+ {
+ return false;
+ }
+
+ public String toString()
+ {
+ return m_bundle.getSymbolicName() + "; version=" + m_bundle.getVersion();
+ }
+
+}
Modified: felix/trunk/connect/src/main/java/org/apache/felix/connect/Revision.java
URL: http://svn.apache.org/viewvc/felix/trunk/connect/src/main/java/org/apache/felix/connect/Revision.java?rev=1642172&r1=1642171&r2=1642172&view=diff
==============================================================================
--- felix/trunk/connect/src/main/java/org/apache/felix/connect/Revision.java (original)
+++ felix/trunk/connect/src/main/java/org/apache/felix/connect/Revision.java Thu Nov 27 15:14:39 2014
@@ -21,11 +21,11 @@ package org.apache.felix.connect;
import java.net.URL;
import java.util.Enumeration;
-abstract class Revision
+public interface Revision
{
- public abstract long getLastModified();
+ public long getLastModified();
- public abstract URL getEntry(String entryName);
+ public URL getEntry(String entryName);
- public abstract Enumeration<String> getEntries();
+ public Enumeration<String> getEntries();
}
\ No newline at end of file
Modified: felix/trunk/connect/src/main/java/org/apache/felix/connect/URLRevision.java
URL: http://svn.apache.org/viewvc/felix/trunk/connect/src/main/java/org/apache/felix/connect/URLRevision.java?rev=1642172&r1=1642171&r2=1642172&view=diff
==============================================================================
--- felix/trunk/connect/src/main/java/org/apache/felix/connect/URLRevision.java (original)
+++ felix/trunk/connect/src/main/java/org/apache/felix/connect/URLRevision.java Thu Nov 27 15:14:39 2014
@@ -20,10 +20,10 @@ package org.apache.felix.connect;
import java.net.MalformedURLException;
import java.net.URL;
+import java.util.Collections;
import java.util.Enumeration;
-import java.util.Properties;
-class URLRevision extends Revision
+class URLRevision implements Revision
{
private final URL m_url;
private final long m_lastModified;
@@ -47,9 +47,10 @@ class URLRevision extends Revision
return m_lastModified;
}
- public Enumeration getEntries()
+ @Override
+ public Enumeration<String> getEntries()
{
- return new Properties().elements();
+ return Collections.emptyEnumeration();
}
@Override
Modified: felix/trunk/connect/src/main/java/org/apache/felix/connect/felix/framework/ServiceRegistrationImpl.java
URL: http://svn.apache.org/viewvc/felix/trunk/connect/src/main/java/org/apache/felix/connect/felix/framework/ServiceRegistrationImpl.java?rev=1642172&r1=1642171&r2=1642172&view=diff
==============================================================================
--- felix/trunk/connect/src/main/java/org/apache/felix/connect/felix/framework/ServiceRegistrationImpl.java (original)
+++ felix/trunk/connect/src/main/java/org/apache/felix/connect/felix/framework/ServiceRegistrationImpl.java Thu Nov 27 15:14:39 2014
@@ -18,17 +18,27 @@
*/
package org.apache.felix.connect.felix.framework;
-import java.util.*;
-
-import org.osgi.framework.*;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.Dictionary;
+import java.util.Enumeration;
+import java.util.Map;
+import java.util.Set;
+
+import org.osgi.framework.Bundle;
+import org.osgi.framework.Constants;
+import org.osgi.framework.ServiceException;
+import org.osgi.framework.ServiceFactory;
+import org.osgi.framework.ServiceReference;
+import org.osgi.framework.ServiceRegistration;
+import org.osgi.framework.wiring.BundleCapability;
+import org.osgi.framework.wiring.BundleRevision;
import org.apache.felix.connect.felix.framework.util.MapToDictionary;
import org.apache.felix.connect.felix.framework.util.StringMap;
import org.apache.felix.connect.felix.framework.util.Util;
-import org.osgi.framework.wiring.BundleCapability;
-import org.osgi.framework.wiring.BundleRevision;
-class ServiceRegistrationImpl implements ServiceRegistration
+class ServiceRegistrationImpl<T> implements ServiceRegistration<T>
{
// Service registry.
private final ServiceRegistry m_registry;
@@ -41,16 +51,16 @@ class ServiceRegistrationImpl implements
// Service object.
private volatile Object m_svcObj;
// Service factory interface.
- private volatile ServiceFactory m_factory;
+ private volatile ServiceFactory<T> m_factory;
// Associated property dictionary.
private volatile Map m_propMap = new StringMap(false);
// Re-usable service reference.
- private final ServiceReferenceImpl m_ref;
+ private final ServiceReferenceImpl<T> m_ref;
// Flag indicating that we are unregistering.
private volatile boolean m_isUnregistering = false;
public ServiceRegistrationImpl(ServiceRegistry registry, Bundle bundle,
- String[] classes, Long serviceId, Object svcObj, Dictionary dict)
+ String[] classes, Long serviceId, Object svcObj, Dictionary dict)
{
m_registry = registry;
m_bundle = bundle;
@@ -77,7 +87,7 @@ class ServiceRegistrationImpl implements
m_svcObj = null;
}
- public synchronized ServiceReference getReference()
+ public synchronized ServiceReferenceImpl<T> getReference()
{
// Make sure registration is valid.
if (!isValid())
@@ -152,7 +162,7 @@ class ServiceRegistrationImpl implements
* itself.
*
* @return The service object associated with the registration.
- **/
+ */
Object getService()
{
return m_svcObj;
@@ -175,7 +185,7 @@ class ServiceRegistrationImpl implements
}
}
- void ungetService(Bundle relBundle, Object svcObj)
+ void ungetService(Bundle relBundle, T svcObj)
{
// If the service object is a service factory, then
// let it release the service object.
@@ -238,19 +248,24 @@ class ServiceRegistrationImpl implements
}
if (svcObj != null)
{
- for (int i = 0; i < m_classes.length; i++)
+ for (String className : m_classes)
{
- try {
- if (!Class.forName(m_classes[i]).isAssignableFrom(svcObj.getClass()))
+ Class<?> clazz = Util.loadClassUsingClass(svcObj.getClass(), className);
+ if ((clazz == null) || !clazz.isAssignableFrom(svcObj.getClass()))
{
- throw new ServiceException(
- "Service cannot be cast: " + m_classes[i],
- ServiceException.FACTORY_ERROR);
+ if (clazz == null)
+ {
+ throw new ServiceException(
+ "Service cannot be cast due to missing class: " + className,
+ ServiceException.FACTORY_ERROR);
+ }
+ else
+ {
+ throw new ServiceException(
+ "Service cannot be cast: " + className,
+ ServiceException.FACTORY_ERROR);
+ }
}
- } catch (ClassNotFoundException ex) {
- throw new ServiceException("Service is missing class: " + m_classes[i], ServiceException.FACTORY_ERROR);
- }
-
}
}
else
@@ -261,18 +276,17 @@ class ServiceRegistrationImpl implements
return svcObj;
}
- private void ungetFactoryUnchecked(Bundle bundle, Object svcObj)
+ private void ungetFactoryUnchecked(Bundle bundle, T svcObj)
{
m_factory.ungetService(bundle, this, svcObj);
}
-
//
// ServiceReference implementation
//
- class ServiceReferenceImpl implements ServiceReference, BundleCapability
+ class ServiceReferenceImpl<T> implements ServiceReference<T>, BundleCapability
{
private final ServiceReferenceMap m_map;
@@ -290,6 +304,13 @@ class ServiceRegistrationImpl implements
// Capability methods.
//
+
+ @Override
+ public BundleRevision getResource()
+ {
+ return getRevision();
+ }
+
@Override
public BundleRevision getRevision()
{
@@ -305,30 +326,28 @@ class ServiceRegistrationImpl implements
@Override
public Map<String, String> getDirectives()
{
- return Collections.EMPTY_MAP;
+ return Collections.emptyMap();
}
@Override
- public Map<String,Object> getAttributes()
+ public Map<String, Object> getAttributes()
{
return m_map;
}
- public List<String> getUses()
- {
- return Collections.EMPTY_LIST;
- }
-
+ @Override
public Object getProperty(String s)
{
return ServiceRegistrationImpl.this.getProperty(s);
}
+ @Override
public String[] getPropertyKeys()
{
return ServiceRegistrationImpl.this.getPropertyKeys();
}
+ @Override
public Bundle getBundle()
{
// The spec says that this should return null if
@@ -336,6 +355,7 @@ class ServiceRegistrationImpl implements
return (isValid()) ? m_bundle : null;
}
+ @Override
public Bundle[] getUsingBundles()
{
return ServiceRegistrationImpl.this.getUsingBundles();
@@ -349,12 +369,15 @@ class ServiceRegistrationImpl implements
{
oc = oc + ocs[i];
if (i < ocs.length - 1)
+ {
oc = oc + ", ";
+ }
}
oc = oc + "]";
return oc;
}
+ @Override
public boolean isAssignableTo(Bundle requester, String className)
{
// Always return true if the requester is the same as the provider.
@@ -365,9 +388,8 @@ class ServiceRegistrationImpl implements
// Boolean flag.
boolean allow = true;
- // Get the package.
- String pkgName = Util.getClassPackage(className);
- /*
+ /* // Get the package.
+ * String pkgName = Util.getClassPackage(className);
* Module requesterModule = ((BundleImpl)
* requester).getCurrentModule(); // Get package wiring from service
* requester. Wire requesterWire = Util.getWire(requesterModule,
@@ -444,6 +466,7 @@ class ServiceRegistrationImpl implements
return allow;
}
+ @Override
public int compareTo(Object reference)
{
ServiceReference other = (ServiceReference) reference;
@@ -485,66 +508,78 @@ class ServiceRegistrationImpl implements
}
}
- private class ServiceReferenceMap implements Map
+ private class ServiceReferenceMap implements Map<String, Object>
{
+ @Override
public int size()
{
throw new UnsupportedOperationException("Not supported yet.");
}
+ @Override
public boolean isEmpty()
{
throw new UnsupportedOperationException("Not supported yet.");
}
+ @Override
public boolean containsKey(Object o)
{
throw new UnsupportedOperationException("Not supported yet.");
}
+ @Override
public boolean containsValue(Object o)
{
throw new UnsupportedOperationException("Not supported yet.");
}
+ @Override
public Object get(Object o)
{
return ServiceRegistrationImpl.this.getProperty((String) o);
}
- public Object put(Object k, Object v)
+ @Override
+ public Object put(String k, Object v)
{
throw new UnsupportedOperationException("Not supported yet.");
}
+ @Override
public Object remove(Object o)
{
throw new UnsupportedOperationException("Not supported yet.");
}
- public void putAll(Map map)
+ @Override
+ public void putAll(Map<? extends String, ? extends Object> map)
{
throw new UnsupportedOperationException("Not supported yet.");
}
+ @Override
public void clear()
{
throw new UnsupportedOperationException("Not supported yet.");
}
- public Set<Object> keySet()
+ @Override
+ public Set<String> keySet()
{
throw new UnsupportedOperationException("Not supported yet.");
}
+ @Override
public Collection<Object> values()
{
throw new UnsupportedOperationException("Not supported yet.");
}
- public Set<Entry<Object, Object>> entrySet()
+ @Override
+ public Set<Entry<String, Object>> entrySet()
{
- return Collections.EMPTY_SET;
+ return Collections.emptySet();
}
}
}
\ No newline at end of file
Modified: felix/trunk/connect/src/main/java/org/apache/felix/connect/felix/framework/ServiceRegistry.java
URL: http://svn.apache.org/viewvc/felix/trunk/connect/src/main/java/org/apache/felix/connect/felix/framework/ServiceRegistry.java?rev=1642172&r1=1642171&r2=1642172&view=diff
==============================================================================
--- felix/trunk/connect/src/main/java/org/apache/felix/connect/felix/framework/ServiceRegistry.java (original)
+++ felix/trunk/connect/src/main/java/org/apache/felix/connect/felix/framework/ServiceRegistry.java Thu Nov 27 15:14:39 2014
@@ -18,80 +18,94 @@
*/
package org.apache.felix.connect.felix.framework;
-import java.util.*;
-
-import org.osgi.framework.*;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.Dictionary;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+import java.util.SortedSet;
+import java.util.TreeSet;
+import java.util.WeakHashMap;
+
+import org.osgi.framework.Bundle;
+import org.osgi.framework.Constants;
+import org.osgi.framework.ServiceEvent;
+import org.osgi.framework.ServiceException;
+import org.osgi.framework.ServiceFactory;
+import org.osgi.framework.ServiceReference;
+import org.osgi.framework.ServiceRegistration;
+import org.osgi.framework.wiring.BundleCapability;
import org.apache.felix.connect.felix.framework.capabilityset.CapabilitySet;
import org.apache.felix.connect.felix.framework.capabilityset.SimpleFilter;
-import org.osgi.framework.wiring.BundleCapability;
public class ServiceRegistry
{
private long m_currentServiceId = 1L;
// Maps bundle to an array of service registrations.
- private final Map m_regsMap = Collections.synchronizedMap(new HashMap());
+ private final Map<Bundle, ServiceRegistration[]> m_regsMap = Collections.synchronizedMap(new HashMap<Bundle, ServiceRegistration[]>());
// Capability set for all service registrations.
- private final CapabilitySet m_regCapSet;
+ private final CapabilitySet<ServiceRegistrationImpl.ServiceReferenceImpl> m_regCapSet;
// Maps registration to thread to keep track when a
// registration is in use, which will cause other
// threads to wait.
- private final Map m_lockedRegsMap = new HashMap();
+ private final Map<ServiceRegistrationImpl, Thread> m_lockedRegsMap = new HashMap<ServiceRegistrationImpl, Thread>();
// Maps bundle to an array of usage counts.
- private final Map m_inUseMap = new HashMap();
+ private final Map<Bundle, UsageCount[]> m_inUseMap = new HashMap<Bundle, UsageCount[]>();
private final ServiceRegistryCallbacks m_callbacks;
private final WeakHashMap<ServiceReference, ServiceReference> m_blackList =
- new WeakHashMap<ServiceReference, ServiceReference>();
+ new WeakHashMap<ServiceReference, ServiceReference>();
private final static Class<?>[] m_hookClasses =
- {
- org.osgi.framework.hooks.bundle.FindHook.class,
- org.osgi.framework.hooks.bundle.EventHook.class,
- org.osgi.framework.hooks.service.EventHook.class,
- org.osgi.framework.hooks.service.EventListenerHook.class,
- org.osgi.framework.hooks.service.FindHook.class,
- org.osgi.framework.hooks.service.ListenerHook.class,
- org.osgi.framework.hooks.weaving.WeavingHook.class,
- org.osgi.framework.hooks.resolver.ResolverHookFactory.class,
- org.osgi.service.url.URLStreamHandlerService.class,
- java.net.ContentHandler.class
- };
+ {
+ org.osgi.framework.hooks.bundle.FindHook.class,
+ org.osgi.framework.hooks.bundle.EventHook.class,
+ org.osgi.framework.hooks.service.EventHook.class,
+ org.osgi.framework.hooks.service.EventListenerHook.class,
+ org.osgi.framework.hooks.service.FindHook.class,
+ org.osgi.framework.hooks.service.ListenerHook.class,
+ org.osgi.framework.hooks.weaving.WeavingHook.class,
+ org.osgi.framework.hooks.resolver.ResolverHookFactory.class,
+ org.osgi.service.url.URLStreamHandlerService.class,
+ java.net.ContentHandler.class
+ };
private final Map<Class<?>, Set<ServiceReference<?>>> m_allHooks =
- new HashMap<Class<?>, Set<ServiceReference<?>>>();
+ new HashMap<Class<?>, Set<ServiceReference<?>>>();
public ServiceRegistry(ServiceRegistryCallbacks callbacks)
{
m_callbacks = callbacks;
- List indices = new ArrayList();
- indices.add(Constants.OBJECTCLASS);
- m_regCapSet = new CapabilitySet(indices, false);
+ m_regCapSet = new CapabilitySet(Collections.singletonList(Constants.OBJECTCLASS), false);
}
public ServiceReference[] getRegisteredServices(Bundle bundle)
{
- ServiceRegistration[] regs = (ServiceRegistration[]) m_regsMap.get(bundle);
+ ServiceRegistration[] regs = m_regsMap.get(bundle);
if (regs != null)
{
- List refs = new ArrayList(regs.length);
- for (int i = 0; i < regs.length; i++)
+ List<ServiceReference> refs = new ArrayList<ServiceReference>(regs.length);
+ for (ServiceRegistration reg : regs)
{
try
{
- refs.add(regs[i].getReference());
+ refs.add(reg.getReference());
}
catch (IllegalStateException ex)
{
// Don't include the reference as it is not valid anymore
}
}
- return (ServiceReference[]) refs.toArray(new ServiceReference[refs.size()]);
+ return refs.toArray(new ServiceReference[refs.size()]);
}
return null;
}
// Caller is expected to fire REGISTERED event.
public ServiceRegistration registerService(
- Bundle bundle, String[] classNames, Object svcObj, Dictionary dict)
+ Bundle bundle, String[] classNames, Object svcObj, Dictionary dict)
{
ServiceRegistrationImpl reg = null;
@@ -99,7 +113,7 @@ public class ServiceRegistry
{
// Create the service registration.
reg = new ServiceRegistrationImpl(
- this, bundle, classNames, new Long(m_currentServiceId++), svcObj, dict);
+ this, bundle, classNames, m_currentServiceId++, svcObj, dict);
// Keep track of registered hooks.
addHooks(classNames, svcObj, reg.getReference());
@@ -107,19 +121,19 @@ public class ServiceRegistry
// Get the bundles current registered services.
ServiceRegistration[] regs = (ServiceRegistration[]) m_regsMap.get(bundle);
m_regsMap.put(bundle, addServiceRegistration(regs, reg));
- m_regCapSet.addCapability((BundleCapability) reg.getReference());
+ m_regCapSet.addCapability(reg.getReference());
}
// Notify callback objects about registered service.
if (m_callbacks != null)
{
m_callbacks.serviceChanged(new ServiceEvent(
- ServiceEvent.REGISTERED, reg.getReference()), null);
+ ServiceEvent.REGISTERED, reg.getReference()), null);
}
return reg;
}
- public void unregisterService(Bundle bundle, ServiceRegistration reg)
+ public void unregisterService(Bundle bundle, ServiceRegistrationImpl reg)
{
// If this is a hook, it should be removed.
removeHook(reg.getReference());
@@ -133,16 +147,16 @@ public class ServiceRegistry
// new bundles will be able to look up the service.
// Now remove the registered service.
- ServiceRegistration[] regs = (ServiceRegistration[]) m_regsMap.get(bundle);
+ ServiceRegistration[] regs = m_regsMap.get(bundle);
m_regsMap.put(bundle, removeServiceRegistration(regs, reg));
- m_regCapSet.removeCapability((BundleCapability) reg.getReference());
+ m_regCapSet.removeCapability(reg.getReference());
}
// Notify callback objects about unregistering service.
if (m_callbacks != null)
{
m_callbacks.serviceChanged(
- new ServiceEvent(ServiceEvent.UNREGISTERING, reg.getReference()), null);
+ new ServiceEvent(ServiceEvent.UNREGISTERING, reg.getReference()), null);
}
// Now forcibly unget the service object for all stubborn clients.
@@ -152,7 +166,9 @@ public class ServiceRegistry
for (int i = 0; (clients != null) && (i < clients.length); i++)
{
while (ungetService(clients[i], reg.getReference()))
+ {
; // Keep removing until it is no longer possible
+ }
}
((ServiceRegistrationImpl) reg).invalidate();
}
@@ -165,7 +181,6 @@ public class ServiceRegistry
* bundle.
*
* @param bundle the bundle whose services should be unregistered.
- *
*/
public void unregisterServices(Bundle bundle)
{
@@ -173,7 +188,7 @@ public class ServiceRegistry
ServiceRegistration[] regs = null;
synchronized (this)
{
- regs = (ServiceRegistration[]) m_regsMap.get(bundle);
+ regs = m_regsMap.get(bundle);
}
// Note, there is no race condition here with respect to the
@@ -197,7 +212,7 @@ public class ServiceRegistry
}
}
- public synchronized List getServiceReferences(String className, SimpleFilter filter)
+ public synchronized Collection<ServiceReference<?>> getServiceReferences(String className, SimpleFilter filter)
{
if ((className == null) && (filter == null))
{
@@ -212,16 +227,16 @@ public class ServiceRegistry
else if ((className != null) && (filter != null))
{
// Return services matching the class name and filter.
- List filters = new ArrayList(2);
+ List<Object> filters = new ArrayList<Object>(2);
filters.add(new SimpleFilter(Constants.OBJECTCLASS, className, SimpleFilter.EQ));
filters.add(filter);
filter = new SimpleFilter(null, filters, SimpleFilter.AND);
}
// else just use the specified filter.
- Set<BundleCapability> matches = m_regCapSet.match(filter, false);
+ Set<ServiceRegistrationImpl.ServiceReferenceImpl> matches = m_regCapSet.match(filter, false);
- return new ArrayList(matches);
+ return (Collection) matches;
}
public synchronized ServiceReference[] getServicesInUse(Bundle bundle)
@@ -246,7 +261,7 @@ public class ServiceRegistry
// Get the service registration.
ServiceRegistrationImpl reg =
- ((ServiceRegistrationImpl.ServiceReferenceImpl) ref).getRegistration();
+ ((ServiceRegistrationImpl.ServiceReferenceImpl) ref).getRegistration();
synchronized (this)
{
@@ -258,9 +273,9 @@ public class ServiceRegistry
if (o.equals(Thread.currentThread()))
{
throw new ServiceException(
- "ServiceFactory.getService() resulted in a cycle.",
- ServiceException.FACTORY_ERROR,
- null);
+ "ServiceFactory.getService() resulted in a cycle.",
+ ServiceException.FACTORY_ERROR,
+ null);
}
// Otherwise, wait for it to be freed.
@@ -339,7 +354,7 @@ public class ServiceRegistry
{
UsageCount usage = null;
ServiceRegistrationImpl reg =
- ((ServiceRegistrationImpl.ServiceReferenceImpl) ref).getRegistration();
+ ((ServiceRegistrationImpl.ServiceReferenceImpl) ref).getRegistration();
synchronized (this)
{
@@ -351,7 +366,7 @@ public class ServiceRegistry
if (o.equals(Thread.currentThread()))
{
throw new IllegalStateException(
- "ServiceFactory.ungetService() resulted in a cycle.");
+ "ServiceFactory.ungetService() resulted in a cycle.");
}
// Otherwise, wait for it to be freed.
@@ -385,7 +400,7 @@ public class ServiceRegistry
{
// Remove reference from usages array.
((ServiceRegistrationImpl.ServiceReferenceImpl) ref)
- .getRegistration().ungetService(bundle, usage.m_svcObj);
+ .getRegistration().ungetService(bundle, usage.m_svcObj);
}
}
finally
@@ -423,14 +438,13 @@ public class ServiceRegistry
* specified bundle.
*
* @param bundle the bundle whose services are to be released.
- *
*/
public void ungetServices(Bundle bundle)
{
UsageCount[] usages;
synchronized (this)
{
- usages = (UsageCount[]) m_inUseMap.get(bundle);
+ usages = m_inUseMap.get(bundle);
}
if (usages == null)
@@ -445,10 +459,10 @@ public class ServiceRegistry
// Remove each service object from the
// service cache.
- for (int i = 0; i < usages.length; i++)
+ for (UsageCount usage : usages)
{
// Keep ungetting until all usage count is zero.
- while (ungetService(bundle, usages[i].m_ref))
+ while (ungetService(bundle, usage.m_ref))
{
// Empty loop body.
}
@@ -458,22 +472,18 @@ public class ServiceRegistry
public synchronized Bundle[] getUsingBundles(ServiceReference ref)
{
Bundle[] bundles = null;
- for (Iterator iter = m_inUseMap.entrySet().iterator(); iter.hasNext();)
+ for (Map.Entry<Bundle, UsageCount[]> entry : m_inUseMap.entrySet())
{
- Map.Entry entry = (Map.Entry) iter.next();
- Bundle bundle = (Bundle) entry.getKey();
- UsageCount[] usages = (UsageCount[]) entry.getValue();
- for (int useIdx = 0; useIdx < usages.length; useIdx++)
+ Bundle bundle = entry.getKey();
+ UsageCount[] usages = entry.getValue();
+ for (UsageCount usage : usages)
{
- if (usages[useIdx].m_ref.equals(ref))
+ if (usage.m_ref.equals(ref))
{
// Add the bundle to the array to be returned.
if (bundles == null)
{
- bundles = new Bundle[]
- {
- bundle
- };
+ bundles = new Bundle[]{bundle};
}
else
{
@@ -494,19 +504,19 @@ public class ServiceRegistry
if (m_callbacks != null)
{
m_callbacks.serviceChanged(
- new ServiceEvent(ServiceEvent.MODIFIED, reg.getReference()), oldProps);
+ new ServiceEvent(ServiceEvent.MODIFIED, reg.getReference()), oldProps);
}
}
private static ServiceRegistration[] addServiceRegistration(
- ServiceRegistration[] regs, ServiceRegistration reg)
+ ServiceRegistration[] regs, ServiceRegistration reg)
{
if (regs == null)
{
regs = new ServiceRegistration[]
- {
- reg
- };
+ {
+ reg
+ };
}
else
{
@@ -519,7 +529,7 @@ public class ServiceRegistry
}
private static ServiceRegistration[] removeServiceRegistration(
- ServiceRegistration[] regs, ServiceRegistration reg)
+ ServiceRegistration[] regs, ServiceRegistration reg)
{
for (int i = 0; (regs != null) && (i < regs.length); i++)
{
@@ -538,7 +548,7 @@ public class ServiceRegistry
if (i < newRegs.length)
{
System.arraycopy(
- regs, i + 1, newRegs, i, newRegs.length - i);
+ regs, i + 1, newRegs, i, newRegs.length - i);
}
regs = newRegs;
}
@@ -552,9 +562,8 @@ public class ServiceRegistry
* specified service reference.
*
* @param bundle The bundle whose usage counts are being searched.
- * @param ref The service reference to find in the bundle's usage counts.
+ * @param ref The service reference to find in the bundle's usage counts.
* @return The associated usage count or null if not found.
- *
*/
private UsageCount getUsageCount(Bundle bundle, ServiceReference ref)
{
@@ -577,13 +586,11 @@ public class ServiceRegistry
* be incremented.
*
* @param bundle The bundle acquiring the service.
- * @param ref The service reference of the acquired service.
- * @param svcObj The service object of the acquired service.
- *
+ * @param ref The service reference of the acquired service.
*/
private UsageCount addUsageCount(Bundle bundle, ServiceReference ref)
{
- UsageCount[] usages = (UsageCount[]) m_inUseMap.get(bundle);
+ UsageCount[] usages = m_inUseMap.get(bundle);
UsageCount usage = new UsageCount();
usage.m_ref = ref;
@@ -591,9 +598,9 @@ public class ServiceRegistry
if (usages == null)
{
usages = new UsageCount[]
- {
- usage
- };
+ {
+ usage
+ };
}
else
{
@@ -618,8 +625,7 @@ public class ServiceRegistry
* usage count for the specified service reference.
*
* @param bundle The bundle whose usage count should be removed.
- * @param ref The service reference whose usage count should be removed.
- *
+ * @param ref The service reference whose usage count should be removed.
*/
private void flushUsageCount(Bundle bundle, ServiceReference ref)
{
@@ -641,7 +647,7 @@ public class ServiceRegistry
if (i < newUsages.length)
{
System.arraycopy(
- usages, i + 1, newUsages, i, newUsages.length - i);
+ usages, i + 1, newUsages, i, newUsages.length - i);
}
usages = newUsages;
}
@@ -726,7 +732,7 @@ public class ServiceRegistry
// We maintain the hooks sorted, so if ranking has changed for example,
// we need to ensure the order remains correct by resorting the hooks.
Object svcObj = ((ServiceRegistrationImpl.ServiceReferenceImpl) ref)
- .getRegistration().getService();
+ .getRegistration().getService();
String[] classNames = (String[]) ref.getProperty(Constants.OBJECTCLASS);
for (Class<?> hookClass : m_hookClasses)
@@ -750,7 +756,7 @@ public class ServiceRegistry
private void removeHook(ServiceReference ref)
{
Object svcObj = ((ServiceRegistrationImpl.ServiceReferenceImpl) ref)
- .getRegistration().getService();
+ .getRegistration().getService();
String[] classNames = (String[]) ref.getProperty(Constants.OBJECTCLASS);
for (Class<?> hookClass : m_hookClasses)
@@ -777,25 +783,18 @@ public class ServiceRegistry
{
synchronized (m_allHooks)
{
- Set<ServiceReference<?>> hooks = m_allHooks.get(hookClass);
+ @SuppressWarnings("unchecked")
+ Set<ServiceReference<S>> hooks = (Set) m_allHooks.get(hookClass);
if (hooks != null)
{
- SortedSet sorted = new TreeSet<ServiceReference<?>>(Collections.reverseOrder());
+ SortedSet<ServiceReference<S>> sorted = new TreeSet<ServiceReference<S>>(Collections.reverseOrder());
sorted.addAll(hooks);
- return asTypedSortedSet(sorted);
+ return sorted;
}
- return Collections.EMPTY_SET;
+ return Collections.emptySet();
}
}
- private static <S> SortedSet<ServiceReference<S>> asTypedSortedSet(
- SortedSet<ServiceReference<?>> ss)
- {
- return (SortedSet<ServiceReference<S>>) (SortedSet) ss;
-
-
- }
-
private static class UsageCount
{
public int m_count = 0;
@@ -805,6 +804,6 @@ public class ServiceRegistry
public interface ServiceRegistryCallbacks
{
- void serviceChanged(ServiceEvent event, Dictionary oldProps);
+ void serviceChanged(ServiceEvent event, Dictionary<String, ?> oldProps);
}
}
\ No newline at end of file