1 package org.sapia.regis.ant; 2 3 import java.io.File ; 4 import java.io.FileInputStream ; 5 import java.util.Properties ; 6 7 import javax.naming.InitialContext ; 8 9 import org.apache.tools.ant.AntClassLoader; 10 import org.apache.tools.ant.BuildException; 11 import org.apache.tools.ant.Task; 12 import org.sapia.regis.Configurable; 13 import org.sapia.regis.Path; 14 import org.sapia.regis.remote.client.RegistryImporter; 15 import org.sapia.regis.util.Utils; 16 17 27 public class RegistryTask extends Task{ 28 29 private String host, username, password, url, jndiName; 30 private Path path; 31 private int port = -1; 32 33 private File config; 34 35 38 public void setHost(String host) { 39 this.host = host; 40 } 41 42 45 public void setPort(int port) { 46 this.port = port; 47 } 48 49 53 public void setUrl(String url){ 54 this.url = url; 55 } 56 57 public void setJndiName(String jndiName) { 58 this.jndiName = jndiName; 59 } 60 61 65 public void setConfig(File file){ 66 config = file; 67 } 68 69 72 public void setPassword(String password) { 73 this.password = password; 74 } 75 76 79 public void setUsername(String username) { 80 this.username = username; 81 } 82 83 87 public void setNodePath(String path){ 88 this.path = Path.parse(path); 89 } 90 91 public void execute() throws BuildException{ 92 AntClassLoader taskloader = (AntClassLoader)this.getClass().getClassLoader(); 93 taskloader.setThreadContextLoader(); 94 try{ 95 RegistryImporter importer = new RegistryImporter(); 96 Configurable conf; 97 if(jndiName != null){ 98 if(url == null){ 99 throw new BuildException("Naming service url not set"); 100 } 101 Properties props = new Properties (); 102 props.setProperty(InitialContext.PROVIDER_URL, url); 103 try{ 104 conf = (Configurable)importer.lookup(jndiName, props); 105 }catch(Exception e){ 106 throw new BuildException("Could not lookup registry", e); 107 } 108 } 109 else{ 110 if(host == null){ 111 throw new BuildException("Registry host not set"); 112 } 113 if(port == -1){ 114 throw new BuildException("Registry host not set"); 115 } 116 try{ 117 conf = (Configurable)importer.lookup(host, port); 118 }catch(Exception e){ 119 throw new BuildException("Could not connect to registry", e); 120 } 121 } 122 if(username == null){ 123 throw new BuildException("Registry username not set"); 124 } 125 if(password == null){ 126 throw new BuildException("Registry password not set"); 127 } 128 if(config == null){ 129 throw new BuildException("Registry config file not set"); 130 } 131 try{ 132 conf.load(path, username, password, Utils.loadAsString(new FileInputStream (config)), null); 133 }catch(Exception e){ 134 throw new BuildException("Could load configuration", e); 135 } 136 }finally{ 137 taskloader.resetThreadContextLoader(); 138 } 139 } 140 } 141 | Popular Tags |