#chiroito ’s blog

Java を中心とした趣味の技術について

Infinispan の Hot Rod で設定ファイルとコードによる設定を組み合わせる

Infinispanを触ってて、設定ファイルで設定した内容に加え、コードによる設定を足したいなと思うことがあったので書いてみました。 以下の通りにするとHot Rodの設定ファイルであるresources/hotrod-client.propertiesを読み込みつつ、コードによる設定もできます。

ConfigurationBuilder cb = new ConfigurationBuilder();
// 設定ファイルの読み込み
ClassLoader cl = Thread.currentThread().getContextClassLoader();
cb.classLoader(cl);
InputStream stream = FileLookupFactory.newInstance().lookupFile("hotrod-client.properties", cl);
Properties properties = new Properties();
properties.load(stream);
cb.withProperties(properties);
// コードによる設定
cb.addContextInitializer(new CustomInitializerImpl());

RemoteCacheManager manager = new RemoteCacheManager(cb.build());

参考にしたソースはorg.infinispan.client.hotrod.RemoteCacheManagerクラスのRemoteCacheManager(boolean start)Properties loadFromStream(InputStream stream)です。

   public RemoteCacheManager(boolean start) {
      ConfigurationBuilder builder = new ConfigurationBuilder();
      ClassLoader cl = Thread.currentThread().getContextClassLoader();
      builder.classLoader(cl);
      InputStream stream = FileLookupFactory.newInstance().lookupFile(HOTROD_CLIENT_PROPERTIES, cl);
      if (stream == null) {
         HOTROD.couldNotFindPropertiesFile(HOTROD_CLIENT_PROPERTIES);
      } else {
         try {
            builder.withProperties(loadFromStream(stream));
         } finally {
            Util.close(stream);
         }
      }
      this.configuration = builder.build();
      this.counterManager = new RemoteCounterManager();
      this.syncTransactionTable = new SyncModeTransactionTable(configuration.transaction().timeout());
      this.xaTransactionTable = new XaModeTransactionTable(configuration.transaction().timeout());
      registerMBean();
      if (start) actualStart();
   }
   private Properties loadFromStream(InputStream stream) {
      Properties properties = new Properties();
      try {
         properties.load(stream);
      } catch (IOException e) {
         throw new HotRodClientException("Issues configuring from client hotrod-client.properties", e);
      }
      return properties;
   }