---
schema_version: "1"
id: 495ff6b888bff38cdc168207d58bb83da850b7afd723a02a8ea111c8b31cbed8
title: カスタム設定プロパティプロバイダー
collection: mulesoft
version: current
locale: ja-jp
portal_url: https://docs.salesforce.com/mulesoft/current/ja-jp/mule-runtime/4.5/custom-configuration-properties-provider.md
canonical_url: https://docs.mulesoft.com/jp/mule-runtime/4.5/custom-configuration-properties-provider
source_site: docs.mulesoft.com
content_type: documentation
content_hash: dc0f193f85c603211d4730ace62870daf5f277bac18722ba8eb0b3ed8ef5a8d1
description: MuleSoft Documentation Site
release: "4.5"
latest: false
---
# カスタム設定プロパティプロバイダー

Mule SDK および Mule API を使用して、アプリケーションで設定プロパティ値を検出できるようにするカスタム設定プロパティプロバイダーを作成できます。

## [](#mule-api)Mule API

API のメインインターフェースは次のとおりです。

- `ConfigurationPropertiesProviderFactory`  
    このインターフェースは、設定プロパティプロバイダーのカスタム実装を検出するために使用されます。Mule Runtime Engine (Mule) は、​`getSupportedComponentIdentifier()`​ メソッドによって提供される名前空間と名前に一致する設定要素を検出すると、​`createProvider(..)`​ メソッドで定義した設定を使用して ​`ConfigurationPropertiesProvider`​ を作成するようにファクトリーに要求します。Mule は、SPI を使用して ​`ConfigurationPropertiesProviderFactory`​ のインスタンスを検出します。
- `ConfigurationPropertiesProvider`  
    Mule が設定プロパティを検出すると、このインターフェースを呼び出してプロパティの値を解決します。

## [](#例-セキュア設定プロパティプロバイダー)例: セキュア設定プロパティプロバイダー

セキュリティモジュールは、API を使用して ​`ConfigurationPropertiesProviderFactory`​ を実装します。

```java
import static org.mule.runtime.api.component.ComponentIdentifier.builder;

public class SecureConfigurationPropertiesProviderFactory implements ConfigurationPropertiesProviderFactory {

  public static final String EXTENSION_NAMESPACE = "secure-properties";
  public static final String SECURE_CONFIGURATION_PROPERTIES_ELEMENT = "config";
  public static final ComponentIdentifier SECURE_CONFIGURATION_PROPERTIES =
      builder().namespace(EXTENSION_NAMESPACE).name(SECURE_CONFIGURATION_PROPERTIES_ELEMENT).build();

  ...

  @Override
  public ComponentIdentifier getSupportedComponentIdentifier() {
    return SECURE_CONFIGURATION_PROPERTIES;
  }

  @Override
  public SecureConfigurationPropertiesProvider createProvider(ConfigurationParameters parameters,
                                                              ResourceProvider externalResourceProvider) {
    String file = parameters.getStringParameter("file");
    ..

    String key = parameters.getStringParameter("key");
    ...

    return new SecureConfigurationPropertiesProvider(..);
  }

}
```

`getSupportedComponentIdentifier()`​ によって返される ​`ComponentIdentifier`​ は、次の設定コンポーネントに対して照合されます。

```xml
<mule xmlns="http://www.mulesoft.org/schema/mule/core"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xmlns:secure-properties="http://www.mulesoft.org/schema/mule/secure-properties"
      xsi:schemaLocation="
        http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd
        http://www.mulesoft.org/schema/mule/secure-properties http://www.mulesoft.org/schema/mule/secure-properties/current/mule-secure-properties.xsd">

    <secure-properties:config key="decryption-key" file="file1.yaml" name="test">
        <secure-properties:encrypt algorithm="AES" mode="CBC"/>
    </secure-properties:config>

</mule>
```

Mule は、設定を処理するときに ​`secure-properties:config`​ コンポーネントを検出し、​`SecureConfigurationPropertiesProvider.createProvider(..)`​ メソッドを呼び出して、設定プロパティを解決するために使用される ​`SecureConfigurationPropertiesProvider`​ のインスタンスを作成します。

```java
public class SecureConfigurationPropertiesProvider extends DefaultConfigurationPropertiesProvider {

  private final static String SECURE_PREFIX = "secure::";
  private final static Pattern SECURE_PATTERN = Pattern.compile("\\$\\{" + SECURE_PREFIX + "[^}]*}");

  private final EncryptionAlgorithm algorithm;
  private final EncryptionMode mode;
  private final boolean fileLevelEncryption;
  private final SecurePropertyPlaceholderModule securePropertyPlaceholderModule = new SecurePropertyPlaceholderModule();


  public SecureConfigurationPropertiesProvider(ResourceProvider resourceProvider, String file, EncryptionAlgorithm algorithm,
                                               String key, EncryptionMode mode, String encoding, boolean fileLevelEncryption) {
      ...
  }

  @Override
  public Optional<ConfigurationProperty> getConfigurationProperty(String configurationAttributeKey) {
    if (configurationAttributeKey.startsWith(SECURE_PREFIX)) {
      String effectiveKey = configurationAttributeKey.substring(SECURE_PREFIX.length());

      ConfigurationProperty originalConfigurationProperty = configurationAttributes.get(effectiveKey);
      if (originalConfigurationProperty == null) {
        return empty();
      }
      String originalString = ((String) originalConfigurationProperty.getRawValue());
      String encryptedValue = originalString.substring(originalConfigurationProperty.getKey().length() + 1,
                                                       originalString.length() - algorithm.name().length() - mode.name().length()
                                                           - 2);
      final String decryptedValue = resolveInnerProperties(securePropertyPlaceholderModule.convertPropertyValue(encryptedValue));
      return of(new ConfigurationProperty() {

        @Override
        public Object getSource() {
          return originalConfigurationProperty.getSource();
        }

        @Override
        public Object getRawValue() {
          return decryptedValue;
        }

        @Override
        public String getKey() {
          return originalConfigurationProperty.getKey();
        }
      });
    } else {
      return empty();
    }
  }

  @Override
  public String getDescription() {
    ComponentLocation location = (ComponentLocation) getAnnotation(LOCATION_KEY);
    return format("<secure-properties file=\"%s\"> - file: %s, line number: %s", fileLocation,
                  location.getFileName().orElse(UNKNOWN),
                  location.getLineInFile().map(String::valueOf).orElse("unknown"));
  }

  ...
}
```

このリゾルバーに固有のプレフィックス (​`PREFIX::`​ 形式) を定義します。プレフィックスにより、ユーザーは特定のリゾルバーに絞り込むことができます。これは、​`SECURE_PREFIX`​ で定義されるプレフィックスを使用して ​`SecureConfigurationPropertiesProvider`​ に実装されます。

設定では、プレフィックスを次のように使用する必要があります。

```xml
<mule xmlns="http://www.mulesoft.org/schema/mule/core"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xmlns:secure-properties="http://www.mulesoft.org/schema/mule/secure-properties"
      xsi:schemaLocation="
        http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd
        http://www.mulesoft.org/schema/mule/secure-properties http://www.mulesoft.org/schema/mule/secure-properties/current/mule-secure-properties.xsd">

    <secure-properties:config key="decryption-key" file="file1.yaml" name="test">
        <secure-properties:encrypt algorithm="AES" mode="CBC"/>
    </secure-properties:config>

    <flow name="main">
        <set-payload value="${secure::property.key2}"/>
    </flow>

</mule>
```

`set-payload`​ の ​`value`​ 属性でどのようにセキュアなプロパティのリゾルバー (​`secure::`​ プレフィックス) が使用されているのかを確認してください。

## [](#例-mule-sdk-モジュール)例: Mule SDK モジュール

Studio 内でカスタム設定プロパティプロバイダーを有効にする設定要素を作成するには、Mule SDK モジュールを作成する必要があります。

