Thursday, 15 January 2015

Create Multifield in Felix

import java.util.Dictionary;
import org.apache.felix.scr.annotations.Activate;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Modified;
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.OsgiUtil;
import org.osgi.framework.Constants;
import org.osgi.service.component.ComponentContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@Component(immediate = true, label = "Random Configuration Service", description = "Random Configuration Service", metatype = true)
@Properties({
@Property(name = Constants.SERVICE_DESCRIPTION, value = "Random Configuration Service"),
@Property(name = Constants.SERVICE_VENDOR, value = "Random") })
@Service(value = RandomConfigurationService.class)
public class RandomConfigurationService {

private static final Logger log = LoggerFactory
.getLogger(RandomConfigurationService.class);
@Property(label = "socialConnectUrl", value = "http://www.Random.com/wsq/socialMedia?", description = "Url to be used to get Social connect details")
public static final String SOCIAL_CONNECT_SERVICE_URL = "socialConnectUrl";

@Property(label = "linkedinServiceUrl", value = "http://localhost:8080/Random/service/linkedin?", description = "Url to be used to get linkedin feed details")
public static final String LINKEDIN_SERVICE_URL = "linkedinServiceUrl";


@Property(label = "faceBookUrl", value = "https://www.facebook.com/", description = "Url to be used for facebook like,comment and share")
public static final String FACEBOOK_BASE_URL = "faceBookUrl";

@Property(label = "twitterReplyToUrl", value = "https://twitter.com/intent/tweet?in_reply_to=", description = "Url to be used to reply to twitter post")
public static final String TWITTER_REPLY_TO_URL = "twitterReplyToUrl";

@Property(label = "twitterRetwetUrl", value = "https://twitter.com/intent/retweet?tweet_id=", description = "Url to be used to retweet to twitter post")
public static final String TWITTER_RETWEET_URL = "twitterRetwetUrl";

@Property(label = "twitterFavoriteUrl", value = "https://twitter.com/intent/favorite?tweet_id=", description = "Url to be used to favorite to twiter post")
public static final String TWITTER_FAVORITE_URL = "twitterFavoriteUrl";

@Property(label = "linkedinUrl", value = "https://www.linkedin.com/company/", description = "Url to be used for linkedin like,comment and share")
public static final String LINKEDIN_BASE_URL = "linkedinUrl";

@Property(value={"us-en|Randomcom","uk-en|Random.co.uk"}, label="Random Domains", description="Defines the Random domains.")
public static final String Random_DOMAINS = "Random.domain";


    public static String getPropertyValue(String key) {
return (String) props.get(key);
}

private static Dictionary props = null;

/**
     * Method to get String property value from config. If property can not be found or can not be cast to a string,
     * return default string with key. This will make it apparent if there is some config missing
     *
     * @param key
     * @return configuration value String
     */
    public static String getStringPropertyValue( String key ) {
        String property = (String)props.get( key );

        if ( property != null ) {
            return property;
        }
        log.error("String property does not exist in configuration: " + key);
        return null;
    }
 
    /**
     * Method to get String array property values from config. If property can not be found or can not be cast to a string,
     * return default string with key. This will make it apparent if there is some config missing
     *
     * @param key
     * @return configuration value String Array
     */
    public static String[] getStringArrayPropertyValue( String key ) {
        String[] property = OsgiUtil.toStringArray(props.get(key));;

        if ( property != null ) {
            return property;
        }
        log.error("String property does not exist in configuration: " + key);
        return null;
    }

 
    @Activate
    protected void activate( ComponentContext context ) {
        try {
            if(log.isDebugEnabled()) {
                log.debug( "SN Socail Media Config Service Activated" );
            }
         
            props = context.getProperties();
        } catch ( Exception e ) {
            log.error( "Error occured in activate method", e );
        }
    }

    @Modified
    protected void modified( ComponentContext context ) {
        try {
            if(log.isDebugEnabled()) {
                log.debug( "SN Socail Media Config Service Modified" );
            }
         
            props = context.getProperties();
        } catch ( Exception e ) {
            log.error( "Error occured in modified method", e );
        }
    }

}



For array Values call as
String[] RandomDomains = RandomConfigurationService.getStringArrayPropertyValue(RandomConfigurationService.Random_DOMAINS);

No comments:

Post a Comment