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 resource-ref} element in {@code application.xml} file.
25 *
26 * <pre>
27 * <resource-ref>
28 * <res-ref-name>jdbc/myDs</res-ref-name>
29 * <res-type>javax.sql.DataSource</res-type>
30 * <res-auth>Container</res-auth>
31 * </resource-ref>
32 * </pre>
33 * or
34 * <pre>
35 * <resource-ref>
36 * <res-ref-name>jdbc/myDs</res-ref-name>
37 * <res-type>javax.sql.DataSource</res-type>
38 * <lookup-name>jdbc/lookup/myDs</lookup-name>
39 * </resource-ref>
40 * </pre>
41 *
42 * @author Karl Heinz Marbaise
43 * @since 3.0.0
44 */
45 public class ResourceRef {
46 static final String RESOURCE_REF = "resource-ref";
47
48 static final String RESOURCE_REF_NAME = "res-ref-name";
49
50 static final String RESOURCE_TYPE = "res-type";
51
52 static final String RESOURCE_AUTH = "res-auth";
53
54 static final String LOOKUP_NAME = "lookup-name";
55
56 private String name;
57
58 private String type;
59
60 private String auth;
61
62 private String lookupName;
63
64 /**
65 * @param name The res-ref-name.
66 * @param type The res-type
67 * @param auth The res-auth.
68 * @param lookupName The lookup-name.
69 */
70 public ResourceRef(String name, String type, String auth, String lookupName) {
71 if (name == null || name.isEmpty()) {
72 throw new IllegalArgumentException(
73 RESOURCE_REF_NAME + " in " + RESOURCE_REF_NAME + " element cannot be null.");
74 } else if ((type == null || type.isEmpty()) && (auth == null || auth.isEmpty())) {
75 throw new IllegalArgumentException(RESOURCE_TYPE + " in " + RESOURCE_REF_NAME + " element cannot be null ");
76 }
77
78 this.name = name;
79 this.type = type;
80 this.auth = auth;
81 this.lookupName = lookupName;
82 }
83
84 /**
85 * Appends the {@code XML} representation of this env-entry.
86 *
87 * @param writer the writer to use
88 */
89 public void appendResourceRefEntry(XMLWriter writer) {
90 writer.startElement(RESOURCE_REF);
91
92 // res-name
93 doWriteElement(writer, RESOURCE_REF_NAME, getName());
94
95 // res_ref-type
96 if (getType() != null) {
97 doWriteElement(writer, RESOURCE_TYPE, getType());
98 }
99
100 // ref-auth
101 if (getAuth() != null) {
102 doWriteElement(writer, RESOURCE_AUTH, getAuth());
103 }
104
105 // lookup-name
106 if (getLookupName() != null) {
107 doWriteElement(writer, LOOKUP_NAME, getLookupName());
108 }
109
110 // end of ejb-ref
111 writer.endElement();
112 }
113
114 private void doWriteElement(XMLWriter writer, String element, String text) {
115 writer.startElement(element);
116 writer.writeText(text);
117 writer.endElement();
118 }
119
120 /**
121 * @return {@link #name}
122 */
123 public String getName() {
124 return name;
125 }
126
127 /**
128 * @param name {@link #name}
129 */
130 public void setName(String name) {
131 this.name = name;
132 }
133
134 /**
135 * @return {@link #type}
136 */
137 public String getType() {
138 return type;
139 }
140
141 /**
142 * @param type {@link #type}
143 */
144 public void setType(String type) {
145 this.type = type;
146 }
147
148 /**
149 * @return {@link #auth}
150 */
151 public String getAuth() {
152 return auth;
153 }
154
155 /**
156 * @param auth {@link #auth}
157 */
158 public void setAuth(String auth) {
159 this.auth = auth;
160 }
161
162 /**
163 * @return {@link #lookupName}
164 */
165 public String getLookupName() {
166 return lookupName;
167 }
168
169 /**
170 * @param lookupName {@link #lookupName}
171 */
172 public void setLookupName(String lookupName) {
173 this.lookupName = lookupName;
174 }
175 }