Author: cziegeler
Date: Mon Jan 21 09:06:49 2008
New Revision: 613935

URL: http://svn.apache.org/viewvc?rev=613935&view=rev
Log:
Referenced services can come and go - always check if service is available 
before calling to avoid ugly NPEs

Modified:
    
incubator/sling/trunk/sling/event/src/main/java/org/apache/sling/event/impl/AbstractRepositoryEventHandler.java
    
incubator/sling/trunk/sling/event/src/main/java/org/apache/sling/event/impl/DistributingEventHandler.java
    
incubator/sling/trunk/sling/event/src/main/java/org/apache/sling/event/impl/JobEventHandler.java
    
incubator/sling/trunk/sling/event/src/main/java/org/apache/sling/event/impl/TimedEventHandler.java

Modified: 
incubator/sling/trunk/sling/event/src/main/java/org/apache/sling/event/impl/AbstractRepositoryEventHandler.java
URL: 
http://svn.apache.org/viewvc/incubator/sling/trunk/sling/event/src/main/java/org/apache/sling/event/impl/AbstractRepositoryEventHandler.java?rev=613935&r1=613934&r2=613935&view=diff
==============================================================================
--- 
incubator/sling/trunk/sling/event/src/main/java/org/apache/sling/event/impl/AbstractRepositoryEventHandler.java
 (original)
+++ 
incubator/sling/trunk/sling/event/src/main/java/org/apache/sling/event/impl/AbstractRepositoryEventHandler.java
 Mon Jan 21 09:06:49 2008
