1 /*
2  * Copyright (C) 2003  robert rowntree
3 
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8 
9  * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 * GNU General Public License for more details.
13
14 * ref. http://www.gnu.org/copyleft/gpl.html
15*/
16/**
17 * Main responsibilites:
18 *  1. Collaborate with cls=FileEventClient to manage command-line args.
19 *      Semantic is that the arg values will collaborate with "listFiles".
20 *      
21 *  2. Create a list of files that represent the data that was just
22 *      produced and that needs to be published.
23 *
24 * Details:  
25 * decide where to get the correct cmdLine args..
26 * Arg values will then be used to create a fileList with a call to
27 * "setFiles()". SetFiles activity collaborates with FileSelector class.
28 * 
29 * Sample call:
30 *      this.setFiles(FileSelector.getFiles(args[n], args[n+1])) 
31 *  
32 * @param args - see "this.init()"
33 */
34/*rev pass topicnm thru to the call on WebService*/
35/*rev by rcr 4/1/04 for new polling that needs sessionID property*/
36/* Java class "FileTopicBean.java" generated from Poseidon for UML.
37 *  Poseidon for UML is developed by <A HREF="http://www.gentleware.com">Gentleware</A>.
38 *  Generated with <A HREF="http://jakarta.apache.org/velocity/">velocity</A> template engine.
39 */
40package com.borneo.po.event;
41
42import java.util.Collection;
43import java.util.StringTokenizer;
44import java.util.Vector;
45
46import org.apache.log4j.Logger;
47import org.apache.log4j.PropertyConfigurator;
48
49import com.borneo.util.FileSelector;
50import com.borneo.util.POProperty;
51/**
52 */
53public class FileTopicType implements FileTopic {
54
55  ///////////////////////////////////////
56  // attributes
57  protected static Logger log;
58
59/**
60 */
61    private Collection file; // of type String
62    private String sessionId; // to Id specific cdc for polling requests
63    private String topic; 
64    ///////////////////////////////////////
65    // operations    
66    public FileTopicType (){
67    }
68    public FileTopicType (String arg, Collection collection){
69        setTopic(arg);
70        setFiles(collection);
71    }
72
73    public FileTopicType (String[] args){
74
75        if (this.init(args)){
76            if(log.isDebugEnabled() ){POProperty.list(System.out);}
77        } 
78        else {
79            log.error("usage : req'd arg missing (arg=data.source.filename or arg=data.source.regex");
80        } 
81    }
82    /**
83     * Property values affect "source" and "target" of the data transport.
84     * <p>A client request to create an event wrapper for some new data
85     * has presented here. Before that data can be wrapped in an event
86     * and posted, some config work needs doing.</p><p>
87     * Config properties for the file exchange can come from either
88     * Command Line or more static version in the config file.
89     * The client's command line args may be used to override
90     * Configuration key/value pairs contained in the config
91     * file. Any of the "source" elements below may be included
92     * on the cmdLne using "key=value" expressions. 
93     *   Sample cmdLne arg :
94     *      $java clsNme  data.source.lastfile=2003-07-29T15:23:30.609-08:00
95     * 
96     *   Semantic description of arg above :
97     *      when selecting files from the sourcedirectory, using
98     *      either filename or pattern for the file names, apply
99     *      a "TimeStamp" filter to each prospective file. Pass the
00     *      file only if it has a Timestamp indicating that it was
01     *      created SINCE the value for XML Data Type=DATE indicated 
02     *      by valueOf "lastfile" element.
03     * 
04     * * * * * *   XML SCHEMA - source/target for data exchange * * * 
05             <fileAssociation>
06                <source>
07                    <filename>my_purchase_order.txt</filename>
08                    <sourcedirectory>C:\intranet\dist\work\data</sourcedirectory>
09                    <regex>walmart_po.*\.txt</regex>
10                    <lastfile>2003-07-29T15:23:30.609-08:00</lastfile>
11                </source>
12                <target>
13                    <targetdirectory>data</targetdirectory>
14                </target>
15            </fileAssociation>
16     * * * * * *   XML SCHEMA - end 
17     * * * * * * * * * * * * * * * * * * * * * * * * * * **      
18     * @param args - command line args in the list below may be used
19     *  to overide configuration file properties. 
20     *  Note :  filename or regex is REQUIRED
21     *          sourcedirectory is REQUIRED 
22     * * * * * * * * * * * * * * * * * * * * * * * * * * **
23     * * * * * *   CMD LINE Args - source/target for data exchange * * * 
24        data.source.filename=
25        data.source.sourcedirectory=J:\\po_1_2\\config
26        data.source.regex=
27        data.source.lastfile=
28        data.topic.name=mytest
29        data.topic.action=publish
30
31     * @return
32     */
33    private boolean init(String[] args){
34
35        for (int i=0; i < args.length;i++){
36            StringTokenizer myst = new StringTokenizer (args[i] ,"=",false);                                
37            if ( myst.countTokens() > 1 ){
38                this.syncProperty(myst.nextToken(),myst.nextToken());
39            }                                      
40        }
41        //TODO Demo Polling is using "source" not "target" as destination??
42        // this should be fixd so polling writes transmissions to "target"
43        if ( 
44            POProperty.get(
45                "data.source.sourcedirectory"
46            ).equalsIgnoreCase("") 
47            ||
48            POProperty.get(
49                    "data.source.sourcedirectory"
50                ).equalsIgnoreCase("null")
51        ){POProperty.set(
52            "data.source.sourcedirectory"
53            , System.getProperty("user.dir")
54        );
55        }
56
57        if ( 
58            POProperty.get(
59                "data.topic.name"
60            ).equalsIgnoreCase("")          
61        ){POProperty.set(
62            "data.topic.name"
63            , "mytest"
64        );
65        }
66        this.setTopic(
67            POProperty.get("data.topic.name")           
68        );
69        // 3/31/04 when subscribing or polling , now need the sessionID
70        if ( 
71            POProperty.get(
72                "data.topic.action"
73            ).equalsIgnoreCase("subscribe")         
74        ){
75            this.setSessionId(POProperty.get("data.subscription.refid"));
76        }       
77                
78        
79        // slash in first position for path that is "relative"
80        //   to "user.dir" where the app. was launched
81        // this means that my value in sourcedirectory is NOT
82        //   an absolute path to the data that will be published
83        if ( 
84            POProperty.get(
85                "data.source.sourcedirectory"
86            ).startsWith("/")           
87        ){                                              
88            POProperty.set(
89                "data.source.sourcedirectory"
90                , 
91                System.getProperty("user.dir")
92                + System.getProperty("file.separator")
93                + POProperty.get(
94                    "data.source.sourcedirectory"
95                ).substring(
96                    1, 
97                    POProperty.get(
98                        "data.source.sourcedirectory"
99                    ).length()
00                )   
01            );      
02        }
03        // props mods 9/17 dropped big section
04        // pushed intelligence into FileSelector class meth=
05        // "getFilesfromProps()"
06        // polling mods 5/4/04 
07        //    data.topic.action="subscribe" bypass call to setFiles()
08        if ( 
09                POProperty.get(
10                    "data.topic.action"
11                ).equalsIgnoreCase("publish")           
12            ){
13            this.setFiles(new Vector());
14            if(FileSelector.getFilesFromProps().size() > 0){
15                this.setFiles(FileSelector.getFilesFromProps());
16            }
17        }
18        return true;
19/*      
20        if (
21            ! POProperty.get("data.source.regex").equalsIgnoreCase("")      
22        &&       
23            POProperty.get("data.source.filename").equalsIgnoreCase("")
24        &&
25            ! POProperty.get("data.source.lastfile").equalsIgnoreCase("")
26        ) 
27            {       
28            this.setFiles(
29                FileSelector.getFiles(
30                    POProperty.get(
31                        "data.source.sourcedirectory"
32                    )
33                    , POProperty.get(
34                        "data.source.regex"
35                    )
36                    , POProperty.get(
37                        "data.source.lastfile"
38                    )
39                )
40            );
41 
42        return true;
43        } else
44//      TODO this isn't really correct to allow a regex as valuOF "filename"
45        { if(           
46            POProperty.get("data.source.regex").equalsIgnoreCase("")        
47        &&       
48            ! POProperty.get("data.source.filename").equalsIgnoreCase("")           
49            ){
50                this.setFiles(
51                    FileSelector.getFiles(
52                        POProperty.get(
53                            "data.source.sourcedirectory"
54                        )
55                        , POProperty.get(
56                            "data.source.filename"
57                        )
58                    )
59                );              
60        return true;        
61        }   
62        }       
63                        
64        if (
65            ! POProperty.get("data.source.regex").equalsIgnoreCase("")      
66        &&       
67            POProperty.get("data.source.filename").equalsIgnoreCase("")
68        ) 
69            {       
70            this.setFiles(
71                FileSelector.getFiles(
72                    POProperty.get(
73                        "data.source.sourcedirectory"
74                    )
75                    , POProperty.get(
76                        "data.source.regex"
77                    )
78                )
79            );
80 
81        return true;
82        } else
83//      TODO this isn't really correct to allow a regex as valuOF "filename"
84        { if(           
85            POProperty.get("data.source.regex").equalsIgnoreCase("")        
86        &&       
87            ! POProperty.get("data.source.filename").equalsIgnoreCase("")           
88            ){
89                this.setFiles(
90                    FileSelector.getFiles(
91                        POProperty.get(
92                            "data.source.sourcedirectory"
93                        )
94                        , POProperty.get(
95                            "data.source.filename"
96                        )
97                    )
98                );              
99        return true;        
00        } else{
01            return false;   
02        }   
03        }
04    */  
05
06    }
07    private void syncProperty (String key, String val){
08        POProperty.set(key,val);
09        // TODO see more robust reconciliation of what may 
10        //    present via CmdLine versus what properties file
11        //    has for values of the same "keys"
12        //    sample belo from one of the Axis classes by Apache
13/*      if(name == null || value == null)
14                 return;
15             if(name.equals("javax.xml.rpc.security.auth.username"))
16             {
17                 if(!(value instanceof String))
18                     throw new IllegalArgumentException(JavaUtils.getMessage("badProp00", new String[] {
19                         name, "java.lang.String", value.getClass().getName()
20                     }));
21                 setUsername((String)value);
22             } else
23             if(name.equals("javax.xml.rpc.security.auth.password")){}
24*/
25    }
26
27/**
28 * <p>
29 * Does ...
30 * </p><p>
31 * 
32 * @return a String with ...
33 * </p>
34 */
35    public String getTopic() {        
36//        return topic;
37// delegate
38        return POProperty.get("data.topic.name");
39    } // end getTopic        
40
41/**
42 * <p>
43 * Does ...
44 * </p><p>
45 * 
46 * </p><p>
47 * 
48 * @param _topic ...
49 * </p>
50 */
51    public void setTopic(String _topic) {        
52//        topic = _topic;
53// delegate
54        POProperty.set(
55            "data.topic.name"
56            ,_topic
57        );
58
59    } // end setTopic        
60
61    public void setSessionId(String _sessionId){
62        POProperty.set(
63                "data.subscription.refid"
64                ,_sessionId
65            );  
66    }
67    public String getSessionId(){
68        return POProperty.get("data.subscription.refid");
69    }
70/**
71 * <p>
72 * Does ...
73 * </p><p>
74 * 
75 * @return a Collection with ...
76 * </p>
77 */
78    public Collection getFiles() {        
79        return file;
80    } // end getFiles        
81
82/**
83 * <p>
84 * Does ...
85 * </p><p>
86 * 
87 * </p><p>
88 * 
89 * @param _file ...
90 * </p>
91 */
92    public void setFiles(Collection _file) {        
93        file = _file;
94    } // end setFiles        
95
96/**
97 * <p>
98 * Does ...
99 * </p><p>
00 * 
01 * </p><p>
02 * 
03 * @param _file ...
04 * </p>
05 */
06    public void addFile(String _file) {        
07        if (! file.contains(_file)) file.add(_file);
08    } // end addFile        
09
10/**
11 * <p>
12 * Does ...
13 * </p><p>
14 * 
15 * </p><p>
16 * 
17 * @param _file ...
18 * </p>
19 */
20    public void removeFile(String _file) {        
21        file.remove(_file);
22    } // end removeFile 
23    
24    static 
25        {
26            log = Logger.getLogger((com.borneo.po.event.FileTopicType.class).getName());
27            PropertyConfigurator.configure(com.borneo.po.event.FileTopicType.class.getClassLoader().getResource(
28                "config/log4j.properties"
29            ));
30        }       
31
32} // end FileTopicBean
33
34
35
36