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