Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: b73526af8e0e990da815e3efc2175d9fc085ca34 (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
/****************************************************************************
 * 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.provider.jmdns.identity;

import java.net.URI;
import java.net.URISyntaxException;
import org.eclipse.ecf.core.identity.*;
import org.eclipse.ecf.discovery.identity.IServiceTypeID;

public class JMDNSNamespace extends Namespace {

	private static final long serialVersionUID = -7220857203720337921L;

	public static final String JMDNS_SCHEME = "jmdns"; //$NON-NLS-1$

	public static final String NAME = "ecf.namespace.jmdns"; //$NON-NLS-1$

	public JMDNSNamespace() {
		super();
	}

	/**
	 * @param description description for namespace
	 * @since 4.3
	 */
	public JMDNSNamespace(String description) {
		super(NAME, description);
	}

	private String getInitFromExternalForm(final Object[] args) {
		if (args == null || args.length < 1 || args[0] == null)
			return null;
		if (args[0] instanceof String) {
			final String arg = (String) args[0];
			if (arg.startsWith(getScheme() + Namespace.SCHEME_SEPARATOR)) {
				final int index = arg.indexOf(Namespace.SCHEME_SEPARATOR);
				if (index >= arg.length())
					return null;
				return arg.substring(index + 1);
			}
		}
		return null;
	}

	/* (non-Javadoc)
	 * @see org.eclipse.ecf.core.identity.Namespace#createInstance(java.lang.Object[])
	*/
	public ID createInstance(final Object[] parameters) {
		String type = null;
		try {
			final String init = getInitFromExternalForm(parameters);
			if (init != null)
				type = init;
			else {
				if (parameters == null || parameters.length < 1 || parameters.length > 2) {
					throw new IDCreateException("Parameters cannot be null and must be of length 1 or 2"); //$NON-NLS-1$
				}
				if (parameters[0] instanceof IServiceTypeID) {
					type = ((IServiceTypeID) parameters[0]).getName();
				} else if (parameters[0] instanceof String) {
					type = (String) parameters[0];
				} else
					throw new IDCreateException("Service type id parameter has to be of type String or IServiceTypeID"); //$NON-NLS-1$
			}
		} catch (final Exception e) {
			throw new IDCreateException(getName() + " createInstance()", e); //$NON-NLS-1$
		}
		final JMDNSServiceTypeID stid = new JMDNSServiceTypeID(this, type);
		if (parameters.length == 1) {
			return stid;
		} else if (parameters[1] instanceof String) {
			try {
				final URI uri = new URI((String) parameters[1]);
				return new JMDNSServiceID(this, stid, uri);
			} catch (URISyntaxException e) {
				throw new IDCreateException("Second parameter as String must follow URI syntax"); //$NON-NLS-1$
			}
		} else if (parameters[1] instanceof URI) {
			return new JMDNSServiceID(this, stid, (URI) parameters[1]);
		} else {
			throw new IDCreateException("Second parameter must be of either String or URI type"); //$NON-NLS-1$
		}
	}

	/* (non-Javadoc)
	 * @see org.eclipse.ecf.core.identity.Namespace#getScheme()
	 */
	public String getScheme() {
		return JMDNS_SCHEME;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.ecf.core.identity.Namespace#getSupportedParameterTypesForCreateInstance()
	 */
	public Class[][] getSupportedParameterTypes() {
		return new Class[][] {{String.class}, {String.class, String.class}, {IServiceTypeID.class}, {String.class, URI.class}};
	}
}

Back to the top