001: /*
002: * BEGIN_HEADER - DO NOT EDIT
003: *
004: * The contents of this file are subject to the terms
005: * of the Common Development and Distribution License
006: * (the "License"). You may not use this file except
007: * in compliance with the License.
008: *
009: * You can obtain a copy of the license at
010: * https://open-esb.dev.java.net/public/CDDLv1.0.html.
011: * See the License for the specific language governing
012: * permissions and limitations under the License.
013: *
014: * When distributing Covered Code, include this CDDL
015: * HEADER in each file and include the License file at
016: * https://open-esb.dev.java.net/public/CDDLv1.0.html.
017: * If applicable add the following below this CDDL HEADER,
018: * with the fields enclosed by brackets "[]" replaced with
019: * your own identifying information: Portions Copyright
020: * [year] [name of copyright owner]
021: */
022:
023: /*
024: * @(#)JBICoreInstaller.java
025: * Copyright 2004-2007 Sun Microsystems, Inc. All Rights Reserved.
026: *
027: * END_HEADER - DO NOT EDIT
028: */
029: package com.sun.jbi.installer;
030:
031: import java.io.File;
032: import java.io.FileNotFoundException;
033: import java.io.FileInputStream;
034: import java.io.FileOutputStream;
035: import java.io.IOException;
036: import java.text.SimpleDateFormat;
037: import java.util.logging.Logger;
038: import java.util.Properties;
039: import java.util.ResourceBundle;
040: import java.util.Date;
041: import java.util.zip.ZipException;
042: import java.io.FileReader;
043: import java.io.BufferedReader;
044:
045: /**
046: * This class is the main entry point to the installer for
047: * JBI Addons Lite.
048: */
049: public class JBICoreInstaller {
050:
051: public static final String DATE_FORMAT = "yyyyMMdd";
052: private String mAntScriptName = null;
053: private String mAntBaseDir = null;
054: private Logger mLogger = null;
055: private Properties mCmdLineParams = new Properties();
056: private boolean mIsJBICoreInstalled = true;
057: private String isErrorMessage = "";
058: private boolean mIsJBICoreUninstalled = true;
059: private ResourceBundle resourceBundle = null;
060:
061: /**
062: * The main method of the JBI components Addons installer.
063: * This method takes the appserver installation dir as the
064: * first argument
065: * @param args, the first argument should be appserver install dir
066: *
067: */
068: public static void main(String[] args) {
069: JBICoreInstaller jbiCoreInstaller = null;
070: try {
071: jbiCoreInstaller = new JBICoreInstaller();
072: jbiCoreInstaller.setResourceBundle();
073: jbiCoreInstaller.setLogger();
074:
075: if (args.length < 2) {
076: jbiCoreInstaller.getLogger().severe(
077: jbiCoreInstaller.getResourceBundle().getString(
078: "usage"));
079: return;
080: }
081:
082: String asInstallDir = args[0];
083:
084: boolean overwrite = false;
085: if (args.length == 3) {
086: if (args[2].equalsIgnoreCase("overwrite")) {
087: overwrite = true;
088: }
089: }
090:
091: if (!asInstallDir.equals(".")
092: && args[1].equalsIgnoreCase("install")) {
093: jbiCoreInstaller.install(asInstallDir, overwrite);
094: if (jbiCoreInstaller.mIsJBICoreInstalled) {
095: jbiCoreInstaller.getLogger().info(
096: jbiCoreInstaller.getResourceBundle()
097: .getString(
098: "installation-successful")
099: + asInstallDir
100: + File.separator
101: + InstallConstants.JBI_INSTALL_DIR);
102: } else {
103: jbiCoreInstaller.getLogger().severe(
104: jbiCoreInstaller.getResourceBundle()
105: .getString("installation-failed")
106: + jbiCoreInstaller.isErrorMessage);
107: }
108: } else if (!asInstallDir.equals(".")
109: && args[1].equalsIgnoreCase("uninstall")) {
110: System.out
111: .println("Uninstall is not supported at this time.");
112:
113: /* jbiCoreInstaller.uninstall(asInstallDir);
114: if (jbiCoreInstaller.mIsJBICoreUninstalled){
115: jbiCoreInstaller.getLogger().info(
116: jbiCoreInstaller.getResourceBundle().getString(
117: "uninstallation-successful") + asInstallDir + File.separator +
118: InstallConstants.JBI_INSTALL_DIR);
119: } else {
120: jbiCoreInstaller.getLogger().severe(
121: jbiCoreInstaller.getResourceBundle().getString("uninstallation-failed"));
122: } */
123: } else {
124: jbiCoreInstaller.getLogger().severe(
125: jbiCoreInstaller.getResourceBundle().getString(
126: "usage"));
127: return;
128: }
129:
130: } catch (Exception e) {
131: jbiCoreInstaller.getLogger().severe(e.getMessage());
132: }
133:
134: }
135:
136: /**
137: * This method is used to install JBI components
138: * @param InstallationContext ic
139: */
140: public void install(String appServerInstallRoot, boolean overwrite) {
141: String timeStamp = "";
142: try {
143: String pattern = "yyyyMMddHHmm";
144: String underscore = "_";
145: SimpleDateFormat df = new SimpleDateFormat(pattern);
146: String result = df.format(new Date());
147: timeStamp = underscore + result;
148: } catch (Exception e) {
149: //Here we do not do anything and the timestamp will be an empty string
150: }
151:
152: mLogger = Logger
153: .getLogger("com.sun.jbi.installer.JBICoreInstaller");
154:
155: mAntBaseDir = System.getProperty("java.io.tmpdir")
156: + File.separator + "jbi-core-installer" + timeStamp;
157:
158: mAntScriptName = mAntBaseDir + File.separator
159: + InstallConstants.JBI_INSTALL_SCRIPT;
160:
161: String currentPath = getCurrentPath();
162: currentPath = new File(currentPath).getAbsolutePath();
163: String jbiCoreInstallerJar = currentPath;
164:
165: File baseDir = new File(mAntBaseDir);
166: if (!baseDir.exists()) {
167: baseDir.mkdirs();
168: }
169:
170: try {
171: JarFactory jrFctry = new JarFactory(mAntBaseDir);
172: jrFctry.unJar(new File(jbiCoreInstallerJar));
173: } catch (ZipException zipEx) {
174: mIsJBICoreInstalled = false;
175: mLogger.severe(getResourceBundle().getString(
176: "jar-exception")
177: + jbiCoreInstallerJar + zipEx.getMessage());
178: } catch (IOException ioEx) {
179: mIsJBICoreInstalled = false;
180: mLogger.severe(getResourceBundle().getString(
181: "copy-jar-to-as-dir")
182: + jbiCoreInstallerJar + ioEx.getMessage());
183: }
184:
185: if (!overwrite) {
186: try {
187: FileReader reader = new FileReader(appServerInstallRoot
188: + File.separator + "jbi" + File.separator
189: + "jbi.ver");
190: BufferedReader br = new BufferedReader(reader);
191: String inputLine = null;
192: String buildNumber = null;
193: while ((inputLine = br.readLine()) != null) {
194: // build number should be BUILD_NUMBER="071127_2"
195: if (inputLine.contains("BUILD_NUMBER")) {
196: buildNumber = inputLine;
197: break;
198: }
199: }
200: br.close();
201: reader.close();
202: String asBuildNumber = "20"
203: + buildNumber.substring(14, 20);
204:
205: File jbiFile = new File(mAntBaseDir + File.separator
206: + "jbi-install.jar");
207: Date jbiDate = new Date(jbiFile.lastModified());
208: SimpleDateFormat SDFDATE = new SimpleDateFormat(
209: DATE_FORMAT);
210: SDFDATE.setLenient(false);
211: String jbiDateString = SDFDATE.format(jbiDate);
212:
213: if (asBuildNumber.compareTo(jbiDateString) > 0) {
214: System.out
215: .println("\nThe installer is too old to replace the JBI in the application server.\n");
216: mIsJBICoreInstalled = false;
217: isErrorMessage = "The installer is too old to replace the JBI in the application server\n";
218: try {
219: deleteDirAndContents(baseDir, true);
220: } catch (Exception e) {
221: mLogger
222: .warning("Removing temp folder failed:\n"
223: + e.getMessage());
224: }
225: return;
226: }
227: } catch (FileNotFoundException fnfe) {
228: } catch (IOException ioe) {
229: }
230: }
231:
232: // if AS_INSTALL for windows contains \, replace with / and
233: // set them in build.properties file
234: if (appServerInstallRoot.indexOf("\\") != -1) {
235: appServerInstallRoot = appServerInstallRoot.replace('\\',
236: '/');
237: }
238:
239: mCmdLineParams.setProperty(InstallConstants.AS_INSTALL_DIR,
240: appServerInstallRoot);
241:
242: //the log entries in tmp dir and then move it to jbi dir
243: String tempLogFile = mAntBaseDir + File.separator
244: + "jbi-core-installer.log";
245:
246: try {
247: AntRunner ant = new AntRunner();
248: if (!ant.runAnt(appServerInstallRoot, mAntScriptName,
249: InstallConstants.JBI_INSTALL_TARGET,
250: mCmdLineParams, tempLogFile)) {
251: mLogger.severe(getResourceBundle().getString(
252: "ant-script-failed")
253: + mAntScriptName);
254: }
255: } catch (Exception e) {
256: mIsJBICoreInstalled = false;
257: mLogger.severe(getResourceBundle().getString(
258: "Unable-to-execute-ant-script")
259: + e.getMessage());
260: }
261:
262: String logFile = appServerInstallRoot + File.separator
263: + InstallConstants.JBI_INSTALL_DIR + File.separator
264: + "jbi-core-installer.log";
265: try {
266: File tmpLogFile = new File(tempLogFile);
267: InstallerUtilities.copyFile(tmpLogFile, new File(logFile));
268: tmpLogFile = null;
269:
270: } catch (Exception e) {
271: mLogger.warning(getResourceBundle().getString(
272: "error-in-moving-log-file")
273: + logFile + "\n" + e.getMessage());
274: }
275: try {
276: deleteDirAndContents(baseDir, true);
277: } catch (Exception e) {
278: mLogger.warning("Removing temp folder failed:\n"
279: + e.getMessage());
280: }
281: }
282:
283: /*
284: * This method is used to get the current path from the java.class.path
285: */
286: private String getCurrentPath() {
287: String delimiter = "";
288: String javaClassPath = System.getProperty("java.class.path");
289: if (System.getProperty("os.name").contains("SunOS")
290: || System.getProperty("os.name").contains("Linux")
291: || System.getProperty("os.name").contains("Mac OS X")) {
292: delimiter = ":";
293: } else if (System.getProperty("os.name").contains("Windows")) {
294: delimiter = ";";
295: }
296: String[] paths = javaClassPath.split(delimiter);
297: for (int i = 0; i < paths.length; i++) {
298: if (paths[i].contains("jbi-core-installer.jar")) {
299: return paths[i];
300: }
301: }
302: return javaClassPath;
303: }
304:
305: /**
306: * This method is used to uninstall JBI Addons Lite
307: * @param InstallationContext ic
308: */
309: public void uninstall(String appServerInstallRoot) {
310: mLogger = Logger
311: .getLogger("com.sun.jbi.installer.JBICoreInstaller");
312:
313: try {
314: String uninstallDirStr = appServerInstallRoot
315: + File.separator + "jbi";
316: File uninstallDir = new File(uninstallDirStr);
317: deleteDirAndContents(uninstallDir, true);
318:
319: String uninstallFileStr = appServerInstallRoot
320: + File.separator + "jbi-install.jar";
321: File uninstallFile = new File(uninstallFileStr);
322: uninstallFile.delete();
323:
324: mLogger.info(getResourceBundle().getString(
325: "uninstallation-successful")
326: + appServerInstallRoot);
327:
328: } catch (Exception e) {
329: mIsJBICoreUninstalled = false;
330: mLogger.severe(getResourceBundle().getString(
331: "error-in-uninstall-core")
332: + e.getMessage());
333: }
334: }
335:
336: public boolean deleteDirAndContents(File path, boolean recursive) {
337: if (path.exists()) {
338: File[] files = path.listFiles();
339: for (int i = 0; i < files.length; i++) {
340: if (files[i].isDirectory()) {
341: if (recursive) {
342: deleteDirAndContents(files[i], recursive);
343: }
344: } else {
345: boolean deleted = files[i].delete();
346: if (!deleted) {
347: files[i].deleteOnExit();
348: }
349: }
350: }
351: }
352: boolean removed = path.delete();
353: if (!removed) {
354: path.deleteOnExit();
355: }
356: return true;
357: }
358:
359: /**
360: * This method is used to set Logger
361: */
362: private void setLogger() {
363: mLogger = Logger
364: .getLogger("com.sun.jbi.installer.JBIComponentsInstaller");
365: }
366:
367: /**
368: * This method is used to get Logger
369: */
370: private Logger getLogger() {
371: return mLogger;
372: }
373:
374: /**
375: * This method is used to set ResourceBundle
376: */
377: private void setResourceBundle() {
378: resourceBundle = ResourceBundle
379: .getBundle(InstallConstants.RESOURCE_BUNDLE);
380: }
381:
382: /**
383: * This method is used to get ResourceBundle
384: */
385: public ResourceBundle getResourceBundle() {
386: return resourceBundle;
387: }
388:
389: }
|