KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > web > core > jsploader > JspDataObject


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.web.core.jsploader;
21
22 import java.beans.PropertyChangeListener JavaDoc;
23 import java.beans.PropertyChangeEvent JavaDoc;
24 import java.io.IOException JavaDoc;
25 import java.lang.ref.WeakReference JavaDoc;
26 import java.nio.charset.Charset JavaDoc;
27 import java.util.Date JavaDoc;
28 import java.util.EventListener JavaDoc;
29
30 import org.openide.*;
31 import org.openide.cookies.EditorCookie;
32 import org.openide.cookies.SaveCookie;
33 import org.openide.filesystems.*;
34 import org.openide.loaders.*;
35 import org.openide.util.*;
36 import org.openide.nodes.Node;
37 import org.openide.nodes.CookieSet;
38
39 import org.netbeans.modules.web.core.QueryStringCookie;
40 import org.netbeans.modules.web.core.WebExecSupport;
41
42 /** Object that provides main functionality for internet data loader.
43  *
44  * @author Petr Jiricka
45  */

46 public class JspDataObject extends MultiDataObject implements QueryStringCookie {
47     
48     public static final String JavaDoc EA_JSP_ERRORPAGE = "jsp_errorpage"; // NOI18N
49
// property for the servlet dataobject corresponding to this page
50
public static final String JavaDoc PROP_SERVLET_DATAOBJECT = "servlet_do"; // NOI18N
51
public static final String JavaDoc PROP_CONTENT_LANGUAGE = "contentLanguage"; // NOI18N
52
public static final String JavaDoc PROP_SCRIPTING_LANGUAGE = "scriptingLanguage"; // NOI18N
53
public static final String JavaDoc PROP_ENCODING = "encoding"; // NOI18N
54
public static final String JavaDoc PROP_SERVER_CHANGE = "PROP_SERVER_CHANGE";// NOI18N
55
public static final String JavaDoc PROP_REQUEST_PARAMS = "PROP_REQUEST_PARAMS"; //NOI18N
56

57     static final String JavaDoc ATTR_FILE_ENCODING = "Content-Encoding"; // NOI18N
58

59     transient private EditorCookie servletEdit;
60     transient protected JspServletDataObject servletDataObject;
61     // it is guaranteed that if servletDataObject != null, then this is its
62
// last modified date at the time of last refresh
63
transient private Date JavaDoc servletDataObjectDate;
64     transient private CompileData compileData;
65     transient private boolean firstStart;
66     transient private Listener JavaDoc listener;
67     transient private BaseJspEditorSupport editorSupport;
68     transient final private static boolean debug = false;
69     
70     public JspDataObject(FileObject pf, final UniFileLoader l) throws DataObjectExistsException {
71         super(pf, l);
72         CookieSet cookies = getCookieSet();
73         initialize();
74     }
75     
76     // Public accessibility for e.g. JakartaServerPlugin.
77
// [PENDING] Handle this more nicely.
78
public org.openide.nodes.CookieSet getCookieSet0() {
79         return super.getCookieSet();
80     }
81     
82     public Node.Cookie getCookie(Class JavaDoc type) {
83         if (type.isAssignableFrom(BaseJspEditorSupport.class)) {
84             return getJspEditorSupport();
85         }
86         return super.getCookie(type);
87     }
88     
89     protected org.openide.nodes.Node createNodeDelegate() {
90         return new JspNode(this);
91     }
92     
93     private synchronized BaseJspEditorSupport getJspEditorSupport() {
94         if (editorSupport == null) {
95             editorSupport = new BaseJspEditorSupport(this);
96         }
97         return editorSupport;
98     }
99     
100     protected EditorCookie createServletEditor() {
101         return new ServletEditor(this);
102     }
103     
104     public synchronized CompileData getPlugin() {
105         if (compileData == null) {
106             if ( firstStart ) {
107                 firstStart=false;
108             }
109             compileData = new CompileData(this);
110             checkRefreshServlet();
111         }
112         return compileData;
113     }
114     
115     /** Invalidates the current copy of server plugin for this JSP.
116      * @param reload true if the new version of the plugin should be loaded.
117      */

118     public synchronized void refreshPlugin(boolean reload) {
119         //System.out.println("REFRESHING PLUGIN " + reload);
120
compileData = null;
121         if (reload)
122             getPlugin();
123     }
124     
125     public void refreshPlugin() {
126         refreshPlugin(true);
127     }
128     
129     public JspServletDataObject getServletDataObject() {
130         // force registering the servlet
131
getPlugin();
132         return servletDataObject;
133     }
134     
135     /** Returns the MIME type of the content language for this page set in this file's attributes.
136      * If nothing is set, defaults to 'text/html'.
137      */

138     public String JavaDoc getContentLanguage() {
139         return "text/html"; // NOI18N
140
}
141     
142     /** Returns the MIME type of the scripting language for this page set in this file's attributes.
143      * If nothing is set, defaults to 'text/x-java'.
144      */

145     public String JavaDoc getScriptingLanguage() {
146         return "text/x-java"; // NOI18N
147
}
148     
149     public String JavaDoc getFileEncoding(boolean forceParse, boolean useEditor) {
150         //read the encoding property and if not empty return it
151
String JavaDoc encoding = (String JavaDoc)getPrimaryFile().getAttribute(PROP_ENCODING);
152         if(encoding != null) {
153             return encoding;
154         } else {
155             TagLibParseSupport tlps = (TagLibParseSupport)getCookie(TagLibParseSupport.class);
156             return tlps.getCachedOpenInfo(forceParse, useEditor).getEncoding();
157         }
158     }
159     
160     public void setFileEncoding(String JavaDoc encoding) {
161         encoding = encoding.trim();
162         if(encoding.length() == 0) {
163             encoding = null; //clear the property
164
}
165         try {
166             getPrimaryFile().setAttribute(PROP_ENCODING, encoding);
167         } catch(IOException JavaDoc e) {
168             ErrorManager.getDefault().notify(ErrorManager.WARNING, e);
169         }
170     }
171     
172     private static final String JavaDoc CORRECT_WINDOWS_31J = "windows-31j";
173     private static final String JavaDoc CORRECT_EUC_JP = "EUC-JP";
174     private static final String JavaDoc CORRECT_GB2312 = "GB2312";
175     private static final String JavaDoc CORRECT_BIG5 = "BIG5";
176     
177     private static String JavaDoc canonizeEncoding(String JavaDoc encodingAlias) {
178         
179         // canonic name first
180
if (Charset.isSupported(encodingAlias)) {
181             Charset JavaDoc cs = Charset.forName(encodingAlias);
182             encodingAlias = cs.name();
183         }
184         
185         // this is not supported on JDK 1.4.1
186
if (encodingAlias.equalsIgnoreCase("MS932")) {
187             return CORRECT_WINDOWS_31J;
188         }
189         // this is not a correct charset by http://www.iana.org/assignments/character-sets
190
if (encodingAlias.equalsIgnoreCase("euc-jp-linux")) {
191             return CORRECT_EUC_JP;
192         }
193         // chinese encodings that must be adjusted
194
if (encodingAlias.equalsIgnoreCase("EUC-CN")) {
195             return CORRECT_GB2312;
196         }
197         if (encodingAlias.equalsIgnoreCase("GBK")) {
198             return CORRECT_GB2312;
199         }
200         if (encodingAlias.equalsIgnoreCase("GB18030")) {
201             return CORRECT_GB2312;
202         }
203         if (encodingAlias.equalsIgnoreCase("EUC-TW")) {
204             return CORRECT_BIG5;
205         }
206         
207         return encodingAlias;
208     }
209     
210     private void initialize() {
211         firstStart = true;
212         listener = new Listener JavaDoc();
213         listener.register(getPrimaryFile());
214         refreshPlugin(false);
215     }
216     
217     /** Updates classFileData, servletDataObject, servletEdit
218      * This does not need to be synchronized, because the calling method
219      * getPlugin() is synchronized.
220      */

221     private void checkRefreshServlet() {
222         
223         final DataObject oldServlet = servletDataObject;
224         if (debug)
225             System.out.println("refreshing servlet, old = " + oldServlet); // NOI18N
226

227         // dataobject
228
try {
229             FileObject servletFileObject = updateServletFileObject();
230             if(debug) System.out.println("refreshing servlet, new servletFile = " + servletFileObject); // NOI18N
231
if (servletFileObject != null) {
232                 // if the file has not changed, just return
233
if ((oldServlet != null) &&
234                         (oldServlet.getPrimaryFile() == servletFileObject) &&
235                         (servletFileObject.lastModified().equals(servletDataObjectDate)))
236                     return; // performance
237

238                 // set the origin JSP page
239
JspServletDataObject.setSourceJspPage(servletFileObject, this);
240                 
241                 //set the preferred DataLoader
242
DataLoaderPool.setPreferredLoader(servletFileObject, DataLoader.getLoader(JspServletDataLoader.class));
243                 
244                 
245                 // now the loader should recognize that this servlet was generated from a JSP
246
DataObject dObj= DataObject.find(servletFileObject);
247                 if (debug) {
248                     System.out.println("checkRefr::servletDObj=" + // NOI18N
249
((dObj == null) ? "null" : dObj.getClass().getName()) + // NOI18N
250
"/" + dObj); // NOI18N
251
}
252                 /*if (!(dObj instanceof JspServletDataObject)) {
253                     // need to re-recognize
254                     dObj = rerecognize(dObj);
255                 }*/

256                 if (dObj instanceof JspServletDataObject) {
257                     servletDataObject = (JspServletDataObject)dObj;
258                     servletDataObjectDate = dObj.getPrimaryFile().lastModified();
259                 }
260                 // set the encoding of the generated servlet
261
String JavaDoc encoding = compileData.getServletEncoding();
262                 if (encoding != null) {
263                     if (!"".equals(encoding)) {
264                         try {
265                             Charset.forName(encoding);
266                         } catch (IllegalArgumentException JavaDoc ex) {
267                             IOException JavaDoc t = new IOException JavaDoc(
268                                     NbBundle.getMessage(JspDataObject.class, "FMT_UnsupportedEncoding", encoding)
269                                     );
270                             ErrorManager.getDefault().annotate(t, ex);
271                             ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, t);
272                         }
273                     } else
274                         encoding = null;
275                 }
276                 try {
277                     // actually set the encoding
278
servletFileObject.setAttribute(ATTR_FILE_ENCODING, encoding); //NOI18N
279
} catch (IOException JavaDoc ex) {
280                     ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, ex);
281                 }
282             } else
283                 servletDataObject = null;
284         } catch (IOException JavaDoc e) {
285             ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, e);
286             servletDataObject = null;
287         }
288         
289         // editor
290
if ((oldServlet == null)/*&&(servletDataObject != null)*/) {
291         } else {
292             RequestProcessor.postRequest(
293                     new Runnable JavaDoc() {
294                 public void run() {
295                     updateServletEditor();
296                     // Bugfix 31143: oldValue must be null, since if oldValue == newValue, no change will be fired
297
JspDataObject.this.firePropertyChange0(PROP_SERVLET_DATAOBJECT, null, getServletDataObject());
298                     // the state of some CookieActions may need to be updated
299
JspDataObject.this.firePropertyChange0(PROP_COOKIE, null, null);
300                 }
301             }
302             );
303         }
304     }
305     
306     /** This method causes a DataObject to be re-recognized by the loader system.
307      * This is a poor practice and should not be normally used, as it uses reflection
308      * to call a protected method DataObject.dispose().
309      */

