Refactor FeatureFinder

Project: http://git-wip-us.apache.org/repos/asf/karaf/repo
Commit: http://git-wip-us.apache.org/repos/asf/karaf/commit/39440e82
Tree: http://git-wip-us.apache.org/repos/asf/karaf/tree/39440e82
Diff: http://git-wip-us.apache.org/repos/asf/karaf/diff/39440e82

Branch: refs/heads/master
Commit: 39440e82e31cab3a6a2986b2bfdbe09503be2563
Parents: a82539b
Author: Christian Schneider <[email protected]>
Authored: Wed May 17 10:18:23 2017 +0200
Committer: Christian Schneider <[email protected]>
Committed: Thu May 18 11:37:09 2017 +0200

----------------------------------------------------------------------
 .../karaf/features/internal/osgi/Activator.java |  9 +-
 .../internal/service/FeatureFinder.java         | 87 -----------------
 .../internal/service/FeatureRepoFinder.java     | 98 ++++++++++++++++++++
 .../internal/service/FeaturesServiceImpl.java   |  4 +-
 4 files changed, 103 insertions(+), 95 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/karaf/blob/39440e82/features/core/src/main/java/org/apache/karaf/features/internal/osgi/Activator.java
----------------------------------------------------------------------
diff --git 
a/features/core/src/main/java/org/apache/karaf/features/internal/osgi/Activator.java
 
b/features/core/src/main/java/org/apache/karaf/features/internal/osgi/Activator.java
index bc59af7..8abd82b 100644
--- 
a/features/core/src/main/java/org/apache/karaf/features/internal/osgi/Activator.java
+++ 
b/features/core/src/main/java/org/apache/karaf/features/internal/osgi/Activator.java
@@ -43,7 +43,7 @@ import 
org.apache.karaf.features.internal.resolver.Slf4jResolverLog;
 import org.apache.karaf.features.internal.service.BootFeaturesInstaller;
 import org.apache.karaf.features.internal.service.EventAdminListener;
 import org.apache.karaf.features.internal.service.FeatureConfigInstaller;
-import org.apache.karaf.features.internal.service.FeatureFinder;
+import org.apache.karaf.features.internal.service.FeatureRepoFinder;
 import org.apache.karaf.features.internal.service.FeaturesServiceConfig;
 import org.apache.karaf.features.internal.service.FeaturesServiceImpl;
 import org.apache.karaf.features.internal.service.BundleInstallSupport;
