【问题标题】:How to access cfg file in KARAF/etc using PAX-CDI如何使用 PAX-CDI 访问 KARAF/etc 中的 cfg 文件
【发布时间】:2017-11-18 12:45:02
【问题描述】:

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

例如:

KARAF_HOME\etc\import.cfg

如何使用它 @OsgiServiceProvider

【问题讨论】:

  • 那些 *.cfg 文件通常被视为配置管理员的配置。由于您已经在 OSGi 环境中运行,因此最好坚持其工作方式,因此只需让配置管理员来配置您的 CDI 对象。最好也向任何一个相关社区询问,例如 karaf 和 ops4j。两者都有邮件列表,你会在那里得到适当的反馈。
  • 感谢您的建议,但我不尝试修改现有配置,而是尝试访问添加到我的捆绑包的新配置文件
  • 这同样适用:)

标签: osgi karaf pax


【解决方案1】:

你可以通过 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 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=我的秘密

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-14
    • 2014-12-26
    • 1970-01-01
    • 2016-11-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多