@@ -166,7 +166,11 @@
      */
     protected Session createSession()
     throws RepositoryException {
-        return this.repository.loginAdministrative(null);
+        final SlingRepository repo = this.repository;
+        if ( repo == null ) {
+            throw new RepositoryException("Repository is currently not 
available.");
+        }
+        return repo.loginAdministrative(null);
     }
 
     /**

Modified: 
incubator/sling/trunk/sling/event/src/main/java/org/apache/sling/event/impl/DistributingEventHandler.java
URL: 
http://svn.apache.org/viewvc/incubator/sling/trunk/sling/event/src/main/java/org/apache/sling/event/impl/DistributingEventHandler.java?rev=613935&r1=613934&r2=613935&view=diff
==============================================================================
--- 
incubator/sling/trunk/sling/event/src/main/java/org/apache/sling/event/impl/DistributingEventHandler.java
 (original)
+++ 
incubator/sling/trunk/sling/event/src/main/java/org/apache/sling/event/impl/DistributingEventHandler.java
 Mon Jan 21 09:06:49 2008
@@ -32,6 +32,7 @@
 import org.apache.jackrabbit.util.ISO8601;
 import org.apache.sling.event.EventUtil;
 import org.osgi.service.event.Event;
+import org.osgi.service.event.EventAdmin;
 
 /**
  * This event handler distributes events across an application cluster.
@@ -110,7 +111,12 @@
                 } else if ( info.nodePath != null) {
                     try {
                         final Node eventNode = (Node) 
this.session.getItem(info.nodePath);
-                        this.eventAdmin.postEvent(this.readEvent(eventNode));
+                        final EventAdmin localEA = this.eventAdmin;
+                        if ( localEA != null ) {
+                            localEA.postEvent(this.readEvent(eventNode));
+                        } else {
+                            this.logger.error("Unable to post event as no 
event admin is available.");
+                        }
                     } catch (Exception ex) {
                         this.logger.error("Exception during reading the event 
from the repository.", ex);
                     }

Modified: 
incubator/sling/trunk/sling/event/src/main/java/org/apache/sling/event/impl/JobEventHandler.java
URL: 
http://svn.apache.org/viewvc/incubator/sling/trunk/sling/event/src/main/java/org/apache/sling/event/impl/JobEventHandler.java?rev=613935&r1=613934&r2=613935&view=diff
==============================================================================
--- 
incubator/sling/trunk/sling/event/src/main/java/org/apache/sling/event/impl/JobEventHandler.java
 (original)
+++ 
incubator/sling/trunk/sling/event/src/main/java/org/apache/sling/event/impl/JobEventHandler.java
 Mon Jan 21 09:06:49 2008
@@ -41,6 +41,7 @@
 import org.apache.sling.event.JobStatusProvider;
 import org.osgi.service.component.ComponentContext;
 import org.osgi.service.event.Event;
+import org.osgi.service.event.EventAdmin;
 
 
 /**
@@ -385,9 +386,14 @@
             final Event jobEvent = this.getJobEvent(event, eventNode, 
lockToken);
             eventNode.setProperty(EventHelper.NODE_PROPERTY_PROCESSOR, 
this.applicationId);
             eventNode.save();
-            this.eventAdmin.sendEvent(jobEvent);
-            // do not unlock if sending was successful
-            unlock = false;
+            final EventAdmin localEA = this.eventAdmin;
+            if ( localEA != null ) {
+                localEA.sendEvent(jobEvent);
+                // do not unlock if sending was successful
+                unlock = false;
+            } else {
+                this.logger.error("Job event can't be sent as no event admin 
is available.");
+            }
         } catch (RepositoryException re) {
             // if an exception occurs, we just log
             this.logger.error("Exception during job processing.", re);

Modified: 
incubator/sling/trunk/sling/event/src/main/java/org/apache/sling/event/impl/TimedEventHandler.java
URL: 
http://svn.apache.org/viewvc/incubator/sling/trunk/sling/event/src/main/java/org/apache/sling/event/impl/TimedEventHandler.java?rev=613935&r1=613934&r2=613935&view=diff
==============================================================================
--- 
incubator/sling/trunk/sling/event/src/main/java/org/apache/sling/event/impl/TimedEventHandler.java
 (original)
+++ 
incubator/sling/trunk/sling/event/src/main/java/org/apache/sling/event/impl/TimedEventHandler.java
 Mon Jan 21 09:06:49 2008
@@ -66,9 +66,6 @@
     /** @scr.reference */
     protected Scheduler scheduler;
 
-    /** @scr.reference */
-    protected EventAdmin eventAdmin;
-
     /**
      * Start the repository session and add this handler as an observer
      * for new events created on other nodes.
@@ -228,14 +225,15 @@
      * @return
      */
     protected boolean processEvent(final Event event, final ScheduleInfo 
scheduleInfo) {
-        if ( this.scheduler != null ) {
+        final Scheduler localScheduler = this.scheduler;
+        if ( localScheduler != null ) {
             // is this a stop event?
             if ( scheduleInfo.isStopEvent() ) {
                 if ( this.logger.isDebugEnabled() ) {
                     this.logger.debug("Stopping timed event " + 
event.getProperty(EventUtil.PROPERTY_TIMED_EVENT_TOPIC) + "(" + 
scheduleInfo.jobId + ")");
                 }
                 try {
-                    this.scheduler.removeJob(scheduleInfo.jobId);
+                    localScheduler.removeJob(scheduleInfo.jobId);
                 } catch (NoSuchElementException nsee) {
                     // this can happen if the job is scheduled on another node
                     // so we can just ignore this
@@ -266,18 +264,18 @@
                     if ( this.logger.isDebugEnabled() ) {
                         this.logger.debug("Adding timed event " + 
config.get(JOB_TOPIC) + "(" + scheduleInfo.jobId + ")" + " with cron expression 
" + scheduleInfo.expression);
                     }
-                    this.scheduler.addJob(scheduleInfo.jobId, this, config, 
scheduleInfo.expression, false);
+                    localScheduler.addJob(scheduleInfo.jobId, this, config, 
scheduleInfo.expression, false);
                 } else if ( scheduleInfo.period != null ) {
                     if ( this.logger.isDebugEnabled() ) {
                         this.logger.debug("Adding timed event " + 
config.get(JOB_TOPIC) + "(" + scheduleInfo.jobId + ")" + " with period " + 
scheduleInfo.period);
                     }
-                    this.scheduler.addPeriodicJob(scheduleInfo.jobId, this, 
config, scheduleInfo.period, false);
+                    localScheduler.addPeriodicJob(scheduleInfo.jobId, this, 
config, scheduleInfo.period, false);
                 } else {
                     // then it must be date
                     if ( this.logger.isDebugEnabled() ) {
                         this.logger.debug("Adding timed event " + 
config.get(JOB_TOPIC) + "(" + scheduleInfo.jobId + ")" + " with date " + 
scheduleInfo.date);
                     }
-                    this.scheduler.fireJobAt(scheduleInfo.jobId, this, config, 
scheduleInfo.date);
+                    localScheduler.fireJobAt(scheduleInfo.jobId, this, config, 
scheduleInfo.date);
                 }
                 return true;
             } catch (Exception e) {
@@ -356,9 +354,10 @@
         final String topic = (String) 
context.getConfiguration().get(JOB_TOPIC);
         @SuppressWarnings("unchecked")
         final Dictionary<Object, Object> properties = (Dictionary<Object, 
Object>) context.getConfiguration().get(JOB_CONFIG);
-        if ( this.eventAdmin != null ) {
+        final EventAdmin ea = this.eventAdmin;
+        if ( ea != null ) {
             try {
-                this.eventAdmin.postEvent(new Event(topic, properties));
+                ea.postEvent(new Event(topic, properties));
             } catch (IllegalArgumentException iae) {
                 this.logger.error("Scheduled event has illegal topic: " + 
topic, iae);
             }


Reply via email to