KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > sapia > regis > ant > RegistryTask


1 package org.sapia.regis.ant;
2
3 import java.io.File JavaDoc;
4 import java.io.FileInputStream JavaDoc;
5 import java.util.Properties JavaDoc;
6
7 import javax.naming.InitialContext JavaDoc;
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 /**
18  * This Ant task is used to update a configuration into a remote registry.
19  * It must be given the host/port of that registry, or the JNDI name of that
20  * registry as well as the URL of the Ubik JNDI server to which it is bound.
21  *
22  * @see org.sapia.regis.remote.RegistryServer
23  *
24  * @author yduchesne
25  *
26  */

27 public class RegistryTask extends Task{
28
29   private String JavaDoc host, username, password, url, jndiName;
30   private Path path;
31   private int port = -1;
32
33   private File JavaDoc config;
34   
35   /**
36    * @param host the registry host to which to connect
37    */

38   public void setHost(String JavaDoc host) {
39     this.host = host;
40   }
41
42   /**
43    * @param port the port of the registry which to connect
44    */

45   public void setPort(int port) {
46     this.port = port;
47   }
48
49   /**
50    * @param url the URL of the Ubik JNDI server from which to lookup
51    * the registry to update (example: <code>ubik://localhost:1099</code>)
52    */

53   public void setUrl(String JavaDoc url){
54     this.url = url;
55   }
56   
57   public void setJndiName(String JavaDoc jndiName) {
58     this.jndiName = jndiName;
59   }
60   
61   /**
62    * @param file the registry configuration <code>File</code> to upload
63    * into the remote registry
64    */

65   public void setConfig(File JavaDoc file){
66     config = file;
67   }
68   
69   /**
70    * @param password the password of the registry to which to connect
71    */

72   public void setPassword(String JavaDoc password) {
73     this.password = password;
74   }
75
76   /**
77    * @param username the username of the registry to which to connect
78    */

79   public void setUsername(String JavaDoc username) {
80     this.username = username;
81   }
82   
83   /**
84    * @param path the path to the node that is to be updated (if not specified, root
85    * the root not will be updated).
86    */

87   public void setNodePath(String JavaDoc 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 JavaDoc props = new Properties JavaDoc();
102         props.setProperty(InitialContext.PROVIDER_URL, url);
103         try{
104           conf = (Configurable)importer.lookup(jndiName, props);
105         }catch(Exception JavaDoc 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 JavaDoc 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 JavaDoc(config)), null);
133       }catch(Exception JavaDoc e){
134         throw new BuildException("Could load configuration", e);
135       }
136     }finally{
137       taskloader.resetThreadContextLoader();
138     }
139   }
140 }
141
Popular Tags