001 /* 002 * Copyright (C) Christian Schulte, 2005-206 003 * All rights reserved. 004 * 005 * Redistribution and use in source and binary forms, with or without 006 * modification, are permitted provided that the following conditions 007 * are met: 008 * 009 * o Redistributions of source code must retain the above copyright 010 * notice, this list of conditions and the following disclaimer. 011 * 012 * o Redistributions in binary form must reproduce the above copyright 013 * notice, this list of conditions and the following disclaimer in 014 * the documentation and/or other materials provided with the 015 * distribution. 016 * 017 * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, 018 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY 019 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL 020 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY DIRECT, INDIRECT, 021 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 022 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 023 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 024 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 025 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 026 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 027 * 028 * $JOMC: AbstractResourcesWriteMojo.java 4469 2012-04-01 00:12:58Z schulte2005 $ 029 * 030 */ 031 package org.jomc.mojo; 032 033 import java.io.File; 034 import java.util.Locale; 035 import java.util.logging.Level; 036 import javax.xml.bind.JAXBContext; 037 import javax.xml.bind.util.JAXBSource; 038 import javax.xml.transform.Source; 039 import org.apache.commons.io.FileUtils; 040 import org.apache.maven.model.Resource; 041 import org.apache.maven.plugin.MojoExecutionException; 042 import org.apache.maven.project.MavenProject; 043 import org.jomc.model.Module; 044 import org.jomc.modlet.ModelContext; 045 import org.jomc.modlet.ModelValidationReport; 046 import org.jomc.modlet.ObjectFactory; 047 import org.jomc.tools.ResourceFileProcessor; 048 049 /** 050 * Base class for writing resource files. 051 * 052 * @author <a href="mailto:schulte2005@users.sourceforge.net">Christian Schulte</a> 053 * @version $JOMC: AbstractResourcesWriteMojo.java 4469 2012-04-01 00:12:58Z schulte2005 $ 054 */ 055 public abstract class AbstractResourcesWriteMojo extends AbstractJomcMojo 056 { 057 058 /** Constant for the name of the tool backing the class. */ 059 private static final String TOOLNAME = "ResourceFileProcessor"; 060 061 /** 062 * The language of the default language properties file of generated resource bundle properties resources. 063 * 064 * @parameter expression="${jomc.resourceBundleDefaultLanguage}" 065 */ 066 private String resourceBundleDefaultLanguage; 067 068 /** Creates a new {@code AbstractResourcesWriteMojo} instance. */ 069 public AbstractResourcesWriteMojo() 070 { 071 super(); 072 } 073 074 @Override 075 protected final void executeTool() throws Exception 076 { 077 this.logSeparator(); 078 079 if ( this.isResourceProcessingEnabled() ) 080 { 081 this.logProcessingModule( TOOLNAME, this.getResourcesModuleName() ); 082 083 final ModelContext context = this.createModelContext( this.getResourcesClassLoader() ); 084 final ResourceFileProcessor tool = this.createResourceFileProcessor( context ); 085 final JAXBContext jaxbContext = context.createContext( this.getModel() ); 086 final Source source = new JAXBSource( jaxbContext, new ObjectFactory().createModel( tool.getModel() ) ); 087 final ModelValidationReport validationReport = context.validateModel( this.getModel(), source ); 088 089 if ( this.resourceBundleDefaultLanguage != null ) 090 { 091 tool.setResourceBundleDefaultLocale( 092 new Locale( this.resourceBundleDefaultLanguage.toLowerCase( Locale.ENGLISH ) ) ); 093 094 } 095 096 this.log( context, validationReport.isModelValid() ? Level.INFO : Level.SEVERE, validationReport ); 097 098 if ( validationReport.isModelValid() ) 099 { 100 final Module module = tool.getModules().getModule( this.getResourcesModuleName() ); 101 102 if ( module != null ) 103 { 104 if ( !this.getResourcesDirectory().exists() && !this.getResourcesDirectory().mkdirs() ) 105 { 106 throw new MojoExecutionException( Messages.getMessage( 107 "failedCreatingDirectory", this.getResourcesDirectory().getAbsolutePath() ) ); 108 109 } 110 111 tool.writeResourceBundleResourceFiles( module, this.getResourcesDirectory() ); 112 113 if ( !this.getResourcesDirectory().equals( this.getResourcesOutputDirectory() ) ) 114 { 115 FileUtils.copyDirectory( this.getResourcesDirectory(), this.getResourcesOutputDirectory() ); 116 } 117 118 final Resource resource = new Resource(); 119 resource.setDirectory( this.getResourcesDirectory().getAbsolutePath() ); 120 resource.setFiltering( false ); 121 122 this.addMavenResource( this.getMavenProject(), resource ); 123 124 this.logToolSuccess( TOOLNAME ); 125 } 126 else 127 { 128 this.logMissingModule( this.getResourcesModuleName() ); 129 } 130 } 131 else 132 { 133 throw new MojoExecutionException( Messages.getMessage( "resourceProcessingFailure" ) ); 134 } 135 } 136 else if ( this.isLoggable( Level.INFO ) ) 137 { 138 this.log( Level.INFO, Messages.getMessage( "resourceProcessingDisabled" ), null ); 139 } 140 } 141 142 /** 143 * Gets the name of the module to write resource files of. 144 * 145 * @return The name of the module to write resource files of. 146 * 147 * @throws MojoExecutionException if getting the name fails. 148 */ 149 protected abstract String getResourcesModuleName() throws MojoExecutionException; 150 151 /** 152 * Gets the class loader to use for writing resource files. 153 * 154 * @return The class loader to use for writing resource files. 155 * 156 * @throws MojoExecutionException if getting the class loader fails. 157 */ 158 protected abstract ClassLoader getResourcesClassLoader() throws MojoExecutionException; 159 160 /** 161 * Gets the directory to write the resource files to. 162 * 163 * @return The directory to write the resource files to. 164 * 165 * @throws MojoExecutionException if getting the directory fails. 166 */ 167 protected abstract File getResourcesDirectory() throws MojoExecutionException; 168 169 /** 170 * Gets the directory to copy resource files to. 171 * 172 * @return The directory to copy resource files to. 173 * 174 * @throws MojoExecutionException if getting the directory fails. 175 * 176 * @since 1.2 177 */ 178 protected abstract File getResourcesOutputDirectory() throws MojoExecutionException; 179 180 /** 181 * Adds a resource to a {@code MavenProjecŧ}. 182 * 183 * @param mavenProject The {@code MavenProject} to add a resource to. 184 * @param resource The {@code Resource} to add. 185 * 186 * @throws MojoExecutionException if adding the resource fails. 187 * 188 * @since 1.2 189 */ 190 protected abstract void addMavenResource( MavenProject mavenProject, Resource resource ) 191 throws MojoExecutionException; 192 193 }