KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > nbbuild > SetCluster


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.nbbuild;
21
22 import java.io.*;
23 import java.util.*;
24
25 import org.apache.tools.ant.BuildException;
26 import org.apache.tools.ant.taskdefs.*;
27 import org.apache.tools.ant.Project;
28 import org.apache.tools.ant.Task;
29
30 /** Settigns the given property to cluster value
31  *
32  * @author Michal Zlamal
33  */

34 public class SetCluster extends Task {
35     private String JavaDoc name = null;
36     private String JavaDoc propertiesList = null;
37     private String JavaDoc cluster;
38     private String JavaDoc thisModuleName = null;
39     private String JavaDoc defaultLocation = null;
40     
41     /** Sets the name of property which should contain the value */
42     public void setName(String JavaDoc name) {
43         this.name = name;
44     }
45     
46     /** Comma separated list of properties. One of those properties should contain the name of module from what it is ran. */
47     public void setList( String JavaDoc propertiesList ) {
48         this.propertiesList = propertiesList;
49     }
50     
51     /** Name of a cluster */
52     public void setCluster (String JavaDoc cluster) {
53         this.cluster = cluster;
54     }
55     
56     /** Name of this module */
57     public void setModule(String JavaDoc module) {
58         thisModuleName = module;
59     }
60
61     /** Location of default cluster */
62     public void setDefaultLocation(String JavaDoc defaultLocation) {
63         this.defaultLocation = defaultLocation;
64     }
65     
66     public void execute() throws BuildException {
67         if (name == null)
68             throw new BuildException("Name of property to set have to be specified",this.getLocation());
69         if (propertiesList != null) {
70             if (cluster != null)
71                 throw new BuildException("Either list or cluster property can be specified not both",this.getLocation());
72             if (thisModuleName == null)
73                 throw new BuildException("The name of current module have to be set", this.getLocation());
74         } else {
75             if (cluster == null) {
76                 throw new BuildException("Either list or cluster property have to be specified",this.getLocation());
77             }
78             if (thisModuleName != null) {
79                 throw new BuildException("When cluster property is used thisModuleName should not be set",this.getLocation());
80             }
81         }
82         
83         if (cluster != null) {
84             String JavaDoc clusterDir = this.getProject().getProperty(cluster + ".dir");
85             if (clusterDir == null) throw new BuildException( "Property: " + cluster + ".dir have to be defined", this.getLocation());
86             this.getProject().setProperty( name, clusterDir );
87             return;
88         }
89         
90         HashSet modules = new HashSet();
91         
92         StringTokenizer tokens = new StringTokenizer( propertiesList, " \t\n\f\r," );
93         while (tokens.hasMoreTokens()) {
94             String JavaDoc property = tokens.nextToken().trim();
95             String JavaDoc list = this.getProject().getProperty( property );
96             if (list == null) throw new BuildException("Property: " + property + " is not defined anywhere",this.getLocation());
97             StringTokenizer modTokens = new StringTokenizer(list," \t\n\f\r,");
98             while (modTokens.hasMoreTokens()) {
99                 String JavaDoc module = modTokens.nextToken();
100                 log( property + " " + module, Project.MSG_VERBOSE );
101                 if (module.equals(thisModuleName)) {
102                     // We found the list reffering to this module
103
String JavaDoc clusterDir = this.getProject().getProperty(property + ".dir");
104                     if (clusterDir == null) throw new BuildException( "Property: " + property + ".dir have to be defined", this.getLocation());
105                     log( "Property: " + name + " will be set to " + clusterDir, Project.MSG_VERBOSE);
106                     this.getProject().setProperty( name, clusterDir );
107                     return;
108                 }
109             }
110         }
111        log("No cluster list with this module: " + thisModuleName + " was found. Using default cluster location: " + defaultLocation, Project.MSG_WARN);
112        if (defaultLocation == null)
113            throw new BuildException("No default cluster location defined", this.getLocation());
114
115        log( "Property: " + name + " will be set to " + defaultLocation, Project.MSG_VERBOSE);
116        this.getProject().setProperty( name, defaultLocation );
117     }
118 }
119
Popular Tags