KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > updatecenters > resources > NetBeansClusterCreator


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.updatecenters.resources;
21
22 import java.io.File JavaDoc;
23 import java.io.FileOutputStream JavaDoc;
24 import java.io.IOException JavaDoc;
25 import java.io.OutputStream JavaDoc;
26 import java.util.ArrayList JavaDoc;
27 import java.util.List JavaDoc;
28 import java.util.StringTokenizer JavaDoc;
29 import org.netbeans.modules.autoupdate.AutoupdateClusterCreator;
30
31
32 /** Modifies the etc/netbeans.conf if necessary.
33  *
34  * @author Jaroslav Tulach
35  */

36 public final class NetBeansClusterCreator extends AutoupdateClusterCreator {
37     protected File JavaDoc findCluster(String JavaDoc clusterName) {
38         File JavaDoc[] parent = new File JavaDoc[1];
39         File JavaDoc conf = findConf(parent, new ArrayList JavaDoc<File JavaDoc>());
40         return conf != null && conf.isFile() && conf.canWrite() ? new File JavaDoc(parent[0], clusterName) : null;
41     }
42     
43     private static File JavaDoc findConf(File JavaDoc[] parent, List JavaDoc<? super File JavaDoc> clusters) {
44         StringTokenizer JavaDoc tok = new StringTokenizer JavaDoc(System.getProperty("netbeans.dirs"), File.pathSeparator); // NOI18N
45
while (tok.hasMoreElements()) {
46             File JavaDoc cluster = new File JavaDoc(tok.nextToken());
47             clusters.add(cluster);
48             if (!cluster.exists()) {
49                 continue;
50             }
51             
52             
53             
54             if (parent[0] == null) {
55                 parent[0] = cluster.getParentFile();
56             }
57             
58             if (!parent[0].equals(cluster.getParentFile())) {
59                 // we can handle only case when all clusters are in
60
// the same directory these days
61
return null;
62             }
63         }
64         
65         return new File JavaDoc(new File JavaDoc(parent[0], "etc"), "netbeans.clusters");
66     }
67     
68     protected File JavaDoc[] registerCluster(String JavaDoc clusterName, File JavaDoc cluster) throws IOException JavaDoc {
69         File JavaDoc[] parent = new File JavaDoc[1];
70         List JavaDoc<File JavaDoc> clusters = new ArrayList JavaDoc<File JavaDoc>();
71         File JavaDoc conf = findConf(parent, clusters);
72         assert conf != null;
73         clusters.add(cluster);
74         OutputStream JavaDoc os = new FileOutputStream JavaDoc(conf, true);
75         os.write('\n');
76         os.write(clusterName.getBytes());
77         os.write('\n');
78         os.close();
79         return clusters.toArray(new File JavaDoc[0]);
80     }
81 }
82
Popular Tags