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.visualweb.project.jsfloader;
043:
044: import org.netbeans.modules.visualweb.project.jsf.api.JsfProjectUtils;
045: import java.io.IOException;
046: import java.lang.reflect.Method;
047: import org.netbeans.api.java.loaders.JavaDataSupport;
048: import org.openide.ErrorManager;
049: import org.openide.filesystems.FileObject;
050: import org.openide.loaders.DataObject;
051: import org.openide.loaders.DataObjectExistsException;
052: import org.openide.loaders.MultiDataObject;
053: import org.openide.loaders.MultiFileLoader;
054: import org.openide.util.NbBundle;
055:
056: /**
057: * Data lodaer of JSF Java DataObjects, what means it loads java file,
058: * only in case there is corresponding jsp file, and maintains its loose
059: * reference, which is kind-of one part of virtual JSF DataObject (which
060: * we are simulating).
061: *
062: * @author Peter Zavadsky
063: */
064: public class JsfJavaDataLoader extends MultiFileLoader {
065:
066: static final long serialVersionUID = -5809935261731217882L;
067:
068: public static final String JAVA_EXTENSION = "java"; // NOI18N
069:
070: protected static Object dataObjectPool;
071: protected static Method dataObjectPoolFindMethod;
072:
073: static {
074: // EAT: All this to make sure I dont make a change to NB to make DataObjectPool public
075: try {
076: Class clazz = Class
077: .forName("org.openide.loaders.DataObjectPool");
078: Method getPoolMethod = clazz.getDeclaredMethod("getPOOL",
079: new Class[0]);
080: getPoolMethod.setAccessible(true);
081: dataObjectPool = getPoolMethod.invoke(null, new Object[0]);
082: dataObjectPoolFindMethod = clazz.getDeclaredMethod("find",
083: new Class[] { FileObject.class });
084: dataObjectPoolFindMethod.setAccessible(true);
085: } catch (Exception e) {
086: ErrorManager.getDefault().notify(ErrorManager.ERROR, e);
087: }
088: }
089:
090: public static DataObject DataObjectPoolFind(FileObject fileObject) {
091: // EAT: All this to make sure I dont make a change to NB to make DataObjectPool public
092: try {
093: Object result = dataObjectPoolFindMethod.invoke(
094: dataObjectPool, new Object[] { fileObject });
095: return (DataObject) result;
096: } catch (Exception e) {
097: ErrorManager.getDefault().notify(ErrorManager.ERROR, e);
098: return null;
099: }
100: }
101:
102: public JsfJavaDataLoader() {
103: // use String name instead of class.getName() for performance reasons
104: super (
105: "org.netbeans.modules.visualweb.project.jsfloader.JsfJavaDataObject"); // NOI18N
106: }
107:
108: protected String defaultDisplayName() {
109: return NbBundle.getMessage(JsfJavaDataLoader.class,
110: "PROP_JsfJavaDataLoader_Name");
111: }
112:
113: protected String actionsContext() {
114: return "Loaders/text/x-java/Actions/"; // NOI18N
115: }
116:
117: protected ThreadLocal findPrimaryFileThreadLocal = new ThreadLocal();
118:
119: protected FileObject findPrimaryFile(FileObject fo) {
120: if (fo.isFolder()) {
121: return null;
122: }
123:
124: FileObject primaryFile = findPrimaryJavaFile(fo);
125: if (primaryFile == null) {
126: return null;
127: }
128:
129: Object attrib = primaryFile
130: .getAttribute(JsfJavaDataObject.JSF_ATTRIBUTE);
131: if (attrib != null && attrib instanceof Boolean) {
132: if (((Boolean) attrib).booleanValue() == true)
133: return primaryFile;
134: else
135: return null;
136: }
137:
138: // XXX JsfProjectUtils.isJsfProjectFile() is very slow and should be revisited
139: // since this affects all loaded java files in NetBeans
140: boolean isTemplate = Utils.isTemplateFileObject(primaryFile);
141: if (!isTemplate
142: && !JsfProjectUtils.isJsfProjectFile(primaryFile)) {
143: return null;
144: }
145:
146: // It is most likely a Java file, however, we need to see if there is already a JsfJavaDataObject registered
147: // for this file object. There is a case where by in middle of refactoring, the JSP and the JAVA file are not
148: // linked as they should be, but there is still a JsfJavaDataObject registered uner this file object
149: // Duplicated in JsfJspDataObject
150: // XXX This causes some performance issues and should probably be removed
151: DataObject dataObject = DataObjectPoolFind(fo);
152: if (dataObject instanceof JsfJavaDataObject) {
153: return fo;
154: }
155: // We now know look for the jsp file
156: FileObject jspFile = Utils.findJspForJava(primaryFile);
157: if (jspFile == null) {
158: // If there is no backing file, then this is not a JSF data object.
159: return null;
160: }
161: // It has to be linked vice versa too.
162: if (primaryFile != Utils.findJavaForJsp(jspFile)) {
163: return null;
164: }
165:
166: if (!isTemplate) {
167: setJsfFileAttribute(primaryFile);
168: }
169:
170: return primaryFile;
171: }
172:
173: protected MultiDataObject createMultiObject(
174: final FileObject primaryFile)
175: throws DataObjectExistsException, IOException {
176: return new JsfJavaDataObject(primaryFile, this );
177: }
178:
179: @Override
180: protected MultiDataObject.Entry createPrimaryEntry(
181: MultiDataObject obj, FileObject primaryFile) {
182: return JavaDataSupport.createJavaFileEntry(obj, primaryFile);
183: }
184:
185: @Override
186: protected MultiDataObject.Entry createSecondaryEntry(
187: MultiDataObject obj, FileObject secondaryFile) {
188: throw new UnsupportedOperationException("Not supported yet.");
189: }
190:
191: private FileObject findPrimaryJavaFile(FileObject fo) {
192: if (fo.getExt().equals(JAVA_EXTENSION)) {
193: return fo;
194: }
195: return null;
196: }
197:
198: private void setJsfFileAttribute(FileObject fo) {
199: try {
200: fo.setAttribute(JsfJavaDataObject.JSF_ATTRIBUTE,
201: Boolean.TRUE);
202: } catch (IOException ex) {
203: ErrorManager.getDefault().notify(
204: ErrorManager.INFORMATIONAL, ex);
205: }
206: }
207: }
|