KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > util > launcher > parser > RepositoryZeus


1 /*====================================================================
2  
3  ObjectWeb Util Launcher Package.
4  Copyright (C) 2004 INRIA & USTL - LIFL - GOAL
5  Contact: openccm@objectweb.org
6  
7  This library is free software; you can redistribute it and/or
8  modify it under the terms of the GNU Lesser General Public
9  License as published by the Free Software Foundation; either
10  version 2.1 of the License, or any later version.
11  
12  This library is distributed in the hope that it will be useful,
13  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  Lesser General Public License for more details.
16  
17  You should have received a copy of the GNU Lesser General Public
18  License along with this library; if not, write to the Free Software
19  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
20  USA
21  
22  Initial developer(s): Romain Rouvoy.
23  Contributor(s): Christophe Demarey,Christophe Contreras.
24  
25  --------------------------------------------------------------------
26  $Id: RepositoryZeus.java,v 1.5 2005/05/30 16:29:15 merle Exp $
27  ====================================================================*/

28
29 package org.objectweb.util.launcher.parser;
30
31 import java.util.HashMap JavaDoc;
32 import java.util.Iterator JavaDoc;
33 import java.util.StringTokenizer JavaDoc;
34
35 import org.objectweb.util.launcher.LauncherException;
36 import org.objectweb.util.trace.TraceSystem;
37
38 /**
39  * Specialization of the repository for considering Zeus descriptions.
40  *
41  * @author <a HREF="mailto:Romain.Rouvoy@lifl.fr">Romain Rouvoy</a>
42  * @version 0.1
43  */