310    /* private DataObject rerecognize(DataObject dObj) {
311         // invalidate the object so it can be rerecognized
312         FileObject prim = dObj.getPrimaryFile();
313         try {
314             dObj.setValid(false);
315             return DataObject.find(prim);
316         }
317         catch (java.beans.PropertyVetoException e) {
318             ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, e);
319         }
320         catch (DataObjectNotFoundException e) {
321             ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, e);
322         }
323         return dObj;
324     }*/

325     
326     /** JDK 1.2 compiler hack. */
327     public void firePropertyChange0(String JavaDoc propertyName, Object JavaDoc oldValue, Object JavaDoc newValue) {
328         super.firePropertyChange(propertyName, oldValue, newValue);
329     }
330     
331     /** Returns an editor for the servlet. Architecturally, a better solution would be to attach a cookie for
332      * editing the servlet, but we choose this approach for performance reasons - this allows lazy initialization of
333      * the editor (unlike the cookie). */

334     public EditorCookie getServletEditor() {
335         DataObject obj = getServletDataObject();
336         if ((obj == null) != (servletEdit == null))
337             updateServletEditor();
338         return servletEdit;
339     }
340     
341     private void updateServletEditor() {
342         if (servletDataObject == null) {
343             if (servletEdit != null) {
344                 servletEdit.close();
345                 servletEdit = null;
346             }
347         } else {
348             if (servletEdit == null) {
349                 servletEdit = createServletEditor();
350             }
351         }
352     }
353     
354     
355     /** Gets the current fileobject of the servlet corresponding to this JSP or null if may not exist.
356      * Note that the file still doesn't need to exist, even if it's not null.
357      * This does not need to be synchronized, because the calling method
358      * getPlugin() is synchronized.
359      */

