KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > SOFA > Connector > EEG > EEM > Maker


1 /*
2  * Maker.java
3  *
4  * Created on 2. duben 2002, 13:42
5  */

6
7 package SOFA.Connector.EEG.EEM;
8
9 import org.w3c.dom.Document JavaDoc;
10 import org.w3c.dom.Element JavaDoc;
11 import org.w3c.dom.Node JavaDoc;
12
13 /**
14  *
15  * @author ghort
16  * @version
17  */

18 public class Maker implements SOFA.Connector.EEG.EEGPluginInterface {
19     
20     protected java.util.LinkedList JavaDoc action; /* of ActionInterface */
21     protected java.util.LinkedList JavaDoc element; /* of ElementDescription */
22
23   protected SOFA.SOFAnode.Run.CCNS.ConnectorCacheNS ccns;
24     
25     protected javax.xml.parsers.DocumentBuilderFactory JavaDoc documentBuilderFactory;
26     protected javax.xml.parsers.DocumentBuilder JavaDoc documentBuilder;
27     
28     protected Document JavaDoc configDoc;
29     
30     protected java.util.HashMap JavaDoc props;
31
32     protected String JavaDoc elementsPath;
33     protected String JavaDoc elementsPackage;
34     
35     protected String JavaDoc builtDir;
36     protected String JavaDoc builtPackage;
37
38     protected String JavaDoc translateMangledPath(String JavaDoc mangled) throws java.rmi.RemoteException JavaDoc {
39         StringBuffer JavaDoc buf=new StringBuffer JavaDoc();
40         int i, len=mangled.length();
41         char a;
42
43         buf.append("EEM__");
44
45         for (i=0;i<len;i++) {
46             a=mangled.charAt(i);
47             if (a=='_') {
48                 buf.append("__");
49             } else if (a==':') {
50                 if (i+1<len && mangled.charAt(i+1)==':') {
51                     i++;
52                     buf.append("_CC_");
53                 } else {
54                     buf.append("_C_");
55                 }
56             } else if (a=='/') {
57                 buf.append("_S_");
58             } else if (a=='.') {
59                 buf.append("_D_");
60             } else if (a=='?') {
61                 buf.append("_Q_");
62             } else if (a=='!') {
63                 buf.append("_E_");
64             } else if (Character.isJavaIdentifierPart(a)) {
65                 buf.append(a);
66             } else {
67                 buf.append("_"+Integer.toString(a,16)+"_");
68             }
69         }
70         return ccns.getHashByName(buf.toString());
71     }
72     
73     /** Creates new Maker */
74     public Maker(java.util.HashMap JavaDoc props) throws SOFA.Connector.EEG.EEGPluginException {
75         this.props=props;
76          
77         try {
78             documentBuilderFactory=javax.xml.parsers.DocumentBuilderFactory.newInstance();
79             documentBuilder=documentBuilderFactory.newDocumentBuilder();
80         
81             String JavaDoc confFile=(String JavaDoc)props.get("configuration");
82             if (confFile==null) {
83                 confFile="EEM/conf.xml";
84             }
85             confFile=SOFA.Connector.EEG.EEGenerator.getEEGConfigRoot()+"/"+confFile;
86             configDoc=documentBuilder.parse(new java.io.File JavaDoc(confFile));
87             
88             Element JavaDoc elParent=configDoc.getDocumentElement();
89             Node JavaDoc elL1, elL2, elL3;
90             
91             action=new java.util.LinkedList JavaDoc();
92             element=new java.util.LinkedList JavaDoc();
93             
94             System.out.println(" Loading Maker actions & elements:");
95             elL1=elParent.getFirstChild();
96             while (elL1!=null) {
97                 if (elL1.getNodeType()==Node.ELEMENT_NODE && elL1.getNodeName().equals("elementsPath")) {
98                     elL2=elL1.getFirstChild();
99                     while (elL2!=null) {
100                         if (elL2.getNodeType()==Node.TEXT_NODE) {
101                             elementsPath=SOFA.Connector.EEG.EEGenerator.getEEGRoot()+"/EEM/"+elL2.getNodeValue();
102                             break;
103                         }
104                         elL2=elL2.getNextSibling();
105                     }
106                 } else if (elL1.getNodeType()==Node.ELEMENT_NODE && elL1.getNodeName().equals("elementsPackage")) {
107                     elL2=elL1.getFirstChild();
108                     while (elL2!=null) {
109                         if (elL2.getNodeType()==Node.TEXT_NODE) {
110                             elementsPackage=elL2.getNodeValue();
111                             break;
112                         }
113                         elL2=elL2.getNextSibling();
114                     }
115                 } else if (elL1.getNodeType()==Node.ELEMENT_NODE && elL1.getNodeName().equals("builtDir")) {
116                     elL2=elL1.getFirstChild();
117                     while (elL2!=null) {
118                         if (elL2.getNodeType()==Node.TEXT_NODE) {
119                             builtDir=SOFA.Connector.EEG.EEGenerator.getEEGRoot()+"/EEM/"+elL2.getNodeValue();
120                             break;
121                         }
122                         elL2=elL2.getNextSibling();
123                     }
124                 } else if (elL1.getNodeType()==Node.ELEMENT_NODE && elL1.getNodeName().equals("builtPackage")) {
125                     elL2=elL1.getFirstChild();
126                     while (elL2!=null) {
127                         if (elL2.getNodeType()==Node.TEXT_NODE) {
128                             builtPackage=elL2.getNodeValue();
129                             break;
130                         }
131                         elL2=elL2.getNextSibling();
132                     }
133                 } else if (elL1.getNodeType()==Node.ELEMENT_NODE && elL1.getNodeName().equals("EEMActions")) {
134                     elL2=elL1.getFirstChild();
135                     while (elL2!=null) {
136                         if (elL2.getNodeType()==Node.ELEMENT_NODE && elL2.getNodeName().equals("EEMAction")) {
137                             
138                             java.util.HashMap JavaDoc plProps=new java.util.HashMap JavaDoc();
139                             elL3=elL2.getFirstChild();
140                             while (elL3!=null) {
141                                 if (elL3.getNodeType()==Node.ELEMENT_NODE) {
142                                     Element JavaDoc el=(Element JavaDoc)elL3;
143                                     if (el.getTagName().equals("property") && el.hasAttribute("name") && el.hasAttribute("value")) {
144                                         plProps.put(el.getAttribute("name"),el.getAttribute("value"));
145                                     }
146                                 }
147                                 elL3=elL3.getNextSibling();
148                             }
149                             
150                             try {
151                                 System.out.print(" Action ("+((Element JavaDoc)elL2).getAttribute("class")+") ");
152                                 Class JavaDoc cls=Class.forName(((Element JavaDoc)elL2).getAttribute("class"));
153                                 Class JavaDoc[] coPar=new Class JavaDoc[1];
154                                 coPar[0]=Class.forName("java.util.HashMap");
155                                 java.lang.reflect.Constructor JavaDoc co=cls.getConstructor(coPar);
156                             
157                                 Object JavaDoc[] coInst=new Object JavaDoc[1];
158                                 coInst[0]=plProps;
159                                 ActionInterface plgObject=(ActionInterface)co.newInstance(coInst);
160                             
161                                 action.add(plgObject);
162                             } catch (Exception JavaDoc excpt) {
163                                 System.out.println(" ... loading failed");
164                                 throw excpt;
165                             }
166
167                             System.out.println("loaded");
168                         }
169                         elL2=elL2.getNextSibling();
170                     }
171                 } else if (elL1.getNodeType()==Node.ELEMENT_NODE && elL1.getNodeName().equals("EEMElements")) {
172                     elL2=elL1.getFirstChild();
173                     while (elL2!=null) {
174                         if (elL2.getNodeType()==Node.ELEMENT_NODE && elL2.getNodeName().equals("EEMElement")) {
175                             ElementDescription elDescr=ElementDescription.fromXMLFile(elementsPath+"/"+((Element JavaDoc)elL2).getAttribute("file"));
176                             element.add(elDescr);
177                             System.out.println(" Element description "+elDescr.name+" ("+((Element JavaDoc)elL2).getAttribute("file")+") loaded");
178                         }
179                         elL2=elL2.getNextSibling();
180                     }
181                 }
182                 elL1=elL1.getNextSibling();
183             }
184
185             builtDir=System.getProperty("SOFA.Connector.EEG.EEM.BuiltDir",builtDir);
186             builtPackage=System.getProperty("SOFA.Connector.EEG.EEM.BuiltPackage",builtPackage);
187                         
188             System.out.println(" Connecting to Connector Cache Name Server.");
189       ccns=SOFA.Connector.CCNSAccess.CCNSAccess.openCCNS();
190       
191         } catch (Exception JavaDoc e) {
192             throw new SOFA.Connector.EEG.EEGPluginException("Can't parse configuration",e);
193         }
194     }
195
196     public String JavaDoc getName() {
197         return "EEM";
198     }
199      
200     public SOFA.Connector.EEG.ElementTechnologyDescriptor[] canGenerate(SOFA.Connector.EEG.ElementQuery query) {
201         java.util.Iterator JavaDoc iter;
202         java.util.LinkedList JavaDoc techDesc=new java.util.LinkedList JavaDoc();
203
204         for (iter=element.iterator();;) {
205             ElementDescription ed;
206             try {
207                 ed=(ElementDescription)iter.next();
208             } catch (java.util.NoSuchElementException JavaDoc e) {
209                 break;
210             }
211             if (query.type.equals(ed.type)) {
212                 boolean isSuitable=true;
213                 int i, j;
214                 for (i=0;i<ed.conditions.length && isSuitable;i++) {
215                     for (j=0;j<query.props.length;j++) {
216                         if (query.props[j].name.equals(ed.conditions[i].name) && query.props[j].value.equals(ed.conditions[i].value)) {
217                             break;
218                         }
219                     }
220                     if (j==query.props.length) {
221                         isSuitable=false;
222                     }
223                 }
224
225                 if (isSuitable) {
226                     SOFA.Connector.EEG.ElementTechnologyDescriptor tDesc=new SOFA.Connector.EEG.ElementTechnologyDescriptor();
227                     tDesc.name=ed.name;
228                     tDesc.implementation=getName()+":"+ed.name;
229                     techDesc.add(tDesc);
230                 }
231             }
232         }
233
234         SOFA.Connector.EEG.ElementTechnologyDescriptor[] ret=new SOFA.Connector.EEG.ElementTechnologyDescriptor[techDesc.size()];
235         iter=techDesc.iterator();
236         for (int i=0;;i++) {
237             try {
238                 ret[i]=(SOFA.Connector.EEG.ElementTechnologyDescriptor)iter.next();
239             } catch (java.util.NoSuchElementException JavaDoc e) {
240                 break;
241             }
242         }
243         
244         return ret;
245     }
246     
247     public SOFA.Connector.EEG.ElementOutputDescriptor generate(SOFA.Connector.EEG.ElementInputDescriptor elDescr) throws SOFA.Connector.EEG.EEGPluginException {
248         java.util.Iterator JavaDoc iter;
249         ElementDescription ed;
250         String JavaDoc impl;
251         int i;
252
253         i=elDescr.implementation.indexOf(':');
254         try {
255             if (i==-1 || !elDescr.implementation.substring(0,i).equals(getName())) {
256                 throw new SOFA.Connector.EEG.EEGPluginException("Malformed implementation specification.");
257             }
258             impl=elDescr.implementation.substring(i+1);
259         } catch (Exception JavaDoc e) {
260             throw new SOFA.Connector.EEG.EEGPluginException("Malformed implementation specification.");
261         }
262     
263         iter=element.iterator();
264         while (iter.hasNext()) {
265             ed=(ElementDescription)iter.next();
266             if (ed.name.equals(impl)) {
267                 java.util.Iterator JavaDoc it, it2;
268                 
269                 SOFA.Connector.ECG.VariableExpander expander=new SOFA.Connector.ECG.VariableExpander();
270                 for (i=0;i<elDescr.props.length;i++) {
271                     try {
272                         expander.addSubst(elDescr.props[i].name,elDescr.props[i].value);
273                     } catch (SOFA.Connector.ECG.VariableExpanderAmbiguityException e) {
274                     }
275                 }
276
277                 String JavaDoc mangledDirName;
278                 String JavaDoc mangledPackageName;
279                 try {
280                     String JavaDoc mangledPath=translateMangledPath(expander.expand(ed.mangled));
281                     mangledDirName=builtDir+"/"+mangledPath;
282                     mangledPackageName=builtPackage+"."+mangledPath;
283                 } catch (SOFA.Connector.ECG.VariableExpanderException e) {
284                     throw new SOFA.Connector.EEG.EEGPluginException("Can't resolve variable '"+ed.mangled+"'.",e);
285                 } catch (java.rmi.RemoteException JavaDoc e) {
286           throw new SOFA.Connector.EEG.EEGPluginException("Can't use CCNS to translate element name.",e);
287         }
288
289                 java.io.File JavaDoc mangledDir=new java.io.File JavaDoc(mangledDirName);
290
291                 if (!mangledDir.isDirectory())
292                 {
293                     if (!mangledDir.mkdirs()) {
294                         throw new SOFA.Connector.EEG.EEGPluginException("Can't create built dir '"+mangledDirName+"'.");
295                     }
296
297                     try {
298                         ActionInterface act;
299                         it=ed.make.iterator();
300                         while (it.hasNext()) {
301                             Object JavaDoc cmdOrBld=it.next();
302
303                             if (cmdOrBld instanceof ElementDescriptionCommand) {
304                                 ElementDescriptionCommand cmd=(ElementDescriptionCommand)cmdOrBld;
305                             
306                                 try {
307                                     for (it2=action.iterator();;) {
308                                         act=(ActionInterface)it2.next();
309                                         if (act.getName().equals(cmd.action)) {
310                                             break;
311                                         }
312                                     }
313                                 } catch (java.util.NoSuchElementException JavaDoc e) {
314                                     throw new SOFA.Connector.EEG.EEGPluginException("Can't find action '"+cmd.action+"'.",e);
315                                 }
316
317                                 System.out.print(" Performing action '"+cmd.action+"' ... ");
318                                 
319                                 try {
320                                     SOFA.Connector.Property[] actParams=new SOFA.Connector.Property[cmd.params.length];
321                                     for (i=0;i<cmd.params.length;i++) {
322                                         try {
323                                             actParams[i]=new SOFA.Connector.Property(cmd.params[i].name,expander.expand(cmd.params[i].value));
324                                         } catch (SOFA.Connector.ECG.VariableExpanderException e) {
325                                             throw new SOFA.Connector.EEG.EEGPluginException("Can't resolve variable '"+cmd.params[i].name+"'.",e);
326                                         }
327                                     }
328
329                                     try {
330                                         act.perform(actParams,elementsPath,elementsPackage,mangledDirName,mangledPackageName);
331                                     } catch (ActionException e) {
332                                         throw new SOFA.Connector.EEG.EEGPluginException("Can't perform action '"+cmd.action+"' on element '"+elDescr.implementation+"'.",e);
333                                     }
334                                 } catch (SOFA.Connector.EEG.EEGPluginException excpt) {
335                                     System.out.println("failed");
336                                     throw excpt;
337                                 }
338
339                                 System.out.println("OK");
340                             } else {
341                                 ElementDescriptionBuild bld=(ElementDescriptionBuild)cmdOrBld;
342                             
343                                 System.out.println(" Performing build for '"+bld.element+"':");
344
345                                 SOFA.Connector.EEG.ElementInputDescriptor iDes=new SOFA.Connector.EEG.ElementInputDescriptor();
346                                 iDes.instanceName=elDescr.instanceName;
347                                 iDes.implementation=getName()+":"+bld.element;
348                                 iDes.techProps=elDescr.techProps;
349
350                                 SOFA.Connector.Property[] bldProps=new SOFA.Connector.Property[bld.props.length];
351                                 for (i=0;i<bld.props.length;i++) {
352                                     try {
353                                         bldProps[i]=new SOFA.Connector.Property(bld.props[i].name,expander.expand(bld.props[i].value));
354                                     } catch (SOFA.Connector.ECG.VariableExpanderException e) {
355                                         throw new SOFA.Connector.EEG.EEGPluginException("Can't resolve variable '"+bld.props[i].name+"'.",e);
356                                     }
357                                 }
358                                 iDes.props=bldProps;
359                                 
360                                 try {
361                                     SOFA.Connector.EEG.ElementOutputDescriptor oDes=generate(iDes);
362
363                                     String JavaDoc genPackage=oDes.impl.substring(0,oDes.impl.lastIndexOf('.'));
364                                     String JavaDoc genPath=genPackage.replace('.','/');
365                                     expander.addSubst(bld.packageVar,genPackage);
366                                     expander.addSubst(bld.pathVar,genPath);
367                                 } catch (Exception JavaDoc excpt) {
368                                     throw new SOFA.Connector.EEG.EEGPluginException("Can't build '"+bld.element+"'.",excpt);
369                                 }
370                                 
371                                 System.out.println(" Build '"+bld.element+"' successful.");
372                             }
373                         }
374                     } catch (SOFA.Connector.EEG.EEGPluginException e) {
375 // FIXME: in try {
376
// SOFA.Connector.EEG.fileutil.Delete.delete(mangledDir);
377
// mangledDir.delete();
378
// } catch (SOFA.Connector.EEG.fileutil.DeleteException excpt) {
379
// }
380
throw e;
381                     }
382                 }
383                 
384                 SOFA.Connector.EEG.ElementOutputDescriptor outDescr=new SOFA.Connector.EEG.ElementOutputDescriptor();
385                 outDescr.name=elDescr.instanceName;
386                 outDescr.impl=mangledPackageName+"."+ed.name;
387                 outDescr.params=new SOFA.Connector.Property[0]; // FIXME: najit zpusob, jak naplnit parametry
388

389                 return outDescr;
390             }
391         }
392         throw new SOFA.Connector.EEG.EEGPluginException("Implementation '"+elDescr.implementation+"' not found.");
393     }
394
395 }
396
Popular Tags