Skip to content

Instantly share code, notes, and snippets.

@justinedelson
Created November 12, 2011 02:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save justinedelson/1359911 to your computer and use it in GitHub Desktop.
Save justinedelson/1359911 to your computer and use it in GitHub Desktop.
AdapterWebConsolePlugin POC
/*
* 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.sling.adapter.internal;
import static org.apache.sling.api.adapter.AdapterFactory.*;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
import javax.servlet.Servlet;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
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.Service;
import org.apache.sling.commons.osgi.OsgiUtil;
import org.osgi.framework.Bundle;
import org.osgi.framework.BundleContext;
import org.osgi.framework.Constants;
import org.osgi.framework.Filter;
import org.osgi.framework.InvalidSyntaxException;
import org.osgi.framework.ServiceReference;
import org.osgi.service.component.ComponentContext;
import org.osgi.util.tracker.ServiceTracker;
import org.osgi.util.tracker.ServiceTrackerCustomizer;
@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")
})
@SuppressWarnings("serial")
public class AdapterWebConsolePlugin extends HttpServlet implements ServiceTrackerCustomizer {
private Map<String, List<String>> adaptableMap;
private Map<Object, AdapterDescription> adapters;
private ServiceTracker adapterTracker;
private BundleContext bundleContext;
public Object addingService(ServiceReference reference) {
Object service = this.bundleContext.getService(reference);
adapters.put(service, createDescription(reference));
update();
return service;
}
public void modifiedService(ServiceReference reference, Object service) {
adapters.put(service, createDescription(reference));
update();
}
public void removedService(ServiceReference reference, Object service) {
adapters.remove(service);
}
private AdapterDescription createDescription(ServiceReference reference) {
AdapterDescription desc = new AdapterDescription();
desc.adaptables = OsgiUtil.toStringArray(reference.getProperty(ADAPTABLE_CLASSES));
desc.adapters = OsgiUtil.toStringArray(reference.getProperty(ADAPTER_CLASSES));
desc.condition = OsgiUtil.toString(reference.getProperty("adapter.condition"), null);
desc.bundle = reference.getBundle();
return desc;
}
private void update() {
Map<String, List<String>> newMap = new TreeMap<String, List<String>>();
for (AdapterDescription desc : adapters.values()) {
for (String adaptable : desc.adaptables) {
List<String> list = newMap.get(adaptable);
if (list == null) {
list = new ArrayList<String>();
newMap.put(adaptable, list);
}
list.addAll(Arrays.asList(desc.adapters));
}
}
adaptableMap = newMap;
}
protected void activate(ComponentContext ctx) throws InvalidSyntaxException {
this.bundleContext = ctx.getBundleContext();
this.adapters = new HashMap<Object, AdapterDescription>();
Filter filter = this.bundleContext.createFilter("(&(adaptables=*)(adapters=*))");
this.adapterTracker = new ServiceTracker(this.bundleContext, filter, this);
this.adapterTracker.open();
}
protected void deactivate(ComponentContext ctx) {
this.adapterTracker.close();
this.adapters = null;
}
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
resp.getWriter().println(adapters.toString());
resp.getWriter().println(adaptableMap.toString());
}
class AdapterDescription {
private String[] adaptables;
private String[] adapters;
private Bundle bundle;
private String condition;
@Override
public String toString() {
return "AdapterDescription [adaptables=" + Arrays.toString(adaptables) + ", adapters="
+ Arrays.toString(adapters) + ", condition=" + condition + ", bundle=" + bundle + "]";
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment