2009/05/20 - Apache Shale has been retired.

For more information, please explore the Attic.

Coverage Report - org.apache.shale.dialog.base.AbstractDialogContextManager
 
Classes in this File Line Coverage Branch Coverage Complexity
AbstractDialogContextManager
0%
0/45
0%
0/10
2.875
 
 1  
 /*
 2  
  * Licensed to the Apache Software Foundation (ASF) under one or more
 3  
  * contributor license agreements.  See the NOTICE file distributed with
 4  
  * this work for additional information regarding copyright ownership.
 5  
  * The ASF licenses this file to you under the Apache License, Version 2.0
 6  
  * (the "License"); you may not use this file except in compliance with
 7  
  * the License.  You may obtain a copy of the License at
 8  
  *
 9  
  *      http://www.apache.org/licenses/LICENSE-2.0
 10  
  *
 11  
  * Unless required by applicable law or agreed to in writing, software
 12  
  * distributed under the License is distributed on an "AS IS" BASIS,
 13  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 14  
  * See the License for the specific language governing permissions and
 15  
  * limitations under the License.
 16  
  */
 17  
 
 18  
 package org.apache.shale.dialog.base;
 19  
 
 20  
 import java.util.ArrayList;
 21  
 import java.util.List;
 22  
 
 23  
 import javax.faces.context.FacesContext;
 24  
 import javax.servlet.http.HttpSessionBindingEvent;
 25  
 import javax.servlet.http.HttpSessionBindingListener;
 26  
 
 27  
 import org.apache.shale.dialog.Constants;
 28  
 import org.apache.shale.dialog.DialogContext;
 29  
 import org.apache.shale.dialog.DialogContextManager;
 30  
 import org.apache.shale.dialog.DialogContextManagerListener;
 31  
 import org.apache.shale.dialog.DialogLifecycleListener;
 32  
 
 33  
 /**
 34  
  * <p>Abstract base class for {@link DialogContextManager} implementations.
 35  
  * Provides listener registration and event firing convenience methods.
 36  
  * Subclasses are expected to be serializable.</p>
 37  
  *
 38  
  * @since 1.0.4
 39  
  */
 40  0
 public abstract class AbstractDialogContextManager
 41  
   implements DialogContextManager, HttpSessionBindingListener {
 42  
     
 43  
 
 44  
     // ------------------------------------------------------ Instance Variables
 45  
 
 46  
 
 47  
     /**
 48  
      * <p><code>List</code> of registered {@link DialogContextManagerListener}
 49  
      * instances.</p>
 50  
      */
 51  0
     private List listeners = new ArrayList();
 52  
 
 53  
 
 54  
     // -------------------------------------- HttpSessionBindingListener Methods
 55  
 
 56  
 
 57  
     /**
 58  
      * <p>Handle an instance of this class being bound into an HttpSession.</p>
 59  
      *
 60  
      * @param event HttpSessionBindingEvent to be handled
 61  
      */
 62  
     public void valueBound(HttpSessionBindingEvent event) {
 63  
 
 64  0
         DialogLifecycleListener listener = lifecycleListener();
 65  0
         if (listener != null) {
 66  0
             listener.onInit(this);
 67  
         }
 68  
 
 69  0
     }
 70  
 
 71  
 
 72  
     /**
 73  
      * <p>Handle an instance of this class being unbound from an HttpSession.</p>
 74  
      *
 75  
      * @param event HttpSessionBindingEvent to be handled
 76  
      */
 77  
     public void valueUnbound(HttpSessionBindingEvent event) {
 78  
 
 79  0
         DialogLifecycleListener listener = lifecycleListener();
 80  0
         if (listener != null) {
 81  0
             listener.onDestroy(this);
 82  
         }
 83  
 
 84  0
     }
 85  
 
 86  
 
 87  
     // --------------------------------------------------- Listener Registration
 88  
 
 89  
 
 90  
     /**
 91  
      * <p>Register a new {@link DialogContextManagerListener} instance.</p>
 92  
      *
 93  
      * @param listener The new listener instance to be registered
 94  
      */
 95  
     public void addDialogContextManagerListener(DialogContextManagerListener listener) {
 96  
 
 97  0
         if (listener == null) {
 98  0
             throw new IllegalArgumentException("Cannot register null DialogContextManagerListener");
 99  
         }
 100  
 
 101  0
         synchronized (listeners) {
 102  0
             if (listeners.contains(listener)) {
 103  0
                 throw new IllegalArgumentException("DialogContextManagerListener already registered");
 104  
             }
 105  0
             listener.setDialogContextManager(this); // attach self reference
 106  0
             listeners.add(listener);
 107  0
         }
 108  
 
 109  0
     }
 110  
 
 111  
 
 112  
     /**
 113  
      * <p>Return the set of currently registered {@link DialogContextManagerListener}s.
 114  
      * If there are no registered listeners, a zero-length array is returned.</p>
 115  
      */
 116  
     public DialogContextManagerListener[] getDialogContextManagerListeners() {
 117  
 
 118  0
         synchronized (listeners) {
 119  0
             return (DialogContextManagerListener[])
 120  
               listeners.toArray(new DialogContextManagerListener[listeners.size()]);
 121  0
         }
 122  
 
 123  
     }
 124  
 
 125  
 
 126  
     /**
 127  
      * <p>Deregister an existing {@link DialogContextManagerListener} instance.</p>
 128  
      *
 129  
      * @param listener The existing listener to be deregistered
 130  
      */
 131  
     public void removeDialogContextManagerListener(DialogContextManagerListener listener) {
 132  
 
 133  0
         if (listener == null) {
 134  0
             throw new IllegalArgumentException("Cannot remove null DialogContextManagerListener");
 135  
         }
 136  
 
 137  
         boolean removed;
 138  0
         synchronized (listeners) {
 139  0
             removed = listeners.remove(listener);
 140  0
         }
 141  0
         if (removed) {
 142  0
             listener.setDialogContextManager(null); // detach self reference
 143  
         }
 144  
 
 145  
 
 146  0
     }
 147  
 
 148  
 
 149  
     // ---------------------------------------------------- Event Firing Methods
 150  
 
 151  
 
 152  
     /**
 153  
      * <p>Fire an <code>onCreate()</code> event to all registered listeners.</p>
 154  
      *
 155  
      * @param context The {@link DialogContext} instance that has been created
 156  
      */
 157  
     protected void fireOnCreate(DialogContext context) {
 158  
 
 159  0
         DialogContextManagerListener[] listeners =
 160  
           getDialogContextManagerListeners();
 161  0
         for (int i = 0; i < listeners.length; i++) {
 162  0
             listeners[i].onCreate(context);
 163  
         }
 164  
 
 165  0
     }
 166  
 
 167  
 
 168  
     /**
 169  
      * <p>Fire an <code>onRemove()</code> event to all registered listeners.</p>
 170  
      *
 171  
      * @param context The {@link DialogContext} instance that has been removed
 172  
      */
 173  
     protected void fireOnRemove(DialogContext context) {
 174  
 
 175  0
         DialogContextManagerListener[] listeners =
 176  
           getDialogContextManagerListeners();
 177  0
         for (int i = 0; i < listeners.length; i++) {
 178  0
             listeners[i].onRemove(context);
 179  
         }
 180  
 
 181  0
     }
 182  
 
 183  
 
 184  
     // --------------------------------------------------------- Private Methods
 185  
 
 186  
 
 187  
     /**
 188  
      * <p>Return the {@link DialogLifecycleListener} for this application
 189  
      * (if any); otherwise, return <code>null</code>.</p>
 190  
      */
 191  
     private DialogLifecycleListener lifecycleListener() {
 192  
 
 193  0
         FacesContext context = FacesContext.getCurrentInstance();
 194  0
         if (context == null) {
 195  0
             return null;
 196  
         }
 197  0
         Object result =
 198  
           context.getApplication().getVariableResolver().
 199  
           resolveVariable(context, Constants.LIFECYCLE_ATTR);
 200  0
         if ((result != null) && (result instanceof DialogLifecycleListener)) {
 201  0
             return (DialogLifecycleListener) result;
 202  
         } else {
 203  0
             return null;
 204  
         }
 205  
 
 206  
     }
 207  
 
 208  
 
 209  
 }