KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > apisupport > project > metainf > ServiceViewUpdater


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.apisupport.project.metainf;
21
22 import java.io.File JavaDoc;
23 import java.io.IOException JavaDoc;
24 import java.util.ArrayList JavaDoc;
25 import java.util.Iterator JavaDoc;
26 import java.util.List JavaDoc;
27 import java.util.Map JavaDoc;
28 import java.util.WeakHashMap JavaDoc;
29 import org.netbeans.api.project.Project;
30 import org.netbeans.api.project.ProjectManager;
31 import org.netbeans.api.project.ui.OpenProjects;
32 import org.netbeans.modules.apisupport.project.NbModuleProject;
33 import org.netbeans.modules.apisupport.project.spi.NbModuleProvider;
34 import org.netbeans.modules.apisupport.project.SuiteProvider;
35 import org.netbeans.modules.apisupport.project.suite.SuiteProject;
36 import org.netbeans.modules.apisupport.project.universe.ModuleList;
37 import org.netbeans.spi.project.SubprojectProvider;
38 import org.netbeans.spi.project.support.ant.PropertyEvaluator;
39 import org.openide.filesystems.FileObject;
40 import org.openide.filesystems.FileUtil;
41
42 /**
43  * It contains cached Services for suites. It should be better to move it to Suite Project.
44  */

45 final class ServiceViewUpdater {
46     private static Map JavaDoc <SuiteProject,List JavaDoc<Service>> suiteToServices = new WeakHashMap JavaDoc();
47     /** Creates a new instance of ServiceViewUpdater */
48     
49     /** Called if a service was update. It will refresh all openeden ServiceNodeHandlers
50      */

51     public static void serviceUpdated(Service service,ServiceNodeHandler handler) throws IOException JavaDoc {
52         // get suite
53
SuiteProject suite = getSuite(handler);
54         if (suite != null) {
55             SubprojectProvider spp = (SubprojectProvider) suite.getLookup().lookup(SubprojectProvider.class);
56             for (Iterator JavaDoc it = spp.getSubprojects().iterator(); it.hasNext() ; ) {
57                  ServiceNodeHandler handler2 = (ServiceNodeHandler) ((Project)it.next()).getLookup().lookup(ServiceNodeHandler.class);
58                  if (handler2 != null) {
59                     handler2.updateService(service);
60                  }
61             }
62         } else {
63             NbModuleProvider.NbModuleType type = ((NbModuleProvider) handler.getProject().getLookup().lookup(NbModuleProvider.class)).getModuleType();
64             if (type == NbModuleProvider.STANDALONE) {
65                 // standalone module
66
// update only this project
67
handler.updateService(service);
68             } else if (type == NbModuleProvider.NETBEANS_ORG) {
69                // nb org project type
70
// update all opened projects
71
File JavaDoc nbroot = ModuleList.findNetBeansOrg(FileUtil.toFile(handler.getProject().getProjectDirectory()));
72  
73                 FileObject nbAllfo = FileUtil.toFileObject(nbroot);
74                 if (nbAllfo != null) {
75                     Project projects[] = OpenProjects.getDefault().getOpenProjects();
76                     // iterate through all opened projects
77
for (int i = 0; i < projects.length; i++) {
78                         // for all opened projects for nb_all
79
if (projects[i] instanceof NbModuleProject) {
80                             NbModuleProject prj = (NbModuleProject) projects[i];
81                             NbModuleProvider.NbModuleType prjType = ((NbModuleProvider) handler.getProject().getLookup().lookup(NbModuleProvider.class)).getModuleType();
82                             if (prjType == NbModuleProvider.NETBEANS_ORG &&
83                                 FileUtil.isParentOf(nbAllfo,prj.getProjectDirectory())) {
84                                  ServiceNodeHandler handler2 = (ServiceNodeHandler) prj.getLookup().lookup(ServiceNodeHandler.class);
85                                  if (handler2 != null) {
86                                     handler2.updateService(service);
87                                  }
88
89                             }
90                         }
91                     }
92                 }
93             }
94         }
95     }
96     /** Get SuiteProject of NbModuleProject
97      */

98     private static SuiteProject getSuite(ServiceNodeHandler handler) throws IOException JavaDoc {
99         SuiteProject retPrj = null;
100         NbModuleProject p = handler.getProject();
101         NbModuleProvider.NbModuleType type = ((NbModuleProvider) p.getLookup().lookup(NbModuleProvider.class)).getModuleType();
102         if (type == NbModuleProvider.SUITE_COMPONENT) {
103             SuiteProvider suiteProv = (SuiteProvider) p.getLookup().lookup(SuiteProvider.class);
104             assert suiteProv != null : p;
105             File JavaDoc suiteDir = suiteProv.getSuiteDirectory();
106             if (suiteDir == null || !suiteDir.isDirectory()) {
107                 throw new IOException JavaDoc("Could not locate suite for " + p); // NOI18N
108
}
109             Project prj = ProjectManager.getDefault().findProject(FileUtil.toFileObject(suiteDir));
110             if (prj instanceof SuiteProject) {
111                 retPrj = (SuiteProject)prj;
112             }
113         }
114         return retPrj;
115     }
116     
117
118     /** @return services from platform and modules
119      */

120     static List JavaDoc getAllServices(ServiceNodeHandler serviceNodeHandler) throws IOException JavaDoc {
121         SuiteProject suite = getSuite(serviceNodeHandler);
122         if (suite != null) {
123             List JavaDoc <Service> services = (List JavaDoc) suiteToServices.get(suite);
124             if (services == null) {
125                 services = Service.getPlatfromServices(serviceNodeHandler.getProject());
126             }
127             SubprojectProvider subprojects = (SubprojectProvider)suite.getLookup().lookup(SubprojectProvider.class);
128             for (Iterator JavaDoc sIt = subprojects.getSubprojects().iterator() ; sIt.hasNext() ; ) {
129                 NbModuleProject project = (NbModuleProject)sIt.next();
130                 services.addAll(Service.getOnlyProjectServices(project));
131             }
132             suiteToServices.put(suite,services);
133             return services;
134         } else {
135             return Service.getPlatfromServices(serviceNodeHandler.getProject());
136         }
137     }
138     
139     /** @return subset of services which are in project of handler
140      */

141     static List JavaDoc<Service> filterServices(List JavaDoc services,ServiceNodeHandler handler) throws IOException JavaDoc {
142         if (getSuite(handler) != null) {
143             List JavaDoc<Service> allServices = getAllServices(handler);
144             List JavaDoc <Service> retList = new ArrayList JavaDoc();
145             String JavaDoc cnb = handler.getProject().getCodeNameBase();
146             for (int i = 0 ; i < allServices.size() ; i++) {
147                Service service = (Service) allServices.get(i);
148                if (service.getCodebase().equals(cnb)) {
149                    retList.add(service);
150                }
151             }
152             return retList;
153         } else {
154             return Service.getOnlyProjectServices(handler.getProject());
155         }
156     }
157 }
158
Popular Tags