001: /*
002: * Copyright 2003 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.loaders;
007:
008: import java.io.File;
009: import java.io.FileOutputStream;
010: import java.io.ObjectOutputStream;
011:
012: import java.util.TreeSet;
013: import java.util.StringTokenizer;
014:
015: import org.openide.TopManager;
016: import org.openide.ErrorManager;
017:
018: import org.openide.actions.EditAction;
019: import org.openide.actions.OpenAction;
020: import org.openide.actions.ViewAction;
021: import org.openide.actions.FileSystemAction;
022: import org.openide.actions.CutAction;
023: import org.openide.actions.CopyAction;
024: import org.openide.actions.PasteAction;
025: import org.openide.actions.DeleteAction;
026: import org.openide.actions.RenameAction;
027: import org.openide.actions.ToolsAction;
028: import org.openide.actions.PropertiesAction;
029:
030: import org.openide.filesystems.FileObject;
031: import org.openide.filesystems.FileUtil;
032: import org.openide.filesystems.FileSystem;
033:
034: import org.openide.util.NbBundle;
035: import org.openide.util.actions.SystemAction;
036:
037: import org.openide.loaders.DataObjectExistsException;
038: import org.openide.loaders.DataNode;
039: import org.openide.loaders.FileEntry;
040: import org.openide.loaders.MultiDataObject;
041: import org.openide.loaders.MultiDataObject.Entry;
042: import org.openide.loaders.UniFileLoader;
043:
044: import org.netbeans.modules.web.config.WebAppDescriptor;
045:
046: import com.sun.portal.ffj.actions.PortletXMLGroupAction;
047:
048: import com.sun.portal.ffj.util.PSConstants;
049: import com.sun.portal.ffj.util.PSFormat;
050: import com.sun.portal.ffj.util.PortletWebInf;
051:
052: import com.sun.portal.ffj.filesystems.PSFileSystem;
053:
054: public class PortletAppLoader extends UniFileLoader implements
055: PSConstants {
056:
057: public PortletAppLoader() {
058: super (PortletAppDataObject.class);
059: }
060:
061: protected void initialize() {
062: super .initialize();
063:
064: String displayName = NbBundle.getMessage(PSXmlLoader.class,
065: "LBL_PortletAppLoader");
066: setDisplayName(displayName);
067: }
068:
069: protected SystemAction[] defaultActions() {
070: return new SystemAction[] {
071: SystemAction.get(FileSystemAction.class), null,
072: SystemAction.get(CutAction.class),
073: SystemAction.get(CopyAction.class),
074: SystemAction.get(PasteAction.class), null,
075: SystemAction.get(DeleteAction.class), null,
076: SystemAction.get(ToolsAction.class),
077: SystemAction.get(PropertiesAction.class), null,
078: SystemAction.get(PortletXMLGroupAction.class) };
079: }
080:
081: protected MultiDataObject createMultiObject(FileObject primaryFile)
082: throws DataObjectExistsException {
083: return new PortletAppDataObject(primaryFile, this );
084: }
085:
086: protected FileObject findPrimaryFile(FileObject fo) {
087:
088: // must have the right name.
089:
090: if (!fo.getNameExt().equals(PORTLET_APP_CONTROL)) {
091: return null;
092: }
093:
094: // must be at root level.
095:
096: FileObject parent = fo.getParent();
097: if (parent == null) {
098: return null;
099: }
100: if (!parent.isRoot()) {
101: return null;
102: }
103:
104: // A web module group seems to be a TreeSet which contains WebAppDescriptor's,
105: // serialized to a file with the extension ".serverConfig.
106:
107: // We do this at this point so that we can do it after the WEB-INF
108: // directory has been picked up by the apporopriate data loader.
109:
110: try {
111: FileSystem fs = fo.getFileSystem();
112: File rd = FileUtil.toFile(fs.getRoot());
113: File grp = new File(rd, "WEB-INF/" + PORTLET_WEBGROUP_NAME
114: + "." + PORTLET_WEBGROUP_EXT);
115:
116: if (!grp.exists()) {
117: PSFileSystem psFS = (PSFileSystem) PSFileSystem
118: .getPSFilesystem(true);
119: WebAppDescriptor portlet = new WebAppDescriptor(fs
120: .getSystemName(), PORTLET_MAP_NAME);
121: WebAppDescriptor psfs = new WebAppDescriptor(psFS
122: .getDisplayName(), PSFS_MAP_NAME);
123: TreeSet ts = new TreeSet();
124: ts.add(portlet);
125: ts.add(psfs);
126: ObjectOutputStream os = new ObjectOutputStream(
127: new FileOutputStream(grp));
128: os.writeObject(ts);
129: os.flush();
130: os.close();
131: }
132:
133: } catch (Exception ex) {
134: TopManager.getDefault().getErrorManager().notify(
135: ErrorManager.USER, ex);
136: }
137:
138: return fo;
139: }
140:
141: // Figured out by reverse engineering manually defined module groups - even
142: // though the display name has backslashes, the group seems to work only
143: // with forward slashes. Note that this does NOT apply to the PSFS.
144:
145: private static String displayName(String str) {
146:
147: String pfx;
148: if (str.startsWith("\\")) {
149: pfx = "/";
150: str = str.substring(1);
151: } else {
152: pfx = "";
153: }
154:
155: StringTokenizer toks = new StringTokenizer(str, "\\");
156: StringBuffer buf = new StringBuffer();
157: while (toks.hasMoreTokens()) {
158: buf.append(pfx);
159: buf.append(toks.nextToken());
160: pfx = "/";
161: }
162:
163: return buf.toString();
164: }
165:
166: protected MultiDataObject.Entry createPrimaryEntry(
167: MultiDataObject obj, FileObject primaryFile) {
168: return new PSFormat(obj, primaryFile);
169: }
170: }
|