KickJava   Java API By Example, From Geeks To Geeks.

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


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

11 package org.eclipse.core.runtime.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.osgi.framework.*;
17
18 /**
19  * Internal class.
20  */

21 public class EclipseCommandProvider implements CommandProvider {
22     private BundleContext context;
23
24     public EclipseCommandProvider(BundleContext context) {
25         this.context = context;
26     }
27
28     public String JavaDoc getHelp() {
29         StringBuffer JavaDoc help = new StringBuffer JavaDoc(512);
30         help.append(EclipseAdaptorMsg.NEW_LINE);
31         help.append("---"); //$NON-NLS-1$
32
help.append(EclipseAdaptorMsg.formatter.getString("ECLIPSE_CONSOLE_COMMANDS_HEADER"));//$NON-NLS-1$
33
help.append("---"); //$NON-NLS-1$
34
help.append(EclipseAdaptorMsg.NEW_LINE);
35         help.append("\tdiag - " + EclipseAdaptorMsg.formatter.getString("ECLIPSE_CONSOLE_HELP_DIAG_COMMAND_DESCRIPTION"));//$NON-NLS-1$ //$NON-NLS-2$
36
help.append(EclipseAdaptorMsg.NEW_LINE);
37         help.append("\tactive - " + EclipseAdaptorMsg.formatter.getString("ECLIPSE_CONSOLE_HELP_ACTIVE_COMMAND_DESCRIPTION"));//$NON-NLS-1$ //$NON-NLS-2$
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.formatter.getString("ECLIPSE_CONSOLE_NO_BUNDLE_SPECIFIED_ERROR"));//$NON-NLS-1$
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.formatter.getString("ECLIPSE_CONSOLE_NO_CONSTRAINTS_NO_PLATFORM_ADMIN_MESSAGE"));//$NON-NLS-1$
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(EclipseAdaptorMsg.formatter.getString("ECLIPSE_CONSOLE_CANNOT_FIND_BUNDLE_ERROR", nextArg));//$NON-NLS-1$
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                 if (unsatisfied.length == 0) {
80                     // init default message
81
String JavaDoc message = EclipseAdaptorMsg.formatter.getString("ECLIPSE_CONSOLE_NO_CONSTRAINTS"); //$NON-NLS-1$
82
if (!bundle.isResolved()) {
83                         // another version might have been picked
84
String JavaDoc symbolicName = bundle.getSymbolicName();
85                         BundleDescription resolved = symbolicName == null ? null : getResolvedBundle(systemState, symbolicName);
86                         if (resolved != null)
87                             message = EclipseAdaptorMsg.formatter.getString("ECLIPSE_CONSOLE_OTHER_VERSION", resolved.getLocation()); //$NON-NLS-1$
88
}
89                     ci.print(" "); //$NON-NLS-1$
90
ci.println(message);//$NON-NLS-1$
91
}
92                 for (int i = 0; i < unsatisfied.length; i++) {
93                     ci.print(" "); //$NON-NLS-1$
94
ci.println(EclipseAdaptorMsg.getResolutionFailureMessage(unsatisfied[i]));
95                 }
96                 nextArg = ci.nextArgument();
97             }
98         } finally {
99             context.ungetService(platformAdminRef);
100         }
101     }
102
103     private BundleDescription getResolvedBundle(State state, String JavaDoc symbolicName) {
104         BundleDescription[] homonyms = state.getBundles(symbolicName);
105         for (int i = 0; i < homonyms.length; i++)
106             if (homonyms[i].isResolved())
107                 return homonyms[i];
108         return null;
109     }
110
111     public void _active(CommandInterpreter ci) throws Exception JavaDoc {
112         Bundle[] allBundles = context.getBundles();
113         int activeCount = 0;
114         for (int i = 0; i < allBundles.length; i++)
115             if (allBundles[i].getState() == Bundle.ACTIVE) {
116                 ci.println(allBundles[i]);
117                 activeCount++;
118             }
119         ci.print(" "); //$NON-NLS-1$
120
ci.println(EclipseAdaptorMsg.formatter.getString("ECLIPSE_CONSOLE_BUNDLES_ACTIVE", activeCount)); //$NON-NLS-1$
121
}
122 }
Popular Tags