PageRenderTime 44ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/projects/netbeans-7.3/libs.nbi.ant/stub/ext/components/products/helloworld/src/org/mycompany/ConfigurationLogic.java

https://gitlab.com/essere.lab.public/qualitas.class-corpus
Java | 479 lines | 361 code | 50 blank | 68 comment | 41 complexity | 451045678bea46afd1af5159f8f2928e MD5 | raw file
  1. /*
  2. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
  3. *
  4. * Copyright 1997-2011 Oracle and/or its affiliates. All rights reserved.
  5. *
  6. * Oracle and Java are registered trademarks of Oracle and/or its affiliates.
  7. * Other names may be trademarks of their respective owners.
  8. *
  9. * The contents of this file are subject to the terms of either the GNU
  10. * General Public License Version 2 only ("GPL") or the Common
  11. * Development and Distribution License("CDDL") (collectively, the
  12. * "License"). You may not use this file except in compliance with the
  13. * License. You can obtain a copy of the License at
  14. * http://www.netbeans.org/cddl-gplv2.html
  15. * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
  16. * specific language governing permissions and limitations under the
  17. * License. When distributing the software, include this License Header
  18. * Notice in each file and include the License file at
  19. * nbbuild/licenses/CDDL-GPL-2-CP. Oracle designates this
  20. * particular file as subject to the "Classpath" exception as provided
  21. * by Oracle in the GPL Version 2 section of the License file that
  22. * accompanied this code. If applicable, add the following below the
  23. * License Header, with the fields enclosed by brackets [] replaced by
  24. * your own identifying information:
  25. * "Portions Copyrighted [year] [name of copyright owner]"
  26. *
  27. * Contributor(s):
  28. *
  29. * The Original Software is NetBeans. The Initial Developer of the Original
  30. * Software is Sun Microsystems, Inc. Portions Copyright 1997-2011 Sun
  31. * Microsystems, Inc. All Rights Reserved.
  32. *
  33. * If you wish your version of this file to be governed by only the CDDL
  34. * or only the GPL Version 2, indicate your decision by adding
  35. * "[Contributor] elects to include this software in this distribution
  36. * under the [CDDL or GPL Version 2] license." If you do not indicate a
  37. * single choice of license, a recipient has the option to distribute
  38. * your version of this file under either the CDDL, the GPL Version 2 or
  39. * to extend the choice of license to its licensees as provided above.
  40. * However, if you add GPL Version 2 code and therefore, elected the GPL
  41. * Version 2 license, then the option applies only if the new code is
  42. * made subject to such option by the copyright holder.
  43. */
  44. package org.mycompany;
  45. import java.util.List;
  46. import java.io.File;
  47. import java.io.FileOutputStream;
  48. import java.io.IOException;
  49. import java.io.InputStream;
  50. import java.util.Locale;
  51. import java.util.Map;
  52. import org.mycompany.wizard.panels.HelloWorldPanel;
  53. import org.mycompany.installer.utils.applications.NetBeansRCPUtils;
  54. import org.netbeans.installer.product.components.ProductConfigurationLogic;
  55. import org.netbeans.installer.product.components.Product;
  56. import org.netbeans.installer.utils.FileUtils;
  57. import org.netbeans.installer.utils.helper.RemovalMode;
  58. import org.netbeans.installer.utils.exceptions.InitializationException;
  59. import org.netbeans.installer.utils.exceptions.InstallationException;
  60. import org.netbeans.installer.utils.exceptions.UninstallationException;
  61. import org.netbeans.installer.utils.progress.Progress;
  62. import org.netbeans.installer.utils.system.shortcut.FileShortcut;
  63. import org.netbeans.installer.utils.system.shortcut.LocationType;
  64. import org.netbeans.installer.utils.system.shortcut.Shortcut;
  65. import org.netbeans.installer.utils.SystemUtils;
  66. import org.netbeans.installer.utils.LogManager;
  67. import org.netbeans.installer.utils.ResourceUtils;
  68. import org.netbeans.installer.utils.StreamUtils;
  69. import org.netbeans.installer.utils.StringUtils;
  70. import org.netbeans.installer.utils.exceptions.NativeException;
  71. import org.netbeans.installer.wizard.Wizard;
  72. import org.netbeans.installer.wizard.components.WizardComponent;
  73. public class ConfigurationLogic extends ProductConfigurationLogic {
  74. private List<WizardComponent> wizardComponents;
  75. // constructor //////////////////////////////////////////////////////////////////
  76. public ConfigurationLogic() throws InitializationException {
  77. wizardComponents = Wizard.loadWizardComponents(
  78. WIZARD_COMPONENTS_URI,
  79. getClass().getClassLoader());
  80. }
  81. public List<WizardComponent> getWizardComponents() {
  82. return wizardComponents;
  83. }
  84. @Override
  85. public boolean allowModifyMode() {
  86. return false;
  87. }
  88. @Override
  89. public void install(Progress progress) throws InstallationException {
  90. final Product product = getProduct();
  91. final File installLocation = product.getInstallationLocation();
  92. //final FilesList filesList = product.getInstalledFiles();
  93. if (SystemUtils.isMacOS()) {
  94. File f = new File(installLocation, ICON_MACOSX);
  95. if(!f.exists()) {
  96. try {
  97. FileUtils.writeFile(f,
  98. ResourceUtils.getResource(ICON_MACOSX_RESOURCE,
  99. getClass().getClassLoader()));
  100. getProduct().getInstalledFiles().add(f);
  101. } catch (IOException e) {
  102. LogManager.log(
  103. "... cannot handle icns icon " + f, e); // NOI18N
  104. }
  105. }
  106. }
  107. if (Boolean.parseBoolean(getProperty(HelloWorldPanel.CREATE_DESKTOP_SHORTCUT_PROPERTY))) {
  108. LogManager.logIndent(
  109. "creating the desktop shortcut for the application"); // NOI18N
  110. if (!SystemUtils.isMacOS()) {
  111. try {
  112. progress.setDetail(getString("CL.install.desktop")); // NOI18N
  113. if (SystemUtils.isCurrentUserAdmin()) {
  114. LogManager.log(
  115. "... current user is an administrator " + // NOI18N
  116. "-- creating the shortcut for all users"); // NOI18N
  117. SystemUtils.createShortcut(
  118. getDesktopShortcut(installLocation),
  119. LocationType.ALL_USERS_DESKTOP);
  120. product.setProperty(
  121. DESKTOP_SHORTCUT_LOCATION_PROPERTY,
  122. ALL_USERS_PROPERTY_VALUE);
  123. } else {
  124. LogManager.log(
  125. "... current user is an ordinary user " + // NOI18N
  126. "-- creating the shortcut for the current " + // NOI18N
  127. "user only"); // NOI18N
  128. SystemUtils.createShortcut(
  129. getDesktopShortcut(installLocation),
  130. LocationType.CURRENT_USER_DESKTOP);
  131. getProduct().setProperty(
  132. DESKTOP_SHORTCUT_LOCATION_PROPERTY,
  133. CURRENT_USER_PROPERTY_VALUE);
  134. }
  135. } catch (NativeException e) {
  136. LogManager.unindent();
  137. LogManager.log(
  138. getString("CL.install.error.desktop"), // NOI18N
  139. e);
  140. }
  141. } else {
  142. LogManager.log(
  143. "... skipping this step as we're on Mac OS"); // NOI18N
  144. }
  145. }
  146. LogManager.logUnindent(
  147. "... done"); // NOI18N
  148. /////////////////////////////////////////////
  149. // create start menu shortcut
  150. if (Boolean.parseBoolean(getProperty(HelloWorldPanel.CREATE_START_MENU_SHORTCUT_PROPERTY))) {
  151. LogManager.logIndent(
  152. "creating the start menu shortcut for the application"); // NOI18N
  153. try {
  154. progress.setDetail(getString("CL.install.start.menu")); // NOI18N
  155. if (SystemUtils.isCurrentUserAdmin()) {
  156. LogManager.log(
  157. "... current user is an administrator " + // NOI18N
  158. "-- creating the shortcut for all users"); // NOI18N
  159. SystemUtils.createShortcut(
  160. getStartMenuShortcut(installLocation),
  161. LocationType.ALL_USERS_START_MENU);
  162. getProduct().setProperty(
  163. START_MENU_SHORTCUT_LOCATION_PROPERTY,
  164. ALL_USERS_PROPERTY_VALUE);
  165. } else {
  166. LogManager.log(
  167. "... current user is an ordinary user " + // NOI18N
  168. "-- creating the shortcut for the current " + // NOI18N
  169. "user only"); // NOI18N
  170. SystemUtils.createShortcut(
  171. getStartMenuShortcut(installLocation),
  172. LocationType.CURRENT_USER_START_MENU);
  173. getProduct().setProperty(
  174. START_MENU_SHORTCUT_LOCATION_PROPERTY,
  175. CURRENT_USER_PROPERTY_VALUE);
  176. }
  177. } catch (NativeException e) {
  178. LogManager.log(
  179. getString("CL.install.error.start.menu"), // NOI18N
  180. e);
  181. }
  182. LogManager.logUnindent(
  183. "... done"); // NOI18N
  184. }
  185. }
  186. @Override
  187. public void uninstall(Progress progress) throws UninstallationException {
  188. final Product product = getProduct();
  189. final File installLocation = product.getInstallationLocation();
  190. //NetBeansUtils.warnNetbeansRunning(installLocation);
  191. /////////////////////////////////////////////////////////////////////////////
  192. if (Boolean.parseBoolean(getProperty(HelloWorldPanel.CREATE_START_MENU_SHORTCUT_PROPERTY))) {
  193. try {
  194. progress.setDetail(getString("CL.uninstall.start.menu")); // NOI18N
  195. final String shortcutLocation =
  196. getProduct().getProperty(START_MENU_SHORTCUT_LOCATION_PROPERTY);
  197. if ((shortcutLocation == null)
  198. || shortcutLocation.equals(CURRENT_USER_PROPERTY_VALUE)) {
  199. SystemUtils.removeShortcut(
  200. getStartMenuShortcut(installLocation),
  201. LocationType.CURRENT_USER_START_MENU,
  202. true);
  203. } else {
  204. SystemUtils.removeShortcut(
  205. getStartMenuShortcut(installLocation),
  206. LocationType.ALL_USERS_START_MENU,
  207. true);
  208. }
  209. } catch (NativeException e) {
  210. LogManager.log(
  211. getString("CL.uninstall.error.start.menu"), // NOI18N
  212. e);
  213. }
  214. }
  215. /////////////////////////////////////////////////////////////////////////////
  216. if (Boolean.parseBoolean(getProperty(HelloWorldPanel.CREATE_DESKTOP_SHORTCUT_PROPERTY))) {
  217. if (!SystemUtils.isMacOS()) {
  218. try {
  219. progress.setDetail(getString("CL.uninstall.desktop")); // NOI18N
  220. final String shortcutLocation = getProduct().getProperty(
  221. DESKTOP_SHORTCUT_LOCATION_PROPERTY);
  222. if ((shortcutLocation == null)
  223. || shortcutLocation.equals(CURRENT_USER_PROPERTY_VALUE)) {
  224. SystemUtils.removeShortcut(
  225. getDesktopShortcut(installLocation),
  226. LocationType.CURRENT_USER_DESKTOP,
  227. false);
  228. } else {
  229. SystemUtils.removeShortcut(
  230. getDesktopShortcut(installLocation),
  231. LocationType.ALL_USERS_DESKTOP,
  232. false);
  233. }
  234. } catch (NativeException e) {
  235. LogManager.log(
  236. getString("CL.uninstall.error.desktop"), // NOI18N
  237. e);
  238. }
  239. }
  240. }
  241. if (Boolean.getBoolean("remove.app.userdir")) {
  242. try {
  243. progress.setDetail(getString("CL.uninstall.remove.userdir")); // NOI18N
  244. LogManager.logIndent("Removing application`s userdir... ");
  245. File userDir = NetBeansRCPUtils.getApplicationUserDirFile(installLocation);
  246. LogManager.log("... application userdir location : " + userDir);
  247. if (FileUtils.exists(userDir) && FileUtils.canWrite(userDir)) {
  248. FileUtils.deleteFile(userDir, true);
  249. FileUtils.deleteEmptyParents(userDir);
  250. }
  251. LogManager.log("... application userdir totally removed");
  252. } catch (IOException e) {
  253. LogManager.log("Can`t remove application userdir", e);
  254. } finally {
  255. LogManager.unindent();
  256. }
  257. }
  258. /////////////////////////////////////////////////////////////////////////////
  259. //remove cluster/update files
  260. /*
  261. try {
  262. progress.setDetail(getString("CL.uninstall.update.files")); // NOI18N
  263. for(String cluster : CLUSTERS) {
  264. File updateDir = new File(installLocation, cluster + File.separator + "update");
  265. if ( updateDir.exists()) {
  266. FileUtils.deleteFile(updateDir, true);
  267. }
  268. }
  269. } catch (IOException e) {
  270. LogManager.log(
  271. getString("CL.uninstall.error.update.files"), // NOI18N
  272. e);
  273. }
  274. */
  275. /////////////////////////////////////////////////////////////////////////////
  276. progress.setPercentage(Progress.COMPLETE);
  277. }
  278. @Override
  279. public String getExecutable() {
  280. if (SystemUtils.isWindows()) {
  281. return EXECUTABLE_WINDOWS;
  282. } else {
  283. return EXECUTABLE_UNIX;
  284. }
  285. }
  286. @Override
  287. public String getIcon() {
  288. if (SystemUtils.isWindows()) {
  289. return ICON_WINDOWS;
  290. } else if (SystemUtils.isMacOS()) {
  291. return ICON_MACOSX;
  292. } else {
  293. return ICON_UNIX;
  294. }
  295. }
  296. public RemovalMode getRemovalMode() {
  297. return RemovalMode.LIST;
  298. }
  299. @Override
  300. public boolean registerInSystem() {
  301. return true;
  302. }
  303. @Override
  304. public boolean requireLegalArtifactSaving() {
  305. return false;
  306. }
  307. @Override
  308. public boolean requireDotAppForMacOs() {
  309. return true;
  310. }
  311. @Override
  312. public boolean wrapForMacOs() {
  313. return true;
  314. }
  315. private Shortcut getDesktopShortcut(final File directory) {
  316. return getShortcut(
  317. getStrings("CL.desktop.shortcut.name"), // NOI18N
  318. getStrings("CL.desktop.shortcut.description"), // NOI18N
  319. getString("CL.desktop.shortcut.path"), // NOI18N
  320. directory);
  321. }
  322. private Shortcut getStartMenuShortcut(final File directory) {
  323. if (SystemUtils.isMacOS()) {
  324. return getShortcut(
  325. getStrings("CL.start.menu.shortcut.name.macosx"), // NOI18N
  326. getStrings("CL.start.menu.shortcut.description"), // NOI18N
  327. getString("CL.start.menu.shortcut.path"), // NOI18N
  328. directory);
  329. } else {
  330. return getShortcut(
  331. getStrings("CL.start.menu.shortcut.name"), // NOI18N
  332. getStrings("CL.start.menu.shortcut.description"), // NOI18N
  333. getString("CL.start.menu.shortcut.path"), // NOI18N
  334. directory);
  335. }
  336. }
  337. private Shortcut getShortcut(
  338. final Map<Locale, String> names,
  339. final Map<Locale, String> descriptions,
  340. final String relativePath,
  341. final File location) {
  342. final File icon;
  343. final File executable;
  344. if (SystemUtils.isWindows()) {
  345. icon = new File(location, ICON_WINDOWS);
  346. } else if (SystemUtils.isMacOS()) {
  347. icon = new File(location, ICON_MACOSX);
  348. } else {
  349. icon = new File(location, ICON_UNIX);
  350. LogManager.log("... icon file: " + icon);
  351. if(!FileUtils.exists(icon)) {
  352. LogManager.log("... icon file does not exist: " + icon);
  353. InputStream is = null;
  354. is = ResourceUtils.getResource(ICON_UNIX_RESOURCE, this.getClass().getClassLoader());
  355. if(is!=null) {
  356. FileOutputStream fos =null;
  357. try {
  358. fos = new FileOutputStream(icon);
  359. StreamUtils.transferData(is, fos);
  360. is.close();
  361. fos.close();
  362. getProduct().getInstalledFiles().add(icon);
  363. } catch (IOException e) {
  364. LogManager.log(e);
  365. } finally {
  366. if(fos!=null) {
  367. try {
  368. fos.close();
  369. } catch (IOException e) {
  370. }
  371. }
  372. }
  373. }
  374. }
  375. }
  376. if (SystemUtils.isWindows()) {
  377. executable = new File(location, EXECUTABLE_WINDOWS);
  378. } else {
  379. executable = new File(location, EXECUTABLE_UNIX);
  380. }
  381. final String name = names.get(new Locale(StringUtils.EMPTY_STRING));
  382. final FileShortcut shortcut = new FileShortcut(name, executable);
  383. shortcut.setNames(names);
  384. shortcut.setDescriptions(descriptions);
  385. shortcut.setCategories(SHORTCUT_CATEGORIES);
  386. shortcut.setFileName(SHORTCUT_FILENAME);
  387. shortcut.setIcon(icon);
  388. shortcut.setRelativePath(relativePath);
  389. shortcut.setWorkingDirectory(location);
  390. shortcut.setModifyPath(true);
  391. return shortcut;
  392. }
  393. public static final String SHORTCUT_FILENAME =
  394. ResourceUtils.getString(ConfigurationLogic.class, "CL.app.name") + ".desktop"; // NOI18N
  395. public static final String[] SHORTCUT_CATEGORIES = new String[]{
  396. "Application"
  397. };
  398. public static final String BIN_SUBDIR =
  399. "bin/";
  400. public static final String EXECUTABLE_WINDOWS =
  401. BIN_SUBDIR
  402. + ResourceUtils.getString(ConfigurationLogic.class, "CL.app.name") + ".exe"; // NOI18N
  403. public static final String EXECUTABLE_UNIX =
  404. BIN_SUBDIR
  405. + ResourceUtils.getString(ConfigurationLogic.class, "CL.app.name"); // NOI18N
  406. public static final String ICON_WINDOWS =
  407. EXECUTABLE_WINDOWS;
  408. public static final String ICON_UNIX =
  409. ResourceUtils.getString(ConfigurationLogic.class,
  410. "CL.unix.icon.name"); // NOI18N
  411. public static final String ICON_UNIX_RESOURCE =
  412. ResourceUtils.getString(ConfigurationLogic.class,
  413. "CL.unix.icon.resource"); // NOI18N
  414. public static final String ICON_MACOSX =
  415. ResourceUtils.getString(ConfigurationLogic.class, "CL.app.name") + ".icns"; // NOI18N
  416. public static final String ICON_MACOSX_RESOURCE =
  417. "org/mycompany/" + ResourceUtils.getString(ConfigurationLogic.class, "CL.app.name") + ".icns"; // NOI18N
  418. public static final String WIZARD_COMPONENTS_URI =
  419. "resource:" + // NOI18N
  420. "org/mycompany/wizard.xml"; // NOI18N
  421. private static final String DESKTOP_SHORTCUT_LOCATION_PROPERTY =
  422. "desktop.shortcut.location"; // NOI18N
  423. private static final String START_MENU_SHORTCUT_LOCATION_PROPERTY =
  424. "start.menu.shortcut.location"; // NOI18N
  425. private static final String ALL_USERS_PROPERTY_VALUE =
  426. "all.users"; // NOI18N
  427. private static final String CURRENT_USER_PROPERTY_VALUE =
  428. "current.user"; // NOI18N
  429. }