You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by rw...@apache.org on 2004/12/17 13:19:29 UTC

cvs commit: jakarta-commons-sandbox/id/src/java/org/apache/commons/id/task UUIDTask.java

rwinston    2004/12/17 04:19:29

  Added:       id/src/java/org/apache/commons/id/task UUIDTask.java
  Log:
  Add UUID generator Ant task
  
  Revision  Changes    Path
  1.1                  jakarta-commons-sandbox/id/src/java/org/apache/commons/id/task/UUIDTask.java
  
  Index: UUIDTask.java
  ===================================================================
  /*
   * Copyright 2002-2004 The Apache Software Foundation.
   *
   * Licensed 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.commons.id.task;
  
  import org.apache.commons.id.IdentifierUtils;
  import org.apache.commons.id.uuid.UUID;
  import org.apache.tools.ant.BuildException;
  import org.apache.tools.ant.Task;
  import org.apache.tools.ant.types.EnumeratedAttribute;
  
  /**
   * Simple Ant task to generate a UUID.
   * $Id: UUIDTask.java,v 1.1 2004/12/17 12:19:29 rwinston Exp $
   */
  public class UUIDTask extends Task {
  	
  	private String uuidVersion = "VERSION_FOUR";
  	private String name = "www.apache.org";
  	private String namespace = "urn:uuid:B4F00409-CEF8-4822-802C-DEB20704C365";
  	
  	public void setName(String name) {
  		this.name = name;
  	}
  	
  	public void setNamespace(String namespace) {
  		this.namespace = namespace;
  	}
  	
  	public static class UUIDVersion extends EnumeratedAttribute {
  		public String[] getValues() {
  			return new String[] { "VERSION_ONE", "VERSION_THREE", "VERSION_FOUR", "VERSION_FIVE" };
  		}
  	}
  	
  	public void setVersion(UUIDVersion newVersion) {
  		uuidVersion = newVersion.getValue();
  	}
  	
  	public void execute() throws BuildException {
  		UUID uuid = null;
  		if (uuidVersion.equals("VERSION_THREE")) {
  			uuid = UUID.nameUUIDFromString(name, new UUID(namespace), UUID.MD5_ENCODING);
  		}
  		else if (uuidVersion.equals("VERSION_FIVE")) {
  			uuid = UUID.nameUUIDFromString(name, new UUID(namespace), UUID.SHA1_ENCODING);
  		}
  		else if (uuidVersion.equals("VERSION_FOUR")) {
  			uuid = (UUID) IdentifierUtils.UUID_VERSION_FOUR_GENERATOR.nextIdentifier();
  		}
  		else if (uuidVersion.equals("VERSION_ONE")) {
  			uuid = UUID.timeUUID();
  		}
  		
  		log("Setting property:" + uuid.toString());
  		setProperty("uuid", uuid.toString());
  	}
  	
  	private void setProperty(String name, String value) {
          getProject().setProperty(name, value);
      }
  	
  }
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org