KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 2005, 2006 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.Guard JavaDoc;
15 import java.security.GuardedObject JavaDoc;
16 import java.util.HashMap JavaDoc;
17 import java.util.Map JavaDoc;
18 import org.eclipse.osgi.framework.log.FrameworkLogEntry;
19 import org.eclipse.osgi.util.NLS;
20 import org.osgi.framework.*;
21 import org.osgi.service.application.ApplicationDescriptor;
22 import org.osgi.service.application.ScheduledApplication;
23 import org.osgi.service.event.*;
24 import org.osgi.util.tracker.ServiceTracker;
25
26 public class EclipseScheduledApplication implements ScheduledApplication, EventHandler {
27     private static final String JavaDoc FILTER_PREFIX = "(&(objectclass=" + ApplicationDescriptor.class.getName() + ")(" + ApplicationDescriptor.APPLICATION_PID + "="; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
28
private static final String JavaDoc FILTER_POSTFIX = "))"; //$NON-NLS-1$
29

30     private boolean recurring;
31     private String JavaDoc topic;
32     private String JavaDoc eventFilter;
33     private Map JavaDoc args;
34     private String JavaDoc appPid;
35     private String JavaDoc id;
36     private ServiceRegistration sr;
37     private ServiceTracker appTracker;
38     private boolean removed = false;
39
40     EclipseScheduledApplication(BundleContext context, String JavaDoc id, String JavaDoc appPid, Map JavaDoc args, String JavaDoc topic, String JavaDoc eventFilter, boolean recurring) throws InvalidSyntaxException {
41         this.id = id;
42         this.appPid = appPid;
43         this.args = args;
44         this.topic = topic == null || topic.trim().equals("") || topic.trim().equals("*") ? null : topic; //$NON-NLS-1$ //$NON-NLS-2$
45
this.eventFilter = eventFilter;
46         this.recurring = recurring;
47         appTracker = new ServiceTracker(context, context.createFilter(FILTER_PREFIX + appPid + FILTER_POSTFIX), null);
48         Activator.openTracker(appTracker, false);
49     }
50
51     public String JavaDoc getScheduleId() {
52         return id;
53     }
54
55     String JavaDoc getAppPid() {
56         return appPid;
57     }
58
59     public synchronized String JavaDoc getTopic() {
60         if (removed)
61             throw new IllegalStateException JavaDoc(Messages.scheduled_app_removed);
62         return topic;
63     }
64
65     public synchronized String JavaDoc getEventFilter() {
66         if (removed)
67             throw new IllegalStateException JavaDoc(Messages.scheduled_app_removed);
68         return eventFilter;
69     }
70
71     public synchronized boolean isRecurring() {
72         if (removed)
73             throw new IllegalStateException JavaDoc(Messages.scheduled_app_removed);
74         return recurring;
75     }
76
77     public synchronized ApplicationDescriptor getApplicationDescriptor() {
78         if (removed)
79             throw new IllegalStateException JavaDoc(Messages.scheduled_app_removed);
80         return (ApplicationDescriptor) Activator.getService(appTracker);
81     }
82
83     public synchronized Map JavaDoc getArguments() {
84         if (removed)
85             throw new IllegalStateException JavaDoc(Messages.scheduled_app_removed);
86         return args == null ? null : new HashMap JavaDoc(args);
87     }
88
89     private Map JavaDoc getArguments(Event trigger) {
90         Map JavaDoc result = args == null ? new HashMap JavaDoc() : getArguments();
91         result.put(TRIGGERING_EVENT, new GuardedObject JavaDoc(trigger, new TriggerGuard(trigger.getTopic())));
92         return result;
93     }
94
95     public synchronized void remove() {
96         if (removed)
97             return;
98         removed = true;
99         AppPersistence.removeScheduledApp(this);
100         if (sr != null)
101             sr.unregister();
102         sr = null;
103         appTracker.close();
104     }
105
106     public synchronized void handleEvent(Event event) {
107         try {
108             if (removed)
109                 return;
110             ApplicationDescriptor desc = getApplicationDescriptor();
111             if (desc == null)
112                 // in this case the application descriptor was removed;
113
// we must return and keep the scheduled app incase the application comes back
114
return;
115             desc.launch(getArguments(event));
116         } catch (Exception JavaDoc e) {
117             String JavaDoc message = NLS.bind(Messages.scheduled_app_launch_error, sr);
118             Activator.log(new FrameworkLogEntry(Activator.PI_APP, FrameworkLogEntry.WARNING, 0, message, 0, e, null));
119             return; // return here to avoid removing non-recurring apps when an error occurs
120
}
121         if (!isRecurring())
122             remove();
123     }
124
125     synchronized void setServiceRegistration(ServiceRegistration sr) {
126         this.sr = sr;
127         if (removed) // just incase we were removed before the sr was set
128
sr.unregister();
129     }
130
131     /*
132      * This is used to guard the event topic argument which is passed to an application
133      * when we are launching it from a scheduling.
134      */

135     public class TriggerGuard implements Guard JavaDoc {
136         String JavaDoc eventTopic;
137
138         public TriggerGuard(String JavaDoc topic) {
139             this.eventTopic = topic;
140         }
141
142         /*
143          * does the proper TopicPermission check for the event topic
144          */

145         public void checkGuard(Object JavaDoc object) throws SecurityException JavaDoc {
146             SecurityManager JavaDoc sm = System.getSecurityManager();
147             if (sm != null)
148                 sm.checkPermission(new TopicPermission(eventTopic, TopicPermission.SUBSCRIBE));
149         }
150
151     }
152 }
153
Popular Tags