001: package com.sun.portal.fabric.tasks;
002:
003: import com.sun.portal.admin.common.context.PSConfigContext;
004: import com.sun.portal.admin.common.PSConfigConstants;
005: import com.sun.portal.log.common.PortalLogger;
006:
007: import com.sun.portal.fabric.util.AntUtil;
008: import com.sun.portal.fabric.util.FileUtil;
009: import com.sun.portal.fabric.util.NetworkUtil;
010: import com.sun.portal.util.Platform;
011:
012: import java.io.File;
013:
014: import java.util.logging.Logger;
015: import java.util.logging.Level;
016: import java.util.Vector;
017:
018: /**
019: * This class creates a derby server instance, starts and stops it.
020: */
021: public class Derby {
022:
023: private static String fs = Platform.fs;
024:
025: private AntUtil antUtil;
026: private static Logger logger;
027:
028: private static String derbyPropFileLoc;
029: private static String derbyHostName;
030: private static String derbyAntFile = "derby.xml";
031: private static String derbyLibDir;
032: private static String buildFileLoc;
033: private static String propFileLoc;
034: private static final String PS_CONFIG_PROPERTIES = "PS_CONFIG";
035:
036: /**
037: * Default Constructor
038: *
039: * @param pcc PSConfigContext
040: * @param host Hostname
041: */
042: public Derby(PSConfigContext pcc, String host) {
043:
044: logger = PortalLogger.getLogger(Derby.class);
045: antUtil = new AntUtil(pcc.getPSDataDir());
046: buildFileLoc = pcc.getPSBaseDir() + fs + "lib" + fs
047: + derbyAntFile;
048: propFileLoc = pcc.getPSConfigDir() + fs
049: + PSConfigConstants.PS_CONFIG_FILE;
050: derbyPropFileLoc = pcc.getPSDataDir() + fs + "derby" + fs
051: + PSConfigConstants.PS_DERBY_CONFIG_FILE;
052: derbyHostName = host;
053: derbyLibDir = pcc.getDerbyLibDir();
054: }
055:
056: /**
057: * Configures a default derby server instance
058: */
059: public void configureDerby() {
060:
061: createDerbyInstance();
062: if (replaceTokensInDerbyPropFile()) {
063: startDerbyInstance();
064: }
065: }
066:
067: /**
068: * Unconfigures default derby server instance
069: */
070: public void unconfigureDerby() {
071:
072: logger.log(Level.FINEST, "PSFB_CSPFT0312");
073: stopDerbyInstance();
074: logger.log(Level.FINEST, "PSFB_CSPFT0313");
075:
076: }
077:
078: /**
079: * Creates a default derby server Instance
080: */
081: public void createDerbyInstance() {
082:
083: Vector targets = new Vector();
084: targets.add("create-instance");
085: logger.log(Level.FINEST, "PSFB_CSPFT0314");
086: antUtil.runant(buildFileLoc, targets, "derby",
087: PS_CONFIG_PROPERTIES, propFileLoc);
088: logger.log(Level.FINEST, "PSFB_CSPFT0315");
089: }
090:
091: /**
092: * Starts the default derby server instance
093: */
094: public void startDerbyInstance() {
095:
096: Vector targets = new Vector();
097: targets.add("start-instance");
098: logger.log(Level.FINEST, "PSFB_CSPFT0222");
099: antUtil.runant(buildFileLoc, targets, "derby",
100: PS_CONFIG_PROPERTIES, propFileLoc);
101: logger.log(Level.FINEST, "PSFB_CSPFT0316");
102: }
103:
104: /**
105: * Stops the default derby server Instance
106: */
107: public void stopDerbyInstance() {
108:
109: Vector targets = new Vector();
110: targets.add("stop-instance");
111: logger.log(Level.FINEST, "PSFB_CSPFT0317");
112: antUtil.runant(buildFileLoc, targets, "derby",
113: PS_CONFIG_PROPERTIES, propFileLoc);
114: logger.log(Level.FINEST, "PSFB_CSPFT0318");
115: }
116:
117: /**
118: * Replace tokens in derby.properties file
119: *
120: * @return true if derby.properties exists
121: */
122: private boolean replaceTokensInDerbyPropFile() {
123:
124: File propertiesFile = new File(derbyPropFileLoc);
125: boolean bRetCode = propertiesFile.exists();
126: if (bRetCode) {
127: String availablePort = NetworkUtil.getNextAvailablePort(
128: derbyHostName, "1527");
129: FileUtil.replaceTokenInFile(propertiesFile, "%PS_HOST%",
130: derbyHostName);
131: FileUtil.replaceTokenInFile(propertiesFile, "%DERBY_PORT%",
132: availablePort);
133: } else {
134: logger.log(Level.INFO, "PSFB_CSPFT0223", derbyPropFileLoc);
135: }
136:
137: return bRetCode;
138: }
139: }
|