You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@sling.apache.org by GitBox <gi...@apache.org> on 2019/08/26 22:23:42 UTC

[GitHub] [sling-org-apache-sling-models-impl] schaefa commented on a change in pull request #14: SLING-8655 - Updated dependency on API, provided Externalized Path In…

schaefa commented on a change in pull request #14: SLING-8655 - Updated dependency on API, provided Externalized Path In…
URL: https://github.com/apache/sling-org-apache-sling-models-impl/pull/14#discussion_r317823448
 
 

 ##########
 File path: src/main/java/org/apache/sling/models/impl/injectors/ExternalizedPathInjector.java
 ##########
 @@ -0,0 +1,113 @@
+/*
+ * 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.models.impl.injectors;
+
+import org.apache.commons.lang3.ObjectUtils;
+import org.apache.sling.api.resource.ResourceResolver;
+import org.apache.sling.api.resource.ValueMap;
+import org.apache.sling.models.annotations.ExternalizePath;
+import org.apache.sling.models.annotations.injectorspecific.ExternalizedPathProvider;
+import org.apache.sling.models.spi.DisposalCallbackRegistry;
+import org.apache.sling.models.spi.Injector;
+import org.jetbrains.annotations.NotNull;
+import org.osgi.framework.Constants;
+import org.osgi.service.component.annotations.Component;
+import org.osgi.service.component.annotations.Reference;
+import org.osgi.service.component.annotations.ReferenceCardinality;
+import org.osgi.service.component.annotations.ReferencePolicy;
+
+import java.lang.reflect.AnnotatedElement;
+import java.lang.reflect.Type;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.Comparator;
+import java.util.List;
+
+@Component(
+    property=Constants.SERVICE_RANKING+":Integer=1000",
+    service={
+        Injector.class
+    }
+)
+public class ExternalizedPathInjector
+    extends AbstractInjector
+    implements Injector
+{
+    List<ExternalizedPathProvider> providerList = new ArrayList<>();
+
+    @Reference(policy = ReferencePolicy.DYNAMIC, cardinality = ReferenceCardinality.OPTIONAL)
+    void bindExternalizedPathProvider(ExternalizedPathProvider provider) {
+        providerList.add(provider);
+        // The providers are sorted so that the one with the highest priority is the first entry
+        Collections.sort(
+            providerList,
+            Comparator.comparingInt(ExternalizedPathProvider::getPriority).reversed()
+        );
+    }
+
+    void unbindExternalizedPathProvider(ExternalizedPathProvider provider) {
+        providerList.remove(provider);
+    }
+
+    public ExternalizedPathInjector() {
+        bindExternalizedPathProvider(new DefaultExternalizedPathProvider());
+    }
+
+    @Override
+    public @NotNull String getName() {
+        return "externalize-path";
+    }
+
+    @Override
+    public Object getValue(@NotNull Object adaptable, String name, @NotNull Type type, @NotNull AnnotatedElement element,
+            @NotNull DisposalCallbackRegistry callbackRegistry) {
+        if (adaptable == ObjectUtils.NULL) {
+            return null;
+        }
+        if (element.isAnnotationPresent(ExternalizePath.class)) {
+            ValueMap properties = getValueMap(adaptable);
+            if(properties != ObjectUtils.NULL) {
+                String imagePath = properties.get(name, String.class);
+                if(imagePath != null) {
+                    ExternalizedPathProvider provider = providerList.get(0);
+                    return provider.externalize(adaptable, imagePath);
+                }
+            }
+        }
+        return null;
+    }
+
+    /** Fallback Implementation of the Externalized Path Provider that uses the Resource Resolver's map function **/
+    private class DefaultExternalizedPathProvider
 
 Review comment:
   Moved this to be an regular OSGi Component

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services