Skip to content

Instantly share code, notes, and snippets.

@RF0
Created September 14, 2016 10:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save RF0/cd79e76cc161811c711ed15eec375917 to your computer and use it in GitHub Desktop.
Save RF0/cd79e76cc161811c711ed15eec375917 to your computer and use it in GitHub Desktop.
MyAppConfig.java - example for Enonic XP
common_configPath=${xp.home}/config/
services_prefix = /_/service/com.enonic.myapp
#LOCAL (default)
common_portalUrl = http://localhost:8080/portal/master
#DEV
#common_portalUrl = https://dev.enonic.com
#ATEST
#common_portalUrl = https://atest.enonic.com
#PROD
#common_portalUrl = https://enonic.com
const myAppConfig = __.newBean('com.enonic.myapp.MyAppConfig');
exports.get = function (req) {
const servicePrefix = `${myAppConfig.getProperty('services_prefix')}`;
const portalUrl = myAppConfig.getProperty('common_portalUrl');
package com.enonic.myapp;
import com.enonic.xp.home.HomeDir;
import org.osgi.service.component.annotations.Activate;
import org.osgi.service.component.annotations.Component;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
import java.util.Map;
@Component(immediate = true, configurationPid = "com.enonic.myapp")
public class MyAppConfig {
private static Map<String, String> properties;
public MyAppConfig() {
}
Logger LOG = LoggerFactory.getLogger(this.getClass());
@Activate
public void activate(final Map<String, String> map) {
createDefaultConigFileIfNotPresent();
this.properties = map;
}
private void createDefaultConigFileIfNotPresent() {
try {
java.nio.file.Path configPath = Paths.get(HomeDir.get().toString() + "/config/com.enonic.myapp.cfg");
if (Files.exists(configPath)) {
return;
}
InputStream defaultIndexConfigIs = this.getClass().getResourceAsStream("com.enonic.myapp.cfg");
Files.copy(defaultIndexConfigIs, configPath, StandardCopyOption.REPLACE_EXISTING);
} catch (IOException e) {
}
}
public String getProperty(String propertyName){
if (propertyName == null || "".equals(propertyName)) {
LOG.warn("propertyName null or empty in MyApp.getProperty");
return propertyName;
}
if (properties == null || properties.isEmpty()){
LOG.warn("properties null or empty in MyApp.getProperty({}). Returning propertyName",propertyName);
return propertyName;
}
String property = properties.get(propertyName);
if (property == null) {
LOG.warn("property is null in in MyApp.getProperty({}). Returning propertyName",propertyName);
return propertyName;
}
LOG.info("Return property value {} for {}", property.trim(), propertyName);
return property.trim();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment