/*
 * Copyright 2012-2016 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.springframework.boot.jta.narayana;

import com.arjuna.ats.arjuna.recovery.RecoveryManager;
import com.arjuna.ats.arjuna.recovery.RecoveryModule;
import com.arjuna.ats.internal.jta.recovery.arjunacore.XARecoveryModule;
import com.arjuna.ats.jbossatx.jta.RecoveryManagerService;
import com.arjuna.ats.jta.recovery.XAResourceRecoveryHelper;

import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.util.Assert;

/**
 * Bean to set up Narayana recovery manager.
 *
 * @author Gytis Trikleris
 * @since 1.4.0
 */
public class NarayanaRecoveryManagerBean implements InitializingBean, DisposableBean {

	private final RecoveryManagerService recoveryManagerService;

	public NarayanaRecoveryManagerBean(RecoveryManagerService recoveryManagerService) {
		Assert.notNull(recoveryManagerService, "RecoveryManagerService must not be null");
		this.recoveryManagerService = recoveryManagerService;
	}

	@Override
	public void afterPropertiesSet() throws Exception {
		this.recoveryManagerService.create();
		this.recoveryManagerService.start();
	}

	@Override
	public void destroy() throws Exception {
		this.recoveryManagerService.stop();
		this.recoveryManagerService.destroy();
	}

	void registerXAResourceRecoveryHelper(
			XAResourceRecoveryHelper xaResourceRecoveryHelper) {
		getXARecoveryModule(RecoveryManager.manager())
				.addXAResourceRecoveryHelper(xaResourceRecoveryHelper);
	}

	private XARecoveryModule getXARecoveryModule(RecoveryManager recoveryManager) {
		for (RecoveryModule recoveryModule : recoveryManager.getModules()) {
			if (recoveryModule instanceof XARecoveryModule) {
				return (XARecoveryModule) recoveryModule;
			}
		}
		throw new IllegalStateException(
				"XARecoveryModule is not registered with recovery manager");
	}

}

@@ -667,117 +667,8 @@
 a;%0A%0A
-import com.arjuna.ats.arjuna.recovery.RecoveryManager;%0Aimport com.arjuna.ats.arjuna.recovery.RecoveryModule;%0A
 impo
@@ -1816,20 +1816,16 @@
 yHelper(
-%0A%09%09%09
 XAResour
@@ -1895,39 +1895,9 @@
 ule(
-RecoveryManager.manager())%0A%09%09%09%09
+)
 .add
@@ -2002,50 +2002,16 @@
 ule(
-RecoveryManager recoveryManager
 ) %7B%0A%09%09
-for (
+XA
 Reco
@@ -2021,17 +2021,19 @@
 yModule 
-r
+xaR
 ecoveryM
@@ -2042,19 +2042,21 @@
 ule 
-: r
+= XAR
 ecoveryM
 anag
@@ -2055,139 +2055,82 @@
 eryM
-anager.getModules()) %7B%0A%09%09%09if (recoveryModule instanceof XARecoveryModule) %7B%0A%09%09%09%09return (XARecoveryModule) recoveryModule;%0A%09%09%09%7D%0A%09%09%7D%0A
+odule.getRegisteredXARecoveryModule();%0A%09%09if (xaRecoveryModule == null) %7B%0A%09
 %09%09th
@@ -2160,16 +2160,17 @@
 eption(%0A
+%09
 %09%09%09%09%22XAR
@@ -2226,14 +2226,45 @@
 ager%22);%0A
+%09%09%7D%0A%09%09return xaRecoveryModule;%0A
 %09%7D%0A%0A%7D%0A

Simplify retrieval of Narayana XARecoveryModule