カスタム設定プロパティリゾルバー拡張機能の実装を開始するためのすべてのインフラストラクチャコードが含まれる [サンプルプロジェクト](https://github.com/mulesoft/mule-custom-properties-providers-module-example)​をダウンロードまたはチェックアウトできます。

このサンプルプロジェクトは、Mule SDK モジュールです。詳細は、​[「Mule SDK の使用開始」](https://docs.mulesoft.com/mule-sdk/latest/getting-started)​を参照してください。

### [](#カスタムプロパティソースにアクセスするモジュールをカスタマイズする)カスタムプロパティソースにアクセスするモジュールをカスタマイズする

Mule SDK モジュールをカスタマイズする手順は次のとおりです。

1. [サンプルプロジェクト](https://github.com/mulesoft/mule-custom-properties-providers-module-example)​を任意の IDE にインポートします。
2. `pom.xml`​ ファイルを開きます。
    
    1. モジュールの GAV (​`groupId`​、​`artifactId`​、​`version`​) を定義します。
    2. モジュールの ​`name`​ を定義します。
    3. プロパティプロバイダーが必要とする最小 Mule バージョンを確認します。この例は、考えられるすべてのシナリオをカバーできる Mule 4.1.1 以降と互換性があります。​[「SDK バージョンの選択」](https://docs.mulesoft.com/mule-sdk/latest/choosing-version)​を参照してください。
3. コードのパッケージ名 (​`com.my.company.custom.provider.api`​) を変更します。
4. `resources/META-INF/services/org.mule.runtime.config.api.dsl.model.properties.ConfigurationPropertiesProviderFactory`​ を開き、以前に変更したパッケージ名に合わせてコンテンツを変更します。
5. `CustomConfigurationPropertiesExtension`​ クラスを開きます。
    
    1. `EXTENSION_NAME`​ 定数をモジュールの名前に変更します。
    2. `@Extension`​ アノテーションの ​`vendor`​ メソッドパラメーターを会社名に変更します。
    3. 最後のセクションをカスタマイズして、モジュールの ​`config`​ 要素で設定できるパラメーターを定義します。
6. `CustomConfigurationPropertiesProviderFactory`​ クラスを開きます。
    
    1. モジュールで解決する必要のある設定プロパティに合わせて、​`CUSTOM_PROPERTIES_PREFIX`​ 値をわかりやすいプレフィックスに変更します。
    2. カスタムソースのプロパティをルックアップするようにクラス実装を変更します。
7. 新しいモジュール機能をカバーするために、より多くのテストケースで ​`test/munit`​ の MUnit テストケースを更新します。

モジュールの準備ができたら、​`mvn clean install`​ を使用してローカルにインストールし、Studio からモジュールにアクセスできるようにすることができます。

## [](#mule-アプリケーションでカスタムプロパティプロバイダーを使用する)Mule アプリケーションでカスタムプロパティプロバイダーを使用する

カスタムプロパティプロバイダーを使用する手順は、次のとおりです。

1. Studio でアプリケーションを作成します。
2. 新しいモジュールに連動関係を追加します。
    
    1. `pom.xml`​ ファイルを開きます。
    2. GAV を使用して、モジュールに配置する新しい連動関係を ​`<dependencies>`​ タグ内に追加します。
    3. `<classifier>mule-plugin</classifier>`​ は、Mule モジュールであるため忘れずに追加します。
    4. 変更を保存します。

次に、アプリケーション XML ファイルを開き、​**\[Global Elements (グローバル要素)\]**​ タブで ​**\[Create (作成)\]**​ をクリックします。​**\[Connector Configuration (コネクタ設定)\]**​ には、次のようにカスタムモジュールの設定を選択するオプションがあります。

![custom configuration provider](/mulesoft/current/ja-jp/mule-runtime/4.5/_images/custom-configuration-provider.png)

これで、新しいコンポーネントを設定し、モジュールで定義されたプレフィックスのプロパティを使用できるようになりました。

## [](#カスタム設定プロパティプロバイダーの使用とコネクタの使用)カスタム設定プロパティプロバイダーの使用とコネクタの使用

静的プロパティの場合、実行時に変更されないため、プロパティプロバイダーのアプローチを使用します。実行時にプロパティが変更される可能性がある場合、そのいずれかの操作として値を提供できるコネクタを作成します。

<table><tbody><tr><td class="icon"><i class="fa icon-note" title="Note"></i></td><td class="content"><a href="https://docs.mulesoft.com/mule-sdk/latest/getting-started" class="xref page">Java 用 Mule SDK</a>​ は、カスタム設定プロパティプロバイダーを実装するために使用されますが、​<a href="https://docs.mulesoft.com/mule-sdk/latest/connections" class="xref page">ConnectionProviders</a>​ を使用して外部ソースに接続することはできません。これは、​<code>ConnectionProviders</code>​ が Mule Runtime によって管理されており、静的プロパティの解決後のフェーズで初期化されるためです。そのため、接続テストはカスタムプロパティプロバイダーではサポートされません。</td></tr></tbody></table>