@@ -84,7 +84,6 @@ import org.slf4j.LoggerFactory;
 )
 public class Activator extends BaseActivator {
 
-    public static final String FEATURES_REPOS_PID = 
"org.apache.karaf.features.repos";
     public static final String FEATURES_SERVICE_CONFIG_FILE = 
"org.apache.karaf.features.cfg";
 
     private static final String STATE_FILE = "state.json";
@@ -144,10 +143,8 @@ public class Activator extends BaseActivator {
             dgmb.registerMBean();
         }
 
-        FeatureFinder featureFinder = new FeatureFinder();
-        Hashtable<String, Object> props = new Hashtable<>();
-        props.put(Constants.SERVICE_PID, FEATURES_REPOS_PID);
-        register(ManagedService.class, featureFinder, props);
+        FeatureRepoFinder featureFinder = new FeatureRepoFinder();
+        register(ManagedService.class, featureFinder, 
FeatureRepoFinder.getServiceProperties());
 
         List<Repository> repositories = new ArrayList<>();
         String[] resourceRepositories = getStringArray("resourceRepositories", 
"");

http://git-wip-us.apache.org/repos/asf/karaf/blob/39440e82/features/core/src/main/java/org/apache/karaf/features/internal/service/FeatureFinder.java
----------------------------------------------------------------------
diff --git 
a/features/core/src/main/java/org/apache/karaf/features/internal/service/FeatureFinder.java
 
b/features/core/src/main/java/org/apache/karaf/features/internal/service/FeatureFinder.java
deleted file mode 100644
index 2ee08e7..0000000
--- 
a/features/core/src/main/java/org/apache/karaf/features/internal/service/FeatureFinder.java
+++ /dev/null
@@ -1,87 +0,0 @@
-/*
- * 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.karaf.features.internal.service;
-
-import java.net.URI;
-import java.util.Dictionary;
-import java.util.Enumeration;
-import java.util.HashMap;
-import java.util.Map;
-import java.util.Set;
-
-import org.apache.felix.utils.properties.InterpolationHelper;
-import org.osgi.service.cm.ConfigurationException;
-import org.osgi.service.cm.ManagedService;
-
-public class FeatureFinder implements ManagedService {
-
-    final Map<String, String> nameToArtifactMap = new HashMap<>();
-
-    public String[] getNames() {
-        synchronized (nameToArtifactMap) {
-            Set<String> strings = nameToArtifactMap.keySet();
-            return strings.toArray(new String[strings.size()]);
-        }
-    }
-
-    public URI getUriFor(String name, String version) {
-        String url;
-        synchronized (nameToArtifactMap) {
-            url = nameToArtifactMap.get(name);
-        }
-        if (url == null) {
-            return null;
-        }
-        if (version != null) {
-            // replace the version in the URL with the provided one
-            url = FeatureFinder.replaceVersion(url, version);
-        }
-        return URI.create(url);
-    }
-
-    @SuppressWarnings("rawtypes")
-    public void updated(Dictionary properties) throws ConfigurationException {
-        synchronized (nameToArtifactMap) {
-            if (properties != null) {
-                nameToArtifactMap.clear();
-                Enumeration keys = properties.keys();
-                while (keys.hasMoreElements()) {
-                    String key = (String) keys.nextElement();
-                    if (!"felix.fileinstall.filename".equals(key) && 
!"service.pid".equals(key)) {
-                        nameToArtifactMap.put(key, (String) 
properties.get(key));
-                    }
-                }
-            }
-        }
-    }
-
-    private static String replaceVersion(String url, String version) {
-        if (url.startsWith("mvn:")) {
-            // mvn:groupId/artifactId/version...
-            int index = url.indexOf('/');
-            index = url.indexOf('/', index + 1);
-
-            String first = url.substring(0, index);
-            index = url.indexOf('/', index + 1);
-            String second = url.substring(index + 1);
-
-            return first + "/" + version + "/" + second;
-        }
-        return url;
-    }
-
-}

http://git-wip-us.apache.org/repos/asf/karaf/blob/39440e82/features/core/src/main/java/org/apache/karaf/features/internal/service/FeatureRepoFinder.java
----------------------------------------------------------------------
diff --git 
a/features/core/src/main/java/org/apache/karaf/features/internal/service/FeatureRepoFinder.java
 
b/features/core/src/main/java/org/apache/karaf/features/internal/service/FeatureRepoFinder.java
new file mode 100644
index 0000000..4b1c655
--- /dev/null
+++ 
b/features/core/src/main/java/org/apache/karaf/features/internal/service/FeatureRepoFinder.java
@@ -0,0 +1,98 @@
+/*
+ * 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.karaf.features.internal.service;
+
+import java.net.URI;
+import java.util.Dictionary;
+import java.util.Enumeration;
+import java.util.HashMap;
+import java.util.Hashtable;
+import java.util.Map;
+import java.util.Set;
+
+import org.osgi.framework.Constants;
+import org.osgi.service.cm.ConfigurationException;
+import org.osgi.service.cm.ManagedService;
+
+public class FeatureRepoFinder implements ManagedService {
+    public static final String FEATURES_REPOS_PID = 
"org.apache.karaf.features.repos";
+
+    final Map<String, String> nameToArtifactMap = new HashMap<>();
+
+    public static Dictionary<String, Object> getServiceProperties() {
+        Dictionary<String, Object> props = new Hashtable<>();
+        props.put(Constants.SERVICE_PID, FeatureRepoFinder.FEATURES_REPOS_PID);
+        return props;
+    }
+
+    public synchronized String[] getNames() {
+        Set<String> strings = nameToArtifactMap.keySet();
+        return strings.toArray(new String[strings.size()]);
+    }
+
+    public synchronized URI getUriFor(String name, String version) {
+        String artifactUri = nameToArtifactMap.get(name);
+        if (artifactUri == null) {
+            return null;
+        }
+        if (version != null) {
+            artifactUri = FeatureRepoFinder.replaceVersion(artifactUri, 
version);
+        }
+        return URI.create(artifactUri);
+    }
+
+    @SuppressWarnings("rawtypes")
+    public synchronized void updated(Dictionary properties) throws 
ConfigurationException {
+        if (properties != null) {
+            nameToArtifactMap.clear();
+            Enumeration keys = properties.keys();
+            while (keys.hasMoreElements()) {
+                String key = (String)keys.nextElement();
+                if (!isSystemKey(key)) {
+                    nameToArtifactMap.put(key, (String)properties.get(key));
+                }
+            }
+        }
+    }
+
+    private boolean isSystemKey(String key) {
+        return "felix.fileinstall.filename".equals(key) || 
"service.pid".equals(key);
+    }
+
+    /**
+     * Replace the version in the URL with the provided one. 
+     * Only processes mvn urls like mvn:groupId/artifactId/version... 
+     * 
+     * @param url
+     * @param version
+     * @return
+     */
+    private static String replaceVersion(String url, String version) {
+        if (url.startsWith("mvn:")) {
+            int firstSlash = url.indexOf('/');
+            int secondSlash = url.indexOf('/', firstSlash + 1);
+
+            String before = url.substring(0, secondSlash);
+            int thirdSlash = url.indexOf('/', secondSlash + 1);
+            String after = url.substring(thirdSlash + 1);
+
+            return before + "/" + version + "/" + after;
+        }
+        return url;
+    }
+
+}

http://git-wip-us.apache.org/repos/asf/karaf/blob/39440e82/features/core/src/main/java/org/apache/karaf/features/internal/service/FeaturesServiceImpl.java
----------------------------------------------------------------------
diff --git 
a/features/core/src/main/java/org/apache/karaf/features/internal/service/FeaturesServiceImpl.java
 
b/features/core/src/main/java/org/apache/karaf/features/internal/service/FeaturesServiceImpl.java
index a14667b..32e0bff 100644
--- 
a/features/core/src/main/java/org/apache/karaf/features/internal/service/FeaturesServiceImpl.java
+++ 
b/features/core/src/main/java/org/apache/karaf/features/internal/service/FeaturesServiceImpl.java
@@ -96,7 +96,7 @@ public class FeaturesServiceImpl implements FeaturesService, 
Deployer.DeployCall
      * Used to load and save the {@link State} of this service.
      */
     private final StateStorage storage;
-    private final FeatureFinder featureFinder;
+    private final FeatureRepoFinder featureFinder;
     private final ConfigurationAdmin configurationAdmin;
     private final Resolver resolver;
     private final BundleInstallSupport installSupport;
@@ -121,7 +121,7 @@ public class FeaturesServiceImpl implements 
FeaturesService, Deployer.DeployCall
     private Map<String, Map<String, Feature>> featureCache;
 
     public FeaturesServiceImpl(StateStorage storage,
-                               FeatureFinder featureFinder,
+                               FeatureRepoFinder featureFinder,
                                ConfigurationAdmin configurationAdmin,
                                Resolver resolver,
                                BundleInstallSupport installSupport,

Reply via email to