KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > shark > ApplicationMappingsAdministrationImpl


1
2 package org.enhydra.shark;
3
4 import java.util.ArrayList JavaDoc;
5 import java.util.HashMap JavaDoc;
6 import java.util.Iterator JavaDoc;
7 import java.util.List JavaDoc;
8 import java.util.Map JavaDoc;
9 import org.enhydra.shark.api.ApplicationMappingTransaction;
10 import org.enhydra.shark.api.RepositoryTransaction;
11 import org.enhydra.shark.api.RootException;
12 import org.enhydra.shark.api.SharkTransaction;
13 import org.enhydra.shark.api.client.wfbase.BaseException;
14 import org.enhydra.shark.api.client.wfservice.ApplicationMap;
15 import org.enhydra.shark.api.client.wfservice.ApplicationMappingAdministration;
16 import org.enhydra.shark.api.internal.appmappersistence.ApplicationMappingManager;
17 import org.enhydra.shark.api.internal.repositorypersistence.RepositoryPersistenceManager;
18 import org.enhydra.shark.api.internal.toolagent.ToolAgentFactory;
19 import org.enhydra.shark.xpdl.elements.Application;
20 import org.enhydra.shark.xpdl.elements.Applications;
21 import org.enhydra.shark.xpdl.elements.WorkflowProcess;
22 import org.enhydra.shark.xpdl.elements.WorkflowProcesses;
23
24 /**
25  * Implementation of ApplicationMappings interface.
26  * Provide methods to manage mappings among XPDL application definitions and
27  * shark's tool agents.
28  *
29  * @author Zoran Milakovic
30  *
31  */

