Debian Codegraph @

bundles/org.eclipse.equinox.p2.engine/src/org/eclipse/equinox/internal/p2/engine/DebugHelper.java

raw
1 /*******************************************************************************
2 * Copyright (c) 2009, 2015 IBM Corporation and others.
3 *
4 * This program and the accompanying materials
5 * are made available under the terms of the Eclipse Public License 2.0
6 * which accompanies this distribution, and is available at
7 * https://www.eclipse.org/legal/epl-2.0/
8 *
9 * SPDX-License-Identifier: EPL-2.0
10 *
11 * Contributors:
12 * IBM Corporation - initial API and implementation
13 * Red Hat Inc. - Bug 460967
14 *******************************************************************************/
15 package org.eclipse.equinox.internal.p2.engine;
16
17 import java.io.File;
18 import java.util.*;
19 import java.util.Map.Entry;
20 import org.eclipse.equinox.internal.p2.core.helpers.ServiceHelper;
21 import org.eclipse.equinox.p2.engine.IProfile;
22 import org.eclipse.equinox.p2.engine.ProvisioningContext;
23 import org.eclipse.equinox.p2.engine.spi.ProvisioningAction;
24 import org.eclipse.equinox.p2.metadata.IArtifactKey;
25 import org.eclipse.equinox.p2.metadata.IInstallableUnit;
26 import org.eclipse.osgi.service.debug.DebugOptions;
27
28 public class DebugHelper {
29 public static final String LINE_SEPARATOR = System.lineSeparator();
30
31 public static final boolean DEBUG_PROFILE_REGISTRY;
32 public static final boolean DEBUG_ENGINE;
33 public static final boolean DEBUG_ENGINE_SESSION;
34 public static final boolean DEBUG_CERTIFICATE_CHECKER_UNSIGNED;
35 public static final boolean DEBUG_CERTIFICATE_CHECKER_UNTRUSTED;
36
37 static {
38 DebugOptions options = ServiceHelper.getService(EngineActivator.getContext(), DebugOptions.class);
39 if (options != null) {
40 DEBUG_PROFILE_REGISTRY = options.getBooleanOption(EngineActivator.ID + "/profileregistry/debug", false); //$NON-NLS-1$
41 DEBUG_ENGINE = options.getBooleanOption(EngineActivator.ID + "/engine/debug", false); //$NON-NLS-1$
42 DEBUG_ENGINE_SESSION = options.getBooleanOption(EngineActivator.ID + "/enginesession/debug", false); //$NON-NLS-1$
43 DEBUG_CERTIFICATE_CHECKER_UNSIGNED = options.getBooleanOption(EngineActivator.ID + "/certificatechecker/unsigned", false); //$NON-NLS-1$
44 DEBUG_CERTIFICATE_CHECKER_UNTRUSTED = options.getBooleanOption(EngineActivator.ID + "/certificatechecker/untrusted", false); //$NON-NLS-1$
45 } else {
46 DEBUG_PROFILE_REGISTRY = false;
47 DEBUG_ENGINE = false;
48 DEBUG_ENGINE_SESSION = false;
49 DEBUG_CERTIFICATE_CHECKER_UNSIGNED = false;
50 DEBUG_CERTIFICATE_CHECKER_UNTRUSTED = false;
51 }
52 }
53
54 public static void debug(String name, String message) {
55 StringBuilder buffer = new StringBuilder();
56 buffer.append("["); //$NON-NLS-1$
57 buffer.append(EngineActivator.ID + "-" + name); //$NON-NLS-1$
58 buffer.append("] "); //$NON-NLS-1$
59 buffer.append(new Date(System.currentTimeMillis()));
60 buffer.append(" - ["); //$NON-NLS-1$
61 buffer.append(Thread.currentThread().getName());
62 buffer.append("] " + LINE_SEPARATOR); //$NON-NLS-1$
63 buffer.append(message);
64 System.out.println(buffer.toString());
65 }
66
67 public static String formatArray(Collection<? extends Object> array, boolean toString, boolean newLines) {
68 if (array == null || array.size() == 0)
69 return "[]"; //$NON-NLS-1$
70
71 StringBuilder buffer = new StringBuilder();
72 buffer.append('[');
73 boolean first = true;
74 for (Object elem : array) {
75 if (first)
76 first = false;
77 else
78 buffer.append(',');
79
80 if (toString)
81 buffer.append(elem.toString());
82 else
83 buffer.append(elem.getClass().getName());
84 if (newLines)
85 buffer.append(DebugHelper.LINE_SEPARATOR);
86 else
87 buffer.append(' ');
88 }
89 buffer.append(']');
90 return buffer.toString();
91 }
92
93 public static String formatOperation(PhaseSet phaseSet, Operand[] operands, ProvisioningContext context) {
94 StringBuilder buffer = new StringBuilder();
95 buffer.append("phaseSet=" + formatPhaseSet(phaseSet)); //$NON-NLS-1$
96 buffer.append(","); //$NON-NLS-1$
97 buffer.append(DebugHelper.LINE_SEPARATOR);
98 buffer.append("operands=" + formatOperands(operands)); //$NON-NLS-1$
99 buffer.append(","); //$NON-NLS-1$
100 buffer.append(DebugHelper.LINE_SEPARATOR);
101 buffer.append("context=" + formatContext(context)); //$NON-NLS-1$
102 return buffer.toString();
103 }
104
105 public static String formatOperands(Operand[] operands) {
106 String[] operandStrings = new String[operands.length];
107 for (int i = 0; i < operands.length; i++) {
108 if (operands[i] instanceof InstallableUnitOperand) {
109 InstallableUnitOperand iuOperand = (InstallableUnitOperand) operands[i];
110 operandStrings[i] = formatInstallableUnitOperand(iuOperand);
111 } else {
112 operandStrings[i] = operands[i].toString();
113 }
114 }
115 return DebugHelper.formatArray(Arrays.asList(operandStrings), true, true);
116 }
117
118 public static String formatInstallableUnitOperand(InstallableUnitOperand iuOperand) {
119 StringBuilder buffer = new StringBuilder();
120 buffer.append(iuOperand.first());
121 if (iuOperand.first() != null && iuOperand.first().getFragments() != null)
122 buffer.append(DebugHelper.formatArray(iuOperand.first().getFragments(), true, false));
123 buffer.append(" --> "); //$NON-NLS-1$
124 buffer.append(iuOperand.second());
125 if (iuOperand.second() != null && iuOperand.second().getFragments() != null)
126 buffer.append(DebugHelper.formatArray(iuOperand.second().getFragments(), true, false));
127 return buffer.toString();
128 }
129
130 public static String formatPhaseSet(PhaseSet phaseSet) {
131 StringBuilder buffer = new StringBuilder(phaseSet.getClass().getName());
132 buffer.append(DebugHelper.formatArray(Arrays.asList(phaseSet.getPhases()), false, false));
133 return buffer.toString();
134 }
135
136 public static String formatContext(ProvisioningContext context) {
137 return context.toString();
138 }
139
140 public static String formatAction(ProvisioningAction action, Map<String, Object> parameters) {
141 StringBuilder buffer = new StringBuilder();
142 buffer.append(action.getClass().getName());
143 if (action instanceof ParameterizedProvisioningAction) {
144 ParameterizedProvisioningAction parameterizedAction = (ParameterizedProvisioningAction) action;
145 buffer.append("{action=" + parameterizedAction.getAction().getClass().getName()); //$NON-NLS-1$
146 buffer.append(", actionText=" + parameterizedAction.getActionText() + "}"); //$NON-NLS-1$ //$NON-NLS-2$
147 }
148 buffer.append(DebugHelper.LINE_SEPARATOR);
149 buffer.append("parameters=" + formatParameters(parameters)); //$NON-NLS-1$
150 return buffer.toString();
151 }
152
153 public static String formatParameters(Map<String, ? extends Object> parameters) {
154 Iterator<? extends Entry<String, ? extends Object>> it = parameters.entrySet().iterator();
155 if (!it.hasNext())
156 return "{}"; //$NON-NLS-1$
157
158 StringBuilder buffer = new StringBuilder();
159 buffer.append('{');
160 for (;;) {
161 Entry<String, ? extends Object> e = it.next();
162 String key = e.getKey();
163 buffer.append(key);
164 buffer.append('=');
165 Object value = e.getValue();
166 if (value == null)
167 buffer.append(value);
168 else if (value instanceof String || value instanceof File || value instanceof Operand || value instanceof IArtifactKey || value instanceof IInstallableUnit)
169 buffer.append(value);
170 else if (value instanceof IProfile)
171 buffer.append(((IProfile) value).getProfileId());
172 else
173 buffer.append(value.getClass().getName());
174 if (!it.hasNext()) {
175 buffer.append('}');
176 break;
177 }
178 buffer.append(',');
179 buffer.append(DebugHelper.LINE_SEPARATOR);
180 buffer.append(' ');
181 }
182 return buffer.toString();
183 }
184 }