Thursday, 15 January 2015

Created Field 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.osgi.framework.Constants;
import org.osgi.service.component.ComponentContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@Component(immediate = true, metatype = true, label = "Random Social Share Config")
@Properties({
@Property(name = Constants.SERVICE_DESCRIPTION, value = "Random Social Share Config"),
@Property(name = Constants.SERVICE_VENDOR, value = "Random") })
@Service(value = SocialShareID.class)
public class SocialShareID {
private static final Logger log = LoggerFactory.getLogger(SocialShareID.class);

@Property(label = "data_ga_tracker", value = "", description = "This value is Environment Specific")
public static final String SOCIAL_SHARE_ID = "data_ga_tracker";

private static Dictionary props = null;

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

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;
    }

@Activate
   protected void activate( ComponentContext context ) {
       try {
           if(log.isDebugEnabled()) {
               log.debug( "Socail Share Config 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( "Socail Share Config Modified" );
           }          
           props = context.getProperties();
       } catch ( Exception e ) {
           log.error( "Error occured in modified method", e );
       }
   }
}


In JSP add below line
import class and
<c:set var="getSocialShareID" value='<%=SocialShareID.getStringPropertyValue(SocialShareID.SOCIAL_SHARE_ID)%>'/>

No comments:

Post a Comment