/arquillian/common/src/main/java/org/jboss/as/arquillian/container/CommonDeployableContainer.java

https://bitbucket.org/cprenzberg/wildfly · Java · 173 lines · 122 code · 29 blank · 22 comment · 2 complexity · 9b3aff543fa5eac7b97c5c76ed4855b2 MD5 · raw file

  1. /*
  2. * JBoss, Home of Professional Open Source
  3. * Copyright 2009, Red Hat Middleware LLC, and individual contributors
  4. * by the @authors tag. See the copyright.txt in the distribution for a
  5. * full listing of individual contributors.
  6. *
  7. * Licensed under the Apache License, Version 2.0 (the "License");
  8. * you may not use this file except in compliance with the License.
  9. * You may obtain a copy of the License at
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. package org.jboss.as.arquillian.container;
  18. import static org.jboss.as.arquillian.container.Authentication.getCallbackHandler;
  19. import java.net.UnknownHostException;
  20. import java.util.Properties;
  21. import java.util.logging.Level;
  22. import java.util.logging.Logger;
  23. import javax.naming.Context;
  24. import javax.naming.InitialContext;
  25. import javax.naming.NamingException;
  26. import org.jboss.arquillian.container.spi.client.container.DeployableContainer;
  27. import org.jboss.arquillian.container.spi.client.container.DeploymentException;
  28. import org.jboss.arquillian.container.spi.client.container.LifecycleException;
  29. import org.jboss.arquillian.container.spi.client.protocol.ProtocolDescription;
  30. import org.jboss.arquillian.container.spi.client.protocol.metadata.ProtocolMetaData;
  31. import org.jboss.arquillian.container.spi.context.annotation.ContainerScoped;
  32. import org.jboss.arquillian.core.api.InstanceProducer;
  33. import org.jboss.arquillian.core.api.annotation.ApplicationScoped;
  34. import org.jboss.arquillian.core.api.annotation.Inject;
  35. import org.jboss.as.controller.client.ModelControllerClient;
  36. import org.jboss.shrinkwrap.api.Archive;
  37. import org.jboss.shrinkwrap.descriptor.api.Descriptor;
  38. import org.jboss.util.NotImplementedException;
  39. import org.xnio.IoUtils;
  40. /**
  41. * A JBossAS deployable container
  42. *
  43. * @author Thomas.Diesler@jboss.com
  44. * @since 17-Nov-2010
  45. */
  46. public abstract class CommonDeployableContainer<T extends CommonContainerConfiguration> implements DeployableContainer<T> {
  47. private static final String JBOSS_URL_PKG_PREFIX = "org.jboss.ejb.client.naming";
  48. private T containerConfig;
  49. @Inject
  50. @ContainerScoped
  51. private InstanceProducer<ManagementClient> managementClient;
  52. @Inject
  53. @ContainerScoped
  54. private InstanceProducer<ArchiveDeployer> archiveDeployer;
  55. @Inject
  56. @ApplicationScoped
  57. private InstanceProducer<Context> jndiContext;
  58. @Override
  59. public ProtocolDescription getDefaultProtocol() {
  60. return new ProtocolDescription("jmx-as7");
  61. }
  62. @Override
  63. public void setup(T config) {
  64. containerConfig = config;
  65. }
  66. @Override
  67. public final void start() throws LifecycleException {
  68. if(containerConfig.getUsername() != null) {
  69. Authentication.username = containerConfig.getUsername();
  70. Authentication.password = containerConfig.getPassword();
  71. }
  72. ModelControllerClient modelControllerClient = null;
  73. try {
  74. modelControllerClient = ModelControllerClient.Factory.create(
  75. containerConfig.getManagementProtocol(),
  76. containerConfig.getManagementAddress(),
  77. containerConfig.getManagementPort(),
  78. getCallbackHandler());
  79. } catch (UnknownHostException e) {
  80. throw new RuntimeException(e);
  81. }
  82. ManagementClient client = new ManagementClient(modelControllerClient, containerConfig.getManagementAddress(), containerConfig.getManagementPort(), containerConfig.getManagementProtocol());
  83. managementClient.set(client);
  84. ArchiveDeployer deployer = new ArchiveDeployer(modelControllerClient);
  85. archiveDeployer.set(deployer);
  86. try {
  87. final Properties jndiProps = new Properties();
  88. jndiProps.setProperty(Context.URL_PKG_PREFIXES, JBOSS_URL_PKG_PREFIX);
  89. jndiContext.set(new InitialContext(jndiProps));
  90. } catch (final NamingException ne) {
  91. throw new LifecycleException("Could not set JNDI Naming Context", ne);
  92. }
  93. try {
  94. startInternal();
  95. } catch (LifecycleException e) {
  96. safeCloseClient();
  97. throw e;
  98. }
  99. }
  100. protected abstract void startInternal() throws LifecycleException;
  101. @Override
  102. public final void stop() throws LifecycleException {
  103. try {
  104. stopInternal();
  105. } finally {
  106. safeCloseClient();
  107. }
  108. }
  109. protected abstract void stopInternal() throws LifecycleException;
  110. protected T getContainerConfiguration() {
  111. return containerConfig;
  112. }
  113. protected ManagementClient getManagementClient() {
  114. return managementClient.get();
  115. }
  116. protected ModelControllerClient getModelControllerClient() {
  117. return getManagementClient().getControllerClient();
  118. }
  119. @Override
  120. public ProtocolMetaData deploy(Archive<?> archive) throws DeploymentException {
  121. String runtimeName = archiveDeployer.get().deploy(archive);
  122. return getManagementClient().getProtocolMetaData(runtimeName);
  123. }
  124. @Override
  125. public void undeploy(Archive<?> archive) throws DeploymentException {
  126. archiveDeployer.get().undeploy(archive.getName());
  127. }
  128. @Override
  129. public void deploy(Descriptor descriptor) throws DeploymentException {
  130. throw new NotImplementedException();
  131. }
  132. @Override
  133. public void undeploy(Descriptor descriptor) throws DeploymentException {
  134. throw new NotImplementedException();
  135. }
  136. private void safeCloseClient() {
  137. try {
  138. IoUtils.safeClose(getManagementClient());
  139. } catch (final Exception e) {
  140. Logger.getLogger(this.getClass().getName()).log(Level.WARNING,
  141. "Caught exception closing ModelControllerClient", e);
  142. }
  143. }
  144. }