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