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.filesystems;
007:
008: import java.util.StringTokenizer;
009:
010: import java.io.FileNotFoundException;
011: import java.io.IOException;
012: import java.io.BufferedReader;
013: import java.io.BufferedWriter;
014: import java.io.File;
015: import java.io.FileReader;
016: import java.io.FileWriter;
017:
018: import org.openide.TopManager;
019: import org.openide.ErrorManager;
020:
021: import com.sun.portal.ffj.util.PSConstants;
022:
023: public class PSFSInstall implements PSConstants {
024:
025: public static boolean postCreate() {
026: boolean retVal = true;
027:
028: // desktop/desktopconfig.properties
029: String fileName = PSFileSystem.getRootPath() + File.separator
030: + PS_DESKTOP_DIR + File.separator + PS_DESKTOP_CONFIG
031: + PS_EXT_SEPARATOR + "properties";
032: retVal = update(fileName);
033:
034: return retVal;
035: }
036:
037: public static File getPSJarFile() {
038: return getModulesExtJarFile("psrun.jar");
039: }
040:
041: public static File getServletJarFile() {
042: return getModulesExtJarFile("servlet-2.2.jar");
043: }
044:
045: public static File getPortletJarFile() {
046: return new File(PSFileSystem.getRootPath(),
047: "WEB-INF/common/portlet.jar");
048: }
049:
050: private static boolean update(String fileName) {
051: String from[] = { "$(PSFS)", "$(PSRUNJAR)", "$(SERVLETJAR)",
052: "$(PATHSEP)" };
053: String to[] = new String[from.length];
054: to[0] = reverseBS(PSFileSystem.getRootPath());
055: to[1] = reverseBS(getPSJarFile().getPath());
056: to[2] = reverseBS(getServletJarFile().getPath());
057: to[3] = File.pathSeparator;
058:
059: return replace(from, to, fileName, fileName);
060: }
061:
062: public static String reverseBS(String str) {
063:
064: String pfx;
065: if (str.startsWith("\\")) {
066: pfx = "/";
067: str = str.substring(1);
068: } else {
069: pfx = "";
070: }
071:
072: StringTokenizer toks = new StringTokenizer(str, "\\");
073: StringBuffer buf = new StringBuffer();
074: while (toks.hasMoreTokens()) {
075: buf.append(pfx);
076: buf.append(toks.nextToken());
077: pfx = "/";
078: }
079:
080: return buf.toString();
081: }
082:
083: public static boolean replace(String from[], String to[],
084: String srcFileName, String destFileName) {
085: //
086: String method = "replace: ";
087: System.out.println(method + "Entry: srcFileName = "
088: + srcFileName + ", destFileName = " + destFileName);
089: boolean retVal = true;
090:
091: if (srcFileName.equals(destFileName)) {
092: destFileName += ".temp";
093: retVal = replace(from, to, srcFileName, destFileName);
094:
095: File removeFile = new File(srcFileName);
096: removeFile.delete();
097: File renameFile = new File(destFileName);
098: renameFile.renameTo(removeFile);
099:
100: return retVal;
101: }
102:
103: //
104: try {
105: BufferedReader br = new BufferedReader(new FileReader(
106: srcFileName));
107: BufferedWriter bw = new BufferedWriter(new FileWriter(
108: destFileName));
109:
110: String line = br.readLine();
111: while (line != null) {
112: for (int i = 0; i < from.length; ++i) {
113: StringBuffer buf = new StringBuffer();
114: int index = line.indexOf(from[i]);
115: while (index >= 0) {
116: buf.append(line.substring(0, index));
117: buf.append(to[i]);
118: line = line.substring(index + from[i].length());
119: index = line.indexOf(from[i]);
120: }
121: buf.append(line);
122: line = buf.toString();
123: }
124: bw.write(line);
125: bw.newLine();
126: line = br.readLine();
127: }
128: br.close();
129: bw.close();
130: } catch (IOException ioe) {
131: TopManager.getDefault().getErrorManager().notify(
132: ErrorManager.WARNING, ioe);
133: retVal = false;
134: }
135:
136: System.out.println(method + "Exit: Returning " + retVal);
137: return retVal;
138: }
139:
140: private static File getModulesExtJarFile(String jar) {
141:
142: String userDir = System.getProperty("netbeans.user");
143:
144: String homeDir = System.getProperty("netbeans.home");
145: if (homeDir == null) {
146: homeDir = ".";
147: }
148:
149: if (userDir != null) {
150: String path = userDir + File.separator + "modules"
151: + File.separator + "ext" + File.separator + jar;
152: File f = new File(path);
153: if (f.exists()) {
154: return f;
155: }
156: }
157:
158: String path = homeDir + File.separator + "modules"
159: + File.separator + "ext" + File.separator + jar;
160: return new File(path);
161: }
162: }
|