PageRenderTime 24ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/modules/core/src/main/java/org/torquebox/core/processors/AbstractSplitYamlParsingProcessor.java

https://gitlab.com/meetly/torquebox
Java | 194 lines | 120 code | 37 blank | 37 comment | 27 complexity | 1846367782368e2992e882685a6a3c41 MD5 | raw file
  1. /*
  2. * Copyright 2008-2013 Red Hat, Inc, and individual contributors.
  3. *
  4. * This is free software; you can redistribute it and/or modify it
  5. * under the terms of the GNU Lesser General Public License as
  6. * published by the Free Software Foundation; either version 2.1 of
  7. * the License, or (at your option) any later version.
  8. *
  9. * This software is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * Lesser General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU Lesser General Public
  15. * License along with this software; if not, write to the Free
  16. * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
  17. * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
  18. */
  19. package org.torquebox.core.processors;
  20. import java.util.List;
  21. import org.jboss.as.server.deployment.Attachments;
  22. import org.jboss.as.server.deployment.DeploymentPhaseContext;
  23. import org.jboss.as.server.deployment.DeploymentUnit;
  24. import org.jboss.as.server.deployment.DeploymentUnitProcessingException;
  25. import org.jboss.as.server.deployment.module.ResourceRoot;
  26. import org.jboss.logging.Logger;
  27. import org.jboss.vfs.VirtualFile;
  28. import org.projectodd.polyglot.core.processors.AbstractParsingProcessor;
  29. import org.projectodd.polyglot.core.util.DeploymentUtils;
  30. import org.projectodd.polyglot.core.util.DeprecationLogger;
  31. import org.torquebox.core.TorqueBoxMetaData;
  32. import org.torquebox.core.app.RubyAppMetaData;
  33. import org.torquebox.core.util.YAMLUtils;
  34. /**
  35. * Abstract deployer base-class supporting <code>torquebox.yml</code> sectional
  36. * parsing.
  37. *
  38. * <p>
  39. * For a given subsystem 'foo', a torquebox.yml section named 'foo:' can
  40. * configure it or optionally (deprecated) a file named foo.yml.
  41. * </p>
  42. *
  43. * @author Bob McWhirter
  44. */
  45. public abstract class AbstractSplitYamlParsingProcessor extends AbstractParsingProcessor {
  46. /** Name of the section within torquebox.yml. */
  47. private String sectionName;
  48. /** Optional file-name for NAME.yml parsing separate from torquebox.yml. */
  49. private String fileName;
  50. /** Does this deploy unit support a standalone *.yml descriptor? */
  51. private boolean supportsStandalone = true;
  52. private boolean standaloneDeprecated = true;
  53. /** Does this deploy support a *-<name>.yml format? */
  54. private boolean supportsSuffix = false;
  55. /** Is the app root required for this deploy unit? **/
  56. private boolean supportsRootless = false;
  57. private static final Logger log = Logger.getLogger( "org.torquebox.core" );
  58. public static void logDeprecation(DeploymentUnit unit, String message) {
  59. DeprecationLogger.getLogger( unit ).append( message );
  60. }
  61. public AbstractSplitYamlParsingProcessor() {
  62. }
  63. @Override
  64. public void deploy(DeploymentPhaseContext phaseContext) throws DeploymentUnitProcessingException {
  65. DeploymentUnit unit = phaseContext.getDeploymentUnit();
  66. ResourceRoot resourceRoot = unit.getAttachment( Attachments.DEPLOYMENT_ROOT );
  67. TorqueBoxMetaData globalMetaData = unit.getAttachment( TorqueBoxMetaData.ATTACHMENT_KEY );
  68. Object data = null;
  69. if (globalMetaData != null) {
  70. data = globalMetaData.getSection( getSectionName() );
  71. }
  72. VirtualFile root = resourceRoot.getRoot();
  73. if (data == null && isSupportsStandalone()) {
  74. VirtualFile metaDataFile = getMetaDataFile( root, getFileName() );
  75. if ((metaDataFile == null || !metaDataFile.exists()) && this.supportsSuffix) {
  76. List<VirtualFile> matches = getMetaDataFileBySuffix( root, "-" + getFileName() );
  77. if (!matches.isEmpty()) {
  78. if (matches.size() > 1) {
  79. log.warn( "Multiple matches: " + matches );
  80. }
  81. metaDataFile = matches.get( 0 );
  82. }
  83. }
  84. if ((metaDataFile != null) && metaDataFile.exists()) {
  85. if (!metaDataFile.equals( root ) && this.standaloneDeprecated) {
  86. logDeprecation( unit, "Usage of " + getFileName() + " is deprecated. Please use torquebox.yml." );
  87. }
  88. try {
  89. data = YAMLUtils.parseYaml( metaDataFile );
  90. } catch (Exception e) {
  91. throw new DeploymentUnitProcessingException( "Error processing yaml: ", e );
  92. }
  93. }
  94. } else {
  95. // If data has been specified for this section, and the deployment
  96. // is rootless,
  97. // but rootlessness is not supported, then error out.
  98. RubyAppMetaData rubyMetaData = unit.getAttachment( RubyAppMetaData.ATTACHMENT_KEY );
  99. if (data != null && !isSupportsRootless() && rubyMetaData != null && DeploymentUtils.isUnitRootless( unit )) {
  100. throw new DeploymentUnitProcessingException( String.format(
  101. "Error processing deployment %s: The section %s requires an app root to be specified, but none has been provided.",
  102. unit.getName(), getSectionName() ) );
  103. }
  104. }
  105. if (data == null) {
  106. return;
  107. }
  108. try {
  109. parse( unit, data );
  110. } catch (DeploymentUnitProcessingException e) {
  111. throw e;
  112. } catch (Exception e) {
  113. e.printStackTrace();
  114. throw new DeploymentUnitProcessingException( e );
  115. }
  116. }
  117. public String getFileName() {
  118. if (this.fileName != null) {
  119. return this.fileName;
  120. }
  121. return getSectionName() + ".yml";
  122. }
  123. public String getSectionName() {
  124. return this.sectionName;
  125. }
  126. public boolean isSupportsRootless() {
  127. return supportsRootless;
  128. }
  129. public boolean isStandaloneDeprecated() {
  130. return this.standaloneDeprecated;
  131. }
  132. public boolean isSupportsStandalone() {
  133. return this.supportsStandalone;
  134. }
  135. public boolean isSupportsSuffix() {
  136. return this.supportsSuffix;
  137. }
  138. protected abstract void parse(DeploymentUnit unit, Object data) throws Exception;
  139. public void setFileName(String fileName) {
  140. this.fileName = fileName;
  141. }
  142. public void setSupportsRootless(boolean supportsRootless) {
  143. this.supportsRootless = supportsRootless;
  144. }
  145. public void setSectionName(String sectionName) {
  146. this.sectionName = sectionName;
  147. }
  148. public void setStandaloneDeprecated(boolean deprecated) {
  149. this.standaloneDeprecated = deprecated;
  150. }
  151. public void setSupportsStandalone(boolean supports) {
  152. this.supportsStandalone = supports;
  153. }
  154. public void setSupportsSuffix(boolean supports) {
  155. this.supportsSuffix = supports;
  156. }
  157. }