Skip to main content



      Home
Home » Eclipse Projects » Eclipse Platform » How does my plugin know if it's running headless?
How does my plugin know if it's running headless? [message #318205] Mon, 23 July 2007 12:36 Go to next message
Eclipse UserFriend
Hello,

I'm writing a plugin that can be used in both headless applications as
well as Eclipse with full UI; at some places (e.g. user interaction) it
should behave differently when running headless or running with UI.

Is there a commonly accepted technique for knowing whether the current
application is headless or not?

What I'm currently trying is this:

Bundle swtBundle = null;
BundleContext ctx =
Activator.getDefault().getBundle().getBundleContext();
Bundle[] bundles = ctx.getBundles();
for (int i=0; i<bundles.length; i++) {
if ("org.eclipse.swt".equals(bundles[i].getSymbolicName())) {
if (swtBundle==null || bundles[i].getState()==Bundle.ACTIVE) {
swtBundle = bundles[i];
}
}
}
if (swtBundle==null) {
System.out.println("HEADLESS detected!");
}


Does that make sense?

Thanks,
--
Martin Oberhuber
Target Management Project Lead, DSDP PMC Member
http://www.eclipse.org/dsdp/tm
Re: How does my plugin know if it's running headless? [message #318225 is a reply to message #318205] Mon, 23 July 2007 14:12 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: mark_melvin.amis.com

I usually just do this:

if (PlatformUI.isWorkbenchRunning()) {
.
.
} else {
.
.
}

Although I'm assuming you are aware of this method and do not want a =

dependency on PlatformUI?

Mark.

On Mon, 23 Jul 2007 12:36:29 -0400, Martin Oberhuber =

<martin.oberhuber@windriver.com> wrote:

> Hello,
>
> I'm writing a plugin that can be used in both headless applications as=
=

> well as Eclipse with full UI; at some places (e.g. user interaction) i=
t =

> should behave differently when running headless or running with UI.
>
> Is there a commonly accepted technique for knowing whether the current=
=

> application is headless or not?
>
> What I'm currently trying is this:
>
> Bundle swtBundle =3D null;
> BundleContext ctx =3D
> Activator.getDefault().getBundle().getBundleContext();
> Bundle[] bundles =3D ctx.getBundles();
> for (int i=3D0; i<bundles.length; i++) {
> if ("org.eclipse.swt".equals(bundles[i].getSymbolicName())) {
> if (swtBundle=3D=3Dnull || bundles[i].getState()=3D=3DBundle.ACTI=
VE) {
> swtBundle =3D bundles[i];
> }
> }
> }
> if (swtBundle=3D=3Dnull) {
> System.out.println("HEADLESS detected!");
> }
>
>
> Does that make sense?
>
> Thanks,
> --
> Martin Oberhuber
> Target Management Project Lead, DSDP PMC Member
> http://www.eclipse.org/dsdp/tm
Re: How does my plugin know if it's running headless? [message #318271 is a reply to message #318225] Tue, 24 July 2007 07:54 Go to previous messageGo to next message
Eclipse UserFriend
> Although I'm assuming you are aware of this method and do not want a
> dependency on PlatformUI?

Yes I'd like to avoid starting PlatformUI just
for checking whether the Workbench is running...

Thanks,
--
Martin Oberhuber
Target Management Project Lead, DSDP PMC Member
http://www.eclipse.org/dsdp/tm


Mark Melvin wrote:
> I usually just do this:
>
> if (PlatformUI.isWorkbenchRunning()) {
> .
> .
> } else {
> .
> .
> }
>
> Although I'm assuming you are aware of this method and do not want a
> dependency on PlatformUI?
>
> Mark.
>
> On Mon, 23 Jul 2007 12:36:29 -0400, Martin Oberhuber
> <martin.oberhuber@windriver.com> wrote:
>
>> Hello,
>>
>> I'm writing a plugin that can be used in both headless applications as
>> well as Eclipse with full UI; at some places (e.g. user interaction)
>> it should behave differently when running headless or running with UI.
>>
>> Is there a commonly accepted technique for knowing whether the current
>> application is headless or not?
>>
>> What I'm currently trying is this:
>>
>> Bundle swtBundle = null;
>> BundleContext ctx =
>> Activator.getDefault().getBundle().getBundleContext();
>> Bundle[] bundles = ctx.getBundles();
>> for (int i=0; i<bundles.length; i++) {
>> if ("org.eclipse.swt".equals(bundles[i].getSymbolicName())) {
>> if (swtBundle==null || bundles[i].getState()==Bundle.ACTIVE) {
>> swtBundle = bundles[i];
>> }
>> }
>> }
>> if (swtBundle==null) {
>> System.out.println("HEADLESS detected!");
>> }
>>
>>
>> Does that make sense?
>>
>> Thanks,
>> --
>> Martin Oberhuber
>> Target Management Project Lead, DSDP PMC Member
>> http://www.eclipse.org/dsdp/tm
Re: How does my plugin know if it's running headless? [message #318272 is a reply to message #318205] Tue, 24 July 2007 09:14 Go to previous messageGo to next message
Eclipse UserFriend
Martin Oberhuber wrote:
> Hello,
>
> I'm writing a plugin that can be used in both headless applications as
> well as Eclipse with full UI; at some places (e.g. user interaction) it
> should behave differently when running headless or running with UI.
>
> Is there a commonly accepted technique for knowing whether the current
> application is headless or not?


