[MEAR-247] resource-ref in generated application.xml
 o Added the generation of resource-ref entries in application.xml


git-svn-id: https://svn.apache.org/repos/asf/maven/plugins/trunk@1806364 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/src/main/java/org/apache/maven/plugins/ear/ApplicationXmlWriter.java b/src/main/java/org/apache/maven/plugins/ear/ApplicationXmlWriter.java
index 58dbd3a..6c6142d 100644
--- a/src/main/java/org/apache/maven/plugins/ear/ApplicationXmlWriter.java
+++ b/src/main/java/org/apache/maven/plugins/ear/ApplicationXmlWriter.java
@@ -33,9 +33,11 @@
 final class ApplicationXmlWriter

     extends AbstractXmlWriter

 {

-    public static final String DOCTYPE_1_3 = "application PUBLIC\n"

-        + "\t\"-//Sun Microsystems, Inc.//DTD J2EE Application 1.3//EN\"\n"

-        + "\t\"http://java.sun.com/dtd/application_1_3.dtd\"";

+    //@formatter:off

+    public static final String DOCTYPE_1_3 = "application PUBLIC\n" 

+            + "\t\"-//Sun Microsystems, Inc.//DTD J2EE Application 1.3//EN\"\n"

+            + "\t\"http://java.sun.com/dtd/application_1_3.dtd\"";

+    //@formatter:on

 

     private static final String APPLICATION_ELEMENT = "application";

 

@@ -134,6 +136,10 @@
             {

                 ejbEntry.appendEjbRefEntry( writer );

             }

+            for ( ResourceRef resourceEntry : context.getResourceRefs() )

+            {

+                resourceEntry.appendResourceRefEntry( writer );

+            }

         }

 

         writer.endElement();

diff --git a/src/main/java/org/apache/maven/plugins/ear/ApplicationXmlWriterContext.java b/src/main/java/org/apache/maven/plugins/ear/ApplicationXmlWriterContext.java
index 6cd9fdc..314f330 100644
--- a/src/main/java/org/apache/maven/plugins/ear/ApplicationXmlWriterContext.java
+++ b/src/main/java/org/apache/maven/plugins/ear/ApplicationXmlWriterContext.java
@@ -40,9 +40,11 @@
     private final List<SecurityRole> securityRoles;

 

     private final List<EnvEntry> envEntries;

-    

+

     private final List<EjbRef> ejbEntries;

 

+    private final List<ResourceRef> resourceRefs;

+

     private final String displayName;

 

     private final String description;

@@ -55,15 +57,16 @@
 

     public ApplicationXmlWriterContext( File destinationFile, List<EarModule> earModules,

                                         List<SecurityRole> securityRoles, List<EnvEntry> envEntries,

-                                        List<EjbRef> ejbEntries,

-                                        String displayName, String description, String libraryDirectory,

-                                        String applicationName, Boolean initializeInOrder )

+                                        List<EjbRef> ejbEntries, List<ResourceRef> resourceRefs, String displayName,

+                                        String description, String libraryDirectory, String applicationName,

+                                        Boolean initializeInOrder )

     {

         this.destinationFile = destinationFile;

         this.earModules = earModules;

         this.securityRoles = securityRoles;

         this.envEntries = envEntries;

         this.ejbEntries = ejbEntries;

+        this.resourceRefs = resourceRefs;

         this.displayName = displayName;

         this.description = description;

         this.libraryDirectory = libraryDirectory;

@@ -133,6 +136,16 @@
     }

 

     /**

+     * Returns the list of {@link ResourceRef}.

+     * 

+     * @return the resource-ref elements.

+     */

+    public List<ResourceRef> getResourceRefs()

+    {

+        return resourceRefs;

+    }

+

+    /**

      * Returns the display name.

      * 

      * @return the display name

diff --git a/src/main/java/org/apache/maven/plugins/ear/GenerateApplicationXmlMojo.java b/src/main/java/org/apache/maven/plugins/ear/GenerateApplicationXmlMojo.java
index 501d5fe..a430d53 100644
--- a/src/main/java/org/apache/maven/plugins/ear/GenerateApplicationXmlMojo.java
+++ b/src/main/java/org/apache/maven/plugins/ear/GenerateApplicationXmlMojo.java
@@ -152,6 +152,12 @@
     private PlexusConfiguration ejbRefs;

 

     /**

+     * The {@code resource-ref} entries.

+     */

+    @Parameter

+    private PlexusConfiguration resourceRefs;

+

+    /**

      * {@inheritDoc}

      */

     public void execute()

@@ -241,8 +247,9 @@
         final ApplicationXmlWriter writer = new ApplicationXmlWriter( javaEEVersion, encoding, generateModuleId );

         final ApplicationXmlWriterContext context =

             new ApplicationXmlWriterContext( descriptor, getModules(), buildSecurityRoles(), buildEnvEntries(),

-                                             buildEjbEntries(), displayName, description, getActualLibraryDirectory(),

-                                             applicationName, initializeInOrder ).setApplicationId( applicationId );

