001: /*
002: * Copyright 2002 Sun Microsystems, Inc. All rights reserved.
003: * PROPRIETARY/CONFIDENTIAL. Use of this product is subject to license terms.
004: */
005:
006: package com.sun.portal.ffj.actions;
007:
008: import java.util.Vector;
009: import java.util.Iterator;
010:
011: import java.io.IOException;
012: import java.io.File;
013: import java.io.FileInputStream;
014: import java.io.FileOutputStream;
015:
016: import org.netbeans.modules.web.core.WebExecSupport;
017:
018: import org.openide.loaders.XMLDataObject;
019: import org.openide.loaders.DataNode;
020:
021: import org.openide.filesystems.FileObject;
022: import org.openide.filesystems.FileSystem;
023: import org.openide.filesystems.FileUtil;
024: import org.openide.filesystems.FileStateInvalidException;
025:
026: import org.openide.cookies.ExecCookie;
027: import org.openide.cookies.InstanceCookie;
028: import org.openide.cookies.CompilerCookie;
029:
030: import org.openide.compiler.CompilerJob;
031: import org.openide.compiler.Compiler;
032: import org.openide.compiler.CompilerTask;
033:
034: import org.openide.nodes.Node;
035:
036: import org.openide.util.actions.NodeAction;
037:
038: import org.openide.loaders.DataObject;
039: import org.openide.loaders.DataObjectNotFoundException;
040:
041: import org.openide.TopManager;
042: import org.openide.ErrorManager;
043:
044: import com.sun.portal.ffj.util.ParEntry;
045: import com.sun.portal.ffj.util.Transfer;
046:
047: import com.sun.portal.ffj.cookies.ParEntryInstance;
048:
049: import com.sun.portal.ffj.filesystems.PSFileSystem;
050:
051: public abstract class PSNodeAction extends NodeAction {
052:
053: protected abstract FileObject getPSRunObject(PSFileSystem psFS);
054:
055: protected abstract String constructQueryString(ParEntry pe,
056: String pePathName, PSFileSystem psFS);
057:
058: protected boolean restart() {
059: return false;
060: }
061:
062: protected boolean enable(Node[] nodes) {
063: return true;
064: }
065:
066: protected void performAction(Node[] nodes) {
067:
068: XMLDataObject xob = (XMLDataObject) (nodes[0])
069: .getCookie(DataObject.class);
070:
071: // Get ParEntry file qualified pathname.
072: FileObject foPE = xob.getPrimaryFile();
073: String pePathName = PSFileSystem.getAbsolutePathName(foPE);
074:
075: // Get ParEntry object. We used to do this as a cookie. Keep the
076: // creation class as a cookie, and just do it inline.
077:
078: ParEntryInstance cookie = new ParEntryInstance();
079: cookie.attachTo(xob);
080: ParEntry pe = ParEntry.getParEntry(cookie);
081:
082: try {
083: FileSystem fs = foPE.getFileSystem();
084:
085: // Look for java file and compile it.
086: compile(fs, foPE);
087:
088: // transfer workspace files to portal server
089: transfer(foPE, pe);
090: } catch (FileStateInvalidException fsie) {
091: TopManager.getDefault().getErrorManager().notify(
092: ErrorManager.USER, fsie);
093: }
094:
095: // Execute
096: execute(pe, pePathName);
097: }
098:
099: protected void compile(FileSystem fs, FileObject fo) {
100: try {
101: String jfn = fo.getPackageName('/') + ".java";
102: FileObject jfo = fs.findResource(jfn);
103: if (jfo == null) {
104: return;
105: }
106:
107: CompilerCookie cc = (CompilerCookie) DataObject.find(jfo)
108: .getCookie(CompilerCookie.Compile.class);
109: if (cc == null) {
110: return;
111: }
112:
113: CompilerJob cjob = new CompilerJob(Compiler.DEPTH_ONE);
114: cc.addToJob(cjob, Compiler.DEPTH_ONE);
115: CompilerTask tsk = cjob.start();
116: tsk.waitFinished();
117: } catch (DataObjectNotFoundException donfe) {
118: TopManager.getDefault().getErrorManager().notify(
119: ErrorManager.USER, donfe);
120: }
121: }
122:
123: // Process all the transfers from the workspace contained in the ParEntry.
124:
125: protected void transfer(FileObject foPE, ParEntry pe) {
126:
127: // force saves
128:
129: TopManager.getDefault().saveAll();
130:
131: // get directories
132:
133: PSFileSystem psFS = (PSFileSystem) PSFileSystem
134: .getPSFilesystem(true);
135: File psfd = psFS.getRootDirectory();
136: File wsroot = null;
137: try {
138: FileSystem fs = foPE.getFileSystem();
139: wsroot = FileUtil.toFile(fs.getRoot());
140: } catch (FileStateInvalidException fsie) {
141: TopManager.getDefault().getErrorManager().notify(
142: ErrorManager.USER, fsie);
143: return;
144: }
145: FileObject folder = foPE.getParent();
146: String wsdir = folder == null ? null : folder.toString();
147:
148: // run through all the Transfer's
149:
150: Iterator it = pe.getTransfers();
151: while (it.hasNext()) {
152: Transfer tr = (Transfer) it.next();
153: tr.transfer(wsroot, wsdir, psfd);
154: }
155: }
156:
157: protected void execute(ParEntry pe, String pePathName) {
158: try {
159: PSFileSystem psFS = (PSFileSystem) PSFileSystem
160: .getPSFilesystem(true);
161:
162: FileObject fo = getPSRunObject(psFS);
163: ExecCookie cookie = (ExecCookie) DataObject.find(fo)
164: .getCookie(ExecCookie.class);
165:
166: String queryString = constructQueryString(pe, pePathName,
167: psFS);
168: WebExecSupport.setQueryString(fo, queryString);
169:
170: if (restart()) {
171: org.netbeans.modules.web.execution.WebDefaultExecPerformer
172: .setIncremental(false);
173: }
174:
175: cookie.start();
176: } catch (Exception donfe) {
177: TopManager.getDefault().getErrorManager().notify(
178: ErrorManager.USER, donfe);
179: }
180: }
181: }
|