360     private FileObject updateServletFileObject() throws IOException JavaDoc {
361         return compileData.getServletFileObject();
362     }
363     
364     
365     /////// -------- FIELDS AND METHODS FOR MANIPULATING THE PARSED INFORMATION -------- ////////
366

367     /** Updates the information about statically included pages for these pages.
368      * E.g. tells the included pages that they are included in this page. */

369 /* private void updateIncludedPagesInfo(JspCompilationInfo compInfo) throws IOException {
370         FileObject included[] = compInfo.getIncludedFileObjects();
371         for (int i = 0; i < included.length; i++) {
372             IncludedPagesSupport.setIncludedIn(getPrimaryFile(), included[i]);
373         }
374     }*/

375     
376     public void setQueryString(String JavaDoc params) throws java.io.IOException JavaDoc {
377         WebExecSupport.setQueryString(getPrimaryEntry().getFile(), params);
378         firePropertyChange(PROP_REQUEST_PARAMS, null, null);
379     }
380     
381     protected org.openide.filesystems.FileObject handleRename(String JavaDoc str) throws java.io.IOException JavaDoc {
382         if ("".equals(str)) // NOI18N
383
throw new IOException JavaDoc(NbBundle.getMessage(JspDataObject.class, "FMT_Not_Valid_FileName"));
384         
385         org.openide.filesystems.FileObject retValue;
386         
387         retValue = super.handleRename(str);
388         return retValue;
389     }
390     
391     public void addSaveCookie(SaveCookie cookie){
392         getCookieSet().add(cookie);
393     }
394     
395     public void removeSaveCookie(){
396         Node.Cookie cookie = getCookie(SaveCookie.class);
397         if (cookie!=null) getCookieSet().remove(cookie);
398     }
399     
400     protected FileObject handleMove(DataFolder df) throws IOException JavaDoc {
401         
402         FileObject retValue;
403         
404         retValue = super.handleMove(df);
405         
406         // fix for issue #55961 - remove old TagLibParseSupport and add new one.
407
TagLibParseSupport tlps = null;
408         tlps = (TagLibParseSupport)getCookie(TagLibParseSupport.class);
409         if (tlps != null){
410             getCookieSet().remove(tlps);
411             tlps = new TagLibParseSupport(retValue);
412             getCookieSet().add(tlps);
413         }
414         return retValue;
415     }
416     
417     
418     ////// -------- INNER CLASSES ---------
419

