File: ReleaseProperties.java

package info (click to toggle)
derby 10.13.1.1-2
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 78,628 kB
  • sloc: java: 691,070; sql: 42,686; xml: 20,542; sh: 3,373; sed: 96; makefile: 48
file content (347 lines) | stat: -rw-r--r-- 11,986 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
/*

   Derby - Class org.apache.derbyPreBuild.ReleaseProperties

   Licensed to the Apache Software Foundation (ASF) under one or more
   contributor license agreements.  See the NOTICE file distributed with
   this work for additional information regarding copyright ownership.
   The ASF licenses this file to You under the Apache License, Version 2.0
   (the "License"); you may not use this file except in compliance with
   the License.  You may obtain a copy of the License at

      http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.

 */

package org.apache.derbyPreBuild;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Calendar;
import java.util.Properties;
import java.util.StringTokenizer;

import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.taskdefs.Property;
import org.apache.tools.ant.Task;

/**
 * <p>
 * This ant task creates the release properties needed to define the release id
 * when building the Derby distributions. For a description of the Derby release id,
 * see http://db.apache.org/derby/papers/versionupgrade.html
 * </p>
 *
 * <p>
 * This task also sets a property for use by downstream targets during
 * the release-build:
 * </p>
 *
 * <ul>
 * <li><b>derby.release.id.new</b> - The new id for the branch, in case we were asked to bump the release id.</li>
 * </ul>
 */

public class ReleaseProperties extends Task
{
    /////////////////////////////////////////////////////////////////////////
    //
    //  CONSTANTS
    //
    /////////////////////////////////////////////////////////////////////////
    private static final String LS = System.getProperty("line.separator");
    private static final String APACHE_LICENSE_HEADER =
        "# Licensed to the Apache Software Foundation (ASF) under one or more" + LS +
        "# contributor license agreements.  See the NOTICE file distributed with" + LS +
        "# this work for additional information regarding copyright ownership." + LS +
        "# The ASF licenses this file to you under the Apache License, Version 2.0" + LS +
        "# (the \"License\"); you may not use this file except in compliance with" + LS +
        "# the License.  You may obtain a copy of the License at" + LS +
        "#" + LS +
        "#     http://www.apache.org/licenses/LICENSE-2.0" + LS +
        "#" + LS +
        "# Unless required by applicable law or agreed to in writing, software" + LS +
        "# distributed under the License is distributed on an \"AS IS\" BASIS," + LS +
        "# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied." + LS +
        "# See the License for the specific language governing permissions and" + LS +
        "# limitations under the License." + LS + LS;
    
	public final static int	MAINT_ENCODING = 1000000;
    private final static int MAINT_LENGTH = 7;

    private final   static  String  DRDA_MAINT = "drdamaint";
    private final   static  int DRDA_MAINT_ID_DEFAULT = 0;

    // properties to set on the way out
    private static final String NEW_RELEASE_ID = "derby.release.id.new";

    /////////////////////////////////////////////////////////////////////////
    //
    //  STATE
    //
    /////////////////////////////////////////////////////////////////////////

    // set by caller. Derby release id of the form "N.N.N.N" or "N.N.N.N beta"
    private String _releaseID;

    // set by caller. name of file where release properties will be written
    private String _releasePropertiesFileName;
    
    // set by caller. true if the last digit of the release id should be bumped.
    private boolean _bump;
    
    /////////////////////////////////////////////////////////////////////////
    //
    //  CONSTRUCTORS
    //
    /////////////////////////////////////////////////////////////////////////

   /**
     * <p>
     * Let Ant conjure us out of thin air.
     * </p>
     */
    public ReleaseProperties()
    {}
    
    /////////////////////////////////////////////////////////////////////////
    //
    //  Task BEHAVIOR
    //
    /////////////////////////////////////////////////////////////////////////

        
    /** <p>Let Ant set the Derby release id, a string of the form N.N.N.N.</p>*/
    public void setReleaseID( String releaseID ) { _releaseID = releaseID; }

    /** <p>Let Ant set the output file name.</p>*/
    public void setReleasePropertiesFileName( String fileName ) { _releasePropertiesFileName = fileName; }

    /** <p>Let Ant set our bumping behavior to true or false.</p>*/
    public void setBump( String bumpFlag ) { _bump = Boolean.parseBoolean( bumpFlag ); }