I'm not sure if there is a better way to find out, but if you use
core.runtime there is possibly a faster way:

org.eclipse.core.runtime.Platform.getBundle("org.eclipse.swt ")

I think it uses the PackageAdmin service to find the named bundle.

Later,
PW
Re: How does my plugin know if it's running headless? [message #318284 is a reply to message #318272] Tue, 24 July 2007 12:32 Go to previous messageGo to next message
Eclipse UserFriend
Ah that's a great idea, thanks!
So I'd get the Bundle for SWT, and then ask whether it's been started:

Bundle b = Platform.getBundle("org.eclipse.swt");
if (b==null || b.getState()!=Bundle.ACTIVE) {
System.out.println("headless!");
}

would that be a valid check, or is there yet any flaw?
Or possibility to do it even more elegant?

Thanks,
--
Martin Oberhuber
Target Management Project Lead, DSDP PMC Member
http://www.eclipse.org/dsdp/tm


Paul Webster wrote:
> Martin Oberhuber wrote:
>> Hello,
>>
>> I'm writing a plugin that can be used in both headless applications as
>> well as Eclipse with full UI; at some places (e.g. user interaction)
>> it should behave differently when running headless or running with UI.
>>
>> Is there a commonly accepted technique for knowing whether the current
>> application is headless or not?
>
>
> I'm not sure if there is a better way to find out, but if you use
> core.runtime there is possibly a faster way:
>
> org.eclipse.core.runtime.Platform.getBundle("org.eclipse.swt ")
>
> I think it uses the PackageAdmin service to find the named bundle.
>
> Later,
> PW
Re: How does my plugin know if it's running headless? [message #318309 is a reply to message #318284] Wed, 25 July 2007 09:16 Go to previous messageGo to next message
Eclipse UserFriend
FYI,

zx cross-posted this question on his blog at
http://mea-bloga.blogspot.com/2007/07/am-i-headless.html
and some commenters said that the check were not appropriate
in some cases.

I guess that in fact a really correct check depends on what you want to
accomplish in your headless application. Some headless apps like a
graphics converter may want SWT for some tasks even if there is no
display and no window. Others might use a different UI toolkit than SWT.

I do think, though, that the idea of the check is still valid, with the
actual bundle being tested replaced by whatever else one thinks is
appropriate. Test for "org.eclipse.ui" or any other bundle you don't
expect to be active when running headless and thus get your customized
headless check.

> Bundle b = Platform.getBundle("org.eclipse.swt");
> if (b==null || b.getState()!=Bundle.ACTIVE) {
> System.out.println("headless!");
> }

Using PlatformUI.isWorkbenchRunning() as Mark Melvin has suggested is
also an option. Downside: This activates org.eclipse.ui for the mere
task of knowing whether UI is active or not.

A third possibility is writing your own Application for the things you
want to do headless, and do a System.setProperty("HEADLESS"); in there
-- then your plugins can test for that property. Downside: this will not
work if your plugin is designed to run in several different environments.

Any other comments or suggestions?

Thanks,
--
Martin Oberhuber
Target Management Project Lead, DSDP PMC Member
http://www.eclipse.org/dsdp/tm
Re: How does my plugin know if it's running headless? [message #318321 is a reply to message #318309] Wed, 25 July 2007 14:17 Go to previous message
Eclipse UserFriend
Martin Oberhuber wrote:
> FYI,
>
> zx cross-posted this question on his blog at
> http://mea-bloga.blogspot.com/2007/07/am-i-headless.html
> and some commenters said that the check were not appropriate
> in some cases.
>
> I guess that in fact a really correct check depends on what you want to
> accomplish in your headless application. Some headless apps like a
> graphics converter may want SWT for some tasks even if there is no
> display and no window. Others might use a different UI toolkit than SWT.


Like you mentioned, you could "move up" the hierarchy, then.
org.eclipse.ui.workbench is where most of the PlatformUI code resides.
If that's not active, you are definitely not running a workbench :-) If
it is active, you could query your isWorkbenchRunning().

Later,
PW
Previous Topic:how to find out programmatically if a bundle is in workspace or target platform?
Next Topic:best way to have users dl required plugins for my update site?
Goto Forum:
  


Current Time: Sat May 10 04:21:54 EDT 2025

Powered by FUDForum. Page generated in 0.03504 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top