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 General
007: * Public License Version 2 only ("GPL") or the Common Development and Distribution
008: * License("CDDL") (collectively, the "License"). You may not use this file except in
009: * compliance with the License. You can obtain a copy of the License at
010: * http://www.netbeans.org/cddl-gplv2.html or nbbuild/licenses/CDDL-GPL-2-CP. See the
011: * License for the specific language governing permissions and limitations under the
012: * License. When distributing the software, include this License Header Notice in
013: * each file and include the License file at nbbuild/licenses/CDDL-GPL-2-CP. Sun
014: * designates this particular file as subject to the "Classpath" exception as
015: * provided by Sun in the GPL Version 2 section of the License file that
016: * accompanied this code. If applicable, add the following below the License Header,
017: * with the fields enclosed by brackets [] replaced by your own identifying
018: * information: "Portions Copyrighted [year] [name of copyright owner]"
019: *
020: * Contributor(s):
021: *
022: * The Original Software is NetBeans. The Initial Developer of the Original Software
023: * is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun Microsystems, Inc. All
024: * Rights Reserved.
025: *
026: * If you wish your version of this file to be governed by only the CDDL or only the
027: * GPL Version 2, indicate your decision by adding "[Contributor] elects to include
028: * this software in this distribution under the [CDDL or GPL Version 2] license." If
029: * you do not indicate a single choice of license, a recipient has the option to
030: * distribute your version of this file under either the CDDL, the GPL Version 2 or
031: * to extend the choice of license to its licensees as provided above. However, if
032: * you add GPL Version 2 code and therefore, elected the GPL Version 2 license, then
033: * the option applies only if the new code is made subject to such option by the
034: * copyright holder.
035: */
036:
037: package org.netbeans.installer.wizard.components.actions;
038:
039: import java.io.File;
040: import java.io.FileOutputStream;
041: import java.io.IOException;
042: import java.util.ArrayList;
043: import java.util.zip.ZipOutputStream;
044: import org.netbeans.installer.Installer;
045: import org.netbeans.installer.product.Registry;
046: import org.netbeans.installer.utils.ErrorManager;
047: import org.netbeans.installer.utils.FileProxy;
048: import org.netbeans.installer.utils.FileUtils;
049: import org.netbeans.installer.utils.LogManager;
050: import org.netbeans.installer.utils.ResourceUtils;
051: import org.netbeans.installer.utils.StringUtils;
052: import org.netbeans.installer.utils.SystemUtils;
053: import org.netbeans.installer.utils.applications.JavaUtils;
054: import org.netbeans.installer.utils.exceptions.DownloadException;
055: import org.netbeans.installer.utils.helper.Platform;
056: import org.netbeans.installer.utils.progress.Progress;
057: import org.netbeans.installer.utils.system.launchers.LauncherProperties;
058: import org.netbeans.installer.utils.system.launchers.LauncherResource;
059: import org.netbeans.installer.utils.system.launchers.impl.CommandLauncher;
060: import org.netbeans.installer.wizard.components.WizardAction;
061:
062: /**
063: *
064: * @author Dmitry Lipin
065: */
066: public class CreateMacOSAppLauncherAction extends WizardAction {
067: // Constants
068: public static final String DEFAULT_TITLE = ResourceUtils.getString(
069: CreateMacOSAppLauncherAction.class, "CMALA.title"); // NOI18N
070:
071: public static final String DEFAULT_DESCRIPTION = ResourceUtils
072: .getString(CreateMacOSAppLauncherAction.class,
073: "CMALA.description"); // NOI18N
074: public static final String DEFAULT_ERROR_FAILED_CREATE_LAUNCHER = ResourceUtils
075: .getString(CreateMacOSAppLauncherAction.class,
076: "CMALA.error.failed.create.launcher");//NOI18N
077: public static final String ERROR_FAILED_CREATE_LAUNCHER_PROPERTY = "error.failed.create.launcher";//NOI18N
078:
079: public static final String APP_NAME_PROPERTY = "nbi.macosx.application.directory.name"; // NOI18N
080:
081: public static final String DEFAULT_APP_DIRECTORY_NAME = "NetBeans Installer"; // NOI18N
082:
083: public static final String DEFAULT_ICNS_ICON_NAME = "icon.icns"; //NOI18N
084:
085: public CreateMacOSAppLauncherAction() {
086: setProperty(TITLE_PROPERTY, DEFAULT_TITLE);
087: setProperty(DESCRIPTION_PROPERTY, DEFAULT_DESCRIPTION);
088: setProperty(ERROR_FAILED_CREATE_LAUNCHER_PROPERTY,
089: DEFAULT_ERROR_FAILED_CREATE_LAUNCHER);
090: }
091:
092: public void execute() {
093: LogManager.logEntry("creating the macosx app launcher");
094:
095: final String targetPath = System
096: .getProperty(Registry.CREATE_BUNDLE_PATH_PROPERTY);
097: final File targetFile = new File(targetPath);
098:
099: final Progress progress = new Progress();
100:
101: getWizardUi().setProgress(progress);
102:
103: try {
104: final Platform platform = Registry.getInstance()
105: .getTargetPlatform();
106: final LauncherProperties properties = new LauncherProperties();
107:
108: final String appNameSystem = System
109: .getProperty(APP_NAME_PROPERTY);
110:
111: final String appName = (appNameSystem != null) ? appNameSystem
112: : DEFAULT_APP_DIRECTORY_NAME;
113:
114: final String testJDKName = ResourceUtils
115: .getResourceFileName(JavaUtils.TEST_JDK_RESOURCE);
116: properties.addJar(new LauncherResource(
117: LauncherResource.Type.RELATIVE_LAUNCHER_PARENT,
118: "../Resources/" + appName + "/"
119: + new File(targetPath).getName()));
120: properties.setTestJVM(new LauncherResource(
121: LauncherResource.Type.RELATIVE_LAUNCHER_PARENT,
122: "../Resources/" + appName + "/" + testJDKName));
123:
124: properties.setJvmArguments(new String[] { "-Xmx256m",
125: "-Xms64m" });
126:
127: properties.setMainClass(Installer.class.getName());
128: properties.setTestJVMClass(JavaUtils.TEST_JDK_CLASSNAME);
129:
130: File tmpDirectory = FileUtils.createTempFile(SystemUtils
131: .getTempDirectory(), false, true);
132: FileUtils.mkdirs(tmpDirectory);
133:
134: File appDirectory = new File(tmpDirectory, appName + ".app");
135: File contentsDirectory = new File(appDirectory, "Contents");
136: File resDirectory = new File(contentsDirectory, "Resources");
137: File macosDirectory = new File(contentsDirectory, "MacOS");
138: File appInsideDir = new File(resDirectory, appName);
139: File outputFile = new File(macosDirectory, "executable");
140:
141: FileUtils.mkdirs(appDirectory);
142: FileUtils.mkdirs(contentsDirectory);
143: FileUtils.mkdirs(resDirectory);
144: FileUtils.mkdirs(appInsideDir);
145: FileUtils.mkdirs(macosDirectory);
146:
147: final String iconName = DEFAULT_ICNS_ICON_NAME;
148:
149: properties.setOutput(outputFile, false);
150:
151: String iconUri = System
152: .getProperty(CommandLauncher.JAVA_APPLICATION_ICON_PROPERTY);
153:
154: if (iconUri == null) {
155: iconUri = CommandLauncher.JAVA_APPLICATION_ICON_DEFAULT_URI;
156: }
157:
158: File iconFile = FileProxy.getInstance().getFile(iconUri,
159: true);
160:
161: File iconFileTarget = new File(resDirectory, iconName);
162:
163: FileUtils.copyFile(iconFile, iconFileTarget);
164:
165: properties
166: .getJvmArguments()
167: .add(
168: "-Xdock:icon="
169: + LauncherResource.Type.RELATIVE_LAUNCHER_PARENT
170: .getPathString("../Resources/"
171: + iconName));
172:
173: File file = SystemUtils.createLauncher(properties,
174: platform, progress).getOutputFile();
175:
176: File testJDKFile = FileProxy.getInstance().getFile(
177: JavaUtils.TEST_JDK_URI, true);
178:
179: FileUtils.copyFile(testJDKFile, new File(appInsideDir,
180: testJDKName));
181:
182: FileUtils.copyFile(targetFile, new File(appInsideDir,
183: targetFile.getName()));
184:
185: File infoplist = new File(contentsDirectory, "Info.plist");
186: FileUtils.writeFile(infoplist, StringUtils.format(
187: FileUtils.INFO_PLIST_STUB, appName, 1.0, 0,
188: outputFile.getName(), iconFileTarget.getName()));
189:
190: final String name = targetFile.getName();
191: final int index = name.lastIndexOf(".");
192: final String zipName = name.substring(0,
193: (index == -1) ? name.length() : index)
194: + ".zip";
195:
196: File zipFile = new File(targetFile.getParentFile(), zipName);
197: ZipOutputStream zos = new ZipOutputStream(
198: new FileOutputStream(zipFile));
199:
200: FileUtils.zip(appDirectory, zos, appDirectory
201: .getParentFile(), new ArrayList<File>());
202: zos.close();
203: FileUtils.deleteFile(tmpDirectory, true);
204:
205: System.setProperty(Registry.CREATE_BUNDLE_PATH_PROPERTY,
206: zipFile.getPath());
207: if (!targetFile.equals(file)) {
208: FileUtils.deleteFile(targetFile);
209: System.setProperty(
210: Registry.CREATE_BUNDLE_PATH_PROPERTY, file
211: .getPath());
212:
213: }
214: } catch (IOException e) {
215: ErrorManager.notifyError(
216: getProperty(ERROR_FAILED_CREATE_LAUNCHER_PROPERTY),
217: e);
218: } catch (DownloadException e) {
219: ErrorManager.notifyError(
220: getProperty(ERROR_FAILED_CREATE_LAUNCHER_PROPERTY),
221: e);
222: }
223:
224: LogManager.logExit("finished creating the app launcher");
225: }
226: }
|