This is an automated email from the ASF dual-hosted git repository.

cziegeler pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/felix-dev.git


The following commit(s) were added to refs/heads/master by this push:
     new c1bfa0646d FELIX-6531 : Web Console 4.8.0 does not generate metatype 
configurations for services. Clean up code a little bit
c1bfa0646d is described below

commit c1bfa0646dc913afd4ee1b90c3c7aa5dfcdeae0f
Author: Carsten Ziegeler <[email protected]>
AuthorDate: Thu May 19 12:06:45 2022 +0200

    FELIX-6531 : Web Console 4.8.0 does not generate metatype configurations 
for services. Clean up code a little bit
---
 .../internal/configuration/ConfigJsonSupport.java  |  42 +----
 .../configuration/MetaTypeServiceSupport.java      | 183 +++++++--------------
 .../internal/configuration/MetaTypeSupport.java    |  19 +--
 3 files changed, 72 insertions(+), 172 deletions(-)

diff --git 
a/webconsole/src/main/java/org/apache/felix/webconsole/internal/configuration/ConfigJsonSupport.java
 
b/webconsole/src/main/java/org/apache/felix/webconsole/internal/configuration/ConfigJsonSupport.java
index 767aaeface..5c06570766 100644
--- 
a/webconsole/src/main/java/org/apache/felix/webconsole/internal/configuration/ConfigJsonSupport.java
+++ 
b/webconsole/src/main/java/org/apache/felix/webconsole/internal/configuration/ConfigJsonSupport.java
@@ -20,7 +20,6 @@ package org.apache.felix.webconsole.internal.configuration;
 
 
 import java.io.IOException;
-import java.io.InputStream;
 import java.io.PrintWriter;
 import java.lang.reflect.Array;
 import java.util.ArrayList;
@@ -337,23 +336,11 @@ class ConfigJsonSupport {
                 }
 
                 // insert and entry for the PID
