KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > geronimo > gbean > AbstractNameQuery


1 /**
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17
18 package org.apache.geronimo.gbean;
19
20 import java.io.Serializable JavaDoc;
21 import java.net.URI JavaDoc;
22 import java.net.URISyntaxException JavaDoc;
23 import java.util.ArrayList JavaDoc;
24 import java.util.Collections JavaDoc;
25 import java.util.HashSet JavaDoc;
26 import java.util.Iterator JavaDoc;
27 import java.util.List JavaDoc;
28 import java.util.Map JavaDoc;
29 import java.util.Set JavaDoc;
30 import java.util.TreeMap JavaDoc;
31 import java.util.TreeSet JavaDoc;
32
33 import org.apache.geronimo.kernel.repository.Artifact;
34
35 /**
36  * @version $Rev: 476049 $ $Date: 2006-11-16 23:35:17 -0500 (Thu, 16 Nov 2006) $
37  */

38 public class AbstractNameQuery implements Serializable JavaDoc {
39     private static final long serialVersionUID = 7444620122607155678L;
40
41     private final Artifact artifact;
42     private final Map JavaDoc name;
43     private final Set JavaDoc interfaceTypes;
44
45     private final URI JavaDoc uri;
46
47     public AbstractNameQuery(AbstractName abstractName) {
48         this(abstractName, null);
49     }
50
51     public AbstractNameQuery(AbstractName abstractName, Set JavaDoc interfaceTypes) {
52         this.artifact = abstractName.getArtifact();
53         this.name = abstractName.getName();
54         this.interfaceTypes = interfaceTypes == null ? Collections.EMPTY_SET : interfaceTypes;
55         this.uri = createURI(artifact, name, this.interfaceTypes);
56     }
57
58     public AbstractNameQuery(Artifact artifact, Map JavaDoc name) {
59         this.artifact = artifact;
60         this.name = name;
61         this.interfaceTypes = Collections.EMPTY_SET;
62         this.uri = createURI(artifact, name, interfaceTypes);
63     }
64
65     public AbstractNameQuery(Artifact artifact, Map JavaDoc name, String JavaDoc interfaceType) {
66         this.artifact = artifact;
67         this.name = name;
68         if (interfaceType != null) {
69             this.interfaceTypes = Collections.singleton(interfaceType);
70         } else {
71             this.interfaceTypes = Collections.EMPTY_SET;
72         }
73         this.uri = createURI(artifact, name, interfaceTypes);
74     }
75
76     public AbstractNameQuery(String JavaDoc interfaceType) {
77         this.artifact = null;
78         this.name = Collections.EMPTY_MAP;
79         this.interfaceTypes = Collections.singleton(interfaceType);
80         this.uri = createURI(artifact, name, interfaceTypes);
81     }
82
83     public AbstractNameQuery(Artifact artifact, Map JavaDoc name, Set JavaDoc interfaceTypes) {
84         this.artifact = artifact;
85         this.name = name;
86         if (interfaceTypes == null) interfaceTypes = Collections.EMPTY_SET;
87         this.interfaceTypes = interfaceTypes;
88         this.uri = createURI(artifact, name, this.interfaceTypes);
89     }
90
91     public AbstractNameQuery(URI JavaDoc uri) {
92         if (uri == null) throw new NullPointerException JavaDoc("uri is null");
93
94         //
95
// Artifact
96
//
97
String JavaDoc artifactString = uri.getPath();
98         //this doesn't seem to happen
99
// if (artifactString == null) throw new IllegalArgumentException("uri does not contain a path part used for the artifact");
100

101         if (artifactString != null && artifactString.length() > 0) {
102             List JavaDoc artifactParts = split(artifactString, '/');
103             if (artifactParts.size() != 4) {
104                 throw new IllegalArgumentException JavaDoc("uri path must be in the form [groupId]/[artifactId]/[version]/[type] : " + artifactString);
105             }
106
107             String JavaDoc groupId = (String JavaDoc) artifactParts.get(0);
108             if (groupId.length() == 0) groupId = null;
109
110             String JavaDoc artifactId = (String JavaDoc) artifactParts.get(1);
111             if (artifactId.length() == 0) artifactId = null;
112
113             String JavaDoc version = (String JavaDoc) artifactParts.get(2);
114             if (version.length() == 0) version = null;
115
116             String JavaDoc type = (String JavaDoc) artifactParts.get(3);
117             if (type.length() == 0) type = null;
118
119             artifact = new Artifact(groupId, artifactId, version, type);
120         } else {
121             artifact = null;
122         }
123
124         //
125
// name map
126
//
127
name = new TreeMap JavaDoc();
128         String JavaDoc nameString = uri.getQuery();
129         List JavaDoc nameParts = split(nameString, ',');
130         for (Iterator JavaDoc iterator = nameParts.iterator(); iterator.hasNext();) {
131             String JavaDoc namePart = (String JavaDoc) iterator.next();
132             List JavaDoc keyValue = split(namePart, '=');
133             if (keyValue.size() != 2) {
134                 throw new IllegalArgumentException JavaDoc("uri query string must be in the form [vendorId]/artifactId/[version]/[type] : " + nameString);
135             }
136             String JavaDoc key = (String JavaDoc) keyValue.get(0);
137             String JavaDoc value = (String JavaDoc) keyValue.get(1);
138             if (name.containsKey(key)) {
139                 throw new IllegalArgumentException JavaDoc("uri query string contains the key '" + key + "' twice : " + nameString);
140             }
141             name.put(key, value);
142         }
143 // if (name.isEmpty()) {
144
// throw new IllegalArgumentException("name is empty: " + nameString);
145
// }
146

147         String JavaDoc interfaceString = uri.getFragment();
148         List JavaDoc interfaces = split(interfaceString, ',');
149         interfaceTypes = new HashSet JavaDoc(interfaces);
150
151         //
152
// uri
153
//
154
this.uri = createURI(artifact, name, interfaceTypes);
155     }
156
157     private static List JavaDoc split(String JavaDoc source, char delim) {
158         List JavaDoc parts = new ArrayList JavaDoc();
159         if (source != null && source.length() > 0) {
160             for (int index = source.indexOf(delim); index >= 0; index = source.indexOf(delim)) {
161                 String JavaDoc part = source.substring(0, index);
162                 source = source.substring(index + 1);
163                 parts.add(part);
164             }
165             parts.add(source);
166         }
167         return parts;
168     }
169
170     private static URI JavaDoc createURI(Artifact artifact, Map JavaDoc name, Set JavaDoc interfaceTypes) {
171         StringBuffer JavaDoc queryString = new StringBuffer JavaDoc();
172         TreeMap JavaDoc treeMap = new TreeMap JavaDoc(name);
173         for (Iterator JavaDoc iterator = treeMap.entrySet().iterator(); iterator.hasNext();) {
174             Map.Entry JavaDoc entry = (Map.Entry JavaDoc) iterator.next();
175             String JavaDoc key = (String JavaDoc) entry.getKey();
176             String JavaDoc value = (String JavaDoc) entry.getValue();
177             queryString.append(key).append('=').append(value);
178             if (iterator.hasNext()) {
179                 queryString.append(',');
180             }
181         }
182         StringBuffer JavaDoc fragmentString = new StringBuffer JavaDoc();
183         TreeSet JavaDoc treeSet = new TreeSet JavaDoc(interfaceTypes);
184         for (Iterator JavaDoc iterator = treeSet.iterator(); iterator.hasNext();) {
185             String JavaDoc interfaceType = (String JavaDoc) iterator.next();
186             fragmentString.append(interfaceType);
187             if (iterator.hasNext()) {
188                 fragmentString.append(',');
189             }
190         }
191         try {
192             return new URI JavaDoc(null, null, artifact == null? null: artifact.toString(), queryString.toString(), fragmentString.toString());
193         } catch (URISyntaxException JavaDoc e) {
194             IllegalArgumentException JavaDoc illegalArgumentException = new IllegalArgumentException JavaDoc();
195             illegalArgumentException.initCause(e);
196             throw illegalArgumentException;
197         }
198     }
199
200
201     public Artifact getArtifact() {
202         return artifact;
203     }
204
205     public Map JavaDoc getName() {
206         return name;
207     }
208
209     public Set JavaDoc getInterfaceTypes() {
210         return interfaceTypes;
211     }
212
213     public String JavaDoc toString() {
214         return uri.toString();
215     }
216
217     public URI JavaDoc toURI() {
218         return uri;
219     }
220
221     public boolean equals(Object JavaDoc o) {
222         if (this == o) return true;
223         if (o == null || getClass() != o.getClass()) return false;
224
225         final AbstractNameQuery that = (AbstractNameQuery) o;
226
227         if (artifact != null ? !artifact.equals(that.artifact) : that.artifact != null) return false;
228         if (interfaceTypes != null ? !interfaceTypes.equals(that.interfaceTypes) : that.interfaceTypes != null)
229             return false;
230         return !(name != null ? !name.equals(that.name) : that.name != null);
231
232     }
233
234     public int hashCode() {
235         int result;
236         result = (artifact != null ? artifact.hashCode() : 0);
237         result = 29 * result + (name != null ? name.hashCode() : 0);
238         result = 29 * result + (interfaceTypes != null ? interfaceTypes.hashCode() : 0);
239         return result;
240     }
241
242
243     public boolean matches(AbstractName info, Set JavaDoc targetInterfaceTypes) {
244         if (!info.getName().entrySet().containsAll(name.entrySet())) {
245             return false;
246         }
247         if (!targetInterfaceTypes.containsAll(interfaceTypes)) {
248             return false;
249         }
250         if (artifact == null) {
251             return true;
252         }
253         Artifact otherArtifact = info.getArtifact();
254         return artifact.matches(otherArtifact);
255     }
256
257     /**
258      * N.B. parameter info is supposed to be more specific than this.
259      * This is the opposite of the meaning of Artifact.matches.
260      *
261      * @param info
262      * @return if info is a more specific version of this name query.
263      */

264     public boolean matches(AbstractNameQuery info) {
265         if (!info.getName().entrySet().containsAll(name.entrySet())) {
266             return false;
267         }
268         if (!info.getInterfaceTypes().containsAll(interfaceTypes)) {
269             return false;
270         }
271         if (artifact == null) {
272             return true;
273         }
274         Artifact otherArtifact = info.getArtifact();
275         return artifact.matches(otherArtifact);
276     }
277 }
278
Popular Tags