01: /*
02: Licensed to the Apache Software Foundation (ASF) under one or more
03: contributor license agreements. See the NOTICE file distributed with
04: this work for additional information regarding copyright ownership.
05: The ASF licenses this file to You under the Apache License, Version 2.0
06: (the "License"); you may not use this file except in compliance with
07: the License. You may obtain a copy of the License at
08:
09: 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: $Header:$
18: */
19: package org.apache.beehive.controls.test.container;
20:
21: import java.io.InputStream;
22: import java.beans.beancontext.BeanContextChild;
23: import java.beans.beancontext.BeanContextServiceProvider;
24:
25: import org.apache.beehive.controls.runtime.bean.ControlContainerContext;
26: import org.apache.beehive.controls.runtime.bean.WebContextFactoryProvider;
27: import org.apache.beehive.controls.spi.context.ControlBeanContextFactory;
28:
29: /**
30: * ControlContainerContext implementation used to test controls in a standalone JVM.
31: */
32: public class ControlTestContainerContext extends
33: ControlContainerContext {
34:
35: private transient BeanContextServiceProvider _cbcFactoryProvider;
36:
37: public ControlTestContainerContext() {
38: //
39: // This sets the BeanContextServicesFactory instance on the ControlBeanContext and allows this
40: // CCC object to be created with a BeanContextServicesDelegate of the type returned by this factory
41: //
42: super (WebContextFactoryProvider.WEB_CONTEXT_BCS_FACTORY);
43: }
44:
45: /**
46: * Called by BeanContextSupport superclass during construction and deserialization to
47: * initialize subclass transient state
48: */
49: public void initialize() {
50: super .initialize();
51: // Register an *internal* service that is used to create ControlBeanContext objects for
52: // children of this control container
53: //
54: _cbcFactoryProvider = WebContextFactoryProvider.getProvider();
55: addService(ControlBeanContextFactory.class, _cbcFactoryProvider);
56: }
57:
58: /**
59: * Override ControlBeanContext.getService(). A control bean creates its bean context using the
60: * ControlBeanContextFactory service provided by this context. A control bean will attempt to create
61: * its context before adding its self to this context as a child. This creates a chicken/egg problem since
62: * only a child of a context may request a service from it.
63: *
64: * This method provides a way to crack the chicken/egg problem by first trying to get the service using the
65: * control bean context's getService() method, and if that call returns null and the requested service is
66: * the ControlBeanContextFactory then returning an instance of the service provider.
67: *
68: * @param serviceClass
69: * @param selector
70: * @return
71: */
72: public <T> T getService(Class<T> serviceClass, Object selector) {
73: T service = super .getService(serviceClass, selector);
74: if (service == null
75: && serviceClass.equals(ControlBeanContextFactory.class)) {
76: return (T) _cbcFactoryProvider.getService(this , this ,
77: serviceClass, selector);
78: }
79: return service;
80: }
81:
82: public InputStream getResourceAsStream(String name,
83: BeanContextChild bcc) {
84: ClassLoader classLoader = Thread.currentThread()
85: .getContextClassLoader();
86: InputStream inputStream = classLoader.getResourceAsStream(name);
87: return inputStream;
88: }
89: }
|