--- a/ide.ergonomics/src/org/netbeans/modules/ide/ergonomics/fod/FeatureProjectFactory.java Fri Dec 19 15:16:27 2008 +0100 +++ a/ide.ergonomics/src/org/netbeans/modules/ide/ergonomics/fod/FeatureProjectFactory.java Mon Dec 22 11:23:03 2008 +0100 @@ -115,7 +115,6 @@ @Override protected void projectOpened() { RequestProcessor.getDefault ().post (this, 0, Thread.NORM_PRIORITY).waitFinished (); - ProjectOpenedHook hook; if (success) { try { state.notifyDeleted(); @@ -124,12 +123,7 @@ throw new IllegalStateException("New project shall be found! " + p); // NOI18N } delegate.associate(p); - - hook = p.getLookup().lookup(ProjectOpenedHook.class); - Method m = ProjectOpenedHook.class.getDeclaredMethod("projectOpened"); // NOI18N - m.setAccessible(true); - m.invoke(hook); - } catch (Exception ex) { + } catch (IOException ex) { Exceptions.printStackTrace(ex); } } --- a/projectui/src/org/netbeans/modules/project/ui/OpenProjectList.java Fri Dec 19 15:16:27 2008 +0100 +++ a/projectui/src/org/netbeans/modules/project/ui/OpenProjectList.java Mon Dec 22 11:23:03 2008 +0100 @@ -60,6 +60,7 @@ import java.util.Collections; import java.util.Comparator; import java.util.HashMap; +import java.util.HashSet; import java.util.Iterator; import java.util.LinkedHashSet; import java.util.LinkedList; @@ -1005,22 +1006,34 @@ private static boolean notifyOpened(Project p) { boolean ok = true; - for (Iterator i = p.getLookup().lookupAll(ProjectOpenedHook.class).iterator(); i.hasNext(); ) { - ProjectOpenedHook hook = (ProjectOpenedHook) i.next(); - - try { - ProjectOpenedTrampoline.DEFAULT.projectOpened(hook); - } catch (RuntimeException e) { - LOGGER.log(Level.WARNING, null, e); - // Do not try to call its close hook if its open hook already failed: - INSTANCE.openProjects.remove(p); - INSTANCE.removeModuleInfo(p); - ok = false; - } catch (Error e) { - LOGGER.log(Level.WARNING, null, e); - INSTANCE.openProjects.remove(p); - INSTANCE.removeModuleInfo(p); - ok = false; + Set notified = new HashSet(); + for (;;) { + int cnt = 0; + for (Iterator i = p.getLookup().lookupAll(ProjectOpenedHook.class).iterator(); i.hasNext(); ) { + ProjectOpenedHook hook = (ProjectOpenedHook) i.next(); + if (!notified.add(hook)) { + continue; + } + cnt++; + + try { + ProjectOpenedTrampoline.DEFAULT.projectOpened(hook); + } catch (RuntimeException e) { + LOGGER.log(Level.WARNING, null, e); + // Do not try to call its close hook if its open hook already failed: + INSTANCE.openProjects.remove(p); + INSTANCE.removeModuleInfo(p); + ok = false; + } catch (Error e) { + LOGGER.log(Level.WARNING, null, e); + INSTANCE.openProjects.remove(p); + INSTANCE.removeModuleInfo(p); + ok = false; + } + } + + if (cnt == 0) { + break; } } return ok; --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ 6c6e7bf5b156 Mon Dec 22 11:23:03 2008 +0100 @@ -0,0 +1,122 @@ +/* + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. + * + * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved. + * + * The contents of this file are subject to the terms of either the GNU + * General Public License Version 2 only ("GPL") or the Common + * Development and Distribution License("CDDL") (collectively, the + * "License"). You may not use this file except in compliance with the + * License. You can obtain a copy of the License at + * http://www.netbeans.org/cddl-gplv2.html + * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the + * specific language governing permissions and limitations under the + * License. When distributing the software, include this License Header + * Notice in each file and include the License file at + * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this + * particular file as subject to the "Classpath" exception as provided + * by Sun in the GPL Version 2 section of the License file that + * accompanied this code. If applicable, add the following below the + * License Header, with the fields enclosed by brackets [] replaced by + * your own identifying information: + * "Portions Copyrighted [year] [name of copyright owner]" + * + * If you wish your version of this file to be governed by only the CDDL + * or only the GPL Version 2, indicate your decision by adding + * "[Contributor] elects to include this software in this distribution + * under the [CDDL or GPL Version 2] license." If you do not indicate a + * single choice of license, a recipient has the option to distribute + * your version of this file under either the CDDL, the GPL Version 2 or + * to extend the choice of license to its licensees as provided above. + * However, if you add GPL Version 2 code and therefore, elected the GPL + * Version 2 license, then the option applies only if the new code is + * made subject to such option by the copyright holder. + * + * Contributor(s): + * + * Portions Copyrighted 2007 Sun Microsystems, Inc. + */ + +package org.netbeans.modules.project.ui; + +import org.netbeans.api.project.Project; +import org.netbeans.api.project.ProjectManager; +import org.netbeans.junit.MockServices; +import org.netbeans.junit.NbTestCase; +import org.netbeans.modules.project.ui.actions.TestSupport; +import org.netbeans.modules.project.ui.actions.TestSupport.TestProject; +import org.netbeans.spi.project.ui.ProjectOpenedHook; +import org.openide.filesystems.FileObject; +import org.openide.filesystems.FileUtil; +import org.openide.util.Lookup; +import org.openide.util.lookup.Lookups; + +/** + * + * @author Jaroslav Tulach + */ +public class OpenProjectHookAddedWhileOpenTest extends NbTestCase { + private TestProjectOpenedHookImpl hook2; + private TestProjectOpenedHookImpl hook1; + private TestProject project; + public OpenProjectHookAddedWhileOpenTest(String testName) { + super(testName); + } + + @Override + protected void setUp() throws Exception { + clearWorkDir(); + + MockServices.setServices(TestSupport.TestProjectFactory.class); + + FileObject workDir = FileUtil.toFileObject(getWorkDir()); + assertNotNull(workDir); + + FileObject prj = TestSupport.createTestProject(workDir, "prj0"); + project = (TestProject)ProjectManager.getDefault ().findProject (prj); + assertNotNull("Project found", project); + + hook1 = new TestProjectOpenedHookImpl(project); + hook2 = new TestProjectOpenedHookImpl(project); + + Lookup initialLookup = Lookups.singleton(hook1); + hook1.newLookup = Lookups.fixed(hook1, hook2); + + project.setLookup(initialLookup); + } + + public void testBehaviourOfProjectsLogicNode() throws Exception { + OpenProjectList.getDefault().open(project); + + assertEquals("First hook opened once", 1, hook1.cnt); + assertEquals("Second hook opened once", 1, hook2.cnt); + + OpenProjectList.getDefault().close(new Project[] { project }, true); + + assertEquals("First hook closed once", 1, hook1.close); + assertEquals("Second hook closed once", 1, hook2.close); + } + + private static class TestProjectOpenedHookImpl extends ProjectOpenedHook { + private final TestProject project; + Lookup newLookup; + int cnt; + int close; + + public TestProjectOpenedHookImpl(TestProject p) { + this.project = p; + } + + protected void projectClosed() { + close++; + } + + protected void projectOpened() { + cnt++; + if (newLookup != null) { + project.setLookup(newLookup); + } + } + + } +}