+                                             buildEjbEntries(), buildResourceRefs(), displayName, description,

+                                             getActualLibraryDirectory(), applicationName,

+                                             initializeInOrder ).setApplicationId( applicationId );

         writer.write( context );

     }

 

@@ -434,6 +441,63 @@
     }

 

     /**

+     * Builds the <code>resource-ref</code> based on the configuration.

+     * 

+     * @return a list of ResourceRef object(s)

+     * @throws EarPluginException if the configuration is invalid

+     */

+    private List<ResourceRef> buildResourceRefs()

+        throws EarPluginException

+    {

+        final List<ResourceRef> result = new ArrayList<ResourceRef>();

+        if ( resourceRefs == null )

+        {

+            return result;

+        }

+        try

+        {

+            getLog().debug( "Resources found" );

+            StringSearchInterpolator ssi = new StringSearchInterpolator();

+            ValueSource vs = new MapBasedValueSource( project.getProperties() );

+            ssi.addValueSource( vs );

+

+            // TODO: Check if this is a good idea hard code that here? Better idea?

+            final PlexusConfiguration[] allResourceRefEntries = resourceRefs.getChildren( "resourceRef" );

+

+            getLog().debug( "allResourceRefEntries: " + allResourceRefEntries );

+            getLog().debug( "allResourceRefEntries length: " + allResourceRefEntries.length );

+            for ( PlexusConfiguration resEntry : allResourceRefEntries )

+            {

+                getLog().debug( "Resources resEntry:" + resEntry.getName() );

+

+                // CHECKSTYLE_OFF: LineLength

+                final String childResRefName =

+                    interpolate( ssi, resEntry.getChild( ResourceRef.RESOURCE_REF_NAME ).getValue() );

+                final String childResType =

+                    interpolate( ssi, resEntry.getChild( ResourceRef.RESOURCE_TYPE ).getValue() );

+                final String childResRefAuth =

+                    interpolate( ssi, resEntry.getChild( ResourceRef.RESOURCE_AUTH ).getValue() );

+                // CHECKSTYLE_ON: LineLength

+

+                try

+                {

+                    result.add( new ResourceRef( childResRefName, childResType, childResRefAuth ) );

+                }

+                catch ( IllegalArgumentException e )

+                {

+                    throw new EarPluginException( "Invalid resource-ref [" + resEntry + "]", e );

+                }

+            }

+            return result;

+        }

+        catch ( InterpolationException e )

+        {

+            throw new EarPluginException( "Interpolation exception:", e );

+        }

+

+    }

+

+    /**

      * Returns the value to use for the <tt>library-directory</tt> element, based on the library directory mode.

      */

     private String getActualLibraryDirectory()

