1 11 12 package org.eclipse.equinox.internal.app; 13 14 import java.security.Guard ; 15 import java.security.GuardedObject ; 16 import java.util.HashMap ; 17 import java.util.Map ; 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 FILTER_PREFIX = "(&(objectclass=" + ApplicationDescriptor.class.getName() + ")(" + ApplicationDescriptor.APPLICATION_PID + "="; private static final String FILTER_POSTFIX = "))"; 30 private boolean recurring; 31 private String topic; 32 private String eventFilter; 33 private Map args; 34 private String appPid; 35 private String id; 36 private ServiceRegistration sr; 37 private ServiceTracker appTracker; 38 private boolean removed = false; 39 40 EclipseScheduledApplication(BundleContext context, String id, String appPid, Map args, String topic, String 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; 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 getScheduleId() { 52 return id; 53 } 54 55 String getAppPid() { 56 return appPid; 57 } 58 59 public synchronized String getTopic() { 60 if (removed) 61 throw new IllegalStateException (Messages.scheduled_app_removed); 62 return topic; 63 } 64 65 public synchronized String getEventFilter() { 66 if (removed) 67 throw new IllegalStateException (Messages.scheduled_app_removed); 68 return eventFilter; 69 } 70 71 public synchronized boolean isRecurring() { 72 if (removed) 73 throw new IllegalStateException (Messages.scheduled_app_removed); 74 return recurring; 75 } 76 77 public synchronized ApplicationDescriptor getApplicationDescriptor() { 78 if (removed) 79 throw new IllegalStateException (Messages.scheduled_app_removed); 80 return (ApplicationDescriptor) Activator.getService(appTracker); 81 } 82 83 public synchronized Map getArguments() { 84 if (removed) 85 throw new IllegalStateException (Messages.scheduled_app_removed); 86 return args == null ? null : new HashMap (args); 87 } 88 89 private Map getArguments(Event trigger) { 90 Map result = args == null ? new HashMap () : getArguments(); 91 result.put(TRIGGERING_EVENT, new GuardedObject (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 return; 115 desc.launch(getArguments(event)); 116 } catch (Exception e) { 117 String 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; } 121 if (!isRecurring()) 122 remove(); 123 } 124 125 synchronized void setServiceRegistration(ServiceRegistration sr) { 126 this.sr = sr; 127 if (removed) sr.unregister(); 129 } 130 131 135 public class TriggerGuard implements Guard { 136 String eventTopic; 137 138 public TriggerGuard(String topic) { 139 this.eventTopic = topic; 140 } 141 142 145 public void checkGuard(Object object) throws SecurityException { 146 SecurityManager sm = System.getSecurityManager(); 147 if (sm != null) 148 sm.checkPermission(new TopicPermission(eventTopic, TopicPermission.SUBSCRIBE)); 149 } 150 151 } 152 } 153 | Popular Tags |