`
zhaozhongwei
  • 浏览: 52640 次
  • 性别: Icon_minigender_1
  • 来自: 沈阳
社区版块
存档分类
最新评论

Opening files in Eclipse from the command line(转发)

阅读更多

I ran a query to see all the bugs fixed in the Eclipse Platform in 3.6; it is a long list (4309 and counting). Felipe gets credit for the oldest bug fixed (raised in 2001), but in a close second is bug 4922 (raised only a day later).

This bug is about opening files in eclipse from the command line. Fixing it required a coordinated effort between Platform UI, SWT , and the Equinox launcher . A lot of the credit for what was done goes to Kevin Barnes.

This post is an effort to explain some of the technical details of what is going here.

On the Mac... : On the mac all we do is handle the apple event "kAEOpenDocuments ", most of the rest of this post doesn't apply to the mac.

Windows and GTK... Everything below applies to Windows and GTK, though there are some differences in the implementation details.

On Motif... Sorry, this doesn't work on motif.

The Launcher

Everything starts in the eclipse launcher. We added a few new command line options:

  • --launcher.openFile : obvious enough, specifies the file we want to open.
  • --launcher.defaultAction : less obvious, specifies the action to take when the launcher is started without any '-' arguments on the command line. Currently the only support value is "openFile ".
  • --launcher.timeout : a timeout value for how long we should spend trying to communicate with an already running eclipse before we give up and just open a new eclipse instance. Default is 60 (seconds).

The first argument is obvious enough, open the specified file in eclipse.

eclipse --launcher.openFile myFile.txt

This is great, but it is a bit much to type on the command line and is not quite enough to make everyone happy. We introduced the "default action" argument. This goes in the eclipse.ini file, the value should be "openFile ":

...
-showsplash
org.eclipse.platform
--launcher.defaultAction

openFile

-vmargs
-Xms256m
-Xmx768m

This tells the launcher that if it is called with a command line that only contains arguments that don't start with "- ", then those arguments should be treated as if they followed "--launcher.openFile ".

eclipse myFile.txt

This is the kind of command line the launcher will receive on windows when you double click a file that is associated with eclipse, or you select files and choose "Open With" or "Send To " Eclipse.

Relative paths will be resolved first against the current working directory, and second against the eclipse program directory.

Talking to SWT

The launcher talks to SWT through the use of a hidden window. The launcher and SWT both need to agree on the name of this window . This allows the launcher to find an already running eclipse and tell it to open the file. Any RCP application will need to ensure they get this right for things to work.

The launcher bases this on its "official name". The official name can be set with the -name argument. If -name is not set, then the official name is derived from the launcher executable, the extension is removed and the first letter is capitalized: rcp.exe becomes Rcp .

SWT bases this on the value set with the Display.setAppName() function. Normally, this is set by the Workbench when it creates the display and the value is the "appName" taken from the product extension point.

Listening to SWT

To take advantage of this, an RCP Application will need to register a listener for the SWT.OpenDocument event. It should register this listener before calling PlatformUI.createAndRunWorkbench so that the listener is in place before the workbench starts running the event loop.

The event loop will start running while the splash screen is still up, so events may arrive before the workbench is ready to actually open an editor for the file. This means that the listener should save the file paths it gets from the OpenDocument events so they can be opened at some later time. WorkbenchAdvisor#eventLoopIdle can be a good place to check for saved open file events.

Implementation details

Here is an overview of the flow of events in the launcher when processing --launcher.openFile on windows.

  1. Get the Official Name. As mentioned above, this is the "-name" argument, or derived from the executable name. For this explanation, we will be using "OfficialName ".
  2. Create and lock a mutex named "SWT_Mutex_OfficialName".
    • If multiple files are selected and opened on windows, then a seperate eclipse process will be created for each one. The mutex allows us to ensure only one eclipse instance is actually started.
    • One process will win the race to acquire the mutex, at this point, there will be no eclipse instance running that has the SWT window available. This process will start normally and eventually create the SWT window at which point it will release the mutex.
    • All the other processes wait trying to acquire the mutex, once the original process releases it, they will be able to find the SWT window and post their open file message there.
    • Each process only waits for --launcher.timeout seconds (default 60 seconds) before giving up and just starting its own full eclipse instance.


  3. Find the window named "SWT_Window_OfficialName"
    • If no such window exists, we are the first eclipse instance. In this case, we set a timer to look again later and then proceed with starting eclipse.
    • The timer fires every second for --launcher.timeout seconds.
    • If we don't find the SWT window before the timeout (perhaps it took too long for the workbench to create the display), then we will be unable to open the file.

  4. Send a message to the SWT window
    • Once we've found the SWT window, we create a custom message named "SWT_OPENDOC". We send this message with wParam & lParam specifying a shared memory id.
    • We write to the name of the file to open into shared memory, and when SWT receives the SWT_OPENDOC message, it uses that id to read the shared memory.
    • The launcher has long used shared memory on all platforms for the splash screen, restarts and exit messages.
    • Once SWT reads the file name from shared memory, it posts its own SWT.OpenDocument event.

On GTK, things happen in a similar manner with a few differences:

  1. We use semaphores .
    • Semaphores are not cleaned up automatically if the process exits unexpectedly. So we try to hold the semaphore for as short a time as possible and we install SIGINT and SIGQUIT signal handlers for the time we hold the semaphore.
    • The launcher creates a hidden GTK window named SWT_Window_LauncherOfficalName which is used in the same way as the mutex on windows. This lets us avoid holding the semaphore for an extended time while the first eclipse process starts up.

  2. We use a property instead of a message.
    • The property is named org.eclipse.swt.filePath.message .
    • The value is a colon separate list of file paths to open. Shared memory is not used like it is on windows.
分享到:
评论

相关推荐

    appfuse.tar.gz

    From the command line, cd into the appfuse directory and type "mvn idea:idea". Get a cup of coffee or soda (or even better, a beer!) while you wait for Maven to download all the dependencies. After...

    megax_10.0.5-1_amd64.deb ubuntu linux

    The second mode is the Prototype mode which is used solely for generating MEGA Analysis Options (.mao) files that specify analysis settings when using MEGA from a command shell The command-line ...

    Notepad++ 6.5.5

    Add new command line argument "-r" for opening files recursively (with wildcard characters). Fix the crash bug while setting current document as Objective-C source code. Fix rename not replacing ...

    UE(官方下载)

    We are often asked if it is possible to run an UltraEdit macro or script on a file from the command line. The answer is yes - and it's not only possible, it's extremely simple! Using find/replace ...

    BURNINTEST--硬件检测工具

    command line option is used. Release 5.3 build 1014 WIN32 release 30 January 2008 - Corrected a problem where the loopback sound test could run out of memory if run for several days. Release 5.3 ...

    hash table spell checking

    The program begins by opening a word list text file, specified by a command line parameter. The program outputs an error message and terminates if it cannot open the specified word list text file. A ...

    数据结构作业Hash表

    The program begins by opening a word list text file, specified by a command line parameter. The program outputs an error message and terminates if it cannot open the specified word list text file. A ...

    联想旭日420AXP系统芯片组驱动

    package is different from the '-A' command line flag option described under Compressed .ZIP Distribution. Example: To extract INF files using the '-A' flag described below, the installation ...

    Foundations for Analytics with Python O-Reilly-2016-Clinton W. Brownley

    Opening, searching in, and closing dozens of files isn’t fun, and the task becomes more and more challenging as the number of files increases. Because the problem involves searching through CSV and ...

    The C Cheat Sheet - An Introduction to Programming in C

    7.6 Command-Line Parameters 8.0 Multi-File Programs 8.1 Basic Concepts 8.2 Include Files as Interfaces 8.3 Object Files and Linking 8.4 The Details of the Compilation Process 9.0 The Standard C ...

    Computer Science from the Bottom Up

    Trying to understand the operating system is unfortunately not as easy as just opening the bonnet. The current Linux kernel runs into the millions of lines of code, add to that the other critical ...

    Advanced Programming in the UNIX Environment

    Typical services include executing a new program, opening a file, reading a file, allocating a region of memory, getting the current time of day, and so on. The focus of this text is to describe the ...

    word自动转化为chm

    Undoubtedly, Microsoft Word is the best choice for writing documents, but you have to use different tools to create CHM help files and online documents. However, what bothers you most is not using how...

    Django 1.1 Testing and Debugging.pdf

    Using the twill command line program 155 Using twill in a TestCase 159 Summary 167 Chapter 6: Django Debugging Overview 169 Django debug settings 170 The DEBUG and TEMPLATE_DEBUG settings 170 ...

    myBase Desktop 6.3.7

    The problem is that opening one or more Word Excel attachments may cause the attachments to be accidentally trashed and disappear from in the attachment pane The problem source is that myBase by ...

    aspectjweaver-1.9.6.jar报错error in opening zip file,下载后替换

    java: 读取D:\Develop\maven\repository\org\aspectj\aspectjweaver\1.9.6\aspectjweaver-1.9.6.jar时出错; error in opening zip file 提示jar出问题,找到此处,删除重新下载,但是jar依旧是损坏状态

    Solution for Designer error when opening WorldWind.cs in WW1.4.0

    When I load the WW project in my Visual C# 2005 Express Edition , I get the following error when I open WorldWind.cs. I can open the code but the designer refuses to open it. The designer cannot ...

    CE中文版-启点CE过NP中文.exe

    They can be downloaded from the download section where you can find the other translation files, or right here June 7 2017:Cheat Engine 6.7 Released: Cheat Engine 6.7 has been released. New lua ...

    PLSQL.Developer v11.0.0.1762 主程序+ v10中文包+keygen

    When enabled the folder history when opening and saving files is project specific. When disabled, the general history will be used. Project file browser preference. When enabled the project has its ...

    EGit用户指南

    3.19 Finding the author of each line in a file 3.20 Working with Submodules 3.20.1 Cloning Repositories with Submodules 3.20.2 Browsing Submodules 3.20.3 Adding a Submodule 3.20.4 Updating ...

Global site tag (gtag.js) - Google Analytics