diff --git a/src/main/java/org/apache/maven/plugins/ear/ResourceRef.java b/src/main/java/org/apache/maven/plugins/ear/ResourceRef.java
new file mode 100644
index 0000000..5f3d905
--- /dev/null
+++ b/src/main/java/org/apache/maven/plugins/ear/ResourceRef.java
@@ -0,0 +1,162 @@
+package org.apache.maven.plugins.ear;
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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.
+ */
+
+import org.codehaus.plexus.util.StringUtils;
+import org.codehaus.plexus.util.xml.XMLWriter;
+
+/**
+ * Representation of {@code resource-ref} element in {@code application.xml} file.
+ * 
+ * <pre>
+ * &lt;resource-ref&gt;
+ *   &lt;res-ref-name&gt;jdbc/myDs&lt;/res-ref-name&gt;
+ *   &lt;res-type&gt;javax.sql.DataSource&lt;/res-type&gt;
+ *   &lt;res-auth&gt;Container&lt;/res-auth&gt;
+ * &lt;/resource-ref&gt;
+ * </pre>
+ * 
+ * @author Karl Heinz Marbaise
+ * @since 3.0.0
+ */
+public class ResourceRef
+{
+    static final String RESOURCE_REF = "resource-ref";
+
+    static final String RESOURCE_REF_NAME = "res-ref-name";
+
+    static final String RESOURCE_TYPE = "res-type";
+
+    static final String RESOURCE_AUTH = "res-auth";
+
+    private String name;
+
+    private String type;
+
+    private String auth;
+
+    /**
+     * @param name The res-ref-name.
+     * @param type The res-type
+     * @param auth The res-auth.
+     */
+    public ResourceRef( String name, String type, String auth )
+    {
+        if ( StringUtils.isEmpty( name ) )
+        {
+            throw new IllegalArgumentException( RESOURCE_REF_NAME + " in " + RESOURCE_REF_NAME
+                + " element cannot be null." );
+        }
+        else if ( StringUtils.isEmpty( type ) && StringUtils.isEmpty( auth ) )
+        {
+            throw new IllegalArgumentException( RESOURCE_TYPE + " in " + RESOURCE_REF_NAME
+                + " element cannot be null " );
+        }
+
+        this.name = name;
+        this.type = type;
+        this.auth = auth;
+
+    }
+
+    /**
+     * Appends the <tt>XML</tt> representation of this env-entry.
+     * 
+     * @param writer the writer to use
+     */
+    public void appendResourceRefEntry( XMLWriter writer )
+    {
+        writer.startElement( RESOURCE_REF );
+
+        // res-name
+        doWriteElement( writer, RESOURCE_REF_NAME, getName() );
+
+        // res_ref-type
+        if ( getType() != null )
+        {
+            doWriteElement( writer, RESOURCE_TYPE, getType() );
+        }
+
+        // ref-auth
+        if ( getAuth() != null )
+        {
+            doWriteElement( writer, RESOURCE_AUTH, getAuth() );
+        }
+
+        // end of ejb-ref
+        writer.endElement();
+    }
+
+    private void doWriteElement( XMLWriter writer, String element, String text )
+    {
+        writer.startElement( element );
+        writer.writeText( text );
+        writer.endElement();
+    }
+
+    /**
+     * @return {@link #name}
+     */
+    public String getName()
+    {
+        return name;
+    }
+
+    /**
+     * @param name {@link #name}
+     */
+    public void setName( String name )
+    {
+        this.name = name;
+    }
+
+    /**
+     * @return {@link #type}
+     */
+    public String getType()
+    {
+        return type;
+    }
+
+    /**
+     * @param type {@link #type}
+     */
+    public void setType( String type )
+    {
+        this.type = type;
+    }
+
+    /**
+     * @return {@link #auth}
+     */
+    public String getAuth()
+    {
+        return auth;
+    }
+
+    /**
+     * @param auth {@link #auth}
+     */
+    public void setAuth( String auth )
+    {
+        this.auth = auth;
+    }
+
+}
diff --git a/src/site/apt/examples/specifying-resource-ref-entries-for-the-generated-application-xml.apt.vm b/src/site/apt/examples/specifying-resource-ref-entries-for-the-generated-application-xml.apt.vm
new file mode 100644
index 0000000..fe854a7
--- /dev/null
+++ b/src/site/apt/examples/specifying-resource-ref-entries-for-the-generated-application-xml.apt.vm
@@ -0,0 +1,57 @@
+  ------
+  Specifying Resource Refs For The Generated application.xml
+  ------
+  Karl Heinz Marbaise <khmarbaise@apache.org>
+  ------
+  August 27, 2017
+
+~~ Copyright 2006 The Apache Software Foundation.
+~~
+~~ 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.
+
+~~ NOTE: For help with the syntax of this file, see:
+~~ http://maven.apache.org/doxia/references/apt-format.html
+
+
+Specifying Resource Ref entries For The Generated application.xml
+
+  Resource Ref entries can be added as from the JavaEE 6 spec. For instance:
+
++--------
+  <build>
+    <plugins>
+      <plugin>
+        <groupId>org.apache.maven.plugins</groupId>
+        <artifactId>maven-ear-plugin</artifactId>
+        <version>${project.version}</version>
+        <configuration>
+           [...]
+           <version>6</version>
+           [...]
+           <resourceRefs>
+             <resourceRef>
+               <res-ref-name>jdbc/myDs1</res-ref-name>
+               <res-type>javax.sql.DataSource</res-type>
+               <res-auth>Container</res-auth>
+             </resourceRef>
+             <resourceRef>
+               <res-ref-name>jdbc/myDs2</res-ref-name>
+               <res-type>javax.sql.DataSource</res-type>
+               <res-auth>Container</res-auth>
+             </resourceRef>
+            </resourceRefs>
+          </configuration>
+      </plugin>
+    </plugins>
+  </build>
++---------
diff --git a/src/site/apt/index.apt.vm b/src/site/apt/index.apt.vm
index c178672..f355c41 100644
--- a/src/site/apt/index.apt.vm
+++ b/src/site/apt/index.apt.vm
@@ -126,6 +126,9 @@
   * {{{./examples/specifying-env-entries-for-the-generated-application-xml.html}
     Specifying Environment Entries For The Generated application.xml}}
 
+  * {{{./examples/specifying-resource-ref-entries-for-the-generated-application-xml.html}
+    Specifying Resource Ref Entries For The Generated application.xml}}
+
   * {{{./examples/generating-jboss-app.html} Generating the jboss-app.xml file}}
 
   * {{{./examples/generating-modules-id.html} Generating modules id}}