1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package net.sf.jameleon.ant;
20
21 import java.io.File;
22 import java.io.FileInputStream;
23 import java.io.FileOutputStream;
24 import java.io.IOException;
25 import java.util.Iterator;
26 import java.util.Properties;
27 import java.util.Set;
28
29 import org.apache.tools.ant.BuildException;
30 import org.apache.tools.ant.Task;
31
32 import net.sf.jameleon.util.Configurator;
33
34 /***
35 * An Ant task that takes the registered FunctionalPoints and generates a syntax reference for each one.
36 */
37 public class UpgradeTask extends Task{
38
39
40 protected File environmentProperties = new File("res/Environment.properties");
41 protected File jameleonGUIProperties = new File("lib/jameleon-gui.properties");
42
43 public UpgradeTask(){
44 }
45
46 /***
47 * The location of the Enivornment.properties
48 * @param f The location of the Enivornment.properties
49 */
50 public void setEnvironmentProperties(File f){
51 environmentProperties = f;
52 }
53
54 /***
55 * The location of jameleon-gui.properties
56 * @param f The location of jameleon-gui.properties
57 */
58 public void setJameleonGUIProperties(File f){
59 jameleonGUIProperties = f;
60 }
61
62 /***
63 * Jameleon's implementation of Task.execute().
64 *
65 * @exception BuildException Ant's way of reporting exception
66 */
67 public final void execute() throws BuildException {
68 throwExceptionOnNoFile(environmentProperties, "environmentProperties");
69 File config = new File("jameleon.conf");
70 if (config.exists()) {
71 throw new BuildException(config.getPath()+" already exists! No need to update. If you need to update,"+
72 " but you copied jameleon.conf from another location, then simply either remove the"+
73 " file and rerun this operation or try your luck with the existing jameleon.conf." );
74 }
75 if ( environmentProperties.renameTo(config) ){
76 System.out.println("Renamed '"+environmentProperties.getPath() + "' to '"+config.getPath()+"'.");
77 throwExceptionOnNoFile(jameleonGUIProperties, "jameleonGUIProperties");
78
79 Properties uiProps = updateClasspathEntries();
80 Properties confProps = new Properties();
81 try{
82 confProps.load(new FileInputStream(config));
83 confProps.putAll(uiProps);
84 confProps.store(new FileOutputStream(config), null);
85 System.out.println("Applied properties in '"+jameleonGUIProperties.getPath() + "' towards '"+config.getPath()+"'.");
86 }catch(IOException ioe){
87 throw new BuildException(ioe);
88 }
89
90 if (!jameleonGUIProperties.delete()){
91 throw new BuildException("Could not delete "+jameleonGUIProperties.getPath());
92 }
93 System.out.println("Removed '"+jameleonGUIProperties.getPath()+"'.");
94 }else{
95 throw new BuildException("Could not move "+environmentProperties.getPath()+
96 " to "+ config.getPath());
97 }
98 }
99
100 private Properties updateClasspathEntries(){
101 Configurator.clearInstance();
102 Configurator cfg = Configurator.getInstance();
103 cfg.setConfigName(jameleonGUIProperties.getPath());
104 Set keys = cfg.getKeysStartingWith("classpath.dir");
105 Iterator it = keys.iterator();
106 String key;
107 int entries = 1;
108 int maxNum = keys.size();
109 for (; it.hasNext(); entries++){
110 key = (String)it.next();
111 cfg.setValue("classpath.entry"+padNumber(entries, maxNum), cfg.getValue(key));
112 cfg.setValue(key, null);
113 }
114 keys = cfg.getKeysStartingWith("classpath.file");
115 it = keys.iterator();
116 maxNum += keys.size();
117 for (; it.hasNext(); entries++){
118 key = (String)it.next();
119 cfg.setValue("classpath.entry"+padNumber(entries, maxNum), cfg.getValue(key));
120 cfg.setValue(key, null);
121 }
122 cfg.setValue("sourceDir", null);
123 return cfg.getProperties();
124 }
125
126 private void throwExceptionOnNoFile(File f, String attributeName) throws BuildException{
127 if (f == null || !f.exists() || f.isDirectory()) {
128 throw new BuildException("'"+attributeName+"': "+f.getPath() +
129 ": is either a directory or is non-existent!");
130 }
131 }
132
133 protected String padNumber(int num, int maxNum){
134 String numS = ""+num;
135 String maxNumS = ""+maxNum;
136 int numOfZeros = maxNumS.length() - numS.length();
137 for (int i = 0; i < numOfZeros; i++) {
138 numS = 0 + numS;
139 }
140 return numS;
141 }
142
143 }