下面列出了java.util.Dictionary#put ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* Constructor for tests only.
* @param messagingClient
* @param osgiHelper
*/
JmxWrapperForMessagingClient( IMessagingClient messagingClient, OsgiHelper osgiHelper ) {
this.messagingClient = messagingClient != null ? messagingClient : new DismissClient();
// Register the object as service in the OSGi registry.
// Apache Aries should then map it to a MBean if the JMX management
// is available. It is a little bit dirty.
BundleContext bundleCtx = osgiHelper.findBundleContext();
if( bundleCtx != null ) {
this.logger.fine( "Running in an OSGi environment. Trying to register a MBean for the messaging." );
Dictionary<String,String> properties = new Hashtable<> ();
properties.put( "jmx.objectname", "net.roboconf:type=messaging" );
try {
this.serviceReg = bundleCtx.registerService( MessagingApiMBean.class, this, properties );
this.logger.fine( "A MBean was successfully registered for the messaging." );
} catch( Exception e ) {
this.logger.severe( "A MBean could not be registered for the messaging." );
Utils.logException( this.logger, e );
}
}
}
public Dictionary<String, ?> getProperties ()
{
try
{
final Dictionary<String, Object> result = new Hashtable<String, Object> ();
final Map<?, ?> properties = new BeanUtilsBean2 ().describe ( this.targetBean );
for ( final Map.Entry<?, ?> entry : properties.entrySet () )
{
if ( entry.getValue () != null )
{
result.put ( entry.getKey ().toString (), entry.getValue () );
}
}
return result;
}
catch ( final Exception e )
{
logger.warn ( "Failed to get dictionary", e );
return new Hashtable<String, Object> ( 1 );
}
}
public ServiceRegistration<SwingBundleDisplayer> register()
{
if (reg != null) {
return reg;
}
open();
final Dictionary<String, Object> props = new Hashtable<String, Object>();
props.put(SwingBundleDisplayer.PROP_NAME, getName());
props.put(SwingBundleDisplayer.PROP_DESCRIPTION, getDescription());
props.put(SwingBundleDisplayer.PROP_ISDETAIL, isDetail()
? Boolean.TRUE
: Boolean.FALSE);
reg =
Activator.getBC()
.registerService(SwingBundleDisplayer.class, this, props);
return reg;
}
@Test
public void testPut() throws Exception {
EventAdminMock eventAdminMock = new EventAdminMock();
RestCollector collector = new RestCollector();
Dictionary<String, Object> config = new Hashtable<>();
config.put("request.method", "PUT");
config.put("request", "test");
config.put("url", "http://localhost:9090/test/submit");
collector.unmarshaller = new RawUnmarshaller();
collector.dispatcher = eventAdminMock;
collector.activate(config);
collector.run();
Assert.assertEquals(1, eventAdminMock.postedEvents.size());
Event event = eventAdminMock.postedEvents.get(0);
Assert.assertEquals(200, event.getProperty("http.response.code"));
Assert.assertEquals("hello put test\n", event.getProperty("payload"));
}
@Before
public void setUp() throws IOException, InterruptedException {
ttsService = new TTSServiceStub();
hliStub = new HumanLanguageInterpreterStub();
voice = new VoiceStub();
registerService(voice);
registerService(hliStub);
registerService(ttsService);
Dictionary<String, Object> config = new Hashtable<String, Object>();
config.put(CONFIG_DEFAULT_TTS, ttsService.getId());
config.put(CONFIG_DEFAULT_HLI, hliStub.getId());
config.put(CONFIG_DEFAULT_VOICE, voice.getUID());
ConfigurationAdmin configAdmin = super.getService(ConfigurationAdmin.class);
String pid = "org.eclipse.smarthome.voice";
Configuration configuration = configAdmin.getConfiguration(pid);
configuration.update(config);
}
private void registerConsole ()
{
try
{
final Console console = new Console ( this.manager );
final Dictionary<String, Object> properties = new Hashtable<String, Object> ();
properties.put ( "osgi.command.scope", "hds" ); //$NON-NLS-1$
properties.put ( "osgi.command.function", new String[] { "list", "purgeAll", "remove", "create" } ); //$NON-NLS-1$
context.registerService ( Console.class, console, properties );
}
catch ( final Exception e )
{
logger.warn ( "Failed to register console", e ); //$NON-NLS-1$
}
}
@Test
public void testLog4j2ConfigUpdate() throws IOException, ConfigurationException {
Logger logger = LoggerFactory.getLogger(LoggingConfigurationOSGiTest.class);
Assert.assertNotNull(managedService, "Managed Service is null");
//default log level is "ERROR"
Assert.assertEquals(logger.isErrorEnabled(), true);
Assert.assertEquals(logger.isDebugEnabled(), false);
Dictionary<String, Object> properties = new Hashtable<>();
String log4jConfigFilePath = Paths.get(loggingConfigDirectory, LOG4J2_CONFIG_FILE).toString();
properties.put(LOG4J2_CONFIG_FILE_KEY, log4jConfigFilePath);
managedService.updated(properties);
//updated log level is "DEBUG"
Assert.assertEquals(logger.isDebugEnabled(), true);
}
public ChannelEventTriggerHandler(Trigger module, BundleContext bundleContext) {
super(module);
this.eventOnChannel = (String) module.getConfiguration().get(CFG_CHANNEL_EVENT);
this.channelUID = (String) module.getConfiguration().get(CFG_CHANNEL);
this.bundleContext = bundleContext;
this.types.add("ChannelTriggeredEvent");
Dictionary<String, Object> properties = new Hashtable<>();
properties.put("event.topics", TOPIC);
eventSubscriberRegistration = this.bundleContext.registerService(EventSubscriber.class.getName(), this,
properties);
}
private void configureAutoLinking(Boolean on) throws IOException {
ConfigurationAdmin configAdmin = getService(ConfigurationAdmin.class);
org.osgi.service.cm.Configuration config = configAdmin.getConfiguration("org.eclipse.smarthome.links", null);
Dictionary<String, Object> properties = config.getProperties();
if (properties == null) {
properties = new Hashtable<>();
}
properties.put("autoLinks", on.toString());
config.update(properties);
}
private void put ( final Dictionary<String, Object> properties, final String key, final Object value )
{
if ( value instanceof String && ( (String)value ).isEmpty () )
{
return;
}
if ( value != null )
{
properties.put ( key, value );
}
}
@Override
protected Entry<AttributeDataSourceSummarizer> createService ( final UserInformation userInformation, final String configurationId, final BundleContext context, final Map<String, String> parameters ) throws Exception
{
final AttributeDataSourceSummarizer service = new AttributeDataSourceSummarizer ( this.executor, this.tracker );
service.update ( parameters );
final Dictionary<String, String> properties = new Hashtable<String, String> ();
properties.put ( DataSource.DATA_SOURCE_ID, configurationId );
this.pool.addService ( configurationId, service, properties );
return new Entry<AttributeDataSourceSummarizer> ( configurationId, service );
}
public GroupCommandTriggerHandler(Trigger module, BundleContext bundleContext) {
super(module);
this.groupName = (String) module.getConfiguration().get(CFG_GROUPNAME);
this.command = (String) module.getConfiguration().get(CFG_COMMAND);
this.types = Collections.singleton(ItemCommandEvent.TYPE);
this.bundleContext = bundleContext;
Dictionary<String, Object> properties = new Hashtable<>();
this.topic = "smarthome/items/";
properties.put("event.topics", topic);
eventSubscriberRegistration = this.bundleContext.registerService(EventSubscriber.class.getName(), this,
properties);
}
PCEPTopologyProviderBeanCSS(final PCEPTopologyConfiguration configDependencies,
final InstructionScheduler instructionScheduler, final PCEPTopologyProviderBean bean) {
this.scheduler = instructionScheduler;
this.sgi = this.scheduler.getIdentifier();
this.pcepTopoProvider = PCEPTopologyProvider.create(bean, this.scheduler, configDependencies);
final Dictionary<String, String> properties = new Hashtable<>();
properties.put(PCEPTopologyProvider.class.getName(), configDependencies.getTopologyId().getValue());
this.serviceRegistration = bean.bundleContext
.registerService(DefaultTopologyReference.class.getName(), this.pcepTopoProvider, properties);
LOG.info("PCEP Topology Provider service {} registered", getIdentifier().getName());
this.cssRegistration = bean.cssp.registerClusterSingletonService(this);
}
@Override
protected Entry<DataStoreDataSource> createService ( final UserInformation userInformation, final String configurationId, final BundleContext context, final Map<String, String> parameters ) throws Exception
{
logger.debug ( "Creating new memory source: {}", configurationId );
final DataStoreDataSource source = new DataStoreDataSource ( context, configurationId, this.executor, this.dataNodeTracker );
source.update ( parameters );
final Dictionary<String, String> properties = new Hashtable<String, String> ();
properties.put ( DataSource.DATA_SOURCE_ID, configurationId );
this.objectPool.addService ( configurationId, source, properties );
return new Entry<DataStoreDataSource> ( configurationId, source );
}
/**
* Tests configuring the controller.
*/
@Test
public void testConfiguration() {
Dictionary<String, String> properties = new Hashtable<>();
properties.put("openflowPorts", "1,2,3,4,5");
properties.put("workerThreads", "5");
controller.setConfigParams(properties);
IntStream.rangeClosed(1, 5)
.forEach(i -> assertThat(controller.openFlowPorts, hasItem(i)));
assertThat(controller.workerThreads, is(5));
}
@Override
protected Entry<DaveDevice> createService ( final UserInformation userInformation, final String configurationId, final BundleContext context, final Map<String, String> parameters ) throws Exception
{
final DaveDevice device = new DaveDevice ( this.context, configurationId, parameters );
final Dictionary<String, String> properties = new Hashtable<String, String> ();
properties.put ( "daveDevice", configurationId );
return new Entry<DaveDevice> ( configurationId, device, context.registerService ( DaveDevice.class, device, properties ) );
}
@SuppressWarnings("unchecked")
public void init() {
Dictionary properties = new Properties();
properties.put( Constants.SERVICE_PID, "camelinaction.fileinventoryroutefactory");
registration = bundleContext.registerService(ManagedServiceFactory.class.getName(), this, properties);
LOG.info("FileInventoryRouteCamelServiceFactory ready to accept " +
"new config with PID=camelinaction.fileinventoryroutefactory-xxx");
}
/**
* @return String (pid) -> Dictionary
*/
public static List<Dictionary<String, Object>> loadValues(MetaTypeProvider mtp,
XMLElement el)
{
// assertTagName(el, DEFAULTVALUES);
final List<Dictionary<String, Object>> propList =
new ArrayList<Dictionary<String, Object>>();
// String (pid) -> Integer (count)
final Map<String, Integer> countMap = new HashMap<String, Integer>();
for (final Enumeration<?> e = el.enumerateChildren(); e.hasMoreElements();) {
final XMLElement childEl = (XMLElement) e.nextElement();
final String pid = childEl.getName();
ObjectClassDefinition ocd = null;
try {
ocd = mtp.getObjectClassDefinition(pid, null);
} catch (final Exception ignored) {
}
if (ocd == null) {
throw new XMLException("Undefined pid '" + pid + "'", childEl);
}
final Dictionary<String, Object> props =
loadValues(ocd.getAttributeDefinitions(ObjectClassDefinition.ALL),
childEl);
int maxInstances = 1;
if (ocd instanceof OCD) {
maxInstances = ((OCD) ocd).maxInstances;
}
Integer count = countMap.get(pid);
if (count == null) {
count = new Integer(0);
}
count = new Integer(count.intValue() + 1);
if (count.intValue() > maxInstances) {
throw new XMLException("PID " + pid + " can only have " + maxInstances
+ " instance(s), found " + count, el);
}
countMap.put(pid, count);
props.put(maxInstances > 1 ? "factory.pid" : SERVICE_PID, pid);
propList.add(props);
}
return propList;
}
protected ServiceRegistration<Application> registerApplication(
ServiceFactory<Application> serviceFactory, Object... keyValues) {
Dictionary<String, Object> properties = new Hashtable<>();
properties.put(JAX_RS_APPLICATION_BASE, "/test-application");
for (int i = 0; i < keyValues.length; i = i + 2) {
properties.put(keyValues[i].toString(), keyValues[i + 1]);
}
ServiceRegistration<Application> serviceRegistration =
bundleContext.registerService(
Application.class, serviceFactory, properties);
_registrations.add(serviceRegistration);
return serviceRegistration;
}
private void addFactory ( final BundleContext context, final ComponentFactory componentFactory )
{
final Dictionary<String, Object> properties = new Hashtable<> ( 1 );
properties.put ( Constants.SERVICE_VENDOR, "Eclipse SCADA Project" ); //$NON-NLS-1$
this.regs.add ( context.registerService ( ComponentFactory.class, componentFactory, properties ) );
}