You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@felix.apache.org by cz...@apache.org on 2012/12/20 16:54:42 UTC
svn commit: r1424547 [2/2] - in /felix/sandbox/cziegeler/status-printer: ./
src/ src/main/ src/main/java/ src/main/java/org/ src/main/java/org/apache/
src/main/java/org/apache/felix/ src/main/java/org/apache/felix/status/
src/main/java/org/apache/felix...
Added: felix/sandbox/cziegeler/status-printer/src/main/java/org/apache/felix/status/impl/webconsole/ConfigurationPrinterAdapter.java
URL: http://svn.apache.org/viewvc/felix/sandbox/cziegeler/status-printer/src/main/java/org/apache/felix/status/impl/webconsole/ConfigurationPrinterAdapter.java?rev=1424547&view=auto
==============================================================================
--- felix/sandbox/cziegeler/status-printer/src/main/java/org/apache/felix/status/impl/webconsole/ConfigurationPrinterAdapter.java (added)
+++ felix/sandbox/cziegeler/status-printer/src/main/java/org/apache/felix/status/impl/webconsole/ConfigurationPrinterAdapter.java Thu Dec 20 15:54:41 2012
@@ -0,0 +1,234 @@
+/*
+ * 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.status.impl.webconsole;
+
+import java.io.PrintWriter;
+import java.lang.reflect.Method;
+import java.net.URL;
+import java.util.ArrayList;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Set;
+
+import org.apache.felix.status.PrinterMode;
+import org.apache.felix.status.impl.ClassUtils;
+import org.osgi.framework.ServiceReference;
+
+/**
+ * Helper class for a configuration printer.
+ */
+public class ConfigurationPrinterAdapter {
+
+ private final Object printer;
+ public String title;
+ public String label;
+ private final String[] modes;
+ private final boolean escapeHtml;
+ private final Method printMethod;
+ private final Method attachmentMethod;
+
+ private static final List<String> CUSTOM_MODES = new ArrayList<String>();
+ static {
+ CUSTOM_MODES.add( ConsoleConstants.MODE_TXT);
+ CUSTOM_MODES.add( ConsoleConstants.MODE_WEB );
+ CUSTOM_MODES.add( ConsoleConstants.MODE_ZIP );
+ }
+
+ /**
+ * Check whether the class implements the configuration printer.
+ * This is done manually to avoid having the configuration printer class available.
+ */
+ private static boolean isConfigurationPrinter(final Class<?> clazz) {
+ for(final Class<?> i : clazz.getInterfaces() ) {
+ if ( i.getName().equals(ConsoleConstants.INTERFACE_CONFIGURATION_PRINTER) ) {
+ return true;
+ }
+ }
+ if ( clazz.getSuperclass() != null ) {
+ return isConfigurationPrinter(clazz.getSuperclass());
+ }
+ return false;
+ }
+
+ /**
+ * Try to create a new configuration printer adapter.
+ */
+ public static ConfigurationPrinterAdapter createAdapter(
+ final Object service,
+ final ServiceReference ref) {
+ String title;
+ Object modes = null;
+ if ( isConfigurationPrinter(service.getClass()) ) {
+ modes = ref.getProperty(ConsoleConstants.CONFIG_PRINTER_MODES);
+ if ( modes == null ) {
+ modes = ref.getProperty( ConsoleConstants.PROPERTY_MODES );
+ }
+ final Method titleMethod = ClassUtils.searchMethod(service.getClass(), "getTitle", null);
+ if ( titleMethod == null ) {
+ return null;
+ }
+ title = (String)ClassUtils.invoke(service, titleMethod, null);
+ } else {
+ modes = ref.getProperty( ConsoleConstants.CONFIG_PRINTER_MODES );
+ title = (String)ref.getProperty( ConsoleConstants.PLUGIN_TITLE );
+ }
+
+ Object cfgPrinter = null;
+ Method printMethod = null;
+
+ // first: printConfiguration(PrintWriter, String)
+ final Method method2Params = ClassUtils.searchMethod(service.getClass(), "printConfiguration",
+ new Class[] {PrintWriter.class, String.class});
+ if ( method2Params != null ) {
+ cfgPrinter = service;
+ printMethod = method2Params;
+ }
+
+ if ( cfgPrinter == null ) {
+ // second: printConfiguration(PrintWriter)
+ final Method method1Params = ClassUtils.searchMethod(service.getClass(), "printConfiguration",
+ new Class[] {PrintWriter.class});
+ if ( method1Params != null ) {
+ cfgPrinter = service;
+ printMethod = method1Params;
+ }
+ }
+
+ if ( cfgPrinter != null ) {
+ final Object label = ref.getProperty( ConsoleConstants.PLUGIN_LABEL );
+ // check escaping
+ boolean webUnescaped;
+ Object ehObj = ref.getProperty( ConsoleConstants.CONFIG_PRINTER_WEB_UNESCAPED );
+ if ( ehObj instanceof Boolean ) {
+ webUnescaped = ( ( Boolean ) ehObj ).booleanValue();
+ } else if ( ehObj instanceof String ) {
+ webUnescaped = Boolean.valueOf( ( String ) ehObj ).booleanValue();
+ } else {
+ webUnescaped = false;
+ }
+
+ final String[] modesArray;
+ // check modes
+ if ( modes == null || !( modes instanceof String || modes instanceof String[] ) ) {
+ modesArray = null;
+ } else {
+ if ( modes instanceof String ) {
+ if ( CUSTOM_MODES.contains(modes) ) {
+ modesArray = new String[] {modes.toString()};
+ } else {
+ modesArray = null;
+ }
+ } else {
+ final String[] values = (String[])modes;
+ boolean valid = values.length > 0;
+ for(int i=0; i<values.length; i++) {
+ if ( !CUSTOM_MODES.contains(values[i]) ) {
+ valid = false;
+ break;
+ }
+ }
+ if ( valid) {
+ modesArray = values;
+ } else {
+ modesArray = null;
+ }
+ }
+ }
+
+ return new ConfigurationPrinterAdapter(
+ cfgPrinter,
+ printMethod,
+ ClassUtils.searchMethod(cfgPrinter.getClass(), "getAttachments", new Class[] {String.class}),
+ title,
+ (label instanceof String ? (String)label : null),
+ modesArray,
+ !webUnescaped);
+ }
+ return null;
+ }
+
+ private ConfigurationPrinterAdapter( final Object printer,
+ final Method printMethod,
+ final Method attachmentMethod,
+ final String title,
+ final String label,
+ final String[] modesArray,
+ final boolean escapeHtml ) {
+ this.printer = printer;
+ this.title = title;
+ this.label = label;
+ this.escapeHtml = escapeHtml;
+ this.printMethod = printMethod;
+ this.attachmentMethod = attachmentMethod;
+ this.modes = modesArray;
+ }
+
+ /**
+ * Map the modes to status printer modes
+ */
+ public String[] getPrinterModes() {
+ final Set<String> list = new HashSet<String>();
+ if ( this.match(ConsoleConstants.MODE_TXT) || this.match(ConsoleConstants.MODE_ZIP) ) {
+ list.add(PrinterMode.ZIP_FILE.name());
+ }
+ if ( this.match(ConsoleConstants.MODE_WEB) ) {
+ if ( !escapeHtml ) {
+ list.add(PrinterMode.HTML_BODY.name());
+ } else {
+ list.add(PrinterMode.TEXT.name());
+ }
+ }
+ return list.toArray(new String[list.size()]);
+ }
+
+ private boolean match(final String mode) {
+ if ( this.modes == null) {
+ return true;
+ }
+ for(int i=0; i<this.modes.length; i++) {
+ if ( this.modes[i].equals(mode) ) {
+ return true;
+ }
+ }
+ return false;
+ }
+
+ public final void printConfiguration( final PrintWriter pw, final String mode ) {
+ if ( printMethod.getParameterTypes().length > 1 ) {
+ ClassUtils.invoke(this.printer, this.printMethod, new Object[] {pw, mode});
+ } else {
+ ClassUtils.invoke(this.printer, this.printMethod, new Object[] {pw});
+ }
+ }
+
+ public URL[] getAttachments() {
+ // check if printer implements binary configuration printer
+ URL[] attachments = null;
+ if ( attachmentMethod != null ) {
+ attachments = (URL[])ClassUtils.invoke(printer, attachmentMethod, new Object[] {ConsoleConstants.MODE_ZIP});
+ }
+ return attachments;
+ }
+
+ /**
+ * @see java.lang.Object#toString()
+ */
+ @Override
+ public String toString() {
+ return title + " (" + printer.getClass() + ")";
+ }
+}
Propchange: felix/sandbox/cziegeler/status-printer/src/main/java/org/apache/felix/status/impl/webconsole/ConfigurationPrinterAdapter.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: felix/sandbox/cziegeler/status-printer/src/main/java/org/apache/felix/status/impl/webconsole/ConfigurationPrinterAdapter.java
------------------------------------------------------------------------------
svn:keywords = author date id revision rev url
Added: felix/sandbox/cziegeler/status-printer/src/main/java/org/apache/felix/status/impl/webconsole/ConsoleConstants.java
URL: http://svn.apache.org/viewvc/felix/sandbox/cziegeler/status-printer/src/main/java/org/apache/felix/status/impl/webconsole/ConsoleConstants.java?rev=1424547&view=auto
==============================================================================
--- felix/sandbox/cziegeler/status-printer/src/main/java/org/apache/felix/status/impl/webconsole/ConsoleConstants.java (added)
+++ felix/sandbox/cziegeler/status-printer/src/main/java/org/apache/felix/status/impl/webconsole/ConsoleConstants.java Thu Dec 20 15:54:41 2012
@@ -0,0 +1,43 @@
+/*
+ * 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.status.impl.webconsole;
+
+
+public class ConsoleConstants {
+
+ public static final String INTERFACE_CONFIGURATION_PRINTER = "org.apache.felix.webconsole.ConfigurationPrinter"; //$NON-NLS-1$
+
+ public static final String PLUGIN_LABEL = "felix.webconsole.label"; //$NON-NLS-1$
+
+ public static final String PLUGIN_TITLE = "felix.webconsole.title"; //$NON-NLS-1$
+
+ public static final String PLUGIN_CATEGORY = "felix.webconsole.category"; //$NON-NLS-1$
+
+ public static final String CONFIG_PRINTER_MODES = "felix.webconsole.configprinter.modes"; //$NON-NLS-1$
+
+ public static final String CONFIG_PRINTER_WEB_UNESCAPED = "felix.webconsole.configprinter.web.unescaped"; //$NON-NLS-1$
+
+ public static final String MODE_ALWAYS = "always"; //$NON-NLS-1$
+
+ public static final String MODE_WEB = "web"; //$NON-NLS-1$
+
+ public static final String MODE_ZIP = "zip"; //$NON-NLS-1$
+
+ public static final String MODE_TXT = "txt"; //$NON-NLS-1$
+
+ public static final String PROPERTY_MODES = "modes"; //$NON-NLS-1$
+}
Propchange: felix/sandbox/cziegeler/status-printer/src/main/java/org/apache/felix/status/impl/webconsole/ConsoleConstants.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: felix/sandbox/cziegeler/status-printer/src/main/java/org/apache/felix/status/impl/webconsole/ConsoleConstants.java
------------------------------------------------------------------------------
svn:keywords = author date id revision rev url
Added: felix/sandbox/cziegeler/status-printer/src/main/java/org/apache/felix/status/impl/webconsole/WebConsoleAdapter.java
URL: http://svn.apache.org/viewvc/felix/sandbox/cziegeler/status-printer/src/main/java/org/apache/felix/status/impl/webconsole/WebConsoleAdapter.java?rev=1424547&view=auto
==============================================================================
--- felix/sandbox/cziegeler/status-printer/src/main/java/org/apache/felix/status/impl/webconsole/WebConsoleAdapter.java (added)
+++ felix/sandbox/cziegeler/status-printer/src/main/java/org/apache/felix/status/impl/webconsole/WebConsoleAdapter.java Thu Dec 20 15:54:41 2012
@@ -0,0 +1,159 @@
+/*
+ * 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.status.impl.webconsole;
+
+
+import java.io.PrintWriter;
+import java.net.URL;
+import java.util.Dictionary;
+import java.util.HashMap;
+import java.util.Hashtable;
+import java.util.Map;
+
+import org.apache.felix.status.PrinterMode;
+import org.apache.felix.status.StatusPrinter;
+import org.apache.felix.status.ZipAttachmentProvider;
+import org.osgi.framework.BundleContext;
+import org.osgi.framework.Constants;
+import org.osgi.framework.InvalidSyntaxException;
+import org.osgi.framework.ServiceReference;
+import org.osgi.framework.ServiceRegistration;
+import org.osgi.util.tracker.ServiceTracker;
+import org.osgi.util.tracker.ServiceTrackerCustomizer;
+
+
+/**
+ */
+public class WebConsoleAdapter implements ServiceTrackerCustomizer {
+
+ private final BundleContext bundleContext;
+
+ private final ServiceTracker cfgPrinterTracker;
+
+ private final Map<ServiceReference, ServiceRegistration> registrations = new HashMap<ServiceReference, ServiceRegistration>();
+
+ public WebConsoleAdapter(final BundleContext btx) throws InvalidSyntaxException {
+ this.bundleContext = btx;
+ this.cfgPrinterTracker = new ServiceTracker( this.bundleContext,
+ this.bundleContext.createFilter("(|(" + Constants.OBJECTCLASS + "=" + ConsoleConstants.INTERFACE_CONFIGURATION_PRINTER + ")" +
+ "(&(" + ConsoleConstants.PLUGIN_LABEL + "=*)(&("
+ + ConsoleConstants.PLUGIN_TITLE + "=*)("
+ + ConsoleConstants.CONFIG_PRINTER_MODES + "=*))))"),
+ this );
+ this.cfgPrinterTracker.open();
+ }
+
+ /**
+ * Dispose this service
+ */
+ public void dispose() {
+ this.cfgPrinterTracker.close();
+ synchronized ( this.registrations ) {
+ for(final ServiceRegistration reg : this.registrations.values()) {
+ reg.unregister();
+ }
+ this.registrations.clear();
+ }
+ }
+
+ public void add(final ServiceReference reference, final Object service) {
+ final ConfigurationPrinterAdapter cpa = ConfigurationPrinterAdapter.createAdapter(service, reference);
+ if ( cpa != null && cpa.title != null ) {
+ if ( cpa.title.startsWith("%") ) {
+ cpa.title = cpa.title.substring(1);
+ }
+ if ( cpa.label == null ) {
+ cpa.label = cpa.title;
+ }
+ final Dictionary<String, Object> props = new Hashtable<String, Object>();
+ props.put(StatusPrinter.CONFIG_NAME, cpa.label);
+ props.put(StatusPrinter.CONFIG_TITLE, cpa.title);
+ props.put(StatusPrinter.CONFIG_PRINTER_MODES, cpa.getPrinterModes());
+
+ if ( reference.getProperty(ConsoleConstants.PLUGIN_CATEGORY) != null ) {
+ props.put(StatusPrinter.CONFIG_CATEGORY, reference.getProperty(ConsoleConstants.PLUGIN_CATEGORY));
+ }
+ final ServiceRegistration reg = this.bundleContext.registerService(StatusPrinter.class.getName(), new ZipAttachmentProvider() {
+
+ /**
+ * @see org.apache.felix.status.StatusPrinter#print(org.apache.felix.status.PrinterMode, java.io.PrintWriter)
+ */
+ public void print(final PrinterMode mode, final PrintWriter printWriter) {
+ final String m;
+ if ( mode == PrinterMode.HTML_BODY ) {
+ m = ConsoleConstants.MODE_WEB;
+ } else if ( mode == PrinterMode.TEXT ) {
+ m = ConsoleConstants.MODE_TXT;
+ } else if ( mode == PrinterMode.ZIP_FILE ) {
+ m = ConsoleConstants.MODE_ZIP;
+ } else {
+ m = null;
+ }
+ if ( m != null ) {
+ cpa.printConfiguration(printWriter, m);
+ }
+ }
+
+ /**
+ * @see org.apache.felix.status.ZipAttachmentProvider#getAttachments()
+ */
+ public URL[] getAttachments() {
+ return cpa.getAttachments();
+ }
+ }, props);
+ synchronized ( this.registrations ) {
+ this.registrations.put(reference, reg);
+ }
+ }
+ }
+
+ private final void remove(final ServiceReference reference) {
+ final ServiceRegistration reg;
+ synchronized ( this.registrations ) {
+ reg = this.registrations.remove(reference);
+ }
+ if ( reg != null ) {
+ reg.unregister();
+ }
+ }
+
+ /**
+ * @see org.osgi.util.tracker.ServiceTrackerCustomizer#addingService(org.osgi.framework.ServiceReference)
+ */
+ public Object addingService(final ServiceReference reference) {
+ final Object service = this.bundleContext.getService(reference);
+ if ( service != null ) {
+ this.add(reference, service);
+ }
+ return service;
+ }
+ /**
+ * @see org.osgi.util.tracker.ServiceTrackerCustomizer#modifiedService(org.osgi.framework.ServiceReference, java.lang.Object)
+ */
+ public void modifiedService(final ServiceReference reference, final Object service) {
+ this.remove(reference);
+ this.add(reference, service);
+ }
+
+ /**
+ * @see org.osgi.util.tracker.ServiceTrackerCustomizer#removedService(org.osgi.framework.ServiceReference, java.lang.Object)
+ */
+ public void removedService(final ServiceReference reference, final Object service) {
+ this.remove(reference);
+ this.bundleContext.ungetService(reference);
+ }
+}
Propchange: felix/sandbox/cziegeler/status-printer/src/main/java/org/apache/felix/status/impl/webconsole/WebConsoleAdapter.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: felix/sandbox/cziegeler/status-printer/src/main/java/org/apache/felix/status/impl/webconsole/WebConsoleAdapter.java
------------------------------------------------------------------------------
svn:keywords = author date id revision rev url