Author: khmarbaise
Date: Sun Aug 27 13:26:55 2017
New Revision: 1806364

URL: http://svn.apache.org/viewvc?rev=1806364&view=rev
Log:
[MEAR-247] resource-ref in generated application.xml
 o Added the generation of resource-ref entries in application.xml

Added:
    
maven/plugins/trunk/maven-ear-plugin/src/main/java/org/apache/maven/plugins/ear/ResourceRef.java
    
maven/plugins/trunk/maven-ear-plugin/src/site/apt/examples/specifying-resource-ref-entries-for-the-generated-application-xml.apt.vm
Modified:
    
maven/plugins/trunk/maven-ear-plugin/src/main/java/org/apache/maven/plugins/ear/ApplicationXmlWriter.java
    
maven/plugins/trunk/maven-ear-plugin/src/main/java/org/apache/maven/plugins/ear/ApplicationXmlWriterContext.java
    
maven/plugins/trunk/maven-ear-plugin/src/main/java/org/apache/maven/plugins/ear/GenerateApplicationXmlMojo.java
    maven/plugins/trunk/maven-ear-plugin/src/site/apt/index.apt.vm

Modified: 
maven/plugins/trunk/maven-ear-plugin/src/main/java/org/apache/maven/plugins/ear/ApplicationXmlWriter.java
URL: 
http://svn.apache.org/viewvc/maven/plugins/trunk/maven-ear-plugin/src/main/java/org/apache/maven/plugins/ear/ApplicationXmlWriter.java?rev=1806364&r1=1806363&r2=1806364&view=diff
==============================================================================
--- 
maven/plugins/trunk/maven-ear-plugin/src/main/java/org/apache/maven/plugins/ear/ApplicationXmlWriter.java
 (original)
+++ 
maven/plugins/trunk/maven-ear-plugin/src/main/java/org/apache/maven/plugins/ear/ApplicationXmlWriter.java
 Sun Aug 27 13:26:55 2017
@@ -33,9 +33,11 @@ import org.codehaus.plexus.util.xml.XMLW
 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 @@ final class ApplicationXmlWriter
             {
                 ejbEntry.appendEjbRefEntry( writer );
             }
+            for ( ResourceRef resourceEntry : context.getResourceRefs() )
+            {
+                resourceEntry.appendResourceRefEntry( writer );
+            }
         }
 
         writer.endElement();

Modified: 
maven/plugins/trunk/maven-ear-plugin/src/main/java/org/apache/maven/plugins/ear/ApplicationXmlWriterContext.java
URL: 
http://svn.apache.org/viewvc/maven/plugins/trunk/maven-ear-plugin/src/main/java/org/apache/maven/plugins/ear/ApplicationXmlWriterContext.java?rev=1806364&r1=1806363&r2=1806364&view=diff
==============================================================================
--- 
maven/plugins/trunk/maven-ear-plugin/src/main/java/org/apache/maven/plugins/ear/ApplicationXmlWriterContext.java
 (original)
+++ 
maven/plugins/trunk/maven-ear-plugin/src/main/java/org/apache/maven/plugins/ear/ApplicationXmlWriterContext.java
 Sun Aug 27 13:26:55 2017
@@ -40,9 +40,11 @@ class ApplicationXmlWriterContext
     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 @@ class ApplicationXmlWriterContext
 
     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 @@ class ApplicationXmlWriterContext
     }
 
     /**
+     * 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

Modified: 
maven/plugins/trunk/maven-ear-plugin/src/main/java/org/apache/maven/plugins/ear/GenerateApplicationXmlMojo.java
URL: 
http://svn.apache.org/viewvc/maven/plugins/trunk/maven-ear-plugin/src/main/java/org/apache/maven/plugins/ear/GenerateApplicationXmlMojo.java?rev=1806364&r1=1806363&r2=1806364&view=diff
==============================================================================
--- 
maven/plugins/trunk/maven-ear-plugin/src/main/java/org/apache/maven/plugins/ear/GenerateApplicationXmlMojo.java
 (original)
+++ 
maven/plugins/trunk/maven-ear-plugin/src/main/java/org/apache/maven/plugins/ear/GenerateApplicationXmlMojo.java
 Sun Aug 27 13:26:55 2017
@@ -152,6 +152,12 @@ public class GenerateApplicationXmlMojo
     private PlexusConfiguration ejbRefs;
 
     /**
+     * The {@code resource-ref} entries.
+     */
+    @Parameter
+    private PlexusConfiguration resourceRefs;
+
+    /**
      * {@inheritDoc}
      */
     public void execute()
@@ -241,8 +247,9 @@ public class GenerateApplicationXmlMojo
         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 );
     }
 
@@ -425,6 +432,63 @@ public class GenerateApplicationXmlMojo
                 }
             }
             return result;
+        }
+        catch ( InterpolationException e )
+        {
+            throw new EarPluginException( "Interpolation exception:", e );
+        }
+
+    }
+
+    /**
+     * 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 )
         {

Added: 
maven/plugins/trunk/maven-ear-plugin/src/main/java/org/apache/maven/plugins/ear/ResourceRef.java
URL: 
http://svn.apache.org/viewvc/maven/plugins/trunk/maven-ear-plugin/src/main/java/org/apache/maven/plugins/ear/ResourceRef.java?rev=1806364&view=auto
==============================================================================
--- 
maven/plugins/trunk/maven-ear-plugin/src/main/java/org/apache/maven/plugins/ear/ResourceRef.java
 (added)
+++ 
maven/plugins/trunk/maven-ear-plugin/src/main/java/org/apache/maven/plugins/ear/ResourceRef.java
 Sun Aug 27 13:26:55 2017
@@ -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;
+    }
+
+}

Added: 
maven/plugins/trunk/maven-ear-plugin/src/site/apt/examples/specifying-resource-ref-entries-for-the-generated-application-xml.apt.vm
URL: 
http://svn.apache.org/viewvc/maven/plugins/trunk/maven-ear-plugin/src/site/apt/examples/specifying-resource-ref-entries-for-the-generated-application-xml.apt.vm?rev=1806364&view=auto
==============================================================================
--- 
maven/plugins/trunk/maven-ear-plugin/src/site/apt/examples/specifying-resource-ref-entries-for-the-generated-application-xml.apt.vm
 (added)
+++ 
maven/plugins/trunk/maven-ear-plugin/src/site/apt/examples/specifying-resource-ref-entries-for-the-generated-application-xml.apt.vm
 Sun Aug 27 13:26:55 2017
@@ -0,0 +1,57 @@
+  ------
+  Specifying Resource Refs For The Generated application.xml
+  ------
+  Karl Heinz Marbaise <khmarba...@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>
++---------

Modified: maven/plugins/trunk/maven-ear-plugin/src/site/apt/index.apt.vm
URL: 
http://svn.apache.org/viewvc/maven/plugins/trunk/maven-ear-plugin/src/site/apt/index.apt.vm?rev=1806364&r1=1806363&r2=1806364&view=diff
==============================================================================
--- maven/plugins/trunk/maven-ear-plugin/src/site/apt/index.apt.vm (original)
+++ maven/plugins/trunk/maven-ear-plugin/src/site/apt/index.apt.vm Sun Aug 27 
13:26:55 2017
@@ -126,6 +126,9 @@ ${project.name}
   * 
{{{./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}}


Reply via email to