View Javadoc

1   /*
2    * Created on Nov 9, 2005 by turadg
3    *
4    * Copyright (c) 2005 Regents of the University of California (Regents). Created
5    * by TELS, Graduate School of Education, University of California at Berkeley.
6    *
7    * This software is distributed under the GNU Lesser General Public License, v2.
8    *
9    * Permission is hereby granted, without written agreement and without license
10   * or royalty fees, to use, copy, modify, and distribute this software and its
11   * documentation for any purpose, provided that the above copyright notice and
12   * the following two paragraphs appear in all copies of this software.
13   *
14   * REGENTS SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
15   * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16   * PURPOSE. THE SOFTWAREAND ACCOMPANYING DOCUMENTATION, IF ANY, PROVIDED
17   * HEREUNDER IS PROVIDED "AS IS". REGENTS HAS NO OBLIGATION TO PROVIDE
18   * MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
19   *
20   * IN NO EVENT SHALL REGENTS BE LIABLE TO ANY PARTY FOR DIRECT, INDIRECT,
21   * SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES, INCLUDING LOST PROFITS,
22   * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF
23   * REGENTS HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24   */
25  
26  package net.sf.sail.common.beansupport;
27  
28  import java.beans.DesignMode;
29  import java.beans.beancontext.BeanContext;
30  import java.beans.beancontext.BeanContextChildSupport;
31  import java.beans.beancontext.BeanContextServiceAvailableEvent;
32  import java.beans.beancontext.BeanContextServiceRevokedEvent;
33  import java.beans.beancontext.BeanContextServices;
34  import java.beans.beancontext.BeanContextServicesListener;
35  
36  import net.sf.sail.core.beans.Pod;
37  import net.sf.sail.core.util.SailBeanUtils;
38  
39  /**
40   * @author turadg
41   */
42  abstract public class SailBeanContextChildSupport extends
43  		BeanContextChildSupport implements DesignMode {
44  
45  	/**
46  	 * Converts standard BeanContextServiceAvailableEvents into our handier
47  	 * method calls
48  	 * 
49  	 * @author turadg
50  	 */
51  	class SailBeanContextServicesListener implements
52  			BeanContextServicesListener {
53  		public void serviceAvailable(BeanContextServiceAvailableEvent bcsae) {
54  			// bcsae.getSourceAsBeanContextServices() may seem useful but it may
55  			// be
56  			// a non-parent ancestor and thus can't be used.
57  			// So instead use this utility method to get a BeanContextServices
58  			// version of the parent.
59  			// Get a reference to the context
60  			BeanContextServices bcs = SailBeanUtils
61  					.findServicesContextOf(SailBeanContextChildSupport.this);
62  			consumeService(bcs, bcsae.getServiceClass());
63  		}
64  
65  		public void serviceRevoked(BeanContextServiceRevokedEvent bcsre) {
66  			// nothing
67  		}
68  
69  	}
70  
71  	SailBeanContextServicesListener sbcsl = new SailBeanContextServicesListener();
72  	private boolean designTime;
73  
74  	@Override
75  	protected void initializeBeanContextResources() {
76  		BeanContext bc = getBeanContext();
77  		// FIXME why should we have to check this?
78  		if (bc instanceof BeanContextServices) {
79  			BeanContextServices bcs = (BeanContextServices) bc;
80  			registerDesiredServices(bcs);
81  			bcs.addBeanContextServicesListener(sbcsl);
82  		} else
83  			throw new RuntimeException(
84  					this
85  							+ " initialized inside BeanContext not implementing BeanContextServices");
86  	}
87  
88  	/**
89  	 * This method will get called by the SailBeanContextServicesListener when
90  	 * it receives the serviceAvailable BeanContextServiceAvailableEvent
91  	 * 
92  	 * @param bcs
93  	 * @param serviceClass
94  	 */
95  	abstract protected void consumeService(BeanContextServices bcs,
96  			Class<Object> serviceClass);
97  
98  	/**
99  	 * This method is called in initializeBeanContextResources() to register
100 	 * what services this object wants to be made available to it
101 	 * 
102 	 * @param bcs
103 	 */
104 	abstract protected void registerDesiredServices(BeanContextServices bcs);
105 	
106 	public void setDesignTime(boolean designTime) {
107 		firePropertyChange(DesignMode.PROPERTYNAME, Boolean.valueOf(this.designTime), Boolean.valueOf(designTime));
108 		this.designTime = designTime;
109 	}
110 	
111 	public boolean isDesignTime() {
112 		return designTime;
113 	}
114 	
115 	protected boolean isInPod()
116 	{
117 		Object beanContext = getBeanContext();
118 		return beanContext instanceof Pod;
119 	}
120 	
121 }