View Javadoc
1   package org.apache.maven.plugins.ear;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *  http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import org.codehaus.plexus.util.StringUtils;
23  import org.codehaus.plexus.util.xml.XMLWriter;
24  
25  /**
26   * Representation of {@code resource-ref} element in {@code application.xml} file.
27   * 
28   * <pre>
29   * &lt;resource-ref&gt;
30   *   &lt;res-ref-name&gt;jdbc/myDs&lt;/res-ref-name&gt;
31   *   &lt;res-type&gt;javax.sql.DataSource&lt;/res-type&gt;
32   *   &lt;res-auth&gt;Container&lt;/res-auth&gt;
33   * &lt;/resource-ref&gt;
34   * </pre>
35   * 
36   * @author Karl Heinz Marbaise
37   * @since 3.0.0
38   */
39  public class ResourceRef
40  {
41      static final String RESOURCE_REF = "resource-ref";
42  
43      static final String RESOURCE_REF_NAME = "res-ref-name";
44  
45      static final String RESOURCE_TYPE = "res-type";
46  
47      static final String RESOURCE_AUTH = "res-auth";
48  
49      private String name;
50  
51      private String type;
52  
53      private String auth;
54  
55      /**
56       * @param name The res-ref-name.
57       * @param type The res-type
58       * @param auth The res-auth.
59       */
60      public ResourceRef( String name, String type, String auth )
61      {
62          if ( StringUtils.isEmpty( name ) )
63          {
64              throw new IllegalArgumentException( RESOURCE_REF_NAME + " in " + RESOURCE_REF_NAME
65                  + " element cannot be null." );
66          }
67          else if ( StringUtils.isEmpty( type ) && StringUtils.isEmpty( auth ) )
68          {
69              throw new IllegalArgumentException( RESOURCE_TYPE + " in " + RESOURCE_REF_NAME
70                  + " element cannot be null " );
71          }
72  
73          this.name = name;
74          this.type = type;
75          this.auth = auth;
76  
77      }
78  
79      /**
80       * Appends the <tt>XML</tt> representation of this env-entry.
81       * 
82       * @param writer the writer to use
83       */
84      public void appendResourceRefEntry( XMLWriter writer )
85      {
86          writer.startElement( RESOURCE_REF );
87  
88          // res-name
89          doWriteElement( writer, RESOURCE_REF_NAME, getName() );
90  
91          // res_ref-type
92          if ( getType() != null )
93          {
94              doWriteElement( writer, RESOURCE_TYPE, getType() );
95          }
96  
97          // ref-auth
98          if ( getAuth() != null )
99          {
100             doWriteElement( writer, RESOURCE_AUTH, getAuth() );
101         }
102 
103         // end of ejb-ref
104         writer.endElement();
105     }
106 
107     private void doWriteElement( XMLWriter writer, String element, String text )
108     {
109         writer.startElement( element );
110         writer.writeText( text );
111         writer.endElement();
112     }
113 
114     /**
115      * @return {@link #name}
116      */
117     public String getName()
118     {
119         return name;
120     }
121 
122     /**
123      * @param name {@link #name}
124      */
125     public void setName( String name )
126     {
127         this.name = name;
128     }
129 
130     /**
131      * @return {@link #type}
132      */
133     public String getType()
134     {
135         return type;
136     }
137 
138     /**
139      * @param type {@link #type}
140      */
141     public void setType( String type )
142     {
143         this.type = type;
144     }
145 
146     /**
147      * @return {@link #auth}
148      */
149     public String getAuth()
150     {
151         return auth;
152     }
153 
154     /**
155      * @param auth {@link #auth}
156      */
157     public void setAuth( String auth )
158     {
159         this.auth = auth;
160     }
161 
162 }