   /**
     * <p>
     * Create the release properties file from the release id. Sets the
     * property derby.release.id.new equal to the resulting release id.
     * </p>
     */
    public  void    execute()
        throws BuildException
    {
        File                 target = new File( _releasePropertiesFileName );
        FileWriter      propertiesFW = null;
        PrintWriter    propertiesPW = null;

        try {
            int     drdaMaintID = readDRDAMaintID( target );
            System.out.println( "XXX ReleaseProperties. drda maint id = " + drdaMaintID );
            
            VersionID versionID = new VersionID( _releaseID );
            if ( _bump ) { versionID.bump(); }
            
            int major = versionID.getMajor();
            int minor = versionID.getMinor();
            int currentYear = getCurrentYear();

            propertiesFW = new FileWriter( target );
            propertiesPW = new PrintWriter( propertiesFW );

            propertiesPW.println( APACHE_LICENSE_HEADER );

            propertiesPW.println( DRDA_MAINT + "=" + drdaMaintID );
            propertiesPW.println( "maint=" + encodeFixpackAndPoint( versionID ) );
            propertiesPW.println( "major=" + major );
            propertiesPW.println( "minor=" + minor );
            propertiesPW.println( "eversion=" + versionID.getBranchName() );
            propertiesPW.println( "beta=" + versionID.isBeta() );
            propertiesPW.println( "copyright.comment=Copyright 1997, " + currentYear + " The Apache Software Foundation or its licensors, as applicable." );
            propertiesPW.println( "vendor=The Apache Software Foundation" ) ;
            propertiesPW.println( "copyright.year=" + currentYear ) ;
            propertiesPW.println( "release.id.long=" + versionID.toString() ) ;

            setProperty( NEW_RELEASE_ID, versionID.toString() );
        }
        catch (Exception e)
        {
            throw new BuildException( "Could not generate release properties: " + e.getMessage(), e );
        }
        finally
        {
            try {
                finishWriting( propertiesFW, propertiesPW );
            }
            catch (Exception ex)
            {
                throw new BuildException( "Error closing file writers.", ex );
            }
        }
    }
    
    /////////////////////////////////////////////////////////////////////////
    //
    //  MINIONS
    //
    /////////////////////////////////////////////////////////////////////////

    /**
     * <p>
     * Stuff the third and fourth numbers of a Derby release id into the
     * encoded format expected by the Derby release machinery.
     * </p>
     */
    private String encodeFixpackAndPoint( VersionID versionID )
    {
        int result = ( versionID.getFixpack() * MAINT_ENCODING ) + versionID.getPoint();

        // the convention is to represent the number as 7 digits even
        // if the number is 0
        String retval = Integer.toString( result );
        int  length = retval.length();

        int count = MAINT_LENGTH - length;
        for ( int i = 0; i < count; i++ ) { retval = "0" + retval; }

        return retval;
    }

    /**
     * <p>
     * Get the current year as an int.
     * </p>
     */
    private int getCurrentYear()
    {
        return Calendar.getInstance().get( Calendar.YEAR );
    }
    
    /**
     * <p>
     * Flush and close file writers.
     * </p>
     */
    private void    finishWriting( FileWriter fw, PrintWriter pw )
        throws IOException
    {
        if ( (fw == null) || (pw == null) ) { return; }
        
        pw.flush();
        fw.flush();

        pw.close();
        fw.close();
    }

    /**
     * <p>
     * Read the DRDA maintenance id from the existing release properties.
     * Returns 0 if the release properties file doesn't exist.
     * </p>
     */
    private int readDRDAMaintID( File inputFile )
        throws Exception
    {
        if ( !inputFile.exists() ) { return DRDA_MAINT_ID_DEFAULT; }
        
        Properties  releaseProperties = new Properties();
        releaseProperties.load( new FileInputStream( inputFile ) );

        String  stringValue = releaseProperties.getProperty( DRDA_MAINT );

        return Integer.parseInt( stringValue );
    }
    
    /////////////////////////////////////////////////////////////////////////
    //
    //  INNER CLASSES
    //
    /////////////////////////////////////////////////////////////////////////

    public static final class VersionID
    {
        private int _major;
        private int _minor;
        private int _fixpack;
        private int _point;
        private boolean _isBeta = false;

        public VersionID( String text )
            throws BuildException
        {
            StringTokenizer tokenizer = new StringTokenizer( text, ". " );

            try {
                _major = Integer.parseInt( tokenizer.nextToken() );
                _minor = Integer.parseInt( tokenizer.nextToken() );
                _fixpack = Integer.parseInt( tokenizer.nextToken() );
                _point = Integer.parseInt( tokenizer.nextToken() );

                if ( tokenizer.hasMoreTokens() )
                {
                    if ( tokenizer.nextToken().trim().toLowerCase().equals( "beta" ) )
                    { _isBeta = true; }
                    else { throw new Exception( "Illegal trailing token" ); }
                }
            }
            catch (Exception e) { throw badID( text ); }
        }

        /** Bump the last digit of the release id */
        public void bump() { _point++; }

        public int getMajor() { return _major; }
        public int getMinor() { return _minor; }
        public int getFixpack() { return _fixpack; }
        public int getPoint() { return _point; }
        public boolean isBeta() { return _isBeta; }

        public String getBranchName() { return Integer.toString( _major ) + '.' + Integer.toString( _minor ); }

        public String toString()
        {
            StringBuffer buffer = new StringBuffer();

            buffer.append( _major ); buffer.append( '.' );
            buffer.append( _minor ); buffer.append( '.' );
            buffer.append( _fixpack ); buffer.append( '.' );
            buffer.append( _point );

            if ( _isBeta ) { buffer.append( " beta" ); }

            return buffer.toString();
        }

        private BuildException badID( String text )
        {
            return new BuildException( "Version id \"" + text + "\" is not a string of the form \"N.N.N.N\" or \"N.N.N.N beta\"" );
        }
    }

    /**
     * <p>
     * Set an ant property.
     * </p>
     */
    private void    setProperty( String name, String value )
        throws BuildException
    {
        Property    property = new Property();

        property.setName( name );
        property.setValue( value );

        property.setProject( getProject() );
        property.execute();
    }

}