如何使用PAX-CDI访问KARAF / etc中的cfg文件

时间:2017-06-15 21:33:39

标签: osgi karaf pax

如何使用PAX-CDI访问KARAF / etc中的cfg文件

例如:

KARAF_HOME \等\ import.cfg

如何使用它 @OsgiServiceProvider

1 个答案:

答案 0 :(得分:0)

您可以通过DOSGI加载并将属性放入Util中,然后使用Pax-CDI代码访问

<强> DOSGI

@Component(immediate = true, configurationPolicy = ConfigurationPolicy.REQUIRE, configurationPid = "ca.esc.pbm.frontend.security")
public class SecurityConfig{

public static final String TOKEN_SIG_SECRET = "signatureSecret";

@Activate
    private void activate(BundleContext bundleContext, Map<String, ?> properties) throws Exception {

        logger.info("Security Config Activated");

        Properties props = new Properties();

        if (properties.isEmpty()) {
            throw new ComponentException("Config Properties is Empty ");
        }

        logger.info("Property loaded " + properties.size());

        props.putAll(properties);

        PropertiesUtil.setProperty(props);

        System.out.println(properties);

    }
}

Property Util

/**
 * Property Util to load the *.properties form class path
 * 
 * @version 1.0
 */
public class PropertiesUtil {

private static Map<String, String> propertiesMap;

/**
 * Get the property value for the given key from the loaded property
 * 
 * @param name
 * @return String
 */
public static String getProperty(String name) {
    return (String) propertiesMap.get(name);
}

/**
 * Set the property value for the given key from the loaded property
 * 
 * @param props
 */
public static void setProperty(Properties props) {
    Map<String, String> propertiesMapLocal = new HashMap<String, String>();

    for (Object key : props.keySet()) {
        String keyStr = key.toString();
        propertiesMapLocal.put(keyStr, props.getProperty(keyStr));
    }

    propertiesMap = propertiesMapLocal;
}

}

<强> PAX-CDI

@ApplicationScoped
@Named
public class DefaultApiImpl implements DefaultApi {

@Override
    public Response login(String userAgent, String username, String password, UriInfo uriInfo) {

PropertiesUtil.getProperty(SecurityConfig.TOKEN_SIG_SECRET);

}

cfg文件

你可以拥有karaf_home / etc / ca.esc.pbm.frontend.security.cfg 持有一个价值 signatureSecret =我的秘密