001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.modules.apisupport.project.metainf;
043:
044: import java.io.File;
045: import java.io.IOException;
046: import java.util.ArrayList;
047: import java.util.Iterator;
048: import java.util.List;
049: import java.util.Map;
050: import java.util.WeakHashMap;
051: import org.netbeans.api.project.Project;
052: import org.netbeans.api.project.ProjectManager;
053: import org.netbeans.api.project.ui.OpenProjects;
054: import org.netbeans.modules.apisupport.project.NbModuleProject;
055: import org.netbeans.modules.apisupport.project.spi.NbModuleProvider;
056: import org.netbeans.modules.apisupport.project.SuiteProvider;
057: import org.netbeans.modules.apisupport.project.suite.SuiteProject;
058: import org.netbeans.modules.apisupport.project.universe.ModuleList;
059: import org.netbeans.spi.project.SubprojectProvider;
060: import org.openide.filesystems.FileObject;
061: import org.openide.filesystems.FileUtil;
062:
063: /**
064: * It contains cached Services for suites. It should be better to move it to Suite Project.
065: */
066: final class ServiceViewUpdater {
067: private static Map<SuiteProject, List<Service>> suiteToServices = new WeakHashMap<SuiteProject, List<Service>>();
068:
069: /** Creates a new instance of ServiceViewUpdater */
070:
071: /** Called if a service was update. It will refresh all openeden ServiceNodeHandlers
072: */
073: public static void serviceUpdated(Service service,
074: ServiceNodeHandler handler) throws IOException {
075: // get suite
076: SuiteProject suite = getSuite(handler);
077: if (suite != null) {
078: SubprojectProvider spp = suite.getLookup().lookup(
079: org.netbeans.spi.project.SubprojectProvider.class);
080: for (Iterator it = spp.getSubprojects().iterator(); it
081: .hasNext();) {
082: ServiceNodeHandler handler2 = ((org.netbeans.api.project.Project) it
083: .next())
084: .getLookup()
085: .lookup(
086: org.netbeans.modules.apisupport.project.metainf.ServiceNodeHandler.class);
087: if (handler2 != null) {
088: handler2.updateService(service);
089: }
090: }
091: } else {
092: NbModuleProvider.NbModuleType type = (handler.getProject()
093: .getLookup()
094: .lookup(org.netbeans.modules.apisupport.project.spi.NbModuleProvider.class))
095: .getModuleType();
096: if (type == NbModuleProvider.STANDALONE) {
097: // standalone module
098: // update only this project
099: handler.updateService(service);
100: } else if (type == NbModuleProvider.NETBEANS_ORG) {
101: // nb org project type
102: // update all opened projects
103: File nbroot = ModuleList.findNetBeansOrg(FileUtil
104: .toFile(handler.getProject()
105: .getProjectDirectory()));
106:
107: FileObject nbAllfo = FileUtil.toFileObject(nbroot);
108: if (nbAllfo != null) {
109: Project projects[] = OpenProjects.getDefault()
110: .getOpenProjects();
111: // iterate through all opened projects
112: for (int i = 0; i < projects.length; i++) {
113: // for all opened projects for nb_all
114: if (projects[i] instanceof NbModuleProject) {
115: NbModuleProject prj = (NbModuleProject) projects[i];
116: NbModuleProvider.NbModuleType prjType = (handler
117: .getProject().getLookup()
118: .lookup(org.netbeans.modules.apisupport.project.spi.NbModuleProvider.class))
119: .getModuleType();
120: if (prjType == NbModuleProvider.NETBEANS_ORG
121: && FileUtil.isParentOf(nbAllfo, prj
122: .getProjectDirectory())) {
123: ServiceNodeHandler handler2 = prj
124: .getLookup()
125: .lookup(
126: org.netbeans.modules.apisupport.project.metainf.ServiceNodeHandler.class);
127: if (handler2 != null) {
128: handler2.updateService(service);
129: }
130:
131: }
132: }
133: }
134: }
135: }
136: }
137: }
138:
139: /** Get SuiteProject of NbModuleProject
140: */
141: private static SuiteProject getSuite(ServiceNodeHandler handler)
142: throws IOException {
143: SuiteProject retPrj = null;
144: Project p = handler.getProject();
145: NbModuleProvider.NbModuleType type = p.getLookup().lookup(
146: NbModuleProvider.class).getModuleType();
147: if (type == NbModuleProvider.SUITE_COMPONENT) {
148: SuiteProvider suiteProv = p
149: .getLookup()
150: .lookup(
151: org.netbeans.modules.apisupport.project.SuiteProvider.class);
152: assert suiteProv != null : p;
153: File suiteDir = suiteProv.getSuiteDirectory();
154: if (suiteDir == null || !suiteDir.isDirectory()) {
155: throw new IOException("Could not locate suite for " + p); // NOI18N
156: }
157: Project prj = ProjectManager.getDefault().findProject(
158: FileUtil.toFileObject(suiteDir));
159: if (prj instanceof SuiteProject) {
160: retPrj = (SuiteProject) prj;
161: }
162: }
163: return retPrj;
164: }
165:
166: /** @return services from platform and modules
167: */
168: static List<Service> getAllServices(
169: ServiceNodeHandler serviceNodeHandler) throws IOException {
170: SuiteProject suite = getSuite(serviceNodeHandler);
171: if (suite != null) {
172: List<Service> services = suiteToServices.get(suite);
173: if (services == null) {
174: services = Service
175: .getPlatfromServices(serviceNodeHandler
176: .getProject());
177: }
178: SubprojectProvider subprojects = suite.getLookup().lookup(
179: org.netbeans.spi.project.SubprojectProvider.class);
180: for (Iterator sIt = subprojects.getSubprojects().iterator(); sIt
181: .hasNext();) {
182: NbModuleProject project = (NbModuleProject) sIt.next();
183: services
184: .addAll(Service.getOnlyProjectServices(project));
185: }
186: suiteToServices.put(suite, services);
187: return services;
188: } else {
189: return Service.getPlatfromServices(serviceNodeHandler
190: .getProject());
191: }
192: }
193:
194: /** @return subset of services which are in project of handler
195: */
196: static List<Service> filterServices(List services,
197: ServiceNodeHandler handler) throws IOException {
198: if (getSuite(handler) != null) {
199: List<Service> allServices = getAllServices(handler);
200: List<Service> retList = new ArrayList<Service>();
201: NbModuleProvider info = handler.getProject().getLookup()
202: .lookup(NbModuleProvider.class);
203: String cnb = info.getCodeNameBase();
204: for (int i = 0; i < allServices.size(); i++) {
205: Service service = allServices.get(i);
206: if (service.getCodebase().equals(cnb)) {
207: retList.add(service);
208: }
209: }
210: return retList;
211: } else {
212: return Service.getOnlyProjectServices(handler.getProject());
213: }
214: }
215: }
|