44 public class RepositoryZeus
45 implements Repository
46 {
47     
48     /** java properties are identified by separator value in XML files */
49     public static final String JavaDoc separator = "@" ;
50     
51     /**
52      * List of Zeus definitions.
53      */

54     private final HashMap JavaDoc classpath_, argument_, property_, run_, context_ ;
55     
56     /**
57      * Default constructor.
58      */

59     public RepositoryZeus() {
60         this.classpath_ = new HashMap JavaDoc();
61         this.argument_ = new HashMap JavaDoc();
62         this.property_ = new HashMap JavaDoc();
63         this.run_ = new HashMap JavaDoc();
64         this.context_ = new HashMap JavaDoc();
65     }
66     
67     /**
68      * @return Returns the argument.
69      */

70     protected HashMap JavaDoc getArgument() {
71         return argument_;
72     }
73     
74     /**
75      * @return Returns the classpath.
76      */

77     protected HashMap JavaDoc getClasspath() {
78         return classpath_;
79     }
80     
81     /**
82      * @return Returns the context.
83      */

84     protected HashMap JavaDoc getContext() {
85         return context_;
86     }
87     
88     /**
89      * @return Returns the property.
90      */

91     protected HashMap JavaDoc getProperty() {
92         return property_;
93     }
94     
95     /**
96      * @return Returns the run.
97      */

98     protected HashMap JavaDoc getRun() {
99         return run_;
100     }
101     
102     /**
103      * Add a new classpath description in the repository.
104      * @param identifier the identifier of the description.
105      * @param description the description to add.
106      */

107     public void addClasspath(String JavaDoc identifier, Classpath description) {
108         TraceSystem.get("repository").debug("Registering classpath "+identifier+" with value "+description);
109         getClasspath().put(identifier, description);
110     }
111     
112     /**
113      * Add a new properties description in the repository.
114      * @param identifier the identifier of the description.
115      * @param description the description to add.
116      */

117     public void addProperty(String JavaDoc identifier, Properties description) {
118         TraceSystem.get("repository").debug("Registering properties "+identifier+" with value "+description);
119         getProperty().put(identifier, description);
120     }
121     
122     /**
123      * Add a new arguments description in the repository.
124      * @param identifier the identifier of the description.
125      * @param description the description to add.
126      */

127     public void addArgument(String JavaDoc identifier, Arguments description) {
128         TraceSystem.get("repository").debug("Registering arguments "+identifier+" with value "+description);
129         getArgument().put(identifier, description);
130     }
131
132     /**
133      * Add a new run description in the repository.
134      * @param identifier the identifier of the description.
135      * @param description the description to add.
136      */

137     public void addRun(String JavaDoc identifier, Run description) {
138         TraceSystem.get("repository").debug("Registering run "+identifier+" with value "+description);
139         getRun().put(identifier, description);
140     }
141     
142     /**
143      * Add a new context description in the repository.
144      * @param identifier the identifier of the description.
145      * @param description the description to add.
146      */

147     public void addContext(String JavaDoc identifier, Context description) {
148         TraceSystem.get("repository").debug("Registering context "+identifier+" with value "+description);
149         getContext().put(identifier, description);
150     }
151
152     /**
153      * Replace tags @NAME@ in a string.<BR>
154      * NAME must be a jave property.
155      *
156      * @param tagged - The string to replace.
157      *
158      * @return A new String with all tag replacements.
159      */

160     protected String JavaDoc replaceTag(String JavaDoc tagged) {
161         StringBuffer JavaDoc result = new StringBuffer JavaDoc();
162         int start = 0;
163         int end = tagged.indexOf(separator);
164         while(end != -1) {
165             result.append(tagged.substring(start,end));
166             start=end+1;
167             end= tagged.indexOf(separator,start);
168             if (end != -1) {
169                 String JavaDoc tag = tagged.substring(start,end);
170                 String JavaDoc value = System.getProperty(tag);
171                 if (value == null)
172                     result.append("@"+tag+"@");
173                 else {
174                     TraceSystem.get("repository").debug("Replacing tag @"+tag+"@ with value "+value);
175                     result.append(value);
176                 }
177             }
178             start = end+1;
179             end = tagged.indexOf(separator,start);
180         }
181         result.append(tagged.substring(start));
182         return result.toString();
183     }
184     
185     /**
186      * Loads the classpath specified in the classpath parameter.
187      * @param classpath the classpath to load (space-separated).
188      * @param description the context description to complete.
189      */

190     protected void loadClasspath(String JavaDoc classpath, ContextDescription description) {
191         for (StringTokenizer JavaDoc st = new StringTokenizer JavaDoc(classpath!=null?classpath:"");st.hasMoreTokens();){
192             String JavaDoc path = st.nextToken();
193             if (!description.checkDescription(path+"_cp")) {
194                 description.addDescription(path+"_cp");
195                 TraceSystem.get("repository").debug("Loading path "+path+" ...");
196                 Classpath cl = (Classpath)getClasspath().get(path);
197                 if (cl != null)
198                     loadClasspath(cl, description);
199                 else
200                     throw new LauncherException(new Exception JavaDoc("Path " + path + " not found!"));
201             }
202         }
203         
204     }
205     
206     /**
207      * Loads the content of the classpath into the description.
208      * @param classpath the classpath to load.
209      * @param description the description where classpath should be loaded.
210      */

211     protected void loadClasspath(Classpath classpath, ContextDescription description) {
212         if (classpath == null)
213             throw new LauncherException(new Exception JavaDoc("No path found!"));
214         for (Iterator JavaDoc i = classpath.getPathList().iterator() ; i.hasNext() ; ) {
215             Path path = (Path)i.next();
216             if (path.getUrl() != null)
217                 description.addClass(replaceTag(path.getUrl()));
218             else
219                 loadClasspath(path.getClasspath(), description);
220         }
221     }
222     
223     /**
224      * Loads the list of arguments specified in the args parameter.
225      * @param args the list of arguments to load (space-separated).
226      * @param description the context description to complete.
227      */

228     protected void loadArguments(String JavaDoc args, ContextDescription description) {
229         for (StringTokenizer JavaDoc st = new StringTokenizer JavaDoc(args!=null?args:"");st.hasMoreTokens();){
230             String JavaDoc arg = st.nextToken();
231             if (!description.checkDescription(arg+"_args")) {
232                 description.addDescription(arg+"_args");
233                 TraceSystem.get("repository").debug("Loading argument "+arg+" ...");
234                 Arguments arguments = (Arguments)getArgument().get(arg);
235                 if (arguments != null)
236                     loadArguments(arguments, description);
237                 else
238                     throw new LauncherException(new Exception JavaDoc("Argument " + arg + " not found!"));
239             }
240         }
241     }
242     
243     /**
244      * Loads the content of the arguments into the description.
245      * @param args the arguments to load.
246      * @param description the description where arguments should be loaded.
247      */

248     protected void loadArguments(Arguments args, ContextDescription description) {
249         if (args == null)
250             throw new LauncherException(new Exception JavaDoc("No argument found!"));
251         for(Iterator JavaDoc i = args.getArgumentList().iterator() ; i.hasNext() ; ) {
252             Argument arg = (Argument)i.next();
253             if (arg.getValue() != null) {
254                 String JavaDoc toAdd = replaceTag(arg.getValue());
255                 description.addArgument(toAdd);
256             } else if (arg.getLine() != null){
257                 String JavaDoc toAdd = replaceTag(arg.getLine());
258                 for (StringTokenizer JavaDoc st = new StringTokenizer JavaDoc(toAdd);st.hasMoreTokens();)
259                     description.addArgument(st.nextToken());
260             } else
261                 loadArguments(arg.getArguments(), description );
262         }
263     }
264     
265     /**
266      * Loads the list of properties specified in the props parameter.
267      * @param props this list of properties to load (space-separated).
268      * @param description the context description to complete.
269      */

270     protected void loadProperties(String JavaDoc props, ContextDescription description) {
271         for (StringTokenizer JavaDoc st = new StringTokenizer JavaDoc(props!=null?props:"");st.hasMoreTokens();){
272             String JavaDoc prop = st.nextToken();
273             if (!description.checkDescription(prop+"_props")) {
274                 description.addDescription(prop+"_props");
275                 TraceSystem.get("repository").debug("Loading property "+prop+" ...");
276                 Properties properties = (Properties)getProperty().get(prop);
277                 if (properties != null)
278                     loadProperties(properties, description);
279                 else
280                     throw new LauncherException(new Exception JavaDoc("Property " + prop + " not found!"));
281             }
282         }
283     }
284     
285     /**
286      * Loads the content of the properties into the description.
287      * @param props the properties to load.
288      * @param description the description where properties should be loaded.
289      */

290     protected void loadProperties(Properties props, ContextDescription description) {
291         if (props == null)
292             throw new LauncherException(new Exception JavaDoc("No property found!"));
293         for(Iterator JavaDoc i = props.getPropertyList().iterator() ; i.hasNext() ; ) {
294             Property prop = (Property)i.next();
295             if ((prop.getName() != null) && (prop.getValue() != null))
296                 description.addProperty(prop.getName(), replaceTag(prop.getValue()));
297             else
298                 loadProperties(prop.getProperties(), description);
299         }
300     }
301     
302     
303     /**
304      * Loads the list of contexts specified in the ctx parameter.
305      * @param props this list of contexts to load (space-separated).
306      * @param description the context description to complete.
307      */

308     protected void loadContexts(String JavaDoc ctx, ContextDescription description) {
309         for (StringTokenizer JavaDoc st = new StringTokenizer JavaDoc(ctx!=null?ctx:"");st.hasMoreTokens();){
310             String JavaDoc context = st.nextToken();
311             if (!description.checkDescription(context+"_ctx")) {
312                 description.addDescription(context+"_ctx");
313                 TraceSystem.get("repository").debug("Loading context "+context+" ...");
314                 Context context_obj = (Context)getContext().get(context);
315                 if (context_obj != null)
316                     loadContexts(context_obj, description);
317                 else
318                     throw new LauncherException(new Exception JavaDoc("Context " + context + " not found!"));
319             }
320         }
321     }
322     
323     /**
324      * Loads the content of the context into the description.
325      * @param ctx the context to load.
326      * @param description the description where properties should be loaded.
327      */

328     protected void loadContexts(Context ctx, ContextDescription description) {
329         if (ctx == null)
330             throw new LauncherException(new Exception JavaDoc("No context found!"));
331         description.setName(ctx.getId());
332         loadClasspath(ctx.getClasspath(),description);
333         loadArguments(ctx.getArguments(),description);
334         loadProperties(ctx.getProperties(),description);
335         loadContexts(ctx.getContexts(),description);
336     }
337     
338     /**
339      * Loads the content of the run into the description.
340      * @param run the run to load.
341      * @param description the description where properties should be loaded.
342      */

343     protected void loadRun(Run run, RunDescription description) {
344         if (run == null)
345             throw new LauncherException(new Exception JavaDoc("No run found!"));
346         description.setMainclass(run.getMainclassname());
347         description.setName(run.getId());
348         description.setMode(run.getMode());
349         loadClasspath(run.getClasspath(),description);
350         loadArguments(run.getArguments(),description);
351         loadProperties(run.getProperties(),description);
352         loadContexts(run.getContexts(),description);
353     }
354     
355     /**
356      * Complete an existing description with the content of a context description.
357      * @param identifier the identifier of the context description.
358      * @param description the description to complete.
359      */

360     public void completeDescription(String JavaDoc identifier, ContextDescription description) {
361         TraceSystem.get("repository").debug("Complete description with context "+identifier);
362         loadContexts((Context)getContext().get(identifier), description);
363     }
364     
365     /**
366      * Retrieves a description corresponding to the identifier parameter.
367      * @param identifier the identifier of the description to obtain.
368      * @return the description.
369      */

370     public ContextDescription getDescription(String JavaDoc identifier) {
371         Run element = (Run)getRun().get(identifier);
372         if (element != null) {
373             TraceSystem.get("repository").debug("Retrieving run "+identifier);
374             RunDescription desc = new RunDescription();
375             loadRun((Run)element,desc);
376             return desc;
377         } else {
378             Context ctx = (Context)getContext().get(identifier);
379             if (ctx != null) {
380                 TraceSystem.get("repository").debug("Retrieving context "+identifier);
381                 ContextDescription desc = new ContextDescription();
382                 loadContexts((Context)ctx,desc);
383                 return desc;
384             } else
385                 throw new LauncherException(new Exception JavaDoc("No description found for " + identifier + "!"));
386         }
387     }
388 }
389
Popular Tags