-                if ( mtss != null )
-                {
-                    try
-                    {
-                        ObjectClassDefinition ocd = 
mtss.getObjectClassDefinition( cfgs[i], locale );
-                        if ( ocd != null )
-                        {
-                            optionsPlain.put( pid, ocd.getName() );
-                            continue;
-                        }
-                    }
-                    catch ( IllegalArgumentException t )
-                    {
-                        // MetaTypeProvider.getObjectClassDefinition might 
throw illegal
-                        // argument exception. So we must catch it here, 
otherwise the
-                        // other configurations will not be shown
-                        // See https://issues.apache.org/jira/browse/FELIX-2390
+                if ( mtss != null ) {
+                    final ObjectClassDefinition ocd = 
mtss.getObjectClassDefinition( cfgs[i], locale );
+                    if ( ocd != null ) {
+                        optionsPlain.put( pid, ocd.getName() );
+                        continue;
                     }
                 }
 
@@ -452,7 +439,7 @@ class ConfigJsonSupport {
             if (value == null) {
                 value = "";
             }
-            matcher.appendReplacement(sb, matcherQuoteReplacement(value));
+            matcher.appendReplacement(sb, Matcher.quoteReplacement(value));
         }
         matcher.appendTail(sb);
 
@@ -508,23 +495,6 @@ class ConfigJsonSupport {
         }
     }
 
-    /**
-     * Replacement for Matcher.quoteReplacement which is only available in JDK 
1.5 and up.
-     * @param str Unquoted string
-     * @return Quoted string
-     */
-    private static String matcherQuoteReplacement(String str) {
-        StringBuffer sb = new StringBuffer();
-        for (int i = 0; i < str.length(); i++) {
-            char c = str.charAt(i);
-            if (c == '$' || c == '\\') {
-                sb.append('\\');
-            }
-            sb.append(c);
-        }
-        return sb.toString();
-    }
-
     final void listFactoryConfigurations(final JSONWriter jw, final String 
pidFilter, final String locale) {
         try {
             final Map<String, String> optionsFactory = 
getServices(ManagedServiceFactory.class.getName(),
diff --git 
a/webconsole/src/main/java/org/apache/felix/webconsole/internal/configuration/MetaTypeServiceSupport.java
 
b/webconsole/src/main/java/org/apache/felix/webconsole/internal/configuration/MetaTypeServiceSupport.java
index e6794abe37..fb0def4c0b 100644
--- 
a/webconsole/src/main/java/org/apache/felix/webconsole/internal/configuration/MetaTypeServiceSupport.java
+++ 
b/webconsole/src/main/java/org/apache/felix/webconsole/internal/configuration/MetaTypeServiceSupport.java
@@ -35,38 +35,24 @@ import org.osgi.service.metatype.ObjectClassDefinition;
  * methods mostly with respect to using the MetaTypeService to access
  * configuration descriptions.
  */
-class MetaTypeServiceSupport extends MetaTypeSupport
-{
+class MetaTypeServiceSupport extends MetaTypeSupport {
 
     private final BundleContext bundleContext;
 
     private final MetaTypeService service;
 
     /**
-     *
-     * @param bundleContext
-     * @param service
+     * Create a new support object
+     * @param bundleContext The bundle context
+     * @param service The metatype service
      *
      * @throws ClassCastException if {@code service} is not a MetaTypeService 
instances
      */
-    MetaTypeServiceSupport( final BundleContext bundleContext, final Object 
service )
-    {
-        super();
+    MetaTypeServiceSupport( final BundleContext bundleContext, final Object 
service ) {
         this.bundleContext = bundleContext;
         this.service = ( MetaTypeService ) service;
     }
 
-    public BundleContext getBundleContext()
-    {
-        return bundleContext;
-    }
-
-    public MetaTypeService getMetaTypeService()
-    {
-        return service;
-    }
-
-
     /**
      * Returns a map of PIDs and providing bundles of MetaType information. The
      * map is indexed by PID and the value of each entry is the bundle 
providing
@@ -75,8 +61,7 @@ class MetaTypeServiceSupport extends MetaTypeSupport
      * @param locale The name of the locale to get the meta data for.
      * @return see the method description
      */
-    Map<String, ObjectClassDefinition> getPidObjectClasses( final String 
locale )
-    {
+    Map<String, ObjectClassDefinition> getPidObjectClasses( final String 
locale ) {
         return getObjectClassDefinitions( PID_GETTER, locale );
     }
 
@@ -90,8 +75,7 @@ class MetaTypeServiceSupport extends MetaTypeSupport
      * @param locale The name of the locale to get the meta data for.
      * @return see the method description
      */
-    Map<String, ObjectClassDefinition> getFactoryPidObjectClasses( final 
String locale )
-    {
+    Map<String, ObjectClassDefinition> getFactoryPidObjectClasses( final 
String locale ) {
         return getObjectClassDefinitions( FACTORY_PID_GETTER, locale );
     }
 
@@ -109,39 +93,26 @@ class MetaTypeServiceSupport extends MetaTypeSupport
      * @return Map of <code>ObjectClassDefinition</code> objects indexed by the
      *      PID (or factory PID) to which they pertain
      */
-    private Map<String, ObjectClassDefinition> getObjectClassDefinitions( 
final IdGetter idGetter, final String locale )
-    {
+    private Map<String, ObjectClassDefinition> getObjectClassDefinitions( 
final IdGetter idGetter, final String locale ) {
         final Map<String, ObjectClassDefinition> objectClassesDefinitions = 
new HashMap<>();
-        final MetaTypeService mts = this.getMetaTypeService();
-        if ( mts != null )
-        {
-            final Bundle[] bundles = this.getBundleContext().getBundles();
-            for ( int i = 0; i < bundles.length; i++ )
-            {
-                final MetaTypeInformation mti = mts.getMetaTypeInformation( 
bundles[i] );
-                if ( mti != null )
-                {
-                    final String[] idList = idGetter.getIds( mti );
-                    for ( int j = 0; idList != null && j < idList.length; j++ )
-                    {
-                        // After getting the list of PIDs, a configuration  
might be
-                        // removed. So the getObjectClassDefinition will throw
-                        // an exception, and this will prevent ALL 
configuration from
-                        // being displayed. By catching it, the configurations 
will be
-                        // visible
-                        ObjectClassDefinition ocd = null;
-                        try
-                        {
-                            ocd = mti.getObjectClassDefinition( idList[j], 
locale );
-                        }
-                        catch ( IllegalArgumentException ignore )
-                        {
-                            // ignore - just don't show this configuration
-                        }
-                        if ( ocd != null )
-                        {
-                            objectClassesDefinitions.put( idList[j], ocd );
-                        }
+        for ( final Bundle bundle : this.bundleContext.getBundles() ) {
+            final MetaTypeInformation mti = 
this.service.getMetaTypeInformation( bundle );
+            if ( mti != null ) {
+                final String[] idList = idGetter.getIds( mti );
+                for ( int j = 0; idList != null && j < idList.length; j++ ) {
+                    // After getting the list of PIDs, a configuration  might 
be
+                    // removed. So the getObjectClassDefinition will throw
+                    // an exception, and this will prevent ALL configuration 
from
+                    // being displayed. By catching it, the configurations 
will be
+                    // visible
+                    ObjectClassDefinition ocd = null;
+                    try {
+                        ocd = mti.getObjectClassDefinition( idList[j], locale 
);
+                    } catch ( IllegalArgumentException ignore ) {
+                        // ignore - just don't show this configuration
+                    }
+                    if ( ocd != null ) {
+                        objectClassesDefinitions.put( idList[j], ocd );
                     }
                 }
             }
@@ -149,18 +120,14 @@ class MetaTypeServiceSupport extends MetaTypeSupport
         return objectClassesDefinitions;
     }
 
-    ObjectClassDefinition getObjectClassDefinition( Configuration config, 
String locale )
-    {
+    ObjectClassDefinition getObjectClassDefinition( Configuration config, 
String locale ) {
         // if the configuration is bound, try to get the object class
         // definition from the bundle installed from the given location
-        if ( config.getBundleLocation() != null )
-        {
-            Bundle bundle = getBundle( this.getBundleContext(), 
config.getBundleLocation() );
-            if ( bundle != null )
-            {
+        if ( config.getBundleLocation() != null ) {
+            Bundle bundle = getBundle( this.bundleContext, 
config.getBundleLocation() );
+            if ( bundle != null ) {
                 String id = config.getFactoryPid();
-                if ( null == id )
-                {
+                if ( null == id ) {
                     id = config.getPid();
                 }
                 return getObjectClassDefinition( bundle, id, locale );
@@ -171,8 +138,7 @@ class MetaTypeServiceSupport extends MetaTypeSupport
         // bundle with the bound location is installed. We search
         // all bundles for a matching [factory] PID
         // if the configuration is a factory one, use the factory PID
-        if ( config.getFactoryPid() != null )
-        {
+        if ( config.getFactoryPid() != null ) {
             return this.getObjectClassDefinition( config.getFactoryPid(), 
locale );
         }
 
@@ -181,29 +147,19 @@ class MetaTypeServiceSupport extends MetaTypeSupport
     }
 
 
-    ObjectClassDefinition getObjectClassDefinition( Bundle bundle, String pid, 
String locale )
-    {
-        if ( bundle != null )
-        {
-            MetaTypeService mts = this.getMetaTypeService();
-            if ( mts != null )
-            {
-                MetaTypeInformation mti = mts.getMetaTypeInformation( bundle );
-                if ( mti != null )
-                {
-                    // see #getObjectClasses( final IdGetter idGetter, final 
String locale )
-                    try
-                    {
-                        return mti.getObjectClassDefinition( pid, locale );
-                    }
-                    catch ( IllegalArgumentException e )
-                    {
-                        // MetaTypeProvider.getObjectClassDefinition might 
throw illegal
-                        // argument exception. So we must catch it here, 
otherwise the
-                        // other configurations will not be shown
-                        // See https://issues.apache.org/jira/browse/FELIX-2390
-                        // https://issues.apache.org/jira/browse/FELIX-3694
-                    }
+    ObjectClassDefinition getObjectClassDefinition( Bundle bundle, String pid, 
String locale ) {
+        if ( bundle != null ) {
+            final MetaTypeInformation mti = 
this.service.getMetaTypeInformation( bundle );
+            if ( mti != null ) {
+                // see #getObjectClasses( final IdGetter idGetter, final 
String locale )
+                try {
+                    return mti.getObjectClassDefinition( pid, locale );
+                } catch ( IllegalArgumentException e ) {
+                    // MetaTypeProvider.getObjectClassDefinition might throw 
illegal
+                    // argument exception. So we must catch it here, otherwise 
the
+                    // other configurations will not be shown
+                    // See https://issues.apache.org/jira/browse/FELIX-2390
+                    // https://issues.apache.org/jira/browse/FELIX-3694
                 }
             }
         }
@@ -213,39 +169,25 @@ class MetaTypeServiceSupport extends MetaTypeSupport
     }
 
 
-    ObjectClassDefinition getObjectClassDefinition( String pid, String locale )
-    {
-        Bundle[] bundles = this.getBundleContext().getBundles();
-        for ( int i = 0; i < bundles.length; i++ )
-        {
-            try
-            {
-                ObjectClassDefinition ocd = this.getObjectClassDefinition( 
bundles[i], pid, locale );
-                if ( ocd != null )
-                {
-                    return ocd;
-                }
-            }
-            catch ( IllegalArgumentException iae )
-            {
-                // don't care
+    ObjectClassDefinition getObjectClassDefinition( String pid, String locale 
) {
+        Bundle[] bundles = bundleContext.getBundles();
+        for ( final Bundle bundle : bundles) {
+            final ObjectClassDefinition ocd = this.getObjectClassDefinition( 
bundle, pid, locale );
+            if ( ocd != null ) {
+                return ocd;
             }
         }
         return null;
     }
 
 
-    Map<String, MetatypePropertyDescriptor> getAttributeDefinitionMap( 
Configuration config, String locale )
-    {
+    Map<String, MetatypePropertyDescriptor> getAttributeDefinitionMap( 
Configuration config, String locale ) {
         Map<String, MetatypePropertyDescriptor> adMap = new HashMap<>();
         ObjectClassDefinition ocd = this.getObjectClassDefinition( config, 
locale );
-        if ( ocd != null )
-        {
+        if ( ocd != null ) {
             AttributeDefinition[] ad = ocd.getAttributeDefinitions( 
ObjectClassDefinition.ALL );
-            if ( ad != null )
-            {
-                for ( int i = 0; i < ad.length; i++ )
-                {
+            if ( ad != null ) {
+                for ( int i = 0; i < ad.length; i++ ) {
                     adMap.put( ad[i].getID(), new MetatypePropertyDescriptor( 
ad[i], false ) );
                 }
             }
@@ -261,8 +203,7 @@ class MetaTypeServiceSupport extends MetaTypeSupport
      * @see #PID_GETTER
      * @see #FACTORY_PID_GETTER
      */
-    private static interface IdGetter
-    {
+    private static interface IdGetter {
         String[] getIds( MetaTypeInformation metaTypeInformation );
     }
 
@@ -272,11 +213,9 @@ class MetaTypeServiceSupport extends MetaTypeSupport
      *
      * @see #getPidObjectClasses(String)
      */
-    private static final IdGetter PID_GETTER = new IdGetter()
-    {
+    private static final IdGetter PID_GETTER = new IdGetter() {
         @Override
-        public String[] getIds( MetaTypeInformation metaTypeInformation )
-        {
+        public String[] getIds( MetaTypeInformation metaTypeInformation ) {
             return metaTypeInformation.getPids();
         }
     };
@@ -287,11 +226,9 @@ class MetaTypeServiceSupport extends MetaTypeSupport
      *
      * @see #getFactoryPidObjectClasses(String)
      */
-    private static final IdGetter FACTORY_PID_GETTER = new IdGetter()
-    {
+    private static final IdGetter FACTORY_PID_GETTER = new IdGetter() {
         @Override
-        public String[] getIds( MetaTypeInformation metaTypeInformation )
-        {
+        public String[] getIds( MetaTypeInformation metaTypeInformation ) {
             return metaTypeInformation.getFactoryPids();
         }
     };
diff --git 
a/webconsole/src/main/java/org/apache/felix/webconsole/internal/configuration/MetaTypeSupport.java
 
b/webconsole/src/main/java/org/apache/felix/webconsole/internal/configuration/MetaTypeSupport.java
index 2848dedb50..728ce0a1e4 100644
--- 
a/webconsole/src/main/java/org/apache/felix/webconsole/internal/configuration/MetaTypeSupport.java
+++ 
b/webconsole/src/main/java/org/apache/felix/webconsole/internal/configuration/MetaTypeSupport.java
@@ -35,9 +35,7 @@ import org.osgi.service.metatype.AttributeDefinition;
 
 
 /**
- * The <code>ConfigManagerBase</code> is the base class for the
- * ConfigurationAdmin support in the web console. It provides various helper
- * methods mostly with respect to using the MetaTypeService to access
+ * It provides various helper methods mostly with respect to using the 
MetaTypeService to access
  * configuration descriptions.
  */
 class MetaTypeSupport
@@ -48,19 +46,14 @@ class MetaTypeSupport
      */
     static final String PASSWORD_PLACEHOLDER_VALUE = "unmodified"; 
//$NON-NLS-1$
 
-    static Bundle getBundle( final BundleContext bundleContext, final String 
bundleLocation )
-    {
-        if ( bundleLocation == null )
-        {
+    static Bundle getBundle( final BundleContext bundleContext, final String 
bundleLocation ) {
+        if ( bundleLocation == null ) {
             return null;
         }
 
-        Bundle[] bundles = bundleContext.getBundles();
-        for ( int i = 0; i < bundles.length; i++ )
-        {
-            if ( bundleLocation.equals( bundles[i].getLocation() ) )
-            {
-                return bundles[i];
+        for ( final Bundle bundle : bundleContext.getBundles() ) {
+            if ( bundleLocation.equals( bundle.getLocation() ) ) {
+                return bundle;
             }
         }
 

Reply via email to