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-2007 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.compapp.javaee.sunresources;
043:
044: import java.io.File;
045: import java.io.IOException;
046: import java.lang.reflect.Method;
047: import java.util.ArrayList;
048: import java.util.Iterator;
049: import java.util.List;
050: import java.util.Set;
051: import java.util.logging.Logger;
052: import org.netbeans.api.java.project.JavaProjectConstants;
053: import org.netbeans.api.project.Project;
054: import org.netbeans.api.project.ProjectManager;
055: import org.netbeans.api.project.ProjectUtils;
056: import org.netbeans.api.project.SourceGroup;
057: import org.netbeans.api.project.Sources;
058: import org.netbeans.api.project.ant.AntArtifact;
059: import org.netbeans.modules.compapp.javaee.sunresources.tool.archive.ArchiveConstants;
060: import org.netbeans.spi.project.SubprojectProvider;
061: import org.netbeans.spi.project.support.ant.AntProjectHelper;
062: import org.netbeans.spi.project.support.ant.EditableProperties;
063: import org.netbeans.spi.project.support.ant.PropertyEvaluator;
064: import org.openide.DialogDisplayer;
065: import org.openide.ErrorManager;
066: import org.openide.NotifyDescriptor;
067: import org.openide.cookies.EditorCookie;
068: import org.openide.filesystems.FileObject;
069: import org.openide.loaders.DataObject;
070: import org.openide.util.Mutex;
071: import org.openide.util.MutexException;
072: import org.openide.util.NbBundle;
073:
074: /**
075: *
076: * @author echou
077: */
078: public class SunResourcesUtil {
079:
080: public static final String RES_DIR_PROPNAME = "resource.dir"; // NOI18N
081:
082: public static void addJavaEEResourceMetaData(
083: final Project jbiProject, final AntArtifact artifact) {
084: final AntProjectHelper aph = (AntProjectHelper) jbiProject
085: .getLookup().lookup(AntProjectHelper.class);
086: if (aph == null) {
087: NotifyDescriptor d = new NotifyDescriptor.Message(NbBundle
088: .getMessage(SunResourcesUtil.class,
089: "EXC_no_anthelper"),
090: NotifyDescriptor.ERROR_MESSAGE);
091: DialogDisplayer.getDefault().notify(d);
092: return;
093: }
094:
095: try {
096: ProjectManager.mutex().writeAccess(
097: new Mutex.ExceptionAction() {
098: public Object run() throws IOException {
099: EditableProperties ep = aph
100: .getProperties(AntProjectHelper.PROJECT_PROPERTIES_PATH);
101:
102: try {
103: Project p = artifact.getProject();
104: String resourceDirRelative = getResourceDirRelative(p);
105: String projName = ProjectUtils
106: .getInformation(p).getName();
107: String propName = "resource."
108: + projName; // NOI18N
109: String propValue = "${project."
110: + projName + "}/" // NOI18N
111: + resourceDirRelative;
112: ep.setProperty(propName, propValue);
113: } catch (Exception e) {
114: e.printStackTrace();
115: }
116:
117: aph
118: .putProperties(
119: AntProjectHelper.PROJECT_PROPERTIES_PATH,
120: ep);
121: ProjectManager.getDefault().saveProject(
122: jbiProject);
123:
124: return null;
125: }
126: });
127: } catch (MutexException e) {
128: ErrorManager.getDefault().notify(
129: (IOException) e.getException());
130: }
131: }
132:
133: public static void removeJavaEEResourceMetaData(
134: final Project jbiProject, final String projName) {
135: final AntProjectHelper aph = (AntProjectHelper) jbiProject
136: .getLookup().lookup(AntProjectHelper.class);
137: if (aph == null) {
138: NotifyDescriptor d = new NotifyDescriptor.Message(NbBundle
139: .getMessage(SunResourcesUtil.class,
140: "EXC_no_anthelper"),
141: NotifyDescriptor.ERROR_MESSAGE);
142: DialogDisplayer.getDefault().notify(d);
143: return;
144: }
145:
146: try {
147: ProjectManager.mutex().writeAccess(
148: new Mutex.ExceptionAction() {
149: public Object run() throws IOException {
150: EditableProperties ep = aph
151: .getProperties(AntProjectHelper.PROJECT_PROPERTIES_PATH);
152: String propName = "resource." + projName; // NOI18N
153: ep.remove(propName);
154: aph
155: .putProperties(
156: AntProjectHelper.PROJECT_PROPERTIES_PATH,
157: ep);
158: ProjectManager.getDefault().saveProject(
159: jbiProject);
160:
161: return null;
162: }
163: });
164: } catch (MutexException e) {
165: ErrorManager.getDefault().notify(
166: (IOException) e.getException());
167: }
168: }
169:
170: public static String scanForSunResources(Project p)
171: throws Exception {
172: // aggregate *.sun-resource files into memory here
173: ResourceAggregator resAggregator = new ResourceAggregator();
174: List<Project> projList = getSubProjects(p);
175: projList.add(p);
176: for (int n = 0; n < projList.size(); n++) {
177: Project currentP = projList.get(n);
178: FileObject resourceDirFO = getResourceDir(currentP);
179: FileObject[] children = resourceDirFO.getChildren();
180: for (int i = 0; i < children.length; i++) {
181: FileObject fo = children[i];
182: if (!fo.isFolder()
183: && fo.getExt().equalsIgnoreCase("sun-resource")) { // NOI18N
184: resAggregator.addResource(fo);
185: }
186: }
187: }
188:
189: return resAggregator.toSunResourcesXML();
190: }
191:
192: public static String scanForSunResources(String resourceFolder)
193: throws Exception {
194: // aggregate *.sun-resource files into memory here
195:
196: // Backward compatibility
197: if ((resourceFolder == null) || "".equals(resourceFolder)) {
198: Logger logger = Logger.getLogger(SunResourcesUtil.class
199: .getName());
200: logger.warning(NbBundle.getMessage(SunResourcesUtil.class,
201: "EXC_resource_config_missisng"));
202: return "";
203: }
204:
205: ResourceAggregator resAggregator = new ResourceAggregator();
206:
207: File f = new File(resourceFolder);
208: if (f.exists() && f.isDirectory()) {
209: File[] children = f.listFiles();
210: for (int i = 0; i < children.length; i++) {
211: File child = children[i];
212: if (child.isFile()
213: && child.getName().toLowerCase().endsWith(
214: "sun-resource")) { // NOI18N
215: resAggregator.addResource(child);
216: }
217: }
218: }
219: return resAggregator.toSunResourcesXML();
220: }
221:
222: public static String getResourceDirRelative(Project p)
223: throws Exception {
224: // default location of where *.sun-resource files are stored under project
225: String resourceDir = "setup"; // NOI18N
226:
227: // find out where *.sun-resource files are stored
228: AntProjectHelper aph = (AntProjectHelper) p.getLookup().lookup(
229: AntProjectHelper.class);
230: if (aph == null) {
231: // try again to use reflection to get AntProjectHelper
232: Method m = p.getClass().getDeclaredMethod(
233: "getAntProjectHelper"); // NOI18N
234: if (m != null) {
235: aph = (AntProjectHelper) m.invoke(p);
236: }
237: }
238:
239: if (aph != null) {
240: EditableProperties ep = aph
241: .getProperties(AntProjectHelper.PROJECT_PROPERTIES_PATH);
242: String propVal = ep.getProperty(RES_DIR_PROPNAME);
243: if (propVal != null) {
244: resourceDir = propVal;
245: }
246: }
247:
248: return resourceDir;
249: }
250:
251: public static FileObject getResourceDir(Project p) throws Exception {
252: // default location of where *.sun-resource files are stored under project
253: String resourceDir = "setup"; // NOI18N
254:
255: // find out where *.sun-resource files are stored
256: AntProjectHelper aph = (AntProjectHelper) p.getLookup().lookup(
257: AntProjectHelper.class);
258: if (aph == null) {
259: // try again to use reflection to get AntProjectHelper
260: Method m = p.getClass().getDeclaredMethod(
261: "getAntProjectHelper"); // NOI18N
262: if (m != null) {
263: aph = (AntProjectHelper) m.invoke(p);
264: }
265: }
266:
267: if (aph != null) {
268: EditableProperties ep = aph
269: .getProperties(AntProjectHelper.PROJECT_PROPERTIES_PATH);
270: PropertyEvaluator pe = aph.getStandardPropertyEvaluator();
271: String propVal = ep.getProperty(RES_DIR_PROPNAME);
272: if (propVal != null) {
273: propVal = pe.evaluate(propVal);
274: if (propVal != null) {
275: resourceDir = propVal;
276: }
277: }
278: }
279: FileObject resourceDirFO = p.getProjectDirectory()
280: .getFileObject(resourceDir);
281: if (resourceDirFO == null) {
282: resourceDirFO = p.getProjectDirectory().createFolder(
283: resourceDir);
284: }
285: return resourceDirFO;
286: }
287:
288: public static List<Project> getSubProjects(Project p) {
289: List<Project> list = new ArrayList<Project>();
290: SubprojectProvider spp = (SubprojectProvider) p.getLookup()
291: .lookup(SubprojectProvider.class);
292: if (spp != null) {
293: list.addAll(spp.getSubprojects());
294: }
295: return list;
296: }
297:
298: public static ArchiveConstants.ArchiveType getJavaEEProjectType(
299: Project proj) {
300: String projClassName = proj.getClass().getName();
301: if (projClassName.toLowerCase().indexOf("earproject") > -1) { // NOI18N
302: return ArchiveConstants.ArchiveType.EAR;
303: } else if (projClassName.toLowerCase().indexOf("ejbjarproject") > -1) { // NOI18N
304: return ArchiveConstants.ArchiveType.EJB;
305: } else if (projClassName.toLowerCase().indexOf("web.project") > -1) { // NOI18N
306: return ArchiveConstants.ArchiveType.WAR;
307: } else {
308: return ArchiveConstants.ArchiveType.UNKNOWN;
309: }
310: }
311:
312: public static FileObject getProjectDistJar(Project p,
313: String distPropName) throws Exception {
314: AntProjectHelper aph = null;
315: Method m = p.getClass()
316: .getDeclaredMethod("getAntProjectHelper"); // NOI18N
317: if (m != null) {
318: aph = (AntProjectHelper) m.invoke(p);
319: }
320: if (aph == null) {
321: throw new Exception(NbBundle.getMessage(
322: SunResourcesUtil.class, "EXC_no_anthelper2", p
323: .toString()));
324: }
325:
326: EditableProperties ep = aph
327: .getProperties(AntProjectHelper.PROJECT_PROPERTIES_PATH);
328: PropertyEvaluator pe = aph.getStandardPropertyEvaluator();
329: String propVal = ep.getProperty(distPropName);
330: if (propVal != null) {
331: propVal = pe.evaluate(propVal);
332: if (propVal != null) {
333: return p.getProjectDirectory().getFileObject(propVal);
334: }
335: }
336:
337: return null;
338: }
339:
340: // find the java source name under the project
341: // and open it.
342: public static void openSourceFile(final Project p, String javaName) {
343: assert p != null;
344:
345: // look in selected project node first
346: FileObject sourceFile = findSourceInProject(p, javaName);
347:
348: // look in dependent projects
349: if (sourceFile == null) {
350: SubprojectProvider spp = (SubprojectProvider) p.getLookup()
351: .lookup(SubprojectProvider.class);
352: if (spp != null) {
353: Set<? extends org.netbeans.api.project.Project> subs = spp
354: .getSubprojects();
355: for (Iterator<? extends Project> iter = subs.iterator(); iter
356: .hasNext();) {
357: Project subProj = iter.next();
358: sourceFile = findSourceInProject(subProj, javaName);
359: if (sourceFile != null) {
360: break;
361: }
362: }
363: }
364: }
365:
366: if (sourceFile == null) {
367: String msg = NbBundle.getMessage(SunResourcesUtil.class,
368: "EXC_srcfile_notfound", javaName);
369: DialogDisplayer.getDefault().notify(
370: new NotifyDescriptor.Message(msg));
371: } else {
372: // open the fileobject in editor
373: try {
374: DataObject dataObj = DataObject.find(sourceFile);
375: EditorCookie.Observable ec = (EditorCookie.Observable) dataObj
376: .getCookie(EditorCookie.Observable.class);
377: ec.open();
378: } catch (Exception e) {
379: e.printStackTrace();
380: }
381: }
382: }
383:
384: private static FileObject findSourceInProject(final Project p,
385: String javaName) {
386: Sources sources = ProjectUtils.getSources(p);
387: if (sources == null) {
388: return null;
389: }
390:
391: SourceGroup[] sgs = sources
392: .getSourceGroups(JavaProjectConstants.SOURCES_TYPE_JAVA);
393: for (int i = 0; i < sgs.length; i++) {
394: SourceGroup sg = sgs[i];
395: FileObject rootFolder = sg.getRootFolder();
396: FileObject javaFile = rootFolder.getFileObject(javaName);
397: if (javaFile != null) {
398: return javaFile;
399: }
400: }
401:
402: return null;
403: }
404: }
|