Skip to content

Instantly share code, notes, and snippets.

@davidtruchet
Created April 21, 2017 15:51
Show Gist options
  • Save davidtruchet/c923795f61e7e51dcbb1a3cb05d5c8c5 to your computer and use it in GitHub Desktop.
Save davidtruchet/c923795f61e7e51dcbb1a3cb05d5c8c5 to your computer and use it in GitHub Desktop.
<jcr:root xmlns:sling="http://sling.apache.org/jcr/sling/1.0" xmlns:jcr="http://www.jcp.org/jcr/1.0"
jcr:primaryType="sling:OsgiConfig"
youtube.apikey="AIzaSyCgsLOWhd7dJAesI0-WfLiSnjPH0UlUastgghks"
google.head.tag="q8PyBqdxxaNh8V67pUQV1oLCytHfy_hqlUbEJrssa"
aem.design.path="/etc/designs/site"
aem.path.regex="/content/site/en_ca"
aem.default.site.config="{Boolean}false"
google.gtm.id="GTM-UJSF"
/>
<jcr:root xmlns:sling="http://sling.apache.org/jcr/sling/1.0" xmlns:jcr="http://www.jcp.org/jcr/1.0"
jcr:primaryType="sling:OsgiConfig"
youtube.apikey="AIzaSyCgsLOWhd7dJAesI0-WfLiSnjPH0O6FUlU"
google.head.tag="q8PyBqdxxaNh8V67pUQV1oLCytHfy_hqlUbEJrycIg4"
aem.design.path="/etc/designs/site"
aem.path.regex="/content/site"
aem.default.site.config="{Boolean}true"
google.gtm.id="GTM-BKSM"
/>
<jcr:root xmlns:sling="http://sling.apache.org/jcr/sling/1.0" xmlns:jcr="http://www.jcp.org/jcr/1.0"
jcr:primaryType="sling:OsgiConfig"
youtube.apikey="AIzaSyCgsLOWhd7dJAesI0-WfLiSnjPH0O6FUlUasssas"
google.head.tag="q8PyBqdxxaNh8V67pUQV1oLCytHfy_hqlUbEJrycIg4assa"
aem.design.path="/etc/designs/site"
aem.path.regex="/content/site/en_us"
aem.default.site.config="{Boolean}false"
google.gtm.id="GTM-BKAA"
/>
package com.site.dotcom.service.services.impl;
import java.util.ArrayList;
import java.util.List;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Reference;
import org.apache.felix.scr.annotations.ReferenceCardinality;
import org.apache.felix.scr.annotations.ReferencePolicy;
import org.apache.felix.scr.annotations.Service;
import com.site.dotcom.service.services.GlobalConfigConsumerService;
import com.site.dotcom.service.services.GlobalConfigService;
@Component(label = "Global Configuration Factory Consumer", description = "AEM Global Configuration Factory Consumer", immediate = true, metatype = true, enabled = true)
@Service(GlobalConfigConsumerService.class)
public class GlobalConfigConsumerServiceImpl implements GlobalConfigConsumerService {
@Reference(referenceInterface = GlobalConfigService.class, cardinality = ReferenceCardinality.OPTIONAL_MULTIPLE, policy = ReferencePolicy.DYNAMIC)
private List<GlobalConfigService> configs;
@Override
public GlobalConfigService getGlobalConfigService(String path) {
GlobalConfigService defaultService = null;
for (GlobalConfigService globalConfigService : configs) {
if (path.startsWith(globalConfigService.getPath()) && !globalConfigService.getDefaultConfig()) {
return globalConfigService;
} else if (globalConfigService.getDefaultConfig()) {
defaultService = globalConfigService;
}
}
return defaultService;
}
protected synchronized void bindGlobalConfigService(final GlobalConfigService config) {
if (configs == null) {
configs = new ArrayList<GlobalConfigService>();
}
configs.add(config);
}
protected synchronized void unbindGlobalConfigService(final GlobalConfigService config) {
configs.remove(config);
}
}
package com.site.dotcom.service.services.impl;
import java.util.Map;
import org.apache.felix.scr.annotations.Activate;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.ConfigurationPolicy;
import org.apache.felix.scr.annotations.Properties;
import org.apache.felix.scr.annotations.Property;
import org.apache.felix.scr.annotations.Service;
import org.apache.sling.commons.osgi.PropertiesUtil;
import org.osgi.framework.Constants;
import com.site.dotcom.service.services.GlobalConfigService;
/**
*
* @author 3|Share - David Truchet
*
*/
@Component(immediate = true, metatype = true, label = "Site Global Config Service",
enabled = true, policy = ConfigurationPolicy.REQUIRE, configurationFactory = true)
@Service(value = GlobalConfigService.class)
@Properties({ @Property(name = Constants.SERVICE_VENDOR, value = "3|Share"),
@Property(name = Constants.SERVICE_DESCRIPTION, value = "Provides a service with Site dotcom global configurations") })
public class GlobalFactoryConfigImpl implements GlobalConfigService {
private String youTubeAPIKey;
@Property(label = "YouTube API Key", description = "YouTube V3 API Key")
private static final String YOUTUBE_API_KEY = "youtube.apikey";
private String googleHeadTag;
@Property(label = "Google Head Tag", description = "Google head tag key")
private static final String GOOGLE_HEAD_TAG = "google.head.tag";
private String designPath;
@Property(label = "AEM Design Path", description = "AEM page design path")
private static final String AEM_DESIGN_PATH = "aem.design.path";
private String path;
@Property(label = "AEM Path Regex", description = "AEM page path regex")
private static final String AEM_PATH = "aem.path.regex";
private Boolean defaultConfig;
@Property(label = "Default site config", description = "Default site config")
private static final String DEFAULT_CONFIG = "aem.default.site.config";
private String gtmId;
@Property(label = "Google GTM Id", description = "Google GTM Id")
private static final String GOOGLE_GTM_ID = "google.gtm.id";
@Activate
protected void update(final Map<String, Object> properties) {
this.youTubeAPIKey = PropertiesUtil.toString(properties.get(YOUTUBE_API_KEY), "nokey");
this.path = PropertiesUtil.toString(properties.get(AEM_PATH), "nopath");
this.googleHeadTag = PropertiesUtil.toString(properties.get(GOOGLE_HEAD_TAG), "notag");
this.designPath = PropertiesUtil.toString(properties.get(AEM_DESIGN_PATH), "nodesignpath");
this.defaultConfig = PropertiesUtil.toBoolean(properties.get(DEFAULT_CONFIG), false);
this.gtmId = PropertiesUtil.toString(properties.get(GOOGLE_GTM_ID), "nogtmid");
}
@Override
public String getYouTubeAPIKey() {
return this.youTubeAPIKey;
}
@Override
public String getGoogleHeadTag() {
return this.googleHeadTag;
}
@Override
public String getPath() {
return this.path;
}
@Override
public String getDesignPath() {
return this.designPath;
}
@Override
public Boolean getDefaultConfig() {
return defaultConfig;
}
@Override
public String getGtmId() {
return this.gtmId;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment