Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issues/sling 8483 Add a Priority to the Extension Handler to sort them #13

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@
<excludes>
<exclude>readme.md</exclude>
<exclude>src/main/resources/META-INF/services/**</exclude>
<exclude>src/test/resources/META-INF/services/**</exclude>
<exclude>**/*.properties</exclude>
<exclude>launcher/**</exclude>
</excludes>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,14 @@
import java.io.Reader;
import java.net.URL;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.ServiceLoader;
import java.util.Spliterator;
import java.util.Spliterators;
import java.util.stream.Collectors;
import java.util.stream.StreamSupport;

import org.apache.sling.feature.Artifact;
Expand Down Expand Up @@ -163,7 +166,14 @@ public static void prepareLauncher(final LauncherPrepareContext ctx, final Launc
}

extensions: for(final Extension ext : app.getExtensions()) {
for (ExtensionHandler handler : ServiceLoader.load(ExtensionHandler.class, FeatureProcessor.class.getClassLoader()))
Iterator<ExtensionHandler> i = ServiceLoader.load(ExtensionHandler.class, FeatureProcessor.class.getClassLoader()).iterator();
// Stream the iterator, sort them based on Priority in reversed order and then collection into a list
List<ExtensionHandler> prioritizedExtensionHandlerList =
StreamSupport
.stream(Spliterators.spliteratorUnknownSize(i, Spliterator.ORDERED), false)
.sorted(Comparator.comparingInt(ExtensionHandler::getPriority).reversed())
.collect(Collectors.toList());
for (ExtensionHandler handler : prioritizedExtensionHandlerList)
{
if (handler.handle(new ExtensionContextImpl(ctx, config.getInstallation(), loadedFeatures), ext)) {
continue extensions;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@

public class ContentPackageHandler implements ExtensionHandler
{
@Override
public int getPriority() {
return FALLBACK_PRIORITY;
}

@Override
public boolean handle(ExtensionContext context, Extension extension) throws IOException
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ public class RepoInitHandler implements ExtensionHandler
{
private static final AtomicInteger index = new AtomicInteger(1);

@Override
public int getPriority() {
return FALLBACK_PRIORITY;
}

@Override
public boolean handle(ExtensionContext context, Extension extension) throws Exception
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,11 @@

public interface ExtensionHandler
{
/** Priority for Extension Handlers that are just a fallback instance **/
int FALLBACK_PRIORITY = -1;

/** @return The priority of the Extension Handler to select the most appropriate one. The one with the highest priority is selected **/
int getPriority();

public boolean handle(ExtensionContext context, Extension extension) throws Exception;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
* 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.feature.launcher.handler;

public class ExtensionHandler100
extends ExtensionHandlerBase
{
@Override
public int getPriority() {
return 100;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
* 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.feature.launcher.handler;

public class ExtensionHandler200
extends ExtensionHandlerBase
{
@Override
public int getPriority() {
return 200;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* 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.feature.launcher.handler;

import org.apache.sling.feature.Extension;
import org.apache.sling.feature.launcher.spi.extensions.ExtensionContext;
import org.apache.sling.feature.launcher.spi.extensions.ExtensionHandler;

public abstract class ExtensionHandlerBase
implements ExtensionHandler
{
private static int lastPriorityUsed = -100;

public static int getLastPriorityUsed() {
return lastPriorityUsed;
}

@Override
public boolean handle(ExtensionContext context, Extension extension) throws Exception {
boolean answer = lastPriorityUsed == -100;
lastPriorityUsed = getPriority();
return answer;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
* 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.feature.launcher.handler;

public class ExtensionHandlerDefault
extends ExtensionHandlerBase
{
@Override
public int getPriority() {
return FALLBACK_PRIORITY;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/*
* 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.feature.launcher.impl;

import org.apache.sling.feature.ArtifactId;
import org.apache.sling.feature.Extension;
import org.apache.sling.feature.ExtensionType;
import org.apache.sling.feature.Extensions;
import org.apache.sling.feature.Feature;
import org.apache.sling.feature.launcher.handler.ExtensionHandlerBase;
import org.apache.sling.feature.launcher.spi.LauncherPrepareContext;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.HashMap;
import java.util.Map;

import static org.junit.Assert.assertEquals;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.when;

public class FeatureProcessorTest {

private Logger logger = LoggerFactory.getLogger(getClass());

@Test
public void testExtensionHandlerSorting() throws Exception {
LauncherPrepareContext mockContext = mock(LauncherPrepareContext.class);
LauncherConfig mockConfig = mock(LauncherConfig.class);
Feature feature = spy(new Feature(new ArtifactId("g", "a","1.0.0", "test", "jar" )));
Map<ArtifactId, Feature> loadedFeatures = new HashMap<>();
Extension extension = new Extension(
ExtensionType.TEXT,
"ContentHandler",
true
);
Extensions extensions = new Extensions();
extensions.add(extension);
when(feature.getExtensions()).thenReturn(extensions);

FeatureProcessor.prepareLauncher(
mockContext, mockConfig, feature, loadedFeatures
);
logger.info("Last Priority Used: '{}'", ExtensionHandlerBase.getLastPriorityUsed());
assertEquals("Wrong Priority was used", 200, ExtensionHandlerBase.getLastPriorityUsed());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
org.apache.sling.feature.launcher.impl.launchers.FrameworkLauncher
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
org.apache.sling.feature.launcher.handler.ExtensionHandlerDefault
org.apache.sling.feature.launcher.handler.ExtensionHandler200
org.apache.sling.feature.launcher.handler.ExtensionHandler100