Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 7530934d6e174c593880949dbb8724a3e63ba34c (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
/*******************************************************************************
 * Copyright (c) 2018 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.console;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Formatter;
import java.util.List;

import org.apache.felix.service.command.Converter;
import org.eclipse.ecf.core.ContainerTypeDescription;
import org.eclipse.ecf.core.IContainer;
import org.eclipse.ecf.core.IContainerManager;
import org.eclipse.ecf.core.identity.ID;
import org.eclipse.ecf.core.identity.IIDFactory;
import org.eclipse.ecf.core.identity.Namespace;

public abstract class AbstractCommand {

	protected abstract IContainerManager getContainerManager();

	protected abstract IIDFactory getIDFactory();

	protected List<IContainer> getContainers() {
		return Arrays.asList(getContainerManager().getAllContainers().clone());
	}

	protected List<Namespace> getNamespaces() {
		return new ArrayList<Namespace>(getIDFactory().getNamespaces());
	}

	@SuppressWarnings("unchecked")
	protected List<ContainerTypeDescription> getConfigs() {
		return new ArrayList<ContainerTypeDescription>(getContainerManager().getContainerFactory().getDescriptions());

	}

	protected ContainerTypeDescription getContainerTypeDescription(ID containerID) {
		return getContainerManager().getContainerTypeDescription(containerID);
	}

	protected IContainer getContainerForId(String id) {
		for (IContainer c : getContainerManager().getAllContainers().clone())
			if (c.getID().getName().equals(id))
				return c;
		return null;
	}

	protected String printClassArrays(Class<?>[][] types) {
		if (types == null)
			return ""; //$NON-NLS-1$
		StringBuffer sb = new StringBuffer();
		for (int i = 0; i < types.length; i++) {
			Class<?>[] paramTypes = types[i];
			sb.append("<init>").append("("); //$NON-NLS-1$ //$NON-NLS-2$
			for (int j = 0; j < paramTypes.length; j++) {
				Class<?> pType = paramTypes[j];
				sb.append(pType.getName());
				if (j + 1 < paramTypes.length)
					sb.append(","); //$NON-NLS-1$
			}
			sb.append(")"); //$NON-NLS-1$
			if (i + 1 < types.length)
				sb.append(";"); //$NON-NLS-1$
		}
		return sb.toString();
	}

	protected String formatContainer(IContainer c, int level, Converter escape) {
		ID cID = c.getID();
		ID conID = c.getConnectedID();
		String conIDStr = (conID == null) ? "" : conID.getName(); //$NON-NLS-1$
		Class<?> cClass = c.getClass();
		switch (level) {
		case Converter.LINE:
			try (Formatter f = new Formatter();) {
				f.format("%s        %s        %s", cID.getName(), cClass.getName(), conIDStr); //$NON-NLS-1$
				return f.toString();
			}
		case Converter.INSPECT:
			try (Formatter f = new Formatter();) {
				f.format("ID%s\n" + "\tIDNamespace=%s\n" + "\tImplClass=%s\n" + "\tConnectedTo=%s\n" //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
						+ "\tConnectNamespace=%s\n" + "\tConfig=%s", //$NON-NLS-1$ //$NON-NLS-2$
						cID.getName(), cID.getNamespace().getName(), cClass.getName(), conIDStr,
						c.getConnectNamespace().getName(), getContainerTypeDescription(cID).getName());
				return f.toString();
			}
		default:
			return null;
		}
	}

	protected String formatNamespace(Namespace ns, int level, Converter escape) {
		switch (level) {
		case Converter.PART:
			return null;
		case Converter.LINE:
			try (Formatter f = new Formatter();) {
				f.format("%s", ns.getName()); //$NON-NLS-1$
				return f.toString();
			}
		case Converter.INSPECT:
			try (Formatter f = new Formatter();) {
				f.format("ID=%s\n" + "\tScheme=%s\n" + "\tImplClass=%s\n" + "\tDescription=%s\n" //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
						+ "\tFactoryConstructors=%s\n" + "\tSupportedSchemes=%s", //$NON-NLS-1$ //$NON-NLS-2$
						ns.getName(), ns.getScheme(), ns.getClass().getName(), ns.getDescription(),
						printClassArrays(ns.getSupportedParameterTypes()), printStringArray(ns.getSupportedSchemes()));
				return f.toString();
			}
		default:
			return null;
		}
	}

	protected String formatConfig(ContainerTypeDescription ctd, int level, Converter escape) {
		switch (level) {
		case Converter.PART:
			return null;
		case Converter.LINE:
			try (Formatter f = new Formatter();) {
				f.format("%s", ctd.getName()); //$NON-NLS-1$
				return f.toString();
			}
		case Converter.INSPECT:
			try (Formatter f = new Formatter();) {
				f.format("ID=%s\n" + "\tDescription=%s\n" + "\tSupportedConfigs=%s\n" + "\tSupportedIntents=%s\n" //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
						+ "\tFactoryConstructors=%s\n" + "\tAdapterTypes=%s\n" + "\tHidden=%b\n" + "\tServer=%b", //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
						ctd.getName(), ctd.getDescription(), printStringArray(ctd.getSupportedConfigs()),
						printStringArray(ctd.getSupportedIntents()), printClassArrays(ctd.getSupportedParameterTypes()),
						printStringArray(ctd.getSupportedAdapterTypes()), ctd.isHidden(), ctd.isServer());
				return f.toString();
			}
		default:
			return null;
		}
	}

	protected String printStringArray(String[] strarr) {
		return (strarr == null) ? "" : Arrays.asList(strarr).toString(); //$NON-NLS-1$
	}

	public Object convert(Class<?> desiredType, Object in) throws Exception {
		if (desiredType == IContainer.class && in instanceof String)
			return getContainerForId((String) in);
		else if (desiredType == Namespace.class && in instanceof String)
			return getIDFactory().getNamespaceByName((String) in);
		else if (desiredType == ContainerTypeDescription.class)
			return getContainerManager().getContainerFactory().getDescriptionByName((String) in);
		return null;
	}

	public String format(Object target, int level, Converter escape) {
		if (target instanceof IContainer)
			return formatContainer((IContainer) target, level, escape);
		else if (target instanceof Namespace)
			return formatNamespace((Namespace) target, level, escape);
		else if (target instanceof ContainerTypeDescription)
			return formatConfig((ContainerTypeDescription) target, level, escape);
		return null;
	}

}

Back to the top