-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
376 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
60 changes: 60 additions & 0 deletions
60
src/main/java/io/wcm/wcm/commons/instancetype/InstanceTypeService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
/* | ||
* #%L | ||
* wcm.io | ||
* %% | ||
* Copyright (C) 2021 wcm.io | ||
* %% | ||
* Licensed 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. | ||
* #L% | ||
*/ | ||
package io.wcm.wcm.commons.instancetype; | ||
|
||
import java.util.Set; | ||
|
||
import org.jetbrains.annotations.NotNull; | ||
import org.osgi.annotation.versioning.ProviderType; | ||
|
||
/** | ||
* Allows to detect if the current AEM instance is an author or publish instance. | ||
* <p> | ||
* This service does not rely in <code>SlingSettingServices</code> which is deprecated and subject to removal in latest | ||
* AEM versions. | ||
* Instead, it is based on a OSGi configuration which should be configured properly for author and publish instances. | ||
* If not configured, it "guesses" the instance type from other OSGi configs and writes a warning in the logs. | ||
* </p> | ||
*/ | ||
@ProviderType | ||
public interface InstanceTypeService { | ||
|
||
/** | ||
* Returns true if code is running on AEM author instance. | ||
* @return true if AEM author instance. | ||
*/ | ||
boolean isAuthor(); | ||
|
||
/** | ||
* Returns true if code is running on AEM publish instance. | ||
* @return true if AEM publish instance. | ||
*/ | ||
boolean isPublish(); | ||
|
||
/** | ||
* Returns a set with a single "author" or "publish" run mode string. | ||
* This method is provided for for compatibility with code relying on sets of run modes formerly provided by | ||
* <code>SlingSettingsService</code>. | ||
* @return Set with a single element: Either "author" or "publish". | ||
*/ | ||
@NotNull | ||
Set<String> getRunModes(); | ||
|
||
} |
142 changes: 142 additions & 0 deletions
142
src/main/java/io/wcm/wcm/commons/instancetype/impl/InstanceTypeServiceImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,142 @@ | ||
/* | ||
* #%L | ||
* wcm.io | ||
* %% | ||
* Copyright (C) 2021 wcm.io | ||
* %% | ||
* Licensed 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. | ||
* #L% | ||
*/ | ||
package io.wcm.wcm.commons.instancetype.impl; | ||
|
||
import static org.osgi.framework.Constants.SERVICE_PID; | ||
|
||
import java.io.IOException; | ||
import java.util.Collections; | ||
import java.util.Set; | ||
|
||
import org.apache.commons.lang3.StringUtils; | ||
import org.jetbrains.annotations.NotNull; | ||
import org.osgi.framework.InvalidSyntaxException; | ||
import org.osgi.service.cm.Configuration; | ||
import org.osgi.service.cm.ConfigurationAdmin; | ||
import org.osgi.service.component.annotations.Activate; | ||
import org.osgi.service.component.annotations.Component; | ||
import org.osgi.service.component.annotations.Reference; | ||
import org.osgi.service.metatype.annotations.AttributeDefinition; | ||
import org.osgi.service.metatype.annotations.Designate; | ||
import org.osgi.service.metatype.annotations.ObjectClassDefinition; | ||
import org.osgi.service.metatype.annotations.Option; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import com.day.cq.wcm.api.WCMMode; | ||
|
||
import io.wcm.wcm.commons.instancetype.InstanceTypeService; | ||
import io.wcm.wcm.commons.util.RunMode; | ||
|
||
/** | ||
* Implements {@link InstanceTypeService}. | ||
*/ | ||
@Component(service = InstanceTypeService.class) | ||
@Designate(ocd = InstanceTypeServiceImpl.Config.class) | ||
public class InstanceTypeServiceImpl implements InstanceTypeService { | ||
|
||
@ObjectClassDefinition(name = "wcm.io Commons AEM Instance Type", | ||
description = "Configures if the current instance is an author or publish instance, and makes this information accessible for other services.") | ||
@interface Config { | ||
|
||
@AttributeDefinition(name = "Instance Type", | ||
description = "Should be explicitely configured to 'author' or 'publish'. If not set, instance type will be guessed by heuristics from other OSGi configurations.", | ||
options = { | ||
@Option(value = RunMode.AUTHOR, label = "Author"), | ||
@Option(value = RunMode.PUBLISH, label = "Publish"), | ||
@Option(value = InstanceTypeServiceImpl.TYPE_AUTO, label = "Detect automatically (not recommended)") | ||
}) | ||
String instance_type() default InstanceTypeServiceImpl.TYPE_AUTO; | ||
|
||
} | ||
|
||
static final String TYPE_AUTO = "auto"; | ||
|
||
static final String WCM_REQUEST_FILTER_PID = "com.day.cq.wcm.core.WCMRequestFilter"; | ||
static final String WCM_MODE_PROPERTY = "wcmfilter.mode"; | ||
|
||
private boolean isAuthor; | ||
private Set<String> runModes; | ||
|
||
@Reference | ||
private ConfigurationAdmin configAdmin; | ||
|
||
private final Logger log = LoggerFactory.getLogger(InstanceTypeServiceImpl.class); | ||
|
||
@Activate | ||
private void activate(Config config) { | ||
// detect instance type | ||
String instanceType = config.instance_type(); | ||
if (StringUtils.equals(instanceType, RunMode.AUTHOR)) { | ||
isAuthor = true; | ||
} | ||
else if (StringUtils.equals(instanceType, RunMode.PUBLISH)) { | ||
isAuthor = false; | ||
} | ||
else { | ||
// not configured or set to "auto" - rely on guessing author mode | ||
isAuthor = detectAutorMode(); | ||
|
||
log.warn("Please provide a 'wcm.io Commons AEM Instance Type' configuration " | ||
+ "- falling back to guessing instance type from other configuration => {}.", | ||
isAuthor ? RunMode.AUTHOR : RunMode.PUBLISH); | ||
} | ||
|
||
// set matching run mode set | ||
if (isAuthor) { | ||
runModes = Collections.singleton(RunMode.AUTHOR); | ||
} | ||
else { | ||
runModes = Collections.singleton(RunMode.PUBLISH); | ||
} | ||
} | ||
|
||
private boolean detectAutorMode() { | ||
try { | ||
Configuration[] configs = configAdmin.listConfigurations("(" + SERVICE_PID + "=" + WCM_REQUEST_FILTER_PID + ")"); | ||
if (configs != null && configs.length > 0) { | ||
Object defaultWcmMode = configs[0].getProperties().get(WCM_MODE_PROPERTY); | ||
if (defaultWcmMode instanceof String) { | ||
return !StringUtils.equalsIgnoreCase(WCMMode.DISABLED.name(), (String)defaultWcmMode); | ||
} | ||
} | ||
} | ||
catch (IOException | InvalidSyntaxException ex) { | ||
log.warn("Unable to read OSGi configuration: {}", WCM_REQUEST_FILTER_PID, ex); | ||
} | ||
return false; | ||
} | ||
|
||
@Override | ||
public boolean isAuthor() { | ||
return isAuthor; | ||
} | ||
|
||
@Override | ||
public boolean isPublish() { | ||
return !isAuthor; | ||
} | ||
|
||
@Override | ||
public @NotNull Set<String> getRunModes() { | ||
return Collections.unmodifiableSet(runModes); | ||
} | ||
|
||
} |
24 changes: 24 additions & 0 deletions
24
src/main/java/io/wcm/wcm/commons/instancetype/package-info.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
/* | ||
* #%L | ||
* wcm.io | ||
* %% | ||
* Copyright (C) 2018 wcm.io | ||
* %% | ||
* Licensed 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. | ||
* #L% | ||
*/ | ||
/** | ||
* Detect instance type (author/publish) of AEM instance. | ||
*/ | ||
@org.osgi.annotation.versioning.Version("1.0") | ||
package io.wcm.wcm.commons.instancetype; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.