1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements. See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18 package org.apache.commons.id.uuid.task;
19
20 import org.apache.commons.id.uuid.UUID;
21 import org.apache.tools.ant.BuildException;
22 import org.apache.tools.ant.Task;
23 import org.apache.tools.ant.types.EnumeratedAttribute;
24
25
26 /**
27 * Simple Ant task to generate a UUID. See the following Ant script for usage:
28 *
29 * <pre>
30 * <project default="generate.uuid" name="uuid" basedir=".">
31 *
32 * <taskdef name="uuid" className="org.apache.commons.id.uuid.task.UUIDTask"/>
33 *
34 * <target name="generate.uuid" description="Generates a UUID">
35 * <uuid version="VERSION_ONE"/>
36 * <echo message="${uuid}"/>
37 * <uuid version="VERSION_THREE"/>
38 * <echo message="${uuid}"/>
39 * <uuid version="VERSION_FOUR"/>
40 * <echo message="${uuid}"/>
41 * <uuid version="VERSION_FIVE"/>
42 * <echo message="${uuid}"/>
43 * </target>
44 * </project>
45 * </pre>
46 *
47 * The namespace <em>urn:uuid:B4F00409-CEF8-4822-802C-DEB20704C365</em> and the name
48 * <em>www.apache.org</em> is used as default to generate the UUIDs for version 3 and 5.
49 *
50 * @version $Id: UUIDTask.java 480488 2006-11-29 08:57:26Z bayard $
51 * @since 1.0
52 */
53 public class UUIDTask extends Task {
54
55 private String uuidVersion = "VERSION_FOUR";
56 private String name = "www.apache.org";
57 private String namespace = "urn:uuid:B4F00409-CEF8-4822-802C-DEB20704C365";
58
59 /**
60 * Setter for the name used to generate a UUID version 3 or 5.
61 *
62 * @param name the name ot use
63 * @since 1.0
64 */
65 public void setName(String name) {
66 this.name = name;
67 }
68
69 /**
70 * Setter for the namespace used to generate a UUID version 3 or 5.
71 *
72 * @param namespace the name ot use
73 * @since 1.0
74 */
75 public void setNamespace(String namespace) {
76 this.namespace = namespace;
77 }
78
79 /**
80 * The enum for the UUID version.
81 *
82 * @since 1.0
83 */
84 public static class UUIDVersion extends EnumeratedAttribute {
85 public String[] getValues() {
86 return new String[]{"VERSION_ONE", "VERSION_THREE", "VERSION_FOUR", "VERSION_FIVE"};
87 }
88 }
89
90 /**
91 * Set the UUID version to generate.
92 *
93 * @param newVersion the UUID version
94 * @since 1.0
95 */
96 public void setVersion(UUIDVersion newVersion) {
97 uuidVersion = newVersion.getValue();
98 }
99
100 public void execute() throws BuildException {
101 UUID uuid = null;
102 if (uuidVersion.equals("VERSION_THREE")) {
103 uuid = UUID.nameUUIDFromString(name, new UUID(namespace), UUID.MD5_ENCODING);
104 } else if (uuidVersion.equals("VERSION_FIVE")) {
105 uuid = UUID.nameUUIDFromString(name, new UUID(namespace), UUID.SHA1_ENCODING);
106 } else if (uuidVersion.equals("VERSION_FOUR")) {
107 uuid = UUID.randomUUID();
108 } else if (uuidVersion.equals("VERSION_ONE")) {
109 uuid = UUID.timeUUID();
110 }
111
112 setProperty("uuid", uuid.toString());
113 }
114
115 private void setProperty(String name, String value) {
116 getProject().setProperty(name, value);
117 }
118
119 }