Modified: felix/trunk/inventory/src/main/java/org/apache/felix/inventory/impl/webconsole/ConfigurationPrinterAdapter.java URL: http://svn.apache.org/viewvc/felix/trunk/inventory/src/main/java/org/apache/felix/inventory/impl/webconsole/ConfigurationPrinterAdapter.java?rev=1451624&r1=1451623&r2=1451624&view=diff ============================================================================== --- felix/trunk/inventory/src/main/java/org/apache/felix/inventory/impl/webconsole/ConfigurationPrinterAdapter.java (original) +++ felix/trunk/inventory/src/main/java/org/apache/felix/inventory/impl/webconsole/ConfigurationPrinterAdapter.java Fri Mar 1 15:56:39 2013 @@ -1,13 +1,13 @@ /* * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with + * 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 - * + * 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. @@ -31,7 +31,8 @@ import org.osgi.framework.ServiceReferen /** * Helper class for a configuration printer. */ -public class ConfigurationPrinterAdapter { +public class ConfigurationPrinterAdapter +{ private final Object printer; public String title; @@ -42,24 +43,30 @@ public class ConfigurationPrinterAdapter private final Method attachmentMethod; private static final List CUSTOM_MODES = new ArrayList(); - static { - CUSTOM_MODES.add( ConsoleConstants.MODE_TXT); - CUSTOM_MODES.add( ConsoleConstants.MODE_WEB ); - CUSTOM_MODES.add( ConsoleConstants.MODE_ZIP ); + 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. + * This is done manually to avoid having the configuration printer class + * available. */ - private static boolean isConfigurationPrinter(final Class clazz) { + private static boolean isConfigurationPrinter(final Class clazz) + { final Class[] interf = clazz.getInterfaces(); - for(int i=0; i<interf.length; i++) { - if ( interf[i].getName().equals(ConsoleConstants.INTERFACE_CONFIGURATION_PRINTER) ) { + for (int i = 0; i < interf.length; i++) + { + if (interf[i].getName().equals(ConsoleConstants.INTERFACE_CONFIGURATION_PRINTER)) + { return true; } } - if ( clazz.getSuperclass() != null ) { + if (clazz.getSuperclass() != null) + { return isConfigurationPrinter(clazz.getSuperclass()); } return false; @@ -68,107 +75,127 @@ public class ConfigurationPrinterAdapter /** * Try to create a new configuration printer adapter. */ - public static ConfigurationPrinterAdapter createAdapter( - final Object service, - final ServiceReference ref) { + public static ConfigurationPrinterAdapter createAdapter(final Object service, final ServiceReference ref) + { String title; Object modes = null; - if ( isConfigurationPrinter(service.getClass()) ) { + if (isConfigurationPrinter(service.getClass())) + { modes = ref.getProperty(ConsoleConstants.CONFIG_PRINTER_MODES); - if ( modes == null ) { - modes = ref.getProperty( ConsoleConstants.PROPERTY_MODES ); + if (modes == null) + { + modes = ref.getProperty(ConsoleConstants.PROPERTY_MODES); } final Method titleMethod = ClassUtils.searchMethod(service.getClass(), "getTitle", null); - if ( titleMethod == 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 ); + 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 ) { + final Method method2Params = ClassUtils.searchMethod(service.getClass(), "printConfiguration", new Class[] + { PrintWriter.class, String.class }); + if (method2Params != null) + { cfgPrinter = service; printMethod = method2Params; } - if ( cfgPrinter == null ) { + if (cfgPrinter == null) + { // second: printConfiguration(PrintWriter) - final Method method1Params = ClassUtils.searchMethod(service.getClass(), "printConfiguration", - new Class[] {PrintWriter.class}); - if ( method1Params != null ) { + 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 ); + 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 { + 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[] ) ) { + 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 { + } + else + { + if (modes instanceof String) + { + if (CUSTOM_MODES.contains(modes)) + { + modesArray = new String[] + { modes.toString() }; + } + else + { modesArray = null; } - } else { - final String[] values = (String[])modes; + } + 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]) ) { + for (int i = 0; i < values.length; i++) + { + if (!CUSTOM_MODES.contains(values[i])) + { valid = false; break; } } - if ( valid) { + if (valid) + { modesArray = values; - } else { + } + 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 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 ) { + 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; @@ -181,46 +208,65 @@ public class ConfigurationPrinterAdapter /** * Map the modes to inventory printer modes */ - public String[] getPrinterModes() { + public String[] getPrinterModes() + { final Set list = new HashSet(); - if ( this.match(ConsoleConstants.MODE_TXT) || this.match(ConsoleConstants.MODE_ZIP) ) { + if (this.match(ConsoleConstants.MODE_TXT) || this.match(ConsoleConstants.MODE_ZIP)) + { list.add(PrinterMode.TEXT.name()); } - if ( this.match(ConsoleConstants.MODE_WEB) ) { - if ( !escapeHtml ) { + if (this.match(ConsoleConstants.MODE_WEB)) + { + if (!escapeHtml) + { list.add(PrinterMode.HTML_FRAGMENT.name()); - } else { + } + else + { list.add(PrinterMode.TEXT.name()); } } return (String[]) list.toArray(new String[list.size()]); } - private boolean match(final String mode) { - if ( this.modes == null) { + 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) ) { + 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 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() { + 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}); + if (attachmentMethod != null) + { + attachments = (URL[]) ClassUtils.invoke(printer, attachmentMethod, new Object[] + { ConsoleConstants.MODE_ZIP }); } return attachments; } @@ -228,7 +274,8 @@ public class ConfigurationPrinterAdapter /** * @see java.lang.Object#toString() */ - public String toString() { + public String toString() + { return title + " (" + printer.getClass() + ")"; } }
Modified: felix/trunk/inventory/src/main/java/org/apache/felix/inventory/impl/webconsole/ConsoleConstants.java URL: http://svn.apache.org/viewvc/felix/trunk/inventory/src/main/java/org/apache/felix/inventory/impl/webconsole/ConsoleConstants.java?rev=1451624&r1=1451623&r2=1451624&view=diff ============================================================================== --- felix/trunk/inventory/src/main/java/org/apache/felix/inventory/impl/webconsole/ConsoleConstants.java (original) +++ felix/trunk/inventory/src/main/java/org/apache/felix/inventory/impl/webconsole/ConsoleConstants.java Fri Mar 1 15:56:39 2013 @@ -1,13 +1,13 @@ /* * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with + * 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 - * + * 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. @@ -16,8 +16,8 @@ */ package org.apache.felix.inventory.impl.webconsole; - -public class ConsoleConstants { +public class ConsoleConstants +{ public static final String INTERFACE_SERVLET = "javax.servlet.Servlet"; //$NON-NLS-1$ Modified: felix/trunk/inventory/src/main/java/org/apache/felix/inventory/impl/webconsole/ResourceBundleManager.java URL: http://svn.apache.org/viewvc/felix/trunk/inventory/src/main/java/org/apache/felix/inventory/impl/webconsole/ResourceBundleManager.java?rev=1451624&r1=1451623&r2=1451624&view=diff ============================================================================== --- felix/trunk/inventory/src/main/java/org/apache/felix/inventory/impl/webconsole/ResourceBundleManager.java (original) +++ felix/trunk/inventory/src/main/java/org/apache/felix/inventory/impl/webconsole/ResourceBundleManager.java Fri Mar 1 15:56:39 2013 @@ -1,24 +1,23 @@ /* * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file + * or more contributor license agreements. See the NOTICE file * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file + * 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 - * + * 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 + * KIND, either express or implied. See the License for the * specific language governing permissions and limitations * under the License. */ package org.apache.felix.inventory.impl.webconsole; - import java.io.IOException; import java.net.URL; import java.util.Enumeration; @@ -34,10 +33,10 @@ import org.osgi.framework.BundleEvent; import org.osgi.framework.BundleListener; import org.osgi.framework.Constants; - /** * The ResourceBundleManager manages resource bundle instance per OSGi Bundle. - * It contains a local cache, for bundles, but when a bundle is being unistalled, + * It contains a local cache, for bundles, but when a bundle is being + * unistalled, * its resources stored in the cache are cleaned up. */ public class ResourceBundleManager implements BundleListener @@ -47,129 +46,141 @@ public class ResourceBundleManager imple private final Map resourceBundleCaches; - /** * Creates a new object and adds self as a bundle listener - * + * * @param bundleContext the bundle context of the Web Console. */ - public ResourceBundleManager( final BundleContext bundleContext ) + public ResourceBundleManager(final BundleContext bundleContext) { this.bundleContext = bundleContext; this.resourceBundleCaches = new HashMap(); - bundleContext.addBundleListener( this ); + bundleContext.addBundleListener(this); } - /** * Removes the bundle lister. */ public void dispose() { - bundleContext.removeBundleListener( this ); + bundleContext.removeBundleListener(this); } - /** - * This method is used to retrieve a /cached/ instance of the i18n resource associated + * This method is used to retrieve a /cached/ instance of the i18n resource + * associated * with a given bundle. - * + * * @param provider the bundle, provider of the resources * @param locale the requested locale. */ - public ResourceBundle getResourceBundle( final Bundle provider ) { + public ResourceBundle getResourceBundle(final Bundle provider) + { ResourceBundle cache; - final Long key = new Long( provider.getBundleId() ); - synchronized ( resourceBundleCaches ) { - cache = (ResourceBundle) resourceBundleCaches.get( key ); - if ( cache == null && !resourceBundleCaches.containsKey(key)) { + final Long key = new Long(provider.getBundleId()); + synchronized (resourceBundleCaches) + { + cache = (ResourceBundle) resourceBundleCaches.get(key); + if (cache == null && !resourceBundleCaches.containsKey(key)) + { cache = this.loadResourceBundle(provider); - resourceBundleCaches.put( key, cache ); + resourceBundleCaches.put(key, cache); } } return cache; } - // ---------- BundleListener /** * @see org.osgi.framework.BundleListener#bundleChanged(org.osgi.framework.BundleEvent) */ - public final void bundleChanged( BundleEvent event ) + public final void bundleChanged(BundleEvent event) { - if ( event.getType() == BundleEvent.STOPPED ) + if (event.getType() == BundleEvent.STOPPED) { - final Long key = new Long( event.getBundle().getBundleId() ); - synchronized ( resourceBundleCaches ) + final Long key = new Long(event.getBundle().getBundleId()); + synchronized (resourceBundleCaches) { - resourceBundleCaches.remove( key ); + resourceBundleCaches.remove(key); } } } private static final Locale DEFAULT_LOCALE = Locale.ENGLISH; - private ResourceBundle loadResourceBundle(final Bundle bundle) { + private ResourceBundle loadResourceBundle(final Bundle bundle) + { final String path = "_" + DEFAULT_LOCALE.toString(); //$NON-NLS-1$ - final URL source = ( URL ) getResourceBundleEntries(bundle).get( path ); - if ( source != null ) { - try { - return new PropertyResourceBundle( source.openStream() ); - } catch ( final IOException ignore ) { + final URL source = (URL) getResourceBundleEntries(bundle).get(path); + if (source != null) + { + try + { + return new PropertyResourceBundle(source.openStream()); + } + catch (final IOException ignore) + { // ignore } } return null; } - // TODO : Instead of getting all property files, we could just get the one for the default locale + // TODO : Instead of getting all property files, we could just get the one + // for the default locale private synchronized Map getResourceBundleEntries(final Bundle bundle) { - String file = ( String ) bundle.getHeaders().get( Constants.BUNDLE_LOCALIZATION ); - if ( file == null ) + String file = (String) bundle.getHeaders().get(Constants.BUNDLE_LOCALIZATION); + if (file == null) { file = Constants.BUNDLE_LOCALIZATION_DEFAULT_BASENAME; } // remove leading slash - if ( file.startsWith( "/" ) ) //$NON-NLS-1$ + if (file.startsWith("/")) //$NON-NLS-1$ { - file = file.substring( 1 ); + file = file.substring(1); } // split path and base name - int slash = file.lastIndexOf( '/' ); - String fileName = file.substring( slash + 1 ); - String path = ( slash <= 0 ) ? "/" : file.substring( 0, slash ); //$NON-NLS-1$ + int slash = file.lastIndexOf('/'); + String fileName = file.substring(slash + 1); + String path = (slash <= 0) ? "/" : file.substring(0, slash); //$NON-NLS-1$ HashMap resourceBundleEntries = new HashMap(); - Enumeration locales = bundle.findEntries( path, fileName + "*.properties", false ); //$NON-NLS-1$ - if ( locales != null ) + Enumeration locales = bundle.findEntries(path, fileName + "*.properties", false); //$NON-NLS-1$ + if (locales != null) { - while ( locales.hasMoreElements() ) + while (locales.hasMoreElements()) { - URL entry = ( URL ) locales.nextElement(); + URL entry = (URL) locales.nextElement(); // calculate the key String entryPath = entry.getPath(); - final int start = entryPath.lastIndexOf( '/' ) + 1 + fileName.length(); // path, slash and base name + final int start = entryPath.lastIndexOf('/') + 1 + fileName.length(); // path, + // slash + // and + // base + // name final int end = entryPath.length() - 11; // .properties suffix - entryPath = entryPath.substring( start, end ); + entryPath = entryPath.substring(start, end); // the default language is "name.properties" thus the entry // path is empty and must default to "_"+DEFAULT_LOCALE - if (entryPath.length() == 0) { + if (entryPath.length() == 0) + { entryPath = "_" + DEFAULT_LOCALE; //$NON-NLS-1$ } // only add this entry, if the "language" is not provided // by the main bundle or an earlier bound fragment - if (!resourceBundleEntries.containsKey( entryPath )) { - resourceBundleEntries.put( entryPath, entry ); + if (!resourceBundleEntries.containsKey(entryPath)) + { + resourceBundleEntries.put(entryPath, entry); } } } Modified: felix/trunk/inventory/src/main/java/org/apache/felix/inventory/impl/webconsole/WebConsoleAdapter.java URL: http://svn.apache.org/viewvc/felix/trunk/inventory/src/main/java/org/apache/felix/inventory/impl/webconsole/WebConsoleAdapter.java?rev=1451624&r1=1451623&r2=1451624&view=diff ============================================================================== --- felix/trunk/inventory/src/main/java/org/apache/felix/inventory/impl/webconsole/WebConsoleAdapter.java (original) +++ felix/trunk/inventory/src/main/java/org/apache/felix/inventory/impl/webconsole/WebConsoleAdapter.java Fri Mar 1 15:56:39 2013 @@ -1,13 +1,13 @@ /* * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with + * 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 - * + * 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. @@ -16,7 +16,6 @@ */ package org.apache.felix.inventory.impl.webconsole; - import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; @@ -43,12 +42,12 @@ import org.osgi.framework.ServiceRegistr import org.osgi.util.tracker.ServiceTracker; import org.osgi.util.tracker.ServiceTrackerCustomizer; - /** * The web console adapter registers web console status printers * as inventory printers. */ -public class WebConsoleAdapter implements ServiceTrackerCustomizer { +public class WebConsoleAdapter implements ServiceTrackerCustomizer +{ private final BundleContext bundleContext; @@ -58,49 +57,58 @@ public class WebConsoleAdapter implement private final ResourceBundleManager rbManager; - public WebConsoleAdapter(final BundleContext btx) throws InvalidSyntaxException { + public WebConsoleAdapter(final BundleContext btx) throws InvalidSyntaxException + { this.bundleContext = btx; this.rbManager = new ResourceBundleManager(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 = 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() { + public void dispose() + { this.cfgPrinterTracker.close(); final List regs = new ArrayList(); - synchronized ( this.registrations ) { - regs.addAll( this.registrations.values() ); + synchronized (this.registrations) + { + regs.addAll(this.registrations.values()); this.registrations.clear(); } final Iterator i = regs.iterator(); - while (i.hasNext()) { + while (i.hasNext()) + { final ServiceRegistration reg = (ServiceRegistration) i.next(); reg.unregister(); } this.rbManager.dispose(); } - private void add(final ServiceReference reference, final Object service) { + private 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("%") ) { + if (cpa != null && cpa.title != null) + { + if (cpa.title.startsWith("%")) + { final String key = cpa.title.substring(1); final ResourceBundle rb = this.rbManager.getResourceBundle(reference.getBundle()); - if ( rb == null || !rb.containsKey(key) ) { + if (rb == null || !rb.containsKey(key)) + { cpa.title = key; - } else { + } + else + { cpa.title = rb.getString(key); } } - if ( cpa.label == null ) { + if (cpa.label == null) + { cpa.label = cpa.title; } final Dictionary props = new Hashtable(); @@ -108,19 +116,24 @@ public class WebConsoleAdapter implement props.put(InventoryPrinter.CONFIG_TITLE, cpa.title); props.put(InventoryPrinter.CONFIG_PRINTER_MODES, cpa.getPrinterModes()); - final ServiceRegistration reg = this.bundleContext.registerService(InventoryPrinter.class.getName(), new WebConsolePrinter(cpa), props); - synchronized ( this.registrations ) { + final ServiceRegistration reg = this.bundleContext.registerService(InventoryPrinter.class.getName(), + new WebConsolePrinter(cpa), props); + synchronized (this.registrations) + { this.registrations.put(reference, reg); } } } - private final void remove(final ServiceReference reference) { + private final void remove(final ServiceReference reference) + { final ServiceRegistration reg; - synchronized ( this.registrations ) { + synchronized (this.registrations) + { reg = (ServiceRegistration) this.registrations.remove(reference); } - if ( reg != null ) { + if (reg != null) + { reg.unregister(); } } @@ -128,87 +141,123 @@ public class WebConsoleAdapter implement /** * @see org.osgi.util.tracker.ServiceTrackerCustomizer#addingService(org.osgi.framework.ServiceReference) */ - public Object addingService(final ServiceReference reference) { + public Object addingService(final ServiceReference reference) + { final Object service = this.bundleContext.getService(reference); - if ( service != null ) { + if (service != null) + { this.add(reference, service); } return service; } + /** - * @see org.osgi.util.tracker.ServiceTrackerCustomizer#modifiedService(org.osgi.framework.ServiceReference, java.lang.Object) + * @see org.osgi.util.tracker.ServiceTrackerCustomizer#modifiedService(org.osgi.framework.ServiceReference, + * java.lang.Object) */ - public void modifiedService(final ServiceReference reference, final Object service) { + 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) + * @see org.osgi.util.tracker.ServiceTrackerCustomizer#removedService(org.osgi.framework.ServiceReference, + * java.lang.Object) */ - public void removedService(final ServiceReference reference, final Object service) { + public void removedService(final ServiceReference reference, final Object service) + { this.remove(reference); this.bundleContext.ungetService(reference); } - private static class WebConsolePrinter implements InventoryPrinter, ZipAttachmentProvider { + private static class WebConsolePrinter implements InventoryPrinter, ZipAttachmentProvider + { final ConfigurationPrinterAdapter cpa; - public WebConsolePrinter(final ConfigurationPrinterAdapter cpa) { + public WebConsolePrinter(final ConfigurationPrinterAdapter cpa) + { this.cpa = cpa; } /** - * @see org.apache.felix.inventory.InventoryPrinter#print(org.apache.felix.inventory.PrinterMode, java.io.PrintWriter) + * @see org.apache.felix.inventory.InventoryPrinter#print(org.apache.felix.inventory.PrinterMode, + * java.io.PrintWriter) */ - public void print(final PrinterMode mode, final PrintWriter printWriter, final boolean isZip ) { + public void print(final PrinterMode mode, final PrintWriter printWriter, final boolean isZip) + { final String m; - if ( !isZip && mode == PrinterMode.HTML_FRAGMENT ) { + if (!isZip && mode == PrinterMode.HTML_FRAGMENT) + { m = ConsoleConstants.MODE_WEB; - } else if ( !isZip && mode == PrinterMode.TEXT ) { + } + else if (!isZip && mode == PrinterMode.TEXT) + { m = ConsoleConstants.MODE_TXT; - } else if (isZip && (mode == PrinterMode.TEXT || mode == PrinterMode.HTML_FRAGMENT) ) { + } + else if (isZip && (mode == PrinterMode.TEXT || mode == PrinterMode.HTML_FRAGMENT)) + { m = ConsoleConstants.MODE_ZIP; - } else { + } + else + { m = null; } - if ( m != null ) { + if (m != null) + { cpa.printConfiguration(printWriter, m); } } /** - * @see org.apache.felix.inventory.ZipAttachmentProvider#addAttachments(java.lang.String, java.util.zip.ZipOutputStream) + * @see org.apache.felix.inventory.ZipAttachmentProvider#addAttachments(java.lang.String, + * java.util.zip.ZipOutputStream) */ - public void addAttachments(final String namePrefix, final ZipOutputStream zos) - throws IOException { + public void addAttachments(final String namePrefix, final ZipOutputStream zos) throws IOException + { final URL[] attachments = cpa.getAttachments(); - if ( attachments != null ) { - for(int i=0;i<attachments.length;i++) { + if (attachments != null) + { + for (int i = 0; i < attachments.length; i++) + { final URL current = attachments[i]; final String path = current.getPath(); final String name; - if ( path == null || path.length() == 0 ) { + if (path == null || path.length() == 0) + { // sanity code, we should have a path, but if not let's // just create some random name - name = "file" + Double.doubleToLongBits( Math.random() ); - } else { + name = "file" + Double.doubleToLongBits(Math.random()); + } + else + { final int pos = path.lastIndexOf('/'); name = (pos == -1 ? path : path.substring(pos + 1)); } final ZipEntry entry = new ZipEntry(namePrefix + name); zos.putNextEntry(entry); final InputStream is = current.openStream(); - try { + try + { byte[] buffer = new byte[4096]; int n = 0; - while (-1 != (n = is.read(buffer))) { + while (-1 != (n = is.read(buffer))) + { zos.write(buffer, 0, n); } - } finally { - if ( is != null ) { - try { is.close(); } catch (final IOException ignore) {} + } + finally + { + if (is != null) + { + try + { + is.close(); + } + catch (final IOException ignore) + { + } } } zos.closeEntry();
