KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > core > runtime > internal > adaptor > EclipseCommandProvider


1 /*******************************************************************************
2  * Copyright (c) 2004, 2007 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.core.runtime.internal.adaptor;
12
13 import org.eclipse.osgi.framework.console.CommandInterpreter;
14 import org.eclipse.osgi.framework.console.CommandProvider;
15 import org.eclipse.osgi.service.resolver.*;
16 import org.eclipse.osgi.util.NLS;
17 import org.osgi.framework.BundleContext;
18 import org.osgi.framework.ServiceReference;
19
20 /**
21  * Internal class.
22  */

23 public class EclipseCommandProvider implements CommandProvider {
24     private BundleContext context;
25
26     public EclipseCommandProvider(BundleContext context) {
27         this.context = context;
28     }
29
30     public String JavaDoc getHelp() {
31         StringBuffer JavaDoc help = new StringBuffer JavaDoc(512);
32         help.append(EclipseAdaptorMsg.NEW_LINE);
33         help.append("---"); //$NON-NLS-1$
34
help.append(EclipseAdaptorMsg.ECLIPSE_CONSOLE_COMMANDS_HEADER);
35         help.append("---"); //$NON-NLS-1$
36
help.append(EclipseAdaptorMsg.NEW_LINE);
37         help.append("\tdiag - " + EclipseAdaptorMsg.ECLIPSE_CONSOLE_HELP_DIAG_COMMAND_DESCRIPTION);//$NON-NLS-1$
38
return help.toString();
39     }
40
41     private BundleDescription getBundleDescriptionFromToken(State state, String JavaDoc token) {
42         try {
43             long id = Long.parseLong(token);
44             return state.getBundle(id);
45         } catch (NumberFormatException JavaDoc nfe) {
46             BundleDescription[] allBundles = state.getBundles(token);
47             if (allBundles.length > 0)
48                 return allBundles[0];
49         }
50         return null;
51     }
52
53     public void _diag(CommandInterpreter ci) throws Exception JavaDoc {
54         String JavaDoc nextArg = ci.nextArgument();
55         if (nextArg == null) {
56             ci.println(EclipseAdaptorMsg.ECLIPSE_CONSOLE_NO_BUNDLE_SPECIFIED_ERROR);
57             return;
58         }
59         ServiceReference platformAdminRef = context.getServiceReference(PlatformAdmin.class.getName());
60         if (platformAdminRef == null) {
61             ci.print(" "); //$NON-NLS-1$
62
ci.println(EclipseAdaptorMsg.ECLIPSE_CONSOLE_NO_CONSTRAINTS_NO_PLATFORM_ADMIN_MESSAGE);
63             return;
64         }
65         try {
66             PlatformAdmin platformAdmin = (PlatformAdmin) context.getService(platformAdminRef);
67             if (platformAdmin == null)
68                 return;
69             State systemState = platformAdmin.getState(false);
70             while (nextArg != null) {
71                 BundleDescription bundle = getBundleDescriptionFromToken(systemState, nextArg);
72                 if (bundle == null) {
73                     ci.println(NLS.bind(EclipseAdaptorMsg.ECLIPSE_CONSOLE_CANNOT_FIND_BUNDLE_ERROR, nextArg));
74                     nextArg = ci.nextArgument();
75                     continue;
76                 }
77                 ci.println(bundle.getLocation() + " [" + bundle.getBundleId() + "]"); //$NON-NLS-1$ //$NON-NLS-2$
78
VersionConstraint[] unsatisfied = platformAdmin.getStateHelper().getUnsatisfiedConstraints(bundle);
79                 ResolverError[] resolverErrors = platformAdmin.getState(false).getResolverErrors(bundle);
80                 for (int i = 0; i < resolverErrors.length; i++) {
81                     if ((resolverErrors[i].getType() & (ResolverError.MISSING_FRAGMENT_HOST | ResolverError.MISSING_GENERIC_CAPABILITY | ResolverError.MISSING_IMPORT_PACKAGE | ResolverError.MISSING_REQUIRE_BUNDLE)) != 0)
82                         continue;
83                     ci.print(" "); //$NON-NLS-1$
84
ci.println(resolverErrors[i].toString());
85                 }
86
87                 for (int i = 0; i < unsatisfied.length; i++) {
88                     ci.print(" "); //$NON-NLS-1$
89
ci.println(MessageHelper.getResolutionFailureMessage(unsatisfied[i]));
90                 }
91                 if (unsatisfied.length == 0 && resolverErrors.length == 0) {
92                     ci.print(" "); //$NON-NLS-1$
93
ci.println(EclipseAdaptorMsg.ECLIPSE_CONSOLE_NO_CONSTRAINTS);
94                 }
95                 nextArg = ci.nextArgument();
96             }
97         } finally {
98             context.ungetService(platformAdminRef);
99         }
100     }
101 }
102
Popular Tags