KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > equinox > internal > app > EclipseAppDescriptor


1 /*******************************************************************************
2  * Copyright (c) 2005, 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
12 package org.eclipse.equinox.internal.app;
13
14 import java.security.AccessController JavaDoc;
15 import java.util.*;
16 import org.eclipse.equinox.app.IApplicationContext;
17 import org.osgi.framework.*;
18 import org.osgi.service.application.ApplicationDescriptor;
19 import org.osgi.service.application.ApplicationHandle;
20 import org.osgi.service.condpermadmin.BundleSignerCondition;
21 import org.osgi.service.condpermadmin.ConditionInfo;
22
23 /*
24  * An ApplicationDescriptor for an eclipse application.
25  */

26 public class EclipseAppDescriptor extends ApplicationDescriptor {
27     static final String JavaDoc APP_TYPE = "eclipse.application.type"; //$NON-NLS-1$
28
static final String JavaDoc APP_DEFAULT = "eclipse.application.default"; //$NON-NLS-1$
29
static final String JavaDoc APP_TYPE_MAIN_THREAD = "main.thread"; //$NON-NLS-1$
30
static final String JavaDoc APP_TYPE_ANY_THREAD = "any.thread"; //$NON-NLS-1$
31
static final int FLAG_VISIBLE = 0x01;
32     static final int FLAG_CARD_SINGLETON_GLOGAL = 0x02;
33     static final int FLAG_CARD_SINGLETON_SCOPED = 0x04;
34     static final int FLAG_CARD_UNLIMITED = 0x08;
35     static final int FLAG_CARD_LIMITED = 0x10;
36     static final int FLAG_TYPE_MAIN_THREAD = 0x20;
37     static final int FLAG_TYPE_ANY_THREAD = 0x40;
38     static final int FLAG_DEFAULT_APP = 0x80;
39     private long instanceID = 0;
40     private ServiceRegistration sr;
41     private Boolean JavaDoc locked = Boolean.FALSE;
42     private final EclipseAppContainer appContainer;
43     private final Bundle contributor;
44     private final int flags;
45     private final int cardinality;
46     private final String JavaDoc name;
47
48     protected EclipseAppDescriptor(Bundle contributor, String JavaDoc pid, String JavaDoc name, int flags, int cardinality, EclipseAppContainer appContainer) {
49         super(pid);
50         this.name = name;
51         this.contributor = contributor;
52         this.appContainer = appContainer;
53         this.locked = AppPersistence.isLocked(this) ? Boolean.TRUE : Boolean.FALSE;
54         this.flags = flags;
55         this.cardinality = cardinality;
56     }
57
58     protected Map getPropertiesSpecific(String JavaDoc locale) {
59         // just use the service properties; for now we do not localize any properties
60
return getServiceProperties();
61     }
62
63     protected ApplicationHandle launchSpecific(Map arguments) throws Exception JavaDoc {
64         // if this application is locked throw an exception.
65
if (getLocked().booleanValue())
66             throw new IllegalStateException JavaDoc("Cannot launch a locked application."); //$NON-NLS-1$
67
// initialize the appHandle
68
EclipseAppHandle appHandle = createAppHandle(arguments);
69         try {
70             // use the appContainer to launch the application on the main thread.
71
appContainer.launch(appHandle);
72         } catch (Throwable JavaDoc t) {
73             // be sure to destroy the appHandle if an error occurs
74
try {
75                 appHandle.destroy();
76             } catch (Throwable JavaDoc destroyError) {
77                 // ignore and clean up
78
}
79             if (t instanceof Exception JavaDoc)
80                 throw (Exception JavaDoc) t;
81             throw (Error JavaDoc) t;
82         }
83         return appHandle;
84     }
85
86     protected synchronized void lockSpecific() {
87         locked = Boolean.TRUE;
88         // make sure the service properties are updated with the latest lock info
89
refreshProperties();
90     }
91
92     protected synchronized void unlockSpecific() {
93         locked = Boolean.FALSE;
94         // make sure the service properties are updated with the latest lock info
95
refreshProperties();
96     }
97
98     void refreshProperties() {
99         ServiceRegistration reg = getServiceRegistration();
100         if (reg != null)
101             try {
102                 reg.setProperties(getServiceProperties());
103             } catch (IllegalStateException JavaDoc e) {
104                 // this must mean the service was unregistered
105
// just ignore
106
}
107     }
108
109     synchronized void setServiceRegistration(ServiceRegistration sr) {
110         this.sr = sr;
111     }
112
113     private synchronized ServiceRegistration getServiceRegistration() {
114         return sr;
115     }
116
117     private synchronized Boolean JavaDoc getLocked() {
118         return locked;
119     }
120
121     /*
122      * Gets a snapshot of the current service properties.
123      */

124     Hashtable getServiceProperties() {
125         Hashtable props = new Hashtable(10);
126         props.put(ApplicationDescriptor.APPLICATION_PID, getApplicationId());
127         if (name != null)
128             props.put(ApplicationDescriptor.APPLICATION_NAME, name);
129         props.put(ApplicationDescriptor.APPLICATION_CONTAINER, Activator.PI_APP);
130         props.put(ApplicationDescriptor.APPLICATION_LOCATION, getLocation());
131         Boolean JavaDoc launchable = appContainer.isLocked(this) == 0 ? Boolean.TRUE : Boolean.FALSE;
132         props.put(ApplicationDescriptor.APPLICATION_LAUNCHABLE, launchable);
133         props.put(ApplicationDescriptor.APPLICATION_LOCKED, getLocked());
134         Boolean JavaDoc visible = (flags & FLAG_VISIBLE) != 0 ? Boolean.TRUE : Boolean.FALSE;
135         props.put(ApplicationDescriptor.APPLICATION_VISIBLE, visible);
136         props.put(APP_TYPE, getThreadTypeString());
137         if ((flags & FLAG_DEFAULT_APP) != 0)
138             props.put(APP_DEFAULT, Boolean.TRUE);
139         return props;
140     }
141
142     private String JavaDoc getLocation() {
143         if (contributor == null)
144             return ""; //$NON-NLS-1$
145
return Activator.getLocation(contributor);
146     }
147
148     /*
149      * Returns the appHandle. If it does not exist then one is created.
150      */

151     private EclipseAppHandle createAppHandle(Map arguments) {
152         EclipseAppHandle newAppHandle = new EclipseAppHandle(getInstanceID(), arguments, this);
153         ServiceRegistration appHandleReg = (ServiceRegistration) AccessController.doPrivileged(appContainer.getRegServiceAction(new String JavaDoc[] {ApplicationHandle.class.getName(), IApplicationContext.class.getName()}, newAppHandle, newAppHandle.getServiceProperties()));
154         newAppHandle.setServiceRegistration(appHandleReg);
155         return newAppHandle;
156     }
157
158     EclipseAppContainer getContainerManager() {
159         return appContainer;
160     }
161
162     public boolean matchDNChain(String JavaDoc pattern) {
163         if (contributor == null)
164             return false;
165         return BundleSignerCondition.getCondition(contributor, new ConditionInfo(BundleSignerCondition.class.getName(), new String JavaDoc[] {pattern})).isSatisfied();
166     }
167
168     protected boolean isLaunchableSpecific() {
169         return true;
170     }
171
172     public synchronized void unregister() {
173         ServiceRegistration temp = sr;
174         if (temp != null) {
175             sr = null;
176             temp.unregister();
177         }
178     }
179
180     String JavaDoc getThreadTypeString() {
181         if ((flags & FLAG_TYPE_ANY_THREAD) != 0)
182             return APP_TYPE_ANY_THREAD;
183         return APP_TYPE_MAIN_THREAD;
184     }
185
186     int getThreadType() {
187         return flags & (FLAG_TYPE_ANY_THREAD | FLAG_TYPE_MAIN_THREAD);
188     }
189
190     int getCardinalityType() {
191         return flags & (FLAG_CARD_SINGLETON_GLOGAL | FLAG_CARD_SINGLETON_SCOPED | FLAG_CARD_LIMITED | FLAG_CARD_UNLIMITED);
192     }
193
194     int getCardinality() {
195         return cardinality;
196     }
197
198     private synchronized String JavaDoc getInstanceID() {
199         // make sure the instanceID has not reached the max
200
if (instanceID == Long.MAX_VALUE)
201             instanceID = 0;
202         // create a unique instance id
203
return getApplicationId() + "." + instanceID++; //$NON-NLS-1$
204
}
205 }
206
Popular Tags