SLING-6687 : Replace commons.json usage in org.apache.sling.adapter
git-svn-id: https://svn.apache.org/repos/asf/sling/trunk@1788910 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/pom.xml b/pom.xml
index ed2bdf2..c2ccdf5 100644
--- a/pom.xml
+++ b/pom.xml
@@ -23,7 +23,7 @@
<parent>
<groupId>org.apache.sling</groupId>
<artifactId>sling</artifactId>
- <version>26</version>
+ <version>30</version>
<relativePath />
</parent>
@@ -53,20 +53,10 @@
<plugins>
<plugin>
<groupId>org.apache.felix</groupId>
- <artifactId>maven-scr-plugin</artifactId>
- </plugin>
- <plugin>
- <groupId>org.apache.felix</groupId>
<artifactId>maven-bundle-plugin</artifactId>
<extensions>true</extensions>
<configuration>
<instructions>
- <Export-Package>
- org.apache.sling.adapter;version=2.1.0
- </Export-Package>
- <Private-Package>
- org.apache.sling.adapter.internal
- </Private-Package>
<Import-Package>
org.apache.sling.api.adapter;version="[2.2,2.3)",
*
@@ -98,44 +88,54 @@
</dependency>
<dependency>
<groupId>org.apache.sling</groupId>
- <artifactId>org.apache.sling.commons.osgi</artifactId>
- <version>2.1.0</version>
- <scope>provided</scope>
- </dependency>
- <dependency>
- <groupId>org.apache.sling</groupId>
- <artifactId>org.apache.sling.commons.json</artifactId>
- <version>2.0.2-incubator</version>
+ <artifactId>org.apache.sling.commons.johnzon</artifactId>
+ <version>0.1.0-SNAPSHOT</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.osgi</groupId>
- <artifactId>org.osgi.core</artifactId>
- </dependency>
- <dependency>
- <groupId>org.osgi</groupId>
- <artifactId>org.osgi.compendium</artifactId>
- </dependency>
- <dependency>
- <groupId>org.apache.felix</groupId>
- <artifactId>org.apache.felix.scr.annotations</artifactId>
+ <artifactId>osgi.core</artifactId>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
</dependency>
<dependency>
+ <groupId>junit</groupId>
+ <artifactId>junit</artifactId>
+ </dependency>
+ <dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<scope>test</scope>
</dependency>
<dependency>
- <groupId>junit</groupId>
- <artifactId>junit</artifactId>
+ <groupId>javax.servlet</groupId>
+ <artifactId>javax.servlet-api</artifactId>
</dependency>
<dependency>
- <groupId>javax.servlet</groupId>
- <artifactId>servlet-api</artifactId>
+ <groupId>org.apache.sling</groupId>
+ <artifactId>org.apache.sling.commons.osgi</artifactId>
+ <version>2.1.0</version>
+ <scope>provided</scope>
+ </dependency>
+ <dependency>
+ <groupId>org.apache.sling</groupId>
+ <artifactId>org.apache.sling.commons.osgi</artifactId>
+ <version>2.1.0</version>
+ <scope>provided</scope>
+ </dependency>
+ <dependency>
+ <groupId>org.osgi</groupId>
+ <artifactId>org.osgi.service.component</artifactId>
+ <version>1.3.0</version>
+ <scope>provided</scope>
+ </dependency>
+ <dependency>
+ <groupId>org.osgi</groupId>
+ <artifactId>org.osgi.service.event</artifactId>
+ <version>1.3.1</version>
+ <scope>provided</scope>
</dependency>
<dependency>
<groupId>org.jmock</groupId>
@@ -148,9 +148,9 @@
<scope>provided</scope>
</dependency>
<dependency>
- <groupId>commons-lang</groupId>
- <artifactId>commons-lang</artifactId>
- <version>2.5</version>
+ <groupId>org.apache.commons</groupId>
+ <artifactId>commons-lang3</artifactId>
+ <version>3.5</version>
<scope>provided</scope>
</dependency>
<dependency>
diff --git a/src/main/java/org/apache/sling/adapter/internal/AdapterFactoryDescriptor.java b/src/main/java/org/apache/sling/adapter/internal/AdapterFactoryDescriptor.java
index 1075e5f..1ff0e0d 100644
--- a/src/main/java/org/apache/sling/adapter/internal/AdapterFactoryDescriptor.java
+++ b/src/main/java/org/apache/sling/adapter/internal/AdapterFactoryDescriptor.java
@@ -18,6 +18,7 @@
*/
package org.apache.sling.adapter.internal;
+import org.apache.sling.adapter.Adaption;
import org.apache.sling.api.adapter.AdapterFactory;
import org.osgi.framework.ServiceReference;
import org.osgi.framework.ServiceRegistration;
@@ -34,15 +35,15 @@
private final String[] adapters;
- private final ServiceReference reference;
+ private final ServiceReference<AdapterFactory> reference;
private final ComponentContext context;
- private ServiceRegistration adaption;
+ private volatile ServiceRegistration<Adaption> adaption;
public AdapterFactoryDescriptor(
final ComponentContext context,
- final ServiceReference reference,
+ final ServiceReference<AdapterFactory> reference,
final String[] adapters) {
this.reference = reference;
this.context = context;
@@ -51,7 +52,7 @@
public AdapterFactory getFactory() {
if ( factory == null ) {
- factory = (AdapterFactory) context.locateService(
+ factory = context.locateService(
"AdapterFactory", reference);
}
return factory;
@@ -61,11 +62,11 @@
return adapters;
}
- public ServiceRegistration getAdaption() {
+ public ServiceRegistration<Adaption> getAdaption() {
return adaption;
}
- public void setAdaption(ServiceRegistration adaption) {
+ public void setAdaption(ServiceRegistration<Adaption> adaption) {
this.adaption = adaption;
}
}
diff --git a/src/main/java/org/apache/sling/adapter/internal/AdapterFactoryDescriptorMap.java b/src/main/java/org/apache/sling/adapter/internal/AdapterFactoryDescriptorMap.java
index 9286a74..4de3d08 100644
--- a/src/main/java/org/apache/sling/adapter/internal/AdapterFactoryDescriptorMap.java
+++ b/src/main/java/org/apache/sling/adapter/internal/AdapterFactoryDescriptorMap.java
@@ -20,6 +20,7 @@
import java.util.TreeMap;
+import org.apache.sling.api.adapter.AdapterFactory;
import org.osgi.framework.ServiceReference;
/**
@@ -36,7 +37,7 @@
* removed the eventual second instance may actually be used instead.
*/
public class AdapterFactoryDescriptorMap extends
- TreeMap<ServiceReference, AdapterFactoryDescriptor> {
+ TreeMap<ServiceReference<AdapterFactory>, AdapterFactoryDescriptor> {
private static final long serialVersionUID = 2L;
diff --git a/src/main/java/org/apache/sling/adapter/internal/AdapterManagerImpl.java b/src/main/java/org/apache/sling/adapter/internal/AdapterManagerImpl.java
index 17c9215..bb872eb 100644
--- a/src/main/java/org/apache/sling/adapter/internal/AdapterManagerImpl.java
+++ b/src/main/java/org/apache/sling/adapter/internal/AdapterManagerImpl.java
@@ -32,13 +32,6 @@
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
-import org.apache.felix.scr.annotations.Component;
-import org.apache.felix.scr.annotations.Properties;
-import org.apache.felix.scr.annotations.Property;
-import org.apache.felix.scr.annotations.Reference;
-import org.apache.felix.scr.annotations.ReferenceCardinality;
-import org.apache.felix.scr.annotations.ReferencePolicy;
-import org.apache.felix.scr.annotations.Service;
import org.apache.sling.adapter.Adaption;
import org.apache.sling.api.SlingConstants;
import org.apache.sling.api.adapter.AdapterFactory;
@@ -49,6 +42,10 @@
import org.osgi.framework.ServiceReference;
import org.osgi.framework.ServiceRegistration;
import org.osgi.service.component.ComponentContext;
+import org.osgi.service.component.annotations.Component;
+import org.osgi.service.component.annotations.Reference;
+import org.osgi.service.component.annotations.ReferenceCardinality;
+import org.osgi.service.component.annotations.ReferencePolicy;
import org.osgi.service.event.Event;
import org.osgi.service.event.EventAdmin;
import org.osgi.service.packageadmin.PackageAdmin;
@@ -61,15 +58,11 @@
* interface to be used by any clients.
*
*/
-@Component(immediate=true)
-@Service
-@Properties({
- @Property(name=Constants.SERVICE_DESCRIPTION, value="Sling Adapter Manager"),
- @Property(name=Constants.SERVICE_VENDOR, value="The Apache Software Foundation")
-
-})
-@Reference(name="AdapterFactory", referenceInterface=AdapterFactory.class,
-cardinality=ReferenceCardinality.OPTIONAL_MULTIPLE, policy=ReferencePolicy.DYNAMIC)
+@Component(service = AdapterManager.class, immediate=true,
+ property = {
+ Constants.SERVICE_DESCRIPTION + "=Sling Adapter Manager",
+ Constants.SERVICE_VENDOR + "=The Apache Software Foundation"
+ })
public class AdapterManagerImpl implements AdapterManager {
private final Logger log = LoggerFactory.getLogger(getClass());
@@ -85,7 +78,7 @@
* the manager has been activated. These bound services will be accessed as
* soon as the manager is being activated.
*/
- private final List<ServiceReference> boundAdapterFactories = new LinkedList<ServiceReference>();
+ private final List<ServiceReference<AdapterFactory>> boundAdapterFactories = new LinkedList<>();
/**
* A map of {@link AdapterFactoryDescriptorMap} instances. The map is
@@ -95,7 +88,7 @@
*
* @see AdapterFactoryDescriptorMap
*/
- private final Map<String, AdapterFactoryDescriptorMap> descriptors = new HashMap<String, AdapterFactoryDescriptorMap>();
+ private final Map<String, AdapterFactoryDescriptorMap> descriptors = new HashMap<>();
/**
* Matrix of {@link AdapterFactoryDescriptor} instances primarily indexed by the fully
@@ -107,12 +100,12 @@
* whenever an adapter factory is registered on unregistered.
*/
private final ConcurrentMap<String, Map<String, List<AdapterFactoryDescriptor>>> factoryCache
- = new ConcurrentHashMap<String, Map<String, List<AdapterFactoryDescriptor>>>();
+ = new ConcurrentHashMap<>();
/**
* The service tracker for the event admin
*/
- @Reference(cardinality=ReferenceCardinality.OPTIONAL_UNARY, policy=ReferencePolicy.DYNAMIC)
+ @Reference(cardinality=ReferenceCardinality.OPTIONAL, policy=ReferencePolicy.DYNAMIC)
private volatile EventAdmin eventAdmin;
@Reference
@@ -126,6 +119,7 @@
*
* @see org.apache.sling.api.adapter.AdapterManager#getAdapter(java.lang.Object, java.lang.Class)
*/
+ @Override
public <AdapterType> AdapterType getAdapter(final Object adaptable,
final Class<AdapterType> type) {
@@ -171,12 +165,12 @@
this.context = context;
// register all adapter factories bound before activation
- final List<ServiceReference> refs;
+ final List<ServiceReference<AdapterFactory>> refs;
synchronized ( this.boundAdapterFactories ) {
- refs = new ArrayList<ServiceReference>(this.boundAdapterFactories);
+ refs = new ArrayList<>(this.boundAdapterFactories);
boundAdapterFactories.clear();
}
- for (final ServiceReference reference : refs) {
+ for (final ServiceReference<AdapterFactory> reference : refs) {
registerAdapterFactory(context, reference);
}
@@ -196,7 +190,9 @@
/**
* Bind a new adapter factory.
*/
- protected void bindAdapterFactory(final ServiceReference reference) {
+ @Reference(name="AdapterFactory", service=AdapterFactory.class,
+ cardinality=ReferenceCardinality.MULTIPLE, policy=ReferencePolicy.DYNAMIC)
+ protected void bindAdapterFactory(final ServiceReference<AdapterFactory> reference) {
boolean create = true;
if (context == null) {
synchronized ( this.boundAdapterFactories ) {
@@ -214,7 +210,7 @@
/**
* Unbind a adapter factory.
*/
- protected void unbindAdapterFactory(final ServiceReference reference) {
+ protected void unbindAdapterFactory(final ServiceReference<AdapterFactory> reference) {
unregisterAdapterFactory(reference);
}
@@ -245,7 +241,7 @@
* <code>reference</code> from the registry.
*/
private void registerAdapterFactory(final ComponentContext context,
- final ServiceReference reference) {
+ final ServiceReference<AdapterFactory> reference) {
final String[] adaptables = PropertiesUtil.toStringArray(reference.getProperty(ADAPTABLE_CLASSES));
final String[] adapters = PropertiesUtil.toStringArray(reference.getProperty(ADAPTER_CLASSES));
@@ -287,12 +283,12 @@
this.factoryCache.clear();
// register adaption
- final Dictionary<String, Object> props = new Hashtable<String, Object>();
+ final Dictionary<String, Object> props = new Hashtable<>();
props.put(SlingConstants.PROPERTY_ADAPTABLE_CLASSES, adaptables);
props.put(SlingConstants.PROPERTY_ADAPTER_CLASSES, adapters);
- ServiceRegistration adaptionRegistration = this.context.getBundleContext().registerService(
- Adaption.class.getName(), AdaptionImpl.INSTANCE, props);
+ ServiceRegistration<Adaption> adaptionRegistration = this.context.getBundleContext().registerService(
+ Adaption.class, AdaptionImpl.INSTANCE, props);
if (log.isDebugEnabled()) {
log.debug("Registered service {} with {} : {} and {} : {}", new Object[] { Adaption.class.getName(),
SlingConstants.PROPERTY_ADAPTABLE_CLASSES, Arrays.toString(adaptables),
@@ -307,7 +303,7 @@
props));
}
}
-
+
static String getPackageName(String clazz) {
final int lastDot = clazz.lastIndexOf('.');
return lastDot <= 0 ? "" : clazz.substring(0, lastDot);
@@ -316,13 +312,13 @@
/**
* Check that the package containing the class is exported or is a java.*
* class.
- *
+ *
* @param packageAdmin the PackageAdmin service
* @param clazz the class name
* @return true if the package is exported
*/
static boolean checkPackage(PackageAdmin packageAdmin, String clazz) {
- final String packageName = getPackageName(clazz);
+ final String packageName = getPackageName(clazz);
if (packageName.startsWith("java.")) {
return true;
}
@@ -333,7 +329,7 @@
* Unregisters the {@link AdapterFactory} referred to by the service
* <code>reference</code> from the registry.
*/
- private void unregisterAdapterFactory(final ServiceReference reference) {
+ private void unregisterAdapterFactory(final ServiceReference<AdapterFactory> reference) {
synchronized ( this.boundAdapterFactories ) {
boundAdapterFactories.remove(reference);
}
@@ -389,7 +385,7 @@
// send event
final EventAdmin localEA = this.eventAdmin;
if ( localEA != null ) {
- final Dictionary<String, Object> props = new Hashtable<String, Object>();
+ final Dictionary<String, Object> props = new Hashtable<>();
props.put(SlingConstants.PROPERTY_ADAPTABLE_CLASSES, adaptables);
props.put(SlingConstants.PROPERTY_ADAPTER_CLASSES, adapters);
localEA.postEvent(new Event(SlingConstants.TOPIC_ADAPTER_FACTORY_REMOVED,
@@ -433,7 +429,7 @@
* <code>clazz</code>.
*/
private Map<String, List<AdapterFactoryDescriptor>> createAdapterFactoryMap(final Class<?> clazz) {
- final Map<String, List<AdapterFactoryDescriptor>> afm = new HashMap<String, List<AdapterFactoryDescriptor>>();
+ final Map<String, List<AdapterFactoryDescriptor>> afm = new HashMap<>();
// AdapterFactories for this class
AdapterFactoryDescriptorMap afdMap = null;
@@ -443,7 +439,7 @@
if (afdMap != null) {
final List<AdapterFactoryDescriptor> afdSet;
synchronized ( afdMap ) {
- afdSet = new ArrayList<AdapterFactoryDescriptor>(afdMap.values());
+ afdSet = new ArrayList<>(afdMap.values());
}
for (final AdapterFactoryDescriptor afd : afdSet) {
final String[] adapters = afd.getAdapters();
@@ -451,7 +447,7 @@
// to handle service ranking, we add to the end of the list or create a new list
List<AdapterFactoryDescriptor> factoryDescriptors = afm.get(adapter);
if (factoryDescriptors == null) {
- factoryDescriptors = new ArrayList<AdapterFactoryDescriptor>();
+ factoryDescriptors = new ArrayList<>();
afm.put(adapter, factoryDescriptors);
}
factoryDescriptors.add(afd);
@@ -498,7 +494,7 @@
List<AdapterFactoryDescriptor> factoryDescriptors = dest.get(entry.getKey());
if (factoryDescriptors == null) {
- factoryDescriptors = new ArrayList<AdapterFactoryDescriptor>();
+ factoryDescriptors = new ArrayList<>();
dest.put(entry.getKey(), factoryDescriptors);
}
for (AdapterFactoryDescriptor descriptor : entry.getValue()) {
diff --git a/src/main/java/org/apache/sling/adapter/internal/AdapterWebConsolePlugin.java b/src/main/java/org/apache/sling/adapter/internal/AdapterWebConsolePlugin.java
index 012012b..258982f 100644
--- a/src/main/java/org/apache/sling/adapter/internal/AdapterWebConsolePlugin.java
+++ b/src/main/java/org/apache/sling/adapter/internal/AdapterWebConsolePlugin.java
@@ -22,6 +22,7 @@
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
+import java.io.StringReader;
import java.net.URL;
import java.util.ArrayList;
import java.util.Arrays;
@@ -32,6 +33,15 @@
import java.util.List;
import java.util.Map;
+import javax.json.Json;
+import javax.json.JsonArray;
+import javax.json.JsonArrayBuilder;
+import javax.json.JsonException;
+import javax.json.JsonNumber;
+import javax.json.JsonObject;
+import javax.json.JsonObjectBuilder;
+import javax.json.JsonString;
+import javax.json.JsonValue;
import javax.servlet.Servlet;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
@@ -39,16 +49,8 @@
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.io.IOUtils;
-import org.apache.commons.lang.builder.CompareToBuilder;
-import org.apache.felix.scr.annotations.Component;
-import org.apache.felix.scr.annotations.Properties;
-import org.apache.felix.scr.annotations.Property;
-import org.apache.felix.scr.annotations.Reference;
-import org.apache.felix.scr.annotations.Service;
+import org.apache.commons.lang3.builder.CompareToBuilder;
import org.apache.sling.api.adapter.AdapterFactory;
-import org.apache.sling.commons.json.JSONArray;
-import org.apache.sling.commons.json.JSONException;
-import org.apache.sling.commons.json.JSONObject;
import org.apache.sling.commons.osgi.PropertiesUtil;
import org.osgi.framework.Bundle;
import org.osgi.framework.BundleContext;
@@ -59,22 +61,25 @@
import org.osgi.framework.InvalidSyntaxException;
import org.osgi.framework.ServiceReference;
import org.osgi.service.component.ComponentContext;
+import org.osgi.service.component.annotations.Component;
+import org.osgi.service.component.annotations.Reference;
import org.osgi.service.packageadmin.PackageAdmin;
import org.osgi.util.tracker.ServiceTracker;
import org.osgi.util.tracker.ServiceTrackerCustomizer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
-@Component
-@Service(Servlet.class)
-@Properties({ @Property(name = Constants.SERVICE_DESCRIPTION, value = "Adapter Web Console Plugin"),
- @Property(name = Constants.SERVICE_VENDOR, value = "The Apache Software Foundation"),
- @Property(name = "felix.webconsole.label", value = "adapters"),
- @Property(name = "felix.webconsole.title", value = "Sling Adapters"),
- @Property(name = "felix.webconsole.css", value = "/adapters/res/ui/adapters.css"),
- @Property(name = "felix.webconsole.configprinter.modes", value = "always"),
- @Property(name = "felix.webconsole.category", value = "Sling")})
@SuppressWarnings("serial")
+@Component(service = Servlet.class,
+ property = {
+ Constants.SERVICE_DESCRIPTION + "=Adapter Web Console Plugin",
+ Constants.SERVICE_VENDOR + "=The Apache Software Foundation",
+ "felix.webconsole.label=adapters",
+ "felix.webconsole.title=Sling Adapters",
+ "felix.webconsole.css=/adapters/res/ui/adapters.css",
+ "felix.webconsole.configprinter.modes=always",
+ "felix.webconsole.category=Sling"
+ })
public class AdapterWebConsolePlugin extends HttpServlet implements ServiceTrackerCustomizer, BundleListener {
private static final int INDENT = 4;
@@ -84,7 +89,7 @@
private static final String ADAPTER_DEPRECATED = "adapter.deprecated";
private final Logger logger = LoggerFactory.getLogger(AdapterWebConsolePlugin.class);
-
+
@Reference
private PackageAdmin packageAdmin;
@@ -96,6 +101,7 @@
private BundleContext bundleContext;
+ @Override
public Object addingService(final ServiceReference reference) {
final Object service = this.bundleContext.getService(reference);
addServiceMetadata(reference, service);
@@ -107,7 +113,7 @@
final String condition = PropertiesUtil.toString(reference.getProperty(ADAPTER_CONDITION), null);
final boolean deprecated = PropertiesUtil.toBoolean(reference.getProperty(ADAPTER_DEPRECATED), false);
final String[] adaptables = PropertiesUtil.toStringArray(reference.getProperty(ADAPTABLE_CLASSES));
- final List<AdaptableDescription> descriptions = new ArrayList<AdaptableDescription>(adaptables.length);
+ final List<AdaptableDescription> descriptions = new ArrayList<>(adaptables.length);
for (final String adaptable : adaptables) {
descriptions.add(new AdaptableDescription(reference.getBundle(), adaptable, adapters, condition, deprecated));
}
@@ -117,6 +123,7 @@
}
}
+ @Override
public void bundleChanged(final BundleEvent event) {
if (event.getType() == BundleEvent.STOPPED) {
removeBundle(event.getBundle());
@@ -125,10 +132,12 @@
}
}
+ @Override
public void modifiedService(final ServiceReference reference, final Object service) {
addServiceMetadata(reference, service);
}
+ @Override
public void removedService(final ServiceReference reference, final Object service) {
synchronized (this) {
adapterServiceReferences.remove(reference);
@@ -138,7 +147,7 @@
@SuppressWarnings("unchecked")
private void addBundle(final Bundle bundle) {
- final List<AdaptableDescription> descs = new ArrayList<AdaptableDescription>();
+ final List<AdaptableDescription> descs = new ArrayList<>();
try {
final Enumeration<URL> files = bundle.getResources("SLING-INF/adapters.json");
if (files != null) {
@@ -146,18 +155,20 @@
final InputStream stream = files.nextElement().openStream();
final String contents = IOUtils.toString(stream);
IOUtils.closeQuietly(stream);
- final JSONObject obj = new JSONObject(contents);
- for (final Iterator<String> adaptableNames = obj.keys(); adaptableNames.hasNext();) {
+ Map<String, Object> config = new HashMap<>();
+ config.put("org.apache.johnzon.supports-comments", true);
+ final JsonObject obj = Json.createReaderFactory(config).createReader(new StringReader(contents)).readObject();
+ for (final Iterator<String> adaptableNames = obj.keySet().iterator(); adaptableNames.hasNext();) {
final String adaptableName = adaptableNames.next();
- final JSONObject adaptable = obj.getJSONObject(adaptableName);
- for (final Iterator<String> conditions = adaptable.keys(); conditions.hasNext();) {
+ final JsonObject adaptable = obj.getJsonObject(adaptableName);
+ for (final Iterator<String> conditions = adaptable.keySet().iterator(); conditions.hasNext();) {
final String condition = conditions.next();
String[] adapters;
final Object value = adaptable.get(condition);
- if (value instanceof JSONArray) {
- adapters = toStringArray((JSONArray) value);
+ if (value instanceof JsonArray) {
+ adapters = toStringArray((JsonArray) value);
} else {
- adapters = new String[] { value.toString() };
+ adapters = new String[] { unbox(value).toString() };
}
descs.add(new AdaptableDescription(bundle, adaptableName, adapters, condition, false));
}
@@ -172,7 +183,7 @@
}
} catch (final IOException e) {
logger.error("Unable to load adapter descriptors for bundle " + bundle, e);
- } catch (final JSONException e) {
+ } catch (final JsonException e) {
logger.error("Unable to load adapter descriptors for bundle " + bundle, e);
} catch (IllegalStateException e) {
logger.debug("Unable to load adapter descriptors for bundle " + bundle);
@@ -180,12 +191,32 @@
}
- private String[] toStringArray(final JSONArray value) throws JSONException {
- final List<String> result = new ArrayList<String>(value.length());
- for (int i = 0; i < value.length(); i++) {
+ private Object unbox(Object o) {
+ if (o instanceof JsonValue) {
+ switch (((JsonValue)o).getValueType()) {
+ case FALSE:
+ return false;
+ case TRUE:
+ return true;
+ case NULL:
+ return null;
+ case NUMBER:
+ return ((JsonNumber) o).isIntegral() ? ((JsonNumber)o).longValue() : ((JsonNumber) o).doubleValue();
+ case STRING:
+ return ((JsonString) o).getString();
+ default:
+ return o;
+ }
+ }
+ return o;
+ }
+
+ private String[] toStringArray(final JsonArray value) {
+ final List<String> result = new ArrayList<>(value.size());
+ for (int i = 0; i < value.size(); i++) {
result.add(value.getString(i));
}
- return result.toArray(new String[value.length()]);
+ return result.toArray(new String[value.size()]);
}
private void removeBundle(final Bundle bundle) {
@@ -196,7 +227,7 @@
}
private void update() {
- final List<AdaptableDescription> newList = new ArrayList<AdaptableDescription>();
+ final List<AdaptableDescription> newList = new ArrayList<>();
for (final List<AdaptableDescription> descriptions : adapterServiceReferences.values()) {
newList.addAll(descriptions);
}
@@ -209,8 +240,8 @@
protected void activate(final ComponentContext ctx) throws InvalidSyntaxException {
this.bundleContext = ctx.getBundleContext();
- this.adapterServiceReferences = new HashMap<ServiceReference, List<AdaptableDescription>>();
- this.adapterBundles = new HashMap<Bundle, List<AdaptableDescription>>();
+ this.adapterServiceReferences = new HashMap<>();
+ this.adapterBundles = new HashMap<>();
for (final Bundle bundle : this.bundleContext.getBundles()) {
if (bundle.getState() == Bundle.ACTIVE) {
addBundle(bundle);
@@ -243,22 +274,56 @@
private void getJson(final HttpServletResponse resp) throws ServletException, IOException {
resp.setContentType("application/json");
try {
- final JSONObject obj = new JSONObject();
+ Map<String, Map<String, List<String>>> values = new HashMap<>();
for (final AdaptableDescription desc : allAdaptables) {
- final JSONObject adaptableObj;
- if (obj.has(desc.adaptable)) {
- adaptableObj = obj.getJSONObject(desc.adaptable);
- } else {
- adaptableObj = new JSONObject();
- obj.put(desc.adaptable, adaptableObj);
+ final Map<String, List<String>> adaptableObj;
+ if (values.containsKey(desc.adaptable))
+ {
+ adaptableObj = values.get(desc.adaptable);
+ }
+ else {
+ adaptableObj = new HashMap<>();
+ values.put(desc.adaptable, adaptableObj);
}
for (final String adapter : desc.adapters) {
- adaptableObj.accumulate(desc.condition == null ? "" : desc.condition, adapter);
+ List<String> conditions = adaptableObj.get(desc.condition == null ? "" : desc.condition);
+ if (conditions == null)
+ {
+ conditions = new ArrayList<>();
+ adaptableObj.put(desc.condition == null ? "" : desc.condition, conditions);
+ }
+ conditions.add(adapter);
}
}
- resp.getWriter().println(obj.toString(INDENT));
- } catch (final JSONException e) {
+ final JsonObjectBuilder obj = Json.createObjectBuilder();
+
+ for (Map.Entry<String, Map<String, List<String>>> entry : values.entrySet())
+ {
+ JsonObjectBuilder adaptable = Json.createObjectBuilder();
+
+ for (Map.Entry<String, List<String>> subEnty : entry.getValue().entrySet())
+ {
+ if (subEnty.getValue().size() > 1)
+ {
+ JsonArrayBuilder array = Json.createArrayBuilder();
+ for (String condition : subEnty.getValue())
+ {
+ array.add(condition);
+ }
+ adaptable.add(subEnty.getKey(), array);
+ }
+ else
+ {
+ adaptable.add(subEnty.getKey(), subEnty.getValue().get(0));
+ }
+ }
+
+ obj.add(entry.getKey(), adaptable);
+ }
+
+ Json.createGenerator(resp.getWriter()).write(obj.build()).flush();
+ } catch (final JsonException e) {
throw new ServletException("Unable to produce JSON", e);
}
}
@@ -369,6 +434,7 @@
+ ", condition=" + this.condition + ", bundle=" + this.bundle + ", deprecated= " + this.deprecated + "]";
}
+ @Override
public int compareTo(final AdaptableDescription o) {
return new CompareToBuilder().append(this.adaptable, o.adaptable).append(this.condition, o.condition)
.append(this.adapters.length, o.adapters.length)
diff --git a/src/main/java/org/apache/sling/adapter/package-info.java b/src/main/java/org/apache/sling/adapter/package-info.java
new file mode 100644
index 0000000..b9ddc8d
--- /dev/null
+++ b/src/main/java/org/apache/sling/adapter/package-info.java
@@ -0,0 +1,24 @@
+/*
+ * 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.
+ */
+
+@Version("2.1.0")
+package org.apache.sling.adapter;
+
+import org.osgi.annotation.versioning.Version;
+
diff --git a/src/test/java/org/apache/sling/adapter/internal/AdapterManagerTest.java b/src/test/java/org/apache/sling/adapter/internal/AdapterManagerTest.java
index b3c9c4c..1208c85 100644
--- a/src/test/java/org/apache/sling/adapter/internal/AdapterManagerTest.java
+++ b/src/test/java/org/apache/sling/adapter/internal/AdapterManagerTest.java
@@ -18,6 +18,14 @@
*/
package org.apache.sling.adapter.internal;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertNull;
+import static org.junit.Assert.assertTrue;
+
+import java.util.Dictionary;
+import java.util.Map;
+
import org.apache.sling.adapter.Adaption;
import org.apache.sling.adapter.mock.MockAdapterFactory;
import org.apache.sling.api.adapter.AdapterFactory;
@@ -39,14 +47,7 @@
import org.osgi.service.packageadmin.ExportedPackage;
import org.osgi.service.packageadmin.PackageAdmin;
-import java.util.Dictionary;
-import java.util.Map;
-
import junitx.util.PrivateAccessor;
-import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertNotNull;
-import static org.junit.Assert.assertNull;
-import static org.junit.Assert.assertTrue;
@RunWith(JMock.class)
public class AdapterManagerTest {
@@ -100,7 +101,7 @@
allowing(bundleCtx).getServiceReferences(with(any(String.class)), with(any(String.class)));
will(returnValue(null));
allowing(bundleCtx).removeServiceListener(with(any(ServiceListener.class)));
- allowing(bundleCtx).registerService(with(Adaption.class.getName()), with(AdaptionImpl.INSTANCE), with(any(Dictionary.class)));
+ allowing(bundleCtx).registerService(with(Adaption.class), with(AdaptionImpl.INSTANCE), with(any(Dictionary.class)));
will(returnValue(null));
}});
return ctx;
@@ -126,14 +127,14 @@
allowing(bundleCtx).getServiceReferences(with(any(String.class)), with(any(String.class)));
will(returnValue(null));
allowing(bundleCtx).removeServiceListener(with(any(ServiceListener.class)));
- allowing(bundleCtx).registerService(with(Adaption.class.getName()), with(AdaptionImpl.INSTANCE), with(any(Dictionary.class)));
+ allowing(bundleCtx).registerService(with(Adaption.class), with(AdaptionImpl.INSTANCE), with(any(Dictionary.class)));
will(returnValue(null));
}});
return ctx;
}
public static <T> Matcher<T> any(Class<T> type) {
- return new IsAnything<T>();
+ return new IsAnything<>();
}
/**
@@ -156,21 +157,25 @@
this.classes = classes;
}
+ @Override
public boolean isAssignableTo(Bundle bundle, String className) {
// TODO Auto-generated method stub
return false;
}
+ @Override
public Bundle[] getUsingBundles() {
// TODO Auto-generated method stub
return null;
}
+ @Override
public String[] getPropertyKeys() {
// TODO Auto-generated method stub
return null;
}
+ @Override
public Object getProperty(String key) {
if ( key.equals(Constants.SERVICE_RANKING) ) {
return ranking;
@@ -185,11 +190,13 @@
return null;
}
+ @Override
public Bundle getBundle() {
// TODO Auto-generated method stub
return null;
}
+ @Override
public int compareTo(Object reference) {
Integer ranking1 = (Integer)getProperty(Constants.SERVICE_RANKING);
Integer ranking2 = (Integer)((ServiceReference)reference).getProperty(Constants.SERVICE_RANKING);
@@ -493,6 +500,7 @@
public class FirstImplementationAdapterFactory implements AdapterFactory {
+ @Override
@SuppressWarnings("unchecked")
public <AdapterType> AdapterType getAdapter(Object adaptable, Class<AdapterType> type) {
if (adaptable instanceof AdapterObject) {
@@ -511,6 +519,7 @@
public class SecondImplementationAdapterFactory implements AdapterFactory {
+ @Override
@SuppressWarnings("unchecked")
public <AdapterType> AdapterType getAdapter(Object adaptable, Class<AdapterType> type) {
if (adaptable instanceof AdapterObject) {