32 public class ApplicationMappingsAdministrationImpl implements ApplicationMappingAdministration {
33
34
35    private ApplicationMappingManager pmanager;
36    private String JavaDoc userId="Unknown";
37
38    protected ApplicationMappingsAdministrationImpl ()
39    {
40       pmanager=SharkEngineManager.getInstance().getApplicationMapPersistenceManager();
41    }
42
43    public void connect (String JavaDoc userId) {
44       this.userId=userId;
45    }
46
47    public ApplicationMap[] getAllApplications () throws BaseException {
48       ApplicationMap[] retVal;
49       ApplicationMappingTransaction t = null;
50       try {
51          t = SharkUtilities.createApplicationMappingTransaction();
52          retVal = getAllApplications(t);
53       } catch (RootException e) {
54          if (e instanceof BaseException)
55             throw (BaseException)e;
56          else
57             throw new BaseException(e);
58       } finally {
59          SharkUtilities.releaseMappingTransaction(t);
60       }
61       return retVal;
62    }
63
64    public ApplicationMap[] getAllApplications (ApplicationMappingTransaction t) throws BaseException {
65       List JavaDoc al=new ArrayList JavaDoc();
66
67       List JavaDoc packageIds=null;
68
69       RepositoryPersistenceManager rpm=SharkEngineManager
70          .getInstance()
71          .getRepositoryPersistenceManager();
72
73       RepositoryTransaction rt=null;
74       try {
75          rt = SharkUtilities.createRepositoryTransaction();
76          packageIds=rpm.getExistingXPDLIds(rt);
77       } catch (Exception JavaDoc e) {
78          throw new BaseException(e);
79       } finally {
80          SharkUtilities.releaseRepositoryTransaction(rt);
81       }
82
83       Iterator JavaDoc pkgIdsIter=packageIds.iterator();
84       while (pkgIdsIter.hasNext())
85       {
86          String JavaDoc pkgId=(String JavaDoc)pkgIdsIter.next();
87          al.addAll(getApplicationsFromPackage(pkgId));
88       }
89       ApplicationMap[] ams=new ApplicationMap[al.size()];
90       al.toArray(ams);
91       return ams;
92    }
93
94    public ApplicationMap[] getAllApplications (String JavaDoc pkgId) throws BaseException {
95       ApplicationMap[] retVal;
96       ApplicationMappingTransaction t = null;
97       try {
98          t = SharkUtilities.createApplicationMappingTransaction();
99          retVal = getAllApplications(t,pkgId);
100          //SharkUtilities.commitMappingTransaction(t);
101
} catch (RootException e) {
102          //SharkUtilities.rollbackMappingTransaction(t);
103
if (e instanceof BaseException)
104             throw (BaseException)e;
105          else
106             throw new BaseException(e);
107       } finally {
108          SharkUtilities.releaseMappingTransaction(t);
109       }
110       return retVal;
111    }
112
113    public ApplicationMap[] getAllApplications (ApplicationMappingTransaction t,String JavaDoc pkgId) throws BaseException {
114       if (pkgId==null) throw new BaseException("The package Id can't be null");
115       List JavaDoc al=getApplicationsFromPackage(pkgId);
116       ApplicationMap[] ams=new ApplicationMap[al.size()];
117       al.toArray(ams);
118       return ams;
119    }
120
121    public ApplicationMap[] getAllApplications (String JavaDoc pkgId,String JavaDoc pDefId) throws BaseException {
122       ApplicationMap[] retVal;
123       ApplicationMappingTransaction t = null;
124       try {
125          t = SharkUtilities.createApplicationMappingTransaction();
126          retVal = getAllApplications(t,pkgId,pDefId);
127          //SharkUtilities.commitMappingTransaction(t);
128
} catch (RootException e) {
129          //SharkUtilities.rollbackMappingTransaction(t);
130
if (e instanceof BaseException)
131             throw (BaseException)e;
132          else
133             throw new BaseException(e);
134       } finally {
135          SharkUtilities.releaseMappingTransaction(t);
136       }
137       return retVal;
138    }
139
140    public ApplicationMap[] getAllApplications (ApplicationMappingTransaction t,String JavaDoc pkgId,String JavaDoc pDefId) throws BaseException {
141       if (pkgId==null) throw new BaseException("The package Id can't be null");
142       List JavaDoc al=getApplicationsFromPackageProcess(pkgId,pDefId);
143       ApplicationMap[] ams=new ApplicationMap[al.size()];
144       al.toArray(ams);
145       return ams;
146    }
147
148    private List JavaDoc getApplicationsFromPackage (String JavaDoc pkgId) throws BaseException {
149       List JavaDoc al=new ArrayList JavaDoc();
150       org.enhydra.shark.xpdl.elements.Package pkg=
151          SharkEngineManager.getInstance().getXMLInterface().getPackageById(pkgId);
152       if (pkg==null) throw new BaseException("Can't find package with Id="+pkgId);
153       Applications aps=pkg.getApplications();
154       Iterator JavaDoc applications=aps.toElements().iterator();
155       while (applications.hasNext())
156       {
157          Application a=(Application)applications.next();
158          ApplicationMap am=SharkEngineManager
159             .getInstance()
160             .getObjectFactory()
161             .createApplicationMap();
162          am.setPackageId(pkgId);
163          am.setApplicationDefinitionId(a.getId());
164          al.add(am);
165       }
166       Iterator JavaDoc processes=((WorkflowProcesses)pkg.getWorkflowProcesses()).
167          toElements().iterator();
168       while (processes.hasNext())
169       {
170          String JavaDoc pDefId=((WorkflowProcess)processes.next()).getId();
171          al.addAll(getApplicationsFromPackageProcess(pkgId,pDefId));
172       }
173
174       return al;
175    }
176
177    private List JavaDoc getApplicationsFromPackageProcess (String JavaDoc pkgId,String JavaDoc pDefId) throws BaseException {
178       List JavaDoc al=new ArrayList JavaDoc();
179       org.enhydra.shark.xpdl.elements.Package pkg=
180          SharkEngineManager.getInstance().getXMLInterface().getPackageById(pkgId);
181       if (pkg==null) throw new BaseException("Can't find package with Id="+pkgId);
182       WorkflowProcess wp=pkg.getWorkflowProcesses().getWorkflowProcess(pDefId);
183       if (wp==null) throw new BaseException("Can't find process definition with Id="+pDefId+" in pkg "+pkgId);
184       Applications aps=wp.getApplications();
185       Iterator JavaDoc applications=aps.toElements().iterator();
186       while (applications.hasNext())
187       {
188          Application a=(Application)applications.next();
189          ApplicationMap am=SharkEngineManager
190             .getInstance()
191             .getObjectFactory()
192             .createApplicationMap();
193          am.setPackageId(pkgId);
194          am.setProcessDefinitionId(wp.getId());
195          am.setApplicationDefinitionId(a.getId());
196          al.add(am);
197       }
198       return al;
199    }
200
201    public String JavaDoc[] getDefinedToolAgents () throws BaseException {
202       String JavaDoc[] retVal;
203       ApplicationMappingTransaction t = null;
204       try {
205          t = SharkUtilities.createApplicationMappingTransaction();
206          retVal = getDefinedToolAgents(t);
207       } catch (RootException e) {
208          if (e instanceof BaseException)
209             throw (BaseException)e;
210          else
211             throw new BaseException(e);
212       } finally {
213          SharkUtilities.releaseMappingTransaction(t);
214       }
215       return retVal;
216    }
217
218    public String JavaDoc[] getDefinedToolAgents (ApplicationMappingTransaction t) throws BaseException {
219       ToolAgentFactory taf=SharkEngineManager.getInstance().getToolAgentFactory();
220       if (taf==null) throw new BaseException("Working without tool agents !");
221
222       SharkTransaction st=null;
223       try {
224          st = SharkUtilities.createTransaction();
225          List JavaDoc l=taf.getDefinedToolAgents(st);
226          String JavaDoc[] ata=new String JavaDoc[l.size()];
227          l.toArray(ata);
228          return ata;
229       } catch (Exception JavaDoc e) {
230          throw new BaseException(e);
231       } finally {
232          SharkUtilities.releaseTransaction(st);
233       }
234    }
235
236    public Map JavaDoc getToolAgentsInfo () throws BaseException {
237       Map JavaDoc retVal;
238       ApplicationMappingTransaction t = null;
239       try {
240          t = SharkUtilities.createApplicationMappingTransaction();
241          retVal = getToolAgentsInfo(t);
242       } catch (RootException e) {
243          if (e instanceof BaseException)
244             throw (BaseException)e;
245          else
246             throw new BaseException(e);
247       } finally {
248          SharkUtilities.releaseMappingTransaction(t);
249       }
250       return retVal;
251    }
252
253    public Map JavaDoc getToolAgentsInfo (ApplicationMappingTransaction t) throws BaseException {
254       ToolAgentFactory taf=SharkEngineManager.getInstance().getToolAgentFactory();
255       if (taf==null) throw new BaseException("Working without tool agents !");
256
257       SharkTransaction st=null;
258       try {
259          st = SharkUtilities.createTransaction();
260          List JavaDoc l=taf.getDefinedToolAgents(st);
261          Map JavaDoc m=new HashMap JavaDoc();
262          Iterator JavaDoc it=l.iterator();
263          while (it.hasNext()) {
264             String JavaDoc tan=(String JavaDoc)it.next();
265             m.put(tan,taf.createToolAgent(st,tan).getInfo(st));
266          }
267          return m;
268       } catch (Exception JavaDoc e) {
269          throw new BaseException(e);
270       } finally {
271          SharkUtilities.releaseTransaction(st);
272       }
273    }
274
275    public String JavaDoc getToolAgentInfo (String JavaDoc toolAgentFullClassName) throws BaseException {
276       String JavaDoc retVal;
277       ApplicationMappingTransaction t = null;
278       try {
279          t = SharkUtilities.createApplicationMappingTransaction();
280          retVal = getToolAgentInfo(t,toolAgentFullClassName);
281       } catch (RootException e) {
282          if (e instanceof BaseException)
283             throw (BaseException)e;
284          else
285             throw new BaseException(e);
286       } finally {
287          SharkUtilities.releaseMappingTransaction(t);
288       }
289       return retVal;
290    }
291
292    public String JavaDoc getToolAgentInfo (ApplicationMappingTransaction t,String JavaDoc toolAgentFullClassName) throws BaseException {
293       ToolAgentFactory taf=SharkEngineManager.getInstance().getToolAgentFactory();
294       if (taf==null) throw new BaseException("Working without tool agents !");
295
296       SharkTransaction st=null;
297       try {
298          st = SharkUtilities.createTransaction();
299          return taf.createToolAgent(st,toolAgentFullClassName).getInfo(st);
300       } catch (Exception JavaDoc e) {
301          throw new BaseException(e);
302       } finally {
303          SharkUtilities.releaseTransaction(st);
304       }
305    }
306
307    public void addApplicationMapping( ApplicationMap am ) throws BaseException {
308       ApplicationMappingTransaction t = null;
309       try {
310          t = SharkUtilities.createApplicationMappingTransaction();
311          addApplicationMapping(t, am);
312          SharkUtilities.commitMappingTransaction(t);
313       } catch (RootException e) {
314          SharkUtilities.rollbackMappingTransaction(t,e);
315          if (e instanceof BaseException)
316             throw (BaseException)e;
317          else
318             throw new BaseException(e);
319       } finally {
320          SharkUtilities.releaseMappingTransaction(t);
321       }
322    }
323
324    public void addApplicationMapping( ApplicationMappingTransaction trans, ApplicationMap am ) throws BaseException {
325       if (pmanager==null) throw new BaseException("The shark is configured to work without internal implementation of this API");
326       try {
327          pmanager.saveApplicationMapping(
328             trans,
329             this.createPersistentApplicationMapFromClientMap( am ) );
330       } catch ( Exception JavaDoc e ) {
331          throw new BaseException(e);
332       }
333    }
334
335    public ApplicationMap getApplicationMapping(
336       String JavaDoc pkgId,
337       String JavaDoc pDefId,
338       String JavaDoc appDefId) throws BaseException {
339       ApplicationMap retVal;
340       ApplicationMappingTransaction t = null;
341       try {
342          t = SharkUtilities.createApplicationMappingTransaction();
343          retVal = getApplicationMapping(
344             t,
345             pkgId,
346             pDefId,
347             appDefId
348          );
349       } catch (RootException e) {
350          if (e instanceof BaseException)
351             throw (BaseException)e;
352          else
353             throw new BaseException(e);
354       } finally {
355          SharkUtilities.releaseMappingTransaction(t);
356       }
357       return retVal;
358    }
359
360    public ApplicationMap getApplicationMapping(
361       ApplicationMappingTransaction trans,
362       String JavaDoc pkgId,
363       String JavaDoc pDefId,
364       String JavaDoc appDefId ) throws BaseException
365    {
366       if (pmanager==null) throw new BaseException("The shark is configured to work without internal implementation of this API");
367       try
368       {
369          ApplicationMap am =
370             createClientMapFromPersistentApplicationMap(
371             pmanager.getApplicationMap( trans, pkgId, pDefId, appDefId ) );
372          return am;
373       }
374       catch ( Exception JavaDoc e )
375       {
376          throw new BaseException(e);
377       }
378    }
379
380    public void removeApplicationMapping(
381       String JavaDoc packageId,
382       String JavaDoc processDefinitionId,
383       String JavaDoc applicationId ) throws BaseException
384    {
385       ApplicationMap retVal;
386       ApplicationMappingTransaction t = null;
387       try {
388          t = SharkUtilities.createApplicationMappingTransaction();
389          removeApplicationMapping(
390             t,
391             packageId,
392             processDefinitionId,
393             applicationId);
394          SharkUtilities.commitMappingTransaction(t);
395       } catch (RootException e) {
396          SharkUtilities.rollbackMappingTransaction(t,e);
397          if (e instanceof BaseException)
398             throw (BaseException)e;
399          else
400             throw new BaseException(e);
401       } finally {
402          SharkUtilities.releaseMappingTransaction(t);
403       }
404    }
405
406    public void removeApplicationMapping(
407       ApplicationMappingTransaction trans,
408       String JavaDoc packageId,
409       String JavaDoc processDefinitionId,
410       String JavaDoc applicationId ) throws BaseException
411    {
412       if (pmanager==null) throw new BaseException("The shark is configured to work without internal implementation of this API");
413       try
414       {
415          pmanager.deleteApplicationMapping(
416             trans,
417             packageId,
418             processDefinitionId,
419             applicationId );
420       }
421       catch ( Exception JavaDoc e )
422       {
423          throw new BaseException(e);
424       }
425    }
426
427    public ApplicationMap[] getApplicationMappings() throws BaseException {
428       ApplicationMap[] retVal;
429       ApplicationMappingTransaction t = null;
430       try {
431          t = SharkUtilities.createApplicationMappingTransaction();
432          retVal = getApplicationMappings(t);
433          SharkUtilities.commitMappingTransaction(t);
434       } catch (RootException e) {
435          SharkUtilities.rollbackMappingTransaction(t,e);
436          if (e instanceof BaseException)
437             throw (BaseException)e;
438          else
439             throw new BaseException(e);
440       } finally {
441          SharkUtilities.releaseMappingTransaction(t);
442       }
443       return retVal;
444    }
445
446    public ApplicationMap[] getApplicationMappings(ApplicationMappingTransaction trans) throws BaseException
447    {
448       if (pmanager==null) throw new BaseException("The shark is configured to work without internal implementation of this API");
449       try
450       {
451          List JavaDoc clientMapList = new ArrayList JavaDoc();
452          List JavaDoc persistentMapList = pmanager.getAllApplicationMappings(trans);
453          for ( int i = 0; i < persistentMapList.size(); i++ )
454          {
455             clientMapList.add( createClientMapFromPersistentApplicationMap(
456                                                                              ( org.enhydra.shark.api.internal.appmappersistence.ApplicationMap )
457                                     persistentMapList.get( i )
458                               ) );
459          }
460          ApplicationMap[] ama=new ApplicationMap[clientMapList.size()];
461          clientMapList.toArray(ama);
462          return ama;
463       }
464       catch ( Exception JavaDoc e )
465       {
466          throw new BaseException(e);
467       }
468    }
469
470    public ApplicationMap createApplicationMap()
471    {
472       return SharkEngineManager
473          .getInstance()
474          .getObjectFactory()
475          .createApplicationMap();
476    }
477
478    public ApplicationMap createApplicationMap(ApplicationMappingTransaction t) {
479       return SharkEngineManager
480          .getInstance()
481          .getObjectFactory()
482          .createApplicationMap();
483    }
484
485    private org.enhydra.shark.api.internal.appmappersistence.ApplicationMap
486       createPersistentApplicationMapFromClientMap( ApplicationMap am ) throws BaseException
487    {
488       if (pmanager==null) throw new BaseException("The shark is configured to work without internal implementation of this API");
489       org.enhydra.shark.api.internal.appmappersistence.ApplicationMap ampersistent = null;
490       try {
491          ampersistent = pmanager.createApplicationMap();
492          ampersistent.setUsername( am.getUsername() );
493          ampersistent.setPassword( am.getPassword() );
494          ampersistent.setApplicationDefinitionId( am.getApplicationDefinitionId() );
495          ampersistent.setApplicationMode( am.getApplicationMode() );
496          ampersistent.setApplicationName( am.getApplicationName() );
497          ampersistent.setProcessDefinitionId(am.getProcessDefinitionId());
498          ampersistent.setToolAgentClassName( am.getToolAgentClassName() );
499          ampersistent.setPackageId( am.getPackageId() );
500       }catch(Exception JavaDoc e) {
501          throw new BaseException(e);
502       }
503       return ampersistent;
504    }
505
506
507    private ApplicationMap createClientMapFromPersistentApplicationMap(
508       org.enhydra.shark.api.internal.appmappersistence.ApplicationMap am )
509    {
510       if (am==null) return null;
511       ApplicationMap
512          ampClient = createApplicationMap();
513       ampClient.setUsername( am.getUsername() );
514       ampClient.setPassword( am.getPassword() );
515       ampClient.setApplicationDefinitionId( am.getApplicationDefinitionId() );
516       ampClient.setApplicationMode( am.getApplicationMode() );
517       ampClient.setApplicationName( am.getApplicationName() );
518       ampClient.setProcessDefinitionId( am.getProcessDefinitionId() );
519       ampClient.setToolAgentClassName( am.getToolAgentClassName() );
520       ampClient.setPackageId( am.getPackageId() );
521       return ampClient;
522    }
523
524 }
525
Popular Tags