Académique Documents
Professionnel Documents
Culture Documents
Version: 1.1.4
Abstract
Developing fully single-sourced bundles for Eclipse RCP and RAP requires following certain practices. This paper describes dierent patterns and refactorings to achieve single sourcing of applications for both runtime platforms. These patterns have been gleaned from our experiences single sourcing various applications in the Eclipse.org runtime space and tooling space.
Contents
Contents
1 Introduction 2 Workspace organization 2.1 Target platforms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2.2 Using separate workspaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2.3 Develop principally on the RAP Target Platform . . . . . . . . . . . . . . . . . . . . . 3 Dependencies 3.1 The problem - What strategy is 3.1.1 Bundle dependencies . . 3.1.2 Package dependencies . 3.1.3 Split packages . . . . . . 3.2 Favor Require-Bundle . . . . . 5 6 6 6 7 8 8 8 8 8 8 10 10 10 10 12 12 12 13 13 13 14 14 14 16 16 16 18 18 18 20 20 20 20 22 22 22 24 24 24
. . . . .
. . . . .
. . . . .
. . . . .
. . . . .
. . . . .
. . . . .
. . . . .
. . . . .
. . . . .
. . . . .
. . . . .
4 Missing API 4.1 The problem . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4.2 Building fragments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4.3 Extract a compatibility plug-in . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5 Missing extension points 5.1 The problem . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5.2 Move extensions to fragments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6 Application startup and Activator scope 6.1 The problem . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6.2 Separate session-specic code from Activator . . . . . . . . . . . . . . . . . . . . . . . 7 API Dierences 7.1 The problem . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7.2 Encapsulate the problem . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8 Field validation 8.1 The problem . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8.2 Reduce the frequency of validation checks . . . . . . . . . . . . . . . . . . . . . . . . . 9 SWT Resources 9.1 The problem . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9.2 Use higher-level API . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10 Singletons and Scopes 10.1 The problem . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10.2 Use session singletons . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10.3 Move the instance creation to the fragments . . . . . . . . . . . . . . . . . . . . . . . . 11 Jobs and background threads 11.1 The Problem . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11.2 Use a facade . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12 Internationalization and localization 12.1 The problem . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12.2 Make translatable strings non-static . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Contents
12.3 12.4 12.5 12.6 Remove static initializer and supertype . . . Create an NLSHelper and implementations Fix your message references . . . . . . . . . Localize the plug-in manifest . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24 25 26 26
1 Introduction
1 Introduction
The purpose of the Eclipse RT top-level project at Eclipse is to bring together various runtime related eorts and technologies and to foster, promote and house runtime eorts at Eclipse1 . It is part of a larger Equinox Community drive to implement Equinox-based technology across a broad range of computing environments and problem domains. Underlying all these eorts is the common goal of providing a uniform component model across a wide variety of computing environments. The Equinox framework and OSGi [All03] form the basis of this infrastructure. Write once, run everywhere is the main objective of the JavaTM programming language [AHL+ 05]. This holds true most of the time for running the same program on dierent operating systems. But as technology evolves the Eclipse Foundations strives to bring this slogan to a new level. Two technology projects, housed under the Eclipse.org umbrella, namely RAP2 and eRCP3 , provide an alternative runtime environment for RCP applications. This means that the application - normally running as a desktop application on a personal computer - can now be deployed to other runtime environments. For example let us assume we have a ready to launch application based on RCP. By switching the runtime from RCP to RAP, it becomes possible to launch the application on an application server where clients ccan use the application with a Web 2.0 centric interface on a browser. There is no need for the user to install any further add-ons or plugins. In order to achieve full compatibility between the platforms, many concepts implemented in SWT [NW04] need to be adapted to other runtimes. These are hidden behind the public API which remains synchronous across all runtime projects. Developing fully single-sourced bundles for Eclipse RCP and RAP requires following certain practices. It may also be possible to change the structure of the application code to t the current runtime environment. Depending on the specic case, this may be achievable through normal refactorings [FBB+ 99]. The refactorings are not onerous and generally lead to better quality bundles irrespective of the single-sourcing requirement.
2 Workspace organization
2 Workspace organization
2.1 Target platforms
Eclipse PDE uses the concept of a Target Platform to manage the set of third-party bundles available for development. The target platform is the directory containing the pre-built binary bundles that the workspace bundles depend on. It is used to compile the workspace and is also used when the user launches an application by clicking Run or Debug from the workbench. RAP itself delivers its own target platform so developers can easily switch between the RCP plugins and the RAP counterparts. By default Eclipse uses its own installation directory as the Target Platform, which is suitable for developing SDK plug-ins but is less suitable for developing RCP or RAP bundles. For RCP, this default Target Platform is unsuitable because it contains many superuous bundles relating to the IDE functionality, which may inadvertently be pulled in as dependencies of the application. For RAP the default is unsuitable for the same reason, and in addition it doesnt contain all the RAP-specic API bundles. Managing two separate target platforms is currently one of the key challenges when single-sourcing applications for RAP and RCP. Unfortunately PDE only allows one Target Platform at a time. There are two disadvantages to this limit. First, switching the target is very time-consuming as the whole workspace needs to be recompiled. Secondly, we need to close all related projects which do not match the target platform in order to have a clean workspace. A reasonable solution which has been become apparent is to use dierent workspaces for the dierent runtime environments. However, it is important to frequently check the code for compliance with the alternative runtime environments. An automated build should be used to perform a full build against both Target Platforms at regular intervals.
2 Workspace organization
RCP-specic projects RAP-specic projects
3 Dependencies
3 Dependencies
3.1 The problem - What strategy is best for managing dependencies
OSGi (and therefore Eclipse RCP and RAP) oers two separate mechanisms for declaring dependencies between bundles: Import-Package, which provides dependencies on the Java package-level, and Require-Bundle, which provides for dependencies based on whole bundles. 3.1.1 Bundle dependencies Require-Bundle (Require-Bundle ) was introduced in OSGi Release 4, principally to support legacy Eclipse plug-ins, which in Eclipse 3.0 became OSGi bundles. The pre-3.x Eclipse module system did not have any concept similar to Import-Package, and dependencies were specied based on the ID of another plug-in. Therefore in OSGi R4, Require-Bundle works by importing all of the packages exported from a particular named bundle. Require-Bundle still tends to be favored by many Eclipse plug-in developers both for historical reasons and because PDE makes it signicantly easier to use in development than Import-Package. 3.1.2 Package dependencies Import-Package is the original mechanism used in OSGi, and until Release 4 it was the only one. It is very simple: a bundle lists all of the Java packages that it imports from other bundles, including packages supplied by the JRE such as javax.swing, org.w3c.dom etc. These packages are then resolved at runtime by the OSGi framework (i.e., Equinox) against a corresponding exported package from another bundle. 3.1.3 Split packages The eclipse workbench uses the OSGi concept of split packages. This enables OSGi to have virtual packages which are physically split across several bundles. See the OSGi 4.1 specication (3.13.3) for more informations about split packages. In order to import only a specied part of the package, you need to extend the Import-Package declaration with the split attribute as shown in the following MANIFEST.MF fragment:
1 2 3 4
M a n i f e s t V e r s i o n :
1.0
Listing 1: Importing a split package The ui.workbench=split directive tells Equinox to use only the ui.workbench part of this split package. Otherwise the dependencies would not be resolved.
M a n i f e s t V e r s i o n :
1.0
R e q u i r e Bundle : o r g . e c l i p s e . r a p . u i ; r e s o l u t i o n := o p t i o n a l ; v i s i b i l i t y := r e e x p o r t , o r g . e c l i p s e . r a p ; r e s o l u t i o n := o p t i o n a l ; v i s i b i l i t y := r e e x p o r t
3 Dependencies
Now all the applications UI bundles can use the compatibility bundle as their main dependency for getting wired to the correct bundle at runtime.
4 Missing API
4 Missing API
4.1 The problem
Inevitably some parts of an application cannot be single-sourced. For example we may decide to oer convenience features in the RCP edition that are not available under RAP, or develop presentation or theming code for the RAP edition. In each case this leads to code that will compile under RCP but not RAP and vice versa.
Which parts of the application will live in the host bundle and what is platform specic will be discussed in the rest of this paper.
10
4 Missing API
11
<? xml version= 1 . 0 e n c o d i n g=UTF8?> <? e c l i p s e version= 3 . 2 ?> < f r a g m e n t> <e x t e n s i o n p o i n t= o r g . e c l i p s e . r a p . u i . e n t r y p o i n t > <e n t r y p o i n t c l a s s=maildemo . E n t r y P o i n t i d=maildemo . r a p . e n t r y p o i n t p a r a m e t e r=maildemo > </ e n t r y p o i n t> </ e x t e n s i o n> </ f r a g m e n t>
12
Listing 4: Examplary Activator One solution is to move the related code into the createUI method of the entrypoint as this will be executed in the current session context.
1 2 3 4 5 6 7 8 9 10
Listing 5: Examplary entrypoint Another possibility is to use the org.eclipse.ui.startup extension point to contribute new runnables into the startup process of the workbench. These IStartup implementations are registered trough the extension point mechanism as shown in the following snippet:
1 2 3
Listing 6: plugin.xml This is optimal for situations such as when the workbench has a session-scope and the IStartup implementations are to be called when the session starts.
13
7 API Dierences
7 API Dierences
7.1 The problem
Inevitably there are some dierences between the two runtimes - a common problem when single sourcing an application. RAP provides a strict subset of all available SWT and RCP APIs. If the API is available it has the same name, belongs to the same package and behaves the same way as the corresponding RCP API. But this means that API which is not available in RAP needs to be wrapped somehow in order to be able to use it in the common codebase. Some examples for missing API are: GC FileDialog MouseMove Events However, RAP itself provides a minimal set of APIs which are not part of RCP. These were introduced in order to meet several web specic requirements. The following describes how to use these APIs without breaking the RCP implementation.
Listing 7: About Action As ActionFactory.ABOUT is not available we need to replace it with a facade. This facades delegates the actual request during the runtime to the corresponding fragment project. In the case of RCP we can use the real ActionFactory.ABOUT implementation. Providing an alternative action for the web is done by returning another action implementation by the RAP fragment. We replace the real API with a call to our new facade which follows in the next paragraph.
1 2
Listing 8: Replace the call with the facade Then we create the facade we have introduced.
1 2 3 4 5 6 7 8 9 10 11
public abstract c l a s s A c t i o n F a c t o r y F a c a d e { private f i n a l s t a t i c A c t i o n F a c t o r y F a c a d e IMPL ; static { IMPL = ( A c t i o n F a c t o r y F a c a d e ) I m p l e m e n t a t i o n L o a d e r . n e w I n s t a n c e ( ActionFactoryFacade . class ) ; } public s t a t i c IWorkbenchAction c r e a t e A b o u t A c t i o n ( f i n a l IWorkbenchWindow window ) { return IMPL . c r e a t e A b o u t A c t i o n I n t e r n a l ( window ) ; } abstract IWorkbenchAction c r e a t e A b o u t A c t i o n I n t e r n a l ( IWorkbenchWindow window ) ;
14
7 API Dierences
12
Listing 9: Abstract type in host bundle The purpose of the facade is to load the correct implementation of the ActionFactoryFacade and call their internal methods to create the requested objects. The ImplementationLoader is therefore needed to load the implementation of the abstract type from the fragment. It should be implemented as the following snippet shows:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
public c l a s s I m p l e m e n t a t i o n L o a d e r { public s t a t i c O b j e c t n e w I n s t a n c e ( f i n a l C l a s s t y p e ) { S t r i n g name = t y p e . getName ( ) ; Object r e s u l t = null ; try { r e s u l t = t y p e . g e t C l a s s L o a d e r ( ) . l o a d C l a s s ( name + Impl ) . n e w I n s t a n c e ( ) ; } catch ( Throwable t h r o w a b l e ) { S t r i n g t x t = Could n o t l o a d i m p l e m e n t a t i o n f o r { 0 } ; S t r i n g msg = MessageFormat . f o r m a t ( t x t , new O b j e c t [ ] { name } ) ; throw new RuntimeException ( msg , t h r o w a b l e ) ; } return r e s u l t ; } }
Listing 10: ImplementationLoader We now have a facade that encapsulates the required functionality and the ImplementationLoader. Our nal step is to create two dierent implementations of the behavior for the dierent runtimes. As mentioned above the implementation for the ABOUT action can simply be delegated to the original RCP equivalent.
1 2 3 4 5 6 7
Listing 11: Concrete type for RCP fragment For RAP we need to provide our own implementation of the ABOUT action here.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
. . . .
public c l a s s A c t i o n F a c t o r y F a c a d e Impl extends A c t i o n F a c t o r y F a c a d e { private c l a s s AboutAction extends A c t i o n implements IWorkbenchAction { private IWorkbenchWindow window ; public AboutAction ( IWorkbenchWindow window ) { t h i s . window = window ; s e t I d ( about ) ; s e t T e x t ( About RAP MailDemo ) ; s e t T o o l T i p T e x t ( About RAP MailDemo ) ; } public void d i s p o s e ( ) { window = n u l l ; } public void run ( ) { S t r i n g t i t l e = About Message ; S t r i n g msg = T h i s i s t h e about Message o f t h e RAP Mail Demo ; M e s s a g e D i a l o g . o p e n I n f o r m a t i o n ( window . g e t S h e l l ( ) , t i t l e , msg ) ; } } IWorkbenchAction c r e a t e A b o u t A c t i o n I n t e r n a l ( IWorkbenchWindow window ) { return new AboutAction ( window ) ; } }
15
8 Field validation
8 Field validation
8.1 The problem
A common pattern for form validation in RCP and SWT applications is to use the ModifyListener interface to implement keystroke-level validation of eld data. This has the advantage of producing more immediate feedback for the user regarding the data they are entering. The following code snippet shows an example of this technique:
1 2 3 4 5 6 7 8 9 10 11
Text t x t D a t e = new Text ( p a r e n t , SWT.BORDER) ; t x t D a t e . a d d M o d i f y L i s t e n e r ( new M o d i f y L i s t e n e r ( ) { public void modifyText ( ModifyEvent e ) { try { date = f or matter . parse ( txtDate . getText ( ) ) ; setErrorMessage ( null ) ; } catch ( P a r s e E x c e p t i o n e ) { setErrorMessage ( I n v a l i d date ) ; } } }) ;
Listing 13: Usage of ModifyListener Unfortunately the Modify - and VerifyListener of RWT do not behave exactly as their correspoinding parts in SWT. This can be traced back to the distributed nature of RAP. Over a high-latency network connection the SWT behavior can introduce signicant performance degradation to the UI. RWT generally reduces the number of requests sent to the server and decreases server load by merging multiple events into one HTTP request. This is especially interesting when utilizing the doit ag of the incoming event as this may match to several keystrokes instead of a single keystroke. This can lead to semantic dierences as the following example illustrates. Imagine that we have a VerifyLister that restricts the length of a text eld.
1 2 3 4 5 6 7 8 9 10
S W T.MULTI ) ;
Listing 14: Example of semantic dierences With SWT this snippet works as expected. Running the same snippet with RWT you need to be careful as the events are merged into bigger chunks. This can lead to a problem when you type more than 5 characters into the texteld. Depending on the timing it could happen that the doit ag will be set to false for the whole chunk - not only for the events after the fth character. The Eclipse Databinding framework is, in most situations, based on VerifyListener s and thus is subject to the same restrictions. In the case of a high-latency network connection you should be careful when integrating the Databinding framework.
f i n a l Text t = new Text ( s h e l l , SWT.BORDER t . setText ( here i s some text ) ; t . addFocusListener ( new FocusAdapter ( ) {
S W T.MULTI ) ;
16
8 Field validation
6 7 8 9 10 11 12 13
Listing 15: FocusListener for eld validation With this approach the events only get red on focus events no matter what happens inside the text eld.
17
9 SWT Resources
9 SWT Resources
9.1 The problem
As RAP lives in a server environment the logic is only run on the server side while the client is responsible for showing the UI. Thus the concepts of SWT for acquiring system resources like colors or fonts cannot be carried over in exactly the same fashion. RAP also lowers memory consumption on the server-side by reusing existing resources. Therefor RAP banned constructors from all SWT resources while providing factory methods for the following resource classes. org.eclipse.swt.graphics.Color org.eclipse.swt.graphics.Font org.eclipse.swt.graphics.Image org.eclipse.swt.graphics.Cursor When writing SWT code you are responsible for disposing all resources created in your code. Under normal circumstances this is done by calling dispose on the resource object. As this would invalidate the resources across all sessions, RAP does not provide the dispose mechanism at all.
Listing 16: Creating a new color (SWT) Instead of initiating the color object directly,we manage the lifetime of these objects with registries that JFace provides.
1 2 3 4 5 6 7
import o r g . e c l i p s e . j f a c e . r e s o u r c e . C o l o r R e g i s t r y ; import o r g . e c l i p s e . swt . g r a p h i c s . C o l o r ; import o r g . e c l i p s e . swt . g r a p h i c s .RGB; C o l o r R e g i s t r y r e g i s t r y = new C o l o r R e g i s t r y ( D i s p l a y . g e t C u r r e n t ( ) ) ; r e g i s t r y . put ( r e d C o l o r , new RGB( 2 5 5 , 0 , 0 ) ) ; Color redColor = r e g i s t r y . get ( redColor ) ;
Listing 17: Creating a new color (JFace) There may be situations when it is not possible to use the JFace registries. The above mentioned resources can also be obtained directly through the factory methods of RAP. These are available on the Graphics class introduced by RAP.
1 2 3 4 5
18
9 SWT Resources
As the Graphics class is only available in RAP, the corresponding source artifact needs to be added to the RAP specic fragments. For the RCP implementation constructor would be used. An important item to note is that the resources in RCP need to be disposed manually. This results in an empty implementation for the RAP fragment.
19
Listing 19: Singleton (Application scope) To reduce the scope of the object instance to a specic session we use the concept of session singletons . RAP already provides a helper class to create objects like this within the session scope. The class is called SessionSingletonBase and is used as follows:
1 2 3 4 5 6 7 8 9 10 11
Listing 20: Singleton (Session scope) Doing this behind a facade we can reuse the same concept as we used in chapter 7.
20
public i n t e r f a c e I S i n g l e t o n P r o v i d e r { Object g e t I n s t a n c e I n t e r n a l ( ) ; }
Listing 22: Singleton Provider Interface As the singleton provider diers between the platform-specic fragments (singleton for RCP, sessionsingleton for RAP) we need to provide two dierent implementations of the interface. For RCP we implement the ISingletonProvider interface as a regular singleton. This implementation belongs to the RCP-fragment.
1 2 3 4 5 6 7 8 9 10 11
Listing 23: Singleton Provider (RCP) When running the application with multiple users, we want to create a session-scoped singleton. This implementation belongs to the RAP-fragment.
1 2 3 4 5 6 7
Listing 24: Singleton Provider (RAP) With this architecture we are able to reuse the same code in RAP and RCP. The singleton implementation lives in the host bundle and only the way it is initiated is platform-specic.
21
import o r g . e c l i p s e . c o r e . r u n t i m e . j o b s . Job ; import o r g . e c l i p s e . swt . w i d g e t s . D i s p l a y ; public abstract c l a s s J o b F a c t o r y { private f i n a l s t a t i c J o b F a c t o r y IMPL ; static { IMPL = ( J o b F a c t o r y ) I m p l e m e n t a t i o n L o a d e r . n e w I n s t a n c e ( J o b F a c t o r y . c l a s s ) ; } public s t a t i c Job c r e a t e J o b ( f i n a l D i s p l a y d i s p l a y , f i n a l S t r i n g name , runnable ) { return IMPL . c r e a t e J o b I n t e r n a l ( d i s p l a y , name , r u n n a b l e ) ; } abstract Job c r e a t e J o b I n t e r n a l ( D i s p l a y d i s p l a y , } f i n a l JobRunnable
S t r i n g name , JobRunnable r u n n a b l e ) ;
Listing 25: Abstract Job Factory To encapsulate the code that will be run in a job we introduced a JobRunnable. This has the same semantics as a regular java.lang.Runnable but has the ability to transfer the status of the job (see IStatus ).
1 2 3 4 5 6
Listing 26: JobRunnable As RCP assumes only one user (session) we can execute the runnable in a regular job. The implementation for the RCP fragment is shown below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
public c l a s s J o b F a c t o r y I m p l extends J o b F a c t o r y { Job c r e a t e J o b I n t e r n a l ( f i n a l D i s p l a y d i s p l a y , f i n a l S t r i n g name , runnable ) { return new Job ( name ) { protected I S t a t u s run ( I P r o g r e s s M o n i t o r m o n i t o r ) { return r u n n a b l e . run ( m o n i t o r ) ; } }; } } f i n a l JobRunnable
22
package f a c a d e s ; import import import import import org . org . org . org . org . eclipse eclipse eclipse eclipse eclipse . c o r e . runtime . IProgressMonitor ; . c o r e . runtime . I S t a t u s ; . c o r e . r u n t i m e . j o b s . Job ; . rwt . l i f e c y c l e . UICallBack ; . swt . w i d g e t s . D i s p l a y ;
public c l a s s J o b F a c t o r y I m p l extends J o b F a c t o r y { Job c r e a t e J o b I n t e r n a l ( f i n a l D i s p l a y d i s p l a y , f i n a l S t r i n g name , f i n a l JobRunnable runnable ) { Job j o b = new Job ( name ) { protected I S t a t u s run ( f i n a l I P r o g r e s s M o n i t o r m o n i t o r ) { f i n a l I S t a t u s [ ] r e s u l t = new I S t a t u s [ 1 ] ; UICallBack . runNonUIThreadWithFakeContext ( d i s p l a y , new Runnable ( ) { public void run ( ) { r e s u l t [ 0 ] = r u n n a b l e . run ( m o n i t o r ) ; } }) ; return r e s u l t [ 0 ] ; } }; return j o b ; } }
23
import o r g . e c l i p s e . o s g i . u t i l . NLS ; public c l a s s M e s s a g e s extends NLS { private s t a t i c f i n a l S t r i n g BUNDLE NAME = maildemo . m e s s a g e s ; //$NON NLS1 $ public public public public static static static static String String String String OpenViewAction OpenViewAction OpenViewAction OpenViewAction 0; 1; 2; 3;
5 http://help.eclipse.org/ganymede/topic/org.eclipse.jdt.doc.user/concepts/concept-string-externalization.htm
24
public abstract c l a s s NLSHelper { protected s t a t i c f i n a l S t r i n g BUNDLE NAME = sample . m e s s a g e s ; //$NON NLS1 $ private f i n a l s t a t i c NLSHelper IMPL ; static { IMPL = ( NLSHelper ) I m p l e m e n t a t i o n L o a d e r . n e w I n s t a n c e ( NLSHelper . c l a s s ) ; } public s t a t i c M e s s a g e s g e t M e s s a g e s ( C l a s s c l a z z ) { return ( M e s s a g e s ) IMPL . i n t e r n a l G e t M e s s a g e s ( c l a z z ) ; } protected abstract O b j e c t i n t e r n a l G e t M e s s a g e s ( C l a s s c l a z z ) ;
Listing 31: NLS Helper Facade RAP itself provides some helper classes in order to fully support multi-language applications. One of these classes is RWT.NLS which can be found in the org.eclipse.rwt package. This class helps to load the translated .properties les into the class elds. The following snippet shows how to implement the NLSHelper.
1 2 3 4 5 6 7 8 9
import o r g . e c l i p s e . rwt .RWT; public c l a s s NLSHelperImpl extends NLSHelper { protected O b j e c t i n t e r n a l G e t M e s s a g e s ( C l a s s c l a z z ) { return RWT. NLS . g e t I S O 8 8 5 9 1 E n c o d e d (BUNDLE NAME, M e s s a g e s . c l a s s ) ; } }
Listing 32: RAP implementation for NLSHelper As RCP was never designed to support multiple languages during runtime we cannot reuse the NLS implementation of OSGi. Therefore we need to provide our own solution as the following snippet shows.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
. . . . . .
public c l a s s NLSHelperImpl extends NLSHelper { protected O b j e c t i n t e r n a l G e t M e s s a g e s ( C l a s s c l a z z ) { ClassLoader loader = c l a z z . getClassLoader () ; R e s o u r c e B u n d l e b u n d l e = R e s o u r c e B u n d l e . g e t B u n d l e (BUNDLE NAME, L o c a l e . getDefault () , loader ) ; return i n t e r n a l G e t ( bundle , c l a z z ) ; } private O b j e c t i n t e r n a l G e t ( R e s o u r c e B u n d l e bundle , C l a s s c l a z z ) { Object r e s u l t ; try { Constructor c o n s t r u c t o r = c l a z z . getDeclaredConstructor ( null ) ; c o n s t r u c t o r . s e t A c c e s s i b l e ( true ) ; r e s u l t = c o n s t r u c t o r . newInstance ( null ) ; } catch ( f i n a l E x c e p t i o n ex ) { throw new I l l e g a l S t a t e E x c e p t i o n ( ex . g e t M e s s a g e ( ) ) ; } final Field [ ] fieldArray = clazz . getDeclaredFields () ; f o r ( i n t i = 0 ; i < f i e l d A r r a y . l e n g t h ; i ++) { try { int m o d i f i e r s = f i e l d A r r a y [ i ] . g e t M o d i f i e r s ( ) ; i f ( S t r i n g . c l a s s . i s A s s i g n a b l e F r o m ( f i e l d A r r a y [ i ] . getType ( ) )
25
&& M o d i f i e r . i s P u b l i c ( m o d i f i e r s ) && ! M o d i f i e r . i s S t a t i c ( m o d i f i e r s ) ) { try { S t r i n g value = bundle . g e t S t r i n g ( f i e l d A r r a y [ i ] . getName ( ) ) ; i f ( v a l u e != n u l l ) { f i e l d A r r a y [ i ] . s e t A c c e s s i b l e ( true ) ; fieldArray [ i ] . set ( result , value ) ; } } catch ( f i n a l M i s s i n g R e s o u r c e E x c e p t i o n mre ) { f i e l d A r r a y [ i ] . s e t A c c e s s i b l e ( true ) ; f i e l d A r r a y [ i ] . s e t ( r e s u l t , ) ; mre . p r i n t S t a c k T r a c e ( ) ; } } } catch ( f i n a l E x c e p t i o n ex ) { ex . p r i n t S t a c k T r a c e ( ) ; } } return r e s u l t ; } }
Listing 34: Instance elds instead of static elds With this approach we can serve multi-lingual applications with support for multiple concurrent languages - for both RCP and RAP.
<e x t e n s i o n p o i n t= o r g . e c l i p s e . u i . v i e w s > <view i d= o r g . e c l i p s e . r a p . h e l l o w o r l d . h e l l o W o r l d V i e w c l a s s= o r g . e c l i p s e . r a p . h e l l o w o r l d . HelloWorldView name=%helloWorldView name > </ view> </ e x t e n s i o n> <e x t e n s i o n p o i n t= o r g . e c l i p s e . u i . p e r s p e c t i v e s >
6 http://www.eclipse.org/rap/cvs.php
26
Listing 35: Sample plugin.xml with placeholders And heres the plugin.properties:
1 2
Listing 36: plugin.properties To make this work, the OSGi manifest le (MANIFEST.MF) must contain the following line:
1
Bundle L o c a l i z a t i o n :
plugin
Listing 37: MANIFEST.MF In summary, RAP requires special treatment to support the internationalization of extensions because of its server-side, multi-user nature. However the Equinox extension registry does the translation on startup and caches the results. In RAP, dierent sessions may require translations into dierent languages. To solve this, we had to exploit the translation mechanism of the Equinox extension registry. To launch the application from Eclipse, you need to import the plug-in org.eclipse.equinox.registry as source plug-in into your workspace. Make sure that this plug-in is also included in the launch conguration (from the workspace, not from the target platform).
27
Listings
List of Figures
1 2 The RCP and RAP APIs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Extended single sourcing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7 11
List of Tables
1 Bundle Naming Scheme . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
Listings
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 Importing a split package . . . . . . . . . . . . . Optional bundle requirements . . . . . . . . . . . Sample fragment.xml . . . . . . . . . . . . . . . . Examplary Activator . . . . . . . . . . . . . . . . Examplary entrypoint . . . . . . . . . . . . . . . plugin.xml . . . . . . . . . . . . . . . . . . . . . . About Action . . . . . . . . . . . . . . . . . . . . Replace the call with the facade . . . . . . . . . . Abstract type in host bundle . . . . . . . . . . . ImplementationLoader . . . . . . . . . . . . . . . Concrete type for RCP fragment . . . . . . . . . Concrete type for RAP fragment . . . . . . . . . Usage of ModifyListener . . . . . . . . . . . . . . Example of semantic dierences . . . . . . . . . . FocusListener for eld validation . . . . . . . . . Creating a new color (SWT) . . . . . . . . . . . Creating a new color (JFace) . . . . . . . . . . . Creating a new color (RWT) - not recommended Singleton (Application scope) . . . . . . . . . . . Singleton (Session scope) . . . . . . . . . . . . . Singleton (delegating stub) . . . . . . . . . . . . Singleton Provider Interface . . . . . . . . . . . . Singleton Provider (RCP) . . . . . . . . . . . . . Singleton Provider (RAP) . . . . . . . . . . . . . Abstract Job Factory . . . . . . . . . . . . . . . . JobRunnable . . . . . . . . . . . . . . . . . . . . Job Factory Implementation (RCP) . . . . . . . Job Factory Implementation (RAP) . . . . . . . Sample RCP Message class . . . . . . . . . . . . Accessor for Message classes . . . . . . . . . . . . NLS Helper Facade . . . . . . . . . . . . . . . . . RAP implementation for NLSHelper . . . . . . . RCP implementation of the NLSHelper . . . . . Instance elds instead of static elds . . . . . . . Sample plugin.xml with placeholders . . . . . . . plugin.properties . . . . . . . . . . . . . . . . . . MANIFEST.MF . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8 8 12 13 13 13 14 14 14 15 15 15 16 16 16 18 18 18 20 20 20 21 21 21 22 22 22 23 24 24 25 25 25 26 26 27 27
28
References
References
[AHL+ 05] Ken Arnold, David Holmes, Tim Lindholm, Frank Yellin, Frank Yellin, The Java Team, Mary Campione, Kathy Walrath, Patrick Chan, Rosanna Lee, Jonni Kanerva, James Gosling, James Gosling, James Gosling, James Gosling, Bill Joy, Bill Joy, Guy Steele, Guy Steele, Gilad Bracha, and Gilad Bracha. Java language specication, third edition, 2005. [All03] Osgi Alliance. OSGi Service Platform: The OSGi Alliance. IOS Press, December 2003.
[FBB+ 99] Martin Fowler, Kent Beck, John Brant, William Opdyke, and Don Roberts. Refactoring: Improving the Design of Existing Code (The Addison-Wesley Object Technology Series). Addison-Wesley Professional, 7 1999. [GHJV95] Erich Gamma, Richard Helm, Ralph Johnson, and John Vlissides. Addison-Wesley Professional, January 1995. [NW04] Design Patterns.
S. Northover and M. Wilson. SWT: the standard widget toolkit. Volume 1. Boston: AddisonWesley, 2004.
29