001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: * The Original Software is NetBeans. The Initial Developer of the Original
026: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
027: * Microsystems, Inc. All Rights Reserved.
028: *
029: * If you wish your version of this file to be governed by only the CDDL
030: * or only the GPL Version 2, indicate your decision by adding
031: * "[Contributor] elects to include this software in this distribution
032: * under the [CDDL or GPL Version 2] license." If you do not indicate a
033: * single choice of license, a recipient has the option to distribute
034: * your version of this file under either the CDDL, the GPL Version 2 or
035: * to extend the choice of license to its licensees as provided above.
036: * However, if you add GPL Version 2 code and therefore, elected the GPL
037: * Version 2 license, then the option applies only if the new code is
038: * made subject to such option by the copyright holder.
039: */
040:
041: package org.netbeans.lib.profiler.common.integration;
042:
043: import org.netbeans.lib.profiler.ProfilerLogger;
044: import org.netbeans.lib.profiler.common.Profiler;
045: import org.netbeans.lib.profiler.global.CommonConstants;
046: import org.netbeans.lib.profiler.global.Platform;
047: import java.io.*;
048: import java.nio.channels.FileChannel;
049: import java.text.MessageFormat;
050: import java.util.ResourceBundle;
051:
052: /**
053: * Utils for platform- and settings-specific integration instructions.
054: *
055: * @author Tomas Hurka
056: * @author Jiri Sedlacek
057: */
058: public class IntegrationUtils {
059: //~ Static fields/initializers -----------------------------------------------------------------------------------------------
060:
061: // -----
062: // I18N String constants
063: private static final ResourceBundle messages = ResourceBundle
064: .getBundle("org.netbeans.lib.profiler.common.integration.Bundle"); // NOI18N
065: public static final String PLATFORM_JAVA_50 = messages
066: .getString("IntegrationUtils_PlatformJava50"); // NOI18N
067: public static final String PLATFORM_JAVA_60 = messages
068: .getString("IntegrationUtils_PlatformJava60"); // NOI18N
069: public static final String PLATFORM_JAVA_70 = messages
070: .getString("IntegrationUtils_PlatformJava70"); // NOI18N
071: public static final String PLATFORM_WINDOWS_OS = messages
072: .getString("IntegrationUtils_PlatformWindowsOs"); // NOI18N
073: public static final String PLATFORM_WINDOWS_AMD64_OS = messages
074: .getString("IntegrationUtils_PlatformWindowsAmd64Os"); // NOI18N
075: public static final String PLATFORM_LINUX_OS = messages
076: .getString("IntegrationUtils_PlatformLinuxOs"); // NOI18N
077: public static final String PLATFORM_LINUX_AMD64_OS = messages
078: .getString("IntegrationUtils_PlatformLinuxAmd64Os"); // NOI18N
079: public static final String PLATFORM_SOLARIS_INTEL_OS = messages
080: .getString("IntegrationUtils_PlatformSolarisIntelOs"); // NOI18N
081: public static final String PLATFORM_SOLARIS_AMD64_OS = messages
082: .getString("IntegrationUtils_PlatformSolarisAmd64Os"); // NOI18N
083: public static final String PLATFORM_SOLARIS_SPARC_OS = messages
084: .getString("IntegrationUtils_PlatformSolarisSparcOs"); // NOI18N
085: public static final String PLATFORM_SOLARIS_SPARC64_OS = messages
086: .getString("IntegrationUtils_PlatformSolarisSparc64Os"); // NOI18N
087: public static final String PLATFORM_MAC_OS = messages
088: .getString("IntegrationUtils_PlatformMacOs"); // NOI18N
089: public static final String MODIFIED_FOR_PROFILER_STRING = messages
090: .getString("IntegrationUtils_ModifiedForProfilerString"); // NOI18N
091: public static final String ORIGINAL_BACKUP_LOCATION_STRING = messages
092: .getString("IntegrationUtils_OriginalBackupLocationString"); // NOI18N
093: private static final String APPLICATION_STRING = messages
094: .getString("IntegrationUtils_ApplicationString"); // NOI18N
095: private static final String APPLET_STRING = messages
096: .getString("IntegrationUtils_AppletString"); // NOI18N
097: private static final String SERVER_STRING = messages
098: .getString("IntegrationUtils_ServerString"); // NOI18N
099: private static final String DATABASE_STRING = messages
100: .getString("IntegrationUtils_DatabaseString"); // NOI18N
101: private static final String TARGET_STRING = messages
102: .getString("IntegrationUtils_TargetString"); // NOI18N
103: private static final String JDK_50_NAME = messages
104: .getString("IntegrationUtils_Jdk50Name"); // NOI18N
105: private static final String JDK_60_NAME = messages
106: .getString("IntegrationUtils_Jdk60Name"); // NOI18N
107: private static final String JDK_70_NAME = messages
108: .getString("IntegrationUtils_Jdk70Name"); // NOI18N
109: private static final String HTML_REMOTE_STRING = "<"
110: + messages.getString("IntegrationUtils_RemoteString")
111: + ">"; // NOI18N
112: private static final String EXPORT_SETENV_MESSAGE = messages
113: .getString("IntegrationUtils_ExportSetenvMessage"); // NOI18N
114: private static final String REDUCE_OVERHEAD_MESSAGE = messages
115: .getString("IntegrationUtils_ReduceOverheadMessage"); // NOI18N
116: private static final String COPY_FILE_NOT_FOUND_MESSAGE = messages
117: .getString("IntegrationUtils_CopyFileNotFoundMessage"); // NOI18N
118: private static final String COPY_CANNOT_DELETE_FILE_MESSAGE = messages
119: .getString("IntegrationUtils_CopyCannotDeleteFileMessage"); // NOI18N
120: private static final String COPY_ERROR_MESSAGE = messages
121: .getString("IntegrationUtils_CopyErrorMessage"); // NOI18N
122: private static final String BACKUP_FILE_NOT_FOUND_MESSAGE = messages
123: .getString("IntegrationUtils_BackupFileNotFoundMessage"); // NOI18N
124: private static final String BACKUP_CANNOT_DELETE_FILE_MESSAGE = messages
125: .getString("IntegrationUtils_BackupCannotDeleteFileMessage"); // NOI18N
126: private static final String BACKUP_ERROR_MESSAGE = messages
127: .getString("IntegrationUtils_BackupErrorMessage"); // NOI18N
128: private static final String BACKUP_ERROR_COPY_FILE_MESSAGE = messages
129: .getString("IntegrationUtils_BackupErrorCopyFileMessage"); // NOI18N
130: private static final String RESTORE_FILE_NOT_FOUND_MESSAGE = messages
131: .getString("IntegrationUtils_RestoreFileNotFoundMessage"); // NOI18N
132: private static final String RESTORE_CANNOT_DELETE_FILE_MESSAGE = messages
133: .getString("IntegrationUtils_RestoreCannotDeleteFileMessage"); // NOI18N
134: private static final String RESTORE_ERROR_MESSAGE = messages
135: .getString("IntegrationUtils_RestoreErrorMessage"); // NOI18N
136: private static final String MANUAL_REMOTE_STEP1_MESSAGE = messages
137: .getString("IntegrationUtils_ManualRemoteStep1Message"); // NOI18N
138: private static final String MANUAL_REMOTE_STEP2_MESSAGE = messages
139: .getString("IntegrationUtils_ManualRemoteStep2Message"); // NOI18N
140: private static final String REMOTE_ABSOLUTE_PATH_HINT = messages
141: .getString("IntegrationUtils_RemoteAbsolutePathHint"); // NOI18N
142: private static final String SPACES_IN_PATH_WARNING_MSG = messages
143: .getString("IntegrationUtils_SpacesInPathWarningMsg"); // NOI18N
144:
145: // -----
146: public static final String FILE_BACKUP_EXTENSION = ".backup"; //NOI18N
147:
148: //~ Methods ------------------------------------------------------------------------------------------------------------------
149:
150: // Returns command for adding profiler native libraries to path
151: public static String getAddProfilerLibrariesToPathString(
152: String targetPlatform, String targetJVM, boolean isRemote,
153: boolean formatAsHTML) {
154: return getExportEnvVariableValueString(
155: targetPlatform,
156: getNativePathEnvVariableString(targetPlatform),
157: getNativeLibrariesPath(targetPlatform, targetJVM,
158: isRemote)
159: + getPathSeparator(targetPlatform)
160: + getEnvVariableReference(
161: getNativePathEnvVariableString(targetPlatform),
162: targetPlatform), formatAsHTML);
163: }
164:
165: public static String getAssignEnvVariableValueString(
166: String targetPlatform, String variableName,
167: String variableValue) {
168: if (isWindowsPlatform(targetPlatform)) {
169: return getExportCommandString(targetPlatform) + " "
170: + variableName + "=" + variableValue; //NOI18N
171: }
172:
173: return variableName + "=" + variableValue; //NOI18N
174: }
175:
176: // Returns batch file extension bat / sh according to current / selected OS
177: public static String getBatchExtensionString(String targetPlatform) {
178: if (isWindowsPlatform(targetPlatform)) {
179: return ".bat"; //NOI18N
180: }
181:
182: return ".sh"; //NOI18N
183: }
184:
185: // Returns HTML-formatted hint about how to reduce CPU profiling overhead
186: public static String getCPUReduceOverheadHint() {
187: return REDUCE_OVERHEAD_MESSAGE;
188: }
189:
190: // Returns "\" or "/" according to provided platform
191: public static String getDirectorySeparator(String targetPlatform) {
192: if (isWindowsPlatform(targetPlatform)) {
193: return "\\"; //NOI18N
194: }
195:
196: return "/"; //NOI18N
197: }
198:
199: // Returns reference to given environment variable according to current / selected OS
200: public static String getEnvVariableReference(String envVariable,
201: String targetPlatform) {
202: if (isWindowsPlatform(targetPlatform)) {
203: return "%" + envVariable + "%"; //NOI18N
204: }
205:
206: return "$" + envVariable; //NOI18N
207: }
208:
209: // Returns SET / export command according to current / selected OS
210: public static String getExportCommandString(String targetPlatform) {
211: if (isWindowsPlatform(targetPlatform)) {
212: return "SET"; //NOI18N
213: }
214:
215: return "export"; //NOI18N
216: }
217:
218: // Returns expression for exporting environment variable value
219: public static String getExportEnvVariableValueString(
220: String targetPlatform, String variableName,
221: String variableValue, boolean formatAsHTML) {
222: if (isWindowsPlatform(targetPlatform)) {
223: return getAssignEnvVariableValueString(targetPlatform,
224: variableName, variableValue);
225: }
226:
227: if (targetPlatform.equals(PLATFORM_LINUX_OS)) {
228: return getExportCommandString(targetPlatform)
229: + " "
230: + getAssignEnvVariableValueString(targetPlatform,
231: variableName, variableValue); //NOI18N
232: }
233:
234: return getAssignEnvVariableValueString(targetPlatform,
235: variableName, variableValue)
236: + (formatAsHTML ? "<br>" : getLineBreak(targetPlatform))
237: + getExportCommandString(targetPlatform)
238: + " "
239: + variableName; //NOI18N
240: }
241:
242: // Returns HTML-formatted note about export vs. setenv on UNIXes
243: public static String getExportVSSetenvNote() {
244: return EXPORT_SETENV_MESSAGE;
245: }
246:
247: public static boolean isFileModifiedForProfiler(File file) {
248: try {
249: FileReader fr = new FileReader(file);
250: BufferedReader br = new BufferedReader(fr);
251:
252: // check if first line contains Profiler header
253: String line = br.readLine();
254:
255: if (line == null) {
256: br.close();
257:
258: return false;
259: }
260:
261: if (line.indexOf(MODIFIED_FOR_PROFILER_STRING) != -1) {
262: br.close();
263:
264: return true;
265: }
266:
267: // check if second line contains Profiler header (first line can contain #!/bin/sh for UNIX scripts or <?xml version...> for xml file)
268: line = br.readLine();
269:
270: if (line == null) {
271: br.close();
272:
273: return false;
274: }
275:
276: if (line.indexOf(MODIFIED_FOR_PROFILER_STRING) != -1) {
277: br.close();
278:
279: return true;
280: }
281:
282: br.close();
283:
284: return false;
285: } catch (Exception ex) {
286: return false;
287: }
288: }
289:
290: // converts CommonConstants.JDK_15_STRING and CommonConstants.JDK_16_STRING to
291: // IntegrationUtils.PLATFORM_JAVA_50 and IntegrationUtils.PLATFORM_JAVA_60.
292: public static String getJavaPlatformFromJavaVersionString(
293: String javaVersionString) {
294: if (javaVersionString.equals(CommonConstants.JDK_15_STRING)) {
295: return PLATFORM_JAVA_50;
296: } else if (javaVersionString
297: .equals(CommonConstants.JDK_16_STRING)) {
298: return PLATFORM_JAVA_60;
299: } else if (javaVersionString
300: .equals(CommonConstants.JDK_17_STRING)) {
301: return PLATFORM_JAVA_70;
302: }
303:
304: return null;
305: }
306:
307: // Returns friendly java platform name
308: public static String getJavaPlatformName(String javaPlatform) {
309: if (javaPlatform.equals(PLATFORM_JAVA_50)) {
310: return JDK_50_NAME;
311: } else if (javaPlatform.equals(PLATFORM_JAVA_60)) {
312: return JDK_60_NAME;
313: } else if (javaPlatform.equals(PLATFORM_JAVA_70)) {
314: return JDK_70_NAME;
315: }
316:
317: return javaPlatform;
318: }
319:
320: // Returns java platform-specific directory: jdk15 / jdk16
321: public static String getJavaPlatformNativeLibrariesDirectoryName(
322: String javaPlatform) {
323: if (javaPlatform.equals(PLATFORM_JAVA_50)) {
324: return "jdk15"; //NOI18N
325: } else if (javaPlatform.equals(PLATFORM_JAVA_60)) {
326: return "jdk16"; //NOI18N
327: } else if (javaPlatform.equals(PLATFORM_JAVA_70)) {
328: return "jdk16"; //NOI18N // for JDK 7.0 we use the same as for 6.0 for now
329: }
330:
331: throw new IllegalArgumentException("Unsupported platform "
332: + javaPlatform); // NOI18N
333: }
334:
335: // Returns the path to agent native libraries according to current / selected OS
336: public static String getLibsDir(String targetPlatform,
337: boolean isRemote) {
338: if (isRemote) {
339: return HTML_REMOTE_STRING
340: + getDirectorySeparator(targetPlatform) + "lib"; //NOI18N
341: }
342:
343: return Profiler.getDefault().getLibsDir();
344: }
345:
346: public static String getLineBreak(String targetPlatform) {
347: if (isWindowsPlatform(targetPlatform)) {
348: return "\r\n"; //NOI18N
349: }
350:
351: return "\n"; //NOI18N
352: }
353:
354: // Returns current underlying Java platform
355: public static String getLocalJavaPlatform() {
356: int jdkVersion = Platform.getJDKVersionNumber();
357:
358: if (jdkVersion == Platform.JDK_15) {
359: return PLATFORM_JAVA_50;
360: } else if (jdkVersion == Platform.JDK_16) {
361: return PLATFORM_JAVA_60;
362: } else if (jdkVersion == Platform.JDK_17) {
363: return PLATFORM_JAVA_70;
364: }
365:
366: return null;
367: }
368:
369: // Returns locally running OS platform
370: public static String getLocalPlatform(int architecture) {
371: if (architecture == -1) {
372: architecture = Platform.getSystemArchitecture();
373: }
374:
375: if (architecture == Platform.ARCH_32) {
376: if (Platform.isWindows()) {
377: return PLATFORM_WINDOWS_OS;
378: } else if (Platform.isLinux()) {
379: return PLATFORM_LINUX_OS;
380: } else if (Platform.isSolarisIntel()) {
381: return PLATFORM_SOLARIS_INTEL_OS;
382: } else if (Platform.isSolarisSparc()) {
383: return PLATFORM_SOLARIS_SPARC_OS;
384: } else if (Platform.isMac()) {
385: return PLATFORM_MAC_OS;
386: }
387:
388: return PLATFORM_SOLARIS_SPARC_OS; // Not supported platform => assume UNIX
389: } else {
390: if (Platform.isWindows()) {
391: return PLATFORM_WINDOWS_AMD64_OS;
392: } else if (Platform.isLinux()) {
393: return PLATFORM_LINUX_AMD64_OS;
394: } else if (Platform.isSolarisIntel()) {
395: return PLATFORM_SOLARIS_AMD64_OS;
396: } else if (Platform.isSolarisSparc()) {
397: return PLATFORM_SOLARIS_SPARC64_OS;
398: } else if (Platform.isMac()) {
399: return PLATFORM_MAC_OS;
400: }
401:
402: return PLATFORM_SOLARIS_SPARC64_OS; // Not supported platform => assume UNIX
403: }
404: }
405:
406: public static String getManualRemoteStep1(String targetOS,
407: String targetJVM) {
408: return MessageFormat.format(MANUAL_REMOTE_STEP1_MESSAGE,
409: new Object[] { "JDK 5.0/6.0/7.0", targetOS,
410: HTML_REMOTE_STRING }); //NOI18N
411: }
412:
413: public static String getManualRemoteStep2(String targetOS,
414: String targetJVM) {
415: return MessageFormat
416: .format(
417: MANUAL_REMOTE_STEP2_MESSAGE,
418: new Object[] { getRemoteCalibrateCommandString(targetOS) }); //NOI18N
419: }
420:
421: // Returns getLibsDir()/deployed/jdk<15>/<OS> appropriate for current / selected OS
422: public static String getNativeLibrariesPath(String targetPlatform,
423: String targetJVM, boolean isRemote) {
424: return getLibsDir(targetPlatform, isRemote)
425: + getDirectorySeparator(targetPlatform)
426: + "deployed" //NOI18N
427: + getDirectorySeparator(targetPlatform)
428: + getJavaPlatformNativeLibrariesDirectoryName(targetJVM)
429: + getDirectorySeparator(targetPlatform)
430: + getOSPlatformNativeLibrariesDirectoryName(
431: targetPlatform, isRemote);
432: }
433:
434: // Returns name of the environment variable for system path to Profiler native libraries appropriate for current / selected OS
435: public static String getNativePathEnvVariableString(
436: String targetPlatform) {
437: if (isWindowsPlatform(targetPlatform)) {
438: return "Path"; //NOI18N
439: }
440:
441: return "LD_LIBRARY_PATH"; //NOI18N
442: }
443:
444: // returns OS platform- and location-specific directory
445: public static String getOSPlatformNativeLibrariesDirectoryName(
446: String targetPlatform, boolean isRemote) {
447: if (targetPlatform.equals(PLATFORM_WINDOWS_OS)) {
448: return "windows"; //NOI18N
449: } else if (targetPlatform.equals(PLATFORM_WINDOWS_AMD64_OS)) {
450: return "windows-amd64"; //NOI18N
451: } else if (targetPlatform.equals(PLATFORM_LINUX_OS)) {
452: return "linux"; //NOI18N
453: } else if (targetPlatform.equals(PLATFORM_LINUX_AMD64_OS)) {
454: return "linux-amd64"; //NOI18N
455: } else if (targetPlatform.equals(PLATFORM_SOLARIS_INTEL_OS)) {
456: return "solaris-i386"; //NOI18N
457: } else if (targetPlatform.equals(PLATFORM_SOLARIS_AMD64_OS)) {
458: return "solaris-amd64"; //NOI18N
459: } else if (targetPlatform.equals(PLATFORM_SOLARIS_SPARC_OS)) {
460: return "solaris-sparc"; //NOI18N
461: } else if (targetPlatform.equals(PLATFORM_SOLARIS_SPARC64_OS)) {
462: return "solaris-sparcv9"; //NOI18N
463: } else if (targetPlatform.equals(PLATFORM_MAC_OS)) {
464: return "mac"; //NOI18N
465: }
466:
467: return null;
468: }
469:
470: // Returns ";" or ":" according to provided platform
471: public static String getPathSeparator(String targetPlatform) {
472: if (isWindowsPlatform(targetPlatform)) {
473: return ";"; //NOI18N
474: }
475:
476: return ":"; //NOI18N
477: }
478:
479: // Returns extra command line arguments required when attaching on startup
480: public static String getProfilerAgentCommandLineArgs(
481: String targetPlatform, String targetJVM, boolean isRemote,
482: int portNumber) {
483: if ((getNativeLibrariesPath(targetPlatform, targetJVM, isRemote)
484: .indexOf(' ') == -1)) {
485: return getProfilerAgentCommandLineArgsWithoutQuotes(
486: targetPlatform, targetJVM, isRemote, portNumber); //NOI18N
487: }
488: if (!isWindowsPlatform(targetPlatform)) { // Profiler is installed in directory with space on Unix (Linux, Solaris, Mac OS X)
489: // create temporary link in /tmp directory and use it instead of directory with space
490: try {
491: File tmpFile = File.createTempFile("NBProfiler",
492: ".link"); // NOI18N
493: String tmpPath = tmpFile.getAbsolutePath();
494: String libsDirPath = getLibsDir(targetPlatform,
495: isRemote);
496: String args = getProfilerAgentCommandLineArgsWithoutQuotes(
497: targetPlatform, targetJVM, isRemote, portNumber);
498: tmpFile.delete();
499: Runtime.getRuntime().exec(
500: new String[] { "/bin/ln", "-s", libsDirPath,
501: tmpPath }); // NOI18N
502: new File(tmpPath).deleteOnExit();
503: return args.replace(libsDirPath, tmpPath);
504: } catch (IOException ex) {
505: throw new RuntimeException(ex);
506: }
507: }
508:
509: return "-agentpath:"
510: + "\""
511: + getNativeLibrariesPath(targetPlatform, targetJVM,
512: isRemote)
513: + getDirectorySeparator(targetPlatform)
514: + getProfilerAgentLibraryFile(targetPlatform)
515: + "=" //NOI18N
516: + "\\\"" + getLibsDir(targetPlatform, isRemote)
517: + "\\\"\"" + "," + portNumber; //NOI18N
518: }
519:
520: // Returns extra command line arguments without additional quotes required when attaching on startup
521: public static String getProfilerAgentCommandLineArgsWithoutQuotes(
522: String targetPlatform, String targetJVM, boolean isRemote,
523: int portNumber) {
524: return "-agentpath:"
525: + getNativeLibrariesPath(targetPlatform, targetJVM,
526: isRemote)
527: + getDirectorySeparator(targetPlatform)
528: + getProfilerAgentLibraryFile(targetPlatform)
529: + "=" //NOI18N
530: + getLibsDir(targetPlatform, isRemote) + ","
531: + portNumber; //NOI18N
532: }
533:
534: // Returns filename of profiler agent library
535: public static String getProfilerAgentLibraryFile(
536: String targetPlatform) {
537: if (isWindowsPlatform(targetPlatform)) {
538: return "profilerinterface.dll"; //NOI18N
539: } else if (targetPlatform.equals(PLATFORM_MAC_OS)) {
540: return "libprofilerinterface.jnilib"; //NOI18N
541: } else {
542: return "libprofilerinterface.so"; //NOI18N
543: }
544: }
545:
546: public static String getProfilerModifiedFileHeader(
547: String targetPlatform) {
548: return getSilentScriptCommentSign(targetPlatform) + " "
549: + MODIFIED_FOR_PROFILER_STRING
550: + getLineBreak(targetPlatform); //NOI18N
551: }
552:
553: public static String getProfilerModifiedReplaceFileHeader(
554: String targetPlatform) {
555: return getProfilerModifiedFileHeader(targetPlatform)
556: + getSilentScriptCommentSign(targetPlatform) + " "
557: + ORIGINAL_BACKUP_LOCATION_STRING
558: + getLineBreak(targetPlatform); //NOI18N
559: }
560:
561: public static String getRemoteAbsolutePathHint() {
562: return MessageFormat.format(REMOTE_ABSOLUTE_PATH_HINT,
563: new Object[] { HTML_REMOTE_STRING });
564: }
565:
566: // Returns calibration batch filename
567: public static String getRemoteCalibrateCommandString(
568: String targetPlatform) {
569: return HTML_REMOTE_STRING
570: + getDirectorySeparator(targetPlatform) + "bin"
571: + getDirectorySeparator(targetPlatform) + "calibrate"
572: + getBatchExtensionString(targetPlatform); //NOI18N
573: }
574:
575: // Returns profile batch filename
576: public static String getRemoteProfileCommandString(
577: String targetPlatform) {
578: return HTML_REMOTE_STRING
579: + getDirectorySeparator(targetPlatform) + "bin"
580: + getDirectorySeparator(targetPlatform) + "profile"
581: + getBatchExtensionString(targetPlatform); //NOI18N
582: }
583:
584: // returns "rem" or "#" according to provided platform
585: public static String getScriptCommentSign(String targetPlatform) {
586: if (isWindowsPlatform(targetPlatform)) {
587: return "rem"; //NOI18N
588: }
589:
590: return "#"; //NOI18N
591: }
592:
593: // returns "@rem" or "#" according to provided platform
594: public static String getSilentScriptCommentSign(
595: String targetPlatform) {
596: if (isWindowsPlatform(targetPlatform)) {
597: return "@rem"; //NOI18N
598: }
599:
600: return "#"; //NOI18N
601: }
602:
603: public static String getSpacesInPathWarning() {
604: return SPACES_IN_PATH_WARNING_MSG;
605: }
606:
607: // Returns friendly name of the integration target type
608: public static String getTargetTypeName(String targetType,
609: boolean startsUpperCase) {
610: if (targetType
611: .equals(IntegrationProvider2.TARGET_TYPE_APPLICATION)) {
612: return (startsUpperCase ? APPLICATION_STRING
613: : APPLICATION_STRING.toLowerCase());
614: } else if (targetType
615: .equals(IntegrationProvider2.TARGET_TYPE_APPLET)) {
616: return (startsUpperCase ? APPLET_STRING : APPLET_STRING
617: .toLowerCase());
618: } else if (targetType
619: .equals(IntegrationProvider2.TARGET_TYPE_J2EEAPPLICATION)) {
620: return (startsUpperCase ? SERVER_STRING : SERVER_STRING
621: .toLowerCase());
622: } else if (targetType
623: .equals(IntegrationProvider2.TARGET_TYPE_DATABASE)) {
624: return (startsUpperCase ? DATABASE_STRING : DATABASE_STRING
625: .toLowerCase());
626: }
627:
628: return (startsUpperCase ? TARGET_STRING : TARGET_STRING
629: .toLowerCase());
630: }
631:
632: public static boolean isWindowsPlatform(String targetPlatform) {
633: return targetPlatform.equals(PLATFORM_WINDOWS_OS)
634: || targetPlatform.equals(PLATFORM_WINDOWS_AMD64_OS);
635: }
636:
637: public static String getXMLCommendEndSign() {
638: return "-->"; //NOI18N
639: }
640:
641: public static String getXMLCommentStartSign() {
642: return "<!--"; //NOI18N
643: }
644:
645: public static boolean backupFile(File file) {
646: File source = new File(file.getAbsolutePath());
647: File target = new File(source.getAbsolutePath()
648: + FILE_BACKUP_EXTENSION);
649:
650: if (!source.exists()) {
651: ProfilerLogger.severe(MessageFormat.format(
652: BACKUP_FILE_NOT_FOUND_MESSAGE,
653: new Object[] { source.getAbsolutePath() })); //NOI18N
654:
655: return false;
656: }
657:
658: if (target.exists()) {
659: if (!target.delete()) {
660: ProfilerLogger.severe(MessageFormat.format(
661: BACKUP_CANNOT_DELETE_FILE_MESSAGE,
662: new Object[] { target.getAbsolutePath() })); //NOI18N
663:
664: return false;
665: }
666: }
667:
668: // move source to target to correctly preserve file permissions
669: if (!source.renameTo(target)) {
670: ProfilerLogger.severe(MessageFormat.format(
671: BACKUP_ERROR_MESSAGE, new Object[] {
672: source.getAbsolutePath(),
673: target.getAbsolutePath() })); //NOI18N
674:
675: return false;
676: }
677:
678: // re-create source file for further processing
679: try {
680: source = new File(file.getAbsolutePath());
681: source.createNewFile();
682: target = new File(source.getAbsolutePath()
683: + FILE_BACKUP_EXTENSION);
684:
685: FileChannel sourceChannel = new FileOutputStream(source)
686: .getChannel();
687: FileChannel targetChannel = new FileInputStream(target)
688: .getChannel();
689: targetChannel.transferTo(0, targetChannel.size(),
690: sourceChannel);
691: targetChannel.close();
692: sourceChannel.close();
693:
694: return true;
695: } catch (Exception ex) {
696: ProfilerLogger.severe(MessageFormat.format(
697: BACKUP_ERROR_COPY_FILE_MESSAGE, new Object[] {
698: target.getAbsolutePath(),
699: source.getAbsolutePath(), ex })); //NOI18N
700:
701: return false;
702: }
703: }
704:
705: public static boolean copyFile(File sourceFile, File targetFile) {
706: if (!sourceFile.exists()) {
707: ProfilerLogger.severe(MessageFormat.format(
708: COPY_FILE_NOT_FOUND_MESSAGE,
709: new Object[] { sourceFile.getAbsolutePath() })); //NOI18N
710:
711: return false;
712: }
713:
714: if (targetFile.exists()) {
715: if (!targetFile.delete()) {
716: ProfilerLogger.severe(MessageFormat.format(
717: COPY_CANNOT_DELETE_FILE_MESSAGE,
718: new Object[] { targetFile.getAbsolutePath() })); //NOI18N
719:
720: return false;
721: }
722: }
723:
724: try {
725: FileChannel sourceChannel = new FileInputStream(sourceFile)
726: .getChannel();
727: FileChannel destinationChannel = new FileOutputStream(
728: targetFile).getChannel();
729: sourceChannel.transferTo(0, sourceChannel.size(),
730: destinationChannel);
731: sourceChannel.close();
732: destinationChannel.close();
733:
734: return true;
735: } catch (Exception ex) {
736: ProfilerLogger.log(ex);
737: ProfilerLogger.severe(MessageFormat.format(
738: COPY_ERROR_MESSAGE, new Object[] {
739: sourceFile.getAbsolutePath(),
740: targetFile.getAbsolutePath() })); //NOI18N
741:
742: return false;
743: }
744: }
745:
746: public static boolean fileBackupExists(File file) {
747: File target = new File(file.getAbsolutePath()); // file to be restored
748: File source = new File(target.getAbsolutePath()
749: + FILE_BACKUP_EXTENSION); // backup image of this file (file.backup)
750:
751: return source.exists();
752: }
753:
754: public static boolean restoreFile(File file) {
755: File target = file;
756: File source = new File(target.getAbsolutePath()
757: + FILE_BACKUP_EXTENSION);
758:
759: if (!source.exists()) {
760: ProfilerLogger.severe(MessageFormat.format(
761: RESTORE_FILE_NOT_FOUND_MESSAGE, new Object[] {
762: source.getAbsolutePath(),
763: target.getAbsolutePath() })); //NOI18N
764:
765: return false;
766: }
767:
768: if (target.exists()) {
769: if (!target.delete()) {
770: ProfilerLogger.severe(MessageFormat.format(
771: RESTORE_CANNOT_DELETE_FILE_MESSAGE,
772: new Object[] { target.getAbsolutePath() })); //NOI18N
773:
774: return false;
775: }
776: }
777:
778: if (!source.renameTo(target)) {
779: ProfilerLogger.severe(MessageFormat.format(
780: RESTORE_ERROR_MESSAGE, new Object[] {
781: source.getAbsolutePath(),
782: target.getAbsolutePath() })); //NOI18N
783:
784: return false;
785: }
786:
787: return true;
788: }
789: }
|