KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > autoupdate > Autoupdater


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.autoupdate;
21
22 import java.io.File JavaDoc;
23 import java.util.ArrayList JavaDoc;
24 import java.util.Collection JavaDoc;
25 import java.util.Iterator JavaDoc;
26 import java.util.List JavaDoc;
27 import java.util.logging.Logger JavaDoc;
28 import java.util.prefs.Preferences JavaDoc;
29 import org.netbeans.updater.UpdateTracking;
30 import org.openide.LifecycleManager;
31 import org.openide.filesystems.FileObject;
32 import org.openide.filesystems.FileSystem;
33 import org.openide.filesystems.FileUtil;
34 import org.openide.filesystems.Repository;
35 import org.openide.modules.InstalledFileLocator;
36 import org.openide.modules.ModuleInfo;
37 import org.openide.util.Lookup;
38 import org.openide.util.NbPreferences;
39 import org.openide.util.RequestProcessor;
40 import org.openide.windows.WindowManager;
41
42
43 /** This singleton class is a skeleton for running autoupdate, it also
44  * contains all communication with core of IDE implementation. This
45  * communication is handled by org.netbeans.core.UpdateSupport
46  * on the side of IDE Core implementation.
47  *
48  * @author Petr Hrebejk
49  */

50 class Autoupdater extends Object JavaDoc {
51
52     /** Restarts IDE in order to run Updater */
53     static void restart() {
54         LifecycleManager.getDefault().exit();
55     }
56
57     /** Installs autoupdate checker */
58     static void installUpdateChecker( final Runnable JavaDoc updateChecker ) {
59         // don't try to invoke at all in these special cases
60
if (Boolean.getBoolean("netbeans.full.hack") || Boolean.getBoolean("netbeans.close")) { // NOI18N
61
return;
62         }
63         // init IDE identity
64
Settings.getShared().getIdeIdentity();
65         // install update checker when UI is ready (main window shown)
66
WindowManager.getDefault().invokeWhenUIReady(new Runnable JavaDoc () {
67             public void run () {
68                 doInstallUpdateChecker (updateChecker);
69             }
70         });
71     }
72     
73     private static void doInstallUpdateChecker (final Runnable JavaDoc updateChecker) {
74         // bugfix #43655, postpone the connect to AU till the main window is opened
75
RequestProcessor.getDefault().post(updateChecker, 5000);
76     }
77     
78     private static final Logger JavaDoc LOG = Logger.getLogger ("org.netbeans.modules.autoupdate.Autoupdater"); // NOI18N
79

80     private static Preferences JavaDoc proxySettingsNode;
81     
82     private static synchronized Preferences JavaDoc getProxyPreferences () {
83         if (proxySettingsNode == null) {
84             proxySettingsNode = NbPreferences.root ().node ("/org/netbeans/core");
85             assert proxySettingsNode != null;
86         }
87         return proxySettingsNode;
88     }
89
90     /** Gets proxy usage */
91     static int getProxyType () {
92         return getProxyPreferences ().getInt ("proxyType", 1);
93     }
94
95     /** Gets Proxy Host */
96     static String JavaDoc getUserProxyHost() {
97         return getProxyPreferences ().get ("proxyHttpHost", "");
98     }
99
100     /** Gets Proxy Port */
101     static String JavaDoc getUserProxyPort() {
102         return getProxyPreferences ().get ("proxyHttpPort", "");
103    }
104
105     /** Sets the whole proxy configuration */
106     static void setProxyConfiguration (int proxyType, String JavaDoc host, String JavaDoc port) {
107         getProxyPreferences ().putInt ("proxyType", proxyType);
108         getProxyPreferences ().put ("proxyHttpHost", host);
109         getProxyPreferences ().put ("proxyHttpPort", port);
110     }
111     
112     /** Singleton support class for getting directories */
113     static class Support extends Object JavaDoc {
114
115         /** Platform dependent file name separator */
116         private static final String JavaDoc FILE_SEPARATOR = System.getProperty ("file.separator");
117         
118         /** Relative name of update directory */
119         private static final String JavaDoc UPDATE_DIR = "update"; // NOI18N
120

121         /** Relative name of directory where the .NBM files are downloaded */
122         private static final String JavaDoc DOWNLOAD_DIR =UPDATE_DIR + FILE_SEPARATOR + "download"; // NOI18N
123

124         /** Relative name of directory where the patch files are stored */
125         private static final String JavaDoc PATCH_DIR = ModuleUpdate.NBM_LIB + FILE_SEPARATOR + "patches"; // NOI18N
126

127         /** Relative name of key store file */
128         private static final String JavaDoc KS_FILE = ModuleUpdate.NBM_CORE + FILE_SEPARATOR + "ide.ks"; // NOI18N
129

130         /** The name of the log file */
131         public static final String JavaDoc LOG_FILE_NAME = "update.log"; // NOI18N
132

133         /** The name of the install_later file */
134         public static final String JavaDoc LATER_FILE_NAME = "install_later.xml"; // NOI18N
135

136         /** Relative name of temp directory */
137         private static final String JavaDoc UPDATE_TEMP = "autoupdateTemp";
138         
139         /** File object representing the temp directory */
140         private static FileObject tempDir = null;
141
142         /** Disable instantiation */
143         private Support() {}
144
145         /** Retruns array of module descriptions of installed modules */
146         public static ModuleInfo[] getModuleDescriptions() {
147             Collection JavaDoc descs = Lookup.getDefault().lookup(new Lookup.Template(ModuleInfo.class)).allInstances();
148             return (ModuleInfo[])descs.toArray(new ModuleInfo[descs.size()]);
149         }
150
151         /** The directory where to download the distribution files of modules */
152         public static File JavaDoc getDownloadDirectory (File JavaDoc base) {
153             if (base == null) {
154                 base = new File JavaDoc (System.getProperty ("netbeans.user"));
155             }
156             File JavaDoc downloadDirectory = new File JavaDoc (base, DOWNLOAD_DIR);
157             if ( !downloadDirectory.isDirectory() ) {
158                 downloadDirectory.mkdirs();
159             }
160
161             return downloadDirectory;
162         }
163         
164         /** Gets the central directory of patches */
165         public static List JavaDoc/*<File>*/ getPatchDirectories () {
166             List JavaDoc patches = new ArrayList JavaDoc ();
167             List JavaDoc/*<File>*/ clusters = UpdateTracking.clusters (true);
168             assert clusters != null : "Clusters cannot be empty."; // NOI18N
169
Iterator JavaDoc it = clusters.iterator ();
170             while (it.hasNext ()) {
171                 File JavaDoc dir = (File JavaDoc)it.next ();
172                 // don't force all virtually clusters exist
173
if (dir.isDirectory ()) {
174                     patches.add (new File JavaDoc (dir.getPath () + FILE_SEPARATOR + PATCH_DIR));
175                 }
176             }
177             return patches;
178         }
179
180         /** Gets the central keystore */
181         static File JavaDoc getKSFile () {
182 // List ksList = new ArrayList ();
183
// List/*<File>*/ clusters = UpdateTracking.clusters (true);
184
// assert clusters != null : "Clusters cannot be empty."; // NOI18N
185
// Iterator it = clusters.iterator ();
186
// while (it.hasNext ()) {
187
// File dir = (File)it.next ();
188
// assert dir.isDirectory () : "Cluster " + dir + " is directory."; // NOI18N
189
// try {
190
// FileObject foDir = FileUtil.toFileObject (FileUtil.normalizeFile (dir));
191
// FileObject ksFO = foDir.getFileObject (KS_FILE);
192
// if (ksFO != null) {
193
// ksList.add (FileUtil.toFile (ksFO));
194
// }
195
// } catch (IllegalArgumentException iae) {
196
// assert false : iae;
197
// }
198
// }
199
// File[] res = new File[ksList.size ()];
200
// Iterator it2 = ksList.iterator ();
201
// int i = 0;
202
// while (it2.hasNext ()) {
203
// res[i] = (File)it2.next ();
204
// i++;
205
// }
206
File JavaDoc ksFileLocated = InstalledFileLocator.getDefault ().locate (KS_FILE, null, true);
207             return ksFileLocated;
208         }
209
210         static File JavaDoc getInstall_Later (File JavaDoc root) {
211             File JavaDoc file = new File JavaDoc (root.getPath () + FILE_SEPARATOR + DOWNLOAD_DIR + FILE_SEPARATOR + LATER_FILE_NAME);
212             return file;
213         }
214         
215         static File JavaDoc getTempCopyFile(FileObject original) {
216             File JavaDoc f = null;
217             if ( tempDir == null ) {
218                 FileSystem fs = Repository.getDefault().getDefaultFileSystem();
219                 tempDir = fs.getRoot().getFileObject( UPDATE_TEMP );
220                 try {
221                     if ( tempDir == null )
222                         tempDir = fs.getRoot().createFolder( UPDATE_TEMP );
223                 } catch ( java.io.IOException JavaDoc ioe ) {
224                 }
225             }
226             if ( tempDir != null )
227                 try {
228                     FileObject fo = tempDir.getFileObject( original.getName(), "nbm" ); // NOI18N
229
if ( fo == null )
230                         fo = FileUtil.copyFile( original, tempDir, original.getName() );
231                     f = FileUtil.toFile( fo );
232                 } catch ( java.io.IOException JavaDoc ioe ) {
233                 }
234             return f;
235         }
236         
237         static void deleteTempDir() {
238             if ( tempDir == null ) {
239                 FileSystem fs = Repository.getDefault().getDefaultFileSystem();
240                 tempDir = fs.getRoot().getFileObject( UPDATE_TEMP );
241             }
242             if ( tempDir != null ) {
243                 try {
244                     tempDir.delete();
245                 } catch ( java.io.IOException JavaDoc ioe ) {
246                 }
247             }
248         }
249     }
250 }
251
Popular Tags