View Javadoc

1   /*$Id: FileWorkflowStore.java,v 1.3 2004/04/14 13:02:57 nw Exp $
2    * Created on 09-Mar-2004
3    *
4    * Copyright (C) AstroGrid. All rights reserved.
5    *
6    * This software is published under the terms of the AstroGrid 
7    * Software License version 1.2, a copy of which has been included 
8    * with this distribution in the LICENSE.txt file.  
9    *
10  **/
11  package org.astrogrid.portal.workflow.impl;
12  
13  import org.astrogrid.community.beans.v1.Account;
14  import org.astrogrid.io.Piper;
15  import org.astrogrid.portal.workflow.intf.WorkflowInterfaceException;
16  import org.astrogrid.portal.workflow.intf.WorkflowStore;
17  import org.astrogrid.workflow.beans.v1.Workflow;
18  
19  import org.apache.commons.logging.Log;
20  import org.apache.commons.logging.LogFactory;
21  import org.exolab.castor.xml.CastorException;
22  
23  import java.io.File;
24  import java.io.FileReader;
25  import java.io.FileWriter;
26  import java.io.FilenameFilter;
27  import java.io.IOException;
28  import java.io.Reader;
29  import java.io.StringWriter;
30  import java.io.Writer;
31  import java.net.URLDecoder;
32  import java.net.URLEncoder;
33  import java.util.ArrayList;
34  import java.util.List;
35  
36  /*** Implementation of a workflow store based on a local filesystem 
37   * @author Noel Winstanley nw@jb.man.ac.uk 09-Mar-2004
38   *  @deprecated - old implementation of the workflow store, before interface changes.
39   */
40  public class FileWorkflowStore/* implements WorkflowStore */{
41      private static final Log log = LogFactory.getLog(FileWorkflowStore.class);
42      /*** Construct a new FileWorkflowStore
43       * 
44       */
45      public FileWorkflowStore(File baseDir) {
46          log.info ("Creating File-based workflow store at " + baseDir.getAbsolutePath());
47          if (!baseDir.exists()) {
48              baseDir.mkdirs();
49          }
50          this.baseDir = baseDir;
51      }
52      protected final File baseDir;
53      private File mkFile(Account acc,String name) {
54          StringBuffer filename = new StringBuffer();
55          filename.append(acc.getName());
56          filename.append('@');
57          filename.append(acc.getCommunity());
58          filename.append('#');
59          filename.append(name);
60          return new File(baseDir,URLEncoder.encode(filename.toString()));
61      }
62      
63      private static final String WORKFLOW_SUFFIX = "-workflow.xml";
64      private static final String QUERY_SUFFIX = "-query.xml";
65      
66      private File mkWorkflowFile(Account acc, String name) {
67          return mkFile(acc, name + WORKFLOW_SUFFIX);
68      }
69      private File mkQueryFile(Account acc,String name) {
70          return mkFile(acc,name+QUERY_SUFFIX);
71      }
72      /***
73       * @see org.astrogrid.portal.workflow.intf.WorkflowStore#deleteWorkflow(org.astrogrid.community.beans.v1.Account, java.lang.String)
74       */
75      public void deleteWorkflow(Account acc, String name) throws WorkflowInterfaceException {
76              File f = mkWorkflowFile(acc,name);
77              f.delete();              
78      }
79      /***
80       * @see org.astrogrid.portal.workflow.intf.WorkflowStore#readQuery(org.astrogrid.community.beans.v1.Account, java.lang.String)
81       */
82      public String readQuery(Account acc, String name) throws WorkflowInterfaceException {
83          try {
84          File f = mkQueryFile(acc,name);
85          Reader reader = new FileReader(f);
86          Writer writer = new StringWriter();
87          Piper.pipe(reader,writer);
88          reader.close();
89          writer.close();
90          return writer.toString();
91          } catch (IOException e) {
92              throw new WorkflowInterfaceException(e);
93          } 
94      }
95      /***
96       * @see org.astrogrid.portal.workflow.intf.WorkflowStore#listQueries(org.astrogrid.community.beans.v1.Account)
97       */
98      public String[] listQueries(Account acc) throws WorkflowInterfaceException {
99          final List results = new ArrayList();
100         final String prefix = URLEncoder.encode(acc.getName() + "@" + acc.getCommunity() + "#");
101         baseDir.list(new FilenameFilter() { // don't care about results. am using it to iterate through directory contents..
102 
103             public boolean accept(File dir, String name) {
104                 if (name.startsWith(prefix) && name.endsWith(QUERY_SUFFIX)) {
105                     String originalName = name.substring(prefix.length(),name.indexOf(QUERY_SUFFIX));
106                     results.add(URLDecoder.decode(originalName));
107                 }
108                 return false;
109             }
110         });
111         return (String[])results.toArray(new String[]{});
112     }
113     /***
114      * @see org.astrogrid.portal.workflow.intf.WorkflowStore#readWorkflow(org.astrogrid.community.beans.v1.Account, java.lang.String)
115      */
116     public Workflow readWorkflow(Account acc, String name) throws WorkflowInterfaceException {
117         try {
118         File f = mkWorkflowFile(acc,name);
119         Reader reader = new FileReader(f);
120         Workflow wf = Workflow.unmarshalWorkflow(reader);
121         reader.close();
122         return wf;
123         } catch (IOException e) {
124             throw new WorkflowInterfaceException(e);
125         }  catch (CastorException e) {
126             throw new WorkflowInterfaceException(e);
127         }
128     }
129     /***
130      * @see org.astrogrid.portal.workflow.intf.WorkflowStore#listWorkflows(org.astrogrid.community.beans.v1.Account)
131 
132      */
133     public String[] listWorkflows(Account acc) throws WorkflowInterfaceException {
134         final List results = new ArrayList();
135         final String prefix = URLEncoder.encode(acc.getName() + "@" + acc.getCommunity() + "#");
136         baseDir.list(new FilenameFilter() { // don't care about results. am using it to iterate through directory contents..
137 
138             public boolean accept(File dir, String name) {
139                 if (name.startsWith(prefix) && name.endsWith(WORKFLOW_SUFFIX)) {
140                     String originalName = name.substring(prefix.length(),name.indexOf(WORKFLOW_SUFFIX));
141                     results.add(URLDecoder.decode(originalName));
142                 }
143                 return false;
144             }
145         });
146         return (String[])results.toArray(new String[]{});
147     }
148     /***
149      * @see org.astrogrid.portal.workflow.intf.WorkflowStore#saveWorkflow(org.astrogrid.community.beans.v1.Account, org.astrogrid.workflow.beans.v1.Workflow)
150      */
151     public void saveWorkflow(Account acc, Workflow workflow) throws WorkflowInterfaceException {
152         try {
153             File f = mkWorkflowFile(acc,workflow.getName());
154             Writer w = new FileWriter(f);
155             workflow.marshal(w);
156             w.close();
157         } catch (IOException e) {
158             throw new WorkflowInterfaceException(e);
159         } catch (CastorException e) {
160             throw new WorkflowInterfaceException(e);
161         }
162     }
163 }
164 
165 
166 /* 
167 $Log: FileWorkflowStore.java,v $
168 Revision 1.3  2004/04/14 13:02:57  nw
169 cut down workflow store interface. now to implement it.
170 
171 Revision 1.2  2004/03/11 13:53:36  nw
172 merged in branch bz#236 - implementation of interfaces
173 
174 Revision 1.1.2.2  2004/03/11 13:36:10  nw
175 added implementations for the workflow interfaces
176 
177 Revision 1.1.2.1  2004/03/09 17:41:59  nw
178 created a bunch of implementations,
179  
180 */