Skip to main content
summaryrefslogtreecommitdiffstats
blob: d2fbbca322a601d96898ef7a1b5a332be7d42a31 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
/****************************************************************************
 * Copyright (c) 2005, 2010 Jan S. Rellermeyer, Systems Group,
 * Department of Computer Science, ETH Zurich and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *    Jan S. Rellermeyer - initial API and implementation
 *    Markus Alexander Kuppe - enhancements and bug fixes
 *
*****************************************************************************/
package ch.ethz.iks.slp;

import java.io.Serializable;

/**
 * Implementation of the SLP ServiceType class defined in RFC 2614.
 * 
 * @author Jan S. Rellermeyer, Systems Group, ETH Zurich
 * @since 1.0
 */
public final class ServiceType implements Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = 2821061127250972623L;

	/**
	 * the type.
	 */
	private String type = new String();

	/**
	 * is it abstract ?
	 */
	private final boolean isAbstract;

	/**
	 * the concrete type.
	 */
	private final String concreteType;

	/**
	 * the principle type.
	 */
	private final String principleType;

	/**
	 * the abstract type.
	 */
	private final String abstractType;

	/**
	 * the naming authority.
	 */
	private final String namingAuthority;

	/**
	 * creates a new ServiceType instance.
	 * 
	 * @param serviceType
	 *            the string representation of a ServiceType, e.g.
	 * 
	 *            <pre>
	 *      service:osgi:remote
	 * </pre>
	 */
	public ServiceType(final String serviceType) {
		type = serviceType;
		if (!type.startsWith("service:")) {
			throw new IllegalArgumentException("Invalid service type: "
					+ serviceType);
		}
		final int principleStart = 8;
		final int principleEnd = type.indexOf(":", principleStart);

		if (principleEnd != -1) {
			isAbstract = true;
			principleType = type.substring(principleStart, principleEnd);
			abstractType = type.substring(0, principleEnd);
			concreteType = type.substring(principleEnd + 1);
		} else {
			isAbstract = false;
			principleType = type.substring(principleStart);
			abstractType = "";
			concreteType = "";
		}

		final int namingStart = type.indexOf(".") + 1;
		if (namingStart != 0) {
			final int namingEnd = type.indexOf(":", namingStart);
			String na = "";
			if (namingEnd == -1) {
				na = type.substring(namingStart);
			} else {
				na = type.substring(namingStart, namingEnd);
			}
			// 1954772: isNADefault returns false for "IANA"
			if ("IANA".equalsIgnoreCase(na)) {
				namingAuthority = "";
				// remove "iana" from type so toString() is consistent
				type = type.substring(0, namingStart - 1)
						+ type.substring(namingStart + 4, type.length());
			} else {
				namingAuthority = na;
			}
		} else {
			namingAuthority = "";
		}
	}
		
	/**
	 * Always returns true as invalid service types cannot be created any longer.
	 * 
	 * @return always true.
	 */
	public boolean isServiceURL() {
		//Kept for API compatibility
		//Remove when moved to next major
		return true;
	}

	/**
	 * is the ServiceType instance an abstract type ?
	 * 
	 * @return true if thie is the case.
	 */
	public boolean isAbstractType() {
		return isAbstract;
	}

	/**
	 * is the naming authority default (IANA) ?
	 * 
	 * @return true if this is the case.
	 */
	public boolean isNADefault() {
		return "".equals(namingAuthority);
	}

	/**
	 * get the concrete type part of this ServiceType instance.
	 * 
	 * @return a String representing the concrete type.
	 */
	public String getConcreteTypeName() {
		return concreteType;
	}

	/**
	 * get the principle type part of this ServiceType instance.
	 * 
	 * @return a String representing the principle part.
	 */
	public String getPrincipleTypeName() {
		return principleType;
	}

	/**
	 * get the name of the abstract type of this ServiceType instance.
	 * 
	 * @return a String representing the abstract type.
	 */
	public String getAbstractTypeName() {
		return abstractType;
	}

	/**
	 * get the naming authority.
	 * 
	 * @return the naming authority.
	 */
	public String getNamingAuthority() {
		return namingAuthority;
	}

	/**
	 * check if two ServiceTypes are equal.
	 * 
	 * @param obj
	 *            another ServiceType.
	 * @return true if they equal.
	 */
	public boolean equals(final Object obj) {
		if (!(obj instanceof ServiceType)) {
			return false;
		}
		final ServiceType t = (ServiceType) obj;
		return (isAbstract == t.isAbstract
				&& concreteType.equals(t.concreteType)
				&& principleType.equals(t.principleType)
				&& abstractType.equals(t.abstractType) && namingAuthority
				.equals(t.namingAuthority));
	}

	/**
	 * check if a ServiceType matches a ServiceURL or another ServiceType.
	 * 
	 * @param obj
	 *            the object to be compared to.
	 * @return true if this type matches the other object.
	 */
	public boolean matches(final Object obj) {
		if (!(obj instanceof ServiceType)) {
			return false;
		}
		final ServiceType t = (ServiceType) obj;
		return isAbstract ? equals(t)
				|| t.toString().equals(getAbstractTypeName()) : equals(t);
	}

	/**
	 * get a String representation of this ServiceType instance.
	 * 
	 * @return the String representation.
	 */
	public String toString() {
		return type;
	}

	/**
	 * get the hashCode of this ServiceType instance.
	 * 
	 * @return the int value of the hashCode.
	 */
	public int hashCode() {
		return (concreteType.hashCode()) ^ (principleType.hashCode() << 24)
				^ (abstractType.hashCode() << 16)
				^ (namingAuthority.hashCode() << 8);
	}

}

Back to the top