Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: bc80b1bfdab6ce81de6118c342598e1c7fbdc145 (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
/*******************************************************************************
 * Copyright (c) 2003, 2017 IBM Corporation 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:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/

package org.eclipse.osgi.internal.serviceregistry;

import java.lang.reflect.Array;
import java.util.*;
import org.eclipse.osgi.framework.util.Headers;
import org.osgi.framework.Constants;

/**
 * Hashtable for service properties.
 * 
 * Supports case-insensitive key lookup.
 */
class ServiceProperties extends Headers<String, Object> {
	/**
	 * Create a properties object for the service.
	 *
	 * @param props The properties for this service.
	 */
	private ServiceProperties(int size, Dictionary<String, ?> props) {
		super(size);

		if (props == null) {
			return;
		}
		synchronized (props) {
			Enumeration<?> keysEnum = props.keys();

			while (keysEnum.hasMoreElements()) {
				Object key = keysEnum.nextElement();

				if (key instanceof String) {
					String header = (String) key;

					setProperty(header, props.get(header));
				}
			}
		}
	}

	/**
	 * Create a properties object for the service.
	 *
	 * @param props The properties for this service.
	 */
	ServiceProperties(Dictionary<String, ?> props) {
		this((props == null) ? 2 : props.size() + 2, props);
	}

	/**
	 * Get a clone of the value of a service's property.
	 *
	 * @param key header name.
	 * @return Clone of the value of the property or <code>null</code> if there is
	 * no property by that name.
	 */
	Object getProperty(String key) {
		return cloneValue(get(key));
	}

	/**
	 * Get the list of key names for the service's properties.
	 *
	 * @return The list of property key names.
	 */
	synchronized String[] getPropertyKeys() {
		int size = size();

		String[] keynames = new String[size];

		Enumeration<String> keysEnum = keys();

		for (int i = 0; i < size; i++) {
			keynames[i] = keysEnum.nextElement();
		}

		return keynames;
	}

	/**
	 * Put a clone of the property value into this property object.
	 *
	 * @param key Name of property.
	 * @param value Value of property.
	 * @return previous property value.
	 */
	synchronized Object setProperty(String key, Object value) {
		return set(key, cloneValue(value));
	}

	/**
	 * Attempt to clone the value if necessary and possible.
	 *
	 * For some strange reason, you can test to see of an Object is
	 * Cloneable but you can't call the clone method since it is
	 * protected on Object!
	 *
	 * @param value object to be cloned.
	 * @return cloned object or original object if we didn't clone it.
	 */
	private static Object cloneValue(Object value) {
		if (value == null)
			return null;
		if (value instanceof String) /* shortcut String */
			return value;
		if (value instanceof Number) /* shortcut Number */
			return value;
		if (value instanceof Character) /* shortcut Character */
			return value;
		if (value instanceof Boolean) /* shortcut Boolean */
			return value;

		Class<?> clazz = value.getClass();
		if (clazz.isArray()) {
			// Do an array copy
			Class<?> type = clazz.getComponentType();
			int len = Array.getLength(value);
			Object clonedArray = Array.newInstance(type, len);
			System.arraycopy(value, 0, clonedArray, 0, len);
			return clonedArray;
		}
		// must use reflection because Object clone method is protected!!
		try {
			return clazz.getMethod("clone", (Class<?>[]) null).invoke(value, (Object[]) null); //$NON-NLS-1$
		} catch (Exception e) {
			/* clone is not a public method on value's class */
		} catch (Error e) {
			/* JCL does not support reflection; try some well known types */
			if (value instanceof Vector<?>)
				return ((Vector<?>) value).clone();
			if (value instanceof Hashtable<?, ?>)
				return ((Hashtable<?, ?>) value).clone();
		}
		return value;
	}

	public synchronized String toString() {
		String keys[] = getPropertyKeys();

		int size = keys.length;

		StringBuilder sb = new StringBuilder(20 * size);

		sb.append('{');

		int n = 0;
		for (int i = 0; i < size; i++) {
			String key = keys[i];
			if (!key.equals(Constants.OBJECTCLASS)) {
				if (n > 0)
					sb.append(", "); //$NON-NLS-1$

				sb.append(key);
				sb.append('=');
				Object value = get(key);
				if (value.getClass().isArray()) {
					sb.append('[');
					int length = Array.getLength(value);
					for (int j = 0; j < length; j++) {
						if (j > 0)
							sb.append(',');
						sb.append(Array.get(value, j));
					}
					sb.append(']');
				} else {
					sb.append(value);
				}
				n++;
			}
		}

		sb.append('}');

		return sb.toString();
	}
}

Back to the top