001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2008 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: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2008 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.updater;
043:
044: import java.io.File;
045: import java.io.IOException;
046: import javax.swing.SwingUtilities;
047:
048: /**
049: * @author Jiri Rechtacek
050: */
051: public final class UpdaterDispatcher implements Runnable {
052:
053: UpdaterDispatcher() {
054: }
055:
056: private Boolean disable = null;
057: private Boolean install = null;
058: private Boolean uninstall = null;
059:
060: public static final String UPDATE_DIR = "update"; // NOI18N
061: public static final String DEACTIVATE_DIR = "deactivate"; // NOI18N
062: public static final String NEW_UPDATER_DIR = "new_updater"; // NOI18N
063:
064: public static final String DEACTIVATE_LATER = "deactivate_later.txt"; // NOI18N
065:
066: /** Explore <cluster>/update directory and schedules actions handler for
067: * Install/Update, Uninstall or Disable modules
068: *
069: */
070: private void dispatch() {
071: assert !SwingUtilities.isEventDispatchThread() : "Cannot run in EQ";
072: try {
073: // uninstall first
074: if (isUninstallScheduled()) {
075: ModuleDeactivator.delete();
076: }
077:
078: // then disable
079: if (isDisableScheduled()) {
080: ModuleDeactivator.disable();
081: }
082:
083: // finally install/update
084: if (isInstallScheduled()) {
085: try {
086: ModuleUpdater mu = new ModuleUpdater(null);
087: mu.start();
088: mu.join();
089: } catch (InterruptedException ex) {
090: System.out.println("Error: " + ex);
091: }
092: }
093: } catch (Exception x) {
094: System.out.println("Error: Handling delete throws " + x);
095: } finally {
096: UpdaterFrame.getUpdaterFrame().unpackingFinished();
097: }
098: }
099:
100: private boolean isDisableScheduled() {
101: if (disable == null) {
102: exploreUpdateDir();
103: }
104: return disable;
105: }
106:
107: private boolean isUninstallScheduled() {
108: if (uninstall == null) {
109: exploreUpdateDir();
110: }
111: return uninstall;
112: }
113:
114: private boolean isInstallScheduled() {
115: if (install == null) {
116: exploreUpdateDir();
117: }
118: return install;
119: }
120:
121: private void exploreUpdateDir() {
122: // initialize to false
123: install = false;
124: uninstall = false;
125: disable = false;
126:
127: // go over all clusters
128: for (File cluster : UpdateTracking.clusters(true)) {
129: File updateDir = new File(cluster, UPDATE_DIR);
130: if (updateDir.exists() && updateDir.isDirectory()) {
131: // install/update
132: if (install == null || !install) {
133: install = !ModuleUpdater.getModulesToInstall(
134: cluster).isEmpty();
135: }
136: // uninstall
137: if (uninstall == null || !uninstall) {
138: uninstall = ModuleDeactivator
139: .hasModulesForDelete(updateDir);
140: }
141: // disable
142: if (disable == null || !disable) {
143: disable = ModuleDeactivator
144: .hasModulesForDisable(updateDir);
145: }
146: }
147: }
148: }
149:
150: public void run() {
151: dispatch();
152: UpdaterFrame.disposeSplash();
153: }
154:
155: public static void touchLastModified(File cluster) {
156: try {
157: File stamp = new File(cluster, ".lastModified"); // NOI18N
158: if (!stamp.createNewFile()) {
159: stamp.setLastModified(System.currentTimeMillis());
160: }
161: } catch (IOException ex) {
162: ex.printStackTrace();
163: }
164: }
165:
166: }
|