001: /*
002: * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
003: */
004: package com.zerog.ia.customcode.util.fileutils;
005:
006: import java.io.*;
007:
008: import com.zerog.ia.api.pub.*;
009:
010: /**
011: * Rename renames a file or directory.
012: *
013: * @see com.acme.dialogs.CustomCodeAction
014: *
015: * @version 3.0.0
016: */
017: public class Rename extends CustomCodeAction {
018: private static final String INSTALL_MESSAGE = "Renaming files";
019: private static final String UNINSTALL_MESSAGE = "";
020: private static final String ERR_MSG = "Rename: no target or new name specified.";
021: private static final String TARGET_VAR_NAME = "$Rename_Target$";
022: private static final String NEWNAME_VAR_NAME = "$Rename_NewName$";
023: private static final String SUCCESS = "SUCCESS";
024: private static final String ERROR = "ERROR";
025: private boolean isLoaded = false;
026:
027: /**
028: * This is the method that is called at install-time. The InstallerProxy
029: * instance provides methods to access information in the installer,
030: * set status, and control flow.<p>
031: *
032: * For the purposes of the this action (Rename), this method
033: * <ol>
034: * <li>gets its parameters from InstallAnywhere Variables,</li>
035: * <li>checks the parameters' validity,</li>
036: * <li>and renames the file.</li></ol>
037: *
038: * @see com.zerog.ia.api.pub.CustomCodeAction#install
039: */
040: public void install(InstallerProxy ip) throws InstallException {
041: /**
042: * InstallAnywhere variable:RENAME_SUCCESS
043: *
044: * possible return values are:
045: * SUCCESS
046: * ERROR
047: *
048: */
049: ip.setVariable("RENAME_SUCCESS", Rename.SUCCESS);
050:
051: System.out.println("Rename: RENAME_SUCCESS=" + Rename.SUCCESS);
052:
053: /* Get input from InstallAnywhere Variables. The literal contents of
054: the Variables are retieved into the Strings. */
055: String target = ip.substitute(TARGET_VAR_NAME);
056: String newName = ip.substitute(NEWNAME_VAR_NAME);
057:
058: /* substitute() will return an empty string for any InstallAnywhere
059: Variable that hasn't been assigned yet.
060:
061: /* If there is both a source and a destination, copy the files. */
062:
063: if (target.equals("") || newName.equals("")) {
064: error(target, newName);
065: } else {
066: System.out.println("target = " + target + ", newName = "
067: + newName);
068: try {
069: rename(target, newName);
070: } catch (IOException ioe) {
071: System.out.println("Rename: Exception = "
072: + ioe.getMessage());
073: ip.setVariable("RENAME_SUCCESS", Rename.ERROR);
074: System.out.println("Rename: RENAME_SUCCESS="
075: + Rename.ERROR);
076: //throw new NonfatalInstallException( ioe.getMessage() );
077: }
078: }
079: }
080:
081: /**
082: * This is the method that is called at uninstall-time. For
083: * an example of how to effect the uninstallation of something
084: * like this, please see com.acme.fileutils.CopyFile.
085: *
086: * @see com.acme.fileutils.CopyFile
087: * @see com.zerog.ia.api.pub.CustomCodeAction#uninstall
088: */
089: public void uninstall(UninstallerProxy up) throws InstallException {
090: //
091: }
092:
093: /**
094: * This method will be called to display a status message during the
095: * installation.
096: *
097: * @see com.zerog.ia.api.pub.CustomCodeAction#getInstallStatusMessage
098: */
099: public String getInstallStatusMessage() {
100: return INSTALL_MESSAGE;
101: }
102:
103: /**
104: * This method will be called to display a status message during the
105: * uninstall.
106: *
107: * @see com.zerog.ia.api.pub.CustomCodeAction#getUninstallStatusMessage
108: */
109: public String getUninstallStatusMessage() {
110: return UNINSTALL_MESSAGE;
111: }
112:
113: public static void rename(String target, String newName)
114: throws IOException {
115: rename(new File(target), newName);
116: }
117:
118: /**
119: * Rename the file represented by source to the file
120: * represented by destination.
121: */
122: public static void rename(File target, String newName)
123: throws IOException {
124: if (!target.renameTo(new File(target.getParent(), newName)))
125: throw new IOException("Couldn't rename file.");
126: }
127:
128: /**
129: * Print something to indicate that the parameters were not acceptable.
130: */
131: private void error(String target, String newName) {
132: System.err.println(ERR_MSG);
133: System.err.println("Target: " + target);
134: System.err.println("New Name: " + newName);
135: }
136: }
|