001: /**
002: * $Id: ClasspathGenUtil.java,v 1.7 2007/01/26 03:48:34 portalbld Exp $
003: * Copyright 2004 Sun Microsystems, Inc. All
004: * rights reserved. Use of this product is subject
005: * to license terms. Federal Acquisitions:
006: * Commercial Software -- Government Users
007: * Subject to Standard License Terms and
008: * Conditions.
009: *
010: * Sun, Sun Microsystems, the Sun logo, and Sun ONE
011: * are trademarks or registered trademarks of Sun Microsystems,
012: * Inc. in the United States and other countries.
013: */package com.sun.portal.fabric.util;
014:
015: import java.io.File;
016: import java.io.FileReader;
017: import java.io.BufferedReader;
018: import java.io.InputStream;
019: import java.io.InputStreamReader;
020: import java.io.IOException;
021: import java.io.FileNotFoundException;
022: import java.util.Properties;
023: import java.util.logging.Level;
024: import java.util.logging.Logger;
025:
026: import com.sun.portal.util.ResourceLoader;
027: import com.sun.portal.log.common.PortalLogger;
028: import com.sun.portal.util.Platform;
029:
030: public class ClasspathGenUtil {
031:
032: private static final String fs = Platform.fs;
033: private static final String SEPARATOR = "/";
034: private static Logger logger = PortalLogger
035: .getLogger(ClasspathGenUtil.class);
036:
037: public static String getClasspath(String inputfile,
038: boolean bReplaceTokens) {
039:
040: BufferedReader reader = null;
041: StringBuffer cpStrBuf = new StringBuffer();
042:
043: try {
044:
045: // Get the resource loader
046: ResourceLoader rcl = ResourceLoader.getInstance();
047: // Get a property object for the PSConfig.properties file
048: Properties psConfig = rcl
049: .getProperties("PSConfig.properties");
050:
051: // Create a reader for the input classpath file
052: InputStream ins = rcl.getResourceAsStream(inputfile);
053: InputStreamReader insr = new InputStreamReader(ins);
054: reader = new BufferedReader(insr);
055:
056: String line = null;
057: while ((line = reader.readLine()) != null) {
058:
059: // Trim the string for removing spaces
060: line.trim();
061: // If line is empty or is a comment skip processing it
062: if (line.length() < 1 || line.startsWith("#")) {
063: continue;
064: }
065:
066: if (bReplaceTokens) {
067: line = processClasspathEntry(line, psConfig);
068: }
069:
070: // Add this modified entry to the classpath stringbuffer
071: cpStrBuf.append(line);
072: cpStrBuf.append(Platform.pathSep);
073: }
074: } catch (FileNotFoundException fex) {
075: logger.log(Level.SEVERE, "PSFB_CSPFU0048", fex);
076: } catch (IOException ioex) {
077: logger.log(Level.SEVERE, "PSFB_CSPFU0048", ioex);
078: } finally {
079: if (reader != null) {
080: try {
081: reader.close();
082: } catch (IOException ioe) {
083: logger.log(Level.SEVERE, "PSFB_CSPFU0048", ioe);
084: }
085: }
086: }
087:
088: return cpStrBuf.toString();
089: }
090:
091: /**
092: * Every element represents a classpath entry.
093: * A valid classpath entry can be of three types:
094: * 1. The element might be a full path on the filesystem. Just add it
095: * to the classpath
096: * 2. The element might contain a tag + directory. In this case the tag
097: * needs to be replaced with its value in the PSConfig.properties
098: * then add the tagswapped string to classpath
099: * 3. The element might be a tag that needs to be replaced. In this
100: * case replace the tag and add to classpath
101: *
102: * @param entry - classpath entry
103: * @param psConfig - PSConfig.properties
104: * @return
105: */
106:
107: private static String processClasspathEntry(String entry,
108: Properties psConfig) {
109:
110: String tag = "";
111: String value = "";
112:
113: // Get the index of the separator
114: int index = entry.indexOf(SEPARATOR);
115: // If the line contains a separator this might be of type
116: // 1 OR 2. Else it might be of type 3
117: if (index > 0) {
118: // Get the string upto the first occurence of "/"
119: tag = entry.substring(0, index);
120: } else {
121: // The complete line might be a tag
122: // If its is a valid tag then it must be swapped
123: tag = entry;
124: }
125:
126: // Try to fetch the value of this tag from PSConfig
127: // If there is no value for this string in PSConfig
128: // Then the string is not a tag
129: value = psConfig.getProperty(tag);
130:
131: // If the value does not exist in PSConfig,assume that its
132: // not a tag and just add it as is to the classpath
133: if (value != null && value.length() > 1) {
134: entry = entry.replaceFirst(tag, value);
135: }
136: // Replace all "/" to the platform specific file separator
137: entry = entry.replaceAll(SEPARATOR, fs);
138: return entry;
139: }
140:
141: /**
142: * Replace all tags from PSConfig.properties found in the input taggedClasspath
143: * @param taggedClasspath - may contain tags. path elements seperated by ':'
144: * @return
145: */
146: public static String tagSwapClasspath(String taggedClasspath) {
147:
148: StringBuffer cpStrBuf = new StringBuffer();
149:
150: try {
151:
152: // Get the resource loader
153: ResourceLoader rcl = ResourceLoader.getInstance();
154: // Get a property object for the PSConfig.properties file
155: Properties psConfig = rcl
156: .getProperties("PSConfig.properties");
157:
158: String[] cpElement = taggedClasspath.split(":");
159: for (int idx = 0; idx < cpElement.length; idx++) {
160: String entry = cpElement[idx];
161: // Trim the string for removing spaces
162: entry.trim();
163: // If line not empty
164: if (entry.length() > 0) {
165: entry = processClasspathEntry(entry, psConfig);
166:
167: // Add this modified entry to the classpath stringbuffer
168: cpStrBuf.append(entry);
169: cpStrBuf.append(Platform.pathSep);
170: }
171: }
172: } catch (FileNotFoundException fex) {
173: logger.log(Level.SEVERE, "PSFB_CSPFU0049", fex);
174: } catch (IOException ioex) {
175: logger.log(Level.SEVERE, "PSFB_CSPFU0049", ioex);
176: }
177:
178: return cpStrBuf.toString();
179: }
180: }
|