1 /*
2  * Copyright (c) 2002, 2007, Oracle and/or its affiliates. All rights reserved.
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * This code is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License version 2 only, as
7  * published by the Free Software Foundation.
8  *
9  * This code is distributed in the hope that it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12  * version 2 for more details (a copy is included in the LICENSE file that
13  * accompanied this code).
14  *
15  * You should have received a copy of the GNU General Public License version
16  * 2 along with this work; if not, write to the Free Software Foundation,
17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18  *
19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20  * or visit www.oracle.com if you need additional information or have any
21  * questions.
22  */
23 
24 /*
25  * @test
26  * @bug 4328406
27  * @summary Tests NPE while removing a BeanContextServices
28  * @author Mark Davidson
29  */
30 
31 import java.beans.beancontext.BeanContextChild;
32 import java.beans.beancontext.BeanContextChildSupport;
33 import java.beans.beancontext.BeanContextServiceProvider;
34 import java.beans.beancontext.BeanContextServices;
35 import java.beans.beancontext.BeanContextServicesSupport;
36 import java.util.Iterator;
37 
38 public class Test4328406 {
main(String[] args)39     public static void main(String[] args) {
40         for (int i = 0; i < 10; i++) {
41             BeanContextServices container = new BeanContextServicesSupport();
42             BeanContextChild ms1 = new MyService1();
43             BeanContextServices ms2 = new MyService2();
44             BeanContextChild mb = new MyBean();
45 
46             container.add(ms1);
47             container.add(ms2);
48             ms2.add(mb);
49 
50             // exception thrown here
51             container.remove(ms2);
52         }
53     }
54 }
55 
56 class MyService1 extends BeanContextChildSupport implements BeanContextServiceProvider {
initializeBeanContextResources()57     protected void initializeBeanContextResources() {
58         super.initializeBeanContextResources();
59 
60         BeanContextServices bcs = (BeanContextServices) getBeanContext();
61         bcs.addService(this.getClass(), this);
62     }
63 
getService(BeanContextServices bcs, Object requestor, Class serviceClass, Object serviceSelector)64     public Object getService(BeanContextServices bcs, Object requestor, Class serviceClass, Object serviceSelector) {
65         return this;
66     }
67 
releaseService(BeanContextServices bcs, Object requestor, Object service)68     public void releaseService(BeanContextServices bcs, Object requestor, Object
69             service) {
70     }
71 
getCurrentServiceSelectors(BeanContextServices bcs, Class serviceClass)72     public Iterator getCurrentServiceSelectors(BeanContextServices bcs, Class serviceClass) {
73         return null;
74     }
75 }
76 
77 class MyService2 extends BeanContextServicesSupport implements BeanContextServiceProvider {
initializeBeanContextResources()78     protected void initializeBeanContextResources() {
79         super.initializeBeanContextResources();
80 
81         BeanContextServicesSupport bcs = (BeanContextServicesSupport) getBeanContext();
82         try {
83             bcs.getService(this, this, MyService1.class, null, this);
84         }
85         catch (Exception exception) {
86             exception.printStackTrace();
87         }
88         bcs.addService(this.getClass(), this);
89     }
90 
releaseBeanContextResources()91     protected void releaseBeanContextResources() {
92         super.releaseBeanContextResources();
93 
94         BeanContextServices bcs = (BeanContextServices) getBeanContext();
95         bcs.revokeService(this.getClass(), this, true);
96     }
97 
getService(BeanContextServices bcs, Object requestor, Class serviceClass, Object serviceSelector)98     public Object getService(BeanContextServices bcs, Object requestor, Class serviceClass, Object serviceSelector) {
99         return this;
100     }
101 
releaseService(BeanContextServices bcs, Object requestor, Object service)102     public void releaseService(BeanContextServices bcs, Object requestor, Object service) {
103     }
104 
getCurrentServiceSelectors(BeanContextServices bcs, Class serviceClass)105     public Iterator getCurrentServiceSelectors(BeanContextServices bcs, Class serviceClass) {
106         return null;
107     }
108 }
109 
110 class MyBean extends BeanContextChildSupport {
initializeBeanContextResources()111     protected void initializeBeanContextResources() {
112         super.initializeBeanContextResources();
113 
114         BeanContextServices bcs = (BeanContextServices) getBeanContext();
115         try {
116             bcs.getService(this, this, MyService1.class, null, this);
117         }
118         catch (Exception exception) {
119             exception.printStackTrace();
120         }
121         try {
122             bcs.getService(this, this, MyService2.class, null, this);
123         }
124         catch (Exception exception) {
125             exception.printStackTrace();
126         }
127     }
128 }
129