420     private class Listener extends FileChangeAdapter implements PropertyChangeListener JavaDoc/*, ServerRegistryImpl.ServerRegistryListener */{
421         WeakReference JavaDoc weakListener;
422         
423         Listener() {
424         }
425         
426         private void register(FileObject fo) {
427             EventListener JavaDoc el = WeakListeners.create(FileChangeListener.class, this, fo);
428             fo.addFileChangeListener((FileChangeListener) el);
429             weakListener = new WeakReference JavaDoc(el);
430         }
431         private void unregister(FileObject fo) {
432             FileChangeListener listener = (FileChangeListener) weakListener.get();
433             if (listener != null) {
434                 fo.removeFileChangeListener(listener);
435             }
436         }
437         public void propertyChange(PropertyChangeEvent JavaDoc evt) {
438             // listening on properties which could affect the server plugin
439
// saving the file
440
if (PROP_MODIFIED.equals(evt.getPropertyName())) {
441                 if ((Boolean.FALSE).equals(evt.getNewValue())) {
442                     refreshPlugin(false);
443                 }
444             }
445             // primary file changed or files changed
446
if (PROP_PRIMARY_FILE.equals(evt.getPropertyName()) ||
447                     PROP_FILES.equals(evt.getPropertyName())) {
448                 if (evt.getOldValue() instanceof FileObject)
449                     unregister((FileObject)evt.getOldValue());
450                 if (evt.getNewValue() instanceof FileObject)
451                     register((FileObject)evt.getNewValue());;
452                     refreshPlugin(true);
453             }
454             // the context object has changed
455
if (DataObject.PROP_VALID.equals(evt.getPropertyName())) {
456                 if (evt.getSource() instanceof DataObject) {
457                     DataObject dobj = (DataObject)evt.getSource();
458                     if (dobj.getPrimaryFile().getPackageNameExt('/','.').equals("")) { // NOI18N
459
dobj.removePropertyChangeListener(this);
460                         // PENDING
461
//ServerRegistryImpl.getRegistry().removeServerRegistryListener(this);
462
//JspDataObject.this.addWebContextListener();
463
}
464                 }
465             }
466             
467         }
468         
469         public void fileRenamed(FileRenameEvent fe) {
470             refreshPlugin(true);
471         }
472         
473         // implementation of ServerRegistryImpl.ServerRegistryListener
474
/*
475         PENDING
476         public void added(ServerRegistryImpl.ServerEvent added) {
477             serverChange();
478         }
479          
480         public void setAppDefault(ServerRegistryImpl.InstanceEvent inst) {
481             serverChange();
482         }
483          
484         public void setWebDefault(ServerRegistryImpl.InstanceEvent inst) {
485             serverChange();
486         }
487          
488         public void removed(ServerRegistryImpl.ServerEvent removed) {
489             serverChange();
490         }
491          */

492         
493         private void serverChange() {
494             refreshPlugin(true);
495             firePropertyChange0(PROP_SERVER_CHANGE, null, null);
496         }
497     }
498 }
499
500
Popular Tags