Skip to main content
summaryrefslogtreecommitdiffstats
blob: 740359bb13a7b56bf8b836603019b55757c7c773 (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
/*******************************************************************************
 * Copyright (c) 2004 Composent, Inc. 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: Composent, Inc. - initial API and implementation
 ******************************************************************************/

package org.eclipse.ecf.discovery;

import java.io.Serializable;
import java.net.URI;
import org.eclipse.core.runtime.Assert;
import org.eclipse.core.runtime.IAdapterManager;
import org.eclipse.ecf.discovery.identity.IServiceID;
import org.eclipse.ecf.internal.discovery.DiscoveryPlugin;

/**
 * Base implementation of {@link IServiceInfo}.  Subclasses
 * may be created as appropriate.
 */
public class ServiceInfo implements IServiceInfo, Serializable, IContainerServiceInfoAdapter {

	private static final long serialVersionUID = -5651115550295457142L;

	public static final int DEFAULT_PRIORITY = 0;
	public static final int DEFAULT_WEIGHT = 0;

	protected URI uri = null;

	protected IServiceID serviceID;

	protected int priority;

	protected int weight;

	protected IServiceProperties properties;

	protected ServiceInfo() {
		// null constructor for subclasses
	}

	public ServiceInfo(URI anURI, IServiceID serviceID, int priority, int weight, IServiceProperties props) {
		this.uri = anURI;
		Assert.isNotNull(this.uri);
		this.serviceID = serviceID;
		Assert.isNotNull(serviceID);
		this.priority = priority;
		this.weight = weight;
		this.properties = (props == null) ? new ServiceProperties() : props;
	}

	public ServiceInfo(URI anURI, IServiceID serviceID, IServiceProperties props) {
		this(anURI, serviceID, DEFAULT_PRIORITY, DEFAULT_WEIGHT, props);
	}

	public ServiceInfo(URI anURI, IServiceID serviceID) {
		this(anURI, serviceID, DEFAULT_PRIORITY, DEFAULT_WEIGHT, new ServiceProperties());
	}

	/* (non-Javadoc)
	 * @see org.eclipse.ecf.discovery.IServiceInfo#getAddress()
	 */
	public URI getLocation() {
		return uri;
	}

	protected void setLocation(URI address) {
		this.uri = address;
	}

	/* (non-Javadoc)
	 * @see org.eclipse.ecf.discovery.IServiceInfo#getServiceID()
	 */
	public IServiceID getServiceID() {
		return serviceID;
	}

	/* (non-Javadoc)
	 * @see org.eclipse.ecf.discovery.IServiceInfo#getPriority()
	 */
	public int getPriority() {
		return priority;
	}

	/* (non-Javadoc)
	 * @see org.eclipse.ecf.discovery.IServiceInfo#getWeight()
	 */
	public int getWeight() {
		return weight;
	}

	/* (non-Javadoc)
	 * @see org.eclipse.ecf.discovery.IServiceInfo#getServiceProperties()
	 */
	public IServiceProperties getServiceProperties() {
		return properties;
	}

	/* (non-Javadoc)
	 * @see org.eclipse.ecf.discovery.IServiceInfo#isResolved()
	 */
	public boolean isResolved() {
		return (uri != null);
	}

	/* (non-Javadoc)
	 * @see java.lang.Object#toString()
	 */
	public String toString() {
		final StringBuffer buf = new StringBuffer("ServiceInfo["); //$NON-NLS-1$
		buf.append("uri=").append(uri).append(";id=").append(serviceID) //$NON-NLS-1$ //$NON-NLS-2$
				.append(";priority=").append( //$NON-NLS-1$
						priority).append(";weight=").append(weight).append( //$NON-NLS-1$
						";props=").append(properties).append("]"); //$NON-NLS-1$ //$NON-NLS-2$
		return buf.toString();
	}

	/* (non-Javadoc)
	 * @see org.eclipse.core.runtime.IAdaptable#getAdapter(java.lang.Class)
	 */
	public Object getAdapter(Class adapter) {
		if (adapter.isInstance(this)) {
			return this;
		}
		final IAdapterManager adapterManager = DiscoveryPlugin.getDefault().getAdapterManager();
		if (adapterManager == null)
			return null;
		return adapterManager.loadAdapter(this, adapter.getName());
	}

	/* (non-Javadoc)
	 * @see org.eclipse.ecf.discovery.IContainerServiceInfoAdapter#getContainerFactoryName()
	 */
	public String getContainerFactoryName() {
		return (properties == null) ? null : properties.getPropertyString(IDiscoveryContainerAdapter.CONTAINER_FACTORY_NAME_PROPERTY);
	}

	/* (non-Javadoc)
	 * @see org.eclipse.ecf.discovery.IContainerServiceInfoAdapter#getTarget()
	 */
	public String getConnectTarget() {
		if (uri == null || properties == null)
			return null;
		String connectTarget = properties.getPropertyString(IDiscoveryContainerAdapter.CONTAINER_CONNECT_TARGET);
		if (connectTarget != null)
			return connectTarget;
		String t = properties.getPropertyString(IDiscoveryContainerAdapter.CONTAINER_CONNECT_TARGET_PROTOCOL);
		StringBuffer target = new StringBuffer(t);
		String auth = uri.getAuthority();
		String path = properties.getPropertyString(IDiscoveryContainerAdapter.CONTAINER_CONNECT_TARGET_PATH);
		if (path == null)
			path = "/"; //$NON-NLS-1$
		target.append("://").append(auth).append("/").append(path); //$NON-NLS-1$ //$NON-NLS-2$
		return target.toString();
	}

	/* (non-Javadoc)
	 * @see org.eclipse.ecf.discovery.IContainerServiceInfoAdapter#setContainerProperties(java.lang.String, java.lang.String, java.lang.String, java.lang.Boolean)
	 */
	public void setContainerProperties(String containerFactoryName, String connectProtocol, String connectPath, Boolean connectRequiresPassword) {
		Assert.isNotNull(containerFactoryName);
		properties.setPropertyString(IDiscoveryContainerAdapter.CONTAINER_FACTORY_NAME_PROPERTY, containerFactoryName);
		Assert.isNotNull(connectProtocol);
		properties.setPropertyString(IDiscoveryContainerAdapter.CONTAINER_CONNECT_TARGET_PROTOCOL, connectProtocol);
		if (connectPath != null)
			properties.setPropertyString(IDiscoveryContainerAdapter.CONTAINER_CONNECT_TARGET_PATH, connectPath);
		if (connectRequiresPassword != null)
			properties.setPropertyString(IDiscoveryContainerAdapter.CONTAINER_CONNECT_REQUIRES_PASSWORD, connectRequiresPassword.toString());
	}

	/* (non-Javadoc)
	 * @see org.eclipse.ecf.discovery.IContainerServiceInfoAdapter#connectRequiresPassword()
	 */
	public Boolean connectRequiresPassword() {
		String b = properties.getPropertyString(IDiscoveryContainerAdapter.CONTAINER_CONNECT_REQUIRES_PASSWORD);
		if (b == null)
			return null;
		return Boolean.valueOf(b);
	}

	/* (non-Javadoc)
	 * @see org.eclipse.ecf.discovery.IContainerServiceInfoAdapter#setContainerProperties(java.lang.String, java.lang.String, java.lang.Boolean)
	 */
	public void setContainerProperties(String containerFactoryName, String connectTarget, Boolean connectRequiresPassword) {
		Assert.isNotNull(containerFactoryName);
		properties.setPropertyString(IDiscoveryContainerAdapter.CONTAINER_FACTORY_NAME_PROPERTY, containerFactoryName);
		Assert.isNotNull(connectTarget);
		properties.setPropertyString(IDiscoveryContainerAdapter.CONTAINER_CONNECT_TARGET, connectTarget);
		if (connectRequiresPassword != null)
			properties.setPropertyString(IDiscoveryContainerAdapter.CONTAINER_CONNECT_REQUIRES_PASSWORD, connectRequiresPassword.toString());
	}
}

Back to the top