| Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
| UUIDTask |
|
| 1.6666666666666667;1.667 | ||||
| UUIDTask$UUIDVersion |
|
| 1.6666666666666667;1.667 |
| 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 | 4 | public class UUIDTask extends Task { |
| 54 | ||
| 55 | 4 | private String uuidVersion = "VERSION_FOUR"; |
| 56 | 4 | private String name = "www.apache.org"; |
| 57 | 4 | 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 | 2 | this.name = name; |
| 67 | 2 | } |
| 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 | 2 | this.namespace = namespace; |
| 77 | 2 | } |
| 78 | ||
| 79 | /** | |
| 80 | * The enum for the UUID version. | |
| 81 | * | |
| 82 | * @since 1.0 | |
| 83 | */ | |
| 84 | 5 | public static class UUIDVersion extends EnumeratedAttribute { |
| 85 | public String[] getValues() { | |
| 86 | 5 | 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 | 4 | uuidVersion = newVersion.getValue(); |
| 98 | 4 | } |
| 99 | ||
| 100 | public void execute() throws BuildException { | |
| 101 | 4 | UUID uuid = null; |
| 102 | 4 | if (uuidVersion.equals("VERSION_THREE")) { |
| 103 | 1 | uuid = UUID.nameUUIDFromString(name, new UUID(namespace), UUID.MD5_ENCODING); |
| 104 | 1 | } else if (uuidVersion.equals("VERSION_FIVE")) { |
| 105 | 1 | uuid = UUID.nameUUIDFromString(name, new UUID(namespace), UUID.SHA1_ENCODING); |
| 106 | 1 | } else if (uuidVersion.equals("VERSION_FOUR")) { |
| 107 | 1 | uuid = UUID.randomUUID(); |
| 108 | 1 | } else if (uuidVersion.equals("VERSION_ONE")) { |
| 109 | 1 | uuid = UUID.timeUUID(); |
| 110 | } | |
| 111 | ||
| 112 | 4 | setProperty("uuid", uuid.toString()); |
| 113 | 4 | } |
| 114 | ||
| 115 | private void setProperty(String name, String value) { | |
| 116 | 4 | getProject().setProperty(name, value); |
| 117 | 4 | } |
| 118 | ||
| 119 | } |